FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
9  *
10  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 /**
26  * @file
27  * QDM2 decoder
28  * @author Ewald Snel, Benjamin Larsson, Alex Beregszaszi, Roberto Togni
29  *
30  * The decoder is not perfect yet, there are still some distortions
31  * especially on files encoded with 16 or 8 subbands.
32  */
33 
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 "internal.h"
43 #include "rdft.h"
44 #include "mpegaudiodsp.h"
45 #include "mpegaudio.h"
46 
47 #include "qdm2data.h"
48 #include "qdm2_tablegen.h"
49 
50 #undef NDEBUG
51 #include <assert.h>
52 
53 
54 #define QDM2_LIST_ADD(list, size, packet) \
55 do { \
56  if (size > 0) { \
57  list[size - 1].next = &list[size]; \
58  } \
59  list[size].packet = packet; \
60  list[size].next = NULL; \
61  size++; \
62 } while(0)
63 
64 // Result is 8, 16 or 30
65 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
66 
67 #define FIX_NOISE_IDX(noise_idx) \
68  if ((noise_idx) >= 3840) \
69  (noise_idx) -= 3840; \
70 
71 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
72 
73 #define SAMPLES_NEEDED \
74  av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
75 
76 #define SAMPLES_NEEDED_2(why) \
77  av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
78 
79 #define QDM2_MAX_FRAME_SIZE 512
80 
81 typedef int8_t sb_int8_array[2][30][64];
82 
83 /**
84  * Subpacket
85  */
86 typedef struct {
87  int type; ///< subpacket type
88  unsigned int size; ///< subpacket size
89  const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
91 
92 /**
93  * A node in the subpacket list
94  */
95 typedef struct QDM2SubPNode {
96  QDM2SubPacket *packet; ///< packet
97  struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
98 } QDM2SubPNode;
99 
100 typedef struct {
101  float re;
102  float im;
103 } QDM2Complex;
104 
105 typedef struct {
106  float level;
108  const float *table;
109  int phase;
111  int duration;
112  short time_index;
113  short cutoff;
114 } FFTTone;
115 
116 typedef struct {
117  int16_t sub_packet;
119  int16_t offset;
120  int16_t exp;
123 
124 typedef struct {
126 } QDM2FFT;
127 
128 /**
129  * QDM2 decoder context
130  */
131 typedef struct {
132  /// Parameters from codec header, do not change during playback
133  int nb_channels; ///< number of channels
134  int channels; ///< number of channels
135  int group_size; ///< size of frame group (16 frames per group)
136  int fft_size; ///< size of FFT, in complex numbers
137  int checksum_size; ///< size of data block, used also for checksum
138 
139  /// Parameters built from header parameters, do not change during playback
140  int group_order; ///< order of frame group
141  int fft_order; ///< order of FFT (actually fftorder+1)
142  int frame_size; ///< size of data frame
144  int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */
145  int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
146  int cm_table_select; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
147 
148  /// Packets and packet lists
149  QDM2SubPacket sub_packets[16]; ///< the packets themselves
150  QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets
151  QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list
152  int sub_packets_B; ///< number of packets on 'B' list
153  QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors?
154  QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets
155 
156  /// FFT and tones
157  FFTTone fft_tones[1000];
160  FFTCoefficient fft_coefs[1000];
162  int fft_coefs_min_index[5];
163  int fft_coefs_max_index[5];
164  int fft_level_exp[6];
167 
168  /// I/O data
171  float output_buffer[QDM2_MAX_FRAME_SIZE * MPA_MAX_CHANNELS * 2];
172 
173  /// Synthesis filter
175  DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2];
176  int synth_buf_offset[MPA_MAX_CHANNELS];
177  DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
179 
180  /// Mixed temporary data used in decoding
181  float tone_level[MPA_MAX_CHANNELS][30][64];
182  int8_t coding_method[MPA_MAX_CHANNELS][30][64];
183  int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
184  int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
185  int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
186  int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
187  int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
188  int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
189  int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
190 
191  // Flags
192  int has_errors; ///< packet has errors
193  int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type
194  int do_synth_filter; ///< used to perform or skip synthesis filter
195 
197  int noise_idx; ///< index for dithering noise table
198 } QDM2Context;
199 
200 
214 
215 static const uint16_t qdm2_vlc_offs[] = {
216  0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
217 };
218 
219 static const int switchtable[23] = {
220  0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
221 };
222 
223 static av_cold void qdm2_init_vlc(void)
224 {
225  static VLC_TYPE qdm2_table[3838][2];
226 
227  vlc_tab_level.table = &qdm2_table[qdm2_vlc_offs[0]];
228  vlc_tab_level.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0];
229  init_vlc(&vlc_tab_level, 8, 24,
233 
234  vlc_tab_diff.table = &qdm2_table[qdm2_vlc_offs[1]];
235  vlc_tab_diff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1];
236  init_vlc(&vlc_tab_diff, 8, 37,
237  vlc_tab_diff_huffbits, 1, 1,
240 
241  vlc_tab_run.table = &qdm2_table[qdm2_vlc_offs[2]];
242  vlc_tab_run.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2];
243  init_vlc(&vlc_tab_run, 5, 6,
244  vlc_tab_run_huffbits, 1, 1,
245  vlc_tab_run_huffcodes, 1, 1,
247 
248  fft_level_exp_alt_vlc.table = &qdm2_table[qdm2_vlc_offs[3]];
249  fft_level_exp_alt_vlc.table_allocated = qdm2_vlc_offs[4] -
250  qdm2_vlc_offs[3];
251  init_vlc(&fft_level_exp_alt_vlc, 8, 28,
255 
256  fft_level_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[4]];
257  fft_level_exp_vlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
258  init_vlc(&fft_level_exp_vlc, 8, 20,
262 
263  fft_stereo_exp_vlc.table = &qdm2_table[qdm2_vlc_offs[5]];
264  fft_stereo_exp_vlc.table_allocated = qdm2_vlc_offs[6] -
265  qdm2_vlc_offs[5];
266  init_vlc(&fft_stereo_exp_vlc, 6, 7,
270 
271  fft_stereo_phase_vlc.table = &qdm2_table[qdm2_vlc_offs[6]];
272  fft_stereo_phase_vlc.table_allocated = qdm2_vlc_offs[7] -
273  qdm2_vlc_offs[6];
274  init_vlc(&fft_stereo_phase_vlc, 6, 9,
278 
279  vlc_tab_tone_level_idx_hi1.table =
280  &qdm2_table[qdm2_vlc_offs[7]];
281  vlc_tab_tone_level_idx_hi1.table_allocated = qdm2_vlc_offs[8] -
282  qdm2_vlc_offs[7];
283  init_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20,
287 
288  vlc_tab_tone_level_idx_mid.table =
289  &qdm2_table[qdm2_vlc_offs[8]];
290  vlc_tab_tone_level_idx_mid.table_allocated = qdm2_vlc_offs[9] -
291  qdm2_vlc_offs[8];
292  init_vlc(&vlc_tab_tone_level_idx_mid, 8, 24,
296 
297  vlc_tab_tone_level_idx_hi2.table =
298  &qdm2_table[qdm2_vlc_offs[9]];
299  vlc_tab_tone_level_idx_hi2.table_allocated = qdm2_vlc_offs[10] -
300  qdm2_vlc_offs[9];
301  init_vlc(&vlc_tab_tone_level_idx_hi2, 8, 24,
305 
306  vlc_tab_type30.table = &qdm2_table[qdm2_vlc_offs[10]];
307  vlc_tab_type30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
308  init_vlc(&vlc_tab_type30, 6, 9,
312 
313  vlc_tab_type34.table = &qdm2_table[qdm2_vlc_offs[11]];
314  vlc_tab_type34.table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
315  init_vlc(&vlc_tab_type34, 5, 10,
319 
320  vlc_tab_fft_tone_offset[0].table =
321  &qdm2_table[qdm2_vlc_offs[12]];
322  vlc_tab_fft_tone_offset[0].table_allocated = qdm2_vlc_offs[13] -
323  qdm2_vlc_offs[12];
324  init_vlc(&vlc_tab_fft_tone_offset[0], 8, 23,
328 
329  vlc_tab_fft_tone_offset[1].table =
330  &qdm2_table[qdm2_vlc_offs[13]];
331  vlc_tab_fft_tone_offset[1].table_allocated = qdm2_vlc_offs[14] -
332  qdm2_vlc_offs[13];
333  init_vlc(&vlc_tab_fft_tone_offset[1], 8, 28,
337 
338  vlc_tab_fft_tone_offset[2].table =
339  &qdm2_table[qdm2_vlc_offs[14]];
340  vlc_tab_fft_tone_offset[2].table_allocated = qdm2_vlc_offs[15] -
341  qdm2_vlc_offs[14];
342  init_vlc(&vlc_tab_fft_tone_offset[2], 8, 32,
346 
347  vlc_tab_fft_tone_offset[3].table =
348  &qdm2_table[qdm2_vlc_offs[15]];
349  vlc_tab_fft_tone_offset[3].table_allocated = qdm2_vlc_offs[16] -
350  qdm2_vlc_offs[15];
351  init_vlc(&vlc_tab_fft_tone_offset[3], 8, 35,
355 
356  vlc_tab_fft_tone_offset[4].table =
357  &qdm2_table[qdm2_vlc_offs[16]];
358  vlc_tab_fft_tone_offset[4].table_allocated = qdm2_vlc_offs[17] -
359  qdm2_vlc_offs[16];
360  init_vlc(&vlc_tab_fft_tone_offset[4], 8, 38,
364 }
365 
366 static int qdm2_get_vlc(GetBitContext *gb, VLC *vlc, int flag, int depth)
367 {
368  int value;
369 
370  value = get_vlc2(gb, vlc->table, vlc->bits, depth);
371 
372  /* stage-2, 3 bits exponent escape sequence */
373  if (value-- == 0)
374  value = get_bits(gb, get_bits(gb, 3) + 1);
375 
376  /* stage-3, optional */
377  if (flag) {
378  int tmp;
379 
380  if (value >= 60) {
381  av_log(NULL, AV_LOG_ERROR, "value %d in qdm2_get_vlc too large\n", value);
382  return 0;
383  }
384 
385  tmp= vlc_stage3_values[value];
386 
387  if ((value & ~3) > 0)
388  tmp += get_bits(gb, (value >> 2));
389  value = tmp;
390  }
391 
392  return value;
393 }
394 
395 static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth)
396 {
397  int value = qdm2_get_vlc(gb, vlc, 0, depth);
398 
399  return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
400 }
401 
402 /**
403  * QDM2 checksum
404  *
405  * @param data pointer to data to be checksum'ed
406  * @param length data length
407  * @param value checksum value
408  *
409  * @return 0 if checksum is OK
410  */
411 static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
412 {
413  int i;
414 
415  for (i = 0; i < length; i++)
416  value -= data[i];
417 
418  return (uint16_t)(value & 0xffff);
419 }
420 
421 /**
422  * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
423  *
424  * @param gb bitreader context
425  * @param sub_packet packet under analysis
426  */
428  QDM2SubPacket *sub_packet)
429 {
430  sub_packet->type = get_bits(gb, 8);
431 
432  if (sub_packet->type == 0) {
433  sub_packet->size = 0;
434  sub_packet->data = NULL;
435  } else {
436  sub_packet->size = get_bits(gb, 8);
437 
438  if (sub_packet->type & 0x80) {
439  sub_packet->size <<= 8;
440  sub_packet->size |= get_bits(gb, 8);
441  sub_packet->type &= 0x7f;
442  }
443 
444  if (sub_packet->type == 0x7f)
445  sub_packet->type |= (get_bits(gb, 8) << 8);
446 
447  // FIXME: this depends on bitreader-internal data
448  sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];
449  }
450 
451  av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n",
452  sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
453 }
454 
455 /**
456  * Return node pointer to first packet of requested type in list.
457  *
458  * @param list list of subpackets to be scanned
459  * @param type type of searched subpacket
460  * @return node pointer for subpacket if found, else NULL
461  */
463  int type)
464 {
465  while (list != NULL && list->packet != NULL) {
466  if (list->packet->type == type)
467  return list;
468  list = list->next;
469  }
470  return NULL;
471 }
472 
473 /**
474  * Replace 8 elements with their average value.
475  * Called by qdm2_decode_superblock before starting subblock decoding.
476  *
477  * @param q context
478  */
480 {
481  int i, j, n, ch, sum;
482 
484 
485  for (ch = 0; ch < q->nb_channels; ch++)
486  for (i = 0; i < n; i++) {
487  sum = 0;
488 
489  for (j = 0; j < 8; j++)
490  sum += q->quantized_coeffs[ch][i][j];
491 
492  sum /= 8;
493  if (sum > 0)
494  sum--;
495 
496  for (j = 0; j < 8; j++)
497  q->quantized_coeffs[ch][i][j] = sum;
498  }
499 }
500 
501 /**
502  * Build subband samples with noise weighted by q->tone_level.
503  * Called by synthfilt_build_sb_samples.
504  *
505  * @param q context
506  * @param sb subband index
507  */
509 {
510  int ch, j;
511 
513 
514  if (!q->nb_channels)
515  return;
516 
517  for (ch = 0; ch < q->nb_channels; ch++) {
518  for (j = 0; j < 64; j++) {
519  q->sb_samples[ch][j * 2][sb] =
520  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
521  q->sb_samples[ch][j * 2 + 1][sb] =
522  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
523  }
524  }
525 }
526 
527 /**
528  * Called while processing data from subpackets 11 and 12.
529  * Used after making changes to coding_method array.
530  *
531  * @param sb subband index
532  * @param channels number of channels
533  * @param coding_method q->coding_method[0][0][0]
534  */
535 static void fix_coding_method_array(int sb, int channels,
536  sb_int8_array coding_method)
537 {
538  int j, k;
539  int ch;
540  int run, case_val;
541 
542  for (ch = 0; ch < channels; ch++) {
543  for (j = 0; j < 64; ) {
544  if ((coding_method[ch][sb][j] - 8) > 22) {
545  run = 1;
546  case_val = 8;
547  } else {
548  switch (switchtable[coding_method[ch][sb][j] - 8]) {
549  case 0: run = 10;
550  case_val = 10;
551  break;
552  case 1: run = 1;
553  case_val = 16;
554  break;
555  case 2: run = 5;
556  case_val = 24;
557  break;
558  case 3: run = 3;
559  case_val = 30;
560  break;
561  case 4: run = 1;
562  case_val = 30;
563  break;
564  case 5: run = 1;
565  case_val = 8;
566  break;
567  default: run = 1;
568  case_val = 8;
569  break;
570  }
571  }
572  for (k = 0; k < run; k++) {
573  if (j + k < 128) {
574  if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) {
575  if (k > 0) {
577  //not debugged, almost never used
578  memset(&coding_method[ch][sb][j + k], case_val,
579  k *sizeof(int8_t));
580  memset(&coding_method[ch][sb][j + k], case_val,
581  3 * sizeof(int8_t));
582  }
583  }
584  }
585  }
586  j += run;
587  }
588  }
589 }
590 
591 /**
592  * Related to synthesis filter
593  * Called by process_subpacket_10
594  *
595  * @param q context
596  * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
597  */
598 static void fill_tone_level_array(QDM2Context *q, int flag)
599 {
600  int i, sb, ch, sb_used;
601  int tmp, tab;
602 
603  for (ch = 0; ch < q->nb_channels; ch++)
604  for (sb = 0; sb < 30; sb++)
605  for (i = 0; i < 8; i++) {
607  tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
609  else
610  tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
611  if(tmp < 0)
612  tmp += 0xff;
613  q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
614  }
615 
616  sb_used = QDM2_SB_USED(q->sub_sampling);
617 
618  if ((q->superblocktype_2_3 != 0) && !flag) {
619  for (sb = 0; sb < sb_used; sb++)
620  for (ch = 0; ch < q->nb_channels; ch++)
621  for (i = 0; i < 64; i++) {
622  q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
623  if (q->tone_level_idx[ch][sb][i] < 0)
624  q->tone_level[ch][sb][i] = 0;
625  else
626  q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
627  }
628  } else {
629  tab = q->superblocktype_2_3 ? 0 : 1;
630  for (sb = 0; sb < sb_used; sb++) {
631  if ((sb >= 4) && (sb <= 23)) {
632  for (ch = 0; ch < q->nb_channels; ch++)
633  for (i = 0; i < 64; i++) {
634  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
635  q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
636  q->tone_level_idx_mid[ch][sb - 4][i / 8] -
637  q->tone_level_idx_hi2[ch][sb - 4];
638  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
639  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
640  q->tone_level[ch][sb][i] = 0;
641  else
642  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
643  }
644  } else {
645  if (sb > 4) {
646  for (ch = 0; ch < q->nb_channels; ch++)
647  for (i = 0; i < 64; i++) {
648  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
649  q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
650  q->tone_level_idx_hi2[ch][sb - 4];
651  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
652  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
653  q->tone_level[ch][sb][i] = 0;
654  else
655  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
656  }
657  } else {
658  for (ch = 0; ch < q->nb_channels; ch++)
659  for (i = 0; i < 64; i++) {
660  tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
661  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
662  q->tone_level[ch][sb][i] = 0;
663  else
664  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
665  }
666  }
667  }
668  }
669  }
670 }
671 
672 /**
673  * Related to synthesis filter
674  * Called by process_subpacket_11
675  * c is built with data from subpacket 11
676  * Most of this function is used only if superblock_type_2_3 == 0,
677  * never seen it in samples.
678  *
679  * @param tone_level_idx
680  * @param tone_level_idx_temp
681  * @param coding_method q->coding_method[0][0][0]
682  * @param nb_channels number of channels
683  * @param c coming from subpacket 11, passed as 8*c
684  * @param superblocktype_2_3 flag based on superblock packet type
685  * @param cm_table_select q->cm_table_select
686  */
687 static void fill_coding_method_array(sb_int8_array tone_level_idx,
688  sb_int8_array tone_level_idx_temp,
689  sb_int8_array coding_method,
690  int nb_channels,
691  int c, int superblocktype_2_3,
692  int cm_table_select)
693 {
694  int ch, sb, j;
695  int tmp, acc, esp_40, comp;
696  int add1, add2, add3, add4;
697  int64_t multres;
698 
699  if (!superblocktype_2_3) {
700  /* This case is untested, no samples available */
701  avpriv_request_sample(NULL, "!superblocktype_2_3");
702  return;
703  for (ch = 0; ch < nb_channels; ch++)
704  for (sb = 0; sb < 30; sb++) {
705  for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
706  add1 = tone_level_idx[ch][sb][j] - 10;
707  if (add1 < 0)
708  add1 = 0;
709  add2 = add3 = add4 = 0;
710  if (sb > 1) {
711  add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
712  if (add2 < 0)
713  add2 = 0;
714  }
715  if (sb > 0) {
716  add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
717  if (add3 < 0)
718  add3 = 0;
719  }
720  if (sb < 29) {
721  add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
722  if (add4 < 0)
723  add4 = 0;
724  }
725  tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
726  if (tmp < 0)
727  tmp = 0;
728  tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
729  }
730  tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
731  }
732  acc = 0;
733  for (ch = 0; ch < nb_channels; ch++)
734  for (sb = 0; sb < 30; sb++)
735  for (j = 0; j < 64; j++)
736  acc += tone_level_idx_temp[ch][sb][j];
737 
738  multres = 0x66666667LL * (acc * 10);
739  esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
740  for (ch = 0; ch < nb_channels; ch++)
741  for (sb = 0; sb < 30; sb++)
742  for (j = 0; j < 64; j++) {
743  comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
744  if (comp < 0)
745  comp += 0xff;
746  comp /= 256; // signed shift
747  switch(sb) {
748  case 0:
749  if (comp < 30)
750  comp = 30;
751  comp += 15;
752  break;
753  case 1:
754  if (comp < 24)
755  comp = 24;
756  comp += 10;
757  break;
758  case 2:
759  case 3:
760  case 4:
761  if (comp < 16)
762  comp = 16;
763  }
764  if (comp <= 5)
765  tmp = 0;
766  else if (comp <= 10)
767  tmp = 10;
768  else if (comp <= 16)
769  tmp = 16;
770  else if (comp <= 24)
771  tmp = -1;
772  else
773  tmp = 0;
774  coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
775  }
776  for (sb = 0; sb < 30; sb++)
777  fix_coding_method_array(sb, nb_channels, coding_method);
778  for (ch = 0; ch < nb_channels; ch++)
779  for (sb = 0; sb < 30; sb++)
780  for (j = 0; j < 64; j++)
781  if (sb >= 10) {
782  if (coding_method[ch][sb][j] < 10)
783  coding_method[ch][sb][j] = 10;
784  } else {
785  if (sb >= 2) {
786  if (coding_method[ch][sb][j] < 16)
787  coding_method[ch][sb][j] = 16;
788  } else {
789  if (coding_method[ch][sb][j] < 30)
790  coding_method[ch][sb][j] = 30;
791  }
792  }
793  } else { // superblocktype_2_3 != 0
794  for (ch = 0; ch < nb_channels; ch++)
795  for (sb = 0; sb < 30; sb++)
796  for (j = 0; j < 64; j++)
797  coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
798  }
799 }
800 
801 /**
802  *
803  * Called by process_subpacket_11 to process more data from subpacket 11
804  * with sb 0-8.
805  * Called by process_subpacket_12 to process data from subpacket 12 with
806  * sb 8-sb_used.
807  *
808  * @param q context
809  * @param gb bitreader context
810  * @param length packet length in bits
811  * @param sb_min lower subband processed (sb_min included)
812  * @param sb_max higher subband processed (sb_max excluded)
813  */
815  int length, int sb_min, int sb_max)
816 {
817  int sb, j, k, n, ch, run, channels;
818  int joined_stereo, zero_encoding, chs;
819  int type34_first;
820  float type34_div = 0;
821  float type34_predictor;
822  float samples[10], sign_bits[16];
823 
824  if (length == 0) {
825  // If no data use noise
826  for (sb=sb_min; sb < sb_max; sb++)
828 
829  return 0;
830  }
831 
832  for (sb = sb_min; sb < sb_max; sb++) {
833  channels = q->nb_channels;
834 
835  if (q->nb_channels <= 1 || sb < 12)
836  joined_stereo = 0;
837  else if (sb >= 24)
838  joined_stereo = 1;
839  else
840  joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1 (gb) : 0;
841 
842  if (joined_stereo) {
843  if (get_bits_left(gb) >= 16)
844  for (j = 0; j < 16; j++)
845  sign_bits[j] = get_bits1 (gb);
846 
847  if (q->coding_method[0][sb][0] <= 0) {
848  av_log(NULL, AV_LOG_ERROR, "coding method invalid\n");
849  return AVERROR_INVALIDDATA;
850  }
851 
852  for (j = 0; j < 64; j++)
853  if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
854  q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
855 
857  channels = 1;
858  }
859 
860  for (ch = 0; ch < channels; ch++) {
862  zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
863  type34_predictor = 0.0;
864  type34_first = 1;
865 
866  for (j = 0; j < 128; ) {
867  switch (q->coding_method[ch][sb][j / 2]) {
868  case 8:
869  if (get_bits_left(gb) >= 10) {
870  if (zero_encoding) {
871  for (k = 0; k < 5; k++) {
872  if ((j + 2 * k) >= 128)
873  break;
874  samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
875  }
876  } else {
877  n = get_bits(gb, 8);
878  if (n >= 243) {
879  av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
880  return AVERROR_INVALIDDATA;
881  }
882 
883  for (k = 0; k < 5; k++)
884  samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
885  }
886  for (k = 0; k < 5; k++)
887  samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
888  } else {
889  for (k = 0; k < 10; k++)
890  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
891  }
892  run = 10;
893  break;
894 
895  case 10:
896  if (get_bits_left(gb) >= 1) {
897  float f = 0.81;
898 
899  if (get_bits1(gb))
900  f = -f;
901  f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
902  samples[0] = f;
903  } else {
904  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
905  }
906  run = 1;
907  break;
908 
909  case 16:
910  if (get_bits_left(gb) >= 10) {
911  if (zero_encoding) {
912  for (k = 0; k < 5; k++) {
913  if ((j + k) >= 128)
914  break;
915  samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
916  }
917  } else {
918  n = get_bits (gb, 8);
919  if (n >= 243) {
920  av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
921  return AVERROR_INVALIDDATA;
922  }
923 
924  for (k = 0; k < 5; k++)
925  samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
926  }
927  } else {
928  for (k = 0; k < 5; k++)
929  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
930  }
931  run = 5;
932  break;
933 
934  case 24:
935  if (get_bits_left(gb) >= 7) {
936  n = get_bits(gb, 7);
937  if (n >= 125) {
938  av_log(NULL, AV_LOG_ERROR, "Invalid 7bit codeword\n");
939  return AVERROR_INVALIDDATA;
940  }
941 
942  for (k = 0; k < 3; k++)
943  samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
944  } else {
945  for (k = 0; k < 3; k++)
946  samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
947  }
948  run = 3;
949  break;
950 
951  case 30:
952  if (get_bits_left(gb) >= 4) {
953  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
954  if (index >= FF_ARRAY_ELEMS(type30_dequant)) {
955  av_log(NULL, AV_LOG_ERROR, "index %d out of type30_dequant array\n", index);
956  return AVERROR_INVALIDDATA;
957  }
958  samples[0] = type30_dequant[index];
959  } else
960  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
961 
962  run = 1;
963  break;
964 
965  case 34:
966  if (get_bits_left(gb) >= 7) {
967  if (type34_first) {
968  type34_div = (float)(1 << get_bits(gb, 2));
969  samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
970  type34_predictor = samples[0];
971  type34_first = 0;
972  } else {
973  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
974  if (index >= FF_ARRAY_ELEMS(type34_delta)) {
975  av_log(NULL, AV_LOG_ERROR, "index %d out of type34_delta array\n", index);
976  return AVERROR_INVALIDDATA;
977  }
978  samples[0] = type34_delta[index] / type34_div + type34_predictor;
979  type34_predictor = samples[0];
980  }
981  } else {
982  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
983  }
984  run = 1;
985  break;
986 
987  default:
988  samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
989  run = 1;
990  break;
991  }
992 
993  if (joined_stereo) {
994  float tmp[10][MPA_MAX_CHANNELS];
995  for (k = 0; k < run; k++) {
996  tmp[k][0] = samples[k];
997  if ((j + k) < 128)
998  tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
999  }
1000  for (chs = 0; chs < q->nb_channels; chs++)
1001  for (k = 0; k < run; k++)
1002  if ((j + k) < 128)
1003  q->sb_samples[chs][j + k][sb] = q->tone_level[chs][sb][((j + k)/2)] * tmp[k][chs];
1004  } else {
1005  for (k = 0; k < run; k++)
1006  if ((j + k) < 128)
1007  q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
1008  }
1009 
1010  j += run;
1011  } // j loop
1012  } // channel loop
1013  } // subband loop
1014  return 0;
1015 }
1016 
1017 /**
1018  * Init the first element of a channel in quantized_coeffs with data
1019  * from packet 10 (quantized_coeffs[ch][0]).
1020  * This is similar to process_subpacket_9, but for a single channel
1021  * and for element [0]
1022  * same VLC tables as process_subpacket_9 are used.
1023  *
1024  * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
1025  * @param gb bitreader context
1026  */
1027 static int init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
1028  GetBitContext *gb)
1029 {
1030  int i, k, run, level, diff;
1031 
1032  if (get_bits_left(gb) < 16)
1033  return -1;
1034  level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
1035 
1036  quantized_coeffs[0] = level;
1037 
1038  for (i = 0; i < 7; ) {
1039  if (get_bits_left(gb) < 16)
1040  return -1;
1041  run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
1042 
1043  if (i + run >= 8)
1044  return -1;
1045 
1046  if (get_bits_left(gb) < 16)
1047  return -1;
1048  diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
1049 
1050  for (k = 1; k <= run; k++)
1051  quantized_coeffs[i + k] = (level + ((k * diff) / run));
1052 
1053  level += diff;
1054  i += run;
1055  }
1056  return 0;
1057 }
1058 
1059 /**
1060  * Related to synthesis filter, process data from packet 10
1061  * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
1062  * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with
1063  * data from packet 10
1064  *
1065  * @param q context
1066  * @param gb bitreader context
1067  */
1069 {
1070  int sb, j, k, n, ch;
1071 
1072  for (ch = 0; ch < q->nb_channels; ch++) {
1074 
1075  if (get_bits_left(gb) < 16) {
1076  memset(q->quantized_coeffs[ch][0], 0, 8);
1077  break;
1078  }
1079  }
1080 
1081  n = q->sub_sampling + 1;
1082 
1083  for (sb = 0; sb < n; sb++)
1084  for (ch = 0; ch < q->nb_channels; ch++)
1085  for (j = 0; j < 8; j++) {
1086  if (get_bits_left(gb) < 1)
1087  break;
1088  if (get_bits1(gb)) {
1089  for (k=0; k < 8; k++) {
1090  if (get_bits_left(gb) < 16)
1091  break;
1092  q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
1093  }
1094  } else {
1095  for (k=0; k < 8; k++)
1096  q->tone_level_idx_hi1[ch][sb][j][k] = 0;
1097  }
1098  }
1099 
1100  n = QDM2_SB_USED(q->sub_sampling) - 4;
1101 
1102  for (sb = 0; sb < n; sb++)
1103  for (ch = 0; ch < q->nb_channels; ch++) {
1104  if (get_bits_left(gb) < 16)
1105  break;
1106  q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
1107  if (sb > 19)
1108  q->tone_level_idx_hi2[ch][sb] -= 16;
1109  else
1110  for (j = 0; j < 8; j++)
1111  q->tone_level_idx_mid[ch][sb][j] = -16;
1112  }
1113 
1114  n = QDM2_SB_USED(q->sub_sampling) - 5;
1115 
1116  for (sb = 0; sb < n; sb++)
1117  for (ch = 0; ch < q->nb_channels; ch++)
1118  for (j = 0; j < 8; j++) {
1119  if (get_bits_left(gb) < 16)
1120  break;
1121  q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
1122  }
1123 }
1124 
1125 /**
1126  * Process subpacket 9, init quantized_coeffs with data from it
1127  *
1128  * @param q context
1129  * @param node pointer to node with packet
1130  */
1132 {
1133  GetBitContext gb;
1134  int i, j, k, n, ch, run, level, diff;
1135 
1136  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1137 
1139 
1140  for (i = 1; i < n; i++)
1141  for (ch = 0; ch < q->nb_channels; ch++) {
1142  level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
1143  q->quantized_coeffs[ch][i][0] = level;
1144 
1145  for (j = 0; j < (8 - 1); ) {
1146  run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
1147  diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
1148 
1149  if (j + run >= 8)
1150  return -1;
1151 
1152  for (k = 1; k <= run; k++)
1153  q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
1154 
1155  level += diff;
1156  j += run;
1157  }
1158  }
1159 
1160  for (ch = 0; ch < q->nb_channels; ch++)
1161  for (i = 0; i < 8; i++)
1162  q->quantized_coeffs[ch][0][i] = 0;
1163 
1164  return 0;
1165 }
1166 
1167 /**
1168  * Process subpacket 10 if not null, else
1169  *
1170  * @param q context
1171  * @param node pointer to node with packet
1172  */
1174 {
1175  GetBitContext gb;
1176 
1177  if (node) {
1178  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1180  fill_tone_level_array(q, 1);
1181  } else {
1182  fill_tone_level_array(q, 0);
1183  }
1184 }
1185 
1186 /**
1187  * Process subpacket 11
1188  *
1189  * @param q context
1190  * @param node pointer to node with packet
1191  */
1193 {
1194  GetBitContext gb;
1195  int length = 0;
1196 
1197  if (node) {
1198  length = node->packet->size * 8;
1199  init_get_bits(&gb, node->packet->data, length);
1200  }
1201 
1202  if (length >= 32) {
1203  int c = get_bits(&gb, 13);
1204 
1205  if (c > 3)
1208  q->nb_channels, 8 * c,
1210  }
1211 
1212  synthfilt_build_sb_samples(q, &gb, length, 0, 8);
1213 }
1214 
1215 /**
1216  * Process subpacket 12
1217  *
1218  * @param q context
1219  * @param node pointer to node with packet
1220  */
1222 {
1223  GetBitContext gb;
1224  int length = 0;
1225 
1226  if (node) {
1227  length = node->packet->size * 8;
1228  init_get_bits(&gb, node->packet->data, length);
1229  }
1230 
1231  synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
1232 }
1233 
1234 /**
1235  * Process new subpackets for synthesis filter
1236  *
1237  * @param q context
1238  * @param list list with synthesis filter packets (list D)
1239  */
1241 {
1242  QDM2SubPNode *nodes[4];
1243 
1244  nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
1245  if (nodes[0] != NULL)
1246  process_subpacket_9(q, nodes[0]);
1247 
1248  nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1249  if (nodes[1] != NULL)
1250  process_subpacket_10(q, nodes[1]);
1251  else
1252  process_subpacket_10(q, NULL);
1253 
1254  nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1255  if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
1256  process_subpacket_11(q, nodes[2]);
1257  else
1258  process_subpacket_11(q, NULL);
1259 
1260  nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1261  if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
1262  process_subpacket_12(q, nodes[3]);
1263  else
1264  process_subpacket_12(q, NULL);
1265 }
1266 
1267 /**
1268  * Decode superblock, fill packet lists.
1269  *
1270  * @param q context
1271  */
1273 {
1274  GetBitContext gb;
1275  QDM2SubPacket header, *packet;
1276  int i, packet_bytes, sub_packet_size, sub_packets_D;
1277  unsigned int next_index = 0;
1278 
1279  memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1280  memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1281  memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1282 
1283  q->sub_packets_B = 0;
1284  sub_packets_D = 0;
1285 
1286  average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1287 
1289  qdm2_decode_sub_packet_header(&gb, &header);
1290 
1291  if (header.type < 2 || header.type >= 8) {
1292  q->has_errors = 1;
1293  av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
1294  return;
1295  }
1296 
1297  q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1298  packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
1299 
1300  init_get_bits(&gb, header.data, header.size * 8);
1301 
1302  if (header.type == 2 || header.type == 4 || header.type == 5) {
1303  int csum = 257 * get_bits(&gb, 8);
1304  csum += 2 * get_bits(&gb, 8);
1305 
1306  csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1307 
1308  if (csum != 0) {
1309  q->has_errors = 1;
1310  av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
1311  return;
1312  }
1313  }
1314 
1315  q->sub_packet_list_B[0].packet = NULL;
1316  q->sub_packet_list_D[0].packet = NULL;
1317 
1318  for (i = 0; i < 6; i++)
1319  if (--q->fft_level_exp[i] < 0)
1320  q->fft_level_exp[i] = 0;
1321 
1322  for (i = 0; packet_bytes > 0; i++) {
1323  int j;
1324 
1325  if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
1326  SAMPLES_NEEDED_2("too many packet bytes");
1327  return;
1328  }
1329 
1330  q->sub_packet_list_A[i].next = NULL;
1331 
1332  if (i > 0) {
1333  q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1334 
1335  /* seek to next block */
1336  init_get_bits(&gb, header.data, header.size * 8);
1337  skip_bits(&gb, next_index * 8);
1338 
1339  if (next_index >= header.size)
1340  break;
1341  }
1342 
1343  /* decode subpacket */
1344  packet = &q->sub_packets[i];
1345  qdm2_decode_sub_packet_header(&gb, packet);
1346  next_index = packet->size + get_bits_count(&gb) / 8;
1347  sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1348 
1349  if (packet->type == 0)
1350  break;
1351 
1352  if (sub_packet_size > packet_bytes) {
1353  if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1354  break;
1355  packet->size += packet_bytes - sub_packet_size;
1356  }
1357 
1358  packet_bytes -= sub_packet_size;
1359 
1360  /* add subpacket to 'all subpackets' list */
1361  q->sub_packet_list_A[i].packet = packet;
1362 
1363  /* add subpacket to related list */
1364  if (packet->type == 8) {
1365  SAMPLES_NEEDED_2("packet type 8");
1366  return;
1367  } else if (packet->type >= 9 && packet->type <= 12) {
1368  /* packets for MPEG Audio like Synthesis Filter */
1369  QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1370  } else if (packet->type == 13) {
1371  for (j = 0; j < 6; j++)
1372  q->fft_level_exp[j] = get_bits(&gb, 6);
1373  } else if (packet->type == 14) {
1374  for (j = 0; j < 6; j++)
1375  q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
1376  } else if (packet->type == 15) {
1377  SAMPLES_NEEDED_2("packet type 15")
1378  return;
1379  } else if (packet->type >= 16 && packet->type < 48 &&
1380  !fft_subpackets[packet->type - 16]) {
1381  /* packets for FFT */
1383  }
1384  } // Packet bytes loop
1385 
1386  if (q->sub_packet_list_D[0].packet != NULL) {
1388  q->do_synth_filter = 1;
1389  } else if (q->do_synth_filter) {
1390  process_subpacket_10(q, NULL);
1391  process_subpacket_11(q, NULL);
1392  process_subpacket_12(q, NULL);
1393  }
1394 }
1395 
1396 static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
1397  int offset, int duration, int channel,
1398  int exp, int phase)
1399 {
1400  if (q->fft_coefs_min_index[duration] < 0)
1402 
1404  ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1405  q->fft_coefs[q->fft_coefs_index].channel = channel;
1407  q->fft_coefs[q->fft_coefs_index].exp = exp;
1408  q->fft_coefs[q->fft_coefs_index].phase = phase;
1409  q->fft_coefs_index++;
1410 }
1411 
1413  GetBitContext *gb, int b)
1414 {
1415  int channel, stereo, phase, exp;
1416  int local_int_4, local_int_8, stereo_phase, local_int_10;
1417  int local_int_14, stereo_exp, local_int_20, local_int_28;
1418  int n, offset;
1419 
1420  local_int_4 = 0;
1421  local_int_28 = 0;
1422  local_int_20 = 2;
1423  local_int_8 = (4 - duration);
1424  local_int_10 = 1 << (q->group_order - duration - 1);
1425  offset = 1;
1426 
1427  while (get_bits_left(gb)>0) {
1428  if (q->superblocktype_2_3) {
1429  while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1430  if (get_bits_left(gb)<0) {
1431  if(local_int_4 < q->group_size)
1432  av_log(NULL, AV_LOG_ERROR, "overread in qdm2_fft_decode_tones()\n");
1433  return;
1434  }
1435  offset = 1;
1436  if (n == 0) {
1437  local_int_4 += local_int_10;
1438  local_int_28 += (1 << local_int_8);
1439  } else {
1440  local_int_4 += 8 * local_int_10;
1441  local_int_28 += (8 << local_int_8);
1442  }
1443  }
1444  offset += (n - 2);
1445  } else {
1446  offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1447  while (offset >= (local_int_10 - 1)) {
1448  offset += (1 - (local_int_10 - 1));
1449  local_int_4 += local_int_10;
1450  local_int_28 += (1 << local_int_8);
1451  }
1452  }
1453 
1454  if (local_int_4 >= q->group_size)
1455  return;
1456 
1457  local_int_14 = (offset >> local_int_8);
1458  if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
1459  return;
1460 
1461  if (q->nb_channels > 1) {
1462  channel = get_bits1(gb);
1463  stereo = get_bits1(gb);
1464  } else {
1465  channel = 0;
1466  stereo = 0;
1467  }
1468 
1469  exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1470  exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1471  exp = (exp < 0) ? 0 : exp;
1472 
1473  phase = get_bits(gb, 3);
1474  stereo_exp = 0;
1475  stereo_phase = 0;
1476 
1477  if (stereo) {
1478  stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1479  stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1480  if (stereo_phase < 0)
1481  stereo_phase += 8;
1482  }
1483 
1484  if (q->frequency_range > (local_int_14 + 1)) {
1485  int sub_packet = (local_int_20 + local_int_28);
1486 
1487  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1488  channel, exp, phase);
1489  if (stereo)
1490  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1491  1 - channel,
1492  stereo_exp, stereo_phase);
1493  }
1494  offset++;
1495  }
1496 }
1497 
1499 {
1500  int i, j, min, max, value, type, unknown_flag;
1501  GetBitContext gb;
1502 
1503  if (q->sub_packet_list_B[0].packet == NULL)
1504  return;
1505 
1506  /* reset minimum indexes for FFT coefficients */
1507  q->fft_coefs_index = 0;
1508  for (i = 0; i < 5; i++)
1509  q->fft_coefs_min_index[i] = -1;
1510 
1511  /* process subpackets ordered by type, largest type first */
1512  for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1513  QDM2SubPacket *packet = NULL;
1514 
1515  /* find subpacket with largest type less than max */
1516  for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1517  value = q->sub_packet_list_B[j].packet->type;
1518  if (value > min && value < max) {
1519  min = value;
1520  packet = q->sub_packet_list_B[j].packet;
1521  }
1522  }
1523 
1524  max = min;
1525 
1526  /* check for errors (?) */
1527  if (!packet)
1528  return;
1529 
1530  if (i == 0 &&
1531  (packet->type < 16 || packet->type >= 48 ||
1532  fft_subpackets[packet->type - 16]))
1533  return;
1534 
1535  /* decode FFT tones */
1536  init_get_bits(&gb, packet->data, packet->size * 8);
1537 
1538  if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1539  unknown_flag = 1;
1540  else
1541  unknown_flag = 0;
1542 
1543  type = packet->type;
1544 
1545  if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1546  int duration = q->sub_sampling + 5 - (type & 15);
1547 
1548  if (duration >= 0 && duration < 4)
1549  qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
1550  } else if (type == 31) {
1551  for (j = 0; j < 4; j++)
1552  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1553  } else if (type == 46) {
1554  for (j = 0; j < 6; j++)
1555  q->fft_level_exp[j] = get_bits(&gb, 6);
1556  for (j = 0; j < 4; j++)
1557  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1558  }
1559  } // Loop on B packets
1560 
1561  /* calculate maximum indexes for FFT coefficients */
1562  for (i = 0, j = -1; i < 5; i++)
1563  if (q->fft_coefs_min_index[i] >= 0) {
1564  if (j >= 0)
1566  j = i;
1567  }
1568  if (j >= 0)
1570 }
1571 
1573 {
1574  float level, f[6];
1575  int i;
1576  QDM2Complex c;
1577  const double iscale = 2.0 * M_PI / 512.0;
1578 
1579  tone->phase += tone->phase_shift;
1580 
1581  /* calculate current level (maximum amplitude) of tone */
1582  level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1583  c.im = level * sin(tone->phase * iscale);
1584  c.re = level * cos(tone->phase * iscale);
1585 
1586  /* generate FFT coefficients for tone */
1587  if (tone->duration >= 3 || tone->cutoff >= 3) {
1588  tone->complex[0].im += c.im;
1589  tone->complex[0].re += c.re;
1590  tone->complex[1].im -= c.im;
1591  tone->complex[1].re -= c.re;
1592  } else {
1593  f[1] = -tone->table[4];
1594  f[0] = tone->table[3] - tone->table[0];
1595  f[2] = 1.0 - tone->table[2] - tone->table[3];
1596  f[3] = tone->table[1] + tone->table[4] - 1.0;
1597  f[4] = tone->table[0] - tone->table[1];
1598  f[5] = tone->table[2];
1599  for (i = 0; i < 2; i++) {
1600  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
1601  c.re * f[i];
1602  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
1603  c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
1604  }
1605  for (i = 0; i < 4; i++) {
1606  tone->complex[i].re += c.re * f[i + 2];
1607  tone->complex[i].im += c.im * f[i + 2];
1608  }
1609  }
1610 
1611  /* copy the tone if it has not yet died out */
1612  if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1613  memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1614  q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1615  }
1616 }
1617 
1618 static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
1619 {
1620  int i, j, ch;
1621  const double iscale = 0.25 * M_PI;
1622 
1623  for (ch = 0; ch < q->channels; ch++) {
1624  memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
1625  }
1626 
1627 
1628  /* apply FFT tones with duration 4 (1 FFT period) */
1629  if (q->fft_coefs_min_index[4] >= 0)
1630  for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1631  float level;
1632  QDM2Complex c;
1633 
1634  if (q->fft_coefs[i].sub_packet != sub_packet)
1635  break;
1636 
1637  ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1638  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];
1639 
1640  c.re = level * cos(q->fft_coefs[i].phase * iscale);
1641  c.im = level * sin(q->fft_coefs[i].phase * iscale);
1642  q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1643  q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1644  q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1645  q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1646  }
1647 
1648  /* generate existing FFT tones */
1649  for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1651  q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1652  }
1653 
1654  /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1655  for (i = 0; i < 4; i++)
1656  if (q->fft_coefs_min_index[i] >= 0) {
1657  for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1658  int offset, four_i;
1659  FFTTone tone;
1660 
1661  if (q->fft_coefs[j].sub_packet != sub_packet)
1662  break;
1663 
1664  four_i = (4 - i);
1665  offset = q->fft_coefs[j].offset >> four_i;
1666  ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1667 
1668  if (offset < q->frequency_range) {
1669  if (offset < 2)
1670  tone.cutoff = offset;
1671  else
1672  tone.cutoff = (offset >= 60) ? 3 : 2;
1673 
1674  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];
1675  tone.complex = &q->fft.complex[ch][offset];
1676  tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1677  tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1678  tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1679  tone.duration = i;
1680  tone.time_index = 0;
1681 
1682  qdm2_fft_generate_tone(q, &tone);
1683  }
1684  }
1685  q->fft_coefs_min_index[i] = j;
1686  }
1687 }
1688 
1689 static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
1690 {
1691  const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1692  float *out = q->output_buffer + channel;
1693  int i;
1694  q->fft.complex[channel][0].re *= 2.0f;
1695  q->fft.complex[channel][0].im = 0.0f;
1696  q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
1697  /* add samples to output buffer */
1698  for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
1699  out[0] += q->fft.complex[channel][i].re * gain;
1700  out[q->channels] += q->fft.complex[channel][i].im * gain;
1701  out += 2 * q->channels;
1702  }
1703 }
1704 
1705 /**
1706  * @param q context
1707  * @param index subpacket number
1708  */
1710 {
1711  int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1712 
1713  /* copy sb_samples */
1714  sb_used = QDM2_SB_USED(q->sub_sampling);
1715 
1716  for (ch = 0; ch < q->channels; ch++)
1717  for (i = 0; i < 8; i++)
1718  for (k = sb_used; k < SBLIMIT; k++)
1719  q->sb_samples[ch][(8 * index) + i][k] = 0;
1720 
1721  for (ch = 0; ch < q->nb_channels; ch++) {
1722  float *samples_ptr = q->samples + ch;
1723 
1724  for (i = 0; i < 8; i++) {
1726  q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1727  ff_mpa_synth_window_float, &dither_state,
1728  samples_ptr, q->nb_channels,
1729  q->sb_samples[ch][(8 * index) + i]);
1730  samples_ptr += 32 * q->nb_channels;
1731  }
1732  }
1733 
1734  /* add samples to output buffer */
1735  sub_sampling = (4 >> q->sub_sampling);
1736 
1737  for (ch = 0; ch < q->channels; ch++)
1738  for (i = 0; i < q->frame_size; i++)
1739  q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
1740 }
1741 
1742 /**
1743  * Init static data (does not depend on specific file)
1744  *
1745  * @param q context
1746  */
1748  qdm2_init_vlc();
1751  rnd_table_init();
1753 }
1754 
1755 /**
1756  * Init parameters from codec extradata
1757  */
1759 {
1760  QDM2Context *s = avctx->priv_data;
1761  uint8_t *extradata;
1762  int extradata_size;
1763  int tmp_val, tmp, size;
1764 
1765  /* extradata parsing
1766 
1767  Structure:
1768  wave {
1769  frma (QDM2)
1770  QDCA
1771  QDCP
1772  }
1773 
1774  32 size (including this field)
1775  32 tag (=frma)
1776  32 type (=QDM2 or QDMC)
1777 
1778  32 size (including this field, in bytes)
1779  32 tag (=QDCA) // maybe mandatory parameters
1780  32 unknown (=1)
1781  32 channels (=2)
1782  32 samplerate (=44100)
1783  32 bitrate (=96000)
1784  32 block size (=4096)
1785  32 frame size (=256) (for one channel)
1786  32 packet size (=1300)
1787 
1788  32 size (including this field, in bytes)
1789  32 tag (=QDCP) // maybe some tuneable parameters
1790  32 float1 (=1.0)
1791  32 zero ?
1792  32 float2 (=1.0)
1793  32 float3 (=1.0)
1794  32 unknown (27)
1795  32 unknown (8)
1796  32 zero ?
1797  */
1798 
1799  if (!avctx->extradata || (avctx->extradata_size < 48)) {
1800  av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1801  return -1;
1802  }
1803 
1804  extradata = avctx->extradata;
1805  extradata_size = avctx->extradata_size;
1806 
1807  while (extradata_size > 7) {
1808  if (!memcmp(extradata, "frmaQDM", 7))
1809  break;
1810  extradata++;
1811  extradata_size--;
1812  }
1813 
1814  if (extradata_size < 12) {
1815  av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1816  extradata_size);
1817  return -1;
1818  }
1819 
1820  if (memcmp(extradata, "frmaQDM", 7)) {
1821  av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
1822  return -1;
1823  }
1824 
1825  if (extradata[7] == 'C') {
1826 // s->is_qdmc = 1;
1827  av_log(avctx, AV_LOG_ERROR, "stream is QDMC version 1, which is not supported\n");
1828  return -1;
1829  }
1830 
1831  extradata += 8;
1832  extradata_size -= 8;
1833 
1834  size = AV_RB32(extradata);
1835 
1836  if(size > extradata_size){
1837  av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1838  extradata_size, size);
1839  return -1;
1840  }
1841 
1842  extradata += 4;
1843  av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1844  if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
1845  av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1846  return -1;
1847  }
1848 
1849  extradata += 8;
1850 
1851  avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
1852  extradata += 4;
1853  if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) {
1854  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
1855  return AVERROR_INVALIDDATA;
1856  }
1857  avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
1859 
1860  avctx->sample_rate = AV_RB32(extradata);
1861  extradata += 4;
1862 
1863  avctx->bit_rate = AV_RB32(extradata);
1864  extradata += 4;
1865 
1866  s->group_size = AV_RB32(extradata);
1867  extradata += 4;
1868 
1869  s->fft_size = AV_RB32(extradata);
1870  extradata += 4;
1871 
1872  s->checksum_size = AV_RB32(extradata);
1873  if (s->checksum_size >= 1U << 28) {
1874  av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
1875  return AVERROR_INVALIDDATA;
1876  }
1877 
1878  s->fft_order = av_log2(s->fft_size) + 1;
1879 
1880  // something like max decodable tones
1881  s->group_order = av_log2(s->group_size) + 1;
1882  s->frame_size = s->group_size / 16; // 16 iterations per super block
1883 
1885  return AVERROR_INVALIDDATA;
1886 
1887  s->sub_sampling = s->fft_order - 7;
1888  s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1889 
1890  switch ((s->sub_sampling * 2 + s->channels - 1)) {
1891  case 0: tmp = 40; break;
1892  case 1: tmp = 48; break;
1893  case 2: tmp = 56; break;
1894  case 3: tmp = 72; break;
1895  case 4: tmp = 80; break;
1896  case 5: tmp = 100;break;
1897  default: tmp=s->sub_sampling; break;
1898  }
1899  tmp_val = 0;
1900  if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1901  if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1902  if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1903  if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1904  s->cm_table_select = tmp_val;
1905 
1906  if (avctx->bit_rate <= 8000)
1907  s->coeff_per_sb_select = 0;
1908  else if (avctx->bit_rate < 16000)
1909  s->coeff_per_sb_select = 1;
1910  else
1911  s->coeff_per_sb_select = 2;
1912 
1913  // Fail on unknown fft order
1914  if ((s->fft_order < 7) || (s->fft_order > 9)) {
1915  av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
1916  return -1;
1917  }
1918  if (s->fft_size != (1 << (s->fft_order - 1))) {
1919  av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
1920  return AVERROR_INVALIDDATA;
1921  }
1922 
1924  ff_mpadsp_init(&s->mpadsp);
1925 
1926  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1927 
1928  return 0;
1929 }
1930 
1932 {
1933  QDM2Context *s = avctx->priv_data;
1934 
1935  ff_rdft_end(&s->rdft_ctx);
1936 
1937  return 0;
1938 }
1939 
1940 static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
1941 {
1942  int ch, i;
1943  const int frame_size = (q->frame_size * q->channels);
1944 
1945  if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
1946  return -1;
1947 
1948  /* select input buffer */
1949  q->compressed_data = in;
1951 
1952  /* copy old block, clear new block of output samples */
1953  memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1954  memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1955 
1956  /* decode block of QDM2 compressed data */
1957  if (q->sub_packet == 0) {
1958  q->has_errors = 0; // zero it for a new super block
1959  av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1961  }
1962 
1963  /* parse subpackets */
1964  if (!q->has_errors) {
1965  if (q->sub_packet == 2)
1967 
1969  }
1970 
1971  /* sound synthesis stage 1 (FFT) */
1972  for (ch = 0; ch < q->channels; ch++) {
1973  qdm2_calculate_fft(q, ch, q->sub_packet);
1974 
1975  if (!q->has_errors && q->sub_packet_list_C[0].packet != NULL) {
1976  SAMPLES_NEEDED_2("has errors, and C list is not empty")
1977  return -1;
1978  }
1979  }
1980 
1981  /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1982  if (!q->has_errors && q->do_synth_filter)
1984 
1985  q->sub_packet = (q->sub_packet + 1) % 16;
1986 
1987  /* clip and convert output float[] to 16bit signed samples */
1988  for (i = 0; i < frame_size; i++) {
1989  int value = (int)q->output_buffer[i];
1990 
1991  if (value > SOFTCLIP_THRESHOLD)
1992  value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
1993  else if (value < -SOFTCLIP_THRESHOLD)
1994  value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
1995 
1996  out[i] = value;
1997  }
1998 
1999  return 0;
2000 }
2001 
2002 static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
2003  int *got_frame_ptr, AVPacket *avpkt)
2004 {
2005  AVFrame *frame = data;
2006  const uint8_t *buf = avpkt->data;
2007  int buf_size = avpkt->size;
2008  QDM2Context *s = avctx->priv_data;
2009  int16_t *out;
2010  int i, ret;
2011 
2012  if(!buf)
2013  return 0;
2014  if(buf_size < s->checksum_size)
2015  return -1;
2016 
2017  /* get output buffer */
2018  frame->nb_samples = 16 * s->frame_size;
2019  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2020  return ret;
2021  out = (int16_t *)frame->data[0];
2022 
2023  for (i = 0; i < 16; i++) {
2024  if (qdm2_decode(s, buf, out) < 0)
2025  return -1;
2026  out += s->channels * s->frame_size;
2027  }
2028 
2029  *got_frame_ptr = 1;
2030 
2031  return s->checksum_size;
2032 }
2033 
2035  .name = "qdm2",
2036  .type = AVMEDIA_TYPE_AUDIO,
2037  .id = AV_CODEC_ID_QDM2,
2038  .priv_data_size = sizeof(QDM2Context),
2040  .init_static_data = qdm2_init_static_data,
2043  .capabilities = CODEC_CAP_DR1,
2044  .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
2045 };