FFmpeg
qdmc.c
Go to the documentation of this file.
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 #include <stdio.h>
25 
26 #define BITSTREAM_READER_LE
27 
29 #include "libavutil/thread.h"
30 
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "get_bits.h"
34 #include "internal.h"
35 #include "fft.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 {
47 
48  uint8_t frame_bits;
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 
71 } QDMCContext;
72 
73 static float sin_table[512];
74 static VLC vtable[6];
75 
76 static const unsigned code_prefix[] = {
77  0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
78  0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
79  0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
80  0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
81  0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
82  0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
83  0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
84  0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
85  0x3FFFC
86 };
87 
88 static const float amplitude_tab[64] = {
89  1.18750000f, 1.68359380f, 2.37500000f, 3.36718750f, 4.75000000f,
90  6.73437500f, 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f,
91  38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, 152.000000f,
92  215.500000f, 304.000000f, 431.000000f, 608.000000f, 862.000000f,
93  1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, 4864.00000f,
94  6896.00000f, 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f,
95  38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, 155648.000f,
96  220672.000f, 311296.000f, 441344.000f, 622592.000f, 882688.000f,
97  1245184.00f, 1765376.00f, 2490368.00f, 3530752.00f, 4980736.00f,
98  7061504.00f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 };
100 
101 static const uint16_t qdmc_nodes[112] = {
102  0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64,
103  80, 96, 120, 144, 176, 208, 240, 256,
104  0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104,
105  128, 160, 208, 256, 0, 0, 0, 0, 0,
106  0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208,
107  256, 0, 0, 0, 0, 0, 0, 0, 0,
108  0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256,
109  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110  0, 4, 16, 32, 64, 256, 0, 0, 0, 0, 0, 0, 0, 0,
111  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
112 };
113 
114 static const uint8_t noise_bands_size[] = {
115  19, 14, 11, 9, 4, 2, 0
116 };
117 
118 static const uint8_t noise_bands_selector[] = {
119  4, 3, 2, 1, 0, 0, 0,
120 };
121 
122 static const uint8_t qdmc_hufftab[][2] = {
123  /* Noise value - 27 entries */
124  { 1, 2 }, { 10, 7 }, { 26, 9 }, { 22, 9 }, { 24, 9 }, { 14, 9 },
125  { 8, 6 }, { 6, 5 }, { 7, 5 }, { 9, 7 }, { 30, 9 }, { 32, 10 },
126  { 13, 10 }, { 20, 9 }, { 28, 9 }, { 12, 7 }, { 15, 11 }, { 36, 12 },
127  { 0, 12 }, { 34, 10 }, { 18, 9 }, { 11, 9 }, { 16, 9 }, { 5, 3 },
128  { 2, 3 }, { 4, 3 }, { 3, 2 },
129  /* Noise segment length - 12 entries */
130  { 1, 1 }, { 2, 2 }, { 3, 4 }, { 8, 9 }, { 9, 10 }, { 0, 10 },
131  { 13, 8 }, { 7, 7 }, { 6, 6 }, { 17, 5 }, { 4, 4 }, { 5, 4 },
132  /* Amplitude - 28 entries */
133  { 18, 3 }, { 16, 3 }, { 22, 7 }, { 8, 10 }, { 4, 10 }, { 3, 9 },
134  { 2, 8 }, { 23, 8 }, { 10, 8 }, { 11, 7 }, { 21, 5 }, { 20, 4 },
135  { 1, 7 }, { 7, 10 }, { 5, 10 }, { 9, 9 }, { 6, 10 }, { 25, 11 },
136  { 26, 12 }, { 27, 13 }, { 0, 13 }, { 24, 9 }, { 12, 6 }, { 13, 5 },
137  { 14, 4 }, { 19, 3 }, { 15, 3 }, { 17, 2 },
138  /* Frequency differences - 47 entries */
139  { 2, 4 }, { 14, 6 }, { 26, 7 }, { 31, 8 }, { 32, 9 }, { 35, 9 },
140  { 7, 5 }, { 10, 5 }, { 22, 7 }, { 27, 7 }, { 19, 7 }, { 20, 7 },
141  { 4, 5 }, { 13, 5 }, { 17, 6 }, { 15, 6 }, { 8, 5 }, { 5, 4 },
142  { 28, 7 }, { 33, 9 }, { 36, 11 }, { 38, 12 }, { 42, 14 }, { 45, 16 },
143  { 44, 18 }, { 0, 18 }, { 46, 17 }, { 43, 15 }, { 40, 13 }, { 37, 11 },
144  { 39, 12 }, { 41, 12 }, { 34, 8 }, { 16, 6 }, { 11, 5 }, { 9, 4 },
145  { 1, 2 }, { 3, 4 }, { 30, 7 }, { 29, 7 }, { 23, 6 }, { 24, 6 },
146  { 18, 6 }, { 6, 4 }, { 12, 5 }, { 21, 6 }, { 25, 6 },
147  /* Amplitude differences - 9 entries */
148  { 1, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
149  { 8, 8 }, { 0, 8 }, { 2, 1 },
150  /* Phase differences - 9 entries */
151  { 2, 2 }, { 1, 2 }, { 3, 4 }, { 7, 4 }, { 6, 5 }, { 5, 6 },
152  { 0, 6 }, { 4, 4 }, { 8, 2 },
153 };
154 
155 static const uint8_t huff_sizes[] = {
156  27, 12, 28, 47, 9, 9
157 };
158 
159 static const uint8_t huff_bits[] = {
160  12, 10, 12, 12, 8, 6
161 };
162 
164 {
165  const uint8_t (*hufftab)[2] = qdmc_hufftab;
166  int i;
167 
168  for (unsigned i = 0, offset = 0; i < FF_ARRAY_ELEMS(vtable); i++) {
169  static VLC_TYPE vlc_buffer[13698][2];
170  vtable[i].table = &vlc_buffer[offset];
171  vtable[i].table_allocated = FF_ARRAY_ELEMS(vlc_buffer) - offset;
173  &hufftab[0][1], 2, &hufftab[0][0], 2, 1, -1,
175  hufftab += huff_sizes[i];
177  }
178 
179  for (i = 0; i < 512; i++)
180  sin_table[i] = sin(2.0f * i * M_PI * 0.001953125f);
181 }
182 
183 static void make_noises(QDMCContext *s)
184 {
185  int i, j, n0, n1, n2, diff;
186  float *nptr;
187 
188  for (j = 0; j < noise_bands_size[s->band_index]; j++) {
189  n0 = qdmc_nodes[j + 21 * s->band_index ];
190  n1 = qdmc_nodes[j + 21 * s->band_index + 1];
191  n2 = qdmc_nodes[j + 21 * s->band_index + 2];
192  nptr = s->noise_buffer + 256 * j;
193 
194  for (i = 0; i + n0 < n1; i++, nptr++)
195  nptr[0] = i / (float)(n1 - n0);
196 
197  diff = n2 - n1;
198  nptr = s->noise_buffer + (j << 8) + n1 - n0;
199 
200  for (i = n1; i < n2; i++, nptr++, diff--)
201  nptr[0] = diff / (float)(n2 - n1);
202  }
203 }
204 
206 {
207  static AVOnce init_static_once = AV_ONCE_INIT;
208  QDMCContext *s = avctx->priv_data;
209  int ret, fft_size, fft_order, size, g, j, x;
211 
212  ff_thread_once(&init_static_once, qdmc_init_static_data);
213 
214  if (!avctx->extradata || (avctx->extradata_size < 48)) {
215  av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
216  return AVERROR_INVALIDDATA;
217  }
218 
219  bytestream2_init(&b, avctx->extradata, avctx->extradata_size);
220 
221  while (bytestream2_get_bytes_left(&b) > 8) {
222  if (bytestream2_peek_be64(&b) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
223  (uint64_t)MKBETAG('Q','D','M','C')))
224  break;
225  bytestream2_skipu(&b, 1);
226  }
227  bytestream2_skipu(&b, 8);
228 
229  if (bytestream2_get_bytes_left(&b) < 36) {
230  av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
232  return AVERROR_INVALIDDATA;
233  }
234 
235  size = bytestream2_get_be32u(&b);
237  av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
239  return AVERROR_INVALIDDATA;
240  }
241 
242  if (bytestream2_get_be32u(&b) != MKBETAG('Q','D','C','A')) {
243  av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
244  return AVERROR_INVALIDDATA;
245  }
246  bytestream2_skipu(&b, 4);
247 
248  avctx->channels = s->nb_channels = bytestream2_get_be32u(&b);
249  if (s->nb_channels <= 0 || s->nb_channels > 2) {
250  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
251  return AVERROR_INVALIDDATA;
252  }
253  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
255 
256  avctx->sample_rate = bytestream2_get_be32u(&b);
257  avctx->bit_rate = bytestream2_get_be32u(&b);
258  bytestream2_skipu(&b, 4);
259  fft_size = bytestream2_get_be32u(&b);
260  fft_order = av_log2(fft_size) + 1;
261  s->checksum_size = bytestream2_get_be32u(&b);
262  if (s->checksum_size >= 1U << 28) {
263  av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
264  return AVERROR_INVALIDDATA;
265  }
266 
267  if (avctx->sample_rate >= 32000) {
268  x = 28000;
269  s->frame_bits = 13;
270  } else if (avctx->sample_rate >= 16000) {
271  x = 20000;
272  s->frame_bits = 12;
273  } else {
274  x = 16000;
275  s->frame_bits = 11;
276  }
277  s->frame_size = 1 << s->frame_bits;
278  s->subframe_size = s->frame_size >> 5;
279 
280  if (avctx->channels == 2)
281  x = 3 * x / 2;
282  s->band_index = noise_bands_selector[FFMIN(6, llrint(floor(avctx->bit_rate * 3.0 / (double)x + 0.5)))];
283 
284  if ((fft_order < 7) || (fft_order > 9)) {
285  avpriv_request_sample(avctx, "Unknown FFT order %d", fft_order);
286  return AVERROR_PATCHWELCOME;
287  }
288 
289  if (fft_size != (1 << (fft_order - 1))) {
290  av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", fft_size);
291  return AVERROR_INVALIDDATA;
292  }
293 
294  ret = ff_fft_init(&s->fft_ctx, fft_order, 1);
295  if (ret < 0)
296  return ret;
297 
298  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
299 
300  for (g = 5; g > 0; g--) {
301  for (j = 0; j < (1 << g) - 1; j++)
302  s->alt_sin[5-g][j] = sin_table[(((j+1) << (8 - g)) & 0x1FF)];
303  }
304 
305  make_noises(s);
306 
307  return 0;
308 }
309 
311 {
312  QDMCContext *s = avctx->priv_data;
313 
314  ff_fft_end(&s->fft_ctx);
315 
316  return 0;
317 }
318 
319 static int qdmc_get_vlc(GetBitContext *gb, VLC *table, int flag)
320 {
321  int v;
322 
323  if (get_bits_left(gb) < 1)
324  return AVERROR_INVALIDDATA;
325  v = get_vlc2(gb, table->table, table->bits, 2);
326  if (v < 0)
327  v = get_bits(gb, get_bits(gb, 3) + 1);
328 
329  if (flag) {
330  if (v >= FF_ARRAY_ELEMS(code_prefix))
331  return AVERROR_INVALIDDATA;
332 
333  v = code_prefix[v] + get_bitsz(gb, v >> 2);
334  }
335 
336  return v;
337 }
338 
340 {
341  uint32_t label = get_bits_long(gb, 32);
342  uint16_t sum = 226, checksum = get_bits(gb, 16);
343  const uint8_t *ptr = gb->buffer + 6;
344  int i;
345 
346  if (label != MKTAG('Q', 'M', 'C', 1))
347  return AVERROR_INVALIDDATA;
348 
349  for (i = 0; i < s->checksum_size - 6; i++)
350  sum += ptr[i];
351 
352  return sum != checksum;
353 }
354 
356 {
357  int ch, j, k, v, idx, band, lastval, newval, len;
358 
359  for (ch = 0; ch < s->nb_channels; ch++) {
360  for (band = 0; band < noise_bands_size[s->band_index]; band++) {
361  v = qdmc_get_vlc(gb, &vtable[0], 0);
362  if (v < 0)
363  return AVERROR_INVALIDDATA;
364 
365  if (v & 1)
366  v = v + 1;
367  else
368  v = -v;
369 
370  lastval = v / 2;
371  s->noise[ch][band][0] = lastval - 1;
372  for (j = 0; j < 15;) {
373  len = qdmc_get_vlc(gb, &vtable[1], 1);
374  if (len < 0)
375  return AVERROR_INVALIDDATA;
376  len += 1;
377 
378  v = qdmc_get_vlc(gb, &vtable[0], 0);
379  if (v < 0)
380  return AVERROR_INVALIDDATA;
381 
382  if (v & 1)
383  newval = lastval + (v + 1) / 2;
384  else
385  newval = lastval - v / 2;
386 
387  idx = j + 1;
388  if (len + idx > 16)
389  return AVERROR_INVALIDDATA;
390 
391  for (k = 1; idx <= j + len; k++, idx++)
392  s->noise[ch][band][idx] = lastval + k * (newval - lastval) / len - 1;
393 
394  lastval = newval;
395  j += len;
396  }
397  }
398  }
399 
400  return 0;
401 }
402 
403 static void add_tone(QDMCContext *s, int group, int offset, int freq, int stereo_mode, int amplitude, int phase)
404 {
405  const int index = s->nb_tones[group];
406 
407  if (index >= FF_ARRAY_ELEMS(s->tones[group])) {
408  av_log(s->avctx, AV_LOG_WARNING, "Too many tones already in buffer, ignoring tone!\n");
409  return;
410  }
411 
412  s->tones[group][index].offset = offset;
413  s->tones[group][index].freq = freq;
414  s->tones[group][index].mode = stereo_mode;
415  s->tones[group][index].amplitude = amplitude;
416  s->tones[group][index].phase = phase;
417  s->nb_tones[group]++;
418 }
419 
421 {
422  int amp, phase, stereo_mode = 0, i, group, freq, group_size, group_bits;
423  int amp2, phase2, pos2, off;
424 
425  for (group = 0; group < 5; group++) {
426  group_size = 1 << (s->frame_bits - group - 1);
427  group_bits = 4 - group;
428  pos2 = 0;
429  off = 0;
430 
431  for (i = 1; ; i = freq + 1) {
432  int v;
433 
434  v = qdmc_get_vlc(gb, &vtable[3], 1);
435  if (v < 0)
436  return AVERROR_INVALIDDATA;
437 
438  freq = i + v;
439  while (freq >= group_size - 1) {
440  freq += 2 - group_size;
441  pos2 += group_size;
442  off += 1 << group_bits;
443  }
444 
445  if (pos2 >= s->frame_size)
446  break;
447 
448  if (s->nb_channels > 1)
449  stereo_mode = get_bits(gb, 2);
450 
451  amp = qdmc_get_vlc(gb, &vtable[2], 0);
452  if (amp < 0)
453  return AVERROR_INVALIDDATA;
454  phase = get_bits(gb, 3);
455 
456  if (stereo_mode > 1) {
457  amp2 = qdmc_get_vlc(gb, &vtable[4], 0);
458  if (amp2 < 0)
459  return AVERROR_INVALIDDATA;
460  amp2 = amp - amp2;
461 
462  phase2 = qdmc_get_vlc(gb, &vtable[5], 0);
463  if (phase2 < 0)
464  return AVERROR_INVALIDDATA;
465  phase2 = phase - phase2;
466 
467  if (phase2 < 0)
468  phase2 += 8;
469  }
470 
471  if ((freq >> group_bits) + 1 < s->subframe_size) {
472  add_tone(s, group, off, freq, stereo_mode & 1, amp, phase);
473  if (stereo_mode > 1)
474  add_tone(s, group, off, freq, ~stereo_mode & 1, amp2, phase2);
475  }
476  }
477  }
478 
479  return 0;
480 }
481 
482 static void lin_calc(QDMCContext *s, float amplitude, int node1, int node2, int index)
483 {
484  int subframe_size, i, j, k, length;
485  float scale, *noise_ptr;
486 
487  scale = 0.5 * amplitude;
488  subframe_size = s->subframe_size;
489  if (subframe_size >= node2)
490  subframe_size = node2;
491  length = (subframe_size - node1) & 0xFFFC;
492  j = node1;
493  noise_ptr = &s->noise_buffer[256 * index];
494 
495  for (i = 0; i < length; i += 4, j+= 4, noise_ptr += 4) {
496  s->noise2_buffer[j ] += scale * noise_ptr[0];
497  s->noise2_buffer[j + 1] += scale * noise_ptr[1];
498  s->noise2_buffer[j + 2] += scale * noise_ptr[2];
499  s->noise2_buffer[j + 3] += scale * noise_ptr[3];
500  }
501 
502  k = length + node1;
503  noise_ptr = s->noise_buffer + length + (index << 8);
504  for (i = length; i < subframe_size - node1; i++, k++, noise_ptr++)
505  s->noise2_buffer[k] += scale * noise_ptr[0];
506 }
507 
508 static void add_noise(QDMCContext *s, int ch, int current_subframe)
509 {
510  int i, j, aindex;
511  float amplitude;
512  float *im = &s->fft_buffer[0 + ch][s->fft_offset + s->subframe_size * current_subframe];
513  float *re = &s->fft_buffer[2 + ch][s->fft_offset + s->subframe_size * current_subframe];
514 
515  memset(s->noise2_buffer, 0, 4 * s->subframe_size);
516 
517  for (i = 0; i < noise_bands_size[s->band_index]; i++) {
518  if (qdmc_nodes[i + 21 * s->band_index] > s->subframe_size - 1)
519  break;
520 
521  aindex = s->noise[ch][i][current_subframe / 2];
522  amplitude = aindex > 0 ? amplitude_tab[aindex & 0x3F] : 0.0f;
523 
524  lin_calc(s, amplitude, qdmc_nodes[21 * s->band_index + i],
525  qdmc_nodes[21 * s->band_index + i + 2], i);
526  }
527 
528  for (j = 2; j < s->subframe_size - 1; j++) {
529  float rnd_re, rnd_im;
530 
531  s->rndval = 214013U * s->rndval + 2531011;
532  rnd_im = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
533  s->rndval = 214013U * s->rndval + 2531011;
534  rnd_re = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
535  im[j ] += rnd_im;
536  re[j ] += rnd_re;
537  im[j+1] -= rnd_im;
538  re[j+1] -= rnd_re;
539  }
540 }
541 
542 static void add_wave(QDMCContext *s, int offset, int freqs, int group, int stereo_mode, int amp, int phase)
543 {
544  int j, group_bits, pos, pindex;
545  float im, re, amplitude, level, *imptr, *reptr;
546 
547  if (s->nb_channels == 1)
548  stereo_mode = 0;
549 
550  group_bits = 4 - group;
551  pos = freqs >> (4 - group);
552  amplitude = amplitude_tab[amp & 0x3F];
553  imptr = &s->fft_buffer[ stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
554  reptr = &s->fft_buffer[2 + stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
555  pindex = (phase << 6) - ((2 * (freqs >> (4 - group)) + 1) << 7);
556  for (j = 0; j < (1 << (group_bits + 1)) - 1; j++) {
557  pindex += (2 * freqs + 1) << (7 - group_bits);
558  level = amplitude * s->alt_sin[group][j];
559  im = level * sin_table[ pindex & 0x1FF];
560  re = level * sin_table[(pindex + 128) & 0x1FF];
561  imptr[0] += im;
562  imptr[1] -= im;
563  reptr[0] += re;
564  reptr[1] -= re;
565  imptr += s->subframe_size;
566  reptr += s->subframe_size;
567  if (imptr >= &s->fft_buffer[stereo_mode][2 * s->frame_size]) {
568  imptr = &s->fft_buffer[0 + stereo_mode][pos];
569  reptr = &s->fft_buffer[2 + stereo_mode][pos];
570  }
571  }
572 }
573 
574 static void add_wave0(QDMCContext *s, int offset, int freqs, int stereo_mode, int amp, int phase)
575 {
576  float level, im, re;
577  int pos;
578 
579  if (s->nb_channels == 1)
580  stereo_mode = 0;
581 
582  level = amplitude_tab[amp & 0x3F];
583  im = level * sin_table[ (phase << 6) & 0x1FF];
584  re = level * sin_table[((phase << 6) + 128) & 0x1FF];
585  pos = s->fft_offset + freqs + s->subframe_size * offset;
586  s->fft_buffer[ stereo_mode][pos ] += im;
587  s->fft_buffer[2 + stereo_mode][pos ] += re;
588  s->fft_buffer[ stereo_mode][pos + 1] -= im;
589  s->fft_buffer[2 + stereo_mode][pos + 1] -= re;
590 }
591 
592 static void add_waves(QDMCContext *s, int current_subframe)
593 {
594  int w, g;
595 
596  for (g = 0; g < 4; g++) {
597  for (w = s->cur_tone[g]; w < s->nb_tones[g]; w++) {
598  QDMCTone *t = &s->tones[g][w];
599 
600  if (current_subframe < t->offset)
601  break;
602  add_wave(s, t->offset, t->freq, g, t->mode, t->amplitude, t->phase);
603  }
604  s->cur_tone[g] = w;
605  }
606  for (w = s->cur_tone[4]; w < s->nb_tones[4]; w++) {
607  QDMCTone *t = &s->tones[4][w];
608 
609  if (current_subframe < t->offset)
610  break;
611  add_wave0(s, t->offset, t->freq, t->mode, t->amplitude, t->phase);
612  }
613  s->cur_tone[4] = w;
614 }
615 
616 static int decode_frame(QDMCContext *s, GetBitContext *gb, int16_t *out)
617 {
618  int ret, ch, i, n;
619 
620  if (skip_label(s, gb))
621  return AVERROR_INVALIDDATA;
622 
623  s->fft_offset = s->frame_size - s->fft_offset;
624  s->buffer_ptr = &s->buffer[s->nb_channels * s->buffer_offset];
625 
626  ret = read_noise_data(s, gb);
627  if (ret < 0)
628  return ret;
629 
630  ret = read_wave_data(s, gb);
631  if (ret < 0)
632  return ret;
633 
634  for (n = 0; n < 32; n++) {
635  float *r;
636 
637  for (ch = 0; ch < s->nb_channels; ch++)
638  add_noise(s, ch, n);
639 
640  add_waves(s, n);
641 
642  for (ch = 0; ch < s->nb_channels; ch++) {
643  for (i = 0; i < s->subframe_size; i++) {
644  s->cmplx[ch][i].re = s->fft_buffer[ch + 2][s->fft_offset + n * s->subframe_size + i];
645  s->cmplx[ch][i].im = s->fft_buffer[ch + 0][s->fft_offset + n * s->subframe_size + i];
646  s->cmplx[ch][s->subframe_size + i].re = 0;
647  s->cmplx[ch][s->subframe_size + i].im = 0;
648  }
649  }
650 
651  for (ch = 0; ch < s->nb_channels; ch++) {
652  s->fft_ctx.fft_permute(&s->fft_ctx, s->cmplx[ch]);
653  s->fft_ctx.fft_calc(&s->fft_ctx, s->cmplx[ch]);
654  }
655 
656  r = &s->buffer_ptr[s->nb_channels * n * s->subframe_size];
657  for (i = 0; i < 2 * s->subframe_size; i++) {
658  for (ch = 0; ch < s->nb_channels; ch++) {
659  *r++ += s->cmplx[ch][i].re;
660  }
661  }
662 
663  r = &s->buffer_ptr[n * s->subframe_size * s->nb_channels];
664  for (i = 0; i < s->nb_channels * s->subframe_size; i++) {
665  out[i] = av_clipf(r[i], INT16_MIN, INT16_MAX);
666  }
667  out += s->subframe_size * s->nb_channels;
668 
669  for (ch = 0; ch < s->nb_channels; ch++) {
670  memset(s->fft_buffer[ch+0] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
671  memset(s->fft_buffer[ch+2] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
672  }
673  memset(s->buffer + s->nb_channels * (n * s->subframe_size + s->frame_size + s->buffer_offset), 0, 4 * s->subframe_size * s->nb_channels);
674  }
675 
676  s->buffer_offset += s->frame_size;
677  if (s->buffer_offset >= 32768 - s->frame_size) {
678  memcpy(s->buffer, &s->buffer[s->nb_channels * s->buffer_offset], 4 * s->frame_size * s->nb_channels);
679  s->buffer_offset = 0;
680  }
681 
682  return 0;
683 }
684 
685 static av_cold void qdmc_flush(AVCodecContext *avctx)
686 {
687  QDMCContext *s = avctx->priv_data;
688 
689  memset(s->buffer, 0, sizeof(s->buffer));
690  memset(s->fft_buffer, 0, sizeof(s->fft_buffer));
691  s->fft_offset = 0;
692  s->buffer_offset = 0;
693 }
694 
695 static int qdmc_decode_frame(AVCodecContext *avctx, void *data,
696  int *got_frame_ptr, AVPacket *avpkt)
697 {
698  QDMCContext *s = avctx->priv_data;
699  AVFrame *frame = data;
700  GetBitContext gb;
701  int ret;
702 
703  if (!avpkt->data)
704  return 0;
705  if (avpkt->size < s->checksum_size)
706  return AVERROR_INVALIDDATA;
707 
708  s->avctx = avctx;
709  frame->nb_samples = s->frame_size;
710  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
711  return ret;
712 
713  if ((ret = init_get_bits8(&gb, avpkt->data, s->checksum_size)) < 0)
714  return ret;
715 
716  memset(s->nb_tones, 0, sizeof(s->nb_tones));
717  memset(s->cur_tone, 0, sizeof(s->cur_tone));
718 
719  ret = decode_frame(s, &gb, (int16_t *)frame->data[0]);
720  if (ret >= 0) {
721  *got_frame_ptr = 1;
722  return s->checksum_size;
723  }
724  qdmc_flush(avctx);
725  return ret;
726 }
727 
729  .name = "qdmc",
730  .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 1"),
731  .type = AVMEDIA_TYPE_AUDIO,
732  .id = AV_CODEC_ID_QDMC,
733  .priv_data_size = sizeof(QDMCContext),
735  .close = qdmc_decode_close,
737  .flush = qdmc_flush,
739  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
740 };
add_waves
static void add_waves(QDMCContext *s, int current_subframe)
Definition: qdmc.c:592
make_noises
static void make_noises(QDMCContext *s)
Definition: qdmc.c:183
ff_fft_init
#define ff_fft_init
Definition: fft.h:143
AVCodec
AVCodec.
Definition: codec.h:197
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
level
uint8_t level
Definition: svq3.c:204
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:850
r
const char * r
Definition: vf_curves.c:116
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1039
QDMCContext::fft_buffer
float fft_buffer[4][8192 *2]
Definition: qdmc.c:62
out
FILE * out
Definition: movenc.c:54
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:988
GetByteContext
Definition: bytestream.h:33
QDMCContext::nb_channels
int nb_channels
Definition: qdmc.c:54
thread.h
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:478
skip_label
static int skip_label(QDMCContext *s, GetBitContext *gb)
Definition: qdmc.c:339
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:547
noise_bands_selector
static const uint8_t noise_bands_selector[]
Definition: qdmc.c:118
QDMCContext::noise_buffer
float noise_buffer[4096 *2]
Definition: qdmc.c:64
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
im
float im
Definition: fft.c:82
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
index
fg index
Definition: ffmpeg_filter.c:168
lin_calc
static void lin_calc(QDMCContext *s, float amplitude, int node1, int node2, int index)
Definition: qdmc.c:482
w
uint8_t w
Definition: llviddspenc.c:38
QDMCContext::buffer
float buffer[2 *32768]
Definition: qdmc.c:65
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
QDMCContext::noise2_buffer
float noise2_buffer[4096 *2]
Definition: qdmc.c:63
b
#define b
Definition: input.c:40
table
static const uint16_t table[]
Definition: prosumer.c:206
data
const char data[16]
Definition: mxf.c:143
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:798
read_wave_data
static int read_wave_data(QDMCContext *s, GetBitContext *gb)
Definition: qdmc.c:420
INIT_VLC_LE
#define INIT_VLC_LE
Definition: vlc.h:94
QDMCContext::cmplx
FFTComplex cmplx[2][512]
Definition: qdmc.c:69
QDMCTone::freq
int16_t freq
Definition: qdmc.c:41
QDMCTone
Definition: qdmc.c:37
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
QDMCContext::alt_sin
float alt_sin[5][31]
Definition: qdmc.c:61
qdmc_decode_frame
static int qdmc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: qdmc.c:695
VLC_TYPE
#define VLC_TYPE
Definition: vlc.h:24
U
#define U(x)
Definition: vp56_arith.h:37
qdmc_get_vlc
static int qdmc_get_vlc(GetBitContext *gb, VLC *table, int flag)
Definition: qdmc.c:319
GetBitContext
Definition: get_bits.h:62
amplitude_tab
static const float amplitude_tab[64]
Definition: qdmc.c:88
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
ff_init_vlc_from_lengths
int ff_init_vlc_from_lengths(VLC *vlc_arg, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: bitstream.c:381
qdmc_decode_close
static av_cold int qdmc_decode_close(AVCodecContext *avctx)
Definition: qdmc.c:310
QDMCContext::frame_size
int frame_size
Definition: qdmc.c:50
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
sin_table
static float sin_table[512]
Definition: qdmc.c:73
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:481
QDMCContext::subframe_size
int subframe_size
Definition: qdmc.c:51
s
#define s(width, name)
Definition: cbs_vp9.c:257
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
g
const char * g
Definition: vf_curves.c:117
QDMCContext::cur_tone
int cur_tone[5]
Definition: qdmc.c:60
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
qdmc_decode_init
static av_cold int qdmc_decode_init(AVCodecContext *avctx)
Definition: qdmc.c:205
ff_fft_end
#define ff_fft_end
Definition: fft.h:144
QDMCContext::buffer_ptr
float * buffer_ptr
Definition: qdmc.c:66
QDMCContext
Definition: qdmc.c:45
qdmc_hufftab
static const uint8_t qdmc_hufftab[][2]
Definition: qdmc.c:122
get_bits.h
QDMCContext::band_index
int band_index
Definition: qdmc.c:49
decode_frame
static int decode_frame(QDMCContext *s, GetBitContext *gb, int16_t *out)
Definition: qdmc.c:616
f
#define f(width, name)
Definition: cbs_vp9.c:255
QDMCTone::amplitude
int16_t amplitude
Definition: qdmc.c:42
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:63
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:173
add_wave0
static void add_wave0(QDMCContext *s, int offset, int freqs, int stereo_mode, int amp, int phase)
Definition: qdmc.c:574
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
NULL
#define NULL
Definition: coverity.c:32
add_noise
static void add_noise(QDMCContext *s, int ch, int current_subframe)
Definition: qdmc.c:508
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
QDMCContext::rndval
int rndval
Definition: qdmc.c:67
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:429
av_clipf
#define av_clipf
Definition: common.h:170
QDMCContext::noise
uint8_t noise[2][19][17]
Definition: qdmc.c:57
QDMCTone::offset
uint8_t offset
Definition: qdmc.c:40
vtable
static VLC vtable[6]
Definition: qdmc.c:74
AVOnce
#define AVOnce
Definition: thread.h:172
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:104
QDMCContext::fft_offset
int fft_offset
Definition: qdmc.c:52
VLC::table_allocated
int table_allocated
Definition: vlc.h:29
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1648
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
add_wave
static void add_wave(QDMCContext *s, int offset, int freqs, int group, int stereo_mode, int amp, int phase)
Definition: qdmc.c:542
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:996
size
int size
Definition: twinvq_data.h:10344
AV_CODEC_ID_QDMC
@ AV_CODEC_ID_QDMC
Definition: codec_id.h:472
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: common.h:479
QDMCTone::mode
uint8_t mode
Definition: qdmc.c:38
QDMCContext::buffer_offset
int buffer_offset
Definition: qdmc.c:53
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
QDMCContext::checksum_size
int checksum_size
Definition: qdmc.c:55
code_prefix
static const unsigned code_prefix[]
Definition: qdmc.c:76
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
huff_sizes
static const uint8_t huff_sizes[]
Definition: qdmc.c:155
QDMCContext::fft_ctx
FFTContext fft_ctx
Definition: qdmc.c:70
M_PI
#define M_PI
Definition: mathematics.h:52
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:989
flag
#define flag(name)
Definition: cbs_av1.c:553
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:116
FFTContext
Definition: fft.h:83
i
int i
Definition: input.c:406
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:480
QDMCContext::avctx
AVCodecContext * avctx
Definition: qdmc.c:46
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
len
int len
Definition: vorbis_enc_data.h:426
avcodec.h
ret
ret
Definition: filter_design.txt:187
INIT_VLC_STATIC_OVERLONG
#define INIT_VLC_STATIC_OVERLONG
Definition: vlc.h:96
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
QDMCContext::tones
QDMCTone tones[5][8192]
Definition: qdmc.c:58
huff_bits
static const uint8_t huff_bits[]
Definition: qdmc.c:159
pos
unsigned int pos
Definition: spdifenc.c:412
checksum
static volatile int checksum
Definition: adler32.c:30
qdmc_flush
static av_cold void qdmc_flush(AVCodecContext *avctx)
Definition: qdmc.c:685
QDMCContext::frame_bits
uint8_t frame_bits
Definition: qdmc.c:48
fft.h
AVCodecContext
main external API structure.
Definition: avcodec.h:379
channel_layout.h
VLC
Definition: vlc.h:26
qdmc_init_static_data
static av_cold void qdmc_init_static_data(void)
Definition: qdmc.c:163
add_tone
static void add_tone(QDMCContext *s, int group, int offset, int freq, int stereo_mode, int amplitude, int phase)
Definition: qdmc.c:403
VLC::table_size
int table_size
Definition: vlc.h:29
get_bitsz
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:416
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
llrint
#define llrint(x)
Definition: libm.h:394
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:136
qdmc_nodes
static const uint16_t qdmc_nodes[112]
Definition: qdmc.c:101
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:406
QDMCContext::nb_tones
int nb_tones[5]
Definition: qdmc.c:59
read_noise_data
static int read_noise_data(QDMCContext *s, GetBitContext *gb)
Definition: qdmc.c:355
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
VLC::table
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
ff_qdmc_decoder
const AVCodec ff_qdmc_decoder
Definition: qdmc.c:728
QDMCTone::phase
uint8_t phase
Definition: qdmc.c:39
FFTComplex
Definition: avfft.h:37
noise_bands_size
static const uint8_t noise_bands_size[]
Definition: qdmc.c:114
re
float re
Definition: fft.c:82