FFmpeg  3.4.9
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 
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "get_bits.h"
33 #include "internal.h"
34 #include "fft.h"
35 
36 typedef struct QDMCTone {
40  int16_t freq;
41  int16_t amplitude;
42 } QDMCTone;
43 
44 typedef struct QDMCContext {
46 
55 
56  uint8_t noise[2][19][17];
57  QDMCTone tones[5][8192];
58  int nb_tones[5];
59  int cur_tone[5];
60  float alt_sin[5][31];
61  float fft_buffer[4][8192 * 2];
62  float noise2_buffer[4096 * 2];
63  float noise_buffer[4096 * 2];
64  float buffer[2 * 32768];
65  float *buffer_ptr;
66  int rndval;
67 
68  DECLARE_ALIGNED(32, FFTComplex, cmplx)[2][512];
70 } QDMCContext;
71 
72 static float sin_table[512];
73 static VLC vtable[6];
74 
75 static const unsigned code_prefix[] = {
76  0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
77  0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
78  0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
79  0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
80  0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
81  0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
82  0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
83  0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
84  0x3FFFC
85 };
86 
87 static const float amplitude_tab[64] = {
88  1.18750000f, 1.68359380f, 2.37500000f, 3.36718750f, 4.75000000f,
89  6.73437500f, 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f,
90  38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, 152.000000f,
91  215.500000f, 304.000000f, 431.000000f, 608.000000f, 862.000000f,
92  1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, 4864.00000f,
93  6896.00000f, 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f,
94  38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, 155648.000f,
95  220672.000f, 311296.000f, 441344.000f, 622592.000f, 882688.000f,
96  1245184.00f, 1765376.00f, 2490368.00f, 3530752.00f, 4980736.00f,
97  7061504.00f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 };
99 
100 static const uint16_t qdmc_nodes[112] = {
101  0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64,
102  80, 96, 120, 144, 176, 208, 240, 256,
103  0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104,
104  128, 160, 208, 256, 0, 0, 0, 0, 0,
105  0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208,
106  256, 0, 0, 0, 0, 0, 0, 0, 0,
107  0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256,
108  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109  0, 4, 16, 32, 64, 256, 0, 0, 0, 0, 0, 0, 0, 0,
110  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
111 };
112 
113 static const uint8_t noise_bands_size[] = {
114  19, 14, 11, 9, 4, 2, 0
115 };
116 
117 static const uint8_t noise_bands_selector[] = {
118  4, 3, 2, 1, 0, 0, 0,
119 };
120 
121 static const uint8_t noise_value_bits[] = {
122  12, 7, 9, 7, 10, 9, 11, 9, 9, 2, 9, 9, 9, 9,
123  9, 3, 9, 10, 10, 12, 2, 3, 3, 5, 5, 6, 7,
124 };
125 
126 static const uint8_t noise_value_symbols[] = {
127  0, 10, 11, 12, 13, 14, 15, 16, 18, 1, 20, 22, 24,
128  26, 28, 2, 30, 32, 34, 36, 3, 4, 5, 6, 7, 8, 9,
129 };
130 
131 static const uint16_t noise_value_codes[] = {
132  0xC7A, 0x002, 0x0FA, 0x03A, 0x35A, 0x1C2, 0x07A, 0x1FA,
133  0x17A, 0x000, 0x0DA, 0x142, 0x0C2, 0x042, 0x1DA, 0x001,
134  0x05A, 0x15A, 0x27A, 0x47A, 0x003, 0x005, 0x006, 0x012,
135  0x00A, 0x022, 0x01A,
136 };
137 
139  10, 8, 5, 1, 2, 4, 4, 4, 6, 7, 9, 10,
140 };
141 
143  0, 13, 17, 1, 2, 3, 4, 5, 6, 7, 8, 9,
144 };
145 
146 static const uint16_t noise_segment_length_codes[] = {
147  0x30B, 0x8B, 0x1B, 0x0, 0x1, 0x3, 0x7, 0xF, 0x2b, 0x4B, 0xB, 0x10B,
148 };
149 
150 static const uint8_t freq_diff_bits[] = {
151  18, 2, 4, 4, 5, 4, 4, 5, 5, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 6,
152  7, 6, 6, 6, 7, 7, 7, 7, 7, 8, 9, 9, 8, 9, 11, 11, 12, 12, 13, 12,
153  14, 15, 18, 16, 17,
154 };
155 
156 static const uint32_t freq_diff_codes[] = {
157  0x2AD46, 0x1, 0x0, 0x3, 0xC, 0xA, 0x7, 0x18, 0x12, 0xE, 0x4, 0x16,
158  0xF, 0x1C, 0x8, 0x22, 0x26, 0x2, 0x3B, 0x34, 0x74, 0x1F, 0x14, 0x2B,
159  0x1B, 0x3F, 0x28, 0x54, 0x6, 0x4B, 0xB, 0x68, 0xE8, 0x46, 0xC6, 0x1E8,
160  0x146, 0x346, 0x546, 0x746, 0x1D46, 0xF46, 0xD46, 0x6D46, 0xAD46, 0x2D46,
161  0x1AD46,
162 };
163 
164 static const uint8_t amplitude_bits[] = {
165  13, 7, 8, 9, 10, 10, 10, 10, 10, 9, 8, 7, 6,
166  5, 4, 3, 3, 2, 3, 3, 4, 5, 7, 8, 9, 11, 12, 13,
167 };
168 
169 static const uint16_t amplitude_codes[] = {
170  0x1EC6, 0x6, 0xC2, 0x142, 0x242, 0x246, 0xC6, 0x46, 0x42, 0x146, 0xA2,
171  0x62, 0x26, 0x16, 0xE, 0x5, 0x4, 0x3, 0x0, 0x1, 0xA, 0x12, 0x2, 0x22,
172  0x1C6, 0x2C6, 0x6C6, 0xEC6,
173 };
174 
175 static const uint8_t amplitude_diff_bits[] = {
176  8, 2, 1, 3, 4, 5, 6, 7, 8,
177 };
178 
179 static const uint8_t amplitude_diff_codes[] = {
180  0xFE, 0x0, 0x1, 0x2, 0x6, 0xE, 0x1E, 0x3E, 0x7E,
181 };
182 
183 static const uint8_t phase_diff_bits[] = {
184  6, 2, 2, 4, 4, 6, 5, 4, 2,
185 };
186 
187 static const uint8_t phase_diff_codes[] = {
188  0x35, 0x2, 0x0, 0x1, 0xD, 0x15, 0x5, 0x9, 0x3,
189 };
190 
191 #define INIT_VLC_STATIC_LE(vlc, nb_bits, nb_codes, \
192  bits, bits_wrap, bits_size, \
193  codes, codes_wrap, codes_size, \
194  symbols, symbols_wrap, symbols_size, \
195  static_size) \
196  do { \
197  static VLC_TYPE table[static_size][2]; \
198  (vlc)->table = table; \
199  (vlc)->table_allocated = static_size; \
200  ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
201  bits, bits_wrap, bits_size, \
202  codes, codes_wrap, codes_size, \
203  symbols, symbols_wrap, symbols_size, \
204  INIT_VLC_LE | INIT_VLC_USE_NEW_STATIC); \
205  } while (0)
206 
208 {
209  int i;
210 
212  noise_value_bits, 1, 1, noise_value_codes, 2, 2, noise_value_symbols, 1, 1, 4096);
215  noise_segment_length_symbols, 1, 1, 1024);
217  amplitude_bits, 1, 1, amplitude_codes, 2, 2, NULL, 0, 0, 8192);
219  freq_diff_bits, 1, 1, freq_diff_codes, 4, 4, NULL, 0, 0, 262144);
221  amplitude_diff_bits, 1, 1, amplitude_diff_codes, 1, 1, NULL, 0, 0, 256);
223  phase_diff_bits, 1, 1, phase_diff_codes, 1, 1, NULL, 0, 0, 64);
224 
225  for (i = 0; i < 512; i++)
226  sin_table[i] = sin(2.0f * i * M_PI * 0.001953125f);
227 }
228 
229 static void make_noises(QDMCContext *s)
230 {
231  int i, j, n0, n1, n2, diff;
232  float *nptr;
233 
234  for (j = 0; j < noise_bands_size[s->band_index]; j++) {
235  n0 = qdmc_nodes[j + 21 * s->band_index ];
236  n1 = qdmc_nodes[j + 21 * s->band_index + 1];
237  n2 = qdmc_nodes[j + 21 * s->band_index + 2];
238  nptr = s->noise_buffer + 256 * j;
239 
240  for (i = 0; i + n0 < n1; i++, nptr++)
241  nptr[0] = i / (float)(n1 - n0);
242 
243  diff = n2 - n1;
244  nptr = s->noise_buffer + (j << 8) + n1 - n0;
245 
246  for (i = n1; i < n2; i++, nptr++, diff--)
247  nptr[0] = diff / (float)(n2 - n1);
248  }
249 }
250 
252 {
253  QDMCContext *s = avctx->priv_data;
254  int fft_size, fft_order, size, g, j, x;
256 
257  if (!avctx->extradata || (avctx->extradata_size < 48)) {
258  av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
259  return AVERROR_INVALIDDATA;
260  }
261 
262  bytestream2_init(&b, avctx->extradata, avctx->extradata_size);
263 
264  while (bytestream2_get_bytes_left(&b) > 8) {
265  if (bytestream2_peek_be64(&b) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
266  (uint64_t)MKBETAG('Q','D','M','C')))
267  break;
268  bytestream2_skipu(&b, 1);
269  }
270  bytestream2_skipu(&b, 8);
271 
272  if (bytestream2_get_bytes_left(&b) < 36) {
273  av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
275  return AVERROR_INVALIDDATA;
276  }
277 
278  size = bytestream2_get_be32u(&b);
279  if (size > bytestream2_get_bytes_left(&b)) {
280  av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
281  bytestream2_get_bytes_left(&b), size);
282  return AVERROR_INVALIDDATA;
283  }
284 
285  if (bytestream2_get_be32u(&b) != MKBETAG('Q','D','C','A')) {
286  av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
287  return AVERROR_INVALIDDATA;
288  }
289  bytestream2_skipu(&b, 4);
290 
291  avctx->channels = s->nb_channels = bytestream2_get_be32u(&b);
292  if (s->nb_channels <= 0 || s->nb_channels > 2) {
293  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
294  return AVERROR_INVALIDDATA;
295  }
296  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
298 
299  avctx->sample_rate = bytestream2_get_be32u(&b);
300  avctx->bit_rate = bytestream2_get_be32u(&b);
301  bytestream2_skipu(&b, 4);
302  fft_size = bytestream2_get_be32u(&b);
303  fft_order = av_log2(fft_size) + 1;
304  s->checksum_size = bytestream2_get_be32u(&b);
305  if (s->checksum_size >= 1U << 28) {
306  av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
307  return AVERROR_INVALIDDATA;
308  }
309 
310  if (avctx->sample_rate >= 32000) {
311  x = 28000;
312  s->frame_bits = 13;
313  } else if (avctx->sample_rate >= 16000) {
314  x = 20000;
315  s->frame_bits = 12;
316  } else {
317  x = 16000;
318  s->frame_bits = 11;
319  }
320  s->frame_size = 1 << s->frame_bits;
321  s->subframe_size = s->frame_size >> 5;
322 
323  if (avctx->channels == 2)
324  x = 3 * x / 2;
325  s->band_index = noise_bands_selector[FFMIN(6, llrint(floor(avctx->bit_rate * 3.0 / (double)x + 0.5)))];
326 
327  if ((fft_order < 7) || (fft_order > 9)) {
328  avpriv_request_sample(avctx, "Unknown FFT order %d", fft_order);
329  return AVERROR_PATCHWELCOME;
330  }
331 
332  if (fft_size != (1 << (fft_order - 1))) {
333  av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", fft_size);
334  return AVERROR_INVALIDDATA;
335  }
336 
337  ff_fft_init(&s->fft_ctx, fft_order, 1);
338 
339  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
340 
341  for (g = 5; g > 0; g--) {
342  for (j = 0; j < (1 << g) - 1; j++)
343  s->alt_sin[5-g][j] = sin_table[(((j+1) << (8 - g)) & 0x1FF)];
344  }
345 
346  make_noises(s);
347 
348  return 0;
349 }
350 
352 {
353  QDMCContext *s = avctx->priv_data;
354 
355  ff_fft_end(&s->fft_ctx);
356 
357  return 0;
358 }
359 
360 static int qdmc_get_vlc(GetBitContext *gb, VLC *table, int flag)
361 {
362  int v;
363 
364  if (get_bits_left(gb) < 1)
365  return AVERROR_INVALIDDATA;
366  v = get_vlc2(gb, table->table, table->bits, 1);
367  if (v < 0)
368  return AVERROR_INVALIDDATA;
369  if (v)
370  v = v - 1;
371  else
372  v = get_bits(gb, get_bits(gb, 3) + 1);
373 
374  if (flag) {
375  if (v >= FF_ARRAY_ELEMS(code_prefix))
376  return AVERROR_INVALIDDATA;
377 
378  v = code_prefix[v] + get_bitsz(gb, v >> 2);
379  }
380 
381  return v;
382 }
383 
385 {
386  uint32_t label = get_bits_long(gb, 32);
387  uint16_t sum = 226, checksum = get_bits(gb, 16);
388  const uint8_t *ptr = gb->buffer + 6;
389  int i;
390 
391  if (label != MKTAG('Q', 'M', 'C', 1))
392  return AVERROR_INVALIDDATA;
393 
394  for (i = 0; i < s->checksum_size - 6; i++)
395  sum += ptr[i];
396 
397  return sum != checksum;
398 }
399 
401 {
402  int ch, j, k, v, idx, band, lastval, newval, len;
403 
404  for (ch = 0; ch < s->nb_channels; ch++) {
405  for (band = 0; band < noise_bands_size[s->band_index]; band++) {
406  v = qdmc_get_vlc(gb, &vtable[0], 0);
407  if (v < 0)
408  return AVERROR_INVALIDDATA;
409 
410  if (v & 1)
411  v = v + 1;
412  else
413  v = -v;
414 
415  lastval = v / 2;
416  s->noise[ch][band][0] = lastval - 1;
417  for (j = 0; j < 15;) {
418  len = qdmc_get_vlc(gb, &vtable[1], 1);
419  if (len < 0)
420  return AVERROR_INVALIDDATA;
421  len += 1;
422 
423  v = qdmc_get_vlc(gb, &vtable[0], 0);
424  if (v < 0)
425  return AVERROR_INVALIDDATA;
426 
427  if (v & 1)
428  newval = lastval + (v + 1) / 2;
429  else
430  newval = lastval - v / 2;
431 
432  idx = j + 1;
433  if (len + idx > 16)
434  return AVERROR_INVALIDDATA;
435 
436  for (k = 1; idx <= j + len; k++, idx++)
437  s->noise[ch][band][idx] = lastval + k * (newval - lastval) / len - 1;
438 
439  lastval = newval;
440  j += len;
441  }
442  }
443  }
444 
445  return 0;
446 }
447 
448 static void add_tone(QDMCContext *s, int group, int offset, int freq, int stereo_mode, int amplitude, int phase)
449 {
450  const int index = s->nb_tones[group];
451 
452  if (index >= FF_ARRAY_ELEMS(s->tones[group])) {
453  av_log(s->avctx, AV_LOG_WARNING, "Too many tones already in buffer, ignoring tone!\n");
454  return;
455  }
456 
457  s->tones[group][index].offset = offset;
458  s->tones[group][index].freq = freq;
459  s->tones[group][index].mode = stereo_mode;
460  s->tones[group][index].amplitude = amplitude;
461  s->tones[group][index].phase = phase;
462  s->nb_tones[group]++;
463 }
464 
466 {
467  int amp, phase, stereo_mode = 0, i, group, freq, group_size, group_bits;
468  int amp2, phase2, pos2, off;
469 
470  for (group = 0; group < 5; group++) {
471  group_size = 1 << (s->frame_bits - group - 1);
472  group_bits = 4 - group;
473  pos2 = 0;
474  off = 0;
475 
476  for (i = 1; ; i = freq + 1) {
477  int v;
478 
479  v = qdmc_get_vlc(gb, &vtable[3], 1);
480  if (v < 0)
481  return AVERROR_INVALIDDATA;
482 
483  freq = i + v;
484  while (freq >= group_size - 1) {
485  freq += 2 - group_size;
486  pos2 += group_size;
487  off += 1 << group_bits;
488  }
489 
490  if (pos2 >= s->frame_size)
491  break;
492 
493  if (s->nb_channels > 1)
494  stereo_mode = get_bits(gb, 2);
495 
496  amp = qdmc_get_vlc(gb, &vtable[2], 0);
497  if (amp < 0)
498  return AVERROR_INVALIDDATA;
499  phase = get_bits(gb, 3);
500 
501  if (stereo_mode > 1) {
502  amp2 = qdmc_get_vlc(gb, &vtable[4], 0);
503  if (amp2 < 0)
504  return AVERROR_INVALIDDATA;
505  amp2 = amp - amp2;
506 
507  phase2 = qdmc_get_vlc(gb, &vtable[5], 0);
508  if (phase2 < 0)
509  return AVERROR_INVALIDDATA;
510  phase2 = phase - phase2;
511 
512  if (phase2 < 0)
513  phase2 += 8;
514  }
515 
516  if ((freq >> group_bits) + 1 < s->subframe_size) {
517  add_tone(s, group, off, freq, stereo_mode & 1, amp, phase);
518  if (stereo_mode > 1)
519  add_tone(s, group, off, freq, ~stereo_mode & 1, amp2, phase2);
520  }
521  }
522  }
523 
524  return 0;
525 }
526 
527 static void lin_calc(QDMCContext *s, float amplitude, int node1, int node2, int index)
528 {
529  int subframe_size, i, j, k, length;
530  float scale, *noise_ptr;
531 
532  scale = 0.5 * amplitude;
533  subframe_size = s->subframe_size;
534  if (subframe_size >= node2)
535  subframe_size = node2;
536  length = (subframe_size - node1) & 0xFFFC;
537  j = node1;
538  noise_ptr = &s->noise_buffer[256 * index];
539 
540  for (i = 0; i < length; i += 4, j+= 4, noise_ptr += 4) {
541  s->noise2_buffer[j ] += scale * noise_ptr[0];
542  s->noise2_buffer[j + 1] += scale * noise_ptr[1];
543  s->noise2_buffer[j + 2] += scale * noise_ptr[2];
544  s->noise2_buffer[j + 3] += scale * noise_ptr[3];
545  }
546 
547  k = length + node1;
548  noise_ptr = s->noise_buffer + length + (index << 8);
549  for (i = length; i < subframe_size - node1; i++, k++, noise_ptr++)
550  s->noise2_buffer[k] += scale * noise_ptr[0];
551 }
552 
553 static void add_noise(QDMCContext *s, int ch, int current_subframe)
554 {
555  int i, j, aindex;
556  float amplitude;
557  float *im = &s->fft_buffer[0 + ch][s->fft_offset + s->subframe_size * current_subframe];
558  float *re = &s->fft_buffer[2 + ch][s->fft_offset + s->subframe_size * current_subframe];
559 
560  memset(s->noise2_buffer, 0, 4 * s->subframe_size);
561 
562  for (i = 0; i < noise_bands_size[s->band_index]; i++) {
563  if (qdmc_nodes[i + 21 * s->band_index] > s->subframe_size - 1)
564  break;
565 
566  aindex = s->noise[ch][i][current_subframe / 2];
567  amplitude = aindex > 0 ? amplitude_tab[aindex & 0x3F] : 0.0f;
568 
569  lin_calc(s, amplitude, qdmc_nodes[21 * s->band_index + i],
570  qdmc_nodes[21 * s->band_index + i + 2], i);
571  }
572 
573  for (j = 2; j < s->subframe_size - 1; j++) {
574  float rnd_re, rnd_im;
575 
576  s->rndval = 214013U * s->rndval + 2531011;
577  rnd_im = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
578  s->rndval = 214013U * s->rndval + 2531011;
579  rnd_re = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
580  im[j ] += rnd_im;
581  re[j ] += rnd_re;
582  im[j+1] -= rnd_im;
583  re[j+1] -= rnd_re;
584  }
585 }
586 
587 static void add_wave(QDMCContext *s, int offset, int freqs, int group, int stereo_mode, int amp, int phase)
588 {
589  int j, group_bits, pos, pindex;
590  float im, re, amplitude, level, *imptr, *reptr;
591 
592  if (s->nb_channels == 1)
593  stereo_mode = 0;
594 
595  group_bits = 4 - group;
596  pos = freqs >> (4 - group);
597  amplitude = amplitude_tab[amp & 0x3F];
598  imptr = &s->fft_buffer[ stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
599  reptr = &s->fft_buffer[2 + stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
600  pindex = (phase << 6) - ((2 * (freqs >> (4 - group)) + 1) << 7);
601  for (j = 0; j < (1 << (group_bits + 1)) - 1; j++) {
602  pindex += (2 * freqs + 1) << (7 - group_bits);
603  level = amplitude * s->alt_sin[group][j];
604  im = level * sin_table[ pindex & 0x1FF];
605  re = level * sin_table[(pindex + 128) & 0x1FF];
606  imptr[0] += im;
607  imptr[1] -= im;
608  reptr[0] += re;
609  reptr[1] -= re;
610  imptr += s->subframe_size;
611  reptr += s->subframe_size;
612  if (imptr >= &s->fft_buffer[stereo_mode][2 * s->frame_size]) {
613  imptr = &s->fft_buffer[0 + stereo_mode][pos];
614  reptr = &s->fft_buffer[2 + stereo_mode][pos];
615  }
616  }
617 }
618 
619 static void add_wave0(QDMCContext *s, int offset, int freqs, int stereo_mode, int amp, int phase)
620 {
621  float level, im, re;
622  int pos;
623 
624  if (s->nb_channels == 1)
625  stereo_mode = 0;
626 
627  level = amplitude_tab[amp & 0x3F];
628  im = level * sin_table[ (phase << 6) & 0x1FF];
629  re = level * sin_table[((phase << 6) + 128) & 0x1FF];
630  pos = s->fft_offset + freqs + s->subframe_size * offset;
631  s->fft_buffer[ stereo_mode][pos ] += im;
632  s->fft_buffer[2 + stereo_mode][pos ] += re;
633  s->fft_buffer[ stereo_mode][pos + 1] -= im;
634  s->fft_buffer[2 + stereo_mode][pos + 1] -= re;
635 }
636 
637 static void add_waves(QDMCContext *s, int current_subframe)
638 {
639  int w, g;
640 
641  for (g = 0; g < 4; g++) {
642  for (w = s->cur_tone[g]; w < s->nb_tones[g]; w++) {
643  QDMCTone *t = &s->tones[g][w];
644 
645  if (current_subframe < t->offset)
646  break;
647  add_wave(s, t->offset, t->freq, g, t->mode, t->amplitude, t->phase);
648  }
649  s->cur_tone[g] = w;
650  }
651  for (w = s->cur_tone[4]; w < s->nb_tones[4]; w++) {
652  QDMCTone *t = &s->tones[4][w];
653 
654  if (current_subframe < t->offset)
655  break;
656  add_wave0(s, t->offset, t->freq, t->mode, t->amplitude, t->phase);
657  }
658  s->cur_tone[4] = w;
659 }
660 
661 static int decode_frame(QDMCContext *s, GetBitContext *gb, int16_t *out)
662 {
663  int ret, ch, i, n;
664 
665  if (skip_label(s, gb))
666  return AVERROR_INVALIDDATA;
667 
668  s->fft_offset = s->frame_size - s->fft_offset;
669  s->buffer_ptr = &s->buffer[s->nb_channels * s->buffer_offset];
670 
671  ret = read_noise_data(s, gb);
672  if (ret < 0)
673  return ret;
674 
675  ret = read_wave_data(s, gb);
676  if (ret < 0)
677  return ret;
678 
679  for (n = 0; n < 32; n++) {
680  float *r;
681 
682  for (ch = 0; ch < s->nb_channels; ch++)
683  add_noise(s, ch, n);
684 
685  add_waves(s, n);
686 
687  for (ch = 0; ch < s->nb_channels; ch++) {
688  for (i = 0; i < s->subframe_size; i++) {
689  s->cmplx[ch][i].re = s->fft_buffer[ch + 2][s->fft_offset + n * s->subframe_size + i];
690  s->cmplx[ch][i].im = s->fft_buffer[ch + 0][s->fft_offset + n * s->subframe_size + i];
691  s->cmplx[ch][s->subframe_size + i].re = 0;
692  s->cmplx[ch][s->subframe_size + i].im = 0;
693  }
694  }
695 
696  for (ch = 0; ch < s->nb_channels; ch++) {
697  s->fft_ctx.fft_permute(&s->fft_ctx, s->cmplx[ch]);
698  s->fft_ctx.fft_calc(&s->fft_ctx, s->cmplx[ch]);
699  }
700 
701  r = &s->buffer_ptr[s->nb_channels * n * s->subframe_size];
702  for (i = 0; i < 2 * s->subframe_size; i++) {
703  for (ch = 0; ch < s->nb_channels; ch++) {
704  *r++ += s->cmplx[ch][i].re;
705  }
706  }
707 
708  r = &s->buffer_ptr[n * s->subframe_size * s->nb_channels];
709  for (i = 0; i < s->nb_channels * s->subframe_size; i++) {
710  out[i] = av_clipf(r[i], INT16_MIN, INT16_MAX);
711  }
712  out += s->subframe_size * s->nb_channels;
713 
714  for (ch = 0; ch < s->nb_channels; ch++) {
715  memset(s->fft_buffer[ch+0] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
716  memset(s->fft_buffer[ch+2] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
717  }
718  memset(s->buffer + s->nb_channels * (n * s->subframe_size + s->frame_size + s->buffer_offset), 0, 4 * s->subframe_size * s->nb_channels);
719  }
720 
721  s->buffer_offset += s->frame_size;
722  if (s->buffer_offset >= 32768 - s->frame_size) {
723  memcpy(s->buffer, &s->buffer[s->nb_channels * s->buffer_offset], 4 * s->frame_size * s->nb_channels);
724  s->buffer_offset = 0;
725  }
726 
727  return 0;
728 }
729 
730 static av_cold void qdmc_flush(AVCodecContext *avctx)
731 {
732  QDMCContext *s = avctx->priv_data;
733 
734  memset(s->buffer, 0, sizeof(s->buffer));
735  memset(s->fft_buffer, 0, sizeof(s->fft_buffer));
736  s->fft_offset = 0;
737  s->buffer_offset = 0;
738 }
739 
740 static int qdmc_decode_frame(AVCodecContext *avctx, void *data,
741  int *got_frame_ptr, AVPacket *avpkt)
742 {
743  QDMCContext *s = avctx->priv_data;
744  AVFrame *frame = data;
745  GetBitContext gb;
746  int ret;
747 
748  if (!avpkt->data)
749  return 0;
750  if (avpkt->size < s->checksum_size)
751  return AVERROR_INVALIDDATA;
752 
753  s->avctx = avctx;
754  frame->nb_samples = s->frame_size;
755  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
756  return ret;
757 
758  if ((ret = init_get_bits8(&gb, avpkt->data, s->checksum_size)) < 0)
759  return ret;
760 
761  memset(s->nb_tones, 0, sizeof(s->nb_tones));
762  memset(s->cur_tone, 0, sizeof(s->cur_tone));
763 
764  ret = decode_frame(s, &gb, (int16_t *)frame->data[0]);
765  if (ret >= 0) {
766  *got_frame_ptr = 1;
767  return s->checksum_size;
768  }
769  qdmc_flush(avctx);
770  return ret;
771 }
772 
774  .name = "qdmc",
775  .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 1"),
776  .type = AVMEDIA_TYPE_AUDIO,
777  .id = AV_CODEC_ID_QDMC,
778  .priv_data_size = sizeof(QDMCContext),
780  .init_static_data = qdmc_init_static_data,
781  .close = qdmc_decode_close,
783  .flush = qdmc_flush,
784  .capabilities = AV_CODEC_CAP_DR1,
785 };
static const uint32_t freq_diff_codes[]
Definition: qdmc.c:156
static const uint16_t qdmc_nodes[112]
Definition: qdmc.c:100
static const uint16_t amplitude_codes[]
Definition: qdmc.c:169
#define NULL
Definition: coverity.c:32
static int qdmc_get_vlc(GetBitContext *gb, VLC *table, int flag)
Definition: qdmc.c:360
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
static int noise(AVBSFContext *ctx, AVPacket *out)
Definition: noise_bsf.c:38
static void flush(AVCodecContext *avctx)
AVCodec ff_qdmc_decoder
Definition: qdmc.c:773
float buffer[2 *32768]
Definition: qdmc.c:64
float re
Definition: fft.c:82
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:262
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1826
const char * g
Definition: vf_curves.c:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const uint8_t noise_bands_size[]
Definition: qdmc.c:113
QDMCTone tones[5][8192]
Definition: qdmc.c:57
#define avpriv_request_sample(...)
static VLC vtable[6]
Definition: qdmc.c:73
static const uint16_t noise_segment_length_codes[]
Definition: qdmc.c:146
int rndval
Definition: qdmc.c:66
int size
Definition: avcodec.h:1680
const char * b
Definition: vf_curves.c:113
int flag
Definition: cpu.c:34
const uint8_t * buffer
Definition: get_bits.h:57
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
FFTSample re
Definition: avfft.h:38
int nb_tones[5]
Definition: qdmc.c:58
FFTComplex cmplx[2][512]
Definition: qdmc.c:68
#define AV_CH_LAYOUT_STEREO
Definition: qdmc.c:36
AVCodec.
Definition: avcodec.h:3739
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static float sin_table[512]
Definition: qdmc.c:72
static av_cold int qdmc_decode_close(AVCodecContext *avctx)
Definition: qdmc.c:351
static char buffer[20]
Definition: seek.c:32
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2531
uint8_t
#define av_cold
Definition: attributes.h:82
static const uint8_t amplitude_bits[]
Definition: qdmc.c:164
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1876
static AVFrame * frame
const char data[16]
Definition: mxf.c:90
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:104
uint8_t * data
Definition: avcodec.h:1679
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:170
bitstream reader API header.
static av_cold void qdmc_flush(AVCodecContext *avctx)
Definition: qdmc.c:730
#define av_log(a,...)
static const uint8_t phase_diff_bits[]
Definition: qdmc.c:183
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:589
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* fft_permute)(struct FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling fft_calc().
Definition: fft.h:101
float fft_buffer[4][8192 *2]
Definition: qdmc.c:61
static const struct endianess table[]
static const uint8_t noise_bands_selector[]
Definition: qdmc.c:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:181
const char * r
Definition: vf_curves.c:111
uint8_t frame_bits
Definition: qdmc.c:47
int16_t amplitude
Definition: qdmc.c:41
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
static void add_wave(QDMCContext *s, int offset, int freqs, int group, int stereo_mode, int amp, int phase)
Definition: qdmc.c:587
float alt_sin[5][31]
Definition: qdmc.c:60
static const uint8_t freq_diff_bits[]
Definition: qdmc.c:150
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2574
#define INIT_VLC_STATIC_LE(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, symbols, symbols_wrap, symbols_size, static_size)
Definition: qdmc.c:191
int subframe_size
Definition: qdmc.c:50
Definition: fft.h:88
int16_t freq
Definition: qdmc.c:40
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:96
static int decode_frame(QDMCContext *s, GetBitContext *gb, int16_t *out)
Definition: qdmc.c:661
uint8_t noise[2][19][17]
Definition: qdmc.c:56
#define ff_fft_init
Definition: fft.h:149
int buffer_offset
Definition: qdmc.c:52
static const uint8_t noise_segment_length_bits[]
Definition: qdmc.c:138
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:556
int n
Definition: avisynth_c.h:684
static const uint8_t amplitude_diff_codes[]
Definition: qdmc.c:179
static void add_noise(QDMCContext *s, int ch, int current_subframe)
Definition: qdmc.c:553
static volatile int checksum
Definition: adler32.c:30
FFTContext fft_ctx
Definition: qdmc.c:69
#define FF_ARRAY_ELEMS(a)
#define av_log2
Definition: intmath.h:83
int bits
Definition: vlc.h:27
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static const uint16_t noise_value_codes[]
Definition: qdmc.c:131
float * buffer_ptr
Definition: qdmc.c:65
static void add_tone(QDMCContext *s, int group, int offset, int freq, int stereo_mode, int amplitude, int phase)
Definition: qdmc.c:448
static int skip_label(QDMCContext *s, GetBitContext *gb)
Definition: qdmc.c:384
Libavcodec external API header.
int sample_rate
samples per second
Definition: avcodec.h:2523
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:457
static void make_noises(QDMCContext *s)
Definition: qdmc.c:229
uint8_t offset
Definition: qdmc.c:39
main external API structure.
Definition: avcodec.h:1761
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1669
static int read_noise_data(QDMCContext *s, GetBitContext *gb)
Definition: qdmc.c:400
int extradata_size
Definition: avcodec.h:1877
#define llrint(x)
Definition: libm.h:394
static const uint8_t noise_value_bits[]
Definition: qdmc.c:121
int index
Definition: gxfenc.c:89
static void lin_calc(QDMCContext *s, float amplitude, int node1, int node2, int index)
Definition: qdmc.c:527
float im
Definition: fft.c:82
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:347
float noise2_buffer[4096 *2]
Definition: qdmc.c:62
static const uint8_t phase_diff_codes[]
Definition: qdmc.c:187
static const uint8_t amplitude_diff_bits[]
Definition: qdmc.c:175
int band_index
Definition: qdmc.c:48
uint8_t mode
Definition: qdmc.c:37
static void add_waves(QDMCContext *s, int current_subframe)
Definition: qdmc.c:637
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
uint8_t level
Definition: svq3.c:207
static void add_wave0(QDMCContext *s, int offset, int freqs, int stereo_mode, int amp, int phase)
Definition: qdmc.c:619
common internal api header.
FFTSample im
Definition: avfft.h:38
uint8_t phase
Definition: qdmc.c:38
int frame_size
Definition: qdmc.c:49
signed 16 bits
Definition: samplefmt.h:61
int checksum_size
Definition: qdmc.c:54
static const float amplitude_tab[64]
Definition: qdmc.c:87
#define ff_fft_end
Definition: fft.h:150
void(* fft_calc)(struct FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in ff_fft_init().
Definition: fft.h:106
static const unsigned code_prefix[]
Definition: qdmc.c:75
static const uint8_t noise_value_symbols[]
Definition: qdmc.c:126
#define MKBETAG(a, b, c, d)
Definition: common.h:343
int nb_channels
Definition: qdmc.c:53
void * priv_data
Definition: avcodec.h:1803
static av_always_inline int diff(const uint32_t a, const uint32_t b)
int fft_offset
Definition: qdmc.c:51
int len
int channels
number of audio channels
Definition: avcodec.h:2524
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
AVCodecContext * avctx
Definition: qdmc.c:45
static av_cold void qdmc_init_static_data(AVCodec *codec)
Definition: qdmc.c:207
FILE * out
Definition: movenc.c:54
#define M_PI
Definition: mathematics.h:52
const char int length
Definition: avisynth_c.h:768
static av_cold int qdmc_decode_init(AVCodecContext *avctx)
Definition: qdmc.c:251
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:342
This structure stores compressed data.
Definition: avcodec.h:1656
int cur_tone[5]
Definition: qdmc.c:59
static int read_wave_data(QDMCContext *s, GetBitContext *gb)
Definition: qdmc.c:465
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:267
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002
static const uint8_t noise_segment_length_symbols[]
Definition: qdmc.c:142
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:277
float noise_buffer[4096 *2]
Definition: qdmc.c:63
static int qdmc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: qdmc.c:740