atrac3.c
Go to the documentation of this file.
1 /*
2  * Atrac 3 compatible decoder
3  * Copyright (c) 2006-2008 Maxim Poliakovski
4  * Copyright (c) 2006-2008 Benjamin Larsson
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Atrac 3 compatible decoder.
26  * This decoder handles Sony's ATRAC3 data.
27  *
28  * Container formats used to store atrac 3 data:
29  * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
30  *
31  * To use this decoder, a calling application must supply the extradata
32  * bytes provided in the containers above.
33  */
34 
35 #include <math.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 
39 #include "libavutil/float_dsp.h"
40 #include "libavutil/libm.h"
41 #include "avcodec.h"
42 #include "bytestream.h"
43 #include "fft.h"
44 #include "fmtconvert.h"
45 #include "get_bits.h"
46 #include "internal.h"
47 
48 #include "atrac.h"
49 #include "atrac3data.h"
50 
51 #define JOINT_STEREO 0x12
52 #define STEREO 0x2
53 
54 #define SAMPLES_PER_FRAME 1024
55 #define MDCT_SIZE 512
56 
57 typedef struct GainInfo {
59  int lev_code[8];
60  int loc_code[8];
61 } GainInfo;
62 
63 typedef struct GainBlock {
64  GainInfo g_block[4];
65 } GainBlock;
66 
67 typedef struct TonalComponent {
68  int pos;
69  int num_coefs;
70  float coef[8];
72 
73 typedef struct ChannelUnit {
76  float prev_frame[SAMPLES_PER_FRAME];
78  TonalComponent components[64];
79  GainBlock gain_block[2];
80 
82  DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
83 
84  float delay_buf1[46]; ///<qmf delay buffers
85  float delay_buf2[46];
86  float delay_buf3[46];
87 } ChannelUnit;
88 
89 typedef struct ATRAC3Context {
91  //@{
92  /** stream data */
94 
96  //@}
97  //@{
98  /** joint-stereo related variables */
99  int matrix_coeff_index_prev[4];
100  int matrix_coeff_index_now[4];
101  int matrix_coeff_index_next[4];
102  int weighting_delay[6];
103  //@}
104  //@{
105  /** data buffers */
107  float temp_buf[1070];
108  //@}
109  //@{
110  /** extradata */
112  //@}
113 
117 } ATRAC3Context;
118 
119 static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
120 static VLC_TYPE atrac3_vlc_table[4096][2];
121 static VLC spectral_coeff_tab[7];
122 static float gain_tab1[16];
123 static float gain_tab2[31];
124 
125 
126 /**
127  * Regular 512 points IMDCT without overlapping, with the exception of the
128  * swapping of odd bands caused by the reverse spectra of the QMF.
129  *
130  * @param odd_band 1 if the band is an odd band
131  */
132 static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
133 {
134  int i;
135 
136  if (odd_band) {
137  /**
138  * Reverse the odd bands before IMDCT, this is an effect of the QMF
139  * transform or it gives better compression to do it this way.
140  * FIXME: It should be possible to handle this in imdct_calc
141  * for that to happen a modification of the prerotation step of
142  * all SIMD code and C code is needed.
143  * Or fix the functions before so they generate a pre reversed spectrum.
144  */
145  for (i = 0; i < 128; i++)
146  FFSWAP(float, input[i], input[255 - i]);
147  }
148 
149  q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input);
150 
151  /* Perform windowing on the output. */
152  q->fdsp.vector_fmul(output, output, mdct_window, MDCT_SIZE);
153 }
154 
155 /*
156  * indata descrambling, only used for data coming from the rm container
157  */
158 static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
159 {
160  int i, off;
161  uint32_t c;
162  const uint32_t *buf;
163  uint32_t *output = (uint32_t *)out;
164 
165  off = (intptr_t)input & 3;
166  buf = (const uint32_t *)(input - off);
167  if (off)
168  c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
169  else
170  c = av_be2ne32(0x537F6103U);
171  bytes += 3 + off;
172  for (i = 0; i < bytes / 4; i++)
173  output[i] = c ^ buf[i];
174 
175  if (off)
176  avpriv_request_sample(NULL, "Offset of %d", off);
177 
178  return off;
179 }
180 
181 static av_cold void init_atrac3_window(void)
182 {
183  int i, j;
184 
185  /* generate the mdct window, for details see
186  * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
187  for (i = 0, j = 255; i < 128; i++, j--) {
188  float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
189  float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
190  float w = 0.5 * (wi * wi + wj * wj);
191  mdct_window[i] = mdct_window[511 - i] = wi / w;
192  mdct_window[j] = mdct_window[511 - j] = wj / w;
193  }
194 }
195 
197 {
198  ATRAC3Context *q = avctx->priv_data;
199 
200  av_free(q->units);
202 
203  ff_mdct_end(&q->mdct_ctx);
204 
205  return 0;
206 }
207 
208 /**
209  * Mantissa decoding
210  *
211  * @param selector which table the output values are coded with
212  * @param coding_flag constant length coding or variable length coding
213  * @param mantissas mantissa output table
214  * @param num_codes number of values to get
215  */
216 static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
217  int coding_flag, int *mantissas,
218  int num_codes)
219 {
220  int i, code, huff_symb;
221 
222  if (selector == 1)
223  num_codes /= 2;
224 
225  if (coding_flag != 0) {
226  /* constant length coding (CLC) */
227  int num_bits = clc_length_tab[selector];
228 
229  if (selector > 1) {
230  for (i = 0; i < num_codes; i++) {
231  if (num_bits)
232  code = get_sbits(gb, num_bits);
233  else
234  code = 0;
235  mantissas[i] = code;
236  }
237  } else {
238  for (i = 0; i < num_codes; i++) {
239  if (num_bits)
240  code = get_bits(gb, num_bits); // num_bits is always 4 in this case
241  else
242  code = 0;
243  mantissas[i * 2 ] = mantissa_clc_tab[code >> 2];
244  mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3];
245  }
246  }
247  } else {
248  /* variable length coding (VLC) */
249  if (selector != 1) {
250  for (i = 0; i < num_codes; i++) {
251  huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
252  spectral_coeff_tab[selector-1].bits, 3);
253  huff_symb += 1;
254  code = huff_symb >> 1;
255  if (huff_symb & 1)
256  code = -code;
257  mantissas[i] = code;
258  }
259  } else {
260  for (i = 0; i < num_codes; i++) {
261  huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table,
262  spectral_coeff_tab[selector - 1].bits, 3);
263  mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ];
264  mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
265  }
266  }
267  }
268 }
269 
270 /**
271  * Restore the quantized band spectrum coefficients
272  *
273  * @return subband count, fix for broken specification/files
274  */
275 static int decode_spectrum(GetBitContext *gb, float *output)
276 {
277  int num_subbands, coding_mode, i, j, first, last, subband_size;
278  int subband_vlc_index[32], sf_index[32];
279  int mantissas[128];
280  float scale_factor;
281 
282  num_subbands = get_bits(gb, 5); // number of coded subbands
283  coding_mode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
284 
285  /* get the VLC selector table for the subbands, 0 means not coded */
286  for (i = 0; i <= num_subbands; i++)
287  subband_vlc_index[i] = get_bits(gb, 3);
288 
289  /* read the scale factor indexes from the stream */
290  for (i = 0; i <= num_subbands; i++) {
291  if (subband_vlc_index[i] != 0)
292  sf_index[i] = get_bits(gb, 6);
293  }
294 
295  for (i = 0; i <= num_subbands; i++) {
296  first = subband_tab[i ];
297  last = subband_tab[i + 1];
298 
299  subband_size = last - first;
300 
301  if (subband_vlc_index[i] != 0) {
302  /* decode spectral coefficients for this subband */
303  /* TODO: This can be done faster is several blocks share the
304  * same VLC selector (subband_vlc_index) */
305  read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
306  mantissas, subband_size);
307 
308  /* decode the scale factor for this subband */
309  scale_factor = ff_atrac_sf_table[sf_index[i]] *
310  inv_max_quant[subband_vlc_index[i]];
311 
312  /* inverse quantize the coefficients */
313  for (j = 0; first < last; first++, j++)
314  output[first] = mantissas[j] * scale_factor;
315  } else {
316  /* this subband was not coded, so zero the entire subband */
317  memset(output + first, 0, subband_size * sizeof(*output));
318  }
319  }
320 
321  /* clear the subbands that were not coded */
322  first = subband_tab[i];
323  memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
324  return num_subbands;
325 }
326 
327 /**
328  * Restore the quantized tonal components
329  *
330  * @param components tonal components
331  * @param num_bands number of coded bands
332  */
334  TonalComponent *components, int num_bands)
335 {
336  int i, b, c, m;
337  int nb_components, coding_mode_selector, coding_mode;
338  int band_flags[4], mantissa[8];
339  int component_count = 0;
340 
341  nb_components = get_bits(gb, 5);
342 
343  /* no tonal components */
344  if (nb_components == 0)
345  return 0;
346 
347  coding_mode_selector = get_bits(gb, 2);
348  if (coding_mode_selector == 2)
349  return AVERROR_INVALIDDATA;
350 
351  coding_mode = coding_mode_selector & 1;
352 
353  for (i = 0; i < nb_components; i++) {
354  int coded_values_per_component, quant_step_index;
355 
356  for (b = 0; b <= num_bands; b++)
357  band_flags[b] = get_bits1(gb);
358 
359  coded_values_per_component = get_bits(gb, 3);
360 
361  quant_step_index = get_bits(gb, 3);
362  if (quant_step_index <= 1)
363  return AVERROR_INVALIDDATA;
364 
365  if (coding_mode_selector == 3)
366  coding_mode = get_bits1(gb);
367 
368  for (b = 0; b < (num_bands + 1) * 4; b++) {
369  int coded_components;
370 
371  if (band_flags[b >> 2] == 0)
372  continue;
373 
374  coded_components = get_bits(gb, 3);
375 
376  for (c = 0; c < coded_components; c++) {
377  TonalComponent *cmp = &components[component_count];
378  int sf_index, coded_values, max_coded_values;
379  float scale_factor;
380 
381  sf_index = get_bits(gb, 6);
382  if (component_count >= 64)
383  return AVERROR_INVALIDDATA;
384 
385  cmp->pos = b * 64 + get_bits(gb, 6);
386 
387  max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
388  coded_values = coded_values_per_component + 1;
389  coded_values = FFMIN(max_coded_values, coded_values);
390 
391  scale_factor = ff_atrac_sf_table[sf_index] *
392  inv_max_quant[quant_step_index];
393 
394  read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
395  mantissa, coded_values);
396 
397  cmp->num_coefs = coded_values;
398 
399  /* inverse quant */
400  for (m = 0; m < coded_values; m++)
401  cmp->coef[m] = mantissa[m] * scale_factor;
402 
403  component_count++;
404  }
405  }
406  }
407 
408  return component_count;
409 }
410 
411 /**
412  * Decode gain parameters for the coded bands
413  *
414  * @param block the gainblock for the current band
415  * @param num_bands amount of coded bands
416  */
418  int num_bands)
419 {
420  int i, cf, num_data;
421  int *level, *loc;
422 
423  GainInfo *gain = block->g_block;
424 
425  for (i = 0; i <= num_bands; i++) {
426  num_data = get_bits(gb, 3);
427  gain[i].num_gain_data = num_data;
428  level = gain[i].lev_code;
429  loc = gain[i].loc_code;
430 
431  for (cf = 0; cf < gain[i].num_gain_data; cf++) {
432  level[cf] = get_bits(gb, 4);
433  loc [cf] = get_bits(gb, 5);
434  if (cf && loc[cf] <= loc[cf - 1])
435  return AVERROR_INVALIDDATA;
436  }
437  }
438 
439  /* Clear the unused blocks. */
440  for (; i < 4 ; i++)
441  gain[i].num_gain_data = 0;
442 
443  return 0;
444 }
445 
446 /**
447  * Apply gain parameters and perform the MDCT overlapping part
448  *
449  * @param input input buffer
450  * @param prev previous buffer to perform overlap against
451  * @param output output buffer
452  * @param gain1 current band gain info
453  * @param gain2 next band gain info
454  */
455 static void gain_compensate_and_overlap(float *input, float *prev,
456  float *output, GainInfo *gain1,
457  GainInfo *gain2)
458 {
459  float g1, g2, gain_inc;
460  int i, j, num_data, start_loc, end_loc;
461 
462 
463  if (gain2->num_gain_data == 0)
464  g1 = 1.0;
465  else
466  g1 = gain_tab1[gain2->lev_code[0]];
467 
468  if (gain1->num_gain_data == 0) {
469  for (i = 0; i < 256; i++)
470  output[i] = input[i] * g1 + prev[i];
471  } else {
472  num_data = gain1->num_gain_data;
473  gain1->loc_code[num_data] = 32;
474  gain1->lev_code[num_data] = 4;
475 
476  for (i = 0, j = 0; i < num_data; i++) {
477  start_loc = gain1->loc_code[i] * 8;
478  end_loc = start_loc + 8;
479 
480  g2 = gain_tab1[gain1->lev_code[i]];
481  gain_inc = gain_tab2[gain1->lev_code[i + 1] -
482  gain1->lev_code[i ] + 15];
483 
484  /* interpolate */
485  for (; j < start_loc; j++)
486  output[j] = (input[j] * g1 + prev[j]) * g2;
487 
488  /* interpolation is done over eight samples */
489  for (; j < end_loc; j++) {
490  output[j] = (input[j] * g1 + prev[j]) * g2;
491  g2 *= gain_inc;
492  }
493  }
494 
495  for (; j < 256; j++)
496  output[j] = input[j] * g1 + prev[j];
497  }
498 
499  /* Delay for the overlapping part. */
500  memcpy(prev, &input[256], 256 * sizeof(*prev));
501 }
502 
503 /**
504  * Combine the tonal band spectrum and regular band spectrum
505  *
506  * @param spectrum output spectrum buffer
507  * @param num_components number of tonal components
508  * @param components tonal components for this band
509  * @return position of the last tonal coefficient
510  */
511 static int add_tonal_components(float *spectrum, int num_components,
512  TonalComponent *components)
513 {
514  int i, j, last_pos = -1;
515  float *input, *output;
516 
517  for (i = 0; i < num_components; i++) {
518  last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
519  input = components[i].coef;
520  output = &spectrum[components[i].pos];
521 
522  for (j = 0; j < components[i].num_coefs; j++)
523  output[j] += input[j];
524  }
525 
526  return last_pos;
527 }
528 
529 #define INTERPOLATE(old, new, nsample) \
530  ((old) + (nsample) * 0.125 * ((new) - (old)))
531 
532 static void reverse_matrixing(float *su1, float *su2, int *prev_code,
533  int *curr_code)
534 {
535  int i, nsample, band;
536  float mc1_l, mc1_r, mc2_l, mc2_r;
537 
538  for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
539  int s1 = prev_code[i];
540  int s2 = curr_code[i];
541  nsample = band;
542 
543  if (s1 != s2) {
544  /* Selector value changed, interpolation needed. */
545  mc1_l = matrix_coeffs[s1 * 2 ];
546  mc1_r = matrix_coeffs[s1 * 2 + 1];
547  mc2_l = matrix_coeffs[s2 * 2 ];
548  mc2_r = matrix_coeffs[s2 * 2 + 1];
549 
550  /* Interpolation is done over the first eight samples. */
551  for (; nsample < band + 8; nsample++) {
552  float c1 = su1[nsample];
553  float c2 = su2[nsample];
554  c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
555  c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
556  su1[nsample] = c2;
557  su2[nsample] = c1 * 2.0 - c2;
558  }
559  }
560 
561  /* Apply the matrix without interpolation. */
562  switch (s2) {
563  case 0: /* M/S decoding */
564  for (; nsample < band + 256; nsample++) {
565  float c1 = su1[nsample];
566  float c2 = su2[nsample];
567  su1[nsample] = c2 * 2.0;
568  su2[nsample] = (c1 - c2) * 2.0;
569  }
570  break;
571  case 1:
572  for (; nsample < band + 256; nsample++) {
573  float c1 = su1[nsample];
574  float c2 = su2[nsample];
575  su1[nsample] = (c1 + c2) * 2.0;
576  su2[nsample] = c2 * -2.0;
577  }
578  break;
579  case 2:
580  case 3:
581  for (; nsample < band + 256; nsample++) {
582  float c1 = su1[nsample];
583  float c2 = su2[nsample];
584  su1[nsample] = c1 + c2;
585  su2[nsample] = c1 - c2;
586  }
587  break;
588  default:
589  av_assert1(0);
590  }
591  }
592 }
593 
594 static void get_channel_weights(int index, int flag, float ch[2])
595 {
596  if (index == 7) {
597  ch[0] = 1.0;
598  ch[1] = 1.0;
599  } else {
600  ch[0] = (index & 7) / 7.0;
601  ch[1] = sqrt(2 - ch[0] * ch[0]);
602  if (flag)
603  FFSWAP(float, ch[0], ch[1]);
604  }
605 }
606 
607 static void channel_weighting(float *su1, float *su2, int *p3)
608 {
609  int band, nsample;
610  /* w[x][y] y=0 is left y=1 is right */
611  float w[2][2];
612 
613  if (p3[1] != 7 || p3[3] != 7) {
614  get_channel_weights(p3[1], p3[0], w[0]);
615  get_channel_weights(p3[3], p3[2], w[1]);
616 
617  for (band = 256; band < 4 * 256; band += 256) {
618  for (nsample = band; nsample < band + 8; nsample++) {
619  su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
620  su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
621  }
622  for(; nsample < band + 256; nsample++) {
623  su1[nsample] *= w[1][0];
624  su2[nsample] *= w[1][1];
625  }
626  }
627  }
628 }
629 
630 /**
631  * Decode a Sound Unit
632  *
633  * @param snd the channel unit to be used
634  * @param output the decoded samples before IQMF in float representation
635  * @param channel_num channel number
636  * @param coding_mode the coding mode (JOINT_STEREO or regular stereo/mono)
637  */
639  ChannelUnit *snd, float *output,
640  int channel_num, int coding_mode)
641 {
642  int band, ret, num_subbands, last_tonal, num_bands;
643  GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch];
644  GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
645 
646  if (coding_mode == JOINT_STEREO && channel_num == 1) {
647  if (get_bits(gb, 2) != 3) {
648  av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
649  return AVERROR_INVALIDDATA;
650  }
651  } else {
652  if (get_bits(gb, 6) != 0x28) {
653  av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
654  return AVERROR_INVALIDDATA;
655  }
656  }
657 
658  /* number of coded QMF bands */
659  snd->bands_coded = get_bits(gb, 2);
660 
661  ret = decode_gain_control(gb, gain2, snd->bands_coded);
662  if (ret)
663  return ret;
664 
666  snd->bands_coded);
667  if (snd->num_components == -1)
668  return -1;
669 
670  num_subbands = decode_spectrum(gb, snd->spectrum);
671 
672  /* Merge the decoded spectrum and tonal components. */
673  last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
674  snd->components);
675 
676 
677  /* calculate number of used MLT/QMF bands according to the amount of coded
678  spectral lines */
679  num_bands = (subband_tab[num_subbands] - 1) >> 8;
680  if (last_tonal >= 0)
681  num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
682 
683 
684  /* Reconstruct time domain samples. */
685  for (band = 0; band < 4; band++) {
686  /* Perform the IMDCT step without overlapping. */
687  if (band <= num_bands)
688  imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
689  else
690  memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
691 
692  /* gain compensation and overlapping */
693  gain_compensate_and_overlap(snd->imdct_buf,
694  &snd->prev_frame[band * 256],
695  &output[band * 256],
696  &gain1->g_block[band],
697  &gain2->g_block[band]);
698  }
699 
700  /* Swap the gain control buffers for the next frame. */
701  snd->gc_blk_switch ^= 1;
702 
703  return 0;
704 }
705 
706 static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
707  float **out_samples)
708 {
709  ATRAC3Context *q = avctx->priv_data;
710  int ret, i;
711  uint8_t *ptr1;
712 
713  if (q->coding_mode == JOINT_STEREO) {
714  /* channel coupling mode */
715  /* decode Sound Unit 1 */
716  init_get_bits(&q->gb, databuf, avctx->block_align * 8);
717 
718  ret = decode_channel_sound_unit(q, &q->gb, q->units, out_samples[0], 0,
719  JOINT_STEREO);
720  if (ret != 0)
721  return ret;
722 
723  /* Framedata of the su2 in the joint-stereo mode is encoded in
724  * reverse byte order so we need to swap it first. */
725  if (databuf == q->decoded_bytes_buffer) {
726  uint8_t *ptr2 = q->decoded_bytes_buffer + avctx->block_align - 1;
727  ptr1 = q->decoded_bytes_buffer;
728  for (i = 0; i < avctx->block_align / 2; i++, ptr1++, ptr2--)
729  FFSWAP(uint8_t, *ptr1, *ptr2);
730  } else {
731  const uint8_t *ptr2 = databuf + avctx->block_align - 1;
732  for (i = 0; i < avctx->block_align; i++)
733  q->decoded_bytes_buffer[i] = *ptr2--;
734  }
735 
736  /* Skip the sync codes (0xF8). */
737  ptr1 = q->decoded_bytes_buffer;
738  for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
739  if (i >= avctx->block_align)
740  return AVERROR_INVALIDDATA;
741  }
742 
743 
744  /* set the bitstream reader at the start of the second Sound Unit*/
745  init_get_bits8(&q->gb, ptr1, q->decoded_bytes_buffer + avctx->block_align - ptr1);
746 
747  /* Fill the Weighting coeffs delay buffer */
748  memmove(q->weighting_delay, &q->weighting_delay[2],
749  4 * sizeof(*q->weighting_delay));
750  q->weighting_delay[4] = get_bits1(&q->gb);
751  q->weighting_delay[5] = get_bits(&q->gb, 3);
752 
753  for (i = 0; i < 4; i++) {
756  q->matrix_coeff_index_next[i] = get_bits(&q->gb, 2);
757  }
758 
759  /* Decode Sound Unit 2. */
760  ret = decode_channel_sound_unit(q, &q->gb, &q->units[1],
761  out_samples[1], 1, JOINT_STEREO);
762  if (ret != 0)
763  return ret;
764 
765  /* Reconstruct the channel coefficients. */
766  reverse_matrixing(out_samples[0], out_samples[1],
769 
770  channel_weighting(out_samples[0], out_samples[1], q->weighting_delay);
771  } else {
772  /* normal stereo mode or mono */
773  /* Decode the channel sound units. */
774  for (i = 0; i < avctx->channels; i++) {
775  /* Set the bitstream reader at the start of a channel sound unit. */
776  init_get_bits(&q->gb,
777  databuf + i * avctx->block_align / avctx->channels,
778  avctx->block_align * 8 / avctx->channels);
779 
780  ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
781  out_samples[i], i, q->coding_mode);
782  if (ret != 0)
783  return ret;
784  }
785  }
786 
787  /* Apply the iQMF synthesis filter. */
788  for (i = 0; i < avctx->channels; i++) {
789  float *p1 = out_samples[i];
790  float *p2 = p1 + 256;
791  float *p3 = p2 + 256;
792  float *p4 = p3 + 256;
793  ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
794  ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
795  ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
796  }
797 
798  return 0;
799 }
800 
801 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
802  int *got_frame_ptr, AVPacket *avpkt)
803 {
804  AVFrame *frame = data;
805  const uint8_t *buf = avpkt->data;
806  int buf_size = avpkt->size;
807  ATRAC3Context *q = avctx->priv_data;
808  int ret;
809  const uint8_t *databuf;
810 
811  if (buf_size < avctx->block_align) {
812  av_log(avctx, AV_LOG_ERROR,
813  "Frame too small (%d bytes). Truncated file?\n", buf_size);
814  return AVERROR_INVALIDDATA;
815  }
816 
817  /* get output buffer */
818  frame->nb_samples = SAMPLES_PER_FRAME;
819  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
820  return ret;
821 
822  /* Check if we need to descramble and what buffer to pass on. */
823  if (q->scrambled_stream) {
825  databuf = q->decoded_bytes_buffer;
826  } else {
827  databuf = buf;
828  }
829 
830  ret = decode_frame(avctx, databuf, (float **)frame->extended_data);
831  if (ret) {
832  av_log(NULL, AV_LOG_ERROR, "Frame decoding error!\n");
833  return ret;
834  }
835 
836  *got_frame_ptr = 1;
837 
838  return avctx->block_align;
839 }
840 
841 static void atrac3_init_static_data(void)
842 {
843  int i;
844 
847 
848  /* Initialize the VLC tables. */
849  for (i = 0; i < 7; i++) {
850  spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
851  spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
852  atrac3_vlc_offs[i ];
853  init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
854  huff_bits[i], 1, 1,
856  }
857 
858  /* Generate gain tables */
859  for (i = 0; i < 16; i++)
860  gain_tab1[i] = exp2f (4 - i);
861 
862  for (i = -15; i < 16; i++)
863  gain_tab2[i + 15] = exp2f (i * -0.125);
864 }
865 
867 {
868  static int static_init_done;
869  int i, ret;
870  int version, delay, samples_per_frame, frame_factor;
871  const uint8_t *edata_ptr = avctx->extradata;
872  ATRAC3Context *q = avctx->priv_data;
873 
874  if (avctx->channels <= 0 || avctx->channels > 2) {
875  av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
876  return AVERROR(EINVAL);
877  }
878 
879  if (!static_init_done)
881  static_init_done = 1;
882 
883  /* Take care of the codec-specific extradata. */
884  if (avctx->extradata_size == 14) {
885  /* Parse the extradata, WAV format */
886  av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
887  bytestream_get_le16(&edata_ptr)); // Unknown value always 1
888  edata_ptr += 4; // samples per channel
889  q->coding_mode = bytestream_get_le16(&edata_ptr);
890  av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
891  bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
892  frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
893  av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
894  bytestream_get_le16(&edata_ptr)); // Unknown always 0
895 
896  /* setup */
897  samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
898  version = 4;
899  delay = 0x88E;
901  q->scrambled_stream = 0;
902 
903  if (avctx->block_align != 96 * avctx->channels * frame_factor &&
904  avctx->block_align != 152 * avctx->channels * frame_factor &&
905  avctx->block_align != 192 * avctx->channels * frame_factor) {
906  av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
907  "configuration %d/%d/%d\n", avctx->block_align,
908  avctx->channels, frame_factor);
909  return AVERROR_INVALIDDATA;
910  }
911  } else if (avctx->extradata_size == 12 || avctx->extradata_size == 10) {
912  /* Parse the extradata, RM format. */
913  version = bytestream_get_be32(&edata_ptr);
914  samples_per_frame = bytestream_get_be16(&edata_ptr);
915  delay = bytestream_get_be16(&edata_ptr);
916  q->coding_mode = bytestream_get_be16(&edata_ptr);
917  q->scrambled_stream = 1;
918 
919  } else {
920  av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n",
921  avctx->extradata_size);
922  return AVERROR(EINVAL);
923  }
924 
925  if (q->coding_mode == JOINT_STEREO && avctx->channels < 2) {
926  av_log(avctx, AV_LOG_ERROR, "Invalid coding mode\n");
927  return AVERROR_INVALIDDATA;
928  }
929 
930  /* Check the extradata */
931 
932  if (version != 4) {
933  av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
934  return AVERROR_INVALIDDATA;
935  }
936 
937  if (samples_per_frame != SAMPLES_PER_FRAME &&
938  samples_per_frame != SAMPLES_PER_FRAME * 2) {
939  av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
940  samples_per_frame);
941  return AVERROR_INVALIDDATA;
942  }
943 
944  if (delay != 0x88E) {
945  av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
946  delay);
947  return AVERROR_INVALIDDATA;
948  }
949 
950  if (q->coding_mode == STEREO)
951  av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n");
952  else if (q->coding_mode == JOINT_STEREO)
953  av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
954  else {
955  av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
956  q->coding_mode);
957  return AVERROR_INVALIDDATA;
958  }
959 
960  if (avctx->block_align >= UINT_MAX / 2)
961  return AVERROR(EINVAL);
962 
965  if (q->decoded_bytes_buffer == NULL)
966  return AVERROR(ENOMEM);
967 
969 
970  /* initialize the MDCT transform */
971  if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
972  av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
974  return ret;
975  }
976 
977  /* init the joint-stereo decoding data */
978  q->weighting_delay[0] = 0;
979  q->weighting_delay[1] = 7;
980  q->weighting_delay[2] = 0;
981  q->weighting_delay[3] = 7;
982  q->weighting_delay[4] = 0;
983  q->weighting_delay[5] = 7;
984 
985  for (i = 0; i < 4; i++) {
986  q->matrix_coeff_index_prev[i] = 3;
987  q->matrix_coeff_index_now[i] = 3;
988  q->matrix_coeff_index_next[i] = 3;
989  }
990 
992  ff_fmt_convert_init(&q->fmt_conv, avctx);
993 
994  q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
995  if (!q->units) {
996  atrac3_decode_close(avctx);
997  return AVERROR(ENOMEM);
998  }
999 
1000  return 0;
1001 }
1002 
1004  .name = "atrac3",
1005  .type = AVMEDIA_TYPE_AUDIO,
1006  .id = AV_CODEC_ID_ATRAC3,
1007  .priv_data_size = sizeof(ATRAC3Context),
1011  .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1012  .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
1013  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1015 };
static const uint16_t atrac3_vlc_offs[9]
Definition: atrac3data.h:106
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static void reverse_matrixing(float *su1, float *su2, int *prev_code, int *curr_code)
Definition: atrac3.c:532
#define c2
Definition: idct_sh4.c:27
int matrix_coeff_index_next[4]
Definition: atrac3.c:101
struct ATRAC3Context ATRAC3Context
#define JOINT_STEREO
Definition: atrac3.c:51
uint8_t * decoded_bytes_buffer
data buffers
Definition: atrac3.c:106
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static int add_tonal_components(float *spectrum, int num_components, TonalComponent *components)
Combine the tonal band spectrum and regular band spectrum.
Definition: atrac3.c:511
static const uint8_t clc_length_tab[8]
Definition: atrac3data.h:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const uint8_t *const huff_codes[7]
Definition: atrac3data.h:98
#define MDCT_SIZE
Definition: atrac3.c:55
#define SAMPLES_PER_FRAME
Definition: atrac3.c:54
TonalComponent components[64]
Definition: atrac3.c:78
void ff_atrac_iqmf(float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp)
Quadrature mirror synthesis filter.
Definition: atrac.c:82
float coef[8]
Definition: atrac3.c:70
#define VLC_TYPE
Definition: get_bits.h:61
int version
Definition: avisynth_c.h:666
FFTContext mdct_ctx
Definition: atrac3.c:114
#define av_be2ne32(x)
Definition: bswap.h:93
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:225
GetBitContext gb
Definition: atrac3.c:90
output residual component w
#define FFALIGN(x, a)
Definition: common.h:63
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
float ff_atrac_sf_table[64]
Definition: atrac.c:35
static DECLARE_ALIGNED(32, float, mdct_window)
Definition: atrac3.c:119
static const uint8_t *const huff_bits[7]
Definition: atrac3data.h:102
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t bits
Definition: crc.c:216
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
GainBlock gain_block[2]
Definition: atrac3.c:79
#define av_cold
Definition: attributes.h:78
static void channel_weighting(float *su1, float *su2, int *p3)
Definition: atrac3.c:607
window constants for m
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
#define b
Definition: input.c:42
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void ff_atrac_generate_tables(void)
Generate common tables.
Definition: atrac.c:51
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static const int8_t mantissa_clc_tab[4]
Definition: atrac3data.h:114
static const float inv_max_quant[8]
Definition: atrac3data.h:123
uint8_t * data
Atrac common header.
int num_components
Definition: atrac3.c:75
bitstream reader API header.
integer sqrt
Definition: avutil.txt:2
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
static int atrac3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: atrac3.c:801
float, planar
Definition: samplefmt.h:60
frame
Definition: stft.m:14
static int decode_spectrum(GetBitContext *gb, float *output)
Restore the quantized band spectrum coefficients.
Definition: atrac3.c:275
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define s2
Definition: regdef.h:39
int loc_code[8]
Definition: atrac3.c:60
int num_gain_data
Definition: atrac3.c:58
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
int bands_coded
Definition: atrac3.c:74
int flags
CODEC_FLAG_*.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame This method is called when a frame is wanted on an output For an input
float temp_buf[1070]
Definition: atrac3.c:107
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
phase spectrum(unwrapped) ploc
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:81
#define ff_mdct_init
Definition: fft.h:147
#define FFMAX(a, b)
Definition: common.h:56
external API header
#define STEREO
Definition: atrac3.c:52
Definition: get_bits.h:63
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static const uint16_t subband_tab[33]
Definition: atrac3data.h:128
int matrix_coeff_index_now[4]
Definition: atrac3.c:100
static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, float **out_samples)
Definition: atrac3.c:706
Definition: fft.h:62
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static void get_channel_weights(int index, int flag, float ch[2])
Definition: atrac3.c:594
#define FFMIN(a, b)
Definition: common.h:58
static const int8_t mantissa_vlc_tab[18]
Definition: atrac3data.h:116
ret
Definition: avfilter.c:821
float prev_frame[SAMPLES_PER_FRAME]
Definition: atrac3.c:76
int num_coefs
Definition: atrac3.c:69
int gc_blk_switch
Definition: atrac3.c:77
static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
Definition: atrac3.c:158
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:524
static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
Definition: motion_est.c:251
FIXME Range Coding of cr are level
Definition: snow.txt:367
#define INIT_VLC_USE_NEW_STATIC
Definition: get_bits.h:443
int weighting_delay[6]
Definition: atrac3.c:102
int table_allocated
Definition: get_bits.h:66
struct ChannelUnit ChannelUnit
NULL
Definition: eval.c:55
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source code
#define exp2f(x)
Definition: libm.h:82
static int decode_gain_control(GetBitContext *gb, GainBlock *block, int num_bands)
Decode gain parameters for the coded bands.
Definition: atrac3.c:417
static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, int coding_flag, int *mantissas, int num_codes)
Mantissa decoding.
Definition: atrac3.c:216
struct TonalComponent TonalComponent
struct GainBlock GainBlock
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:410
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:426
int coding_mode
stream data
Definition: atrac3.c:93
void * buf
Definition: avisynth_c.h:594
Replacements for frequently missing libm functions.
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
static av_cold int atrac3_decode_close(AVCodecContext *avctx)
Definition: atrac3.c:196
int index
Definition: gxfenc.c:89
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
generate all sine spectral lobes loc
#define s1
Definition: regdef.h:38
ChannelUnit * units
Definition: atrac3.c:95
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static void atrac3_init_static_data(void)
Definition: atrac3.c:841
static av_cold int atrac3_decode_init(AVCodecContext *avctx)
Definition: atrac3.c:866
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
int scrambled_stream
extradata
Definition: atrac3.c:111
#define ff_mdct_end
Definition: fft.h:148
static double c[64]
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
these buffered frames must be flushed immediately if a new input produces new output(Example:frame rate-doubling filter:filter_frame must(1) flush the second copy of the previous frame, if it is still there,(2) push the first copy of the incoming frame,(3) keep the second copy for later.) If the input frame is not enough to produce output
GainInfo g_block[4]
Definition: atrac3.c:64
AVCodec ff_atrac3_decoder
Definition: atrac3.c:1003
static const float matrix_coeffs[8]
Definition: atrac3data.h:137
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
Definition: fmtconvert.c:79
int channels
number of audio channels
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
static av_cold void init_atrac3_window(void)
Definition: atrac3.c:181
#define INTERPOLATE(old, new, nsample)
Definition: atrac3.c:529
struct GainInfo GainInfo
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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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_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_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
int matrix_coeff_index_prev[4]
joint-stereo related variables
Definition: atrac3.c:99
#define M_PI
Definition: mathematics.h:46
static const uint8_t huff_tab_sizes[7]
Definition: atrac3data.h:94
#define FFSWAP(type, a, b)
Definition: common.h:61
void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:118
static void gain_compensate_and_overlap(float *input, float *prev, float *output, GainInfo *gain1, GainInfo *gain2)
Apply gain parameters and perform the MDCT overlapping part.
Definition: atrac3.c:455
Atrac 3 AKA RealAudio 8 compatible decoder data.
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
AVFloatDSPContext fdsp
Definition: atrac3.c:116
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
static int decode_tonal_components(GetBitContext *gb, TonalComponent *components, int num_bands)
Restore the quantized tonal components.
Definition: atrac3.c:333
int lev_code[8]
Definition: atrac3.c:59
#define c1
Definition: idct_sh4.c:26
static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb, ChannelUnit *snd, float *output, int channel_num, int coding_mode)
Decode a Sound Unit.
Definition: atrac3.c:638
FmtConvertContext fmt_conv
Definition: atrac3.c:115