FFmpeg  4.3
atrac9dec.c
Go to the documentation of this file.
1 /*
2  * ATRAC9 decoder
3  * Copyright (c) 2018 Rostislav Pehlivanov <atomnuker@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "internal.h"
23 #include "get_bits.h"
24 #include "fft.h"
25 #include "atrac9tab.h"
26 #include "libavutil/lfg.h"
27 #include "libavutil/float_dsp.h"
28 
29 typedef struct ATRAC9ChannelData {
30  int band_ext;
32  int band_ext_data[4];
35 
37  int precision_fine[30];
38  int precision_mask[30];
39 
40  int codebookset[30];
41 
44 
45  DECLARE_ALIGNED(32, float, coeffs )[256];
46  DECLARE_ALIGNED(32, float, prev_win)[128];
48 
49 typedef struct ATRAC9BlockData {
51 
52  /* Base */
56 
57  /* Stereo block only */
59 
60  /* Band extension only */
64 
65  /* Gradient */
66  int grad_mode;
68  int gradient[31];
69 
70  /* Stereo */
72  int is_signs[30];
73 
74  int reuseable;
75 
77 
78 typedef struct ATRAC9Context {
84 
85  /* Set on init */
91 
92  /* Generated on init */
93  VLC sf_vlc[2][8]; /* Signed/unsigned, length */
94  VLC coeff_vlc[2][8][4]; /* Cookbook, precision, cookbook index */
95  uint8_t alloc_curve[48][48];
96  DECLARE_ALIGNED(32, float, imdct_win)[256];
97 
98  DECLARE_ALIGNED(32, float, temp)[256];
100 
102  GetBitContext *gb)
103 {
104  int grad_range[2];
105  int grad_value[2];
106  int values, sign, base;
107  uint8_t *curve;
108  float scale;
109 
110  b->grad_mode = get_bits(gb, 2);
111  if (b->grad_mode) {
112  grad_range[0] = get_bits(gb, 5);
113  grad_range[1] = 31;
114  grad_value[0] = get_bits(gb, 5);
115  grad_value[1] = 31;
116  } else {
117  grad_range[0] = get_bits(gb, 6);
118  grad_range[1] = get_bits(gb, 6) + 1;
119  grad_value[0] = get_bits(gb, 5);
120  grad_value[1] = get_bits(gb, 5);
121  }
122  b->grad_boundary = get_bits(gb, 4);
123 
124  if (grad_range[0] >= grad_range[1] || grad_range[1] > 31)
125  return AVERROR_INVALIDDATA;
126 
127  if (b->grad_boundary > b->q_unit_cnt)
128  return AVERROR_INVALIDDATA;
129 
130  values = grad_value[1] - grad_value[0];
131  sign = 1 - 2*(values < 0);
132  base = grad_value[0] + sign;
133  scale = (FFABS(values) - 1) / 31.0f;
134  curve = s->alloc_curve[grad_range[1] - grad_range[0] - 1];
135 
136  for (int i = 0; i <= b->q_unit_cnt; i++)
137  b->gradient[i] = grad_value[i >= grad_range[0]];
138 
139  for (int i = grad_range[0]; i < grad_range[1]; i++)
140  b->gradient[i] = base + sign*((int)(scale*curve[i - grad_range[0]]));
141 
142  return 0;
143 }
144 
147 {
148  memset(c->precision_mask, 0, sizeof(c->precision_mask));
149  for (int i = 1; i < b->q_unit_cnt; i++) {
150  const int delta = FFABS(c->scalefactors[i] - c->scalefactors[i - 1]) - 1;
151  if (delta > 0) {
152  const int neg = c->scalefactors[i - 1] > c->scalefactors[i];
153  c->precision_mask[i - neg] += FFMIN(delta, 5);
154  }
155  }
156 
157  if (b->grad_mode) {
158  for (int i = 0; i < b->q_unit_cnt; i++) {
159  c->precision_coarse[i] = c->scalefactors[i];
160  c->precision_coarse[i] += c->precision_mask[i] - b->gradient[i];
161  if (c->precision_coarse[i] < 0)
162  continue;
163  switch (b->grad_mode) {
164  case 1:
165  c->precision_coarse[i] >>= 1;
166  break;
167  case 2:
168  c->precision_coarse[i] = (3 * c->precision_coarse[i]) >> 3;
169  break;
170  case 3:
171  c->precision_coarse[i] >>= 2;
172  break;
173  }
174  }
175  } else {
176  for (int i = 0; i < b->q_unit_cnt; i++)
177  c->precision_coarse[i] = c->scalefactors[i] - b->gradient[i];
178  }
179 
180 
181  for (int i = 0; i < b->q_unit_cnt; i++)
182  c->precision_coarse[i] = FFMAX(c->precision_coarse[i], 1);
183 
184  for (int i = 0; i < b->grad_boundary; i++)
185  c->precision_coarse[i]++;
186 
187  for (int i = 0; i < b->q_unit_cnt; i++) {
188  c->precision_fine[i] = 0;
189  if (c->precision_coarse[i] > 15) {
190  c->precision_fine[i] = FFMIN(c->precision_coarse[i], 30) - 15;
191  c->precision_coarse[i] = 15;
192  }
193  }
194 }
195 
197  GetBitContext *gb, int stereo)
198 {
199  int ext_band = 0;
200 
201  if (b->has_band_ext) {
202  if (b->q_unit_cnt < 13 || b->q_unit_cnt > 20)
203  return AVERROR_INVALIDDATA;
204  ext_band = at9_tab_band_ext_group[b->q_unit_cnt - 13][2];
205  if (stereo) {
206  b->channel[1].band_ext = get_bits(gb, 2);
207  b->channel[1].band_ext = ext_band > 2 ? b->channel[1].band_ext : 4;
208  } else {
209  skip_bits1(gb);
210  }
211  }
212 
213  b->has_band_ext_data = get_bits1(gb);
214  if (!b->has_band_ext_data)
215  return 0;
216 
217  if (!b->has_band_ext) {
218  skip_bits(gb, 2);
219  skip_bits_long(gb, get_bits(gb, 5));
220  return 0;
221  }
222 
223  b->channel[0].band_ext = get_bits(gb, 2);
224  b->channel[0].band_ext = ext_band > 2 ? b->channel[0].band_ext : 4;
225 
226  if (!get_bits(gb, 5)) {
227  for (int i = 0; i <= stereo; i++) {
228  ATRAC9ChannelData *c = &b->channel[i];
229  const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
230  for (int j = 0; j < count; j++) {
231  int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
232  c->band_ext_data[j] = av_clip_uintp2_c(c->band_ext_data[j], len);
233  }
234  }
235 
236  return 0;
237  }
238 
239  for (int i = 0; i <= stereo; i++) {
240  ATRAC9ChannelData *c = &b->channel[i];
241  const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
242  for (int j = 0; j < count; j++) {
243  int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
244  c->band_ext_data[j] = get_bits(gb, len);
245  }
246  }
247 
248  return 0;
249 }
250 
253  int channel_idx, int first_in_pkt)
254 {
255  static const uint8_t mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
256  const int mode = mode_map[channel_idx][get_bits(gb, 2)];
257 
258  memset(c->scalefactors, 0, sizeof(c->scalefactors));
259 
260  if (first_in_pkt && (mode == 4 || ((mode == 3) && !channel_idx))) {
261  av_log(s->avctx, AV_LOG_ERROR, "Invalid scalefactor coding mode!\n");
262  return AVERROR_INVALIDDATA;
263  }
264 
265  switch (mode) {
266  case 0: { /* VLC delta offset */
267  const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)];
268  const int base = get_bits(gb, 5);
269  const int len = get_bits(gb, 2) + 3;
270  const VLC *tab = &s->sf_vlc[0][len];
271 
272  c->scalefactors[0] = get_bits(gb, len);
273 
274  for (int i = 1; i < b->band_ext_q_unit; i++) {
275  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
276  c->scalefactors[i] = val & ((1 << len) - 1);
277  }
278 
279  for (int i = 0; i < b->band_ext_q_unit; i++)
280  c->scalefactors[i] += base - sf_weights[i];
281 
282  break;
283  }
284  case 1: { /* CLC offset */
285  const int len = get_bits(gb, 2) + 2;
286  const int base = len < 5 ? get_bits(gb, 5) : 0;
287  for (int i = 0; i < b->band_ext_q_unit; i++)
288  c->scalefactors[i] = base + get_bits(gb, len);
289  break;
290  }
291  case 2:
292  case 4: { /* VLC dist to baseline */
293  const int *baseline = mode == 4 ? c->scalefactors_prev :
294  channel_idx ? b->channel[0].scalefactors :
296  const int baseline_len = mode == 4 ? b->q_unit_cnt_prev :
297  channel_idx ? b->band_ext_q_unit :
298  b->q_unit_cnt_prev;
299 
300  const int len = get_bits(gb, 2) + 2;
301  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
302  const VLC *tab = &s->sf_vlc[1][len];
303 
304  for (int i = 0; i < unit_cnt; i++) {
305  int dist = get_vlc2(gb, tab->table, 9, 2);
306  c->scalefactors[i] = baseline[i] + dist;
307  }
308 
309  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
310  c->scalefactors[i] = get_bits(gb, 5);
311 
312  break;
313  }
314  case 3: { /* VLC offset with baseline */
315  const int *baseline = channel_idx ? b->channel[0].scalefactors :
317  const int baseline_len = channel_idx ? b->band_ext_q_unit :
318  b->q_unit_cnt_prev;
319 
320  const int base = get_bits(gb, 5) - (1 << (5 - 1));
321  const int len = get_bits(gb, 2) + 1;
322  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
323  const VLC *tab = &s->sf_vlc[0][len];
324 
325  c->scalefactors[0] = get_bits(gb, len);
326 
327  for (int i = 1; i < unit_cnt; i++) {
328  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
329  c->scalefactors[i] = val & ((1 << len) - 1);
330  }
331 
332  for (int i = 0; i < unit_cnt; i++)
333  c->scalefactors[i] += base + baseline[i];
334 
335  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
336  c->scalefactors[i] = get_bits(gb, 5);
337  break;
338  }
339  }
340 
341  for (int i = 0; i < b->band_ext_q_unit; i++)
342  if (c->scalefactors[i] < 0 || c->scalefactors[i] > 31)
343  return AVERROR_INVALIDDATA;
344 
345  memcpy(c->scalefactors_prev, c->scalefactors, sizeof(c->scalefactors));
346 
347  return 0;
348 }
349 
352 {
353  int avg = 0;
354  const int last_sf = c->scalefactors[c->q_unit_cnt];
355 
356  memset(c->codebookset, 0, sizeof(c->codebookset));
357 
358  if (c->q_unit_cnt <= 1)
359  return;
360  if (s->samplerate_idx > 7)
361  return;
362 
363  c->scalefactors[c->q_unit_cnt] = c->scalefactors[c->q_unit_cnt - 1];
364 
365  if (c->q_unit_cnt > 12) {
366  for (int i = 0; i < 12; i++)
367  avg += c->scalefactors[i];
368  avg = (avg + 6) / 12;
369  }
370 
371  for (int i = 8; i < c->q_unit_cnt; i++) {
372  const int prev = c->scalefactors[i - 1];
373  const int cur = c->scalefactors[i ];
374  const int next = c->scalefactors[i + 1];
375  const int min = FFMIN(prev, next);
376  if ((cur - min >= 3 || 2*cur - prev - next >= 3))
377  c->codebookset[i] = 1;
378  }
379 
380 
381  for (int i = 12; i < c->q_unit_cnt; i++) {
382  const int cur = c->scalefactors[i];
383  const int cnd = at9_q_unit_to_coeff_cnt[i] == 16;
384  const int min = FFMIN(c->scalefactors[i + 1], c->scalefactors[i - 1]);
385  if (c->codebookset[i])
386  continue;
387 
388  c->codebookset[i] = (((cur - min) >= 2) && (cur >= (avg - cnd)));
389  }
390 
391  c->scalefactors[c->q_unit_cnt] = last_sf;
392 }
393 
396 {
397  const int max_prec = s->samplerate_idx > 7 ? 1 : 7;
398 
399  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
400 
401  for (int i = 0; i < c->q_unit_cnt; i++) {
403  const int bands = at9_q_unit_to_coeff_cnt[i];
404  const int prec = c->precision_coarse[i] + 1;
405 
406  if (prec <= max_prec) {
407  const int cb = c->codebookset[i];
408  const int cbi = at9_q_unit_to_codebookidx[i];
409  const VLC *tab = &s->coeff_vlc[cb][prec][cbi];
410  const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi];
411  const int groups = bands >> huff->value_cnt_pow;
412 
413  for (int j = 0; j < groups; j++) {
414  uint16_t val = get_vlc2(gb, tab->table, 9, huff->max_bit_size);
415 
416  for (int k = 0; k < huff->value_cnt; k++) {
417  coeffs[k] = sign_extend(val, huff->value_bits);
418  val >>= huff->value_bits;
419  }
420 
421  coeffs += huff->value_cnt;
422  }
423  } else {
424  for (int j = 0; j < bands; j++)
425  coeffs[j] = sign_extend(get_bits(gb, prec), prec);
426  }
427  }
428 }
429 
432 {
433  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
434 
435  for (int i = 0; i < c->q_unit_cnt; i++) {
436  const int start = at9_q_unit_to_coeff_idx[i + 0];
437  const int end = at9_q_unit_to_coeff_idx[i + 1];
438  const int len = c->precision_fine[i] + 1;
439 
440  if (c->precision_fine[i] <= 0)
441  continue;
442 
443  for (int j = start; j < end; j++)
444  c->q_coeffs_fine[j] = sign_extend(get_bits(gb, len), len);
445  }
446 }
447 
450 {
451  memset(c->coeffs, 0, sizeof(c->coeffs));
452 
453  for (int i = 0; i < c->q_unit_cnt; i++) {
454  const int start = at9_q_unit_to_coeff_idx[i + 0];
455  const int end = at9_q_unit_to_coeff_idx[i + 1];
456 
457  const float coarse_c = at9_quant_step_coarse[c->precision_coarse[i]];
458  const float fine_c = at9_quant_step_fine[c->precision_fine[i]];
459 
460  for (int j = start; j < end; j++) {
461  const float vc = c->q_coeffs_coarse[j] * coarse_c;
462  const float vf = c->q_coeffs_fine[j] * fine_c;
463  c->coeffs[j] = vc + vf;
464  }
465  }
466 }
467 
469  const int stereo)
470 {
471  float *src = b->channel[ b->cpe_base_channel].coeffs;
472  float *dst = b->channel[!b->cpe_base_channel].coeffs;
473 
474  if (!stereo)
475  return;
476 
477  if (b->q_unit_cnt <= b->stereo_q_unit)
478  return;
479 
480  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++) {
481  const int sign = b->is_signs[i];
482  const int start = at9_q_unit_to_coeff_idx[i + 0];
483  const int end = at9_q_unit_to_coeff_idx[i + 1];
484  for (int j = start; j < end; j++)
485  dst[j] = sign*src[j];
486  }
487 }
488 
490  const int stereo)
491 {
492  for (int i = 0; i <= stereo; i++) {
493  float *coeffs = b->channel[i].coeffs;
494  for (int j = 0; j < b->q_unit_cnt; j++) {
495  const int start = at9_q_unit_to_coeff_idx[j + 0];
496  const int end = at9_q_unit_to_coeff_idx[j + 1];
497  const int scalefactor = b->channel[i].scalefactors[j];
498  const float scale = at9_scalefactor_c[scalefactor];
499  for (int k = start; k < end; k++)
500  coeffs[k] *= scale;
501  }
502  }
503 }
504 
506  int start, int count)
507 {
508  float maxval = 0.0f;
509  for (int i = 0; i < count; i += 2) {
510  double tmp[2];
511  av_bmg_get(&s->lfg, tmp);
512  c->coeffs[start + i + 0] = tmp[0];
513  c->coeffs[start + i + 1] = tmp[1];
514  maxval = FFMAX(FFMAX(FFABS(tmp[0]), FFABS(tmp[1])), maxval);
515  }
516  /* Normalize */
517  for (int i = 0; i < count; i++)
518  c->coeffs[start + i] /= maxval;
519 }
520 
521 static inline void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6],
522  const int s_unit, const int e_unit)
523 {
524  for (int i = s_unit; i < e_unit; i++) {
525  const int start = at9_q_unit_to_coeff_idx[i + 0];
526  const int end = at9_q_unit_to_coeff_idx[i + 1];
527  for (int j = start; j < end; j++)
528  c->coeffs[j] *= sf[i - s_unit];
529  }
530 }
531 
533  const int stereo)
534 {
535  const int g_units[4] = { /* A, B, C, total units */
536  b->q_unit_cnt,
539  FFMAX(g_units[2], 22),
540  };
541 
542  const int g_bins[4] = { /* A, B, C, total bins */
543  at9_q_unit_to_coeff_idx[g_units[0]],
544  at9_q_unit_to_coeff_idx[g_units[1]],
545  at9_q_unit_to_coeff_idx[g_units[2]],
546  at9_q_unit_to_coeff_idx[g_units[3]],
547  };
548 
549  for (int ch = 0; ch <= stereo; ch++) {
550  ATRAC9ChannelData *c = &b->channel[ch];
551 
552  /* Mirror the spectrum */
553  for (int i = 0; i < 3; i++)
554  for (int j = 0; j < (g_bins[i + 1] - g_bins[i + 0]); j++)
555  c->coeffs[g_bins[i] + j] = c->coeffs[g_bins[i] - j - 1];
556 
557  switch (c->band_ext) {
558  case 0: {
559  float sf[6] = { 0.0f };
560  const int l = g_units[3] - g_units[0] - 1;
561  const int n_start = at9_q_unit_to_coeff_idx[g_units[3] - 1];
562  const int n_cnt = at9_q_unit_to_coeff_cnt[g_units[3] - 1];
563  switch (at9_tab_band_ext_group[b->q_unit_cnt - 13][2]) {
564  case 3:
565  sf[0] = at9_band_ext_scales_m0[0][0][c->band_ext_data[0]];
566  sf[1] = at9_band_ext_scales_m0[0][1][c->band_ext_data[0]];
567  sf[2] = at9_band_ext_scales_m0[0][2][c->band_ext_data[1]];
568  sf[3] = at9_band_ext_scales_m0[0][3][c->band_ext_data[2]];
569  sf[4] = at9_band_ext_scales_m0[0][4][c->band_ext_data[3]];
570  break;
571  case 4:
572  sf[0] = at9_band_ext_scales_m0[1][0][c->band_ext_data[0]];
573  sf[1] = at9_band_ext_scales_m0[1][1][c->band_ext_data[0]];
574  sf[2] = at9_band_ext_scales_m0[1][2][c->band_ext_data[1]];
575  sf[3] = at9_band_ext_scales_m0[1][3][c->band_ext_data[2]];
576  sf[4] = at9_band_ext_scales_m0[1][4][c->band_ext_data[3]];
577  break;
578  case 5:
579  sf[0] = at9_band_ext_scales_m0[2][0][c->band_ext_data[0]];
580  sf[1] = at9_band_ext_scales_m0[2][1][c->band_ext_data[1]];
581  sf[2] = at9_band_ext_scales_m0[2][2][c->band_ext_data[1]];
582  break;
583  }
584 
585  sf[l] = at9_scalefactor_c[c->scalefactors[g_units[0]]];
586 
587  fill_with_noise(s, c, n_start, n_cnt);
588  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
589  break;
590  }
591  case 1: {
592  float sf[6];
593  for (int i = g_units[0]; i < g_units[3]; i++)
594  sf[i - g_units[0]] = at9_scalefactor_c[c->scalefactors[i]];
595 
596  fill_with_noise(s, c, g_bins[0], g_bins[3] - g_bins[0]);
597  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
598  break;
599  }
600  case 2: {
601  const float g_sf[2] = {
604  };
605 
606  for (int i = 0; i < 2; i++)
607  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
608  c->coeffs[j] *= g_sf[i];
609  break;
610  }
611  case 3: {
612  float scale = at9_band_ext_scales_m3[c->band_ext_data[0]][0];
613  float rate = at9_band_ext_scales_m3[c->band_ext_data[1]][1];
614  rate = pow(2, rate);
615  for (int i = g_bins[0]; i < g_bins[3]; i++) {
616  scale *= rate;
617  c->coeffs[i] *= scale;
618  }
619  break;
620  }
621  case 4: {
622  const float m = at9_band_ext_scales_m4[c->band_ext_data[0]];
623  const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
624 
625  for (int i = 0; i < 3; i++)
626  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
627  c->coeffs[j] *= g_sf[i];
628  break;
629  }
630  }
631  }
632 }
633 
636  int frame_idx, int block_idx)
637 {
638  const int first_in_pkt = !get_bits1(gb);
639  const int reuse_params = get_bits1(gb);
640  const int stereo = s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_CPE;
641 
642  if (s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_LFE) {
643  ATRAC9ChannelData *c = &b->channel[0];
644  const int precision = reuse_params ? 8 : 4;
645  c->q_unit_cnt = b->q_unit_cnt = 2;
646 
647  memset(c->scalefactors, 0, sizeof(c->scalefactors));
648  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
649  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
650 
651  for (int i = 0; i < b->q_unit_cnt; i++) {
652  c->scalefactors[i] = get_bits(gb, 5);
653  c->precision_coarse[i] = precision;
654  c->precision_fine[i] = 0;
655  }
656 
657  for (int i = 0; i < c->q_unit_cnt; i++) {
658  const int start = at9_q_unit_to_coeff_idx[i + 0];
659  const int end = at9_q_unit_to_coeff_idx[i + 1];
660  for (int j = start; j < end; j++)
661  c->q_coeffs_coarse[j] = get_bits(gb, c->precision_coarse[i] + 1);
662  }
663 
664  dequantize (s, b, c);
665  apply_scalefactors(s, b, 0);
666 
667  goto imdct;
668  }
669 
670  if (first_in_pkt && reuse_params) {
671  av_log(s->avctx, AV_LOG_ERROR, "Invalid block flags!\n");
672  return AVERROR_INVALIDDATA;
673  }
674 
675  /* Band parameters */
676  if (!reuse_params) {
677  int stereo_band, ext_band;
678  const int min_band_count = s->samplerate_idx > 7 ? 1 : 3;
679  b->reuseable = 0;
680  b->band_count = get_bits(gb, 4) + min_band_count;
682 
684 
686  av_log(s->avctx, AV_LOG_ERROR, "Invalid band count %i!\n",
687  b->band_count);
688  return AVERROR_INVALIDDATA;
689  }
690 
691  if (stereo) {
692  stereo_band = get_bits(gb, 4) + min_band_count;
693  if (stereo_band > b->band_count) {
694  av_log(s->avctx, AV_LOG_ERROR, "Invalid stereo band %i!\n",
695  stereo_band);
696  return AVERROR_INVALIDDATA;
697  }
698  b->stereo_q_unit = at9_tab_band_q_unit_map[stereo_band];
699  }
700 
701  b->has_band_ext = get_bits1(gb);
702  if (b->has_band_ext) {
703  ext_band = get_bits(gb, 4) + min_band_count;
704  if (ext_band < b->band_count) {
705  av_log(s->avctx, AV_LOG_ERROR, "Invalid extension band %i!\n",
706  ext_band);
707  return AVERROR_INVALIDDATA;
708  }
710  }
711  b->reuseable = 1;
712  }
713  if (!b->reuseable) {
714  av_log(s->avctx, AV_LOG_ERROR, "invalid block reused!\n");
715  return AVERROR_INVALIDDATA;
716  }
717 
718  /* Calculate bit alloc gradient */
719  if (parse_gradient(s, b, gb))
720  return AVERROR_INVALIDDATA;
721 
722  /* IS data */
723  b->cpe_base_channel = 0;
724  if (stereo) {
725  b->cpe_base_channel = get_bits1(gb);
726  if (get_bits1(gb)) {
727  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++)
728  b->is_signs[i] = 1 - 2*get_bits1(gb);
729  } else {
730  for (int i = 0; i < FF_ARRAY_ELEMS(b->is_signs); i++)
731  b->is_signs[i] = 1;
732  }
733  }
734 
735  /* Band extension */
736  if (parse_band_ext(s, b, gb, stereo))
737  return AVERROR_INVALIDDATA;
738 
739  /* Scalefactors */
740  for (int i = 0; i <= stereo; i++) {
741  ATRAC9ChannelData *c = &b->channel[i];
742  c->q_unit_cnt = i == b->cpe_base_channel ? b->q_unit_cnt :
743  b->stereo_q_unit;
744  if (read_scalefactors(s, b, c, gb, i, first_in_pkt))
745  return AVERROR_INVALIDDATA;
746 
747  calc_precision (s, b, c);
748  calc_codebook_idx (s, b, c);
749  read_coeffs_coarse(s, b, c, gb);
750  read_coeffs_fine (s, b, c, gb);
751  dequantize (s, b, c);
752  }
753 
755 
756  apply_intensity_stereo(s, b, stereo);
757  apply_scalefactors (s, b, stereo);
758 
759  if (b->has_band_ext && b->has_band_ext_data)
760  apply_band_extension (s, b, stereo);
761 
762 imdct:
763  for (int i = 0; i <= stereo; i++) {
764  ATRAC9ChannelData *c = &b->channel[i];
765  const int dst_idx = s->block_config->plane_map[block_idx][i];
766  const int wsize = 1 << s->frame_log2;
767  const ptrdiff_t offset = wsize*frame_idx*sizeof(float);
768  float *dst = (float *)(frame->extended_data[dst_idx] + offset);
769 
770  s->imdct.imdct_half(&s->imdct, s->temp, c->coeffs);
771  s->fdsp->vector_fmul_window(dst, c->prev_win, s->temp,
772  s->imdct_win, wsize >> 1);
773  memcpy(c->prev_win, s->temp + (wsize >> 1), sizeof(float)*wsize >> 1);
774  }
775 
776  return 0;
777 }
778 
779 static int atrac9_decode_frame(AVCodecContext *avctx, void *data,
780  int *got_frame_ptr, AVPacket *avpkt)
781 {
782  int ret;
783  GetBitContext gb;
784  AVFrame *frame = data;
785  ATRAC9Context *s = avctx->priv_data;
786  const int frames = FFMIN(avpkt->size / s->avg_frame_size, s->frame_count);
787 
788  frame->nb_samples = (1 << s->frame_log2) * frames;
789  ret = ff_get_buffer(avctx, frame, 0);
790  if (ret < 0)
791  return ret;
792 
793  init_get_bits8(&gb, avpkt->data, avpkt->size);
794 
795  for (int i = 0; i < frames; i++) {
796  for (int j = 0; j < s->block_config->count; j++) {
797  ret = atrac9_decode_block(s, &gb, &s->block[j], frame, i, j);
798  if (ret)
799  return ret;
800  align_get_bits(&gb);
801  }
802  }
803 
804  *got_frame_ptr = 1;
805 
806  return avctx->block_align;
807 }
808 
810 {
811  ATRAC9Context *s = avctx->priv_data;
812 
813  for (int j = 0; j < s->block_config->count; j++) {
814  ATRAC9BlockData *b = &s->block[j];
815  const int stereo = s->block_config->type[j] == ATRAC9_BLOCK_TYPE_CPE;
816  for (int i = 0; i <= stereo; i++) {
817  ATRAC9ChannelData *c = &b->channel[i];
818  memset(c->prev_win, 0, sizeof(c->prev_win));
819  }
820  }
821 }
822 
824 {
825  ATRAC9Context *s = avctx->priv_data;
826 
827  for (int i = 1; i < 7; i++)
828  ff_free_vlc(&s->sf_vlc[0][i]);
829  for (int i = 2; i < 6; i++)
830  ff_free_vlc(&s->sf_vlc[1][i]);
831  for (int i = 0; i < 2; i++)
832  for (int j = 0; j < 8; j++)
833  for (int k = 0; k < 4; k++)
834  ff_free_vlc(&s->coeff_vlc[i][j][k]);
835 
836  ff_mdct_end(&s->imdct);
837  av_free(s->fdsp);
838 
839  return 0;
840 }
841 
843 {
844  GetBitContext gb;
845  ATRAC9Context *s = avctx->priv_data;
846  int version, block_config_idx, superframe_idx, alloc_c_len;
847 
848  s->avctx = avctx;
849 
850  av_lfg_init(&s->lfg, 0xFBADF00D);
851 
852  if (avctx->block_align <= 0) {
853  av_log(avctx, AV_LOG_ERROR, "Invalid block align\n");
854  return AVERROR_INVALIDDATA;
855  }
856 
857  if (avctx->extradata_size != 12) {
858  av_log(avctx, AV_LOG_ERROR, "Invalid extradata length!\n");
859  return AVERROR_INVALIDDATA;
860  }
861 
862  version = AV_RL32(avctx->extradata);
863  if (version > 2) {
864  av_log(avctx, AV_LOG_ERROR, "Unsupported version (%i)!\n", version);
865  return AVERROR_INVALIDDATA;
866  }
867 
868  init_get_bits8(&gb, avctx->extradata + 4, avctx->extradata_size);
869 
870  if (get_bits(&gb, 8) != 0xFE) {
871  av_log(avctx, AV_LOG_ERROR, "Incorrect magic byte!\n");
872  return AVERROR_INVALIDDATA;
873  }
874 
875  s->samplerate_idx = get_bits(&gb, 4);
877 
878  block_config_idx = get_bits(&gb, 3);
879  if (block_config_idx > 5) {
880  av_log(avctx, AV_LOG_ERROR, "Incorrect block config!\n");
881  return AVERROR_INVALIDDATA;
882  }
883  s->block_config = &at9_block_layout[block_config_idx];
884 
888 
889  if (get_bits1(&gb)) {
890  av_log(avctx, AV_LOG_ERROR, "Incorrect verification bit!\n");
891  return AVERROR_INVALIDDATA;
892  }
893 
894  /* Average frame size in bytes */
895  s->avg_frame_size = get_bits(&gb, 11) + 1;
896 
897  superframe_idx = get_bits(&gb, 2);
898  if (superframe_idx & 1) {
899  av_log(avctx, AV_LOG_ERROR, "Invalid superframe index!\n");
900  return AVERROR_INVALIDDATA;
901  }
902 
903  s->frame_count = 1 << superframe_idx;
905 
906  if (ff_mdct_init(&s->imdct, s->frame_log2 + 1, 1, 1.0f / 32768.0f))
907  return AVERROR(ENOMEM);
908 
910  if (!s->fdsp)
911  return AVERROR(ENOMEM);
912 
913  /* iMDCT window */
914  for (int i = 0; i < (1 << s->frame_log2); i++) {
915  const int len = 1 << s->frame_log2;
916  const float sidx = ( i + 0.5f) / len;
917  const float eidx = (len - i - 0.5f) / len;
918  const float s_c = sinf(sidx*M_PI - M_PI_2)*0.5f + 0.5f;
919  const float e_c = sinf(eidx*M_PI - M_PI_2)*0.5f + 0.5f;
920  s->imdct_win[i] = s_c / ((s_c * s_c) + (e_c * e_c));
921  }
922 
923  /* Allocation curve */
924  alloc_c_len = FF_ARRAY_ELEMS(at9_tab_b_dist);
925  for (int i = 1; i <= alloc_c_len; i++)
926  for (int j = 0; j < i; j++)
927  s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
928 
929  /* Unsigned scalefactor VLCs */
930  for (int i = 1; i < 7; i++) {
932 
933  init_vlc(&s->sf_vlc[0][i], 9, hf->size, hf->bits, 1, 1, hf->codes,
934  2, 2, 0);
935  }
936 
937  /* Signed scalefactor VLCs */
938  for (int i = 2; i < 6; i++) {
940 
941  int nums = hf->size;
942  int16_t sym[32];
943  for (int j = 0; j < nums; j++)
944  sym[j] = sign_extend(j, hf->value_bits);
945 
946  ff_init_vlc_sparse(&s->sf_vlc[1][i], 9, hf->size, hf->bits, 1, 1,
947  hf->codes, 2, 2, sym, sizeof(*sym), sizeof(*sym), 0);
948  }
949 
950  /* Coefficient VLCs */
951  for (int i = 0; i < 2; i++) {
952  for (int j = 0; j < 8; j++) {
953  for (int k = 0; k < 4; k++) {
954  const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
955  init_vlc(&s->coeff_vlc[i][j][k], 9, hf->size, hf->bits, 1, 1,
956  hf->codes, 2, 2, 0);
957  }
958  }
959  }
960 
961  return 0;
962 }
963 
965  .name = "atrac9",
966  .long_name = NULL_IF_CONFIG_SMALL("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
967  .type = AVMEDIA_TYPE_AUDIO,
968  .id = AV_CODEC_ID_ATRAC9,
969  .priv_data_size = sizeof(ATRAC9Context),
971  .close = atrac9_decode_close,
975  .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
976 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
static const uint8_t at9_tab_sri_max_bands[]
Definition: atrac9tab.h:112
float, planar
Definition: samplefmt.h:69
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
version
Definition: libkvazaar.c:292
AVCodec ff_atrac9_decoder
Definition: atrac9dec.c:964
int precision_mask[30]
Definition: atrac9dec.c:38
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
int precision_coarse[30]
Definition: atrac9dec.c:36
static void apply_band_extension(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:532
static void flush(AVCodecContext *avctx)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static void dequantize(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:448
else temp
Definition: vf_mcdeint.c:256
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
int band_ext_data[4]
Definition: atrac9dec.c:32
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int ff_init_vlc_sparse(VLC *vlc_arg, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:273
int size
Definition: packet.h:356
const char * b
Definition: vf_curves.c:116
int q_unit_cnt_prev
Definition: atrac9dec.c:55
float prev_win[128]
Definition: atrac9dec.c:46
static void read_coeffs_coarse(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
Definition: atrac9dec.c:394
VLC coeff_vlc[2][8][4]
Definition: atrac9dec.c:94
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
float coeffs[256]
Definition: atrac9dec.c:45
AVCodec.
Definition: codec.h:190
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1223
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
int is_signs[30]
Definition: atrac9dec.c:72
AVCodecContext * avctx
Definition: atrac9dec.c:79
uint8_t base
Definition: vp3data.h:202
static const float at9_band_ext_scales_m4[]
Definition: atrac9tab.h:301
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
uint64_t channel_layout
Definition: atrac9tab.h:36
static int16_t block[64]
Definition: dct.c:115
const int max_bit_size
Definition: atrac9tab.h:479
static const HuffmanCodebook at9_huffman_coeffs[][8][4]
Definition: atrac9tab.h:1537
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:215
static const int at9_tab_samplerates[]
Definition: atrac9tab.h:116
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
ATRAC9ChannelData channel[2]
Definition: atrac9dec.c:50
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
uint8_t
#define av_cold
Definition: attributes.h:88
float delta
int avg_frame_size
Definition: atrac9dec.c:87
static const OptionGroupDef groups[]
Definition: ffmpeg_opt.c:3273
#define f(width, name)
Definition: cbs_vp9.c:255
float imdct_win[256]
Definition: atrac9dec.c:96
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
static AVFrame * frame
const char data[16]
Definition: mxf.c:91
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
uint8_t * data
Definition: packet.h:355
bitstream reader API header.
static void fill_with_noise(ATRAC9Context *s, ATRAC9ChannelData *c, int start, int count)
Definition: atrac9dec.c:505
int frame_count
Definition: atrac9dec.c:88
static int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb, int channel_idx, int first_in_pkt)
Definition: atrac9dec.c:251
void av_bmg_get(AVLFG *lfg, double out[2])
Get the next two numbers generated by a Box-Muller Gaussian generator using the random numbers issued...
Definition: lfg.c:49
static void apply_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:489
static const uint8_t at9_tab_sf_weights[][32]
Definition: atrac9tab.h:335
int32_t q_coeffs_coarse[256]
Definition: atrac9dec.c:42
#define av_log(a,...)
static void calc_precision(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:145
static int atrac9_decode_block(ATRAC9Context *s, GetBitContext *gb, ATRAC9BlockData *b, AVFrame *frame, int frame_idx, int block_idx)
Definition: atrac9dec.c:634
static const float at9_quant_step_fine[]
Definition: atrac9tab.h:315
const uint8_t * bits
Definition: atrac9tab.h:473
#define src
Definition: vp8dsp.c:254
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition: float_dsp.h:119
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void read_coeffs_fine(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
Definition: atrac9dec.c:430
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
static const uint8_t at9_tab_band_q_unit_map[]
Definition: atrac9tab.h:93
#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:186
const uint8_t at9_q_unit_to_codebookidx[]
Definition: atrac9tab.h:107
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
static int parse_band_ext(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb, int stereo)
Definition: atrac9dec.c:196
const char * name
Name of the codec implementation.
Definition: codec.h:197
const int size
Definition: atrac9tab.h:475
static void calc_codebook_idx(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:350
#define ff_mdct_init
Definition: fft.h:169
int has_band_ext_data
Definition: atrac9dec.c:62
static const uint8_t offset[127][2]
Definition: vf_spp.c:93
VLC sf_vlc[2][8]
Definition: atrac9dec.c:93
#define FFMAX(a, b)
Definition: common.h:94
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
FFTContext imdct
Definition: atrac9dec.c:81
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
enum ATRAC9BlockType type[5]
Definition: atrac9tab.h:37
static VLC sf_vlc
scale factor DPCM vlc
Definition: wmaprodec.c:128
int frame_log2
Definition: atrac9dec.c:86
Definition: fft.h:88
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:333
#define FFMIN(a, b)
Definition: common.h:96
int32_t scalefactors_prev[31]
Definition: atrac9dec.c:34
int band_ext_q_unit
Definition: atrac9dec.c:63
#define M_PI_2
Definition: mathematics.h:55
int32_t
static const float at9_band_ext_scales_m0[][5][32]
Definition: atrac9tab.h:184
ATRAC9BlockData block[5]
Definition: atrac9dec.c:82
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
static const float at9_scalefactor_c[]
Definition: atrac9tab.h:324
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
#define AV_RL32
Definition: intreadwrite.h:146
int frames
Definition: movenc.c:65
#define FF_ARRAY_ELEMS(a)
static int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb)
Definition: atrac9dec.c:101
static av_cold int atrac9_decode_init(AVCodecContext *avctx)
Definition: atrac9dec.c:842
#define sinf(x)
Definition: libm.h:419
const int value_bits
Definition: atrac9tab.h:478
int sample_rate
samples per second
Definition: avcodec.h:1186
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
int cpe_base_channel
Definition: atrac9dec.c:71
main external API structure.
Definition: avcodec.h:526
const int value_cnt_pow
Definition: atrac9tab.h:477
static const float bands[]
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
int extradata_size
Definition: avcodec.h:628
static void apply_intensity_stereo(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:468
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static const uint8_t at9_q_unit_to_coeff_cnt[]
Definition: atrac9tab.h:97
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
int precision_fine[30]
Definition: atrac9dec.c:37
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: codec.h:93
uint8_t alloc_curve[48][48]
Definition: atrac9dec.c:95
int samplerate_idx
Definition: atrac9dec.c:89
static av_cold int atrac9_decode_close(AVCodecContext *avctx)
Definition: atrac9dec.c:823
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
static const uint8_t at9_tab_band_ext_group[][3]
Definition: atrac9tab.h:130
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:108
static const uint8_t at9_tab_band_ext_cnt[][6]
Definition: atrac9tab.h:121
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
static const float at9_band_ext_scales_m2[]
Definition: atrac9tab.h:271
static void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6], const int s_unit, const int e_unit)
Definition: atrac9dec.c:521
AVFloatDSPContext * fdsp
Definition: atrac9dec.c:80
static const float at9_quant_step_coarse[]
Definition: atrac9tab.h:306
#define avg(a, b, c, d)
static int atrac9_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: atrac9dec.c:779
static const HuffmanCodebook at9_huffman_sf_unsigned[]
Definition: atrac9tab.h:482
common internal api header.
const ATRAC9BlockConfig * block_config
Definition: atrac9dec.c:90
#define ff_mdct_end
Definition: fft.h:170
const int value_cnt
Definition: atrac9tab.h:476
static double c[64]
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
static const uint8_t at9_tab_sri_frame_log2[]
Definition: atrac9tab.h:89
int codebookset[30]
Definition: atrac9dec.c:40
int plane_map[5][2]
Definition: atrac9tab.h:38
static const float at9_band_ext_scales_m3[][2]
Definition: atrac9tab.h:290
const uint16_t * codes
Definition: atrac9tab.h:474
void * priv_data
Definition: avcodec.h:553
#define av_free(p)
static const HuffmanCodebook at9_huffman_sf_signed[]
Definition: atrac9tab.h:492
int gradient[31]
Definition: atrac9dec.c:68
int len
int channels
number of audio channels
Definition: avcodec.h:1187
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:693
static const struct twinvq_data tab
int32_t scalefactors[31]
Definition: atrac9dec.c:33
static const ATRAC9BlockConfig at9_block_layout[]
Definition: atrac9tab.h:42
static const uint8_t at9_tab_b_dist[]
Definition: atrac9tab.h:370
static void atrac9_decode_flush(AVCodecContext *avctx)
Definition: atrac9dec.c:809
#define M_PI
Definition: mathematics.h:52
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:347
float min
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:332
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:366
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
static const int at9_q_unit_to_coeff_idx[]
Definition: atrac9tab.h:102
int32_t q_coeffs_fine[256]
Definition: atrac9dec.c:43
for(j=16;j >0;--j)
static const uint8_t at9_tab_band_ext_lengths[][6][4]
Definition: atrac9tab.h:141
float temp[256]
Definition: atrac9dec.c:98
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.
Definition: common.h:229
static uint8_t tmp[11]
Definition: aes_ctr.c:26