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 
38 #define BITSTREAM_READER_LE
40 #include "avcodec.h"
41 #include "get_bits.h"
42 #include "dsputil.h"
43 #include "internal.h"
44 #include "rdft.h"
45 #include "mpegaudiodsp.h"
46 #include "mpegaudio.h"
47 
48 #include "qdm2data.h"
49 #include "qdm2_tablegen.h"
50 
51 #undef NDEBUG
52 #include <assert.h>
53 
54 
55 #define QDM2_LIST_ADD(list, size, packet) \
56 do { \
57  if (size > 0) { \
58  list[size - 1].next = &list[size]; \
59  } \
60  list[size].packet = packet; \
61  list[size].next = NULL; \
62  size++; \
63 } while(0)
64 
65 // Result is 8, 16 or 30
66 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
67 
68 #define FIX_NOISE_IDX(noise_idx) \
69  if ((noise_idx) >= 3840) \
70  (noise_idx) -= 3840; \
71 
72 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
73 
74 #define SAMPLES_NEEDED \
75  av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
76 
77 #define SAMPLES_NEEDED_2(why) \
78  av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
79 
80 #define QDM2_MAX_FRAME_SIZE 512
81 
82 typedef int8_t sb_int8_array[2][30][64];
83 
87 typedef struct {
88  int type;
89  unsigned int size;
90  const uint8_t *data;
92 
96 typedef struct QDM2SubPNode {
98  struct QDM2SubPNode *next;
99 } QDM2SubPNode;
100 
101 typedef struct {
102  float re;
103  float im;
104 } QDM2Complex;
105 
106 typedef struct {
107  float level;
109  const float *table;
110  int phase;
112  int duration;
113  short time_index;
114  short cutoff;
115 } FFTTone;
116 
117 typedef struct {
118  int16_t sub_packet;
120  int16_t offset;
121  int16_t exp;
124 
125 typedef struct {
127 } QDM2FFT;
128 
132 typedef struct {
134 
137  int channels;
139  int fft_size;
141 
144  int fft_order;
150 
152  QDM2SubPacket sub_packets[16];
153  QDM2SubPNode sub_packet_list_A[16];
154  QDM2SubPNode sub_packet_list_B[16];
156  QDM2SubPNode sub_packet_list_C[16];
157  QDM2SubPNode sub_packet_list_D[16];
158 
160  FFTTone fft_tones[1000];
163  FFTCoefficient fft_coefs[1000];
165  int fft_coefs_min_index[5];
166  int fft_coefs_max_index[5];
167  int fft_level_exp[6];
170 
175 
178  DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2];
179  int synth_buf_offset[MPA_MAX_CHANNELS];
180  DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
182 
184  float tone_level[MPA_MAX_CHANNELS][30][64];
185  int8_t coding_method[MPA_MAX_CHANNELS][30][64];
186  int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
187  int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
188  int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
189  int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
190  int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
191  int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
192  int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
193 
194  // Flags
198 
200  int noise_idx;
201 } QDM2Context;
202 
203 
217 
218 static const uint16_t qdm2_vlc_offs[] = {
219  0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
220 };
221 
222 static const int switchtable[23] = {
223  0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
224 };
225 
226 static av_cold void qdm2_init_vlc(void)
227 {
228  static VLC_TYPE qdm2_table[3838][2];
229 
230  vlc_tab_level.table = &qdm2_table[qdm2_vlc_offs[0]];
231  vlc_tab_level.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0];
232  init_vlc(&vlc_tab_level, 8, 24,
236 
237  vlc_tab_diff.table = &qdm2_table[qdm2_vlc_offs[1]];
238  vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
239  init_vlc(&vlc_tab_diff, 8, 37,
240  vlc_tab_diff_huffbits, 1, 1,
243 
244  vlc_tab_run.table = &qdm2_table[qdm2_vlc_offs[2]];
245  vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
246  init_vlc(&vlc_tab_run, 5, 6,
247  vlc_tab_run_huffbits, 1, 1,
248  vlc_tab_run_huffcodes, 1, 1,
250 
251  fft_level_exp_alt_vlc.table = &qdm2_table[qdm2_vlc_offs[3]];
252  fft_level_exp_alt_vlc.table_allocated = qdm2_vlc_offs[4] -
253  qdm2_vlc_offs[3];
254  init_vlc(&fft_level_exp_alt_vlc, 8, 28,
258 
259  fft_level_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[4]];
260  fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
261  init_vlc(&fft_level_exp_vlc, 8, 20,
265 
266  fft_stereo_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[5]];
267  fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] -
268  qdm2_vlc_offs[5];
269  init_vlc(&fft_stereo_exp_vlc, 6, 7,
273 
274  fft_stereo_phase_vlc.table = &qdm2_table[qdm2_vlc_offs[6]];
275  fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] -
276  qdm2_vlc_offs[6];
277  init_vlc(&fft_stereo_phase_vlc, 6, 9,
281 
282  vlc_tab_tone_level_idx_hi1.table =
283  &qdm2_table[qdm2_vlc_offs[7]];
284  vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] -
285  qdm2_vlc_offs[7];
286  init_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20,
290 
291  vlc_tab_tone_level_idx_mid.table =
292  &qdm2_table[qdm2_vlc_offs[8]];
293  vlc_tab_tone_level_idx_mid.table_allocated = qdm2_vlc_offs[9] -
294  qdm2_vlc_offs[8];
295  init_vlc(&vlc_tab_tone_level_idx_mid, 8, 24,
299 
300  vlc_tab_tone_level_idx_hi2.table =
301  &qdm2_table[qdm2_vlc_offs[9]];
302  vlc_tab_tone_level_idx_hi2.table_allocated = qdm2_vlc_offs[10] -
303  qdm2_vlc_offs[9];
304  init_vlc(&vlc_tab_tone_level_idx_hi2, 8, 24,
308 
309  vlc_tab_type30.table = &qdm2_table[qdm2_vlc_offs[10]];
310  vlc_tab_type30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
311  init_vlc(&vlc_tab_type30, 6, 9,
315 
316  vlc_tab_type34.table = &qdm2_table[qdm2_vlc_offs[11]];
317  vlc_tab_type34.table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
318  init_vlc(&vlc_tab_type34, 5, 10,
322 
323  vlc_tab_fft_tone_offset[0].table =
324  &qdm2_table[qdm2_vlc_offs[12]];
325  vlc_tab_fft_tone_offset[0].table_allocated = qdm2_vlc_offs[13] -
326  qdm2_vlc_offs[12];
327  init_vlc(&vlc_tab_fft_tone_offset[0], 8, 23,
331 
332  vlc_tab_fft_tone_offset[1].table =
333  &qdm2_table[qdm2_vlc_offs[13]];
334  vlc_tab_fft_tone_offset[1].table_allocated = qdm2_vlc_offs[14] -
335  qdm2_vlc_offs[13];
336  init_vlc(&vlc_tab_fft_tone_offset[1], 8, 28,
340 
341  vlc_tab_fft_tone_offset[2].table =
342  &qdm2_table[qdm2_vlc_offs[14]];
343  vlc_tab_fft_tone_offset[2].table_allocated = qdm2_vlc_offs[15] -
344  qdm2_vlc_offs[14];
345  init_vlc(&vlc_tab_fft_tone_offset[2], 8, 32,
349 
350  vlc_tab_fft_tone_offset[3].table =
351  &qdm2_table[qdm2_vlc_offs[15]];
352  vlc_tab_fft_tone_offset[3].table_allocated = qdm2_vlc_offs[16] -
353  qdm2_vlc_offs[15];
354  init_vlc(&vlc_tab_fft_tone_offset[3], 8, 35,
358 
359  vlc_tab_fft_tone_offset[4].table =
360  &qdm2_table[qdm2_vlc_offs[16]];
361  vlc_tab_fft_tone_offset[4].table_allocated = qdm2_vlc_offs[17] -
362  qdm2_vlc_offs[16];
363  init_vlc(&vlc_tab_fft_tone_offset[4], 8, 38,
367 }
368 
369 static int qdm2_get_vlc(GetBitContext *gb, VLC *vlc, int flag, int depth)
370 {
371  int value;
372 
373  value = get_vlc2(gb, vlc->table, vlc->bits, depth);
374 
375  /* stage-2, 3 bits exponent escape sequence */
376  if (value-- == 0)
377  value = get_bits(gb, get_bits(gb, 3) + 1);
378 
379  /* stage-3, optional */
380  if (flag) {
381  int tmp = vlc_stage3_values[value];
382 
383  if ((value & ~3) > 0)
384  tmp += get_bits(gb, (value >> 2));
385  value = tmp;
386  }
387 
388  return value;
389 }
390 
391 static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth)
392 {
393  int value = qdm2_get_vlc(gb, vlc, 0, depth);
394 
395  return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
396 }
397 
407 static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
408 {
409  int i;
410 
411  for (i = 0; i < length; i++)
412  value -= data[i];
413 
414  return (uint16_t)(value & 0xffff);
415 }
416 
424  QDM2SubPacket *sub_packet)
425 {
426  sub_packet->type = get_bits(gb, 8);
427 
428  if (sub_packet->type == 0) {
429  sub_packet->size = 0;
430  sub_packet->data = NULL;
431  } else {
432  sub_packet->size = get_bits(gb, 8);
433 
434  if (sub_packet->type & 0x80) {
435  sub_packet->size <<= 8;
436  sub_packet->size |= get_bits(gb, 8);
437  sub_packet->type &= 0x7f;
438  }
439 
440  if (sub_packet->type == 0x7f)
441  sub_packet->type |= (get_bits(gb, 8) << 8);
442 
443  // FIXME: this depends on bitreader-internal data
444  sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];
445  }
446 
447  av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n",
448  sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
449 }
450 
459  int type)
460 {
461  while (list != NULL && list->packet != NULL) {
462  if (list->packet->type == type)
463  return list;
464  list = list->next;
465  }
466  return NULL;
467 }
468 
476 {
477  int i, j, n, ch, sum;
478 
480 
481  for (ch = 0; ch < q->nb_channels; ch++)
482  for (i = 0; i < n; i++) {
483  sum = 0;
484 
485  for (j = 0; j < 8; j++)
486  sum += q->quantized_coeffs[ch][i][j];
487 
488  sum /= 8;
489  if (sum > 0)
490  sum--;
491 
492  for (j = 0; j < 8; j++)
493  q->quantized_coeffs[ch][i][j] = sum;
494  }
495 }
496 
505 {
506  int ch, j;
507 
509 
510  if (!q->nb_channels)
511  return;
512 
513  for (ch = 0; ch < q->nb_channels; ch++) {
514  for (j = 0; j < 64; j++) {
515  q->sb_samples[ch][j * 2][sb] =
516  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
517  q->sb_samples[ch][j * 2 + 1][sb] =
518  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
519  }
520  }
521 }
522 
531 static int fix_coding_method_array(int sb, int channels,
532  sb_int8_array coding_method)
533 {
534  int j, k;
535  int ch;
536  int run, case_val;
537 
538  for (ch = 0; ch < channels; ch++) {
539  for (j = 0; j < 64; ) {
540  if (coding_method[ch][sb][j] < 8)
541  return -1;
542  if ((coding_method[ch][sb][j] - 8) > 22) {
543  run = 1;
544  case_val = 8;
545  } else {
546  switch (switchtable[coding_method[ch][sb][j] - 8]) {
547  case 0: run = 10;
548  case_val = 10;
549  break;
550  case 1: run = 1;
551  case_val = 16;
552  break;
553  case 2: run = 5;
554  case_val = 24;
555  break;
556  case 3: run = 3;
557  case_val = 30;
558  break;
559  case 4: run = 1;
560  case_val = 30;
561  break;
562  case 5: run = 1;
563  case_val = 8;
564  break;
565  default: run = 1;
566  case_val = 8;
567  break;
568  }
569  }
570  for (k = 0; k < run; k++) {
571  if (j + k < 128) {
572  if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) {
573  if (k > 0) {
575  //not debugged, almost never used
576  memset(&coding_method[ch][sb][j + k], case_val,
577  k *sizeof(int8_t));
578  memset(&coding_method[ch][sb][j + k], case_val,
579  3 * sizeof(int8_t));
580  }
581  }
582  }
583  }
584  j += run;
585  }
586  }
587  return 0;
588 }
589 
597 static void fill_tone_level_array(QDM2Context *q, int flag)
598 {
599  int i, sb, ch, sb_used;
600  int tmp, tab;
601 
602  for (ch = 0; ch < q->nb_channels; ch++)
603  for (sb = 0; sb < 30; sb++)
604  for (i = 0; i < 8; i++) {
606  tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
608  else
609  tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
610  if(tmp < 0)
611  tmp += 0xff;
612  q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
613  }
614 
615  sb_used = QDM2_SB_USED(q->sub_sampling);
616 
617  if ((q->superblocktype_2_3 != 0) && !flag) {
618  for (sb = 0; sb < sb_used; sb++)
619  for (ch = 0; ch < q->nb_channels; ch++)
620  for (i = 0; i < 64; i++) {
621  q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
622  if (q->tone_level_idx[ch][sb][i] < 0)
623  q->tone_level[ch][sb][i] = 0;
624  else
625  q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
626  }
627  } else {
628  tab = q->superblocktype_2_3 ? 0 : 1;
629  for (sb = 0; sb < sb_used; sb++) {
630  if ((sb >= 4) && (sb <= 23)) {
631  for (ch = 0; ch < q->nb_channels; ch++)
632  for (i = 0; i < 64; i++) {
633  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
634  q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
635  q->tone_level_idx_mid[ch][sb - 4][i / 8] -
636  q->tone_level_idx_hi2[ch][sb - 4];
637  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
638  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
639  q->tone_level[ch][sb][i] = 0;
640  else
641  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
642  }
643  } else {
644  if (sb > 4) {
645  for (ch = 0; ch < q->nb_channels; ch++)
646  for (i = 0; i < 64; i++) {
647  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
648  q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
649  q->tone_level_idx_hi2[ch][sb - 4];
650  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
651  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
652  q->tone_level[ch][sb][i] = 0;
653  else
654  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
655  }
656  } else {
657  for (ch = 0; ch < q->nb_channels; ch++)
658  for (i = 0; i < 64; i++) {
659  tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
660  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
661  q->tone_level[ch][sb][i] = 0;
662  else
663  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
664  }
665  }
666  }
667  }
668  }
669 }
670 
686 static void fill_coding_method_array(sb_int8_array tone_level_idx,
687  sb_int8_array tone_level_idx_temp,
688  sb_int8_array coding_method,
689  int nb_channels,
690  int c, int superblocktype_2_3,
691  int cm_table_select)
692 {
693  int ch, sb, j;
694  int tmp, acc, esp_40, comp;
695  int add1, add2, add3, add4;
696  int64_t multres;
697 
698  if (!superblocktype_2_3) {
699  /* This case is untested, no samples available */
701  for (ch = 0; ch < nb_channels; ch++)
702  for (sb = 0; sb < 30; sb++) {
703  for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
704  add1 = tone_level_idx[ch][sb][j] - 10;
705  if (add1 < 0)
706  add1 = 0;
707  add2 = add3 = add4 = 0;
708  if (sb > 1) {
709  add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
710  if (add2 < 0)
711  add2 = 0;
712  }
713  if (sb > 0) {
714  add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
715  if (add3 < 0)
716  add3 = 0;
717  }
718  if (sb < 29) {
719  add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
720  if (add4 < 0)
721  add4 = 0;
722  }
723  tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
724  if (tmp < 0)
725  tmp = 0;
726  tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
727  }
728  tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
729  }
730  acc = 0;
731  for (ch = 0; ch < nb_channels; ch++)
732  for (sb = 0; sb < 30; sb++)
733  for (j = 0; j < 64; j++)
734  acc += tone_level_idx_temp[ch][sb][j];
735 
736  multres = 0x66666667 * (acc * 10);
737  esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
738  for (ch = 0; ch < nb_channels; ch++)
739  for (sb = 0; sb < 30; sb++)
740  for (j = 0; j < 64; j++) {
741  comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
742  if (comp < 0)
743  comp += 0xff;
744  comp /= 256; // signed shift
745  switch(sb) {
746  case 0:
747  if (comp < 30)
748  comp = 30;
749  comp += 15;
750  break;
751  case 1:
752  if (comp < 24)
753  comp = 24;
754  comp += 10;
755  break;
756  case 2:
757  case 3:
758  case 4:
759  if (comp < 16)
760  comp = 16;
761  }
762  if (comp <= 5)
763  tmp = 0;
764  else if (comp <= 10)
765  tmp = 10;
766  else if (comp <= 16)
767  tmp = 16;
768  else if (comp <= 24)
769  tmp = -1;
770  else
771  tmp = 0;
772  coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
773  }
774  for (sb = 0; sb < 30; sb++)
775  fix_coding_method_array(sb, nb_channels, coding_method);
776  for (ch = 0; ch < nb_channels; ch++)
777  for (sb = 0; sb < 30; sb++)
778  for (j = 0; j < 64; j++)
779  if (sb >= 10) {
780  if (coding_method[ch][sb][j] < 10)
781  coding_method[ch][sb][j] = 10;
782  } else {
783  if (sb >= 2) {
784  if (coding_method[ch][sb][j] < 16)
785  coding_method[ch][sb][j] = 16;
786  } else {
787  if (coding_method[ch][sb][j] < 30)
788  coding_method[ch][sb][j] = 30;
789  }
790  }
791  } else { // superblocktype_2_3 != 0
792  for (ch = 0; ch < nb_channels; ch++)
793  for (sb = 0; sb < 30; sb++)
794  for (j = 0; j < 64; j++)
795  coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
796  }
797 }
798 
813  int length, int sb_min, int sb_max)
814 {
815  int sb, j, k, n, ch, run, channels;
816  int joined_stereo, zero_encoding;
817  int type34_first;
818  float type34_div = 0;
819  float type34_predictor;
820  float samples[10], sign_bits[16];
821 
822  if (length == 0) {
823  // If no data use noise
824  for (sb=sb_min; sb < sb_max; sb++)
826 
827  return;
828  }
829 
830  for (sb = sb_min; sb < sb_max; sb++) {
831  channels = q->nb_channels;
832 
833  if (q->nb_channels <= 1 || sb < 12)
834  joined_stereo = 0;
835  else if (sb >= 24)
836  joined_stereo = 1;
837  else
838  joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1 (gb) : 0;
839 
840  if (joined_stereo) {
841  if (get_bits_left(gb) >= 16)
842  for (j = 0; j < 16; j++)
843  sign_bits[j] = get_bits1 (gb);
844 
845  for (j = 0; j < 64; j++)
846  if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
847  q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
848 
850  q->coding_method)) {
852  continue;
853  }
854  channels = 1;
855  }
856 
857  for (ch = 0; ch < channels; ch++) {
859  zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
860  type34_predictor = 0.0;
861  type34_first = 1;
862 
863  for (j = 0; j < 128; ) {
864  switch (q->coding_method[ch][sb][j / 2]) {
865  case 8:
866  if (get_bits_left(gb) >= 10) {
867  if (zero_encoding) {
868  for (k = 0; k < 5; k++) {
869  if ((j + 2 * k) >= 128)
870  break;
871  samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
872  }
873  } else {
874  n = get_bits(gb, 8);
875  for (k = 0; k < 5; k++)
876  samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
877  }
878  for (k = 0; k < 5; k++)
879  samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
880  } else {
881  for (k = 0; k < 10; k++)
882  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
883  }
884  run = 10;
885  break;
886 
887  case 10:
888  if (get_bits_left(gb) >= 1) {
889  float f = 0.81;
890 
891  if (get_bits1(gb))
892  f = -f;
893  f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
894  samples[0] = f;
895  } else {
896  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
897  }
898  run = 1;
899  break;
900 
901  case 16:
902  if (get_bits_left(gb) >= 10) {
903  if (zero_encoding) {
904  for (k = 0; k < 5; k++) {
905  if ((j + k) >= 128)
906  break;
907  samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
908  }
909  } else {
910  n = get_bits (gb, 8);
911  for (k = 0; k < 5; k++)
912  samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
913  }
914  } else {
915  for (k = 0; k < 5; k++)
916  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
917  }
918  run = 5;
919  break;
920 
921  case 24:
922  if (get_bits_left(gb) >= 7) {
923  n = get_bits(gb, 7);
924  for (k = 0; k < 3; k++)
925  samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
926  } else {
927  for (k = 0; k < 3; k++)
928  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
929  }
930  run = 3;
931  break;
932 
933  case 30:
934  if (get_bits_left(gb) >= 4) {
935  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
936  if (index < FF_ARRAY_ELEMS(type30_dequant)) {
937  samples[0] = type30_dequant[index];
938  } else
939  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
940  } else
941  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
942 
943  run = 1;
944  break;
945 
946  case 34:
947  if (get_bits_left(gb) >= 7) {
948  if (type34_first) {
949  type34_div = (float)(1 << get_bits(gb, 2));
950  samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
951  type34_predictor = samples[0];
952  type34_first = 0;
953  } else {
954  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
955  if (index < FF_ARRAY_ELEMS(type34_delta)) {
956  samples[0] = type34_delta[index] / type34_div + type34_predictor;
957  type34_predictor = samples[0];
958  } else
959  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
960  }
961  } else {
962  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
963  }
964  run = 1;
965  break;
966 
967  default:
968  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
969  run = 1;
970  break;
971  }
972 
973  if (joined_stereo) {
974  for (k = 0; k < run && j + k < 128; k++) {
975  q->sb_samples[0][j + k][sb] =
976  q->tone_level[0][sb][(j + k) / 2] * samples[k];
977  if (q->nb_channels == 2) {
978  if (sign_bits[(j + k) / 8])
979  q->sb_samples[1][j + k][sb] =
980  q->tone_level[1][sb][(j + k) / 2] * -samples[k];
981  else
982  q->sb_samples[1][j + k][sb] =
983  q->tone_level[1][sb][(j + k) / 2] * samples[k];
984  }
985  }
986  } else {
987  for (k = 0; k < run; k++)
988  if ((j + k) < 128)
989  q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
990  }
991 
992  j += run;
993  } // j loop
994  } // channel loop
995  } // subband loop
996 }
997 
1008 static void init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
1009  GetBitContext *gb)
1010 {
1011  int i, k, run, level, diff;
1012 
1013  if (get_bits_left(gb) < 16)
1014  return;
1015  level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
1016 
1017  quantized_coeffs[0] = level;
1018 
1019  for (i = 0; i < 7; ) {
1020  if (get_bits_left(gb) < 16)
1021  break;
1022  run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
1023 
1024  if (get_bits_left(gb) < 16)
1025  break;
1026  diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
1027 
1028  for (k = 1; k <= run; k++)
1029  quantized_coeffs[i + k] = (level + ((k * diff) / run));
1030 
1031  level += diff;
1032  i += run;
1033  }
1034 }
1035 
1046 {
1047  int sb, j, k, n, ch;
1048 
1049  for (ch = 0; ch < q->nb_channels; ch++) {
1051 
1052  if (get_bits_left(gb) < 16) {
1053  memset(q->quantized_coeffs[ch][0], 0, 8);
1054  break;
1055  }
1056  }
1057 
1058  n = q->sub_sampling + 1;
1059 
1060  for (sb = 0; sb < n; sb++)
1061  for (ch = 0; ch < q->nb_channels; ch++)
1062  for (j = 0; j < 8; j++) {
1063  if (get_bits_left(gb) < 1)
1064  break;
1065  if (get_bits1(gb)) {
1066  for (k=0; k < 8; k++) {
1067  if (get_bits_left(gb) < 16)
1068  break;
1069  q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
1070  }
1071  } else {
1072  for (k=0; k < 8; k++)
1073  q->tone_level_idx_hi1[ch][sb][j][k] = 0;
1074  }
1075  }
1076 
1077  n = QDM2_SB_USED(q->sub_sampling) - 4;
1078 
1079  for (sb = 0; sb < n; sb++)
1080  for (ch = 0; ch < q->nb_channels; ch++) {
1081  if (get_bits_left(gb) < 16)
1082  break;
1083  q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
1084  if (sb > 19)
1085  q->tone_level_idx_hi2[ch][sb] -= 16;
1086  else
1087  for (j = 0; j < 8; j++)
1088  q->tone_level_idx_mid[ch][sb][j] = -16;
1089  }
1090 
1091  n = QDM2_SB_USED(q->sub_sampling) - 5;
1092 
1093  for (sb = 0; sb < n; sb++)
1094  for (ch = 0; ch < q->nb_channels; ch++)
1095  for (j = 0; j < 8; j++) {
1096  if (get_bits_left(gb) < 16)
1097  break;
1098  q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
1099  }
1100 }
1101 
1109 {
1110  GetBitContext gb;
1111  int i, j, k, n, ch, run, level, diff;
1112 
1113  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1114 
1116 
1117  for (i = 1; i < n; i++)
1118  for (ch = 0; ch < q->nb_channels; ch++) {
1119  level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
1120  q->quantized_coeffs[ch][i][0] = level;
1121 
1122  for (j = 0; j < (8 - 1); ) {
1123  run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
1124  diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
1125 
1126  for (k = 1; k <= run; k++)
1127  q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
1128 
1129  level += diff;
1130  j += run;
1131  }
1132  }
1133 
1134  for (ch = 0; ch < q->nb_channels; ch++)
1135  for (i = 0; i < 8; i++)
1136  q->quantized_coeffs[ch][0][i] = 0;
1137 }
1138 
1146 {
1147  GetBitContext gb;
1148 
1149  if (node) {
1150  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1152  fill_tone_level_array(q, 1);
1153  } else {
1154  fill_tone_level_array(q, 0);
1155  }
1156 }
1157 
1165 {
1166  GetBitContext gb;
1167  int length = 0;
1168 
1169  if (node) {
1170  length = node->packet->size * 8;
1171  init_get_bits(&gb, node->packet->data, length);
1172  }
1173 
1174  if (length >= 32) {
1175  int c = get_bits(&gb, 13);
1176 
1177  if (c > 3)
1180  q->nb_channels, 8 * c,
1182  }
1183 
1184  synthfilt_build_sb_samples(q, &gb, length, 0, 8);
1185 }
1186 
1194 {
1195  GetBitContext gb;
1196  int length = 0;
1197 
1198  if (node) {
1199  length = node->packet->size * 8;
1200  init_get_bits(&gb, node->packet->data, length);
1201  }
1202 
1203  synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
1204 }
1205 
1206 /*
1207  * Process new subpackets for synthesis filter
1208  *
1209  * @param q context
1210  * @param list list with synthesis filter packets (list D)
1211  */
1213 {
1214  QDM2SubPNode *nodes[4];
1215 
1216  nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
1217  if (nodes[0] != NULL)
1218  process_subpacket_9(q, nodes[0]);
1219 
1220  nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1221  if (nodes[1] != NULL)
1222  process_subpacket_10(q, nodes[1]);
1223  else
1225 
1226  nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1227  if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
1228  process_subpacket_11(q, nodes[2]);
1229  else
1231 
1232  nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1233  if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
1234  process_subpacket_12(q, nodes[3]);
1235  else
1237 }
1238 
1239 /*
1240  * Decode superblock, fill packet lists.
1241  *
1242  * @param q context
1243  */
1245 {
1246  GetBitContext gb;
1247  QDM2SubPacket header, *packet;
1248  int i, packet_bytes, sub_packet_size, sub_packets_D;
1249  unsigned int next_index = 0;
1250 
1251  memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1252  memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1253  memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1254 
1255  q->sub_packets_B = 0;
1256  sub_packets_D = 0;
1257 
1258  average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1259 
1261  qdm2_decode_sub_packet_header(&gb, &header);
1262 
1263  if (header.type < 2 || header.type >= 8) {
1264  q->has_errors = 1;
1265  av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
1266  return;
1267  }
1268 
1269  q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1270  packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
1271 
1272  init_get_bits(&gb, header.data, header.size * 8);
1273 
1274  if (header.type == 2 || header.type == 4 || header.type == 5) {
1275  int csum = 257 * get_bits(&gb, 8);
1276  csum += 2 * get_bits(&gb, 8);
1277 
1278  csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1279 
1280  if (csum != 0) {
1281  q->has_errors = 1;
1282  av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
1283  return;
1284  }
1285  }
1286 
1287  q->sub_packet_list_B[0].packet = NULL;
1288  q->sub_packet_list_D[0].packet = NULL;
1289 
1290  for (i = 0; i < 6; i++)
1291  if (--q->fft_level_exp[i] < 0)
1292  q->fft_level_exp[i] = 0;
1293 
1294  for (i = 0; packet_bytes > 0; i++) {
1295  int j;
1296 
1297  if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
1298  SAMPLES_NEEDED_2("too many packet bytes");
1299  return;
1300  }
1301 
1302  q->sub_packet_list_A[i].next = NULL;
1303 
1304  if (i > 0) {
1305  q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1306 
1307  /* seek to next block */
1308  init_get_bits(&gb, header.data, header.size * 8);
1309  skip_bits(&gb, next_index * 8);
1310 
1311  if (next_index >= header.size)
1312  break;
1313  }
1314 
1315  /* decode subpacket */
1316  packet = &q->sub_packets[i];
1317  qdm2_decode_sub_packet_header(&gb, packet);
1318  next_index = packet->size + get_bits_count(&gb) / 8;
1319  sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1320 
1321  if (packet->type == 0)
1322  break;
1323 
1324  if (sub_packet_size > packet_bytes) {
1325  if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1326  break;
1327  packet->size += packet_bytes - sub_packet_size;
1328  }
1329 
1330  packet_bytes -= sub_packet_size;
1331 
1332  /* add subpacket to 'all subpackets' list */
1333  q->sub_packet_list_A[i].packet = packet;
1334 
1335  /* add subpacket to related list */
1336  if (packet->type == 8) {
1337  SAMPLES_NEEDED_2("packet type 8");
1338  return;
1339  } else if (packet->type >= 9 && packet->type <= 12) {
1340  /* packets for MPEG Audio like Synthesis Filter */
1341  QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1342  } else if (packet->type == 13) {
1343  for (j = 0; j < 6; j++)
1344  q->fft_level_exp[j] = get_bits(&gb, 6);
1345  } else if (packet->type == 14) {
1346  for (j = 0; j < 6; j++)
1347  q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
1348  } else if (packet->type == 15) {
1349  SAMPLES_NEEDED_2("packet type 15")
1350  return;
1351  } else if (packet->type >= 16 && packet->type < 48 &&
1352  !fft_subpackets[packet->type - 16]) {
1353  /* packets for FFT */
1355  }
1356  } // Packet bytes loop
1357 
1358  if (q->sub_packet_list_D[0].packet != NULL) {
1360  q->do_synth_filter = 1;
1361  } else if (q->do_synth_filter) {
1365  }
1366 }
1367 
1368 static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
1369  int offset, int duration, int channel,
1370  int exp, int phase)
1371 {
1372  if (q->fft_coefs_min_index[duration] < 0)
1374 
1376  ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1377  q->fft_coefs[q->fft_coefs_index].channel = channel;
1378  q->fft_coefs[q->fft_coefs_index].offset = offset;
1379  q->fft_coefs[q->fft_coefs_index].exp = exp;
1380  q->fft_coefs[q->fft_coefs_index].phase = phase;
1381  q->fft_coefs_index++;
1382 }
1383 
1385  GetBitContext *gb, int b)
1386 {
1387  int channel, stereo, phase, exp;
1388  int local_int_4, local_int_8, stereo_phase, local_int_10;
1389  int local_int_14, stereo_exp, local_int_20, local_int_28;
1390  int n, offset;
1391 
1392  local_int_4 = 0;
1393  local_int_28 = 0;
1394  local_int_20 = 2;
1395  local_int_8 = (4 - duration);
1396  local_int_10 = 1 << (q->group_order - duration - 1);
1397  offset = 1;
1398 
1399  while (1) {
1400  if (q->superblocktype_2_3) {
1401  while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1402  offset = 1;
1403  if (n == 0) {
1404  local_int_4 += local_int_10;
1405  local_int_28 += (1 << local_int_8);
1406  } else {
1407  local_int_4 += 8 * local_int_10;
1408  local_int_28 += (8 << local_int_8);
1409  }
1410  }
1411  offset += (n - 2);
1412  } else {
1413  offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1414  while (offset >= (local_int_10 - 1)) {
1415  offset += (1 - (local_int_10 - 1));
1416  local_int_4 += local_int_10;
1417  local_int_28 += (1 << local_int_8);
1418  }
1419  }
1420 
1421  if (local_int_4 >= q->group_size)
1422  return;
1423 
1424  local_int_14 = (offset >> local_int_8);
1425  if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
1426  return;
1427 
1428  if (q->nb_channels > 1) {
1429  channel = get_bits1(gb);
1430  stereo = get_bits1(gb);
1431  } else {
1432  channel = 0;
1433  stereo = 0;
1434  }
1435 
1436  exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1437  exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1438  exp = (exp < 0) ? 0 : exp;
1439 
1440  phase = get_bits(gb, 3);
1441  stereo_exp = 0;
1442  stereo_phase = 0;
1443 
1444  if (stereo) {
1445  stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1446  stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1447  if (stereo_phase < 0)
1448  stereo_phase += 8;
1449  }
1450 
1451  if (q->frequency_range > (local_int_14 + 1)) {
1452  int sub_packet = (local_int_20 + local_int_28);
1453 
1454  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1455  channel, exp, phase);
1456  if (stereo)
1457  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1458  1 - channel,
1459  stereo_exp, stereo_phase);
1460  }
1461  offset++;
1462  }
1463 }
1464 
1466 {
1467  int i, j, min, max, value, type, unknown_flag;
1468  GetBitContext gb;
1469 
1470  if (q->sub_packet_list_B[0].packet == NULL)
1471  return;
1472 
1473  /* reset minimum indexes for FFT coefficients */
1474  q->fft_coefs_index = 0;
1475  for (i = 0; i < 5; i++)
1476  q->fft_coefs_min_index[i] = -1;
1477 
1478  /* process subpackets ordered by type, largest type first */
1479  for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1480  QDM2SubPacket *packet = NULL;
1481 
1482  /* find subpacket with largest type less than max */
1483  for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1484  value = q->sub_packet_list_B[j].packet->type;
1485  if (value > min && value < max) {
1486  min = value;
1487  packet = q->sub_packet_list_B[j].packet;
1488  }
1489  }
1490 
1491  max = min;
1492 
1493  /* check for errors (?) */
1494  if (!packet)
1495  return;
1496 
1497  if (i == 0 &&
1498  (packet->type < 16 || packet->type >= 48 ||
1499  fft_subpackets[packet->type - 16]))
1500  return;
1501 
1502  /* decode FFT tones */
1503  init_get_bits(&gb, packet->data, packet->size * 8);
1504 
1505  if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1506  unknown_flag = 1;
1507  else
1508  unknown_flag = 0;
1509 
1510  type = packet->type;
1511 
1512  if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1513  int duration = q->sub_sampling + 5 - (type & 15);
1514 
1515  if (duration >= 0 && duration < 4)
1516  qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
1517  } else if (type == 31) {
1518  for (j = 0; j < 4; j++)
1519  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1520  } else if (type == 46) {
1521  for (j = 0; j < 6; j++)
1522  q->fft_level_exp[j] = get_bits(&gb, 6);
1523  for (j = 0; j < 4; j++)
1524  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1525  }
1526  } // Loop on B packets
1527 
1528  /* calculate maximum indexes for FFT coefficients */
1529  for (i = 0, j = -1; i < 5; i++)
1530  if (q->fft_coefs_min_index[i] >= 0) {
1531  if (j >= 0)
1533  j = i;
1534  }
1535  if (j >= 0)
1537 }
1538 
1540 {
1541  float level, f[6];
1542  int i;
1543  QDM2Complex c;
1544  const double iscale = 2.0 * M_PI / 512.0;
1545 
1546  tone->phase += tone->phase_shift;
1547 
1548  /* calculate current level (maximum amplitude) of tone */
1549  level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1550  c.im = level * sin(tone->phase * iscale);
1551  c.re = level * cos(tone->phase * iscale);
1552 
1553  /* generate FFT coefficients for tone */
1554  if (tone->duration >= 3 || tone->cutoff >= 3) {
1555  tone->complex[0].im += c.im;
1556  tone->complex[0].re += c.re;
1557  tone->complex[1].im -= c.im;
1558  tone->complex[1].re -= c.re;
1559  } else {
1560  f[1] = -tone->table[4];
1561  f[0] = tone->table[3] - tone->table[0];
1562  f[2] = 1.0 - tone->table[2] - tone->table[3];
1563  f[3] = tone->table[1] + tone->table[4] - 1.0;
1564  f[4] = tone->table[0] - tone->table[1];
1565  f[5] = tone->table[2];
1566  for (i = 0; i < 2; i++) {
1567  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
1568  c.re * f[i];
1569  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
1570  c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
1571  }
1572  for (i = 0; i < 4; i++) {
1573  tone->complex[i].re += c.re * f[i + 2];
1574  tone->complex[i].im += c.im * f[i + 2];
1575  }
1576  }
1577 
1578  /* copy the tone if it has not yet died out */
1579  if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1580  memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1581  q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1582  }
1583 }
1584 
1585 static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
1586 {
1587  int i, j, ch;
1588  const double iscale = 0.25 * M_PI;
1589 
1590  for (ch = 0; ch < q->channels; ch++) {
1591  memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
1592  }
1593 
1594 
1595  /* apply FFT tones with duration 4 (1 FFT period) */
1596  if (q->fft_coefs_min_index[4] >= 0)
1597  for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1598  float level;
1599  QDM2Complex c;
1600 
1601  if (q->fft_coefs[i].sub_packet != sub_packet)
1602  break;
1603 
1604  ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1605  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];
1606 
1607  c.re = level * cos(q->fft_coefs[i].phase * iscale);
1608  c.im = level * sin(q->fft_coefs[i].phase * iscale);
1609  q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1610  q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1611  q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1612  q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1613  }
1614 
1615  /* generate existing FFT tones */
1616  for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1618  q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1619  }
1620 
1621  /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1622  for (i = 0; i < 4; i++)
1623  if (q->fft_coefs_min_index[i] >= 0) {
1624  for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1625  int offset, four_i;
1626  FFTTone tone;
1627 
1628  if (q->fft_coefs[j].sub_packet != sub_packet)
1629  break;
1630 
1631  four_i = (4 - i);
1632  offset = q->fft_coefs[j].offset >> four_i;
1633  ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1634 
1635  if (offset < q->frequency_range) {
1636  if (offset < 2)
1637  tone.cutoff = offset;
1638  else
1639  tone.cutoff = (offset >= 60) ? 3 : 2;
1640 
1641  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];
1642  tone.complex = &q->fft.complex[ch][offset];
1643  tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1644  tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1645  tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1646  tone.duration = i;
1647  tone.time_index = 0;
1648 
1649  qdm2_fft_generate_tone(q, &tone);
1650  }
1651  }
1652  q->fft_coefs_min_index[i] = j;
1653  }
1654 }
1655 
1656 static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
1657 {
1658  const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1659  float *out = q->output_buffer + channel;
1660  int i;
1661  q->fft.complex[channel][0].re *= 2.0f;
1662  q->fft.complex[channel][0].im = 0.0f;
1663  q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
1664  /* add samples to output buffer */
1665  for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
1666  out[0] += q->fft.complex[channel][i].re * gain;
1667  out[q->channels] += q->fft.complex[channel][i].im * gain;
1668  out += 2 * q->channels;
1669  }
1670 }
1671 
1677 {
1678  int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1679 
1680  /* copy sb_samples */
1681  sb_used = QDM2_SB_USED(q->sub_sampling);
1682 
1683  for (ch = 0; ch < q->channels; ch++)
1684  for (i = 0; i < 8; i++)
1685  for (k = sb_used; k < SBLIMIT; k++)
1686  q->sb_samples[ch][(8 * index) + i][k] = 0;
1687 
1688  for (ch = 0; ch < q->nb_channels; ch++) {
1689  float *samples_ptr = q->samples + ch;
1690 
1691  for (i = 0; i < 8; i++) {
1693  q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1694  ff_mpa_synth_window_float, &dither_state,
1695  samples_ptr, q->nb_channels,
1696  q->sb_samples[ch][(8 * index) + i]);
1697  samples_ptr += 32 * q->nb_channels;
1698  }
1699  }
1700 
1701  /* add samples to output buffer */
1702  sub_sampling = (4 >> q->sub_sampling);
1703 
1704  for (ch = 0; ch < q->channels; ch++)
1705  for (i = 0; i < q->frame_size; i++)
1706  q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
1707 }
1708 
1715  qdm2_init_vlc();
1718  rnd_table_init();
1720 }
1721 
1726 {
1727  QDM2Context *s = avctx->priv_data;
1728  uint8_t *extradata;
1729  int extradata_size;
1730  int tmp_val, tmp, size;
1731 
1732  /* extradata parsing
1733 
1734  Structure:
1735  wave {
1736  frma (QDM2)
1737  QDCA
1738  QDCP
1739  }
1740 
1741  32 size (including this field)
1742  32 tag (=frma)
1743  32 type (=QDM2 or QDMC)
1744 
1745  32 size (including this field, in bytes)
1746  32 tag (=QDCA) // maybe mandatory parameters
1747  32 unknown (=1)
1748  32 channels (=2)
1749  32 samplerate (=44100)
1750  32 bitrate (=96000)
1751  32 block size (=4096)
1752  32 frame size (=256) (for one channel)
1753  32 packet size (=1300)
1754 
1755  32 size (including this field, in bytes)
1756  32 tag (=QDCP) // maybe some tuneable parameters
1757  32 float1 (=1.0)
1758  32 zero ?
1759  32 float2 (=1.0)
1760  32 float3 (=1.0)
1761  32 unknown (27)
1762  32 unknown (8)
1763  32 zero ?
1764  */
1765 
1766  if (!avctx->extradata || (avctx->extradata_size < 48)) {
1767  av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1768  return -1;
1769  }
1770 
1771  extradata = avctx->extradata;
1772  extradata_size = avctx->extradata_size;
1773 
1774  while (extradata_size > 7) {
1775  if (!memcmp(extradata, "frmaQDM", 7))
1776  break;
1777  extradata++;
1778  extradata_size--;
1779  }
1780 
1781  if (extradata_size < 12) {
1782  av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1783  extradata_size);
1784  return -1;
1785  }
1786 
1787  if (memcmp(extradata, "frmaQDM", 7)) {
1788  av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
1789  return -1;
1790  }
1791 
1792  if (extradata[7] == 'C') {
1793 // s->is_qdmc = 1;
1794  av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");
1795  return -1;
1796  }
1797 
1798  extradata += 8;
1799  extradata_size -= 8;
1800 
1801  size = AV_RB32(extradata);
1802 
1803  if(size > extradata_size){
1804  av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1805  extradata_size, size);
1806  return -1;
1807  }
1808 
1809  extradata += 4;
1810  av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1811  if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
1812  av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1813  return -1;
1814  }
1815 
1816  extradata += 8;
1817 
1818  avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
1819  extradata += 4;
1820  if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS)
1821  return AVERROR_INVALIDDATA;
1822  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
1824 
1825  avctx->sample_rate = AV_RB32(extradata);
1826  extradata += 4;
1827 
1828  avctx->bit_rate = AV_RB32(extradata);
1829  extradata += 4;
1830 
1831  s->group_size = AV_RB32(extradata);
1832  extradata += 4;
1833 
1834  s->fft_size = AV_RB32(extradata);
1835  extradata += 4;
1836 
1837  s->checksum_size = AV_RB32(extradata);
1838  if (s->checksum_size >= 1U << 28) {
1839  av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
1840  return AVERROR_INVALIDDATA;
1841  }
1842 
1843  s->fft_order = av_log2(s->fft_size) + 1;
1844 
1845  // something like max decodable tones
1846  s->group_order = av_log2(s->group_size) + 1;
1847  s->frame_size = s->group_size / 16; // 16 iterations per super block
1849  return AVERROR_INVALIDDATA;
1850 
1851  s->sub_sampling = s->fft_order - 7;
1852  s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1853 
1854  switch ((s->sub_sampling * 2 + s->channels - 1)) {
1855  case 0: tmp = 40; break;
1856  case 1: tmp = 48; break;
1857  case 2: tmp = 56; break;
1858  case 3: tmp = 72; break;
1859  case 4: tmp = 80; break;
1860  case 5: tmp = 100;break;
1861  default: tmp=s->sub_sampling; break;
1862  }
1863  tmp_val = 0;
1864  if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1865  if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1866  if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1867  if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1868  s->cm_table_select = tmp_val;
1869 
1870  if (s->sub_sampling == 0)
1871  tmp = 7999;
1872  else
1873  tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
1874  /*
1875  0: 7999 -> 0
1876  1: 20000 -> 2
1877  2: 28000 -> 2
1878  */
1879  if (tmp < 8000)
1880  s->coeff_per_sb_select = 0;
1881  else if (tmp <= 16000)
1882  s->coeff_per_sb_select = 1;
1883  else
1884  s->coeff_per_sb_select = 2;
1885 
1886  // Fail on unknown fft order
1887  if ((s->fft_order < 7) || (s->fft_order > 9)) {
1888  av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
1889  return -1;
1890  }
1891  if (s->fft_size != (1 << (s->fft_order - 1))) {
1892  av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
1893  return AVERROR_INVALIDDATA;
1894  }
1895 
1897  ff_mpadsp_init(&s->mpadsp);
1898 
1899  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1900 
1902  avctx->coded_frame = &s->frame;
1903 
1904  return 0;
1905 }
1906 
1908 {
1909  QDM2Context *s = avctx->priv_data;
1910 
1911  ff_rdft_end(&s->rdft_ctx);
1912 
1913  return 0;
1914 }
1915 
1916 static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
1917 {
1918  int ch, i;
1919  const int frame_size = (q->frame_size * q->channels);
1920 
1921  /* select input buffer */
1922  q->compressed_data = in;
1924 
1925  /* copy old block, clear new block of output samples */
1926  memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1927  memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1928 
1929  /* decode block of QDM2 compressed data */
1930  if (q->sub_packet == 0) {
1931  q->has_errors = 0; // zero it for a new super block
1932  av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1934  }
1935 
1936  /* parse subpackets */
1937  if (!q->has_errors) {
1938  if (q->sub_packet == 2)
1940 
1942  }
1943 
1944  /* sound synthesis stage 1 (FFT) */
1945  for (ch = 0; ch < q->channels; ch++) {
1946  qdm2_calculate_fft(q, ch, q->sub_packet);
1947 
1948  if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) {
1949  SAMPLES_NEEDED_2("has errors, and C list is not empty")
1950  return -1;
1951  }
1952  }
1953 
1954  /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1955  if (!q->has_errors && q->do_synth_filter)
1957 
1958  q->sub_packet = (q->sub_packet + 1) % 16;
1959 
1960  /* clip and convert output float[] to 16bit signed samples */
1961  for (i = 0; i < frame_size; i++) {
1962  int value = (int)q->output_buffer[i];
1963 
1964  if (value > SOFTCLIP_THRESHOLD)
1965  value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
1966  else if (value < -SOFTCLIP_THRESHOLD)
1967  value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
1968 
1969  out[i] = value;
1970  }
1971 
1972  return 0;
1973 }
1974 
1975 static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
1976  int *got_frame_ptr, AVPacket *avpkt)
1977 {
1978  const uint8_t *buf = avpkt->data;
1979  int buf_size = avpkt->size;
1980  QDM2Context *s = avctx->priv_data;
1981  int16_t *out;
1982  int i, ret;
1983 
1984  if(!buf)
1985  return 0;
1986  if(buf_size < s->checksum_size)
1987  return -1;
1988 
1989  /* get output buffer */
1990  s->frame.nb_samples = 16 * s->frame_size;
1991  if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
1992  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1993  return ret;
1994  }
1995  out = (int16_t *)s->frame.data[0];
1996 
1997  for (i = 0; i < 16; i++) {
1998  if (qdm2_decode(s, buf, out) < 0)
1999  return -1;
2000  out += s->channels * s->frame_size;
2001  }
2002 
2003  *got_frame_ptr = 1;
2004  *(AVFrame *)data = s->frame;
2005 
2006  return s->checksum_size;
2007 }
2008 
2010  .name = "qdm2",
2011  .type = AVMEDIA_TYPE_AUDIO,
2012  .id = AV_CODEC_ID_QDM2,
2013  .priv_data_size = sizeof(QDM2Context),
2015  .init_static_data = qdm2_init_static_data,
2018  .capabilities = CODEC_CAP_DR1,
2019  .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
2020 };
static av_cold void qdm2_init_static_data(AVCodec *codec)
Init static data (does not depend on specific file)
Definition: qdm2.c:1714
Various QDM2 tables.
av_cold void ff_rdft_end(RDFTContext *s)
Definition: rdft.c:130
static const uint16_t vlc_tab_tone_level_idx_mid_huffcodes[24]
Definition: qdm2data.h:84
AVFrame frame
Definition: qdm2.c:133
static int16_t * samples
#define SBLIMIT
Definition: mpegaudio.h:43
int size
FFTTone fft_tones[1000]
FFT and tones.
Definition: qdm2.c:160
static const uint8_t vlc_tab_level_huffbits[24]
Definition: qdm2data.h:44
A node in the subpacket list.
Definition: qdm2.c:96
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
QDM2FFT fft
Definition: qdm2.c:169
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:531
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
static const float fft_tone_level_table[2][64]
Definition: qdm2data.h:438
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
static void average_quantized_coeffs(QDM2Context *q)
Replace 8 elements with their average value.
Definition: qdm2.c:475
Subpacket.
Definition: qdm2.c:87
int acc
Definition: yuv2rgb.c:476
int fft_coefs_index
Definition: qdm2.c:164
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:812
#define QDM2_MAX_FRAME_SIZE
Definition: qdm2.c:80
float synth_buf[MPA_MAX_CHANNELS][512 *2]
Definition: qdm2.c:178
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:916
const uint8_t * buffer
Definition: get_bits.h:53
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:187
static const uint8_t fft_stereo_phase_huffbits[9]
Definition: qdm2data.h:230
const float * table
Definition: qdm2.c:109
int coeff_per_sb_select
selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
Definition: qdm2.c:148
static av_cold int qdm2_decode_close(AVCodecContext *avctx)
Definition: qdm2.c:1907
#define VLC_TYPE
Definition: get_bits.h:61
short cutoff
Definition: qdm2.c:114
unsigned int size
subpacket size
Definition: qdm2.c:89
int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26]
Definition: qdm2.c:190
int sub_packet
Definition: qdm2.c:199
uint8_t run
Definition: svq3.c:132
float sb_samples[MPA_MAX_CHANNELS][128][SBLIMIT]
Definition: qdm2.c:180
#define AV_CH_LAYOUT_STEREO
int frequency_range
Definition: qdm2.c:146
signed 16 bits
Definition: samplefmt.h:52
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:423
AVCodec.
Definition: avcodec.h:2960
static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet, int offset, int duration, int channel, int exp, int phase)
Definition: qdm2.c:1368
QDM2SubPNode sub_packet_list_C[16]
packets with errors?
Definition: qdm2.c:156
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:458
static int64_t duration
Definition: avplay.c:249
float re
Definition: qdm2.c:102
static VLC vlc_tab_tone_level_idx_hi1
Definition: qdm2.c:211
int phase
Definition: qdm2.c:110
static VLC vlc_tab_type30
Definition: qdm2.c:214
static av_cold int qdm2_decode_init(AVCodecContext *avctx)
Init parameters from codec extradata.
Definition: qdm2.c:1725
QDM2 decoder context.
Definition: qdm2.c:132
static VLC vlc_tab_fft_tone_offset[5]
Definition: qdm2.c:216
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
int fft_order
order of FFT (actually fftorder+1)
Definition: qdm2.c:144
#define AV_RB32
Definition: intreadwrite.h:130
static void qdm2_decode_fft_packets(QDM2Context *q)
Definition: qdm2.c:1465
int sub_sampling
subsampling: 0=25%, 1=50%, 2=100% */
Definition: qdm2.c:147
#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:1454
static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
Definition: qdm2.c:1585
static const int16_t fft_level_index_table[256]
Definition: qdm2data.h:238
const char data[16]
Definition: mxf.c:66
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:915
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
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:140
static const uint16_t vlc_tab_fft_tone_offset_1_huffcodes[28]
Definition: qdm2data.h:135
static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 12.
Definition: qdm2.c:1193
static int qdm2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: qdm2.c:1975
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 int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
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:137
static void fill_tone_level_array(QDM2Context *q, int flag)
Related to synthesis filter Called by process_subpacket_10.
Definition: qdm2.c:597
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
static const uint8_t fft_stereo_exp_huffbits[7]
Definition: qdm2data.h:221
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:547
int synth_buf_offset[MPA_MAX_CHANNELS]
Definition: qdm2.c:179
static av_cold void rnd_table_init(void)
Definition: qdm2_tablegen.h:55
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:173
const uint8_t * data
pointer to subpacket data (points to input data buffer, it's not a private copy)
Definition: qdm2.c:90
int16_t offset
Definition: qdm2.c:120
float output_buffer[QDM2_MAX_FRAME_SIZE *2]
Definition: qdm2.c:174
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static const int switchtable[23]
Definition: qdm2.c:222
int group_size
size of frame group (16 frames per group)
Definition: qdm2.c:138
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 'B' list
Definition: qdm2.c:155
QDM2SubPNode sub_packet_list_A[16]
list of all packets
Definition: qdm2.c:153
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
int noise_idx
index for dithering noise table
Definition: qdm2.c:200
Definition: avfft.h:73
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static VLC fft_level_exp_alt_vlc
Definition: qdm2.c:207
uint8_t channel
Definition: qdm2.c:119
int duration
Definition: qdm2.c:112
Definition: qdm2.c:125
float tone_level[MPA_MAX_CHANNELS][30][64]
Mixed temporary data used in decoding.
Definition: qdm2.c:184
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:168
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
Definition: rdft.h:60
void ff_mpadsp_init(MPADSPContext *s)
Definition: mpegaudiodsp.c:26
int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8]
Definition: qdm2.c:186
static void qdm2_synthesis_filter(QDM2Context *q, int index)
Definition: qdm2.c:1676
static VLC vlc_tab_diff
Definition: qdm2.c:205
static VLC vlc_tab_level
Definition: qdm2.c:204
static void output_buffer(int16_t **samples, int nchan, int blocksize, int32_t **buffer)
Definition: shorten.c:268
#define QDM2_SB_USED(sub_sampling)
Definition: qdm2.c:66
#define MPA_MAX_CHANNELS
Definition: mpegaudio.h:41
int group_order
Parameters built from header parameters, do not change during playback.
Definition: qdm2.c:143
int bit_rate
the average bitrate
Definition: avcodec.h:1404
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:154
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:98
static const int8_t tone_level_idx_offset_table[30][4]
Definition: qdm2data.h:307
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static VLC vlc_tab_type34
Definition: qdm2.c:215
float ff_mpa_synth_window_float[]
static VLC fft_stereo_exp_vlc
Definition: qdm2.c:209
static void qdm2_decode_super_block(QDM2Context *q)
Definition: qdm2.c:1244
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:515
#define SAMPLES_NEEDED_2(why)
Definition: qdm2.c:77
static const int8_t coding_method_table[5][30]
Definition: qdm2data.h:342
static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
QDM2 checksum.
Definition: qdm2.c:407
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:55
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:213
static VLC fft_level_exp_vlc
Definition: qdm2.c:208
#define INIT_VLC_USE_NEW_STATIC
Definition: get_bits.h:433
int fft_tone_end
Definition: qdm2.c:162
static uint16_t softclip_table[HARDCLIP_THRESHOLD-SOFTCLIP_THRESHOLD+1]
Definition: qdm2_tablegen.h:39
QDM2Complex complex[MPA_MAX_CHANNELS][256]
Definition: qdm2.c:126
static const float type34_delta[10]
Definition: qdm2data.h:526
int bits
Definition: get_bits.h:64
static const float dequant_1bit[2][3]
Definition: qdm2data.h:516
static const uint8_t fft_stereo_exp_huffcodes[7]
Definition: qdm2data.h:217
int table_allocated
Definition: get_bits.h:66
static void build_sb_samples_from_noise(QDM2Context *q, int sb)
Build subband samples with noise weighted by q->tone_level.
Definition: qdm2.c:504
float samples[MPA_MAX_CHANNELS *MPA_FRAME_SIZE]
Definition: qdm2.c:181
NULL
Definition: eval.c:52
static const uint8_t last_coeff[3]
Definition: qdm2data.h:257
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:2104
#define SAMPLES_NEEDED
Definition: qdm2.c:74
static void qdm2_fft_decode_tones(QDM2Context *q, int duration, GetBitContext *gb, int b)
Definition: qdm2.c:1384
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:189
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
AVCodec ff_qdm2_decoder
Definition: qdm2.c:2009
uint8_t phase
Definition: qdm2.c:122
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:418
int fft_coefs_min_index[5]
Definition: qdm2.c:165
static void process_subpacket_9(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 9, init quantized_coeffs with data from it.
Definition: qdm2.c:1108
FFTCoefficient fft_coefs[1000]
Definition: qdm2.c:163
int extradata_size
Definition: avcodec.h:1455
#define INIT_VLC_LE
Definition: get_bits.h:432
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:604
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
int index
Definition: gxfenc.c:72
int has_errors
packet has errors
Definition: qdm2.c:195
static const uint8_t dequant_table[64]
Definition: 4xm.c:110
int fft_level_exp[6]
Definition: qdm2.c:167
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:372
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:686
int16_t sub_packet
Definition: qdm2.c:118
#define HARDCLIP_THRESHOLD
Definition: qdm2_tablegen.h:31
float im
Definition: qdm2.c:103
static VLC vlc_tab_run
Definition: qdm2.c:206
int16_t exp
Definition: qdm2.c:121
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:185
static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth)
Definition: qdm2.c:391
static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 10 if not null, else.
Definition: qdm2.c:1145
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:1008
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: avcodec.h:997
uint8_t level
Definition: svq3.c:133
int fft_size
size of FFT, in complex numbers
Definition: qdm2.c:139
static const uint8_t vlc_tab_fft_tone_offset_0_huffbits[23]
Definition: qdm2data.h:130
Definition: vf_drawbox.c:36
int type
subpacket type
Definition: qdm2.c:88
int fft_coefs_max_index[5]
Definition: qdm2.c:166
int frame_size
size of data frame
Definition: qdm2.c:145
static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
Definition: qdm2.c:1916
#define FIX_NOISE_IDX(noise_idx)
Definition: qdm2.c:68
static const float fft_tone_sample_table[4][16][5]
Definition: qdm2data.h:368
Definition: qdm2.c:106
int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8]
Definition: qdm2.c:188
int nb_channels
Parameters from codec header, do not change during playback.
Definition: qdm2.c:136
int superblocktype_2_3
select fft tables and some algorithm based on superblock type
Definition: qdm2.c:196
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:149
QDM2SubPacket * packet
packet
Definition: qdm2.c:97
QDM2SubPacket sub_packets[16]
Packets and packet lists.
Definition: qdm2.c:152
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:108
int do_synth_filter
used to perform or skip synthesis filter
Definition: qdm2.c:197
const uint8_t * compressed_data
I/O data.
Definition: qdm2.c:172
int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:192
DSP utils.
static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 11.
Definition: qdm2.c:1164
static int qdm2_get_vlc(GetBitContext *gb, VLC *vlc, int flag, int depth)
Definition: qdm2.c:369
MPADSPContext mpadsp
Synthesis filter.
Definition: qdm2.c:177
void * priv_data
Definition: avcodec.h:1382
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:1045
static VLC fft_stereo_phase_vlc
Definition: qdm2.c:210
int channels
number of audio channels
Definition: avcodec.h:2105
#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:1539
QDM2SubPNode sub_packet_list_D[16]
DCT packets.
Definition: qdm2.c:157
static VLC vlc_tab_tone_level_idx_mid
Definition: qdm2.c:212
int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:191
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
static const uint16_t qdm2_vlc_offs[]
Definition: qdm2.c:218
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
struct QDM2SubPNode QDM2SubPNode
A node in the subpacket list.
static av_cold void qdm2_init_vlc(void)
Definition: qdm2.c:226
short time_index
Definition: qdm2.c:113
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:74
int8_t sb_int8_array[2][30][64]
Definition: qdm2.c:82
#define SB_DITHERING_NOISE(sb, noise_idx)
Definition: qdm2.c:72
static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
Definition: qdm2.c:1656
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:111
static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
Definition: qdm2.c:1212
#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:898
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: avcodec.h:1042
for(j=16;j >0;--j)
float level
Definition: qdm2.c:107
int fft_tone_start
Definition: qdm2.c:161
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)