FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qdmc.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 0 346 0.0%
Functions: 0 17 0.0%
Branches: 0 176 0.0%

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