[FFmpeg-devel] [PATCH] avcodec: add QDMC decoder

Paul B Mahol onemda at gmail.com
Thu Jan 5 21:13:38 EET 2017


On 1/5/17, James Almer <jamrial at gmail.com> wrote:
> On 1/5/2017 3:34 PM, Paul B Mahol wrote:
>> diff --git a/libavcodec/qdmc.c b/libavcodec/qdmc.c
>> new file mode 100644
>> index 0000000..5559db3
>> --- /dev/null
>> +++ b/libavcodec/qdmc.c
>> @@ -0,0 +1,817 @@
>> +/*
>> + * QDMC compatible decoder
>> + * Copyright (c) 2017 Paul B Mahol
>> + *
>> + * This file is part of FFmpeg.
>> + *
>> + * FFmpeg is free software; you can redistribute it and/or
>> + * modify it under the terms of the GNU Lesser General Public
>> + * License as published by the Free Software Foundation; either
>> + * version 2.1 of the License, or (at your option) any later version.
>> + *
>> + * FFmpeg is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> + * Lesser General Public License for more details.
>> + *
>> + * You should have received a copy of the GNU Lesser General Public
>> + * License along with FFmpeg; if not, write to the Free Software
>> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
>> 02110-1301 USA
>> + */
>> +
>> +#include <math.h>
>> +#include <stddef.h>
>> +#include <stdio.h>
>> +
>> +#define BITSTREAM_READER_LE
>> +
>> +#include "libavutil/channel_layout.h"
>> +
>> +#include "avcodec.h"
>> +#include "get_bits.h"
>> +#include "internal.h"
>> +#include "fft.h"
>> +
>> +typedef struct QDMCTone {
>> +    uint8_t mode;
>> +    uint8_t phase;
>> +    uint8_t offset;
>> +    int16_t freq;
>> +    int16_t amplitude;
>> +} QDMCTone;
>> +
>> +typedef struct QDMCContext {
>> +    AVCodecContext *avctx;
>> +
>> +    uint8_t frame_bits;
>> +    int band_index;
>> +    int frame_size;
>> +    int subframe_size;
>> +    int fft_offset;
>> +    int buffer_offset;
>> +    float *buffer_ptr;
>> +    int nb_channels;
>> +
>> +    int group_size;
>> +    int checksum_size;
>> +
>> +    uint8_t noise[2][19][16];
>> +    QDMCTone tones[5][8192];
>> +    int nb_tones[5];
>> +    int cur_tone[5];
>> +    float alt_sin[5][31];
>> +    float fft_buffer[4][8192 * 2];
>> +    float noise2_buffer[4096 * 2];
>> +    float noise_buffer[4096 * 2];
>> +    int rndval;
>> +
>> +    DECLARE_ALIGNED(32, FFTComplex, cmplx)[2][512];
>> +    float buffer[2 * 32768];
>> +
>> +    FFTContext fft_ctx;
>> +} QDMCContext;
>> +
>> +static float sin_table[512];
>> +static VLC vtable[6];
>
> Why are these not part of QDMCContext?

They are static, never change, so having it part of context wastes
memory, with duplicate tables for each instance of decoder.

