Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/qdmc.c
Line
Count
Source
1
/*
2
 * QDMC compatible decoder
3
 * Copyright (c) 2017 Paul B Mahol
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include <math.h>
23
#include <stddef.h>
24
25
#define BITSTREAM_READER_LE
26
27
#include "libavutil/channel_layout.h"
28
#include "libavutil/mem_internal.h"
29
#include "libavutil/thread.h"
30
#include "libavutil/tx.h"
31
32
#include "avcodec.h"
33
#include "bytestream.h"
34
#include "codec_internal.h"
35
#include "decode.h"
36
#include "get_bits.h"
37
38
typedef struct QDMCTone {
39
    uint8_t mode;
40
    uint8_t phase;
41
    uint8_t offset;
42
    int16_t freq;
43
    int16_t amplitude;
44
} QDMCTone;
45
46
typedef struct QDMCContext {
47
    AVCodecContext *avctx;
48
49
    uint8_t frame_bits;
50
    int band_index;
51
    int frame_size;
52
    int subframe_size;
53
    int fft_offset;
54
    int buffer_offset;
55
    int nb_channels;
56
    int checksum_size;
57
58
    uint8_t noise[2][19][17];
59
    QDMCTone tones[5][8192];
60
    int nb_tones[5];
61
    int cur_tone[5];
62
    float alt_sin[5][31];
63
    float fft_buffer[4][8192 * 2];
64
    float noise2_buffer[4096 * 2];
65
    float noise_buffer[4096 * 2];
66
    float buffer[2 * 32768];
67
    float *buffer_ptr;
68
    int rndval;
69
70
    DECLARE_ALIGNED(32, AVComplexFloat, cmplx_in)[2][512];
71
    DECLARE_ALIGNED(32, AVComplexFloat, cmplx_out)[2][512];
72
    AVTXContext *fft_ctx;
73
    av_tx_fn itx_fn;
74
} QDMCContext;
75
76
static float sin_table[512];
77
static VLC vtable[6];
78
79
static const unsigned code_prefix[] = {
80
    0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
81
    0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
82
    0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
83
    0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
84
    0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
85
    0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
86
    0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
87
    0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
88
    0x3FFFC
89
};
90
91
static const float amplitude_tab[64] = {
92
    1.18750000f, 1.68359380f, 2.37500000f, 3.36718750f, 4.75000000f,
93
    6.73437500f, 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f,
94
    38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, 152.000000f,
95
    215.500000f, 304.000000f, 431.000000f, 608.000000f, 862.000000f,
96
    1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, 4864.00000f,
97
    6896.00000f, 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f,
98
    38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, 155648.000f,
99
    220672.000f, 311296.000f, 441344.000f, 622592.000f, 882688.000f,
100
    1245184.00f, 1765376.00f, 2490368.00f, 3530752.00f, 4980736.00f,
101
    7061504.00f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102
};
103
104
static const uint16_t qdmc_nodes[112] = {
105
    0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64,
106
    80, 96, 120, 144, 176, 208, 240, 256,
107
    0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104,
108
    128, 160, 208, 256, 0, 0, 0, 0, 0,
109
    0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208,
110
    256, 0, 0, 0, 0, 0, 0, 0, 0,
111
    0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256,
112
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113
    0, 4, 16, 32, 64, 256, 0, 0, 0, 0, 0, 0, 0, 0,
114
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
115
};
116
117
static const uint8_t noise_bands_size[] = {
118
    19, 14, 11, 9, 4, 2, 0
119
};
120
121
static const uint8_t noise_bands_selector[] = {
122
    4, 3, 2, 1, 0, 0, 0,
123
};
124
125
static const uint8_t qdmc_hufftab[][2] = {
126
    /* Noise value - 27 entries */
127
    {  1,  2 }, { 10,  7 }, { 26,  9 }, { 22,  9 }, { 24,  9 }, { 14,  9 },
128
    {  8,  6 }, {  6,  5 }, {  7,  5 }, {  9,  7 }, { 30,  9 }, { 32, 10 },
129
    { 13, 10 }, { 20,  9 }, { 28,  9 }, { 12,  7 }, { 15, 11 }, { 36, 12 },
130
    {  0, 12 }, { 34, 10 }, { 18,  9 }, { 11,  9 }, { 16,  9 }, {  5,  3 },
131
    {  2,  3 }, {  4,  3 }, {  3,  2 },
