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