Libav
atrac3.c
Go to the documentation of this file.
1 /*
2  * ATRAC3 compatible decoder
3  * Copyright (c) 2006-2008 Maxim Poliakovski
4  * Copyright (c) 2006-2008 Benjamin Larsson
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
35 #include <math.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 
39 #include "libavutil/attributes.h"
40 #include "libavutil/float_dsp.h"
41 #include "avcodec.h"
42 #include "bytestream.h"
43 #include "fft.h"
44 #include "get_bits.h"
45 #include "internal.h"
46 
47 #include "atrac.h"
48 #include "atrac3data.h"
49 
50 #define JOINT_STEREO 0x12
51 #define STEREO 0x2
52 
53 #define SAMPLES_PER_FRAME 1024
54 #define MDCT_SIZE 512
55 
56 typedef struct GainBlock {
58 } GainBlock;
59 
60 typedef struct TonalComponent {
61  int pos;
62  int num_coefs;
63  float coef[8];
65 
66 typedef struct ChannelUnit {
69  float prev_frame[SAMPLES_PER_FRAME];
71  TonalComponent components[64];
72  GainBlock gain_block[2];
73 
74  DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
75  DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
76 
77  float delay_buf1[46];
78  float delay_buf2[46];
79  float delay_buf3[46];
80 } ChannelUnit;
81 
82 typedef struct ATRAC3Context {
85 
87 
90 
91 
92  int matrix_coeff_index_prev[4];
93  int matrix_coeff_index_now[4];
94  int matrix_coeff_index_next[4];
95  int weighting_delay[6];
97 
98 
100  float temp_buf[1070];
102 
103 
106 
110 } ATRAC3Context;
111 
113 static VLC_TYPE atrac3_vlc_table[4096][2];
115 
122 static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
123 {
124  int i;
125 
126  if (odd_band) {
135  for (i = 0; i < 128; i++)
136  FFSWAP(float, input[i], input[255 - i]);
137  }
138 
139  q->mdct_ctx.imdct_calc(&q->mdct_ctx, output, input);
140 
141  /* Perform windowing on the output. */
142  q->fdsp.vector_fmul(output, output, mdct_window, MDCT_SIZE);
143 }
144 
145 /*
146  * indata descrambling, only used for data coming from the rm container
147  */
148 static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
149 {
150  int i, off;
151  uint32_t c;
152  const uint32_t *buf;
153  uint32_t *output = (uint32_t *)out;
154 
155  off = (intptr_t)input & 3;
156  buf = (const uint32_t *)(input - off);
157  if (off)
158  c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
159  else
160  c = av_be2ne32(0x537F6103U);
161  bytes += 3 + off;
162  for (i = 0; i < bytes / 4; i++)
163  output[i] = c ^ buf[i];
164 
165  if (off)
166  avpriv_request_sample(NULL, "Offset of %d", off);
167 
168  return off;
169 }
170 
171 static av_cold void init_imdct_window(void)
172 {
173  int i, j;
174 
175  /* generate the mdct window, for details see
176  * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
177  for (i = 0, j = 255; i < 128; i++, j--) {
178  float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
179  float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
180  float w = 0.5 * (wi * wi + wj * wj);
181  mdct_window[i] = mdct_window[511 - i] = wi / w;
182  mdct_window[j] = mdct_window[511 - j] = wj / w;
183  }
184 }
185 
187 {
188  ATRAC3Context *q = avctx->priv_data;
189 
190  av_free(q->units);
192 
193  ff_mdct_end(&q->mdct_ctx);
194 
195  return 0;
196 }
197 
206 static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
207  int coding_flag, int *mantissas,
208  int num_codes)
209 {
210  int i, code, huff_symb;
211 
212  if (selector == 1)
213  num_codes /= 2;
214 
215  if (coding_flag != 0) {
216  /* constant length coding (CLC) */
217  int num_bits = clc_length_tab[selector];
218 
219  if (selector > 1) {
220  for (i = 0; i < num_codes; i++) {
221  if (num_bits)
222  code = get_sbits(gb, num_bits);
223  else
224  code = 0;
225  mantissas[i] = code;
226  }
227  } else {
228  for (i = 0; i < num_codes; i++) {
229  if (num_bits)
230  code = get_bits(gb, num_bits); // num_bits is always 4 in this case
231  else
232  code = 0;
233  mantissas[i * 2 ] = mantissa_clc_tab[code >> 2];
234  mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3];
235  }
236  }
237  } else {
238  /* variable length coding (VLC) */
239  if (selector != 1) {
240  for (i = 0; i < num_codes; i++) {
241  huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
242  spectral_coeff_tab[selector-1].bits, 3);
243  huff_symb += 1;
244  code = huff_symb >> 1;
245  if (huff_symb & 1)
246  code = -code;
247  mantissas[i] = code;
248  }
249  } else {
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  mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ];
254  mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
255  }
256  }
257  }
258 }
259 
265 static int decode_spectrum(GetBitContext *gb, float *output)
266 {
267  int num_subbands, coding_mode, i, j, first, last, subband_size;
268  int subband_vlc_index[32], sf_index[32];
269  int mantissas[128];
270  float scale_factor;
271 
272  num_subbands = get_bits(gb, 5); // number of coded subbands
273  coding_mode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
274 
275  /* get the VLC selector table for the subbands, 0 means not coded */
276  for (i = 0; i <= num_subbands; i++)
277  subband_vlc_index[i] = get_bits(gb, 3);
278 
279  /* read the scale factor indexes from the stream */
280  for (i = 0; i <= num_subbands; i++) {
281  if (subband_vlc_index[i] != 0)
282  sf_index[i] = get_bits(gb, 6);
283  }
284 
285  for (i = 0; i <= num_subbands; i++) {
286  first = subband_tab[i ];
287  last = subband_tab[i + 1];
288 
289  subband_size = last - first;
290 
291  if (subband_vlc_index[i] != 0) {
292  /* decode spectral coefficients for this subband */
293  /* TODO: This can be done faster is several blocks share the
294  * same VLC selector (subband_vlc_index) */
295  read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
296  mantissas, subband_size);
297 
298  /* decode the scale factor for this subband */
299  scale_factor = ff_atrac_sf_table[sf_index[i]] *
300  inv_max_quant[subband_vlc_index[i]];
301 
302  /* inverse quantize the coefficients */
303  for (j = 0; first < last; first++, j++)
304  output[first] = mantissas[j] * scale_factor;
305  } else {
306  /* this subband was not coded, so zero the entire subband */
307  memset(output + first, 0, subband_size * sizeof(*output));
308  }
309  }
310 
311  /* clear the subbands that were not coded */
312  first = subband_tab[i];
313  memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
314  return num_subbands;
315 }
316 
324  TonalComponent *components, int num_bands)
325 {
326  int i, b, c, m;
327  int nb_components, coding_mode_selector, coding_mode;
328  int band_flags[4], mantissa[8];
329  int component_count = 0;
330 
331  nb_components = get_bits(gb, 5);
332 
333  /* no tonal components */
334  if (nb_components == 0)
335  return 0;
336 
337  coding_mode_selector = get_bits(gb, 2);
338  if (coding_mode_selector == 2)
339  return AVERROR_INVALIDDATA;
340 
341  coding_mode = coding_mode_selector & 1;
342 
343  for (i = 0; i < nb_components; i++) {
344  int coded_values_per_component, quant_step_index;
345 
346  for (b = 0; b <= num_bands; b++)
347  band_flags[b] = get_bits1(gb);
348 
349  coded_values_per_component = get_bits(gb, 3);
350 
351  quant_step_index = get_bits(gb, 3);
352  if (quant_step_index <= 1)
353  return AVERROR_INVALIDDATA;
354 
355  if (coding_mode_selector == 3)
356  coding_mode = get_bits1(gb);
357 
358  for (b = 0; b < (num_bands + 1) * 4; b++) {
359  int coded_components;
360 
361  if (band_flags[b >> 2] == 0)
362  continue;
363 
364  coded_components = get_bits(gb, 3);
365 
366  for (c = 0; c < coded_components; c++) {
367  TonalComponent *cmp = &components[component_count];
368  int sf_index, coded_values, max_coded_values;
369  float scale_factor;
370 
371  sf_index = get_bits(gb, 6);
372  if (component_count >= 64)
373  return AVERROR_INVALIDDATA;
374 
375  cmp->pos = b * 64 + get_bits(gb, 6);
376 
377  max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
378  coded_values = coded_values_per_component + 1;
379  coded_values = FFMIN(max_coded_values, coded_values);
380 
381  scale_factor = ff_atrac_sf_table[sf_index] *
382  inv_max_quant[quant_step_index];
383 
384  read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
385  mantissa, coded_values);
386 
387  cmp->num_coefs = coded_values;
388 
389  /* inverse quant */
390  for (m = 0; m < coded_values; m++)
391  cmp->coef[m] = mantissa[m] * scale_factor;
392 
393  component_count++;
394  }
395  }
396  }
397 
398  return component_count;
399 }
400 
408  int num_bands)
409 {
410  int i, j;
411  int *level, *loc;
412 
413  AtracGainInfo *gain = block->g_block;
414 
415  for (i = 0; i <= num_bands; i++) {
416  gain[i].num_points = get_bits(gb, 3);
417  level = gain[i].lev_code;
418  loc = gain[i].loc_code;
419 
420  for (j = 0; j < gain[i].num_points; j++) {
421  level[j] = get_bits(gb, 4);
422  loc[j] = get_bits(gb, 5);
423  if (j && loc[j] <= loc[j - 1])
424  return AVERROR_INVALIDDATA;
425  }
426  }
427 
428  /* Clear the unused blocks. */
429  for (; i < 4 ; i++)
430  gain[i].num_points = 0;
431 
432  return 0;
433 }
434 
443 static int add_tonal_components(float *spectrum, int num_components,
444  TonalComponent *components)
445 {
446  int i, j, last_pos = -1;
447  float *input, *output;
448 
449  for (i = 0; i < num_components; i++) {
450  last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
451  input = components[i].coef;
452  output = &spectrum[components[i].pos];
453 
454  for (j = 0; j < components[i].num_coefs; j++)
455  output[j] += input[j];
456  }
457 
458  return last_pos;
459 }
460 
461 #define INTERPOLATE(old, new, nsample) \
462  ((old) + (nsample) * 0.125 * ((new) - (old)))
463 
464 static void reverse_matrixing(float *su1, float *su2, int *prev_code,
465  int *curr_code)
466 {
467  int i, nsample, band;
468  float mc1_l, mc1_r, mc2_l, mc2_r;
469 
470  for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
471  int s1 = prev_code[i];
472  int s2 = curr_code[i];
473  nsample = band;
474 
475  if (s1 != s2) {
476  /* Selector value changed, interpolation needed. */
477  mc1_l = matrix_coeffs[s1 * 2 ];
478  mc1_r = matrix_coeffs[s1 * 2 + 1];
479  mc2_l = matrix_coeffs[s2 * 2 ];
480  mc2_r = matrix_coeffs[s2 * 2 + 1];
481 
482  /* Interpolation is done over the first eight samples. */
483  for (; nsample < band + 8; nsample++) {
484  float c1 = su1[nsample];
485  float c2 = su2[nsample];
486  c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
487  c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
488  su1[nsample] = c2;
489  su2[nsample] = c1 * 2.0 - c2;
490  }
491  }
492 
493  /* Apply the matrix without interpolation. */
494  switch (s2) {
495  case 0: /* M/S decoding */
496  for (; nsample < band + 256; nsample++) {
497  float c1 = su1[nsample];
498  float c2 = su2[nsample];
499  su1[nsample] = c2 * 2.0;
500  su2[nsample] = (c1 - c2) * 2.0;
501  }
502  break;
503  case 1:
504  for (; nsample < band + 256; nsample++) {
505  float c1 = su1[nsample];
506  float c2 = su2[nsample];
507  su1[nsample] = (c1 + c2) * 2.0;
508  su2[nsample] = c2 * -2.0;
509  }
510  break;
511  case 2:
512  case 3:
513  for (; nsample < band + 256; nsample++) {
514  float c1 = su1[nsample];
515  float c2 = su2[nsample];
516  su1[nsample] = c1 + c2;
517  su2[nsample] = c1 - c2;
518  }
519  break;
520  default:
521  assert(0);
522  }
523  }
524 }
525 
526 static void get_channel_weights(int index, int flag, float ch[2])
527 {
528  if (index == 7) {
529  ch[0] = 1.0;
530  ch[1] = 1.0;
531  } else {
532  ch[0] = (index & 7) / 7.0;
533  ch[1] = sqrt(2 - ch[0] * ch[0]);
534  if (flag)
535  FFSWAP(float, ch[0], ch[1]);
536  }
537 }
538 
539 static void channel_weighting(float *su1, float *su2, int *p3)
540 {
541  int band, nsample;
542  /* w[x][y] y=0 is left y=1 is right */
543  float w[2][2];
544 
545  if (p3[1] != 7 || p3[3] != 7) {
546  get_channel_weights(p3[1], p3[0], w[0]);
547  get_channel_weights(p3[3], p3[2], w[1]);
548 
549  for (band = 256; band < 4 * 256; band += 256) {
550  for (nsample = band; nsample < band + 8; nsample++) {
551  su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
552  su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
553  }
554  for(; nsample < band + 256; nsample++) {
555  su1[nsample] *= w[1][0];
556  su2[nsample] *= w[1][1];
557  }
558  }
559  }
560 }
561 
571  ChannelUnit *snd, float *output,
572  int channel_num, int coding_mode)
573 {
574  int band, ret, num_subbands, last_tonal, num_bands;
575  GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch];
576  GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
577 
578  if (coding_mode == JOINT_STEREO && channel_num == 1) {
579  if (get_bits(gb, 2) != 3) {
580  av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
581  return AVERROR_INVALIDDATA;
582  }
583  } else {
584  if (get_bits(gb, 6) != 0x28) {
585  av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
586  return AVERROR_INVALIDDATA;
587  }
588  }
589 
590  /* number of coded QMF bands */
591  snd->bands_coded = get_bits(gb, 2);
592 
593  ret = decode_gain_control(gb, gain2, snd->bands_coded);
594  if (ret)
595  return ret;
596 
598  snd->bands_coded);
599  if (snd->num_components < 0)
600  return snd->num_components;
601 
602  num_subbands = decode_spectrum(gb, snd->spectrum);
603 
604  /* Merge the decoded spectrum and tonal components. */
605  last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
606  snd->components);
607 
608 
609  /* calculate number of used MLT/QMF bands according to the amount of coded
610  spectral lines */
611  num_bands = (subband_tab[num_subbands] - 1) >> 8;
612  if (last_tonal >= 0)
613  num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
614 
615 
616  /* Reconstruct time domain samples. */
617  for (band = 0; band < 4; band++) {
618  /* Perform the IMDCT step without overlapping. */
619  if (band <= num_bands)
620  imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
621  else
622  memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
623 
624  /* gain compensation and overlapping */
626  &snd->prev_frame[band * 256],
627  &gain1->g_block[band], &gain2->g_block[band],
628  256, &output[band * 256]);
629  }
630 
631  /* Swap the gain control buffers for the next frame. */
632  snd->gc_blk_switch ^= 1;
633 
634  return 0;
635 }
636 
637 static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
638  float **out_samples)
639 {
640  ATRAC3Context *q = avctx->priv_data;
641  int ret, i;
642  uint8_t *ptr1;
643 
644  if (q->coding_mode == JOINT_STEREO) {
645  /* channel coupling mode */
646  /* decode Sound Unit 1 */
647  init_get_bits(&q->gb, databuf, avctx->block_align * 8);
648 
649  ret = decode_channel_sound_unit(q, &q->gb, q->units, out_samples[0], 0,
650  JOINT_STEREO);
651  if (ret != 0)
652  return ret;
653 
654  /* Framedata of the su2 in the joint-stereo mode is encoded in
655  * reverse byte order so we need to swap it first. */
656  if (databuf == q->decoded_bytes_buffer) {
657  uint8_t *ptr2 = q->decoded_bytes_buffer + avctx->block_align - 1;
658  ptr1 = q->decoded_bytes_buffer;
659  for (i = 0; i < avctx->block_align / 2; i++, ptr1++, ptr2--)
660  FFSWAP(uint8_t, *ptr1, *ptr2);
661  } else {
662  const uint8_t *ptr2 = databuf + avctx->block_align - 1;
663  for (i = 0; i < avctx->block_align; i++)
664  q->decoded_bytes_buffer[i] = *ptr2--;
665  }
666 
667  /* Skip the sync codes (0xF8). */
668  ptr1 = q->decoded_bytes_buffer;
669  for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
670  if (i >= avctx->block_align)
671  return AVERROR_INVALIDDATA;
672  }
673 
674 
675  /* set the bitstream reader at the start of the second Sound Unit*/
676  init_get_bits(&q->gb, ptr1, (avctx->block_align - i) * 8);
677 
678  /* Fill the Weighting coeffs delay buffer */
679  memmove(q->weighting_delay, &q->weighting_delay[2],
680  4 * sizeof(*q->weighting_delay));
681  q->weighting_delay[4] = get_bits1(&q->gb);
682  q->weighting_delay[5] = get_bits(&q->gb, 3);
683 
684  for (i = 0; i < 4; i++) {
687  q->matrix_coeff_index_next[i] = get_bits(&q->gb, 2);
688  }
689 
690  /* Decode Sound Unit 2. */
691  ret = decode_channel_sound_unit(q, &q->gb, &q->units[1],
692  out_samples[1], 1, JOINT_STEREO);
693  if (ret != 0)
694  return ret;
695 
696  /* Reconstruct the channel coefficients. */
697  reverse_matrixing(out_samples[0], out_samples[1],
700 
701  channel_weighting(out_samples[0], out_samples[1], q->weighting_delay);
702  } else {
703  /* normal stereo mode or mono */
704  /* Decode the channel sound units. */
705  for (i = 0; i < avctx->channels; i++) {
706  /* Set the bitstream reader at the start of a channel sound unit. */
707  init_get_bits(&q->gb,
708  databuf + i * avctx->block_align / avctx->channels,
709  avctx->block_align * 8 / avctx->channels);
710 
711  ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
712  out_samples[i], i, q->coding_mode);
713  if (ret != 0)
714  return ret;
715  }
716  }
717 
718  /* Apply the iQMF synthesis filter. */
719  for (i = 0; i < avctx->channels; i++) {
720  float *p1 = out_samples[i];
721  float *p2 = p1 + 256;
722  float *p3 = p2 + 256;
723  float *p4 = p3 + 256;
724  ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
725  ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
726  ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
727  }
728 
729  return 0;
730 }
731 
732 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
733  int *got_frame_ptr, AVPacket *avpkt)
734 {
735  AVFrame *frame = data;
736  const uint8_t *buf = avpkt->data;
737  int buf_size = avpkt->size;
738  ATRAC3Context *q = avctx->priv_data;
739  int ret;
740  const uint8_t *databuf;
741 
742  if (buf_size < avctx->block_align) {
743  av_log(avctx, AV_LOG_ERROR,
744  "Frame too small (%d bytes). Truncated file?\n", buf_size);
745  return AVERROR_INVALIDDATA;
746  }
747 
748  /* get output buffer */
749  frame->nb_samples = SAMPLES_PER_FRAME;
750  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
751  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
752  return ret;
753  }
754 
755  /* Check if we need to descramble and what buffer to pass on. */
756  if (q->scrambled_stream) {
758  databuf = q->decoded_bytes_buffer;
759  } else {
760  databuf = buf;
761  }
762 
763  ret = decode_frame(avctx, databuf, (float **)frame->extended_data);
764  if (ret) {
765  av_log(NULL, AV_LOG_ERROR, "Frame decoding error!\n");
766  return ret;
767  }
768 
769  *got_frame_ptr = 1;
770 
771  return avctx->block_align;
772 }
773 
775 {
776  int i;
777 
780 
781  /* Initialize the VLC tables. */
782  for (i = 0; i < 7; i++) {
783  spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
784  spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
785  atrac3_vlc_offs[i ];
786  init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
787  huff_bits[i], 1, 1,
789  }
790 }
791 
793 {
794  int i, ret;
795  int version, delay, samples_per_frame, frame_factor;
796  const uint8_t *edata_ptr = avctx->extradata;
797  ATRAC3Context *q = avctx->priv_data;
798 
799  if (avctx->channels <= 0 || avctx->channels > 2) {
800  av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
801  return AVERROR(EINVAL);
802  }
803 
804  /* Take care of the codec-specific extradata. */
805  if (avctx->extradata_size == 14) {
806  /* Parse the extradata, WAV format */
807  av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
808  bytestream_get_le16(&edata_ptr)); // Unknown value always 1
809  edata_ptr += 4; // samples per channel
810  q->coding_mode = bytestream_get_le16(&edata_ptr);
811  av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
812  bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
813  frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
814  av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
815  bytestream_get_le16(&edata_ptr)); // Unknown always 0
816 
817  /* setup */
818  samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
819  version = 4;
820  delay = 0x88E;
822  q->scrambled_stream = 0;
823 
824  if (avctx->block_align != 96 * avctx->channels * frame_factor &&
825  avctx->block_align != 152 * avctx->channels * frame_factor &&
826  avctx->block_align != 192 * avctx->channels * frame_factor) {
827  av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
828  "configuration %d/%d/%d\n", avctx->block_align,
829  avctx->channels, frame_factor);
830  return AVERROR_INVALIDDATA;
831  }
832  } else if (avctx->extradata_size == 10) {
833  /* Parse the extradata, RM format. */
834  version = bytestream_get_be32(&edata_ptr);
835  samples_per_frame = bytestream_get_be16(&edata_ptr);
836  delay = bytestream_get_be16(&edata_ptr);
837  q->coding_mode = bytestream_get_be16(&edata_ptr);
838  q->scrambled_stream = 1;
839 
840  } else {
841  av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n",
842  avctx->extradata_size);
843  return AVERROR(EINVAL);
844  }
845 
846  /* Check the extradata */
847 
848  if (version != 4) {
849  av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version);
850  return AVERROR_INVALIDDATA;
851  }
852 
853  if (samples_per_frame != SAMPLES_PER_FRAME &&
854  samples_per_frame != SAMPLES_PER_FRAME * 2) {
855  av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n",
856  samples_per_frame);
857  return AVERROR_INVALIDDATA;
858  }
859 
860  if (delay != 0x88E) {
861  av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n",
862  delay);
863  return AVERROR_INVALIDDATA;
864  }
865 
866  if (q->coding_mode == STEREO)
867  av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n");
868  else if (q->coding_mode == JOINT_STEREO) {
869  if (avctx->channels != 2)
870  return AVERROR_INVALIDDATA;
871  av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
872  } else {
873  av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
874  q->coding_mode);
875  return AVERROR_INVALIDDATA;
876  }
877 
878  if (avctx->block_align >= UINT_MAX / 2)
879  return AVERROR(EINVAL);
880 
883  if (!q->decoded_bytes_buffer)
884  return AVERROR(ENOMEM);
885 
887 
888  /* initialize the MDCT transform */
889  if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
890  av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
892  return ret;
893  }
894 
895  /* init the joint-stereo decoding data */
896  q->weighting_delay[0] = 0;
897  q->weighting_delay[1] = 7;
898  q->weighting_delay[2] = 0;
899  q->weighting_delay[3] = 7;
900  q->weighting_delay[4] = 0;
901  q->weighting_delay[5] = 7;
902 
903  for (i = 0; i < 4; i++) {
904  q->matrix_coeff_index_prev[i] = 3;
905  q->matrix_coeff_index_now[i] = 3;
906  q->matrix_coeff_index_next[i] = 3;
907  }
908 
911 
912  q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
913  if (!q->units) {
914  atrac3_decode_close(avctx);
915  return AVERROR(ENOMEM);
916  }
917 
918  return 0;
919 }
920 
922  .name = "atrac3",
923  .long_name = NULL_IF_CONFIG_SMALL("ATRAC3 (Adaptive TRansform Acoustic Coding 3)"),
924  .type = AVMEDIA_TYPE_AUDIO,
925  .id = AV_CODEC_ID_ATRAC3,
926  .priv_data_size = sizeof(ATRAC3Context),
928  .init_static_data = atrac3_init_static_data,
929  .close = atrac3_decode_close,
931  .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
932  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
934 };
float, planar
Definition: samplefmt.h:71
static const uint16_t atrac3_vlc_offs[9]
Definition: atrac3data.h:106
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
static void reverse_matrixing(float *su1, float *su2, int *prev_code, int *curr_code)
Definition: atrac3.c:464
int matrix_coeff_index_next[4]
Definition: atrac3.c:94
#define JOINT_STEREO
Definition: atrac3.c:50
uint8_t * decoded_bytes_buffer
data buffers
Definition: atrac3.c:99
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
static int add_tonal_components(float *spectrum, int num_components, TonalComponent *components)
Combine the tonal band spectrum and regular band spectrum.
Definition: atrac3.c:443
static const uint8_t clc_length_tab[8]
Definition: atrac3data.h:112
static const uint8_t *const huff_codes[7]
Definition: atrac3data.h:98
#define MDCT_SIZE
Definition: atrac3.c:54
AtracGCContext gainc_ctx
Definition: atrac3.c:107
#define SAMPLES_PER_FRAME
Definition: atrac3.c:53
TonalComponent components[64]
Definition: atrac3.c:71
int size
Definition: avcodec.h:1347
int flag
Definition: cpu.c:35
void ff_atrac_iqmf(float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp)
Quadrature mirror synthesis filter.
Definition: atrac.c:127
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:58
float coef[8]
Definition: atrac3.c:63
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
FFTContext mdct_ctx
Definition: atrac3.c:108
static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands caused ...
Definition: atrac3.c:122
#define av_be2ne32(x)
Definition: bswap.h:95
AVCodec.
Definition: avcodec.h:3120
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2189
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:214
GetBitContext gb
Definition: atrac3.c:83
Macro definitions for various function/variable attributes.
int lev_code[7]
level at corresponding control point
Definition: atrac.h:37
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:202
static int16_t block[64]
Definition: dct.c:97
float ff_atrac_sf_table[64]
Definition: atrac.c:36
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:252
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
GainBlock gain_block[2]
Definition: atrac3.c:72
#define av_cold
Definition: attributes.h:66
static av_cold void atrac3_init_static_data(AVCodec *codec)
Definition: atrac3.c:774
static void channel_weighting(float *su1, float *su2, int *p3)
Definition: atrac3.c:539
static float mdct_window[MDCT_SIZE]
Definition: atrac3.c:112
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:52
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
static const int8_t mantissa_clc_tab[4]
Definition: atrac3data.h:114
const char data[16]
Definition: mxf.c:70
static const float inv_max_quant[8]
Definition: atrac3data.h:123
uint8_t * data
Definition: avcodec.h:1346
ATRAC common header.
int num_components
Definition: atrac3.c:68
bitstream reader API header.
int loc_code[7]
location of gain control points
Definition: atrac.h:38
static int atrac3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: atrac3.c:732
#define FFALIGN(x, a)
Definition: macros.h:48
static int decode_spectrum(GetBitContext *gb, float *output)
Restore the quantized band spectrum coefficients.
Definition: atrac3.c:265
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
int bands_coded
Definition: atrac3.c:67
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
float temp_buf[1070]
Definition: atrac3.c:100
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:92
#define ff_mdct_init
Definition: fft.h:151
#define FFMAX(a, b)
Definition: common.h:64
Gain compensation context structure.
Definition: atrac.h:44
#define STEREO
Definition: atrac3.c:51
av_cold void ff_atrac_init_gain_compensation(AtracGCContext *gctx, int id2exp_offset, int loc_scale)
Initialize gain compensation context.
Definition: atrac.c:66
Definition: vlc.h:26
float spectrum[SAMPLES_PER_FRAME]
Definition: atrac3.c:74
static const uint16_t subband_tab[33]
Definition: atrac3data.h:128
int matrix_coeff_index_now[4]
Definition: atrac3.c:93
static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, float **out_samples)
Definition: atrac3.c:637
Definition: fft.h:73
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:788
static void get_channel_weights(int index, int flag, float ch[2])
Definition: atrac3.c:526
#define FFMIN(a, b)
Definition: common.h:66
static const int8_t mantissa_vlc_tab[18]
Definition: atrac3data.h:116
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
float prev_frame[SAMPLES_PER_FRAME]
Definition: atrac3.c:69
int num_coefs
Definition: atrac3.c:62
int gc_blk_switch
Definition: atrac3.c:70
float imdct_buf[SAMPLES_PER_FRAME]
Definition: atrac3.c:75
static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
Definition: atrac3.c:148
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:493
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:257
static VLC_TYPE atrac3_vlc_table[4096][2]
Definition: atrac3.c:113
int weighting_delay[6]
Definition: atrac3.c:95
int table_allocated
Definition: vlc.h:29
NULL
Definition: eval.c:55
static int decode_gain_control(GetBitContext *gb, GainBlock *block, int num_bands)
Decode gain parameters for the coded bands.
Definition: atrac3.c:407
Gain control parameters for one subband.
Definition: atrac.h:35
Libavcodec external API header.
version
Definition: ffv1enc.c:1091
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, int coding_flag, int *mantissas, int num_codes)
Mantissa decoding.
Definition: atrac3.c:206
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
int coding_mode
stream data
Definition: atrac3.c:86
int extradata_size
Definition: avcodec.h:1524
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
static av_cold int atrac3_decode_close(AVCodecContext *avctx)
Definition: atrac3.c:186
int index
Definition: gxfenc.c:72
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:880
AtracGainInfo g_block[4]
Definition: atrac3.c:57
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
ChannelUnit * units
Definition: atrac3.c:88
static av_cold void init_imdct_window(void)
Definition: atrac3.c:171
float delay_buf2[46]
Definition: atrac3.c:78
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
int num_points
number of gain control points
Definition: atrac.h:36
uint8_t level
Definition: svq3.c:204
static av_cold int atrac3_decode_init(AVCodecContext *avctx)
Definition: atrac3.c:792
float delay_buf3[46]
Definition: atrac3.c:79
common internal api header.
int scrambled_stream
extradata
Definition: atrac3.c:104
#define ff_mdct_end
Definition: fft.h:152
#define INIT_VLC_USE_NEW_STATIC
Definition: vlc.h:55
static VLC spectral_coeff_tab[7]
Definition: atrac3.c:114
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
float delay_buf1[46]
qmf delay buffers
Definition: atrac3.c:77
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
AVCodec ff_atrac3_decoder
Definition: atrac3.c:921
void * priv_data
Definition: avcodec.h:1451
static const float matrix_coeffs[8]
Definition: atrac3data.h:137
int channels
number of audio channels
Definition: avcodec.h:2153
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
#define INTERPOLATE(old, new, nsample)
Definition: atrac3.c:461
void ff_atrac_gain_compensation(AtracGCContext *gctx, float *in, float *prev, AtracGainInfo *gc_now, AtracGainInfo *gc_next, int num_samples, float *out)
Apply gain compensation and perform the MDCT overlapping part.
Definition: atrac.c:84
av_cold void ff_atrac_generate_tables(void)
Generate common tables.
Definition: atrac.c:48
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
FILE * out
Definition: movenc.c:54
int matrix_coeff_index_prev[4]
joint-stereo related variables
Definition: atrac3.c:92
#define VLC_TYPE
Definition: vlc.h:24
static const uint8_t huff_tab_sizes[7]
Definition: atrac3data.h:94
#define FFSWAP(type, a, b)
Definition: common.h:69
ATRAC3 AKA RealAudio 8 compatible decoder data.
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:174
AVFloatDSPContext fdsp
Definition: atrac3.c:109
This structure stores compressed data.
Definition: avcodec.h:1323
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
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:211
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
static int decode_tonal_components(GetBitContext *gb, TonalComponent *components, int num_bands)
Restore the quantized tonal components.
Definition: atrac3.c:323
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:570