132
    /* Noise segment length - 12 entries */
133
    {  1,  1 }, {  2,  2 }, {  3,  4 }, {  8,  9 }, {  9, 10 }, {  0, 10 },
134
    { 13,  8 }, {  7,  7 }, {  6,  6 }, { 17,  5 }, {  4,  4 }, {  5,  4 },
135
    /* Amplitude - 28 entries */
136
    { 18,  3 }, { 16,  3 }, { 22,  7 }, {  8, 10 }, {  4, 10 }, {  3,  9 },
137
    {  2,  8 }, { 23,  8 }, { 10,  8 }, { 11,  7 }, { 21,  5 }, { 20,  4 },
138
    {  1,  7 }, {  7, 10 }, {  5, 10 }, {  9,  9 }, {  6, 10 }, { 25, 11 },
139
    { 26, 12 }, { 27, 13 }, {  0, 13 }, { 24,  9 }, { 12,  6 }, { 13,  5 },
140
    { 14,  4 }, { 19,  3 }, { 15,  3 }, { 17,  2 },
141
    /* Frequency differences - 47 entries */
142
    {  2,  4 }, { 14,  6 }, { 26,  7 }, { 31,  8 }, { 32,  9 }, { 35,  9 },
143
    {  7,  5 }, { 10,  5 }, { 22,  7 }, { 27,  7 }, { 19,  7 }, { 20,  7 },
144
    {  4,  5 }, { 13,  5 }, { 17,  6 }, { 15,  6 }, {  8,  5 }, {  5,  4 },
145
    { 28,  7 }, { 33,  9 }, { 36, 11 }, { 38, 12 }, { 42, 14 }, { 45, 16 },
146
    { 44, 18 }, {  0, 18 }, { 46, 17 }, { 43, 15 }, { 40, 13 }, { 37, 11 },
147
    { 39, 12 }, { 41, 12 }, { 34,  8 }, { 16,  6 }, { 11,  5 }, {  9,  4 },
148
    {  1,  2 }, {  3,  4 }, { 30,  7 }, { 29,  7 }, { 23,  6 }, { 24,  6 },
149
    { 18,  6 }, {  6,  4 }, { 12,  5 }, { 21,  6 }, { 25,  6 },
150
    /* Amplitude differences - 9 entries */
151
    {  1,  2 }, {  3,  3 }, {  4,  4 }, {  5,  5 }, {  6,  6 }, {  7,  7 },
152
    {  8,  8 }, {  0,  8 }, {  2,  1 },
153
    /* Phase differences - 9 entries */
154
    {  2,  2 }, {  1,  2 }, {  3,  4 }, {  7,  4 }, {  6,  5 }, {  5,  6 },
155
    {  0,  6 }, {  4,  4 }, {  8,  2 },
