FFmpeg
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 
38 #include "libavutil/mem_internal.h"
39 #include "libavutil/thread.h"
40 #include "libavutil/tx.h"
41 
42 #define BITSTREAM_READER_LE
43 #include "avcodec.h"
44 #include "get_bits.h"
45 #include "bytestream.h"
46 #include "codec_internal.h"
47 #include "decode.h"
48 #include "mpegaudio.h"
49 #include "mpegaudiodsp.h"
50 
51 #include "qdm2_tablegen.h"
52 
53 #define QDM2_LIST_ADD(list, size, packet) \
54 do { \
55  if (size > 0) { \
56  list[size - 1].next = &list[size]; \
57  } \
58  list[size].packet = packet; \
59  list[size].next = NULL; \
60  size++; \
61 } while(0)
62 
63 // Result is 8, 16 or 30
64 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
65 
66 #define FIX_NOISE_IDX(noise_idx) \
67  if ((noise_idx) >= 3840) \
68  (noise_idx) -= 3840; \
69 
70 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
71 
72 #define SAMPLES_NEEDED \
73  av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
74 
75 #define SAMPLES_NEEDED_2(why) \
76  av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
77 
78 #define QDM2_MAX_FRAME_SIZE 512
79 
80 typedef int8_t sb_int8_array[2][30][64];
81 
82 /**
83  * Subpacket
84  */
85 typedef struct QDM2SubPacket {
86  int type; ///< subpacket type
87  unsigned int size; ///< subpacket size
88  const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
90 
91 /**
92  * A node in the subpacket list
93  */
94 typedef struct QDM2SubPNode {
95  QDM2SubPacket *packet; ///< packet
96  struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
97 } QDM2SubPNode;
98 
99 typedef struct FFTTone {
100  float level;
102  const float *table;
103  int phase;
105  int duration;
106  short time_index;
107  short cutoff;
108 } FFTTone;
109 
110 typedef struct FFTCoefficient {
111  int16_t sub_packet;
112  uint8_t channel;
113  int16_t offset;
114  int16_t exp;
115  uint8_t phase;
117 
118 typedef struct QDM2FFT {
121 } QDM2FFT;
122 
123 /**
124  * QDM2 decoder context
125  */
126 typedef struct QDM2Context {
127  /// Parameters from codec header, do not change during playback
128  int nb_channels; ///< number of channels
129  int channels; ///< number of channels
130  int group_size; ///< size of frame group (16 frames per group)
131  int fft_size; ///< size of FFT, in complex numbers
132  int checksum_size; ///< size of data block, used also for checksum
133 
134  /// Parameters built from header parameters, do not change during playback
135  int group_order; ///< order of frame group
136  int fft_order; ///< order of FFT (actually fftorder+1)
137  int frame_size; ///< size of data frame
139  int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */
140  int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
141  int cm_table_select; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
142 
143  /// Packets and packet lists
144  QDM2SubPacket sub_packets[16]; ///< the packets themselves
145  QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets
146  QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list
147  int sub_packets_B; ///< number of packets on 'B' list
148  QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors?
149  QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets
150 
151  /// FFT and tones
163 
164  /// I/O data
165  const uint8_t *compressed_data;
168 
169  /// Synthesis filter
175 
176  /// Mixed temporary data used in decoding
177  float tone_level[MPA_MAX_CHANNELS][30][64];
186 
187  // Flags
188  int has_errors; ///< packet has errors
189  int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type
190  int do_synth_filter; ///< used to perform or skip synthesis filter
191 
193  int noise_idx; ///< index for dithering noise table
194 } QDM2Context;
195 
196 static const int switchtable[23] = {
197  0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
198 };
199 
200 static int qdm2_get_vlc(GetBitContext *gb, const VLC *vlc, int flag, int depth)
201 {
202  int value;
203 
204  value = get_vlc2(gb, vlc->table, vlc->bits, depth);
205 
206  /* stage-2, 3 bits exponent escape sequence */
207  if (value < 0)
208  value = get_bits(gb, get_bits(gb, 3) + 1);
209 
210  /* stage-3, optional */
211  if (flag) {
212  int tmp;
213 
214  if (value >= 60) {
215  av_log(NULL, AV_LOG_ERROR, "value %d in qdm2_get_vlc too large\n", value);
216  return 0;
217  }
218 
220 
221  if ((value & ~3) > 0)
222  tmp += get_bits(gb, (value >> 2));
223  value = tmp;
224  }
225 
226  return value;
227 }
228 
229 static int qdm2_get_se_vlc(const VLC *vlc, GetBitContext *gb, int depth)
230 {
231  int value = qdm2_get_vlc(gb, vlc, 0, depth);
232 
233  return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
234 }
235 
236 /**
237  * QDM2 checksum
238  *
239  * @param data pointer to data to be checksummed
240  * @param length data length
241  * @param value checksum value
242  *
243  * @return 0 if checksum is OK
244  */
245 static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
246 {
247  int i;
248 
249  for (i = 0; i < length; i++)
250  value -= data[i];
251 
252  return (uint16_t)(value & 0xffff);
253 }
254 
255 /**
256  * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
257  *
258  * @param gb bitreader context
259  * @param sub_packet packet under analysis
260  */
262  QDM2SubPacket *sub_packet)
263 {
264  sub_packet->type = get_bits(gb, 8);
265 
266  if (sub_packet->type == 0) {
267  sub_packet->size = 0;
268  sub_packet->data = NULL;
269  } else {
270  sub_packet->size = get_bits(gb, 8);
271 
272  if (sub_packet->type & 0x80) {
273  sub_packet->size <<= 8;
274  sub_packet->size |= get_bits(gb, 8);
275  sub_packet->type &= 0x7f;
276  }
277 
278  if (sub_packet->type == 0x7f)
279  sub_packet->type |= (get_bits(gb, 8) << 8);
280 
281  // FIXME: this depends on bitreader-internal data
282  sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];
283  }
284 
285  av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n",
286  sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
287 }
288 
289 /**
290  * Return node pointer to first packet of requested type in list.
291  *
292  * @param list list of subpackets to be scanned
293  * @param type type of searched subpacket
294  * @return node pointer for subpacket if found, else NULL
295  */
297  int type)
298 {
299  while (list && list->packet) {
300  if (list->packet->type == type)
301  return list;
302  list = list->next;
303  }
304  return NULL;
305 }
306 
307 /**
308  * Replace 8 elements with their average value.
309  * Called by qdm2_decode_superblock before starting subblock decoding.
310  *
311  * @param q context
312  */
314 {
315  int i, j, n, ch, sum;
316 
318 
319  for (ch = 0; ch < q->nb_channels; ch++)
320  for (i = 0; i < n; i++) {
321  sum = 0;
322 
323  for (j = 0; j < 8; j++)
324  sum += q->quantized_coeffs[ch][i][j];
325 
326  sum /= 8;
327  if (sum > 0)
328  sum--;
329 
330  for (j = 0; j < 8; j++)
331  q->quantized_coeffs[ch][i][j] = sum;
332  }
333 }
334 
335 /**
336  * Build subband samples with noise weighted by q->tone_level.
337  * Called by synthfilt_build_sb_samples.
338  *
339  * @param q context
340  * @param sb subband index
341  */
343 {
344  int ch, j;
345 
347 
348  if (!q->nb_channels)
349  return;
350 
351  for (ch = 0; ch < q->nb_channels; ch++) {
352  for (j = 0; j < 64; j++) {
353  q->sb_samples[ch][j * 2][sb] =
354  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
355  q->sb_samples[ch][j * 2 + 1][sb] =
356  SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
357  }
358  }
359 }
360 
361 /**
362  * Called while processing data from subpackets 11 and 12.
363  * Used after making changes to coding_method array.
364  *
365  * @param sb subband index
366  * @param channels number of channels
367  * @param coding_method q->coding_method[0][0][0]
368  */
369 static int fix_coding_method_array(int sb, int channels,
370  sb_int8_array coding_method)
371 {
372  int j, k;
373  int ch;
374  int run, case_val;
375 
376  for (ch = 0; ch < channels; ch++) {
377  for (j = 0; j < 64; ) {
378  if (coding_method[ch][sb][j] < 8)
379  return -1;
380  if ((coding_method[ch][sb][j] - 8) > 22) {
381  run = 1;
382  case_val = 8;
383  } else {
384  switch (switchtable[coding_method[ch][sb][j] - 8]) {
385  case 0: run = 10;
386  case_val = 10;
387  break;
388  case 1: run = 1;
389  case_val = 16;
390  break;
391  case 2: run = 5;
392  case_val = 24;
393  break;
394  case 3: run = 3;
395  case_val = 30;
396  break;
397  case 4: run = 1;
398  case_val = 30;
399  break;
400  case 5: run = 1;
401  case_val = 8;
402  break;
403  default: run = 1;
404  case_val = 8;
405  break;
406  }
407  }
408  for (k = 0; k < run; k++) {
409  if (j + k < 128) {
410  int sbjk = sb + (j + k) / 64;
411  if (sbjk > 29) {
413  continue;
414  }
415  if (coding_method[ch][sbjk][(j + k) % 64] > coding_method[ch][sb][j]) {
416  if (k > 0) {
418  //not debugged, almost never used
419  memset(&coding_method[ch][sb][j + k], case_val,
420  k *sizeof(int8_t));
421  memset(&coding_method[ch][sb][j + k], case_val,
422  3 * sizeof(int8_t));
423  }
424  }
425  }
426  }
427  j += run;
428  }
429  }
430  return 0;
431 }
432 
433 /**
434  * Related to synthesis filter
435  * Called by process_subpacket_10
436  *
437  * @param q context
438  * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
439  */
441 {
442  int i, sb, ch, sb_used;
443  int tmp, tab;
444 
445  for (ch = 0; ch < q->nb_channels; ch++)
446  for (sb = 0; sb < 30; sb++)
447  for (i = 0; i < 8; i++) {
449  tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
451  else
453  if(tmp < 0)
454  tmp += 0xff;
455  q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
456  }
457 
458  sb_used = QDM2_SB_USED(q->sub_sampling);
459 
460  if ((q->superblocktype_2_3 != 0) && !flag) {
461  for (sb = 0; sb < sb_used; sb++)
462  for (ch = 0; ch < q->nb_channels; ch++)
463  for (i = 0; i < 64; i++) {
464  q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
465  if (q->tone_level_idx[ch][sb][i] < 0)
466  q->tone_level[ch][sb][i] = 0;
467  else
468  q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
469  }
470  } else {
471  tab = q->superblocktype_2_3 ? 0 : 1;
472  for (sb = 0; sb < sb_used; sb++) {
473  if ((sb >= 4) && (sb <= 23)) {
474  for (ch = 0; ch < q->nb_channels; ch++)
475  for (i = 0; i < 64; i++) {
476  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
477  q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
478  q->tone_level_idx_mid[ch][sb - 4][i / 8] -
479  q->tone_level_idx_hi2[ch][sb - 4];
480  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
481  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
482  q->tone_level[ch][sb][i] = 0;
483  else
484  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
485  }
486  } else {
487  if (sb > 4) {
488  for (ch = 0; ch < q->nb_channels; ch++)
489  for (i = 0; i < 64; i++) {
490  tmp = q->tone_level_idx_base[ch][sb][i / 8] -
491  q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
492  q->tone_level_idx_hi2[ch][sb - 4];
493  q->tone_level_idx[ch][sb][i] = tmp & 0xff;
494  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
495  q->tone_level[ch][sb][i] = 0;
496  else
497  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
498  }
499  } else {
500  for (ch = 0; ch < q->nb_channels; ch++)
501  for (i = 0; i < 64; i++) {
502  tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
503  if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
504  q->tone_level[ch][sb][i] = 0;
505  else
506  q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
507  }
508  }
509  }
510  }
511  }
512 }
513 
514 /**
515  * Related to synthesis filter
516  * Called by process_subpacket_11
517  * c is built with data from subpacket 11
518  * Most of this function is used only if superblock_type_2_3 == 0,
519  * never seen it in samples.
520  *
521  * @param tone_level_idx
522  * @param tone_level_idx_temp
523  * @param coding_method q->coding_method[0][0][0]
524  * @param nb_channels number of channels
525  * @param c coming from subpacket 11, passed as 8*c
526  * @param superblocktype_2_3 flag based on superblock packet type
527  * @param cm_table_select q->cm_table_select
528  */
529 static void fill_coding_method_array(sb_int8_array tone_level_idx,
530  sb_int8_array tone_level_idx_temp,
531  sb_int8_array coding_method,
532  int nb_channels,
533  int c, int superblocktype_2_3,
534  int cm_table_select)
535 {
536  int ch, sb, j;
537  int tmp, acc, esp_40, comp;
538  int add1, add2, add3, add4;
539  int64_t multres;
540 
541  if (!superblocktype_2_3) {
542  /* This case is untested, no samples available */
543  avpriv_request_sample(NULL, "!superblocktype_2_3");
544  return;
545  for (ch = 0; ch < nb_channels; ch++) {
546  for (sb = 0; sb < 30; sb++) {
547  for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
548  add1 = tone_level_idx[ch][sb][j] - 10;
549  if (add1 < 0)
550  add1 = 0;
551  add2 = add3 = add4 = 0;
552  if (sb > 1) {
553  add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
554  if (add2 < 0)
555  add2 = 0;
556  }
557  if (sb > 0) {
558  add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
559  if (add3 < 0)
560  add3 = 0;
561  }
562  if (sb < 29) {
563  add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
564  if (add4 < 0)
565  add4 = 0;
566  }
567  tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
568  if (tmp < 0)
569  tmp = 0;
570  tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
571  }
572  tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
573  }
574  }
575  acc = 0;
576  for (ch = 0; ch < nb_channels; ch++)
577  for (sb = 0; sb < 30; sb++)
578  for (j = 0; j < 64; j++)
579  acc += tone_level_idx_temp[ch][sb][j];
580 
581  multres = 0x66666667LL * (acc * 10);
582  esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
583  for (ch = 0; ch < nb_channels; ch++)
584  for (sb = 0; sb < 30; sb++)
585  for (j = 0; j < 64; j++) {
586  comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
587  if (comp < 0)
588  comp += 0xff;
589  comp /= 256; // signed shift
590  switch(sb) {
591  case 0:
592  if (comp < 30)
593  comp = 30;
594  comp += 15;
595  break;
596  case 1:
597  if (comp < 24)
598  comp = 24;
599  comp += 10;
600  break;
601  case 2:
602  case 3:
603  case 4:
604  if (comp < 16)
605  comp = 16;
606  }
607  if (comp <= 5)
608  tmp = 0;
609  else if (comp <= 10)
610  tmp = 10;
611  else if (comp <= 16)
612  tmp = 16;
613  else if (comp <= 24)
614  tmp = -1;
615  else
616  tmp = 0;
617  coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
618  }
619  for (sb = 0; sb < 30; sb++)
620  fix_coding_method_array(sb, nb_channels, coding_method);
621  for (ch = 0; ch < nb_channels; ch++)
622  for (sb = 0; sb < 30; sb++)
623  for (j = 0; j < 64; j++)
624  if (sb >= 10) {
625  if (coding_method[ch][sb][j] < 10)
626  coding_method[ch][sb][j] = 10;
627  } else {
628  if (sb >= 2) {
629  if (coding_method[ch][sb][j] < 16)
630  coding_method[ch][sb][j] = 16;
631  } else {
632  if (coding_method[ch][sb][j] < 30)
633  coding_method[ch][sb][j] = 30;
634  }
635  }
636  } else { // superblocktype_2_3 != 0
637  for (ch = 0; ch < nb_channels; ch++)
638  for (sb = 0; sb < 30; sb++)
639  for (j = 0; j < 64; j++)
640  coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
641  }
642 }
643 
644 /**
645  * Called by process_subpacket_11 to process more data from subpacket 11
646  * with sb 0-8.
647  * Called by process_subpacket_12 to process data from subpacket 12 with
648  * sb 8-sb_used.
649  *
650  * @param q context
651  * @param gb bitreader context
652  * @param length packet length in bits
653  * @param sb_min lower subband processed (sb_min included)
654  * @param sb_max higher subband processed (sb_max excluded)
655  */
657  int length, int sb_min, int sb_max)
658 {
659  int sb, j, k, n, ch, run, channels;
660  int joined_stereo, zero_encoding;
661  int type34_first;
662  float type34_div = 0;
663  float type34_predictor;
664  float samples[10];
665  int sign_bits[16] = {0};
666 
667  if (length == 0) {
668  // If no data use noise
669  for (sb=sb_min; sb < sb_max; sb++)
671 
672  return 0;
673  }
674 
675  for (sb = sb_min; sb < sb_max; sb++) {
676  channels = q->nb_channels;
677 
678  if (q->nb_channels <= 1 || sb < 12)
679  joined_stereo = 0;
680  else if (sb >= 24)
681  joined_stereo = 1;
682  else
683  joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
684 
685  if (joined_stereo) {
686  if (get_bits_left(gb) >= 16)
687  for (j = 0; j < 16; j++)
688  sign_bits[j] = get_bits1(gb);
689 
690  for (j = 0; j < 64; j++)
691  if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
692  q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
693 
695  q->coding_method)) {
696  av_log(NULL, AV_LOG_ERROR, "coding method invalid\n");
698  continue;
699  }
700  channels = 1;
701  }
702 
703  for (ch = 0; ch < channels; ch++) {
705  zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
706  type34_predictor = 0.0;
707  type34_first = 1;
708 
709  for (j = 0; j < 128; ) {
710  switch (q->coding_method[ch][sb][j / 2]) {
711  case 8:
712  if (get_bits_left(gb) >= 10) {
713  if (zero_encoding) {
714  for (k = 0; k < 5; k++) {
715  if ((j + 2 * k) >= 128)
716  break;
717  samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
718  }
719  } else {
720  n = get_bits(gb, 8);
721  if (n >= 243) {
722  av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
723  return AVERROR_INVALIDDATA;
724  }
725 
726  for (k = 0; k < 5; k++)
727  samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
728  }
729  for (k = 0; k < 5; k++)
730  samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
731  } else {
732  for (k = 0; k < 10; k++)
734  }
735  run = 10;
736  break;
737 
738  case 10:
739  if (get_bits_left(gb) >= 1) {
740  float f = 0.81;
741 
742  if (get_bits1(gb))
743  f = -f;
744  f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
745  samples[0] = f;
746  } else {
748  }
749  run = 1;
750  break;
751 
752  case 16:
753  if (get_bits_left(gb) >= 10) {
754  if (zero_encoding) {
755  for (k = 0; k < 5; k++) {
756  if ((j + k) >= 128)
757  break;
758  samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
759  }
760  } else {
761  n = get_bits (gb, 8);
762  if (n >= 243) {
763  av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
764  return AVERROR_INVALIDDATA;
765  }
766 
767  for (k = 0; k < 5; k++)
768  samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
769  }
770  } else {
771  for (k = 0; k < 5; k++)
773  }
774  run = 5;
775  break;
776 
777  case 24:
778  if (get_bits_left(gb) >= 7) {
779  n = get_bits(gb, 7);
780  if (n >= 125) {
781  av_log(NULL, AV_LOG_ERROR, "Invalid 7bit codeword\n");
782  return AVERROR_INVALIDDATA;
783  }
784 
785  for (k = 0; k < 3; k++)
786  samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
787  } else {
788  for (k = 0; k < 3; k++)
790  }
791  run = 3;
792  break;
793 
794  case 30:
795  if (get_bits_left(gb) >= 4) {
796  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
798  av_log(NULL, AV_LOG_ERROR, "index %d out of type30_dequant array\n", index);
799  return AVERROR_INVALIDDATA;
800  }
802  } else
804 
805  run = 1;
806  break;
807 
808  case 34:
809  if (get_bits_left(gb) >= 7) {
810  if (type34_first) {
811  type34_div = (float)(1 << get_bits(gb, 2));
812  samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
813  type34_predictor = samples[0];
814  type34_first = 0;
815  } else {
816  unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
818  av_log(NULL, AV_LOG_ERROR, "index %d out of type34_delta array\n", index);
819  return AVERROR_INVALIDDATA;
820  }
821  samples[0] = type34_delta[index] / type34_div + type34_predictor;
822  type34_predictor = samples[0];
823  }
824  } else {
826  }
827  run = 1;
828  break;
829 
830  default:
832  run = 1;
833  break;
834  }
835 
836  if (joined_stereo) {
837  for (k = 0; k < run && j + k < 128; k++) {
838  q->sb_samples[0][j + k][sb] =
839  q->tone_level[0][sb][(j + k) / 2] * samples[k];
840  if (q->nb_channels == 2) {
841  if (sign_bits[(j + k) / 8])
842  q->sb_samples[1][j + k][sb] =
843  q->tone_level[1][sb][(j + k) / 2] * -samples[k];
844  else
845  q->sb_samples[1][j + k][sb] =
846  q->tone_level[1][sb][(j + k) / 2] * samples[k];
847  }
848  }
849  } else {
850  for (k = 0; k < run; k++)
851  if ((j + k) < 128)
852  q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
853  }
854 
855  j += run;
856  } // j loop
857  } // channel loop
858  } // subband loop
859  return 0;
860 }
861 
862 /**
863  * Init the first element of a channel in quantized_coeffs with data
864  * from packet 10 (quantized_coeffs[ch][0]).
865  * This is similar to process_subpacket_9, but for a single channel
866  * and for element [0]
867  * same VLC tables as process_subpacket_9 are used.
868  *
869  * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
870  * @param gb bitreader context
871  */
872 static int init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
873  GetBitContext *gb)
874 {
875  int i, k, run, level, diff;
876 
877  if (get_bits_left(gb) < 16)
878  return -1;
879  level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
880 
881  quantized_coeffs[0] = level;
882 
883  for (i = 0; i < 7; ) {
884  if (get_bits_left(gb) < 16)
885  return -1;
886  run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
887 
888  if (i + run >= 8)
889  return -1;
890 
891  if (get_bits_left(gb) < 16)
892  return -1;
893  diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
894 
895  for (k = 1; k <= run; k++)
896  quantized_coeffs[i + k] = (level + ((k * diff) / run));
897 
898  level += diff;
899  i += run;
900  }
901  return 0;
902 }
903 
904 /**
905  * Related to synthesis filter, process data from packet 10
906  * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
907  * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with
908  * data from packet 10
909  *
910  * @param q context
911  * @param gb bitreader context
912  */
914 {
915  int sb, j, k, n, ch;
916 
917  for (ch = 0; ch < q->nb_channels; ch++) {
919 
920  if (get_bits_left(gb) < 16) {
921  memset(q->quantized_coeffs[ch][0], 0, 8);
922  break;
923  }
924  }
925 
926  n = q->sub_sampling + 1;
927 
928  for (sb = 0; sb < n; sb++)
929  for (ch = 0; ch < q->nb_channels; ch++)
930  for (j = 0; j < 8; j++) {
931  if (get_bits_left(gb) < 1)
932  break;
933  if (get_bits1(gb)) {
934  for (k=0; k < 8; k++) {
935  if (get_bits_left(gb) < 16)
936  break;
937  q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
938  }
939  } else {
940  for (k=0; k < 8; k++)
941  q->tone_level_idx_hi1[ch][sb][j][k] = 0;
942  }
943  }
944 
945  n = QDM2_SB_USED(q->sub_sampling) - 4;
946 
947  for (sb = 0; sb < n; sb++)
948  for (ch = 0; ch < q->nb_channels; ch++) {
949  if (get_bits_left(gb) < 16)
950  break;
952  if (sb > 19)
953  q->tone_level_idx_hi2[ch][sb] -= 16;
954  else
955  for (j = 0; j < 8; j++)
956  q->tone_level_idx_mid[ch][sb][j] = -16;
957  }
958 
959  n = QDM2_SB_USED(q->sub_sampling) - 5;
960 
961  for (sb = 0; sb < n; sb++)
962  for (ch = 0; ch < q->nb_channels; ch++)
963  for (j = 0; j < 8; j++) {
964  if (get_bits_left(gb) < 16)
965  break;
966  q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
967  }
968 }
969 
970 /**
971  * Process subpacket 9, init quantized_coeffs with data from it
972  *
973  * @param q context
974  * @param node pointer to node with packet
975  */
977 {
978  GetBitContext gb;
979  int i, j, k, n, ch, run, level, diff;
980 
981  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
982 
984 
985  for (i = 1; i < n; i++)
986  for (ch = 0; ch < q->nb_channels; ch++) {
987  level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
988  q->quantized_coeffs[ch][i][0] = level;
989 
990  for (j = 0; j < (8 - 1); ) {
991  run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
992  diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
993 
994  if (j + run >= 8)
995  return -1;
996 
997  for (k = 1; k <= run; k++)
998  q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
999 
1000  level += diff;
1001  j += run;
1002  }
1003  }
1004 
1005  for (ch = 0; ch < q->nb_channels; ch++)
1006  for (i = 0; i < 8; i++)
1007  q->quantized_coeffs[ch][0][i] = 0;
1008 
1009  return 0;
1010 }
1011 
1012 /**
1013  * Process subpacket 10 if not null, else
1014  *
1015  * @param q context
1016  * @param node pointer to node with packet
1017  */
1019 {
1020  GetBitContext gb;
1021 
1022  if (node) {
1023  init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1025  fill_tone_level_array(q, 1);
1026  } else {
1027  fill_tone_level_array(q, 0);
1028  }
1029 }
1030 
1031 /**
1032  * Process subpacket 11
1033  *
1034  * @param q context
1035  * @param node pointer to node with packet
1036  */
1038 {
1039  GetBitContext gb;
1040  int length = 0;
1041 
1042  if (node) {
1043  length = node->packet->size * 8;
1044  init_get_bits(&gb, node->packet->data, length);
1045  }
1046 
1047  if (length >= 32) {
1048  int c = get_bits(&gb, 13);
1049 
1050  if (c > 3)
1053  q->nb_channels, 8 * c,
1055  }
1056 
1057  synthfilt_build_sb_samples(q, &gb, length, 0, 8);
1058 }
1059 
1060 /**
1061  * Process subpacket 12
1062  *
1063  * @param q context
1064  * @param node pointer to node with packet
1065  */
1067 {
1068  GetBitContext gb;
1069  int length = 0;
1070 
1071  if (node) {
1072  length = node->packet->size * 8;
1073  init_get_bits(&gb, node->packet->data, length);
1074  }
1075 
1076  synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
1077 }
1078 
1079 /**
1080  * Process new subpackets for synthesis filter
1081  *
1082  * @param q context
1083  * @param list list with synthesis filter packets (list D)
1084  */
1086 {
1087  QDM2SubPNode *nodes[4];
1088 
1090  if (nodes[0])
1091  process_subpacket_9(q, nodes[0]);
1092 
1093  nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1094  if (nodes[1])
1095  process_subpacket_10(q, nodes[1]);
1096  else
1098 
1099  nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1100  if (nodes[0] && nodes[1] && nodes[2])
1101  process_subpacket_11(q, nodes[2]);
1102  else
1104 
1105  nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1106  if (nodes[0] && nodes[1] && nodes[3])
1107  process_subpacket_12(q, nodes[3]);
1108  else
1110 }
1111 
1112 /**
1113  * Decode superblock, fill packet lists.
1114  *
1115  * @param q context
1116  */
1118 {
1119  GetBitContext gb;
1121  int i, packet_bytes, sub_packet_size, sub_packets_D;
1122  unsigned int next_index = 0;
1123 
1124  memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1125  memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1126  memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1127 
1128  q->sub_packets_B = 0;
1129  sub_packets_D = 0;
1130 
1131  average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1132 
1135 
1136  if (header.type < 2 || header.type >= 8) {
1137  q->has_errors = 1;
1138  av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
1139  return;
1140  }
1141 
1142  q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1143  packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
1144 
1145  init_get_bits(&gb, header.data, header.size * 8);
1146 
1147  if (header.type == 2 || header.type == 4 || header.type == 5) {
1148  int csum = 257 * get_bits(&gb, 8);
1149  csum += 2 * get_bits(&gb, 8);
1150 
1151  csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1152 
1153  if (csum != 0) {
1154  q->has_errors = 1;
1155  av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
1156  return;
1157  }
1158  }
1159 
1160  q->sub_packet_list_B[0].packet = NULL;
1161  q->sub_packet_list_D[0].packet = NULL;
1162 
1163  for (i = 0; i < 6; i++)
1164  if (--q->fft_level_exp[i] < 0)
1165  q->fft_level_exp[i] = 0;
1166 
1167  for (i = 0; packet_bytes > 0; i++) {
1168  int j;
1169 
1170  if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
1171  SAMPLES_NEEDED_2("too many packet bytes");
1172  return;
1173  }
1174 
1175  q->sub_packet_list_A[i].next = NULL;
1176 
1177  if (i > 0) {
1178  q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1179 
1180  /* seek to next block */
1181  init_get_bits(&gb, header.data, header.size * 8);
1182  skip_bits(&gb, next_index * 8);
1183 
1184  if (next_index >= header.size)
1185  break;
1186  }
1187 
1188  /* decode subpacket */
1189  packet = &q->sub_packets[i];
1191  next_index = packet->size + get_bits_count(&gb) / 8;
1192  sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1193 
1194  if (packet->type == 0)
1195  break;
1196 
1197  if (sub_packet_size > packet_bytes) {
1198  if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1199  break;
1200  packet->size += packet_bytes - sub_packet_size;
1201  }
1202 
1203  packet_bytes -= sub_packet_size;
1204 
1205  /* add subpacket to 'all subpackets' list */
1207 
1208  /* add subpacket to related list */
1209  if (packet->type == 8) {
1210  SAMPLES_NEEDED_2("packet type 8");
1211  return;
1212  } else if (packet->type >= 9 && packet->type <= 12) {
1213  /* packets for MPEG Audio like Synthesis Filter */
1214  QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1215  } else if (packet->type == 13) {
1216  for (j = 0; j < 6; j++)
1217  q->fft_level_exp[j] = get_bits(&gb, 6);
1218  } else if (packet->type == 14) {
1219  for (j = 0; j < 6; j++)
1220  q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
1221  } else if (packet->type == 15) {
1222  SAMPLES_NEEDED_2("packet type 15")
1223  return;
1224  } else if (packet->type >= 16 && packet->type < 48 &&
1225  !fft_subpackets[packet->type - 16]) {
1226  /* packets for FFT */
1228  }
1229  } // Packet bytes loop
1230 
1231  if (q->sub_packet_list_D[0].packet) {
1233  q->do_synth_filter = 1;
1234  } else if (q->do_synth_filter) {
1238  }
1239 }
1240 
1241 static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
1242  int offset, int duration, int channel,
1243  int exp, int phase)
1244 {
1245  if (q->fft_coefs_min_index[duration] < 0)
1247 
1249  ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1252  q->fft_coefs[q->fft_coefs_index].exp = exp;
1253  q->fft_coefs[q->fft_coefs_index].phase = phase;
1254  q->fft_coefs_index++;
1255 }
1256 
1258  GetBitContext *gb, int b)
1259 {
1260  int channel, stereo, phase, exp;
1261  int local_int_4, local_int_8, stereo_phase, local_int_10;
1262  int local_int_14, stereo_exp, local_int_20, local_int_28;
1263  int n, offset;
1264 
1265  local_int_4 = 0;
1266  local_int_28 = 0;
1267  local_int_20 = 2;
1268  local_int_8 = (4 - duration);
1269  local_int_10 = 1 << (q->group_order - duration - 1);
1270  offset = 1;
1271 
1272  while (get_bits_left(gb)>0) {
1273  if (q->superblocktype_2_3) {
1274  while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1275  if (get_bits_left(gb)<0) {
1276  if(local_int_4 < q->group_size)
1277  av_log(NULL, AV_LOG_ERROR, "overread in qdm2_fft_decode_tones()\n");
1278  return;
1279  }
1280  offset = 1;
1281  if (n == 0) {
1282  local_int_4 += local_int_10;
1283  local_int_28 += (1 << local_int_8);
1284  } else {
1285  local_int_4 += 8 * local_int_10;
1286  local_int_28 += (8 << local_int_8);
1287  }
1288  }
1289  offset += (n - 2);
1290  } else {
1291  if (local_int_10 <= 2) {
1292  av_log(NULL, AV_LOG_ERROR, "qdm2_fft_decode_tones() stuck\n");
1293  return;
1294  }
1295  offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1296  while (offset >= (local_int_10 - 1)) {
1297  offset += (1 - (local_int_10 - 1));
1298  local_int_4 += local_int_10;
1299  local_int_28 += (1 << local_int_8);
1300  }
1301  }
1302 
1303  if (local_int_4 >= q->group_size)
1304  return;
1305 
1306  local_int_14 = (offset >> local_int_8);
1307  if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
1308  return;
1309 
1310  if (q->nb_channels > 1) {
1311  channel = get_bits1(gb);
1312  stereo = get_bits1(gb);
1313  } else {
1314  channel = 0;
1315  stereo = 0;
1316  }
1317 
1319  exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1320  exp = (exp < 0) ? 0 : exp;
1321 
1322  phase = get_bits(gb, 3);
1323  stereo_exp = 0;
1324  stereo_phase = 0;
1325 
1326  if (stereo) {
1327  stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1328  stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1329  if (stereo_phase < 0)
1330  stereo_phase += 8;
1331  }
1332 
1333  if (q->frequency_range > (local_int_14 + 1)) {
1334  int sub_packet = (local_int_20 + local_int_28);
1335 
1336  if (q->fft_coefs_index + stereo >= FF_ARRAY_ELEMS(q->fft_coefs))
1337  return;
1338 
1339  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1340  channel, exp, phase);
1341  if (stereo)
1342  qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1343  1 - channel,
1344  stereo_exp, stereo_phase);
1345  }
1346  offset++;
1347  }
1348 }
1349 
1351 {
1352  int i, j, min, max, value, type, unknown_flag;
1353  GetBitContext gb;
1354 
1355  if (!q->sub_packet_list_B[0].packet)
1356  return;
1357 
1358  /* reset minimum indexes for FFT coefficients */
1359  q->fft_coefs_index = 0;
1360  for (i = 0; i < 5; i++)
1361  q->fft_coefs_min_index[i] = -1;
1362 
1363  /* process subpackets ordered by type, largest type first */
1364  for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1366 
1367  /* find subpacket with largest type less than max */
1368  for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1370  if (value > min && value < max) {
1371  min = value;
1373  }
1374  }
1375 
1376  max = min;
1377 
1378  /* check for errors (?) */
1379  if (!packet)
1380  return;
1381 
1382  if (i == 0 &&
1383  (packet->type < 16 || packet->type >= 48 ||
1384  fft_subpackets[packet->type - 16]))
1385  return;
1386 
1387  /* decode FFT tones */
1388  init_get_bits(&gb, packet->data, packet->size * 8);
1389 
1390  if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1391  unknown_flag = 1;
1392  else
1393  unknown_flag = 0;
1394 
1395  type = packet->type;
1396 
1397  if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1398  int duration = q->sub_sampling + 5 - (type & 15);
1399 
1400  if (duration >= 0 && duration < 4)
1401  qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
1402  } else if (type == 31) {
1403  for (j = 0; j < 4; j++)
1404  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1405  } else if (type == 46) {
1406  for (j = 0; j < 6; j++)
1407  q->fft_level_exp[j] = get_bits(&gb, 6);
1408  for (j = 0; j < 4; j++)
1409  qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1410  }
1411  } // Loop on B packets
1412 
1413  /* calculate maximum indexes for FFT coefficients */
1414  for (i = 0, j = -1; i < 5; i++)
1415  if (q->fft_coefs_min_index[i] >= 0) {
1416  if (j >= 0)
1418  j = i;
1419  }
1420  if (j >= 0)
1422 }
1423 
1425 {
1426  float level, f[6];
1427  int i;
1428  AVComplexFloat c;
1429  const double iscale = 2.0 * M_PI / 512.0;
1430 
1431  tone->phase += tone->phase_shift;
1432 
1433  /* calculate current level (maximum amplitude) of tone */
1434  level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1435  c.im = level * sin(tone->phase * iscale);
1436  c.re = level * cos(tone->phase * iscale);
1437 
1438  /* generate FFT coefficients for tone */
1439  if (tone->duration >= 3 || tone->cutoff >= 3) {
1440  tone->complex[0].im += c.im;
1441  tone->complex[0].re += c.re;
1442  tone->complex[1].im -= c.im;
1443  tone->complex[1].re -= c.re;
1444  } else {
1445  f[1] = -tone->table[4];
1446  f[0] = tone->table[3] - tone->table[0];
1447  f[2] = 1.0 - tone->table[2] - tone->table[3];
1448  f[3] = tone->table[1] + tone->table[4] - 1.0;
1449  f[4] = tone->table[0] - tone->table[1];
1450  f[5] = tone->table[2];
1451  for (i = 0; i < 2; i++) {
1452  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
1453  c.re * f[i];
1454  tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
1455  c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
1456  }
1457  for (i = 0; i < 4; i++) {
1458  tone->complex[i].re += c.re * f[i + 2];
1459  tone->complex[i].im += c.im * f[i + 2];
1460  }
1461  }
1462 
1463  /* copy the tone if it has not yet died out */
1464  if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1465  memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1466  q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1467  }
1468 }
1469 
1470 static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
1471 {
1472  int i, j, ch;
1473  const double iscale = 0.25 * M_PI;
1474 
1475  for (ch = 0; ch < q->channels; ch++) {
1476  memset(q->fft.complex[ch], 0, q->fft_size * sizeof(AVComplexFloat));
1477  }
1478 
1479 
1480  /* apply FFT tones with duration 4 (1 FFT period) */
1481  if (q->fft_coefs_min_index[4] >= 0)
1482  for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1483  float level;
1484  AVComplexFloat c;
1485 
1486  if (q->fft_coefs[i].sub_packet != sub_packet)
1487  break;
1488 
1489  ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1490  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];
1491 
1492  c.re = level * cos(q->fft_coefs[i].phase * iscale);
1493  c.im = level * sin(q->fft_coefs[i].phase * iscale);
1494  q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1495  q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1496  q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1497  q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1498  }
1499 
1500  /* generate existing FFT tones */
1501  for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1503  q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1504  }
1505 
1506  /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1507  for (i = 0; i < 4; i++)
1508  if (q->fft_coefs_min_index[i] >= 0) {
1509  for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1510  int offset, four_i;
1511  FFTTone tone;
1512 
1513  if (q->fft_coefs[j].sub_packet != sub_packet)
1514  break;
1515 
1516  four_i = (4 - i);
1517  offset = q->fft_coefs[j].offset >> four_i;
1518  ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1519 
1520  if (offset < q->frequency_range) {
1521  if (offset < 2)
1522  tone.cutoff = offset;
1523  else
1524  tone.cutoff = (offset >= 60) ? 3 : 2;
1525 
1526  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];
1527  tone.complex = &q->fft.complex[ch][offset];
1528  tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1529  tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1530  tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1531  tone.duration = i;
1532  tone.time_index = 0;
1533 
1534  qdm2_fft_generate_tone(q, &tone);
1535  }
1536  }
1537  q->fft_coefs_min_index[i] = j;
1538  }
1539 }
1540 
1541 static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
1542 {
1543  const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1544  float *out = q->output_buffer + channel;
1545 
1546  q->fft.complex[channel][0].re *= 2.0f;
1547  q->fft.complex[channel][0].im = 0.0f;
1548  q->fft.complex[channel][q->fft_size].re = 0.0f;
1549  q->fft.complex[channel][q->fft_size].im = 0.0f;
1550 
1551  q->rdft_fn(q->rdft_ctx, q->fft.temp[channel], q->fft.complex[channel],
1552  sizeof(AVComplexFloat));
1553 
1554  /* add samples to output buffer */
1555  for (int i = 0; i < FFALIGN(q->fft_size, 8); i++) {
1556  out[0] += q->fft.temp[channel][i].re * gain;
1557  out[q->channels] += q->fft.temp[channel][i].im * gain;
1558  out += 2 * q->channels;
1559  }
1560 }
1561 
1562 /**
1563  * @param q context
1564  * @param index subpacket number
1565  */
1567 {
1568  int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1569 
1570  /* copy sb_samples */
1571  sb_used = QDM2_SB_USED(q->sub_sampling);
1572 
1573  for (ch = 0; ch < q->channels; ch++)
1574  for (i = 0; i < 8; i++)
1575  for (k = sb_used; k < SBLIMIT; k++)
1576  q->sb_samples[ch][(8 * index) + i][k] = 0;
1577 
1578  for (ch = 0; ch < q->nb_channels; ch++) {
1579  float *samples_ptr = q->samples + ch;
1580 
1581  for (i = 0; i < 8; i++) {
1583  q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1584  ff_mpa_synth_window_float, &dither_state,
1585  samples_ptr, q->nb_channels,
1586  q->sb_samples[ch][(8 * index) + i]);
1587  samples_ptr += 32 * q->nb_channels;
1588  }
1589  }
1590 
1591  /* add samples to output buffer */
1592  sub_sampling = (4 >> q->sub_sampling);
1593 
1594  for (ch = 0; ch < q->channels; ch++)
1595  for (i = 0; i < q->frame_size; i++)
1596  q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
1597 }
1598 
1599 /**
1600  * Init static data (does not depend on specific file)
1601  */
1602 static av_cold void qdm2_init_static_data(void) {
1603  qdm2_init_vlc();
1605  rnd_table_init();
1607 
1609 }
1610 
1611 /**
1612  * Init parameters from codec extradata
1613  */
1615 {
1616  static AVOnce init_static_once = AV_ONCE_INIT;
1617  QDM2Context *s = avctx->priv_data;
1618  int ret, tmp_val, tmp, size;
1619  float scale = 1.0f / 2.0f;
1620  GetByteContext gb;
1621 
1622  /* extradata parsing
1623 
1624  Structure:
1625  wave {
1626  frma (QDM2)
1627  QDCA
1628  QDCP
1629  }
1630 
1631  32 size (including this field)
1632  32 tag (=frma)
1633  32 type (=QDM2 or QDMC)
1634 
1635  32 size (including this field, in bytes)
1636  32 tag (=QDCA) // maybe mandatory parameters
1637  32 unknown (=1)
1638  32 channels (=2)
1639  32 samplerate (=44100)
1640  32 bitrate (=96000)
1641  32 block size (=4096)
1642  32 frame size (=256) (for one channel)
1643  32 packet size (=1300)
1644 
1645  32 size (including this field, in bytes)
1646  32 tag (=QDCP) // maybe some tuneable parameters
1647  32 float1 (=1.0)
1648  32 zero ?
1649  32 float2 (=1.0)
1650  32 float3 (=1.0)
1651  32 unknown (27)
1652  32 unknown (8)
1653  32 zero ?
1654  */
1655 
1656  if (!avctx->extradata || (avctx->extradata_size < 48)) {
1657  av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1658  return AVERROR_INVALIDDATA;
1659  }
1660 
1661  bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
1662 
1663  while (bytestream2_get_bytes_left(&gb) > 8) {
1664  if (bytestream2_peek_be64(&gb) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
1665  (uint64_t)MKBETAG('Q','D','M','2')))
1666  break;
1667  bytestream2_skip(&gb, 1);
1668  }
1669 
1670  if (bytestream2_get_bytes_left(&gb) < 12) {
1671  av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1673  return AVERROR_INVALIDDATA;
1674  }
1675 
1676  bytestream2_skip(&gb, 8);
1677  size = bytestream2_get_be32(&gb);
1678 
1679  if (size > bytestream2_get_bytes_left(&gb)) {
1680  av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1682  return AVERROR_INVALIDDATA;
1683  }
1684 
1685  av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1686  if (bytestream2_get_be32(&gb) != MKBETAG('Q','D','C','A')) {
1687  av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1688  return AVERROR_INVALIDDATA;
1689  }
1690 
1691  bytestream2_skip(&gb, 4);
1692 
1693  s->nb_channels = s->channels = bytestream2_get_be32(&gb);
1694  if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) {
1695  av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
1696  return AVERROR_INVALIDDATA;
1697  }
1699  av_channel_layout_default(&avctx->ch_layout, s->channels);
1700 
1701  avctx->sample_rate = bytestream2_get_be32(&gb);
1702  avctx->bit_rate = bytestream2_get_be32(&gb);
1703  s->group_size = bytestream2_get_be32(&gb);
1704  s->fft_size = bytestream2_get_be32(&gb);
1705  s->checksum_size = bytestream2_get_be32(&gb);
1706  if (s->checksum_size >= 1U << 28 || s->checksum_size <= 1) {
1707  av_log(avctx, AV_LOG_ERROR, "data block size invalid (%u)\n", s->checksum_size);
1708  return AVERROR_INVALIDDATA;
1709  }
1710 
1711  s->fft_order = av_log2(s->fft_size) + 1;
1712 
1713  // Fail on unknown fft order
1714  if ((s->fft_order < 7) || (s->fft_order > 9)) {
1715  avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order);
1716  return AVERROR_PATCHWELCOME;
1717  }
1718 
1719  // something like max decodable tones
1720  s->group_order = av_log2(s->group_size) + 1;
1721  s->frame_size = s->group_size / 16; // 16 iterations per super block
1722 
1723  if (s->frame_size > QDM2_MAX_FRAME_SIZE)
1724  return AVERROR_INVALIDDATA;
1725 
1726  s->sub_sampling = s->fft_order - 7;
1727  s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1728 
1729  if (s->frame_size * 4 >> s->sub_sampling > MPA_FRAME_SIZE) {
1730  avpriv_request_sample(avctx, "large frames");
1731  return AVERROR_PATCHWELCOME;
1732  }
1733 
1734  switch ((s->sub_sampling * 2 + s->channels - 1)) {
1735  case 0: tmp = 40; break;
1736  case 1: tmp = 48; break;
1737  case 2: tmp = 56; break;
1738  case 3: tmp = 72; break;
1739  case 4: tmp = 80; break;
1740  case 5: tmp = 100;break;
1741  default: tmp=s->sub_sampling; break;
1742  }
1743  tmp_val = 0;
1744  if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1745  if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1746  if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1747  if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1748  s->cm_table_select = tmp_val;
1749 
1750  if (avctx->bit_rate <= 8000)
1751  s->coeff_per_sb_select = 0;
1752  else if (avctx->bit_rate < 16000)
1753  s->coeff_per_sb_select = 1;
1754  else
1755  s->coeff_per_sb_select = 2;
1756 
1757  if (s->fft_size != (1 << (s->fft_order - 1))) {
1758  av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
1759  return AVERROR_INVALIDDATA;
1760  }
1761 
1762  ret = av_tx_init(&s->rdft_ctx, &s->rdft_fn, AV_TX_FLOAT_RDFT, 1, 2*s->fft_size, &scale, 0);
1763  if (ret < 0)
1764  return ret;
1765 
1766  ff_mpadsp_init(&s->mpadsp);
1767 
1768  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1769 
1770  ff_thread_once(&init_static_once, qdm2_init_static_data);
1771 
1772  return 0;
1773 }
1774 
1776 {
1777  QDM2Context *s = avctx->priv_data;
1778 
1779  av_tx_uninit(&s->rdft_ctx);
1780 
1781  return 0;
1782 }
1783 
1784 static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
1785 {
1786  int ch, i;
1787  const int frame_size = (q->frame_size * q->channels);
1788 
1789  if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
1790  return -1;
1791 
1792  /* select input buffer */
1793  q->compressed_data = in;
1795 
1796  /* copy old block, clear new block of output samples */
1797  memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1798  memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1799 
1800  /* decode block of QDM2 compressed data */
1801  if (q->sub_packet == 0) {
1802  q->has_errors = 0; // zero it for a new super block
1803  av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1805  }
1806 
1807  /* parse subpackets */
1808  if (!q->has_errors) {
1809  if (q->sub_packet == 2)
1811 
1813  }
1814 
1815  /* sound synthesis stage 1 (FFT) */
1816  for (ch = 0; ch < q->channels; ch++) {
1817  qdm2_calculate_fft(q, ch, q->sub_packet);
1818 
1819  if (!q->has_errors && q->sub_packet_list_C[0].packet) {
1820  SAMPLES_NEEDED_2("has errors, and C list is not empty")
1821  return -1;
1822  }
1823  }
1824 
1825  /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1826  if (!q->has_errors && q->do_synth_filter)
1828 
1829  q->sub_packet = (q->sub_packet + 1) % 16;
1830 
1831  /* clip and convert output float[] to 16-bit signed samples */
1832  for (i = 0; i < frame_size; i++) {
1833  int value = (int)q->output_buffer[i];
1834 
1837  else if (value < -SOFTCLIP_THRESHOLD)
1839 
1840  out[i] = value;
1841  }
1842 
1843  return 0;
1844 }
1845 
1847  int *got_frame_ptr, AVPacket *avpkt)
1848 {
1849  const uint8_t *buf = avpkt->data;
1850  int buf_size = avpkt->size;
1851  QDM2Context *s = avctx->priv_data;
1852  int16_t *out;
1853  int i, ret;
1854 
1855  if(!buf)
1856  return 0;
1857  if(buf_size < s->checksum_size)
1858  return -1;
1859 
1860  /* get output buffer */
1861  frame->nb_samples = 16 * s->frame_size;
1862  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1863  return ret;
1864  out = (int16_t *)frame->data[0];
1865 
1866  for (i = 0; i < 16; i++) {
1867  if ((ret = qdm2_decode(s, buf, out)) < 0)
1868  return ret;
1869  out += s->channels * s->frame_size;
1870  }
1871 
1872  *got_frame_ptr = 1;
1873 
1874  return s->checksum_size;
1875 }
1876 
1878  .p.name = "qdm2",
1879  CODEC_LONG_NAME("QDesign Music Codec 2"),
1880  .p.type = AVMEDIA_TYPE_AUDIO,
1881  .p.id = AV_CODEC_ID_QDM2,
1882  .priv_data_size = sizeof(QDM2Context),
1884  .close = qdm2_decode_close,
1886  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1887 };
SAMPLES_NEEDED_2
#define SAMPLES_NEEDED_2(why)
Definition: qdm2.c:75
fft_stereo_exp_vlc
static VLC fft_stereo_exp_vlc
Definition: qdm2_tablegen.h:103
QDM2Context::fft_tone_end
int fft_tone_end
Definition: qdm2.c:154
fft_level_index_table
static const int16_t fft_level_index_table[256]
Definition: qdm2data.h:168
level
uint8_t level
Definition: svq3.c:204
QDM2Context::mpadsp
MPADSPContext mpadsp
Synthesis filter.
Definition: qdm2.c:170
vlc_tab_type30
static VLC vlc_tab_type30
Definition: qdm2_tablegen.h:108
vlc_tab_type34
static VLC vlc_tab_type34
Definition: qdm2_tablegen.h:109
QDM2Context::quantized_coeffs
int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8]
Definition: qdm2.c:179
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
acc
int acc
Definition: yuv2rgb.c:554
mem_internal.h
fix_coding_method_array
static int fix_coding_method_array(int sb, int channels, sb_int8_array coding_method)
Called while processing data from subpackets 11 and 12.
Definition: qdm2.c:369
QDM2Context::frequency_range
int frequency_range
Definition: qdm2.c:138
out
FILE * out
Definition: movenc.c:54
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
FFTCoefficient
Definition: qdm2.c:110
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:80
GetByteContext
Definition: bytestream.h:33
QDM2Context::synth_buf_offset
int synth_buf_offset[MPA_MAX_CHANNELS]
Definition: qdm2.c:172
thread.h
QDM2Context::sub_packet
int sub_packet
Definition: qdm2.c:192
AVTXContext
Definition: tx_priv.h:235
random_dequant_index
static uint8_t random_dequant_index[256][5]
Definition: qdm2_tablegen.h:43
int64_t
long long int64_t
Definition: coverity.c:34
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
ff_mpadsp_init
av_cold void ff_mpadsp_init(MPADSPContext *s)
Definition: mpegaudiodsp.c:81
qdm2_init_static_data
static av_cold void qdm2_init_static_data(void)
Init static data (does not depend on specific file)
Definition: qdm2.c:1602
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:522
MPADSPContext
Definition: mpegaudiodsp.h:28
QDM2Context::sub_packets_B
int sub_packets_B
number of packets on 'B' list
Definition: qdm2.c:147
coding_method_table
static const int8_t coding_method_table[5][30]
Definition: qdm2data.h:272
b
#define b
Definition: input.c:41
data
const char data[16]
Definition: mxf.c:148
QDM2Context::fft_tone_start
int fft_tone_start
Definition: qdm2.c:153
FFCodec
Definition: codec_internal.h:127
AVComplexFloat
Definition: tx.h:27
ff_qdm2_decoder
const FFCodec ff_qdm2_decoder
Definition: qdm2.c:1877
QDM2Context::group_order
int group_order
Parameters built from header parameters, do not change during playback.
Definition: qdm2.c:135
max
#define max(a, b)
Definition: cuda_runtime.h:33
vlc_tab_tone_level_idx_hi1
static VLC vlc_tab_tone_level_idx_hi1
Definition: qdm2_tablegen.h:105
FFTTone::complex
AVComplexFloat * complex
Definition: qdm2.c:101
FFTTone::time_index
short time_index
Definition: qdm2.c:106
SOFTCLIP_THRESHOLD
#define SOFTCLIP_THRESHOLD
Definition: qdm2_tablegen.h:31
softclip_table
static uint16_t softclip_table[HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD+1]
Definition: qdm2_tablegen.h:41
QDM2Context::synth_buf
float synth_buf[MPA_MAX_CHANNELS][512 *2]
Definition: qdm2.c:171
QDM2Context::sub_packet_list_A
QDM2SubPNode sub_packet_list_A[16]
list of all packets
Definition: qdm2.c:145
QDM2FFT
Definition: qdm2.c:118
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
av_tx_init
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:902
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
qdm2_decode
static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
Definition: qdm2.c:1784
process_subpacket_11
static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 11.
Definition: qdm2.c:1037
QDM2Context::has_errors
int has_errors
packet has errors
Definition: qdm2.c:188
QDM2Context::checksum_size
int checksum_size
size of data block, used also for checksum
Definition: qdm2.c:132
QDM2Context::frame_size
int frame_size
size of data frame
Definition: qdm2.c:137
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
synthfilt_build_sb_samples
static int synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb, int length, int sb_min, int sb_max)
Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8.
Definition: qdm2.c:656
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
init_noise_samples
static av_cold void init_noise_samples(void)
Definition: qdm2_tablegen.h:88
AVComplexFloat::im
float im
Definition: tx.h:28
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
QDM2Context::output_buffer
float output_buffer[QDM2_MAX_FRAME_SIZE *MPA_MAX_CHANNELS *2]
Definition: qdm2.c:167
FFTCoefficient::channel
uint8_t channel
Definition: qdm2.c:112
build_sb_samples_from_noise
static void build_sb_samples_from_noise(QDM2Context *q, int sb)
Build subband samples with noise weighted by q->tone_level.
Definition: qdm2.c:342
GetBitContext
Definition: get_bits.h:108
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
process_synthesis_subpackets
static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
Process new subpackets for synthesis filter.
Definition: qdm2.c:1085
QDM2Context::sub_packet_list_C
QDM2SubPNode sub_packet_list_C[16]
packets with errors?
Definition: qdm2.c:148
QDM2SubPacket::data
const uint8_t * data
pointer to subpacket data (points to input data buffer, it's not a private copy)
Definition: qdm2.c:88
switchtable
static const int switchtable[23]
Definition: qdm2.c:196
QDM2Context::fft_coefs
FFTCoefficient fft_coefs[1000]
Definition: qdm2.c:155
fill_coding_method_array
static void fill_coding_method_array(sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp, sb_int8_array coding_method, int nb_channels, int c, int superblocktype_2_3, int cm_table_select)
Related to synthesis filter Called by process_subpacket_11 c is built with data from subpacket 11 Mos...
Definition: qdm2.c:529
vlc_tab_level
static VLC vlc_tab_level
Definition: qdm2_tablegen.h:98
rnd_table_init
static av_cold void rnd_table_init(void)
Definition: qdm2_tablegen.h:57
qdm2_fft_init_coefficient
static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet, int offset, int duration, int channel, int exp, int phase)
Definition: qdm2.c:1241
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_mpa_synth_window_float
float ff_mpa_synth_window_float[]
vlc_tab_run
static VLC vlc_tab_run
Definition: qdm2_tablegen.h:100
process_subpacket_9
static int process_subpacket_9(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 9, init quantized_coeffs with data from it.
Definition: qdm2.c:976
MPA_FRAME_SIZE
#define MPA_FRAME_SIZE
Definition: mpegaudio.h:37
QDM2SubPacket::size
unsigned int size
subpacket size
Definition: qdm2.c:87
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
qdm2_decode_super_block
static void qdm2_decode_super_block(QDM2Context *q)
Decode superblock, fill packet lists.
Definition: qdm2.c:1117
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
qdm2_decode_frame
static int qdm2_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: qdm2.c:1846
qdm2_fft_decode_tones
static void qdm2_fft_decode_tones(QDM2Context *q, int duration, GetBitContext *gb, int b)
Definition: qdm2.c:1257
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
fft_tone_level_table
static const float fft_tone_level_table[2][64]
Definition: qdm2data.h:368
QDM2Context::compressed_data
const uint8_t * compressed_data
I/O data.
Definition: qdm2.c:165
dequant_1bit
static const float dequant_1bit[2][3]
Definition: qdm2data.h:446
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:151
duration
int64_t duration
Definition: movenc.c:64
float
float
Definition: af_crystalizer.c:121
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
FFTCoefficient::phase
uint8_t phase
Definition: qdm2.c:115
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
FIX_NOISE_IDX
#define FIX_NOISE_IDX(noise_idx)
Definition: qdm2.c:66
s
#define s(width, name)
Definition: cbs_vp9.c:198
qdm2_tablegen.h
frame_size
int frame_size
Definition: mxfenc.c:2422
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
QDM2SubPNode::next
struct QDM2SubPNode * next
pointer to next packet in the list, NULL if leaf node
Definition: qdm2.c:96
HARDCLIP_THRESHOLD
#define HARDCLIP_THRESHOLD
Definition: qdm2_tablegen.h:32
QDM2SubPNode
A node in the subpacket list.
Definition: qdm2.c:94
QDM2_LIST_ADD
#define QDM2_LIST_ADD(list, size, packet)
Definition: qdm2.c:53
QDM2Context::do_synth_filter
int do_synth_filter
used to perform or skip synthesis filter
Definition: qdm2.c:190
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
coeff_per_sb_for_dequant
static const uint8_t coeff_per_sb_for_dequant[3][30]
Definition: qdm2data.h:230
channels
channels
Definition: aptx.h:31
decode.h
get_bits.h
SAMPLES_NEEDED
#define SAMPLES_NEEDED
Definition: qdm2.c:72
init_quantized_coeffs_elem0
static int init_quantized_coeffs_elem0(int8_t *quantized_coeffs, GetBitContext *gb)
Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch...
Definition: qdm2.c:872
FFTCoefficient::exp
int16_t exp
Definition: qdm2.c:114
FFTTone::level
float level
Definition: qdm2.c:100
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
if
if(ret)
Definition: filter_design.txt:179
fft_tone_envelope_table
static const float fft_tone_envelope_table[4][31]
Definition: qdm2data.h:406
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:109
qdm2_search_subpacket_type_in_list
static QDM2SubPNode * qdm2_search_subpacket_type_in_list(QDM2SubPNode *list, int type)
Return node pointer to first packet of requested type in list.
Definition: qdm2.c:296
QDM2Context::tone_level
float tone_level[MPA_MAX_CHANNELS][30][64]
Mixed temporary data used in decoding.
Definition: qdm2.c:177
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
qdm2_get_vlc
static int qdm2_get_vlc(GetBitContext *gb, const VLC *vlc, int flag, int depth)
Definition: qdm2.c:200
NULL
#define NULL
Definition: coverity.c:32
QDM2SubPacket
Subpacket.
Definition: qdm2.c:85
QDM2Context::coding_method
int8_t coding_method[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:178
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
qdm2_decode_close
static av_cold int qdm2_decode_close(AVCodecContext *avctx)
Definition: qdm2.c:1775
qdm2_packet_checksum
static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
QDM2 checksum.
Definition: qdm2.c:245
run
uint8_t run
Definition: svq3.c:203
last_coeff
static const uint8_t last_coeff[3]
Definition: qdm2data.h:187
fft_stereo_phase_vlc
static VLC fft_stereo_phase_vlc
Definition: qdm2_tablegen.h:104
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:495
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
list
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining list
Definition: filter_design.txt:25
qdm2_fft_tone_synthesizer
static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
Definition: qdm2.c:1470
QDM2Context::sb_samples
float sb_samples[MPA_MAX_CHANNELS][128][SBLIMIT]
Definition: qdm2.c:173
QDM2FFT::complex
AVComplexFloat complex[MPA_MAX_CHANNELS][256+1]
Definition: qdm2.c:119
AV_CODEC_ID_QDM2
@ AV_CODEC_ID_QDM2
Definition: codec_id.h:459
QDM2Context::compressed_size
int compressed_size
Definition: qdm2.c:166
SBLIMIT
#define SBLIMIT
Definition: mpegaudio.h:44
exp
int8_t exp
Definition: eval.c:74
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:652
AVOnce
#define AVOnce
Definition: thread.h:202
FFTTone::cutoff
short cutoff
Definition: qdm2.c:107
index
int index
Definition: gxfenc.c:89
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
process_subpacket_12
static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 12.
Definition: qdm2.c:1066
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
QDM2Context::sub_packet_list_D
QDM2SubPNode sub_packet_list_D[16]
DCT packets.
Definition: qdm2.c:149
QDM2FFT::temp
AVComplexFloat temp[MPA_MAX_CHANNELS][256]
Definition: qdm2.c:120
f
f
Definition: af_crystalizer.c:121
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1569
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: vvc_intra.c:291
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:109
QDM2Context::tone_level_idx_base
int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8]
Definition: qdm2.c:180
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
size
int size
Definition: twinvq_data.h:10344
AVComplexFloat::re
float re
Definition: tx.h:28
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
qdm2_calculate_fft
static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
Definition: qdm2.c:1541
FFTTone::duration
int duration
Definition: qdm2.c:105
qdm2_decode_init
static av_cold int qdm2_decode_init(AVCodecContext *avctx)
Init parameters from codec extradata.
Definition: qdm2.c:1614
header
static const uint8_t header[24]
Definition: sdr2.c:68
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:164
QDM2Context::tone_level_idx
int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:184
QDM2_SB_USED
#define QDM2_SB_USED(sub_sampling)
Definition: qdm2.c:64
QDM2Context::sub_packets
QDM2SubPacket sub_packets[16]
Packets and packet lists.
Definition: qdm2.c:144
FFTCoefficient::offset
int16_t offset
Definition: qdm2.c:113
QDM2Context::fft_order
int fft_order
order of FFT (actually fftorder+1)
Definition: qdm2.c:136
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
random_dequant_type24
static uint8_t random_dequant_type24[128][3]
Definition: qdm2_tablegen.h:44
vlc_stage3_values
static const int vlc_stage3_values[60]
Definition: qdm2data.h:290
vlc_tab_tone_level_idx_mid
static VLC vlc_tab_tone_level_idx_mid
Definition: qdm2_tablegen.h:106
M_PI
#define M_PI
Definition: mathematics.h:67
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:294
QDM2Context::rdft_fn
av_tx_fn rdft_fn
Definition: qdm2.c:161
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:830
flag
#define flag(name)
Definition: cbs_av1.c:474
fft_subpackets
static const uint8_t fft_subpackets[32]
Definition: qdm2data.h:440
vlc_tab_tone_level_idx_hi2
static VLC vlc_tab_tone_level_idx_hi2
Definition: qdm2_tablegen.h:107
QDM2Context::coeff_per_sb_select
int coeff_per_sb_select
selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
Definition: qdm2.c:140
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:424
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
QDM2Context::fft_level_exp
int fft_level_exp[6]
Definition: qdm2.c:159
QDM2Context::cm_table_select
int cm_table_select
selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
Definition: qdm2.c:141
qdm2_decode_fft_packets
static void qdm2_decode_fft_packets(QDM2Context *q)
Definition: qdm2.c:1350
packet
enum AVPacketSideDataType packet
Definition: decode.c:1381
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
qdm2_decode_sub_packet_header
static void qdm2_decode_sub_packet_header(GetBitContext *gb, QDM2SubPacket *sub_packet)
Fill a QDM2SubPacket structure with packet type, size, and data pointer.
Definition: qdm2.c:261
qdm2_init_vlc
static av_cold void qdm2_init_vlc(void)
Definition: qdm2_tablegen.h:125
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
QDM2Context::sub_sampling
int sub_sampling
subsampling: 0=25%, 1=50%, 2=100% *‍/
Definition: qdm2.c:139
QDM2Context::nb_channels
int nb_channels
Parameters from codec header, do not change during playback.
Definition: qdm2.c:128
mpegaudio.h
tone_level_idx_offset_table
static const int8_t tone_level_idx_offset_table[30][4]
Definition: qdm2data.h:237
avcodec.h
fft_level_exp_alt_vlc
static VLC fft_level_exp_alt_vlc
Definition: qdm2_tablegen.h:101
QDM2Context::fft
QDM2FFT fft
Definition: qdm2.c:162
average_quantized_coeffs
static void average_quantized_coeffs(QDM2Context *q)
Replace 8 elements with their average value.
Definition: qdm2.c:313
VLC::bits
int bits
Definition: vlc.h:37
QDM2Context::tone_level_idx_hi1
int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8]
Definition: qdm2.c:181
ret
ret
Definition: filter_design.txt:187
fill_tone_level_array
static void fill_tone_level_array(QDM2Context *q, int flag)
Related to synthesis filter Called by process_subpacket_10.
Definition: qdm2.c:440
FFTCoefficient::sub_packet
int16_t sub_packet
Definition: qdm2.c:111
FFTTone::phase
int phase
Definition: qdm2.c:103
init_tone_level_dequantization
static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb)
Related to synthesis filter, process data from packet 10 Init part of quantized_coeffs via function i...
Definition: qdm2.c:913
ff_mpa_synth_init_float
void ff_mpa_synth_init_float(void)
fft_tone_sample_table
static const float fft_tone_sample_table[4][16][5]
Definition: qdm2data.h:298
type34_delta
static const float type34_delta[10]
Definition: qdm2data.h:456
coeff_per_sb_for_avg
static const uint8_t coeff_per_sb_for_avg[3][30]
Definition: qdm2data.h:191
U
#define U(x)
Definition: vpx_arith.h:37
AV_TX_FLOAT_RDFT
@ AV_TX_FLOAT_RDFT
Real to complex and complex to real DFTs.
Definition: tx.h:90
QDM2Context::fft_size
int fft_size
size of FFT, in complex numbers
Definition: qdm2.c:131
AVCodecContext
main external API structure.
Definition: avcodec.h:445
fft_level_exp_vlc
static VLC fft_level_exp_vlc
Definition: qdm2_tablegen.h:102
qdm2_synthesis_filter
static void qdm2_synthesis_filter(QDM2Context *q, int index)
Definition: qdm2.c:1566
channel_layout.h
sb_int8_array
int8_t sb_int8_array[2][30][64]
Definition: qdm2.c:80
QDM2Context::noise_idx
int noise_idx
index for dithering noise table
Definition: qdm2.c:193
noise_samples
static float noise_samples[128]
Definition: qdm2_tablegen.h:45
VLC
Definition: vlc.h:36
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:432
QDM2Context::superblocktype_2_3
int superblocktype_2_3
select fft tables and some algorithm based on superblock type
Definition: qdm2.c:189
QDM2Context
QDM2 decoder context.
Definition: qdm2.c:126
QDM2Context::channels
int channels
number of channels
Definition: qdm2.c:129
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
VLC::table
VLCElem * table
Definition: vlc.h:38
QDM2Context::sub_packet_list_B
QDM2SubPNode sub_packet_list_B[16]
FFT packets B are on list.
Definition: qdm2.c:146
ff_mpa_synth_filter_float
void ff_mpa_synth_filter_float(MPADSPContext *s, float *synth_buf_ptr, int *synth_buf_offset, float *window, int *dither_state, float *samples, ptrdiff_t incr, float *sb_samples)
QDM2Context::tone_level_idx_hi2
int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26]
Definition: qdm2.c:183
type30_dequant
static const float type30_dequant[8]
Definition: qdm2data.h:451
vlc_tab_diff
static VLC vlc_tab_diff
Definition: qdm2_tablegen.h:99
mpegaudiodsp.h
qdm2_get_se_vlc
static int qdm2_get_se_vlc(const VLC *vlc, GetBitContext *gb, int depth)
Definition: qdm2.c:229
fft_cutoff_index_table
static const int fft_cutoff_index_table[4][2]
Definition: qdm2data.h:164
process_subpacket_10
static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node)
Process subpacket 10 if not null, else.
Definition: qdm2.c:1018
QDM2Context::fft_coefs_min_index
int fft_coefs_min_index[5]
Definition: qdm2.c:157
QDM2Context::fft_tones
FFTTone fft_tones[1000]
FFT and tones.
Definition: qdm2.c:152
FFTTone::table
const float * table
Definition: qdm2.c:102
QDM2Context::tone_level_idx_mid
int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8]
Definition: qdm2.c:182
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
qdm2_fft_generate_tone
static void qdm2_fft_generate_tone(QDM2Context *q, FFTTone *tone)
Definition: qdm2.c:1424
QDM2Context::fft_coefs_max_index
int fft_coefs_max_index[5]
Definition: qdm2.c:158
QDM2_MAX_FRAME_SIZE
#define QDM2_MAX_FRAME_SIZE
Definition: qdm2.c:78
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
QDM2Context::rdft_ctx
AVTXContext * rdft_ctx
Definition: qdm2.c:160
FFTTone::phase_shift
int phase_shift
Definition: qdm2.c:104
vlc_tab_fft_tone_offset
static VLC vlc_tab_fft_tone_offset[5]
Definition: qdm2_tablegen.h:110
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MPA_MAX_CHANNELS
#define MPA_MAX_CHANNELS
Definition: mpegaudio.h:42
SB_DITHERING_NOISE
#define SB_DITHERING_NOISE(sb, noise_idx)
Definition: qdm2.c:70
dequant_table
static const uint8_t dequant_table[64]
Definition: 4xm.c:117
QDM2SubPacket::type
int type
subpacket type
Definition: qdm2.c:86
QDM2Context::tone_level_idx_temp
int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64]
Definition: qdm2.c:185
QDM2Context::group_size
int group_size
size of frame group (16 frames per group)
Definition: qdm2.c:130
FFTTone
Definition: qdm2.c:99
int
int
Definition: ffmpeg_filter.c:409
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
softclip_table_init
static av_cold void softclip_table_init(void)
Definition: qdm2_tablegen.h:47
channel
channel
Definition: ebur128.h:39
QDM2SubPNode::packet
QDM2SubPacket * packet
packet
Definition: qdm2.c:95
tx.h
QDM2Context::samples
float samples[MPA_MAX_CHANNELS *MPA_FRAME_SIZE]
Definition: qdm2.c:174
min
float min
Definition: vorbis_enc_data.h:429
QDM2Context::fft_coefs_index
int fft_coefs_index
Definition: qdm2.c:156