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