156
};
157
158
static const uint8_t huff_sizes[] = {
159
    27, 12, 28, 47, 9, 9
160
};
161
162
static const uint8_t huff_bits[] = {
163
    12, 10, 12, 12, 8, 6
164
};
165
166
static av_cold void qdmc_init_static_data(void)
167
1
{
168
1
    const uint8_t (*hufftab)[2] = qdmc_hufftab;
169
1
    int i;
170
171
7
    for (unsigned i = 0, offset = 0; i < FF_ARRAY_ELEMS(vtable); i++) {
172
6
        static VLCElem vlc_buffer[13698];
173
6
        vtable[i].table           = &vlc_buffer[offset];
174
6
        vtable[i].table_allocated = FF_ARRAY_ELEMS(vlc_buffer) - offset;
175
6
        ff_vlc_init_from_lengths(&vtable[i], huff_bits[i], huff_sizes[i],
176
6
                                 &hufftab[0][1], 2, &hufftab[0][0], 2, 1, -1,
177
6
                                 VLC_INIT_LE | VLC_INIT_STATIC_OVERLONG, NULL);
178
6
        hufftab += huff_sizes[i];
179
6
        offset  += vtable[i].table_size;
180
6
    }
181
182
513
    for (i = 0; i < 512; i++)
183
512
        sin_table[i] = sin(2.0f * i * M_PI * 0.001953125f);
184
1
}
185
186
static void make_noises(QDMCContext *s)
187
696
{
188
696
    int i, j, n0, n1, n2, diff;
189
696
    float *nptr;
190
191
7.18k
    for (j = 0; j < noise_bands_size[s->band_index]; j++) {
192
6.49k
        n0 = qdmc_nodes[j + 21 * s->band_index    ];
193
6.49k
        n1 = qdmc_nodes[j + 21 * s->band_index + 1];
194
6.49k
        n2 = qdmc_nodes[j + 21 * s->band_index + 2];
195
6.49k
        nptr = s->noise_buffer + 256 * j;
196
197
98.7k
        for (i = 0; i + n0 < n1; i++, nptr++)
198
92.2k
            nptr[0] = i / (float)(n1 - n0);
199
200
6.49k
        diff = n2 - n1;
201
6.49k
        nptr = s->noise_buffer + (j << 8) + n1 - n0;
202
203
182k
        for (i = n1; i < n2; i++, nptr++, diff--)
204
176k
            nptr[0] = diff / (float)(n2 - n1);
205
6.49k
    }
206
696
}
207
208
static av_cold int qdmc_decode_init(AVCodecContext *avctx)
209
1.12k
{
210
1.12k
    static AVOnce init_static_once = AV_ONCE_INIT;
211
1.12k
    QDMCContext *s = avctx->priv_data;
212
1.12k
    int ret, fft_size, fft_order, size, g, j, x;
213
1.12k
    float scale = 1.f;
214
1.12k
    GetByteContext b;
215
216
1.12k
    ff_thread_once(&init_static_once, qdmc_init_static_data);
217
218
1.12k
    if (!avctx->extradata || (avctx->extradata_size < 48)) {
219
181
        av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
220
181
        return AVERROR_INVALIDDATA;
221
181
    }
222
223
946
    bytestream2_init(&b, avctx->extradata, avctx->extradata_size);
224
225
841k
    while (bytestream2_get_bytes_left(&b) > 8) {
226
841k
        if (bytestream2_peek_be64(&b) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
227
841k
                                           (uint64_t)MKBETAG('Q','D','M','C')))
228
871
            break;
229
840k
        bytestream2_skipu(&b, 1);
230
840k
    }
231
946
    bytestream2_skipu(&b, 8);
232
233
946
    if (bytestream2_get_bytes_left(&b) < 36) {
234
79
        av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
235
79
               bytestream2_get_bytes_left(&b));
236
79
        return AVERROR_INVALIDDATA;
237
79
    }
238
239
867
    size = bytestream2_get_be32u(&b);
240
867
    if (size > bytestream2_get_bytes_left(&b)) {
241
30
        av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
242
30
               bytestream2_get_bytes_left(&b), size);
243
30
        return AVERROR_INVALIDDATA;
244
30
    }
245
246
837
    if (bytestream2_get_be32u(&b) != MKBETAG('Q','D','C','A')) {
247
35
        av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
248
35
        return AVERROR_INVALIDDATA;
249
35
    }
250
802
    bytestream2_skipu(&b, 4);
251
252
802
    s->nb_channels = bytestream2_get_be32u(&b);
253
802
    if (s->nb_channels <= 0 || s->nb_channels > 2) {
254
53
        av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
255
53
        return AVERROR_INVALIDDATA;
256
53
    }
257
749
    av_channel_layout_uninit(&avctx->ch_layout);
258
749
    avctx->ch_layout = s->nb_channels == 2 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO :
