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