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