Libav
qdm2.c
Go to the documentation of this file.
1 /*
2  * QDM2 compatible decoder
3  * Copyright (c) 2003 Ewald Snel
4  * Copyright (c) 2005 Benjamin Larsson
5  * Copyright (c) 2005 Alex Beregszaszi
6  * Copyright (c) 2005 Roberto Togni
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
34 #include <math.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 
39 
40 #define BITSTREAM_READER_LE
41 #include "avcodec.h"
42 #include "get_bits.h"
43 #include "internal.h"
44 #include "mpegaudio.h"
45 #include "mpegaudiodsp.h"
46 #include "rdft.h"
47 
48 #include "qdm2data.h"
49 #include "qdm2_tablegen.h"
50 
51 
52 #define QDM2_LIST_ADD(list, size, packet) \
53 do { \
54  if (size > 0) { \
55  list[size - 1].next = &list[size]; \
56  } \
57  list[size].packet = packet; \
58  list[size].next = NULL; \
59  size++; \
60 } while(0)
61 
62 // Result is 8, 16 or 30
63 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
64 
65 #define FIX_NOISE_IDX(noise_idx) \
66  if ((noise_idx) >= 3840) \
67  (noise_idx) -= 3840; \
68 
69 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
70 
71 #define SAMPLES_NEEDED \
72  av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
73 
74 #define SAMPLES_NEEDED_2(why) \
75  av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
76 
77 #define QDM2_MAX_FRAME_SIZE 512
78 
79 typedef int8_t sb_int8_array[2][30][64];
80 
84 typedef struct QDM2SubPacket {
85  int type;
86  unsigned int size;
87  const uint8_t *data;
89 
93 typedef struct QDM2SubPNode {
95  struct QDM2SubPNode *next;
96 } QDM2SubPNode;
97 
98 typedef struct QDM2Complex {
99  float re;
100  float im;
101 } QDM2Complex;
102 
103 typedef struct FFTTone {
104  float level;
106  const float *table;
107  int phase;
109  int duration;
110  short time_index;
111  short cutoff;
112 } FFTTone;
113 
114 typedef struct FFTCoefficient {
115  int16_t sub_packet;
117  int16_t offset;
118  int16_t exp;
121 
122 typedef struct QDM2FFT {
124 } QDM2FFT;
125 
129 typedef struct QDM2Context {
132  int channels;
134  int fft_size;
136 
139  int fft_order;
145 
147  QDM2SubPacket sub_packets[16];
148  QDM2SubPNode sub_packet_list_A[16];
149  QDM2SubPNode sub_packet_list_B[16];
151  QDM2SubPNode sub_packet_list_C[16];
152  QDM2SubPNode sub_packet_list_D[16];
153 
155  FFTTone fft_tones[1000];
158  FFTCoefficient fft_coefs[1000];
160  int fft_coefs_min_index[5];
161  int fft_coefs_max_index[5];
162  int fft_level_exp[6];
165 
170 
173  DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2];
174  int synth_buf_offset[MPA_MAX_CHANNELS];
175  DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
177 
179  float tone_level[MPA_MAX_CHANNELS][30][64];
180  int8_t coding_method[MPA_MAX_CHANNELS][30][64];
181  int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
182  int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
183  int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
184  int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
185  int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
186  int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
187  int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
188 
189  // Flags
193 
195  int noise_idx;
196 } QDM2Context;
197 
198 
212 
213 static const uint16_t qdm2_vlc_offs[] = {
214  0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
215 };
216 
217 static const int switchtable[23] = {
218  0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
219 };
220 
221 static av_cold void qdm2_init_vlc(void)
222 {
223  static VLC_TYPE qdm2_table[3838][2];
224 
225  vlc_tab_level.table = &qdm2_table[qdm2_vlc_offs[0]];
226  vlc_tab_level.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0];
227  init_vlc(&vlc_tab_level, 8, 24,
231 
232  vlc_tab_diff.table = &qdm2_table[qdm2_vlc_offs[1]];
233  vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
234  init_vlc(&vlc_tab_diff, 8, 37,
235  vlc_tab_diff_huffbits, 1, 1,
238 
239  vlc_tab_run.table = &qdm2_table[qdm2_vlc_offs[2]];
240  vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
241  init_vlc(&vlc_tab_run, 5, 6,
242  vlc_tab_run_huffbits, 1, 1,
243  vlc_tab_run_huffcodes, 1, 1,
245 
246  fft_level_exp_alt_vlc.table = &qdm2_table[qdm2_vlc_offs[3]];
247  fft_level_exp_alt_vlc.table_allocated = qdm2_vlc_offs[4] -
248  qdm2_vlc_offs[3];
249  init_vlc(&fft_level_exp_alt_vlc, 8, 28,
253 
254  fft_level_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[4]];
255  fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
256  init_vlc(&fft_level_exp_vlc, 8, 20,
260 
261  fft_stereo_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[5]];
262  fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] -
263  qdm2_vlc_offs[5];
264  init_vlc(&fft_stereo_exp_vlc, 6, 7,
268 
269  fft_stereo_phase_vlc.table = &qdm2_table[qdm2_vlc_offs[6]];
270  fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] -
271  qdm2_vlc_offs[6];
272  init_vlc(&fft_stereo_phase_vlc, 6, 9,
276 
277  vlc_tab_tone_level_idx_hi1.table =
278  &qdm2_table[qdm2_vlc_offs[7]];
279  vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] -
280  qdm2_vlc_offs[7];
281  init_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20,
285 
286  vlc_tab_tone_level_idx_mid.table =
287  &qdm2_table[qdm2_vlc_offs[8]];
288  vlc_tab_tone_level_idx_mid.table_allocated = qdm2_vlc_offs[9] -
289  qdm2_vlc_offs[8];
290  init_vlc(&vlc_tab_tone_level_idx_mid, 8, 24,
294 
295  vlc_tab_tone_level_idx_hi2.table =
296  &qdm2_table[qdm2_vlc_offs[9]];
297  vlc_tab_tone_level_idx_hi2.table_allocated = qdm2_vlc_offs[10] -
298  qdm2_vlc_offs[9];
299  init_vlc(&vlc_tab_tone_level_idx_hi2, 8, 24,
303 
304  vlc_tab_type30.table = &qdm2_table[qdm2_vlc_offs[10]];
305  vlc_tab_type30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
306  init_vlc(&vlc_tab_type30, 6, 9,
310 
311  vlc_tab_type34.table = &qdm2_table[qdm2_vlc_offs[11]];
312  vlc_tab_type34.table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
313  init_vlc(&vlc_tab_type34, 5, 10,
317 
318  vlc_tab_fft_tone_offset[0].table =
319  &qdm2_table[qdm2_vlc_offs[12]];
320  vlc_tab_fft_tone_offset[0].table_allocated = qdm2_vlc_offs[13] -
321  qdm2_vlc_offs[12];
322  init_vlc(&vlc_tab_fft_tone_offset[0], 8, 23,
326 
327  vlc_tab_fft_tone_offset[1].table =
328  &qdm2_table[qdm2_vlc_offs[13]];
329  vlc_tab_fft_tone_offset[1].table_allocated = qdm2_vlc_offs[14] -
330  qdm2_vlc_offs[13];
331  init_vlc(&vlc_tab_fft_tone_offset[1], 8, 28,
335 
336  vlc_tab_fft_tone_offset[2].table =
337  &qdm2_table[qdm2_vlc_offs[14]];
338  vlc_tab_fft_tone_offset[2].table_allocated = qdm2_vlc_offs[15] -
339  qdm2_vlc_offs[14];
340  init_vlc(&vlc_tab_fft_tone_offset[2], 8, 32,
344 
345  vlc_tab_fft_tone_offset[3].table =
346  &qdm2_table[qdm2_vlc_offs[15]];
347  vlc_tab_fft_tone_offset[3].table_allocated = qdm2_vlc_offs[16] -
348  qdm2_vlc_offs[15];
349  init_vlc(&vlc_tab_fft_tone_offset[3], 8, 35,
353 
354  vlc_tab_fft_tone_offset[4].table =
355  &qdm2_table[qdm2_vlc_offs[16]];
356  vlc_tab_fft_tone_offset[4].table_allocated = qdm2_vlc_offs[17] -
357  qdm2_vlc_offs[16];
358  init_vlc(&vlc_tab_fft_tone_offset[4], 8, 38,
362 }
363 
364 static int qdm2_get_vlc(GetBitContext *gb, VLC *vlc, int flag, int depth)
365 {
366  int value;
367 
368  value = get_vlc2(gb, vlc->table, vlc->bits, depth);
369 
370  /* stage-2, 3 bits exponent escape sequence */
371  if (value-- == 0)
372  value = get_bits(gb, get_bits(gb, 3) + 1);
373 
374  /* stage-3, optional */
375  if (flag) {
376  int tmp = vlc_stage3_values[value];
377 
378  if ((value & ~3) > 0)
379  tmp += get_bits(gb, (value >> 2));
380  value = tmp;
381  }
382 
383  return value;
384 }
385 
386 static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth)
387 {
388  int value = qdm2_get_vlc(gb, vlc, 0, depth);
389 
390  return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
391 }
392 
402 static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
403 {
404  int i;
405 
406  for (i = 0; i < length; i++)
407  value -= data[i];
408 
409  return (uint16_t)(value & 0xffff);
410 }
411 
419  QDM2SubPacket *sub_packet)
420 {
421  sub_packet->type = get_bits(gb, 8);
422 
423  if (sub_packet->type == 0) {
424  sub_packet->size = 0;
425  sub_packet->data = NULL;
426  } else {
427  sub_packet->size = get_bits(gb, 8);
428 
429  if (sub_packet->type & 0x80) {
430  sub_packet->size <<= 8;
431  sub_packet->size |= get_bits(gb, 8);
432  sub_packet->type &= 0x7f;
433  }
434 
435  if (sub_packet->type == 0x7f)
436  sub_packet->type |= (get_bits(gb, 8) << 8);
437 
438  // FIXME: this depends on bitreader-internal data
439  sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];
440  }
441 
442  av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n",
443  sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
444 }
445 
454  int type)
455 {
456  while (list && list->packet) {
457  if (list->packet->type == type)
458  return list;
459  list = list->next;
460  }
461  return NULL;
462 }
463 
471 {
472  int i, j, n, ch, sum;
473 
475 
476  for (ch = 0; ch < q->nb_channels; ch++)
477  for (i = 0; i < n; i++) {
478  sum = 0;
479 
480  for (j = 0; j < 8; j++)
481  sum += q->quantized_coeffs[ch][i][j];
482 
483  sum /= 8;
484  if (sum > 0)
485  sum--;
486 
487  for (j = 0; j < 8; j++)
488  q->quantized_coeffs[ch][i][j] = sum;
489  }
490 }
491 
500 {
501  int ch, j;
502 
504 
505  if (!q->nb_channels)
506  return;
507 
508  for (ch = 0; ch < q->nb_channels; ch++) {
509  for (j = 0; j < 64; j++) {
510  q->sb_samples[ch][j * 2][sb] =
511  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
512  q->sb_samples[ch][j * 2 + 1][sb] =
513  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
514  }
515  }
516 }
517 
526 static int fix_coding_method_array(int sb, int channels,
527  sb_int8_array coding_method)
528 {
529  int j, k;
530  int ch;
531  int run, case_val;
532 
533  for (ch = 0; ch < channels; ch++) {
534  for (j = 0; j < 64; ) {
535  if (coding_method[ch][sb][j] < 8)
536  return -1;
537  if ((coding_method[ch][sb][j] - 8) > 22) {
538  run = 1;
539  case_val = 8;
540  } else {
541  switch (switchtable[coding_method[ch][sb][j] - 8]) {
542  case 0: run = 10;
543  case_val = 10;
544  break;
545  case 1: run = 1;
546  case_val = 16;
547  break;
548  case 2: run = 5;
549  case_val = 24;
550  break;
551  case 3: run = 3;
552  case_val = 30;
553  break;
554  case 4: run = 1;
555  case_val = 30;
556  break;
557  case 5: run = 1;
558  case_val = 8;
559  break;
560  default: run = 1;
561  case_val = 8;
562  break;
563  }
564  }
565  for (k = 0; k < run; k++) {
566  if (j + k < 128) {
567  if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) {
568  if (k > 0) {
570  //not debugged, almost never used
571  memset(&coding_method[ch][sb][j + k], case_val,
572  k *sizeof(int8_t));
573  memset(&coding_method[ch][sb][j + k], case_val,
574  3 * sizeof(int8_t));
575  }
576  }
577  }
578  }
579  j += run;
580  }
581  }
582  return 0;
583 }
584 
593 {
594  int i, sb, ch, sb_used;
595  int tmp, tab;
596 
597  for (ch = 0; ch < q->nb_channels; ch++)
598  for (sb = 0; sb < 30; sb++)
599  for (i = 0; i < 8; i++) {
601  tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
603  else
604  tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
605  if(tmp < 0)
606  tmp += 0xff;
607  q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
608  }
609 
610  sb_used = QDM2_SB_USED(q->sub_sampling);
611 
612  if ((q->superblocktype_2_3 != 0) && !flag) {
613  for (sb = 0; sb < sb_used; sb++)
614  for (ch = 0; ch < q->nb_channels; ch++)
615  for (i = 0; i < 64; i++) {
616  q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
617  if (q->tone_level_idx[ch][sb][i] < 0)
618  q->tone_level[ch][sb][i] = 0;
619  else
620  q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
621  }
622  } else {
623  tab = q->superblocktype_2_3 ? 0 : 1;
624  for (sb = 0; sb < sb_used; sb++) {
625  if ((sb >= 4) && (sb <= 23)) {
626  for (ch = 0; ch < q->nb_channels; ch++)
627  for (i = 0; i < 64; i++) {
628  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
629  q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
630  q->tone_level_idx_mid[ch][sb - 4][i / 8] -
631  q->tone_level_idx_hi2[ch][sb - 4];
632  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
633  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
634  q->tone_level[ch][sb][i] = 0;
635  else
636  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
637  }
638  } else {
639  if (sb > 4) {
640  for (ch = 0; ch < q->nb_channels; ch++)
641  for (i = 0; i < 64; i++) {
642  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
643  q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
644  q->tone_level_idx_hi2[ch][sb - 4];
645  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
646  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
647  q->tone_level[ch][sb][i] = 0;
648  else
649  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
650  }
651  } else {
652  for (ch = 0; ch < q->nb_channels; ch++)
653  for (i = 0; i < 64; i++) {
654  tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
655  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
656  q->tone_level[ch][sb][i] = 0;
657  else
658  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
659  }
660  }
661  }
662  }
663  }
664 }
665 
681 static void fill_coding_method_array(sb_int8_array tone_level_idx,
682  sb_int8_array tone_level_idx_temp,
683  sb_int8_array coding_method,
684  int nb_channels,
685  int c, int superblocktype_2_3,
686  int cm_table_select)
687 {
688  int ch, sb, j;
689  int tmp, acc, esp_40, comp;
690  int add1, add2, add3, add4;
691  int64_t multres;
692 
693  if (!superblocktype_2_3) {
694  /* This case is untested, no samples available */
696  for (ch = 0; ch < nb_channels; ch++)
697  for (sb = 0; sb < 30; sb++) {
698  for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
699  add1 = tone_level_idx[ch][sb][j] - 10;
700  if (add1 < 0)
701  add1 = 0;
702  add2 = add3 = add4 = 0;
703  if (sb > 1) {
704  add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
705  if (add2 < 0)
706  add2 = 0;
707  }
708  if (sb > 0) {
709  add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
710  if (add3 < 0)
711  add3 = 0;
712  }
713  if (sb < 29) {
714  add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
715  if (add4 < 0)
716  add4 = 0;
717  }
718  tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
719  if (tmp < 0)
720  tmp = 0;
721  tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
722  }
723  tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
724  }
725 
726  acc = 0;
727  for (ch = 0; ch < nb_channels; ch++)
728  for (sb = 0; sb < 30; sb++)
729  for (j = 0; j < 64; j++)
730  acc += tone_level_idx_temp[ch][sb][j];
731 
732  multres = 0x66666667LL * (acc * 10);
733  esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
734  for (ch = 0; ch < nb_channels; ch++)
735  for (sb = 0; sb < 30; sb++)
736  for (j = 0; j < 64; j++) {
737  comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
738  if (comp < 0)
739  comp += 0xff;
740  comp /= 256; // signed shift
741  switch(sb) {
742  case 0:
743  if (comp < 30)
744  comp = 30;
745  comp += 15;
746  break;
747  case 1:
748  if (comp < 24)
749  comp = 24;
750  comp += 10;
751  break;
752  case 2:
753  case 3:
754  case 4:
755  if (comp < 16)
756  comp = 16;
757  }
758  if (comp <= 5)
759  tmp = 0;
760  else if (comp <= 10)
761  tmp = 10;
762  else if (comp <= 16)
763  tmp = 16;
764  else if (comp <= 24)
765  tmp = -1;
766  else
767  tmp = 0;
768  coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
769  }
770  for (sb = 0; sb < 30; sb++)
771  fix_coding_method_array(sb, nb_channels, coding_method);
772  for (ch = 0; ch < nb_channels; ch++)
773  for (sb = 0; sb < 30; sb++)
774  for (j = 0; j < 64; j++)
775  if (sb >= 10) {
776  if (coding_method[ch][sb][j] < 10)
777  coding_method[ch][sb][j] = 10;
778  } else {
779  if (sb >= 2) {
780  if (coding_method[ch][sb][j] < 16)
781  coding_method[ch][sb][j] = 16;
782  } else {
783  if (coding_method[ch][sb][j] < 30)
784  coding_method[ch][sb][j] = 30;
785  }
786  }
787  } else { // superblocktype_2_3 != 0
788  for (ch = 0; ch < nb_channels; ch++)
789  for (sb = 0; sb < 30; sb++)
790  for (j = 0; j < 64; j++)
791  coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
792  }
793 }
794 
808  int length, int sb_min, int sb_max)
809 {
810  int sb, j, k, n, ch, run, channels;
811  int joined_stereo, zero_encoding;
812  int type34_first;
813  float type34_div = 0;
814  float type34_predictor;
815  float samples[10], sign_bits[16];
816 
817  if (length == 0) {
818  // If no data use noise
819  for (sb=sb_min; sb < sb_max; sb++)
821 
822  return;
823  }
824 
825  for (sb = sb_min; sb < sb_max; sb++) {
826  channels = q->nb_channels;
827 
828  if (q->nb_channels <= 1 || sb < 12)
829  joined_stereo = 0;
830  else if (sb >= 24)
831  joined_stereo = 1;
832  else
833  joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
834 
835  if (joined_stereo) {
836  if (get_bits_left(gb) >= 16)
837  for (j = 0; j < 16; j++)
838  sign_bits[j] = get_bits1(gb);
839 
840  for (j = 0; j < 64; j++)
841  if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
842  q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
843 
845  q->coding_method)) {
847  continue;
848  }
849  channels = 1;
850  }
851 
852  for (ch = 0; ch < channels; ch++) {
854  zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
855  type34_predictor = 0.0;
856  type34_first = 1;
857 
858  for (j = 0; j < 128; ) {
859  switch (q->coding_method[ch][sb][j / 2]) {
860  case 8:
861  if (get_bits_left(gb) >= 10) {
862  if (zero_encoding) {
863  for (k = 0; k < 5; k++) {
864  if ((j + 2 * k) >= 128)
865  break;
866  samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
867  }
868  } else {
869  n = get_bits(gb, 8);
870  for (k = 0; k < 5; k++)
871  samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
872  }
873  for (k = 0; k < 5; k++)
874  samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
875  } else {
876  for (k = 0; k < 10; k++)
877  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
878  }
879  run = 10;
880  break;
881 
882  case 10:
883  if (get_bits_left(gb) >= 1) {
884  float f = 0.81;
885 
886  if (get_bits1(gb))
887  f = -f;
888  f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
889  samples[0] = f;
890  } else {
891  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
892  }
893  run = 1;
894  break;
895 
896  case 16:
897  if (get_bits_left(gb) >= 10) {
898  if (zero_encoding) {
899  for (k = 0; k < 5; k++) {
900  if ((j + k) >= 128)
901  break;
902  samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
903  }
904  } else {
905  n = get_bits (gb, 8);
906  for (k = 0; k < 5; k++)
907  samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
908  }
909  } else {
910  for (k = 0; k < 5; k++)
911  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
912  }
913  run = 5;
914  break;
915 
916  case 24:
917  if (get_bits_left(gb) >= 7) {
918  n = get_bits(gb, 7);
919  for (k = 0; k < 3; k++)
920  samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
921  } else {
922  for (k = 0; k < 3; k++)
923  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
924  }
925  run = 3;
926  break;
927 
928  case 30:
929  if (get_bits_left(gb) >= 4) {
930  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
931  if (index < FF_ARRAY_ELEMS(type30_dequant)) {
932  samples[0] = type30_dequant[index];
933  } else
934  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
935  } else
936  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
937 
938  run = 1;
939  break;
940 
941  case 34:
942  if (get_bits_left(gb) >= 7) {
943  if (type34_first) {
944  type34_div = (float)(1 << get_bits(gb, 2));
945  samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
946  type34_predictor = samples[0];
947  type34_first = 0;
948  } else {
949  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
950  if (index < FF_ARRAY_ELEMS(type34_delta)) {
951  samples[0] = type34_delta[index] / type34_div + type34_predictor;
952  type34_predictor = samples[0];
953  } else
954  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
955  }
956  } else {
957  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
958  }
959  run = 1;
960  break;
961 
962  default:
963  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
964  run = 1;
965  break;
966  }
967 
968  if (joined_stereo) {
969  for (k = 0; k < run && j + k < 128; k++) {
970  q->sb_samples[0][j + k][sb] =
971  q->tone_level[0][sb][(j + k) / 2] * samples[k];
972  if (q->nb_channels == 2) {
973  if (sign_bits[(j + k) / 8])
974  q->sb_samples[1][j + k][sb] =
975  q->tone_level[1][sb][(j + k) / 2] * -samples[k];
976  else
977  q->sb_samples[1][j + k][sb] =
978  q->tone_level[1][sb][(j + k) / 2] * samples[k];
979  }
980  }
981  } else {
982  for (k = 0; k < run; k++)
983  if ((j + k) < 128)
984  q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
985  }
986 
987  j += run;
988  } // j loop
989  } // channel loop
990  } // subband loop
991 }
992 
1003 static void init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
1004  GetBitContext *gb)
1005 {
1006  int i, k, run, level, diff;
1007 
1008  if (get_bits_left(gb) < 16)
1009  return;
1010  level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
1011 
1012  quantized_coeffs[0] = level;
1013 
1014  for (i = 0; i < 7; ) {
1015  if (get_bits_left(gb) < 16)
1016  break;
1017  run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
1018 
1019  if (get_bits_left(gb) < 16)
1020  break;
1021  diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
1022 
1023  for (k = 1; k <= run; k++)
1024  quantized_coeffs[i + k] = (level + ((k * diff) / run));
1025 
1026  level += diff;
1027  i += run;
1028  }
1029 }
1030 
1041 {
1042  int sb, j, k, n, ch;
1043 
1044  for (ch = 0; ch < q->nb_channels; ch++) {
1046 
1047  if (get_bits_left(gb) < 16) {
1048  memset(q->quantized_coeffs[ch][0], 0, 8);
1049  break;
1050  }
1051  }
1052 
1053  n = q->sub_sampling + 1;
1054 
1055  for (sb = 0; sb < n; sb++)
1056  for (ch = 0; ch < q->nb_channels; ch++)
1057  for (j = 0; j < 8; j++) {
1058  if (get_bits_left(gb) < 1)
1059  break;
1060  if (get_bits1(gb)) {
1061  for (k=0; k < 8; k++) {
1062  if (get_bits_left(gb) < 16)
1063  break;
1064  q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
1065  }
1066  } else {
1067  for (k=0; k < 8; k++)
1068  q->tone_level_idx_hi1[ch][sb][j][k] = 0;
1069  }
1070  }
1071 
1072  n = QDM2_SB_USED(q->sub_sampling) - 4;
1073 
1074  for (sb = 0; sb < n; sb++)
1075  for (ch = 0; ch < q->nb_channels; ch++) {
1076  if (get_bits_left(gb) < 16)
1077  break;
1078  q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
1079  if (sb > 19)
1080  q->tone_level_idx_hi2[ch][sb] -= 16;
1081  else
1082  for (j = 0; j < 8; j++)
1083  q->tone_level_idx_mid[ch][sb][j] = -16;
1084  }
1085 
1086  n = QDM2_SB_USED(q->sub_sampling) - 5;
1087 
1088  for (sb = 0; sb < n; sb++)
1089  for (ch = 0; ch < q->nb_channels; ch++)
1090  for (j = 0; j < 8; j++) {
1091  if (get_bits_left(gb) < 16)
1092  break;
1093  q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
1094  }
1095 }
1096 
1104 {
1105  GetBitContext gb;
1106  int i, j, k, n, ch, run, level, diff;
1107 
1108  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1109 
1111 
1112  for (i = 1; i < n; i++)
1113  for (ch = 0; ch < q->nb_channels; ch++) {
1114  level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
1115  q->quantized_coeffs[ch][i][0] = level;
1116 
1117  for (j = 0; j < (8 - 1); ) {
1118  run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
1119  diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
1120 
1121  for (k = 1; k <= run; k++)
1122  q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
1123 
1124  level += diff;
1125  j += run;
1126  }
1127  }
1128 
1129  for (ch = 0; ch < q->nb_channels; ch++)
1130  for (i = 0; i < 8; i++)
1131  q->quantized_coeffs[ch][0][i] = 0;
1132 }
1133 
1141 {
1142  GetBitContext gb;
1143 
1144  if (node) {
1145  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1147  fill_tone_level_array(q, 1);
1148  } else {
1149  fill_tone_level_array(q, 0);
1150  }
1151 }
1152 
1160 {
1161  GetBitContext gb;
1162  int length = 0;
1163 
1164  if (node) {
1165  length = node->packet->size * 8;
1166  init_get_bits(&gb, node->packet->data, length);
1167  }
1168 
1169  if (length >= 32) {
1170  int c = get_bits(&gb, 13);
1171 
1172  if (c > 3)
1175  q->nb_channels, 8 * c,
1177  }
1178 
1179  synthfilt_build_sb_samples(q, &gb, length, 0, 8);
1180 }
1181 
1189 {
1190  GetBitContext gb;
1191  int length = 0;
1192 
1193  if (node) {
1194  length = node->packet->size * 8;
1195  init_get_bits(&gb, node->packet->data, length);
1196  }
1197 
1198  synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
1199 }
1200 
1201 /*
1202  * Process new subpackets for synthesis filter
1203  *
1204  * @param q context
1205  * @param list list with synthesis filter packets (list D)
1206  */
1208 {
1209  QDM2SubPNode *nodes[4];
1210 
1211  nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
1212  if (nodes[0])
1213  process_subpacket_9(q, nodes[0]);
1214 
1215  nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1216  if (nodes[1])
1217  process_subpacket_10(q, nodes[1]);
1218  else
1220 
1221  nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1222  if (nodes[0] && nodes[1] && nodes[2])
1223  process_subpacket_11(q, nodes[2]);
1224  else
1226 
1227  nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1228  if (nodes[0] && nodes[1] && nodes[3])
1229  process_subpacket_12(q, nodes[3]);
1230  else
1232 }
1233 
1234 /*
1235  * Decode superblock, fill packet lists.
1236  *
1237  * @param q context
1238  */
1240 {
1241  GetBitContext gb;
1242  QDM2SubPacket header, *packet;
1243  int i, packet_bytes, sub_packet_size, sub_packets_D;
1244  unsigned int next_index = 0;
1245 
1246  memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1247  memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1248  memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1249 
1250  q->sub_packets_B = 0;
1251  sub_packets_D = 0;
1252 
1253  average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1254 
1256  qdm2_decode_sub_packet_header(&gb, &header);
1257 
1258  if (header.type < 2 || header.type >= 8) {
1259  q->has_errors = 1;
1260  av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
1261  return;
1262  }
1263 
1264  q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1265  packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
1266 
1267  init_get_bits(&gb, header.data, header.size * 8);
1268 
1269  if (header.type == 2 || header.type == 4 || header.type == 5) {
1270  int csum = 257 * get_bits(&gb, 8);
1271  csum += 2 * get_bits(&gb, 8);
1272 
1273  csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1274 
1275  if (csum != 0) {
1276  q->has_errors = 1;
1277  av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
1278  return;
1279  }
1280  }
1281 
1282  q->sub_packet_list_B[0].packet = NULL;
1283  q->sub_packet_list_D[0].packet = NULL;
1284 
1285  for (i = 0; i < 6; i++)
1286  if (--q->fft_level_exp[i] < 0)
1287  q->fft_level_exp[i] = 0;
1288 
1289  for (i = 0; packet_bytes > 0; i++) {
1290  int j;
1291 
1292  if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
1293  SAMPLES_NEEDED_2("too many packet bytes");
1294  return;
1295  }
1296 
1297  q->sub_packet_list_A[i].next = NULL;
1298 
1299  if (i > 0) {
1300  q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1301 
1302  /* seek to next block */
1303  init_get_bits(&gb, header.data, header.size * 8);
1304  skip_bits(&gb, next_index * 8);
1305 
1306  if (next_index >= header.size)
1307  break;
1308  }
1309 
1310  /* decode subpacket */
1311  packet = &q->sub_packets[i];
1312  qdm2_decode_sub_packet_header(&gb, packet);
1313  next_index = packet->size + get_bits_count(&gb) / 8;
1314  sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1315 
1316  if (packet->type == 0)
1317  break;
1318 
1319  if (sub_packet_size > packet_bytes) {
1320  if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1321  break;
1322  packet->size += packet_bytes - sub_packet_size;
1323  }
1324 
1325  packet_bytes -= sub_packet_size;
1326 
1327  /* add subpacket to 'all subpackets' list */
1328  q->sub_packet_list_A[i].packet = packet;
1329 
1330  /* add subpacket to related list */
1331  if (packet->type == 8) {
1332  SAMPLES_NEEDED_2("packet type 8");
1333  return;
1334  } else if (packet->type >= 9 && packet->type <= 12) {
1335  /* packets for MPEG Audio like Synthesis Filter */
1336  QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1337  } else if (packet->type == 13) {
1338  for (j = 0; j < 6; j++)
1339  q->fft_level_exp[j] = get_bits(&gb, 6);
1340  } else if (packet->type == 14) {
1341  for (j = 0; j < 6; j++)
1342  q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
1343  } else if (packet->type == 15) {
1344  SAMPLES_NEEDED_2("packet type 15")
1345  return;
1346  } else if (packet->type >= 16 && packet->type < 48 &&
1347  !fft_subpackets[packet->type - 16]) {
1348  /* packets for FFT */
1350  }
1351  } // Packet bytes loop
1352 
1353  if (q->sub_packet_list_D[0].packet) {
1355  q->do_synth_filter = 1;
1356  } else if (q->do_synth_filter) {
1360  }
1361 }
1362 
1363 static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
1364  int offset, int duration, int channel,
1365  int exp, int phase)
1366 {
1367  if (q->fft_coefs_min_index[duration] < 0)
1369 
1371  ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1372  q->fft_coefs[q->fft_coefs_index].channel = channel;
1373  q->fft_coefs[q->fft_coefs_index].offset = offset;
1374  q->fft_coefs[q->fft_coefs_index].exp = exp;
1375  q->fft_coefs[q->fft_coefs_index].phase = phase;
1376  q->fft_coefs_index++;
1377 }
1378 
1380  GetBitContext *gb, int b)
1381 {
1382  int channel, stereo, phase, exp;
1383  int local_int_4, local_int_8, stereo_phase, local_int_10;
1384  int local_int_14, stereo_exp, local_int_20, local_int_28;
1385  int n, offset;
1386 
1387  local_int_4 = 0;
1388  local_int_28 = 0;
1389  local_int_20 = 2;
1390  local_int_8 = (4 - duration);
1391  local_int_10 = 1 << (q->group_order - duration - 1);
1392  offset = 1;
1393 
1394  while (1) {
1395  if (q->superblocktype_2_3) {
1396  while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1397  offset = 1;
1398  if (n == 0) {
1399  local_int_4 += local_int_10;
1400  local_int_28 += (1 << local_int_8);
1401  } else {
1402  local_int_4 += 8 * local_int_10;
1403  local_int_28 += (8 << local_int_8);
1404  }
1405  }
1406  offset += (n - 2);
1407  } else {
1408  offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1409  while (offset >= (local_int_10 - 1)) {
1410  offset += (1 - (local_int_10 - 1));
1411  local_int_4 += local_int_10;
1412  local_int_28 += (1 << local_int_8);
1413  }
1414  }
1415 
1416  if (local_int_4 >= q->group_size)
1417  return;
1418 
1419  local_int_14 = (offset >> local_int_8);
1420  if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
1421  return;
1422 
1423  if (q->nb_channels > 1) {
1424  channel = get_bits1(gb);
1425  stereo = get_bits1(gb);
1426  } else {
1427  channel = 0;
1428  stereo = 0;
1429  }
1430 
1431  exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1432  exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1433  exp = (exp < 0) ? 0 : exp;
1434 
1435  phase = get_bits(gb, 3);
1436  stereo_exp = 0;
1437  stereo_phase = 0;
1438 
1439  if (stereo) {
1440  stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1441  stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1442  if (stereo_phase < 0)
1443  stereo_phase += 8;
1444  }
1445 
1446  if (q->frequency_range > (local_int_14 + 1)) {
1447  int sub_packet = (local_int_20 + local_int_28);
1448 
1449  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1450  channel, exp, phase);
1451  if (stereo)
1452  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1453  1 - channel,
1454  stereo_exp, stereo_phase);
1455  }
1456  offset++;
1457  }
1458 }
1459 
1461 {
1462  int i, j, min, max, value, type, unknown_flag;
1463  GetBitContext gb;
1464 
1465  if (!q->sub_packet_list_B[0].packet)
1466  return;
1467 
1468  /* reset minimum indexes for FFT coefficients */
1469  q->fft_coefs_index = 0;
1470  for (i = 0; i < 5; i++)
1471  q->fft_coefs_min_index[i] = -1;
1472 
1473  /* process subpackets ordered by type, largest type first */
1474  for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1475  QDM2SubPacket *packet = NULL;
1476 
1477  /* find subpacket with largest type less than max */
1478  for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1479  value = q->sub_packet_list_B[j].packet->type;
1480  if (value > min && value < max) {
1481  min = value;
1482  packet = q->sub_packet_list_B[j].packet;
1483  }
1484  }
1485 
1486  max = min;
1487 
1488  /* check for errors (?) */
1489  if (!packet)
1490  return;
1491 
1492  if (i == 0 &&
1493  (packet->type < 16 || packet->type >= 48 ||
1494  fft_subpackets[packet->type - 16]))
1495  return;
1496 
1497  /* decode FFT tones */
1498  init_get_bits(&gb, packet->data, packet->size * 8);
1499 
1500  if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1501  unknown_flag = 1;
1502  else
1503  unknown_flag = 0;
1504 
1505  type = packet->type;
1506 
1507  if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1508  int duration = q->sub_sampling + 5 - (type & 15);
1509 
1510  if (duration >= 0 && duration < 4)
1511  qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
1512  } else if (type == 31) {
1513  for (j = 0; j < 4; j++)
1514  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1515  } else if (type == 46) {
1516  for (j = 0; j < 6; j++)
1517  q->fft_level_exp[j] = get_bits(&gb, 6);
1518  for (j = 0; j < 4; j++)
1519  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1520  }
1521  } // Loop on B packets
1522 
1523  /* calculate maximum indexes for FFT coefficients */
1524  for (i = 0, j = -1; i < 5; i++)
1525  if (q->fft_coefs_min_index[i] >= 0) {
1526  if (j >= 0)
1528  j = i;
1529  }
1530  if (j >= 0)
1532 }
1533 
1535 {
1536  float level, f[6];
1537  int i;
1538  QDM2Complex c;
1539  const double iscale = 2.0 * M_PI / 512.0;
1540 
1541  tone->phase += tone->phase_shift;
1542 
1543  /* calculate current level (maximum amplitude) of tone */
1544  level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1545  c.im = level * sin(tone->phase * iscale);
1546  c.re = level * cos(tone->phase * iscale);
1547 
1548  /* generate FFT coefficients for tone */
1549  if (tone->duration >= 3 || tone->cutoff >= 3) {
1550  tone->complex[0].im += c.im;
1551  tone->complex[0].re += c.re;
1552  tone->complex[1].im -= c.im;
1553  tone->complex[1].re -= c.re;
1554  } else {
1555  f[1] = -tone->table[4];
1556  f[0] = tone->table[3] - tone->table[0];
1557  f[2] = 1.0 - tone->table[2] - tone->table[3];
1558  f[3] = tone->table[1] + tone->table[4] - 1.0;
1559  f[4] = tone->table[0] - tone->table[1];
1560  f[5] = tone->table[2];
1561  for (i = 0; i < 2; i++) {
1562  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
1563  c.re * f[i];
1564  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
1565  c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
1566  }
1567  for (i = 0; i < 4; i++) {
1568  tone->complex[i].re += c.re * f[i + 2];
1569  tone->complex[i].im += c.im * f[i + 2];
1570  }
1571  }
1572 
1573  /* copy the tone if it has not yet died out */
1574  if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1575  memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1576  q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1577  }
1578 }
1579 
1580 static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
1581 {
1582  int i, j, ch;
1583  const double iscale = 0.25 * M_PI;
1584 
1585  for (ch = 0; ch < q->channels; ch++) {
1586  memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
1587  }
1588 
1589 
1590  /* apply FFT tones with duration 4 (1 FFT period) */
1591  if (q->fft_coefs_min_index[4] >= 0)
1592  for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1593  float level;
1594  QDM2Complex c;
1595 
1596  if (q->fft_coefs[i].sub_packet != sub_packet)
1597  break;
1598 
1599  ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1600  level = (q->fft_coefs[i].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[i].exp & 63];
1601 
1602  c.re = level * cos(q->fft_coefs[i].phase * iscale);
1603  c.im = level * sin(q->fft_coefs[i].phase * iscale);
1604  q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1605  q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1606  q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1607  q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1608  }
1609 
1610  /* generate existing FFT tones */
1611  for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1613  q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1614  }
1615 
1616  /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1617  for (i = 0; i < 4; i++)
1618  if (q->fft_coefs_min_index[i] >= 0) {
1619  for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1620  int offset, four_i;
1621  FFTTone tone;
1622 
1623  if (q->fft_coefs[j].sub_packet != sub_packet)
1624  break;
1625 
1626  four_i = (4 - i);
1627  offset = q->fft_coefs[j].offset >> four_i;
1628  ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1629 
1630  if (offset < q->frequency_range) {
1631  if (offset < 2)
1632  tone.cutoff = offset;
1633  else
1634  tone.cutoff = (offset >= 60) ? 3 : 2;
1635 
1636  tone.level = (q->fft_coefs[j].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[j].exp & 63];
1637  tone.complex = &q->fft.complex[ch][offset];
1638  tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1639  tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1640  tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1641  tone.duration = i;
1642  tone.time_index = 0;
1643 
1644  qdm2_fft_generate_tone(q, &tone);
1645  }
1646  }
1647  q->fft_coefs_min_index[i] = j;
1648  }
1649 }
1650 
1651 static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
1652 {
1653  const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1654  float *out = q->output_buffer + channel;
1655  int i;
1656  q->fft.complex[channel][0].re *= 2.0f;
1657  q->fft.complex[channel][0].im = 0.0f;
1658  q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
1659  /* add samples to output buffer */
1660  for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
1661  out[0] += q->fft.complex[channel][i].re * gain;
1662  out[q->channels] += q->fft.complex[channel][i].im * gain;
1663  out += 2 * q->channels;
1664  }
1665 }
1666 
1672 {
1673  int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1674 
1675  /* copy sb_samples */
1676  sb_used = QDM2_SB_USED(q->sub_sampling);
1677 
1678  for (ch = 0; ch < q->channels; ch++)
1679  for (i = 0; i < 8; i++)
1680  for (k = sb_used; k < SBLIMIT; k++)
1681  q->sb_samples[ch][(8 * index) + i][k] = 0;
1682 
1683  for (ch = 0; ch < q->nb_channels; ch++) {
1684  float *samples_ptr = q->samples + ch;
1685 
1686  for (i = 0; i < 8; i++) {
1688  q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1689  ff_mpa_synth_window_float, &dither_state,
1690  samples_ptr, q->nb_channels,
1691  q->sb_samples[ch][(8 * index) + i]);
1692  samples_ptr += 32 * q->nb_channels;
1693  }
1694  }
1695 
1696  /* add samples to output buffer */
1697  sub_sampling = (4 >> q->sub_sampling);
1698 
1699  for (ch = 0; ch < q->channels; ch++)
1700  for (i = 0; i < q->frame_size; i++)
1701  q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
1702 }
1703 
1710  qdm2_init_vlc();
1713  rnd_table_init();
1715 }
1716 
1721 {
1722  QDM2Context *s = avctx->priv_data;
1723  uint8_t *extradata;
1724  int extradata_size;
1725  int tmp_val, tmp, size;
1726 
1727  /* extradata parsing
1728 
1729  Structure:
1730  wave {
1731  frma (QDM2)
1732  QDCA
1733  QDCP
1734  }
1735 
1736  32 size (including this field)
1737  32 tag (=frma)
1738  32 type (=QDM2 or QDMC)
1739 
1740  32 size (including this field, in bytes)
1741  32 tag (=QDCA) // maybe mandatory parameters
1742  32 unknown (=1)
1743  32 channels (=2)
1744  32 samplerate (=44100)
1745  32 bitrate (=96000)
1746  32 block size (=4096)
1747  32 frame size (=256) (for one channel)
1748  32 packet size (=1300)
1749 
1750  32 size (including this field, in bytes)
1751  32 tag (=QDCP) // maybe some tuneable parameters
1752  32 float1 (=1.0)
1753  32 zero ?
1754  32 float2 (=1.0)
1755  32 float3 (=1.0)
1756  32 unknown (27)
1757  32 unknown (8)
1758  32 zero ?
1759  */
1760 
1761  if (!avctx->extradata || (avctx->extradata_size < 48)) {
1762  av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1763  return AVERROR_INVALIDDATA;
1764  }
1765 
1766  extradata = avctx->extradata;
1767  extradata_size = avctx->extradata_size;
1768 
1769  while (extradata_size > 7) {
1770  if (!memcmp(extradata, "frmaQDM", 7))
1771  break;
1772  extradata++;
1773  extradata_size--;
1774  }
1775 
1776  if (extradata_size < 12) {
1777  av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1778  extradata_size);
1779  return AVERROR_INVALIDDATA;
1780  }
1781 
1782  if (memcmp(extradata, "frmaQDM", 7)) {
1783  av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
1784  return AVERROR_INVALIDDATA;
1785  }
1786 
1787  if (extradata[7] == 'C') {
1788 // s->is_qdmc = 1;
1789  avpriv_report_missing_feature(avctx, "QDMC version 1");
1790  return AVERROR_PATCHWELCOME;
1791  }
1792 
1793  extradata += 8;
1794  extradata_size -= 8;
1795 
1796  size = AV_RB32(extradata);
1797 
1798  if(size > extradata_size){
1799  av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1800  extradata_size, size);
1801  return AVERROR_INVALIDDATA;
1802  }
1803 
1804  extradata += 4;
1805  av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1806  if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
1807  av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1808  return AVERROR_INVALIDDATA;
1809  }
1810 
1811  extradata += 8;
1812 
1813  avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
1814  extradata += 4;
1815  if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS)
1816  return AVERROR_INVALIDDATA;
1817  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
1819 
1820  avctx->sample_rate = AV_RB32(extradata);
1821  extradata += 4;
1822 
1823  avctx->bit_rate = AV_RB32(extradata);
1824  extradata += 4;
1825 
1826  s->group_size = AV_RB32(extradata);
1827  extradata += 4;
1828 
1829  s->fft_size = AV_RB32(extradata);
1830  extradata += 4;
1831 
1832  s->checksum_size = AV_RB32(extradata);
1833  if (s->checksum_size >= 1U << 28) {
1834  av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
1835  return AVERROR_INVALIDDATA;
1836  }
1837 
1838  s->fft_order = av_log2(s->fft_size) + 1;
1839 
1840  // something like max decodable tones
1841  s->group_order = av_log2(s->group_size) + 1;
1842  s->frame_size = s->group_size / 16; // 16 iterations per super block
1844  return AVERROR_INVALIDDATA;
1845 
1846  s->sub_sampling = s->fft_order - 7;
1847  s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1848 
1849  switch ((s->sub_sampling * 2 + s->channels - 1)) {
1850  case 0: tmp = 40; break;
1851  case 1: tmp = 48; break;
1852  case 2: tmp = 56; break;
1853  case 3: tmp = 72; break;
1854  case 4: tmp = 80; break;
1855  case 5: tmp = 100;break;
1856  default: tmp=s->sub_sampling; break;
1857  }
1858  tmp_val = 0;
1859  if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1860  if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1861  if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1862  if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1863  s->cm_table_select = tmp_val;
1864 
1865  if (s->sub_sampling == 0)
1866  tmp = 7999;
1867  else
1868  tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
1869  /*
1870  0: 7999 -> 0
1871  1: 20000 -> 2
1872  2: 28000 -> 2
1873  */
1874  if (tmp < 8000)
1875  s->coeff_per_sb_select = 0;
1876  else if (tmp <= 16000)
1877  s->coeff_per_sb_select = 1;
1878  else
1879  s->coeff_per_sb_select = 2;
1880 
1881  // Fail on unknown fft order
1882  if ((s->fft_order < 7) || (s->fft_order > 9)) {
1883  avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order);
1884  return AVERROR_PATCHWELCOME;
1885  }
1886  if (s->fft_size != (1 << (s->fft_order - 1))) {
1887  av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
1888  return AVERROR_INVALIDDATA;
1889  }
1890 
1892  ff_mpadsp_init(&s->mpadsp);
1893 
1894  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1895 
1896  return 0;
1897 }
1898 
1900 {
1901  QDM2Context *s = avctx->priv_data;
1902 
1903  ff_rdft_end(&s->rdft_ctx);
1904 
1905  return 0;
1906 }
1907 
1908 static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
1909 {
1910  int ch, i;
1911  const int frame_size = (q->frame_size * q->channels);
1912 
1913  /* select input buffer */
1914  q->compressed_data = in;
1916 
1917  /* copy old block, clear new block of output samples */
1918  memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1919  memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1920 
1921  /* decode block of QDM2 compressed data */
1922  if (q->sub_packet == 0) {
1923  q->has_errors = 0; // zero it for a new super block
1924  av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1926  }
1927 
1928  /* parse subpackets */
1929  if (!q->has_errors) {
1930  if (q->sub_packet == 2)
1932 
1934  }
1935 
1936  /* sound synthesis stage 1 (FFT) */
1937  for (ch = 0; ch < q->channels; ch++) {
1938  qdm2_calculate_fft(q, ch, q->sub_packet);
1939 
1940  if (!q->has_errors && q->sub_packet_list_C[0].packet) {
1941  SAMPLES_NEEDED_2("has errors, and C list is not empty")
1942  return -1;
1943  }
1944  }
1945 
1946  /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1947  if (!q->has_errors && q->do_synth_filter)
1949 
1950  q->sub_packet = (q->sub_packet + 1) % 16;
1951 
1952  /* clip and convert output float[] to 16-bit signed samples */
1953  for (i = 0; i < frame_size; i++) {
1954  int value = (int)q->output_buffer[i];
1955 
1956  if (value > SOFTCLIP_THRESHOLD)
1957  value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
1958  else if (value < -SOFTCLIP_THRESHOLD)
1959  value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
1960 
1961  out[i] = value;
1962  }
1963 
1964  return 0;
1965 }
1966 
1967 static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
1968  int *got_frame_ptr, AVPacket *avpkt)
1969 {
1970  AVFrame *frame = data;
1971  const uint8_t *buf = avpkt->data;
1972  int buf_size = avpkt->size;
1973  QDM2Context *s = avctx->priv_data;
1974  int16_t *out;
1975  int i, ret;
1976 
1977  if(!buf)
1978  return 0;
1979  if(buf_size < s->checksum_size)
1980  return -1;
1981 
1982  /* get output buffer */
1983  frame->nb_samples = 16 * s->frame_size;
1984  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1985  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1986  return ret;
1987  }
1988  out = (int16_t *)frame->data[0];
1989 
1990  for (i = 0; i < 16; i++) {
1991  if ((ret = qdm2_decode(s, buf, out)) < 0)
1992  return ret;
1993  out += s->channels * s->frame_size;
1994  }
1995 
1996  *got_frame_ptr = 1;
1997 
1998  return s->checksum_size;
1999 }
2000 
2002  .name = "qdm2",
2003  .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
2004  .type = AVMEDIA_TYPE_AUDIO,
2005  .id = AV_CODEC_ID_QDM2,
2006  .priv_data_size = sizeof(QDM2Context),
2008  .init_static_data = qdm2_init_static_data,
2009  .close = qdm2_decode_close,
2011  .capabilities = AV_CODEC_CAP_DR1,
2012 };
static av_cold void qdm2_init_static_data(AVCodec *codec)
Init static data (does not depend on specific file)
Definition: qdm2.c:1709
Various QDM2 tables.
av_cold void ff_rdft_end(RDFTContext *s)
Definition: rdft.c:131
static const uint16_t vlc_tab_tone_level_idx_mid_huffcodes[24]
Definition: qdm2data.h:84
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define SBLIMIT
Definition: mpegaudio.h:43
FFTTone fft_tones[1000]
FFT and tones.
Definition: qdm2.c:155
static const uint8_t vlc_tab_level_huffbits[24]
Definition: qdm2data.h:44
A node in the subpacket list.
Definition: qdm2.c:93
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
QDM2FFT fft
Definition: qdm2.c:164
static int fix_coding_method_array(int sb, int channels, sb_int8_array coding_method)
Called while processing data from subpackets 11 and 12.
Definition: qdm2.c:526
Definition: vf_drawbox.c:37
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
static const float fft_tone_level_table[2][64]
Definition: qdm2data.h:438
static const uint8_t frame_size[4]
Definition: g723_1.h:219
static void average_quantized_coeffs(QDM2Context *q)
Replace 8 elements with their average value.
Definition: qdm2.c:470
Subpacket.
Definition: qdm2.c:84
int acc
Definition: yuv2rgb.c:482
int fft_coefs_index
Definition: qdm2.c:159
static void synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)
Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8.
Definition: qdm2.c:807
#define QDM2_MAX_FRAME_SIZE
Definition: qdm2.c:77
float synth_buf[MPA_MAX_CHANNELS][512 *2]
Definition: qdm2.c:173
static const uint8_t vlc_tab_type34_huffbits[10]
Definition: qdm2data.h:119
static const uint8_t fft_level_exp_alt_huffbits[28]
Definition: qdm2data.h:200
int size
Definition: avcodec.h:1347
int flag
Definition: cpu.c:35
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
const uint8_t * buffer
Definition: get_bits.h:55
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:58
static const uint16_t vlc_tab_fft_tone_offset_0_huffcodes[23]
Definition: qdm2data.h:124
int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8]
Definition: qdm2.c:182
static const uint8_t fft_stereo_phase_huffbits[9]
Definition: qdm2data.h:230
const float * table
Definition: qdm2.c:106
int coeff_per_sb_select
selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
Definition: qdm2.c:143
static av_cold int qdm2_decode_close(AVCodecContext *avctx)
Definition: qdm2.c:1899
short cutoff
Definition: qdm2.c:111
unsigned int size
subpacket size
Definition: qdm2.c:86
int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26]
Definition: qdm2.c:185
#define FF_ARRAY_ELEMS(a)
int sub_packet
Definition: qdm2.c:194
uint8_t run
Definition: svq3.c:203
float sb_samples[MPA_MAX_CHANNELS][128][SBLIMIT]
Definition: qdm2.c:175
#define AV_CH_LAYOUT_STEREO
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
int frequency_range
Definition: qdm2.c:141
static void qdm2_decode_sub_packet_header(GetBitContext *gb, QDM2SubPacket *sub_packet)
Fill a QDM2SubPacket structure with packet type, size, and data pointer.
Definition: qdm2.c:418
AVCodec.
Definition: avcodec.h:3120
static uint16_t softclip_table[HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD+1]
Definition: qdm2_tablegen.h:39
static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet, int offset, int duration, int channel, int exp, int phase)
Definition: qdm2.c:1363
QDM2SubPNode sub_packet_list_C[16]
packets with errors?
Definition: qdm2.c:151
static QDM2SubPNode * qdm2_search_subpacket_type_in_list(QDM2SubPNode *list, int type)
Return node pointer to first packet of requested type in list.
Definition: qdm2.c:453
float re
Definition: qdm2.c:99
static VLC vlc_tab_tone_level_idx_hi1
Definition: qdm2.c:206
int phase
Definition: qdm2.c:107
static VLC vlc_tab_type30
Definition: qdm2.c:209
static av_cold int qdm2_decode_init(AVCodecContext *avctx)
Init parameters from codec extradata.
Definition: qdm2.c:1720
QDM2 decoder context.
Definition: qdm2.c:129
static VLC vlc_tab_fft_tone_offset[5]
Definition: qdm2.c:211
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
#define av_cold
Definition: attributes.h:66
int fft_order
order of FFT (actually fftorder+1)
Definition: qdm2.c:139
#define AV_RB32
Definition: intreadwrite.h:130
static void qdm2_decode_fft_packets(QDM2Context *q)
Definition: qdm2.c:1460
int sub_sampling
subsampling: 0=25%, 1=50%, 2=100% */
Definition: qdm2.c:142
#define b
Definition: input.c:52
void ff_mpa_synth_init_float(float *window)
#define SOFTCLIP_THRESHOLD
Definition: qdm2_tablegen.h:30
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
Definition: qdm2.c:1580
int64_t duration
Definition: movenc.c:63
static const int16_t fft_level_index_table[256]
Definition: qdm2data.h:238
static const float fft_tone_envelope_table[4][31]
Definition: qdm2data.h:476
static const uint8_t vlc_tab_tone_level_idx_hi2_huffbits[24]
Definition: qdm2data.h:101
uint8_t * data
Definition: avcodec.h:1346
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
bitstream reader API header.
static const uint8_t coeff_per_sb_for_dequant[3][30]
Definition: qdm2data.h:300
int checksum_size
size of data block, used also for checksum
Definition: qdm2.c:135
static const uint16_t vlc_tab_fft_tone_offset_1_huffcodes[28]
Definition: qdm2data.h:135
#define FFALIGN(x, a)
Definition: macros.h:48
static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 12.
Definition: qdm2.c:1188
static int qdm2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: qdm2.c:1967
static const uint8_t fft_subpackets[32]
Definition: qdm2data.h:510
static const uint8_t vlc_tab_run_huffbits[6]
Definition: qdm2data.h:68
static const uint8_t vlc_tab_tone_level_idx_mid_huffbits[24]
Definition: qdm2data.h:90
static const uint8_t vlc_tab_type30_huffbits[9]
Definition: qdm2data.h:110
int channels
number of channels
Definition: qdm2.c:132
static void fill_tone_level_array(QDM2Context *q, int flag)
Related to synthesis filter Called by process_subpacket_10.
Definition: qdm2.c:592
static const uint8_t fft_stereo_exp_huffbits[7]
Definition: qdm2data.h:221
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
int synth_buf_offset[MPA_MAX_CHANNELS]
Definition: qdm2.c:174
static av_cold void rnd_table_init(void)
Definition: qdm2_tablegen.h:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static uint8_t random_dequant_type24[128][3]
Definition: qdm2_tablegen.h:42
static const uint8_t vlc_tab_fft_tone_offset_4_huffbits[38]
Definition: qdm2data.h:184
int compressed_size
Definition: qdm2.c:168
const uint8_t * data
pointer to subpacket data (points to input data buffer, it&#39;s not a private copy)
Definition: qdm2.c:87
int16_t offset
Definition: qdm2.c:117
float output_buffer[QDM2_MAX_FRAME_SIZE *2]
Definition: qdm2.c:169
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
static const int switchtable[23]
Definition: qdm2.c:217
int group_size
size of frame group (16 frames per group)
Definition: qdm2.c:133
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
void ff_mpa_synth_filter_float(MPADSPContext *s, float *synth_buf_ptr, int *synth_buf_offset, float *window, int *dither_state, float *samples, int incr, float *sb_samples)
static const uint16_t vlc_tab_level_huffcodes[24]
VLC TABLES.
Definition: qdm2data.h:38
int sub_packets_B
number of packets on &#39;B&#39; list
Definition: qdm2.c:150
QDM2SubPNode sub_packet_list_A[16]
list of all packets
Definition: qdm2.c:148
int noise_idx
index for dithering noise table
Definition: qdm2.c:195
Definition: avfft.h:73
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static VLC fft_level_exp_alt_vlc
Definition: qdm2.c:202
uint8_t channel
Definition: qdm2.c:116
int duration
Definition: qdm2.c:109
Definition: qdm2.c:122
float tone_level[MPA_MAX_CHANNELS][30][64]
Mixed temporary data used in decoding.
Definition: qdm2.c:179
float FFTSample
Definition: avfft.h:35
static const uint16_t vlc_tab_fft_tone_offset_4_huffcodes[38]
Definition: qdm2data.h:176
RDFTContext rdft_ctx
Definition: qdm2.c:163
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2203
int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8]
Definition: qdm2.c:181
static void qdm2_synthesis_filter(QDM2Context *q, int index)
Definition: qdm2.c:1671
static VLC vlc_tab_diff
Definition: qdm2.c:200
static VLC vlc_tab_level
Definition: qdm2.c:199
static void output_buffer(int16_t **samples, int nchan, int blocksize, int32_t **buffer)
Definition: shorten.c:256
#define QDM2_SB_USED(sub_sampling)
Definition: qdm2.c:63
#define MPA_MAX_CHANNELS
Definition: mpegaudio.h:41
int group_order
Parameters built from header parameters, do not change during playback.
Definition: qdm2.c:138
int bit_rate
the average bitrate
Definition: avcodec.h:1473
static const uint8_t vlc_tab_run_huffcodes[6]
Definition: qdm2data.h:64
audio channel layout utility functions
static const uint8_t vlc_tab_fft_tone_offset_1_huffbits[28]
Definition: qdm2data.h:142
static float noise_samples[128]
Definition: qdm2_tablegen.h:43
QDM2SubPNode sub_packet_list_B[16]
FFT packets B are on list.
Definition: qdm2.c:149
static const uint16_t fft_level_exp_alt_huffcodes[28]
FFT TABLES.
Definition: qdm2data.h:193
static const uint8_t vlc_tab_diff_huffbits[37]
Definition: qdm2data.h:57
struct QDM2SubPNode * next
pointer to next packet in the list, NULL if leaf node
Definition: qdm2.c:95
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
static const int8_t tone_level_idx_offset_table[30][4]
Definition: qdm2data.h:307
static VLC vlc_tab_type34
Definition: qdm2.c:210
float ff_mpa_synth_window_float[]
static VLC fft_stereo_exp_vlc
Definition: qdm2.c:204
static void qdm2_decode_super_block(QDM2Context *q)
Definition: qdm2.c:1239
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:493
#define SAMPLES_NEEDED_2(why)
Definition: qdm2.c:74
static const int8_t coding_method_table[5][30]
Definition: qdm2data.h:342
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
Definition: rdft.h:60
static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
QDM2 checksum.
Definition: qdm2.c:402
static const uint16_t vlc_tab_tone_level_idx_hi1_huffcodes[20]
Definition: qdm2data.h:73
#define QDM2_LIST_ADD(list, size, packet)
Definition: qdm2.c:52
static uint8_t random_dequant_index[256][5]
Definition: qdm2_tablegen.h:41
static const float type30_dequant[8]
Definition: qdm2data.h:521
static VLC vlc_tab_tone_level_idx_hi2
Definition: qdm2.c:208
static VLC fft_level_exp_vlc
Definition: qdm2.c:203
int fft_tone_end
Definition: qdm2.c:157
QDM2Complex complex[MPA_MAX_CHANNELS][256]
Definition: qdm2.c:123
static const float type34_delta[10]
Definition: qdm2data.h:526
#define INIT_VLC_LE
Definition: vlc.h:54
int bits
Definition: vlc.h:27
if(ac->has_optimized_func)
static const float dequant_1bit[2][3]
Definition: qdm2data.h:516
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
static const uint8_t fft_stereo_exp_huffcodes[7]
Definition: qdm2data.h:217
int table_allocated
Definition: vlc.h:29
static void build_sb_samples_from_noise(QDM2Context *q, int sb)
Build subband samples with noise weighted by q->tone_level.
Definition: qdm2.c:499
float samples[MPA_MAX_CHANNELS *MPA_FRAME_SIZE]
Definition: qdm2.c:176
NULL
Definition: eval.c:55
static const uint8_t last_coeff[3]
Definition: qdm2data.h:257
Libavcodec external API header.
static const int fft_cutoff_index_table[4][2]
Definition: qdm2data.h:234
int sample_rate
samples per second
Definition: avcodec.h:2152
#define SAMPLES_NEEDED
Definition: qdm2.c:71
static void qdm2_fft_decode_tones(QDM2Context *q, int duration, GetBitContext *gb, int b)
Definition: qdm2.c:1379
static const uint8_t coeff_per_sb_for_avg[3][30]
Definition: qdm2data.h:261
int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8]
Definition: qdm2.c:184
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
AVCodec ff_qdm2_decoder
Definition: qdm2.c:2001
uint8_t phase
Definition: qdm2.c:119
int fft_coefs_min_index[5]
Definition: qdm2.c:160
static void process_subpacket_9(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 9, init quantized_coeffs with data from it.
Definition: qdm2.c:1103
FFTCoefficient fft_coefs[1000]
Definition: qdm2.c:158
int extradata_size
Definition: avcodec.h:1524
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
int index
Definition: gxfenc.c:72
int has_errors
packet has errors
Definition: qdm2.c:190
static const uint8_t dequant_table[64]
Definition: 4xm.c:112
int fft_level_exp[6]
Definition: qdm2.c:162
static const uint16_t vlc_tab_fft_tone_offset_2_huffcodes[32]
Definition: qdm2data.h:148
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
static void fill_coding_method_array(sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp, sb_int8_array coding_method, int nb_channels, int c, int superblocktype_2_3, int cm_table_select)
Related to synthesis filter Called by process_subpacket_11 c is built with data from subpacket 11 Mos...
Definition: qdm2.c:681
int16_t sub_packet
Definition: qdm2.c:115
#define HARDCLIP_THRESHOLD
Definition: qdm2_tablegen.h:31
float im
Definition: qdm2.c:100
static VLC vlc_tab_run
Definition: qdm2.c:201
int16_t exp
Definition: qdm2.c:118
static const uint8_t vlc_tab_type34_huffcodes[10]
Definition: qdm2data.h:115
int8_t coding_method[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:180
static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth)
Definition: qdm2.c:386
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 10 if not null, else.
Definition: qdm2.c:1140
static void init_quantized_coeffs_elem0(int8_t *quantized_coeffs, GetBitContext *gb)
Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch...
Definition: qdm2.c:1003
static av_cold void softclip_table_init(void)
Definition: qdm2_tablegen.h:45
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
uint8_t level
Definition: svq3.c:204
int fft_size
size of FFT, in complex numbers
Definition: qdm2.c:134
static const uint8_t vlc_tab_fft_tone_offset_0_huffbits[23]
Definition: qdm2data.h:130
int type
subpacket type
Definition: qdm2.c:85
int fft_coefs_max_index[5]
Definition: qdm2.c:161
int frame_size
size of data frame
Definition: qdm2.c:140
static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
Definition: qdm2.c:1908
#define FIX_NOISE_IDX(noise_idx)
Definition: qdm2.c:65
static const float fft_tone_sample_table[4][16][5]
Definition: qdm2data.h:368
Definition: qdm2.c:103
int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8]
Definition: qdm2.c:183
int nb_channels
Parameters from codec header, do not change during playback.
Definition: qdm2.c:131
int superblocktype_2_3
select fft tables and some algorithm based on superblock type
Definition: qdm2.c:191
static const uint8_t fft_stereo_phase_huffcodes[9]
Definition: qdm2data.h:226
common internal api header.
static const uint16_t vlc_tab_tone_level_idx_hi2_huffcodes[24]
Definition: qdm2data.h:95
static const uint16_t vlc_tab_fft_tone_offset_3_huffcodes[35]
Definition: qdm2data.h:161
int cm_table_select
selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
Definition: qdm2.c:144
signed 16 bits
Definition: samplefmt.h:63
#define INIT_VLC_USE_NEW_STATIC
Definition: vlc.h:55
QDM2SubPacket * packet
packet
Definition: qdm2.c:94
QDM2SubPacket sub_packets[16]
Packets and packet lists.
Definition: qdm2.c:147
static const int vlc_stage3_values[60]
Definition: qdm2data.h:360
static const uint8_t vlc_tab_fft_tone_offset_2_huffbits[32]
Definition: qdm2data.h:155
mpeg audio declarations for both encoder and decoder.
static const uint16_t vlc_tab_diff_huffcodes[37]
Definition: qdm2data.h:49
QDM2Complex * complex
Definition: qdm2.c:105
int do_synth_filter
used to perform or skip synthesis filter
Definition: qdm2.c:192
const uint8_t * compressed_data
I/O data.
Definition: qdm2.c:167
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:187
#define MKBETAG(a, b, c, d)
Definition: common.h:257
static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 11.
Definition: qdm2.c:1159
static int qdm2_get_vlc(GetBitContext *gb, VLC *vlc, int flag, int depth)
Definition: qdm2.c:364
MPADSPContext mpadsp
Synthesis filter.
Definition: qdm2.c:172
void * priv_data
Definition: avcodec.h:1451
static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb)
Related to synthesis filter, process data from packet 10 Init part of quantized_coeffs via function i...
Definition: qdm2.c:1040
static VLC fft_stereo_phase_vlc
Definition: qdm2.c:205
int channels
number of audio channels
Definition: avcodec.h:2153
#define av_log2
Definition: intmath.h:85
static const uint8_t vlc_tab_tone_level_idx_hi1_huffbits[20]
Definition: qdm2data.h:79
static void qdm2_fft_generate_tone(QDM2Context *q, FFTTone *tone)
Definition: qdm2.c:1534
QDM2SubPNode sub_packet_list_D[16]
DCT packets.
Definition: qdm2.c:152
static uint8_t tmp[8]
Definition: des.c:38
static VLC vlc_tab_tone_level_idx_mid
Definition: qdm2.c:207
int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:186
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static const uint16_t qdm2_vlc_offs[]
Definition: qdm2.c:213
static const uint8_t vlc_tab_type30_huffcodes[9]
Definition: qdm2data.h:106
static const uint8_t fft_level_exp_huffbits[20]
Definition: qdm2data.h:212
static const struct twinvq_data tab
static av_cold void qdm2_init_vlc(void)
Definition: qdm2.c:221
FILE * out
Definition: movenc.c:54
short time_index
Definition: qdm2.c:110
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
int8_t sb_int8_array[2][30][64]
Definition: qdm2.c:79
#define SB_DITHERING_NOISE(sb, noise_idx)
Definition: qdm2.c:69
static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
Definition: qdm2.c:1651
#define VLC_TYPE
Definition: vlc.h:24
static const uint8_t vlc_tab_fft_tone_offset_3_huffbits[35]
Definition: qdm2data.h:169
int nb_channels
int phase_shift
Definition: qdm2.c:108
static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
Definition: qdm2.c:1207
#define AV_CH_LAYOUT_MONO
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.
Definition: rdft.c:99
#define MPA_FRAME_SIZE
Definition: mpegaudio.h:36
float min
This structure stores compressed data.
Definition: avcodec.h:1323
av_cold void ff_mpadsp_init(MPADSPContext *s)
Definition: mpegaudiodsp.c:27
static const uint16_t fft_level_exp_huffcodes[20]
Definition: qdm2data.h:206
static av_cold void init_noise_samples(void)
Definition: qdm2_tablegen.h:91
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
for(j=16;j >0;--j)
float level
Definition: qdm2.c:104
int fft_tone_start
Definition: qdm2.c:156