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