259
749
                                             (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
260
261
749
    avctx->sample_rate = bytestream2_get_be32u(&b);
262
749
    avctx->bit_rate = bytestream2_get_be32u(&b);
263
749
    bytestream2_skipu(&b, 4);
264
749
    fft_size = bytestream2_get_be32u(&b);
265
749
    fft_order = av_log2(fft_size) + 1;
266
749
    s->checksum_size = bytestream2_get_be32u(&b);
267
749
    if (s->checksum_size >= 1U << 28) {
268
16
        av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
269
16
        return AVERROR_INVALIDDATA;
270
16
    }
271
272
733
    if (avctx->sample_rate >= 32000) {
273
227
        x = 28000;
274
227
        s->frame_bits = 13;
275
506
    } else if (avctx->sample_rate >= 16000) {
276
88
        x = 20000;
277
88
        s->frame_bits = 12;
278
418
    } else {
279
418
        x = 16000;
280
418
        s->frame_bits = 11;
281
418
    }
282
733
    s->frame_size = 1 << s->frame_bits;
283
733
    s->subframe_size = s->frame_size >> 5;
284
285
733
    if (avctx->ch_layout.nb_channels == 2)
286
310
        x = 3 * x / 2;
287
733
    s->band_index = noise_bands_selector[FFMIN(6, llrint(floor(avctx->bit_rate * 3.0 / (double)x + 0.5)))];
288
289
733
    if ((fft_order < 7) || (fft_order > 9)) {
290
27
        avpriv_request_sample(avctx, "Unknown FFT order %d", fft_order);
291
27
        return AVERROR_PATCHWELCOME;
292
27
    }
293
294
706
    if (fft_size != (1 << (fft_order - 1))) {
295
10
        av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", fft_size);
296
10
        return AVERROR_INVALIDDATA;
297
10
    }
298
299
696
    ret = av_tx_init(&s->fft_ctx, &s->itx_fn, AV_TX_FLOAT_FFT, 1, 1 << fft_order, &scale, 0);
300
696
    if (ret < 0)
301
0
        return ret;
302
303
696
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
304
305
4.17k
    for (g = 5; g > 0; g--) {
306
43.1k
        for (j = 0; j < (1 << g) - 1; j++)
307
39.6k
            s->alt_sin[5-g][j] = sin_table[(((j+1) << (8 - g)) & 0x1FF)];
308
3.48k
    }
309
310
696
    make_noises(s);
311
312
696
    return 0;
313
696
}
314
315
static av_cold int qdmc_decode_close(AVCodecContext *avctx)
316
696
{
317
696
    QDMCContext *s = avctx->priv_data;
318
319
696
    av_tx_uninit(&s->fft_ctx);
320
321
696
    return 0;
322
696
}
323
324
static int qdmc_get_vlc(GetBitContext *gb, VLC *table, int flag)
325
6.98M
{
326
6.98M
    int v;
327
328
6.98M
    if (get_bits_left(gb) < 1)
329
4.79k
        return AVERROR_INVALIDDATA;
330
6.98M
    v = get_vlc2(gb, table->table, table->bits, 2);
331
6.98M
    if (v < 0)
332
2.96k
        v = get_bits(gb, get_bits(gb, 3) + 1);
333
334
6.98M
    if (flag) {
335
3.44M
        if (v >= FF_ARRAY_ELEMS(code_prefix))
336
198
            return AVERROR_INVALIDDATA;
337
338
3.44M
        v = code_prefix[v] + get_bitsz(gb, v >> 2);
339
3.44M
    }
340
341
6.98M
    return v;
342
6.98M
}
343
344
static int skip_label(QDMCContext *s, GetBitContext *gb)
345
27.8k
{
346
27.8k
    uint32_t label = get_bits_long(gb, 32);
347
27.8k
    uint16_t sum = 226, checksum = get_bits(gb, 16);
348
27.8k
    const uint8_t *ptr = gb->buffer + 6;
349
27.8k
    int i;
350
351
27.8k
    if (label != MKTAG('Q', 'M', 'C', 1))
352
15.4k
        return AVERROR_INVALIDDATA;
353
354
9.56M
    for (i = 0; i < s->checksum_size - 6; i++)
355
9.55M
        sum += ptr[i];
356
357
12.3k
    return sum != checksum;
358
27.8k
}
359
360
static int read_noise_data(QDMCContext *s, GetBitContext *gb)
361
11.1k
{
362
11.1k
    int ch, j, k, v, idx, band, lastval, newval, len;
363
364
22.4k
    for (ch = 0; ch < s->nb_channels; ch++) {
365
83.7k
        for (band = 0; band < noise_bands_size[s->band_index]; band++) {
366
72.4k
            v = qdmc_get_vlc(gb, &vtable[0], 0);
367
72.4k
            if (v < 0)
368
291
                return AVERROR_INVALIDDATA;
369
370
72.1k
            if (v & 1)
371
3.42k
                v = v + 1;
372
68.7k
            else
373
68.7k
                v = -v;
374
375
72.1k
            lastval = v / 2;
376
72.1k
            s->noise[ch][band][0] = lastval - 1;
377
1.04M
            for (j = 0; j < 15;) {
378
971k
                len = qdmc_get_vlc(gb, &vtable[1], 1);
379
971k
                if (len < 0)
380
857
                    return AVERROR_INVALIDDATA;
381
970k
                len += 1;
382
383
970k
                v = qdmc_get_vlc(gb, &vtable[0], 0);
384
970k
                if (v < 0)
385
886
                    return AVERROR_INVALIDDATA;
386
387
969k
                if (v & 1)
388
37.3k
                    newval = lastval + (v + 1) / 2;
389
931k
                else
390
931k
                    newval = lastval - v / 2;
391
392
969k
                idx = j + 1;
393
969k
                if (len + idx > 16)
394
290
                    return AVERROR_INVALIDDATA;
395
396
2.03M
                for (k = 1; idx <= j + len; k++, idx++)
397
1.07M
                    s->noise[ch][band][idx] = lastval + k * (newval - lastval) / len - 1;
398
399
969k
                lastval = newval;
400
969k
                j += len;
401
969k
            }
402
72.1k
        }
403
13.6k
    }
404
405
8.78k
    return 0;
406
11.1k
}
407
408
static void add_tone(QDMCContext *s, int group, int offset, int freq, int stereo_mode, int amplitude, int phase)
409
2.46M
{
410
2.46M
    const int index = s->nb_tones[group];
411
412
2.46M
    if (index >= FF_ARRAY_ELEMS(s->tones[group])) {
413
0
        av_log(s->avctx, AV_LOG_WARNING, "Too many tones already in buffer, ignoring tone!\n");
414
0
        return;
415
0
    }
416
417
2.46M
    s->tones[group][index].offset    = offset;
418
2.46M
    s->tones[group][index].freq      = freq;
419
2.46M
    s->tones[group][index].mode      = stereo_mode;
420
2.46M
    s->tones[group][index].amplitude = amplitude;
421
2.46M
    s->tones[group][index].phase     = phase;
422
2.46M
    s->nb_tones[group]++;
423
2.46M
}
424
425
static int read_wave_data(QDMCContext *s, GetBitContext *gb)
426
8.78k
{
427
8.78k
    int amp, phase, stereo_mode = 0, i, group, freq, group_size, group_bits;
428
8.78k
    int amp2, phase2, pos2, off;
429
430
38.5k
    for (group = 0; group < 5; group++) {
431
32.7k
        group_size = 1 << (s->frame_bits - group - 1);
432
32.7k
        group_bits = 4 - group;
433
32.7k
        pos2 = 0;
434
32.7k
        off  = 0;
435
436
2.47M
        for (i = 1; ; i = freq + 1) {
437
2.47M
            int v;
438
439
2.47M
            v = qdmc_get_vlc(gb, &vtable[3], 1);
440
2.47M
            if (v < 0)
441
1.37k
                return AVERROR_INVALIDDATA;
442
443
2.47M
            freq = i + v;
444
2.85M
            while (freq >= group_size - 1) {
445
381k
                freq += 2 - group_size;
446
381k
                pos2 += group_size;
447
381k
                off  += 1 << group_bits;
448
381k
            }
449
450
2.47M
            if (pos2 >= s->frame_size)
451
29.7k
                break;
452
453
2.44M
            if (s->nb_channels > 1)
454
372k
                stereo_mode = get_bits(gb, 2);
455
456
2.44M
            amp   = qdmc_get_vlc(gb, &vtable[2], 0);
457
2.44M
            if (amp < 0)
458
823
                return AVERROR_INVALIDDATA;
459
2.44M
            phase = get_bits(gb, 3);
460
461
2.44M
            if (stereo_mode > 1) {
462
28.2k
                amp2   = qdmc_get_vlc(gb, &vtable[4], 0);
463
28.2k
                if (amp2 < 0)
464
526
                    return AVERROR_INVALIDDATA;
465
27.7k
                amp2   = amp - amp2;
466
467
27.7k
                phase2 = qdmc_get_vlc(gb, &vtable[5], 0);
468
27.7k
                if (phase2 < 0)
469
237
                    return AVERROR_INVALIDDATA;
470
27.4k
                phase2 = phase - phase2;
471
472
27.4k
                if (phase2 < 0)
473
10.8k
                    phase2 += 8;
474
27.4k
            }
475
476
2.44M
            if ((freq >> group_bits) + 1 < s->subframe_size) {
477
2.43M
                add_tone(s, group, off, freq, stereo_mode & 1, amp, phase);
478
2.43M
                if (stereo_mode > 1)
479
27.0k
                    add_tone(s, group, off, freq, ~stereo_mode & 1, amp2, phase2);
480
2.43M
            }
481
2.44M
        }
482
32.7k
    }
483
484
5.82k
    return 0;
485
8.78k
}
486
487
static void lin_calc(QDMCContext *s, float amplitude, int node1, int node2, int index)
488
813k
{
489
813k
    int subframe_size, i, j, k, length;
490
813k
    float scale, *noise_ptr;
491
492
813k
    scale = 0.5 * amplitude;
493
813k
    subframe_size = s->subframe_size;
494
813k
    if (subframe_size >= node2)
495
618k
        subframe_size = node2;
496
813k
    length = (subframe_size - node1) & 0xFFFC;
497
813k
    j = node1;
498
813k
    noise_ptr = &s->noise_buffer[256 * index];
499
500
8.07M
    for (i = 0; i < length; i += 4, j+= 4, noise_ptr += 4) {
501
7.25M
        s->noise2_buffer[j    ] += scale * noise_ptr[0];
502
7.25M
        s->noise2_buffer[j + 1] += scale * noise_ptr[1];
503
7.25M
        s->noise2_buffer[j + 2] += scale * noise_ptr[2];
504
7.25M
        s->noise2_buffer[j + 3] += scale * noise_ptr[3];
505
7.25M
    }
506
507
813k
    k = length + node1;
508
813k
    noise_ptr = s->noise_buffer + length + (index << 8);
509
820k
    for (i = length; i < subframe_size - node1; i++, k++, noise_ptr++)
510
6.62k
        s->noise2_buffer[k] += scale * noise_ptr[0];
511
813k
}
512
513
static void add_noise(QDMCContext *s, int ch, int current_subframe)
514
200k
{
515
200k
    int i, j, aindex;
516
200k
    float amplitude;
517
200k
    float *im = &s->fft_buffer[0 + ch][s->fft_offset + s->subframe_size * current_subframe];
518
200k
    float *re = &s->fft_buffer[2 + ch][s->fft_offset + s->subframe_size * current_subframe];
519
520
200k
    memset(s->noise2_buffer, 0, 4 * s->subframe_size);
521
522
1.01M
    for (i = 0; i < noise_bands_size[s->band_index]; i++) {
523
814k
        if (qdmc_nodes[i + 21 * s->band_index] > s->subframe_size - 1)
524
1.15k
            break;
525
526
813k
        aindex = s->noise[ch][i][current_subframe / 2];
527
813k
        amplitude = aindex > 0 ? amplitude_tab[aindex & 0x3F] : 0.0f;
528
529
813k
        lin_calc(s, amplitude, qdmc_nodes[21 * s->band_index + i],
530
813k
                 qdmc_nodes[21 * s->band_index + i + 2], i);
531
813k
    }
532
533
16.5M
    for (j = 2; j < s->subframe_size - 1; j++) {
534
16.3M
        float rnd_re, rnd_im;
535
536
16.3M
        s->rndval = 214013U * s->rndval + 2531011;
537
16.3M
        rnd_im = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
538
16.3M
        s->rndval = 214013U * s->rndval + 2531011;
539
16.3M
        rnd_re = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
540
16.3M
        im[j  ] += rnd_im;
541
16.3M
        re[j  ] += rnd_re;
542
16.3M
        im[j+1] -= rnd_im;
543
16.3M
        re[j+1] -= rnd_re;
544
16.3M
    }
545
200k
}
546
547
static void add_wave(QDMCContext *s, int offset, int freqs, int group, int stereo_mode, int amp, int phase)
548
2.12M
{
549
2.12M
    int j, group_bits, pos, pindex;
550
2.12M
    float im, re, amplitude, level, *imptr, *reptr;
551
552
2.12M
    if (s->nb_channels == 1)
553
1.86M
        stereo_mode = 0;
554
555
2.12M
    group_bits = 4 - group;
556
2.12M
    pos = freqs >> (4 - group);
557
2.12M
    amplitude = amplitude_tab[amp & 0x3F];
558
2.12M
    imptr = &s->fft_buffer[    stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
559
2.12M
    reptr = &s->fft_buffer[2 + stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
560
2.12M
    pindex = (phase << 6) - ((2 * (freqs >> (4 - group)) + 1) << 7);
561
46.9M
    for (j = 0; j < (1 << (group_bits + 1)) - 1; j++) {
562
44.8M
        pindex += (2 * freqs + 1) << (7 - group_bits);
563
44.8M
        level = amplitude * s->alt_sin[group][j];
564
44.8M
        im = level * sin_table[ pindex        & 0x1FF];
565
44.8M
        re = level * sin_table[(pindex + 128) & 0x1FF];
566
44.8M
        imptr[0] += im;
567
44.8M
        imptr[1] -= im;
568
44.8M
        reptr[0] += re;
569
44.8M
        reptr[1] -= re;
570
44.8M
        imptr += s->subframe_size;
571
44.8M
        reptr += s->subframe_size;
572
44.8M
        if (imptr >= &s->fft_buffer[stereo_mode][2 * s->frame_size]) {
573
373k
            imptr = &s->fft_buffer[0 + stereo_mode][pos];
574
373k
            reptr = &s->fft_buffer[2 + stereo_mode][pos];
575
373k
        }
576
44.8M
    }
577
2.12M
}
578
579
static void add_wave0(QDMCContext *s, int offset, int freqs, int stereo_mode, int amp, int phase)
580
256k
{
581
256k
    float level, im, re;
582
256k
    int pos;
583
584
256k
    if (s->nb_channels == 1)
585
175k
        stereo_mode = 0;
586
587
256k
    level = amplitude_tab[amp & 0x3F];
588
256k
    im = level * sin_table[ (phase << 6)        & 0x1FF];
589
256k
    re = level * sin_table[((phase << 6) + 128) & 0x1FF];
590
256k
    pos = s->fft_offset + freqs + s->subframe_size * offset;
591
256k
    s->fft_buffer[    stereo_mode][pos    ] += im;
592
256k
    s->fft_buffer[2 + stereo_mode][pos    ] += re;
593
256k
    s->fft_buffer[    stereo_mode][pos + 1] -= im;
594
256k
    s->fft_buffer[2 + stereo_mode][pos + 1] -= re;
595
256k
}
596
597
static void add_waves(QDMCContext *s, int current_subframe)
598
186k
{
599
186k
    int w, g;
600
601
932k
    for (g = 0; g < 4; g++) {
602
2.86M
        for (w = s->cur_tone[g]; w < s->nb_tones[g]; w++) {
603
2.67M
            QDMCTone *t = &s->tones[g][w];
604
605
2.67M
            if (current_subframe < t->offset)
606
555k
                break;
607
2.12M
            add_wave(s, t->offset, t->freq, g, t->mode, t->amplitude, t->phase);
608
2.12M
        }
609
745k
        s->cur_tone[g] = w;
610
745k
    }
611
442k
    for (w = s->cur_tone[4]; w < s->nb_tones[4]; w++) {
612
427k
        QDMCTone *t = &s->tones[4][w];
613
614
427k
        if (current_subframe < t->offset)
615
171k
            break;
616
256k
        add_wave0(s, t->offset, t->freq, t->mode, t->amplitude, t->phase);
617
256k
    }
618
186k
    s->cur_tone[4] = w;
619
186k
}
620
621
static int decode_frame(QDMCContext *s, GetBitContext *gb, int16_t *out)
622
27.8k
{
623
27.8k
    int ret, ch, i, n;
624
625
27.8k
    if (skip_label(s, gb))
626
16.7k
        return AVERROR_INVALIDDATA;
627
628
11.1k
    s->fft_offset = s->frame_size - s->fft_offset;
629
11.1k
    s->buffer_ptr = &s->buffer[s->nb_channels * s->buffer_offset];
630
631
11.1k
    ret = read_noise_data(s, gb);
632
11.1k
    if (ret < 0)
633
2.32k
        return ret;
634
635
8.78k
    ret = read_wave_data(s, gb);
636
8.78k
    if (ret < 0)
637
2.95k
        return ret;
638
639
192k
    for (n = 0; n < 32; n++) {
640
186k
        float *r;
641
642
386k
        for (ch = 0; ch < s->nb_channels; ch++)
643
200k
            add_noise(s, ch, n);
644
645
186k
        add_waves(s, n);
646
647
386k
        for (ch = 0; ch < s->nb_channels; ch++) {
648
17.1M
            for (i = 0; i < s->subframe_size; i++) {
649
16.9M
                s->cmplx_in[ch][i].re = s->fft_buffer[ch + 2][s->fft_offset + n * s->subframe_size + i];
650
16.9M
                s->cmplx_in[ch][i].im = s->fft_buffer[ch + 0][s->fft_offset + n * s->subframe_size + i];
651
16.9M
                s->cmplx_in[ch][s->subframe_size + i].re = 0;
652
16.9M
                s->cmplx_in[ch][s->subframe_size + i].im = 0;
653
16.9M
            }
654
200k
        }
655
656
386k
        for (ch = 0; ch < s->nb_channels; ch++) {
657
200k
            s->itx_fn(s->fft_ctx, s->cmplx_out[ch], s->cmplx_in[ch], sizeof(float));
658
200k
        }
659
660
186k
        r = &s->buffer_ptr[s->nb_channels * n * s->subframe_size];
661
31.9M
        for (i = 0; i < 2 * s->subframe_size; i++) {
662
65.6M
            for (ch = 0; ch < s->nb_channels; ch++) {
663
33.8M
                *r++ += s->cmplx_out[ch][i].re;
664
33.8M
            }
665
31.7M
        }
666
667
186k
        r = &s->buffer_ptr[n * s->subframe_size * s->nb_channels];
668
17.1M
        for (i = 0; i < s->nb_channels * s->subframe_size; i++) {
669
16.9M
            out[i] = av_clipf(r[i], INT16_MIN, INT16_MAX);
670
16.9M
        }
671
186k
        out += s->subframe_size * s->nb_channels;
672
673
386k
        for (ch = 0; ch < s->nb_channels; ch++) {
674
200k
            memset(s->fft_buffer[ch+0] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
675
200k
            memset(s->fft_buffer[ch+2] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
676
200k
        }
677
186k
        memset(s->buffer + s->nb_channels * (n * s->subframe_size + s->frame_size + s->buffer_offset), 0, 4 * s->subframe_size * s->nb_channels);
678
186k
    }
679
680
5.82k
    s->buffer_offset += s->frame_size;
681
5.82k
    if (s->buffer_offset >= 32768 - s->frame_size) {
682
225
        memcpy(s->buffer, &s->buffer[s->nb_channels * s->buffer_offset], 4 * s->frame_size * s->nb_channels);
683
225
        s->buffer_offset = 0;
684
225
    }
685
686
5.82k
    return 0;
687
8.78k
}
688
689
static av_cold void qdmc_flush(AVCodecContext *avctx)
690
41.4k
{
691
41.4k
    QDMCContext *s = avctx->priv_data;
692
693
41.4k
    memset(s->buffer, 0, sizeof(s->buffer));
694
41.4k
    memset(s->fft_buffer, 0, sizeof(s->fft_buffer));
695
41.4k
    s->fft_offset = 0;
696
41.4k
    s->buffer_offset = 0;
697
41.4k
}
698
699
static int qdmc_decode_frame(AVCodecContext *avctx, AVFrame *frame,
700
                             int *got_frame_ptr, AVPacket *avpkt)
701
60.6k
{
702
60.6k
    QDMCContext *s = avctx->priv_data;
703
60.6k
    GetBitContext gb;
704
60.6k
    int ret;
705
706
60.6k
    if (!avpkt->data)
707
0
        return 0;
708
60.6k
    if (avpkt->size < s->checksum_size)
709
32.8k
        return AVERROR_INVALIDDATA;
710
711
27.8k
    s->avctx = avctx;
712
27.8k
    frame->nb_samples = s->frame_size;
713
27.8k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
714
0
        return ret;
715
716
27.8k
    if ((ret = init_get_bits8(&gb, avpkt->data, s->checksum_size)) < 0)
717
0
        return ret;
718
719
27.8k
    memset(s->nb_tones, 0, sizeof(s->nb_tones));
720
27.8k
    memset(s->cur_tone, 0, sizeof(s->cur_tone));
721
722
27.8k
    ret = decode_frame(s, &gb, (int16_t *)frame->data[0]);
723
27.8k
    if (ret >= 0) {
724
5.82k
        *got_frame_ptr = 1;
725
5.82k
        return s->checksum_size;
726
5.82k
    }
727
22.0k
    qdmc_flush(avctx);
728
22.0k
    return ret;
729
27.8k
}
730
731
const FFCodec ff_qdmc_decoder = {
732
    .p.name           = "qdmc",
733
    CODEC_LONG_NAME("QDesign Music Codec 1"),
734
    .p.type           = AVMEDIA_TYPE_AUDIO,
735
    .p.id             = AV_CODEC_ID_QDMC,
736
    .priv_data_size   = sizeof(QDMCContext),
737
    .init             = qdmc_decode_init,
738
    .close            = qdmc_decode_close,
739
    FF_CODEC_DECODE_CB(qdmc_decode_frame),
740
    .flush            = qdmc_flush,
741
    .p.capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
742
};