>
> [...]
>
>> +
>> +static av_cold int qdmc_init_static_data(QDMCContext *s)
>> +{
>> +    static int done;
>> +    int i, ret;
>> +
>> +    if (done)
>> +        return 0;
>> +
>> +    ret = ff_init_vlc_sparse(&vtable[0], 12,
>> FF_ARRAY_ELEMS(noise_value_bits),
>> +                             noise_value_bits, 1, 1, noise_value_codes,
>> 2, 2, noise_value_symbols, 1, 1, INIT_VLC_LE);
>> +    if (ret < 0)
>> +        return ret;
>> +    ret = ff_init_vlc_sparse(&vtable[1], 10,
>> FF_ARRAY_ELEMS(noise_segment_length_bits),
>> +                             noise_segment_length_bits, 1, 1,
>> noise_segment_length_codes, 2, 2,
>> +                             noise_segment_length_symbols, 1, 1,
>> INIT_VLC_LE);
>> +    if (ret < 0)
>> +        return ret;
>> +    ret = ff_init_vlc_sparse(&vtable[2], 13,
>> FF_ARRAY_ELEMS(amplitude_bits),
>> +                             amplitude_bits, 1, 1, amplitude_codes, 2, 2,
>> NULL, 0, 0, INIT_VLC_LE);
>> +    if (ret < 0)
>> +        return ret;
>> +    ret = ff_init_vlc_sparse(&vtable[3], 18,
>> FF_ARRAY_ELEMS(freq_diff_bits),
>> +                             freq_diff_bits, 1, 1, freq_diff_codes, 4, 4,
>> NULL, 0, 0, INIT_VLC_LE);
>> +    if (ret < 0)
>> +        return ret;
>> +    ret = ff_init_vlc_sparse(&vtable[4], 8,
>> FF_ARRAY_ELEMS(amplitude_diff_bits),
>> +                             amplitude_diff_bits, 1, 1,
>> amplitude_diff_codes, 1, 1, NULL, 0, 0, INIT_VLC_LE);
>> +    if (ret < 0)
>> +        return ret;
>> +    ret = ff_init_vlc_sparse(&vtable[5], 6,
>> FF_ARRAY_ELEMS(phase_diff_bits),
>> +                             phase_diff_bits, 1, 1, phase_diff_codes, 1,
>> 1, NULL, 0, 0, INIT_VLC_LE);
>> +    if (ret < 0)
>> +        return ret;
>> +
>> +    for (i = 0; i < 512; i++)
>> +        sin_table[i] = sin(2 * i * M_PI * 0.001953125);
>
> These constants should be float.

ok

>
>> +
>> +    done = 1;
>> +
>> +    return 0;
>> +}
>
> [...]
>
>> +static av_cold int qdmc_decode_init(AVCodecContext *avctx)
>> +{
>> +    QDMCContext *s = avctx->priv_data;
>> +    uint8_t *extradata;
>> +    int extradata_size, fft_size, fft_order, ret, size, g, j, x;
>> +
>> +    if ((ret = qdmc_init_static_data(s)) < 0)
>> +        return ret;
>> +
>> +    if (!avctx->extradata || (avctx->extradata_size < 48)) {
>> +        av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +
>> +    extradata      = avctx->extradata;
>> +    extradata_size = avctx->extradata_size;
>> +
>> +    while (extradata_size > 8) {
>> +        if (!memcmp(extradata, "frmaQDMC", 8))
>> +            break;
>> +        extradata++;
>> +        extradata_size--;
>
> Use bytestream.h instead. It will simplify this function a lot.

Will do.

>
>> +    }
>> +
>> +    if (extradata_size < 12) {
>> +        av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
>> +               extradata_size);
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +
>> +    if (memcmp(extradata, "frmaQDMC", 8)) {
>> +        av_log(avctx, AV_LOG_ERROR, "invalid headers, QDMC not found\n");
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +
>> +    extradata += 8;
>> +    extradata_size -= 8;
>> +
>> +    size = AV_RB32(extradata);
>> +    extradata += 4;
>> +
>> +    if (size > extradata_size) {
>> +        av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i <
>> %i\n",
>> +               extradata_size, size);
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +
>> +    if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
>> +        av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting
>> QDCA\n");
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +    extradata += 8;
>> +
>> +    avctx->channels = s->nb_channels = AV_RB32(extradata);
>> +    extradata += 4;
>> +    if (s->nb_channels <= 0 || s->nb_channels > 2) {
>> +        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +    avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
>> +                                                   AV_CH_LAYOUT_MONO;
>> +
>> +    avctx->sample_rate = AV_RB32(extradata);
>> +    extradata += 4;
>> +
>> +    avctx->bit_rate = AV_RB32(extradata);
>> +    extradata += 4;
>> +
>> +    s->group_size = AV_RB32(extradata);
>> +    extradata += 4;
>> +
>> +    fft_size = AV_RB32(extradata);
>> +    fft_order = av_log2(fft_size) + 1;
>> +    extradata += 4;
>> +
>> +    s->checksum_size = AV_RB32(extradata);
>> +    if (s->checksum_size >= 1U << 28) {
>> +        av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n",
>> s->checksum_size);
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +
>> +    if (avctx->sample_rate >= 32000) {
>> +        x = 28000;
>> +        s->frame_bits = 13;
>> +    } else if (avctx->sample_rate >= 16000) {
>> +        x = 20000;
>> +        s->frame_bits = 12;
>> +    } else {
>> +        x = 16000;
>> +        s->frame_bits = 11;
>> +    }
>> +    s->frame_size = 1 << s->frame_bits;
>> +    s->subframe_size = s->frame_size >> 5;
>> +
>> +    if (avctx->channels == 2)
>> +        x = 3 * x / 2;
>> +    s->band_index = noise_bands_selector[FFMIN(6,
>> llrint(floor(avctx->bit_rate * 3.0 / (double)x + 0.5)))];
>> +
>> +    if ((fft_order < 7) || (fft_order > 9)) {
>> +        avpriv_request_sample(avctx, "Unknown FFT order %d", fft_order);
>> +        return AVERROR_PATCHWELCOME;
>> +    }
>> +
>> +    if (fft_size != (1 << (fft_order - 1))) {
>> +        av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n",
>> fft_size);
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +
>> +    ff_fft_init(&s->fft_ctx, fft_order, 1);
>> +
>> +    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
>> +
>> +    for (g = 5; g > 0; g--) {
>> +        for (j = 0; j < (1 << g) - 1; j++)
>> +            s->alt_sin[5-g][j] = sin_table[(((j+1) << (8 - g)) & 0x1FF)];
>> +    }
>> +
>> +    make_noises(s);
>> +
>> +    return 0;
>> +}
>
> [...]
>
>> +static void add_noise(QDMCContext *s, int ch, int current_subframe)
>> +{
>> +    int i, j, aindex;
>> +    float amplitude;
>> +    float *im = &s->fft_buffer[0 + ch][s->fft_offset + s->subframe_size *
>> current_subframe];
>> +    float *re = &s->fft_buffer[2 + ch][s->fft_offset + s->subframe_size *
>> current_subframe];
>> +
>> +    memset(s->noise2_buffer, 0, 4 * s->subframe_size);
>> +
>> +    for (i = 0; i < noise_bands_size[s->band_index]; i++) {
>> +        if (qdmc_nodes[i + 21 * s->band_index] > s->subframe_size - 1)
>> +            break;
>> +
>> +        aindex = s->noise[ch][i][current_subframe/2];
>> +        amplitude = 0.0;
>> +        if (aindex > 0)
>> +            amplitude = real_amp(aindex);
>> +
>> +        lin_calc(s, amplitude, qdmc_nodes[21 * s->band_index + i],
>> +                 qdmc_nodes[21 * s->band_index + i + 2], i);
>> +    }
>> +
>> +    for (j = 2; j < s->subframe_size - 1; j++) {
>> +        float rnd_re, rnd_im;
>> +
>> +        s->rndval = 214013 * s->rndval + 2531011;
>> +        rnd_im = ((s->rndval & 0x7FFF) - 16384.0) * 0.000030517578 *
>> s->noise2_buffer[j];
>> +        s->rndval = 214013 * s->rndval + 2531011;
>> +        rnd_re = ((s->rndval & 0x7FFF) - 16384.0) * 0.000030517578 *
>> s->noise2_buffer[j];
>
> Also float.

ok


More information about the ffmpeg-devel mailing list