FFmpeg
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 */
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 (grad_value[0] > 31 || grad_value[1] > 31)
128  return AVERROR_INVALIDDATA;
129 
130  if (b->grad_boundary > b->q_unit_cnt)
131  return AVERROR_INVALIDDATA;
132 
133  values = grad_value[1] - grad_value[0];
134  sign = 1 - 2*(values < 0);
135  base = grad_value[0] + sign;
136  scale = (FFABS(values) - 1) / 31.0f;
137  curve = s->alloc_curve[grad_range[1] - grad_range[0] - 1];
138 
139  for (int i = 0; i <= b->q_unit_cnt; i++)
140  b->gradient[i] = grad_value[i >= grad_range[0]];
141 
142  for (int i = grad_range[0]; i < grad_range[1]; i++)
143  b->gradient[i] = base + sign*((int)(scale*curve[i - grad_range[0]]));
144 
145  return 0;
146 }
147 
150 {
151  memset(c->precision_mask, 0, sizeof(c->precision_mask));
152  for (int i = 1; i < b->q_unit_cnt; i++) {
153  const int delta = FFABS(c->scalefactors[i] - c->scalefactors[i - 1]) - 1;
154  if (delta > 0) {
155  const int neg = c->scalefactors[i - 1] > c->scalefactors[i];
156  c->precision_mask[i - neg] += FFMIN(delta, 5);
157  }
158  }
159 
160  if (b->grad_mode) {
161  for (int i = 0; i < b->q_unit_cnt; i++) {
162  c->precision_coarse[i] = c->scalefactors[i];
163  c->precision_coarse[i] += c->precision_mask[i] - b->gradient[i];
164  if (c->precision_coarse[i] < 0)
165  continue;
166  switch (b->grad_mode) {
167  case 1:
168  c->precision_coarse[i] >>= 1;
169  break;
170  case 2:
171  c->precision_coarse[i] = (3 * c->precision_coarse[i]) >> 3;
172  break;
173  case 3:
174  c->precision_coarse[i] >>= 2;
175  break;
176  }
177  }
178  } else {
179  for (int i = 0; i < b->q_unit_cnt; i++)
180  c->precision_coarse[i] = c->scalefactors[i] - b->gradient[i];
181  }
182 
183 
184  for (int i = 0; i < b->q_unit_cnt; i++)
185  c->precision_coarse[i] = FFMAX(c->precision_coarse[i], 1);
186 
187  for (int i = 0; i < b->grad_boundary; i++)
188  c->precision_coarse[i]++;
189 
190  for (int i = 0; i < b->q_unit_cnt; i++) {
191  c->precision_fine[i] = 0;
192  if (c->precision_coarse[i] > 15) {
193  c->precision_fine[i] = FFMIN(c->precision_coarse[i], 30) - 15;
194  c->precision_coarse[i] = 15;
195  }
196  }
197 }
198 
200  GetBitContext *gb, int stereo)
201 {
202  int ext_band = 0;
203 
204  if (b->has_band_ext) {
205  if (b->q_unit_cnt < 13 || b->q_unit_cnt > 20)
206  return AVERROR_INVALIDDATA;
207  ext_band = at9_tab_band_ext_group[b->q_unit_cnt - 13][2];
208  if (stereo) {
209  b->channel[1].band_ext = get_bits(gb, 2);
210  b->channel[1].band_ext = ext_band > 2 ? b->channel[1].band_ext : 4;
211  } else {
212  skip_bits1(gb);
213  }
214  }
215 
216  b->has_band_ext_data = get_bits1(gb);
217  if (!b->has_band_ext_data)
218  return 0;
219 
220  if (!b->has_band_ext) {
221  skip_bits(gb, 2);
222  skip_bits_long(gb, get_bits(gb, 5));
223  return 0;
224  }
225 
226  b->channel[0].band_ext = get_bits(gb, 2);
227  b->channel[0].band_ext = ext_band > 2 ? b->channel[0].band_ext : 4;
228 
229  if (!get_bits(gb, 5)) {
230  for (int i = 0; i <= stereo; i++) {
231  ATRAC9ChannelData *c = &b->channel[i];
232  const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
233  for (int j = 0; j < count; j++) {
234  int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
235  c->band_ext_data[j] = av_clip_uintp2_c(c->band_ext_data[j], len);
236  }
237  }
238 
239  return 0;
240  }
241 
242  for (int i = 0; i <= stereo; i++) {
243  ATRAC9ChannelData *c = &b->channel[i];
244  const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
245  for (int j = 0; j < count; j++) {
246  int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
247  c->band_ext_data[j] = get_bits(gb, len);
248  }
249  }
250 
251  return 0;
252 }
253 
256  int channel_idx, int first_in_pkt)
257 {
258  static const uint8_t mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
259  const int mode = mode_map[channel_idx][get_bits(gb, 2)];
260 
261  memset(c->scalefactors, 0, sizeof(c->scalefactors));
262 
263  if (first_in_pkt && (mode == 4 || ((mode == 3) && !channel_idx))) {
264  av_log(s->avctx, AV_LOG_ERROR, "Invalid scalefactor coding mode!\n");
265  return AVERROR_INVALIDDATA;
266  }
267 
268  switch (mode) {
269  case 0: { /* VLC delta offset */
270  const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)];
271  const int base = get_bits(gb, 5);
272  const int len = get_bits(gb, 2) + 3;
273  const VLC *tab = &s->sf_vlc[0][len];
274 
275  c->scalefactors[0] = get_bits(gb, len);
276 
277  for (int i = 1; i < b->band_ext_q_unit; i++) {
278  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
279  c->scalefactors[i] = val & ((1 << len) - 1);
280  }
281 
282  for (int i = 0; i < b->band_ext_q_unit; i++)
283  c->scalefactors[i] += base - sf_weights[i];
284 
285  break;
286  }
287  case 1: { /* CLC offset */
288  const int len = get_bits(gb, 2) + 2;
289  const int base = len < 5 ? get_bits(gb, 5) : 0;
290  for (int i = 0; i < b->band_ext_q_unit; i++)
291  c->scalefactors[i] = base + get_bits(gb, len);
292  break;
293  }
294  case 2:
295  case 4: { /* VLC dist to baseline */
296  const int *baseline = mode == 4 ? c->scalefactors_prev :
297  channel_idx ? b->channel[0].scalefactors :
298  c->scalefactors_prev;
299  const int baseline_len = mode == 4 ? b->q_unit_cnt_prev :
300  channel_idx ? b->band_ext_q_unit :
301  b->q_unit_cnt_prev;
302 
303  const int len = get_bits(gb, 2) + 2;
304  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
305  const VLC *tab = &s->sf_vlc[1][len];
306 
307  for (int i = 0; i < unit_cnt; i++) {
308  int dist = get_vlc2(gb, tab->table, 9, 2);
309  c->scalefactors[i] = baseline[i] + dist;
310  }
311 
312  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
313  c->scalefactors[i] = get_bits(gb, 5);
314 
315  break;
316  }
317  case 3: { /* VLC offset with baseline */
318  const int *baseline = channel_idx ? b->channel[0].scalefactors :
319  c->scalefactors_prev;
320  const int baseline_len = channel_idx ? b->band_ext_q_unit :
321  b->q_unit_cnt_prev;
322 
323  const int base = get_bits(gb, 5) - (1 << (5 - 1));
324  const int len = get_bits(gb, 2) + 1;
325  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
326  const VLC *tab = &s->sf_vlc[0][len];
327 
328  c->scalefactors[0] = get_bits(gb, len);
329 
330  for (int i = 1; i < unit_cnt; i++) {
331  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
332  c->scalefactors[i] = val & ((1 << len) - 1);
333  }
334 
335  for (int i = 0; i < unit_cnt; i++)
336  c->scalefactors[i] += base + baseline[i];
337 
338  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
339  c->scalefactors[i] = get_bits(gb, 5);
340  break;
341  }
342  }
343 
344  for (int i = 0; i < b->band_ext_q_unit; i++)
345  if (c->scalefactors[i] < 0 || c->scalefactors[i] > 31)
346  return AVERROR_INVALIDDATA;
347 
348  memcpy(c->scalefactors_prev, c->scalefactors, sizeof(c->scalefactors));
349 
350  return 0;
351 }
352 
355 {
356  int avg = 0;
357  const int last_sf = c->scalefactors[c->q_unit_cnt];
358 
359  memset(c->codebookset, 0, sizeof(c->codebookset));
360 
361  if (c->q_unit_cnt <= 1)
362  return;
363  if (s->samplerate_idx > 7)
364  return;
365 
366  c->scalefactors[c->q_unit_cnt] = c->scalefactors[c->q_unit_cnt - 1];
367 
368  if (c->q_unit_cnt > 12) {
369  for (int i = 0; i < 12; i++)
370  avg += c->scalefactors[i];
371  avg = (avg + 6) / 12;
372  }
373 
374  for (int i = 8; i < c->q_unit_cnt; i++) {
375  const int prev = c->scalefactors[i - 1];
376  const int cur = c->scalefactors[i ];
377  const int next = c->scalefactors[i + 1];
378  const int min = FFMIN(prev, next);
379  if ((cur - min >= 3 || 2*cur - prev - next >= 3))
380  c->codebookset[i] = 1;
381  }
382 
383 
384  for (int i = 12; i < c->q_unit_cnt; i++) {
385  const int cur = c->scalefactors[i];
386  const int cnd = at9_q_unit_to_coeff_cnt[i] == 16;
387  const int min = FFMIN(c->scalefactors[i + 1], c->scalefactors[i - 1]);
388  if (c->codebookset[i])
389  continue;
390 
391  c->codebookset[i] = (((cur - min) >= 2) && (cur >= (avg - cnd)));
392  }
393 
394  c->scalefactors[c->q_unit_cnt] = last_sf;
395 }
396 
399 {
400  const int max_prec = s->samplerate_idx > 7 ? 1 : 7;
401 
402  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
403 
404  for (int i = 0; i < c->q_unit_cnt; i++) {
405  int *coeffs = &c->q_coeffs_coarse[at9_q_unit_to_coeff_idx[i]];
406  const int bands = at9_q_unit_to_coeff_cnt[i];
407  const int prec = c->precision_coarse[i] + 1;
408 
409  if (prec <= max_prec) {
410  const int cb = c->codebookset[i];
411  const int cbi = at9_q_unit_to_codebookidx[i];
412  const VLC *tab = &s->coeff_vlc[cb][prec][cbi];
413  const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi];
414  const int groups = bands >> huff->value_cnt_pow;
415 
416  for (int j = 0; j < groups; j++) {
417  uint16_t val = get_vlc2(gb, tab->table, 9, huff->max_bit_size);
418 
419  for (int k = 0; k < huff->value_cnt; k++) {
420  coeffs[k] = sign_extend(val, huff->value_bits);
421  val >>= huff->value_bits;
422  }
423 
424  coeffs += huff->value_cnt;
425  }
426  } else {
427  for (int j = 0; j < bands; j++)
428  coeffs[j] = sign_extend(get_bits(gb, prec), prec);
429  }
430  }
431 }
432 
435 {
436  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
437 
438  for (int i = 0; i < c->q_unit_cnt; i++) {
439  const int start = at9_q_unit_to_coeff_idx[i + 0];
440  const int end = at9_q_unit_to_coeff_idx[i + 1];
441  const int len = c->precision_fine[i] + 1;
442 
443  if (c->precision_fine[i] <= 0)
444  continue;
445 
446  for (int j = start; j < end; j++)
447  c->q_coeffs_fine[j] = sign_extend(get_bits(gb, len), len);
448  }
449 }
450 
453 {
454  memset(c->coeffs, 0, sizeof(c->coeffs));
455 
456  for (int i = 0; i < c->q_unit_cnt; i++) {
457  const int start = at9_q_unit_to_coeff_idx[i + 0];
458  const int end = at9_q_unit_to_coeff_idx[i + 1];
459 
460  const float coarse_c = at9_quant_step_coarse[c->precision_coarse[i]];
461  const float fine_c = at9_quant_step_fine[c->precision_fine[i]];
462 
463  for (int j = start; j < end; j++) {
464  const float vc = c->q_coeffs_coarse[j] * coarse_c;
465  const float vf = c->q_coeffs_fine[j] * fine_c;
466  c->coeffs[j] = vc + vf;
467  }
468  }
469 }
470 
472  const int stereo)
473 {
474  float *src = b->channel[ b->cpe_base_channel].coeffs;
475  float *dst = b->channel[!b->cpe_base_channel].coeffs;
476 
477  if (!stereo)
478  return;
479 
480  if (b->q_unit_cnt <= b->stereo_q_unit)
481  return;
482 
483  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++) {
484  const int sign = b->is_signs[i];
485  const int start = at9_q_unit_to_coeff_idx[i + 0];
486  const int end = at9_q_unit_to_coeff_idx[i + 1];
487  for (int j = start; j < end; j++)
488  dst[j] = sign*src[j];
489  }
490 }
491 
493  const int stereo)
494 {
495  for (int i = 0; i <= stereo; i++) {
496  float *coeffs = b->channel[i].coeffs;
497  for (int j = 0; j < b->q_unit_cnt; j++) {
498  const int start = at9_q_unit_to_coeff_idx[j + 0];
499  const int end = at9_q_unit_to_coeff_idx[j + 1];
500  const int scalefactor = b->channel[i].scalefactors[j];
501  const float scale = at9_scalefactor_c[scalefactor];
502  for (int k = start; k < end; k++)
503  coeffs[k] *= scale;
504  }
505  }
506 }
507 
509  int start, int count)
510 {
511  float maxval = 0.0f;
512  for (int i = 0; i < count; i += 2) {
513  double tmp[2];
514  av_bmg_get(&s->lfg, tmp);
515  c->coeffs[start + i + 0] = tmp[0];
516  c->coeffs[start + i + 1] = tmp[1];
517  maxval = FFMAX(FFMAX(FFABS(tmp[0]), FFABS(tmp[1])), maxval);
518  }
519  /* Normalize */
520  for (int i = 0; i < count; i++)
521  c->coeffs[start + i] /= maxval;
522 }
523 
524 static inline void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6],
525  const int s_unit, const int e_unit)
526 {
527  for (int i = s_unit; i < e_unit; i++) {
528  const int start = at9_q_unit_to_coeff_idx[i + 0];
529  const int end = at9_q_unit_to_coeff_idx[i + 1];
530  for (int j = start; j < end; j++)
531  c->coeffs[j] *= sf[i - s_unit];
532  }
533 }
534 
536  const int stereo)
537 {
538  const int g_units[4] = { /* A, B, C, total units */
539  b->q_unit_cnt,
540  at9_tab_band_ext_group[b->q_unit_cnt - 13][0],
541  at9_tab_band_ext_group[b->q_unit_cnt - 13][1],
542  FFMAX(g_units[2], 22),
543  };
544 
545  const int g_bins[4] = { /* A, B, C, total bins */
546  at9_q_unit_to_coeff_idx[g_units[0]],
547  at9_q_unit_to_coeff_idx[g_units[1]],
548  at9_q_unit_to_coeff_idx[g_units[2]],
549  at9_q_unit_to_coeff_idx[g_units[3]],
550  };
551 
552  for (int ch = 0; ch <= stereo; ch++) {
553  ATRAC9ChannelData *c = &b->channel[ch];
554 
555  /* Mirror the spectrum */
556  for (int i = 0; i < 3; i++)
557  for (int j = 0; j < (g_bins[i + 1] - g_bins[i + 0]); j++)
558  c->coeffs[g_bins[i] + j] = c->coeffs[g_bins[i] - j - 1];
559 
560  switch (c->band_ext) {
561  case 0: {
562  float sf[6] = { 0.0f };
563  const int l = g_units[3] - g_units[0] - 1;
564  const int n_start = at9_q_unit_to_coeff_idx[g_units[3] - 1];
565  const int n_cnt = at9_q_unit_to_coeff_cnt[g_units[3] - 1];
566  switch (at9_tab_band_ext_group[b->q_unit_cnt - 13][2]) {
567  case 3:
568  sf[0] = at9_band_ext_scales_m0[0][0][c->band_ext_data[0]];
569  sf[1] = at9_band_ext_scales_m0[0][1][c->band_ext_data[0]];
570  sf[2] = at9_band_ext_scales_m0[0][2][c->band_ext_data[1]];
571  sf[3] = at9_band_ext_scales_m0[0][3][c->band_ext_data[2]];
572  sf[4] = at9_band_ext_scales_m0[0][4][c->band_ext_data[3]];
573  break;
574  case 4:
575  sf[0] = at9_band_ext_scales_m0[1][0][c->band_ext_data[0]];
576  sf[1] = at9_band_ext_scales_m0[1][1][c->band_ext_data[0]];
577  sf[2] = at9_band_ext_scales_m0[1][2][c->band_ext_data[1]];
578  sf[3] = at9_band_ext_scales_m0[1][3][c->band_ext_data[2]];
579  sf[4] = at9_band_ext_scales_m0[1][4][c->band_ext_data[3]];
580  break;
581  case 5:
582  sf[0] = at9_band_ext_scales_m0[2][0][c->band_ext_data[0]];
583  sf[1] = at9_band_ext_scales_m0[2][1][c->band_ext_data[1]];
584  sf[2] = at9_band_ext_scales_m0[2][2][c->band_ext_data[1]];
585  break;
586  }
587 
588  sf[l] = at9_scalefactor_c[c->scalefactors[g_units[0]]];
589 
590  fill_with_noise(s, c, n_start, n_cnt);
591  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
592  break;
593  }
594  case 1: {
595  float sf[6];
596  for (int i = g_units[0]; i < g_units[3]; i++)
597  sf[i - g_units[0]] = at9_scalefactor_c[c->scalefactors[i]];
598 
599  fill_with_noise(s, c, g_bins[0], g_bins[3] - g_bins[0]);
600  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
601  break;
602  }
603  case 2: {
604  const float g_sf[2] = {
605  at9_band_ext_scales_m2[c->band_ext_data[0]],
606  at9_band_ext_scales_m2[c->band_ext_data[1]],
607  };
608 
609  for (int i = 0; i < 2; i++)
610  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
611  c->coeffs[j] *= g_sf[i];
612  break;
613  }
614  case 3: {
615  float scale = at9_band_ext_scales_m3[c->band_ext_data[0]][0];
616  float rate = at9_band_ext_scales_m3[c->band_ext_data[1]][1];
617  rate = pow(2, rate);
618  for (int i = g_bins[0]; i < g_bins[3]; i++) {
619  scale *= rate;
620  c->coeffs[i] *= scale;
621  }
622  break;
623  }
624  case 4: {
625  const float m = at9_band_ext_scales_m4[c->band_ext_data[0]];
626  const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
627 
628  for (int i = 0; i < 3; i++)
629  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
630  c->coeffs[j] *= g_sf[i];
631  break;
632  }
633  }
634  }
635 }
636 
639  int frame_idx, int block_idx)
640 {
641  const int first_in_pkt = !get_bits1(gb);
642  const int reuse_params = get_bits1(gb);
643  const int stereo = s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_CPE;
644 
645  if (s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_LFE) {
646  ATRAC9ChannelData *c = &b->channel[0];
647  const int precision = reuse_params ? 8 : 4;
648  c->q_unit_cnt = b->q_unit_cnt = 2;
649 
650  memset(c->scalefactors, 0, sizeof(c->scalefactors));
651  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
652  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
653 
654  for (int i = 0; i < b->q_unit_cnt; i++) {
655  c->scalefactors[i] = get_bits(gb, 5);
656  c->precision_coarse[i] = precision;
657  c->precision_fine[i] = 0;
658  }
659 
660  for (int i = 0; i < c->q_unit_cnt; i++) {
661  const int start = at9_q_unit_to_coeff_idx[i + 0];
662  const int end = at9_q_unit_to_coeff_idx[i + 1];
663  for (int j = start; j < end; j++)
664  c->q_coeffs_coarse[j] = get_bits(gb, c->precision_coarse[i] + 1);
665  }
666 
667  dequantize (s, b, c);
668  apply_scalefactors(s, b, 0);
669 
670  goto imdct;
671  }
672 
673  if (first_in_pkt && reuse_params) {
674  av_log(s->avctx, AV_LOG_ERROR, "Invalid block flags!\n");
675  return AVERROR_INVALIDDATA;
676  }
677 
678  /* Band parameters */
679  if (!reuse_params) {
680  int stereo_band, ext_band;
681  const int min_band_count = s->samplerate_idx > 7 ? 1 : 3;
682  b->reuseable = 0;
683  b->band_count = get_bits(gb, 4) + min_band_count;
684  b->q_unit_cnt = at9_tab_band_q_unit_map[b->band_count];
685 
686  b->band_ext_q_unit = b->stereo_q_unit = b->q_unit_cnt;
687 
688  if (b->band_count > at9_tab_sri_max_bands[s->samplerate_idx]) {
689  av_log(s->avctx, AV_LOG_ERROR, "Invalid band count %i!\n",
690  b->band_count);
691  return AVERROR_INVALIDDATA;
692  }
693 
694  if (stereo) {
695  stereo_band = get_bits(gb, 4) + min_band_count;
696  if (stereo_band > b->band_count) {
697  av_log(s->avctx, AV_LOG_ERROR, "Invalid stereo band %i!\n",
698  stereo_band);
699  return AVERROR_INVALIDDATA;
700  }
701  b->stereo_q_unit = at9_tab_band_q_unit_map[stereo_band];
702  }
703 
704  b->has_band_ext = get_bits1(gb);
705  if (b->has_band_ext) {
706  ext_band = get_bits(gb, 4) + min_band_count;
707  if (ext_band < b->band_count) {
708  av_log(s->avctx, AV_LOG_ERROR, "Invalid extension band %i!\n",
709  ext_band);
710  return AVERROR_INVALIDDATA;
711  }
712  b->band_ext_q_unit = at9_tab_band_q_unit_map[ext_band];
713  }
714  b->reuseable = 1;
715  }
716  if (!b->reuseable) {
717  av_log(s->avctx, AV_LOG_ERROR, "invalid block reused!\n");
718  return AVERROR_INVALIDDATA;
719  }
720 
721  /* Calculate bit alloc gradient */
722  if (parse_gradient(s, b, gb))
723  return AVERROR_INVALIDDATA;
724 
725  /* IS data */
726  b->cpe_base_channel = 0;
727  if (stereo) {
728  b->cpe_base_channel = get_bits1(gb);
729  if (get_bits1(gb)) {
730  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++)
731  b->is_signs[i] = 1 - 2*get_bits1(gb);
732  } else {
733  for (int i = 0; i < FF_ARRAY_ELEMS(b->is_signs); i++)
734  b->is_signs[i] = 1;
735  }
736  }
737 
738  /* Band extension */
739  if (parse_band_ext(s, b, gb, stereo))
740  return AVERROR_INVALIDDATA;
741 
742  /* Scalefactors */
743  for (int i = 0; i <= stereo; i++) {
744  ATRAC9ChannelData *c = &b->channel[i];
745  c->q_unit_cnt = i == b->cpe_base_channel ? b->q_unit_cnt :
746  b->stereo_q_unit;
747  if (read_scalefactors(s, b, c, gb, i, first_in_pkt))
748  return AVERROR_INVALIDDATA;
749 
750  calc_precision (s, b, c);
751  calc_codebook_idx (s, b, c);
752  read_coeffs_coarse(s, b, c, gb);
753  read_coeffs_fine (s, b, c, gb);
754  dequantize (s, b, c);
755  }
756 
757  b->q_unit_cnt_prev = b->has_band_ext ? b->band_ext_q_unit : b->q_unit_cnt;
758 
759  apply_intensity_stereo(s, b, stereo);
760  apply_scalefactors (s, b, stereo);
761 
762  if (b->has_band_ext && b->has_band_ext_data)
763  apply_band_extension (s, b, stereo);
764 
765 imdct:
766  for (int i = 0; i <= stereo; i++) {
767  ATRAC9ChannelData *c = &b->channel[i];
768  const int dst_idx = s->block_config->plane_map[block_idx][i];
769  const int wsize = 1 << s->frame_log2;
770  const ptrdiff_t offset = wsize*frame_idx*sizeof(float);
771  float *dst = (float *)(frame->extended_data[dst_idx] + offset);
772 
773  s->imdct.imdct_half(&s->imdct, s->temp, c->coeffs);
774  s->fdsp->vector_fmul_window(dst, c->prev_win, s->temp,
775  s->imdct_win, wsize >> 1);
776  memcpy(c->prev_win, s->temp + (wsize >> 1), sizeof(float)*wsize >> 1);
777  }
778 
779  return 0;
780 }
781 
782 static int atrac9_decode_frame(AVCodecContext *avctx, void *data,
783  int *got_frame_ptr, AVPacket *avpkt)
784 {
785  int ret;
786  GetBitContext gb;
787  AVFrame *frame = data;
788  ATRAC9Context *s = avctx->priv_data;
789  const int frames = FFMIN(avpkt->size / s->avg_frame_size, s->frame_count);
790 
791  frame->nb_samples = (1 << s->frame_log2) * frames;
792  ret = ff_get_buffer(avctx, frame, 0);
793  if (ret < 0)
794  return ret;
795 
796  init_get_bits8(&gb, avpkt->data, avpkt->size);
797 
798  for (int i = 0; i < frames; i++) {
799  for (int j = 0; j < s->block_config->count; j++) {
800  ret = atrac9_decode_block(s, &gb, &s->block[j], frame, i, j);
801  if (ret)
802  return ret;
803  align_get_bits(&gb);
804  }
805  }
806 
807  *got_frame_ptr = 1;
808 
809  return avctx->block_align;
810 }
811 
813 {
814  ATRAC9Context *s = avctx->priv_data;
815 
816  for (int j = 0; j < s->block_config->count; j++) {
817  ATRAC9BlockData *b = &s->block[j];
818  const int stereo = s->block_config->type[j] == ATRAC9_BLOCK_TYPE_CPE;
819  for (int i = 0; i <= stereo; i++) {
820  ATRAC9ChannelData *c = &b->channel[i];
821  memset(c->prev_win, 0, sizeof(c->prev_win));
822  }
823  }
824 }
825 
827 {
828  ATRAC9Context *s = avctx->priv_data;
829 
830  for (int i = 1; i < 7; i++)
831  ff_free_vlc(&s->sf_vlc[0][i]);
832  for (int i = 2; i < 6; i++)
833  ff_free_vlc(&s->sf_vlc[1][i]);
834  for (int i = 0; i < 2; i++)
835  for (int j = 0; j < 8; j++)
836  for (int k = 0; k < 4; k++)
837  ff_free_vlc(&s->coeff_vlc[i][j][k]);
838 
839  ff_mdct_end(&s->imdct);
840  av_free(s->fdsp);
841 
842  return 0;
843 }
844 
846 {
847  GetBitContext gb;
848  ATRAC9Context *s = avctx->priv_data;
849  int version, block_config_idx, superframe_idx, alloc_c_len;
850 
851  s->avctx = avctx;
852 
853  av_lfg_init(&s->lfg, 0xFBADF00D);
854 
855  if (avctx->block_align <= 0) {
856  av_log(avctx, AV_LOG_ERROR, "Invalid block align\n");
857  return AVERROR_INVALIDDATA;
858  }
859 
860  if (avctx->extradata_size != 12) {
861  av_log(avctx, AV_LOG_ERROR, "Invalid extradata length!\n");
862  return AVERROR_INVALIDDATA;
863  }
864 
865  version = AV_RL32(avctx->extradata);
866  if (version > 2) {
867  av_log(avctx, AV_LOG_ERROR, "Unsupported version (%i)!\n", version);
868  return AVERROR_INVALIDDATA;
869  }
870 
871  init_get_bits8(&gb, avctx->extradata + 4, avctx->extradata_size);
872 
873  if (get_bits(&gb, 8) != 0xFE) {
874  av_log(avctx, AV_LOG_ERROR, "Incorrect magic byte!\n");
875  return AVERROR_INVALIDDATA;
876  }
877 
878  s->samplerate_idx = get_bits(&gb, 4);
879  avctx->sample_rate = at9_tab_samplerates[s->samplerate_idx];
880 
881  block_config_idx = get_bits(&gb, 3);
882  if (block_config_idx > 5) {
883  av_log(avctx, AV_LOG_ERROR, "Incorrect block config!\n");
884  return AVERROR_INVALIDDATA;
885  }
886  s->block_config = &at9_block_layout[block_config_idx];
887 
888  avctx->channel_layout = s->block_config->channel_layout;
891 
892  if (get_bits1(&gb)) {
893  av_log(avctx, AV_LOG_ERROR, "Incorrect verification bit!\n");
894  return AVERROR_INVALIDDATA;
895  }
896 
897  /* Average frame size in bytes */
898  s->avg_frame_size = get_bits(&gb, 11) + 1;
899 
900  superframe_idx = get_bits(&gb, 2);
901  if (superframe_idx & 1) {
902  av_log(avctx, AV_LOG_ERROR, "Invalid superframe index!\n");
903  return AVERROR_INVALIDDATA;
904  }
905 
906  s->frame_count = 1 << superframe_idx;
907  s->frame_log2 = at9_tab_sri_frame_log2[s->samplerate_idx];
908 
909  if (ff_mdct_init(&s->imdct, s->frame_log2 + 1, 1, 1.0f / 32768.0f))
910  return AVERROR(ENOMEM);
911 
913  if (!s->fdsp)
914  return AVERROR(ENOMEM);
915 
916  /* iMDCT window */
917  for (int i = 0; i < (1 << s->frame_log2); i++) {
918  const int len = 1 << s->frame_log2;
919  const float sidx = ( i + 0.5f) / len;
920  const float eidx = (len - i - 0.5f) / len;
921  const float s_c = sinf(sidx*M_PI - M_PI_2)*0.5f + 0.5f;
922  const float e_c = sinf(eidx*M_PI - M_PI_2)*0.5f + 0.5f;
923  s->imdct_win[i] = s_c / ((s_c * s_c) + (e_c * e_c));
924  }
925 
926  /* Allocation curve */
927  alloc_c_len = FF_ARRAY_ELEMS(at9_tab_b_dist);
928  for (int i = 1; i <= alloc_c_len; i++)
929  for (int j = 0; j < i; j++)
930  s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
931 
932  /* Unsigned scalefactor VLCs */
933  for (int i = 1; i < 7; i++) {
935 
936  init_vlc(&s->sf_vlc[0][i], 9, hf->size, hf->bits, 1, 1, hf->codes,
937  2, 2, 0);
938  }
939 
940  /* Signed scalefactor VLCs */
941  for (int i = 2; i < 6; i++) {
943 
944  int nums = hf->size;
945  int16_t sym[32];
946  for (int j = 0; j < nums; j++)
947  sym[j] = sign_extend(j, hf->value_bits);
948 
949  ff_init_vlc_sparse(&s->sf_vlc[1][i], 9, hf->size, hf->bits, 1, 1,
950  hf->codes, 2, 2, sym, sizeof(*sym), sizeof(*sym), 0);
951  }
952 
953  /* Coefficient VLCs */
954  for (int i = 0; i < 2; i++) {
955  for (int j = 0; j < 8; j++) {
956  for (int k = 0; k < 4; k++) {
957  const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
958  init_vlc(&s->coeff_vlc[i][j][k], 9, hf->size, hf->bits, 1, 1,
959  hf->codes, 2, 2, 0);
960  }
961  }
962  }
963 
964  return 0;
965 }
966 
968  .name = "atrac9",
969  .long_name = NULL_IF_CONFIG_SMALL("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
970  .type = AVMEDIA_TYPE_AUDIO,
971  .id = AV_CODEC_ID_ATRAC9,
972  .priv_data_size = sizeof(ATRAC9Context),
974  .close = atrac9_decode_close,
978  .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
979 };
atrac9_decode_close
static av_cold int atrac9_decode_close(AVCodecContext *avctx)
Definition: atrac9dec.c:826
ATRAC9ChannelData::q_coeffs_coarse
int32_t q_coeffs_coarse[256]
Definition: atrac9dec.c:42
AVCodec
AVCodec.
Definition: avcodec.h:3481
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
FF_CODEC_CAP_INIT_THREADSAFE
#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
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
ATRAC9ChannelData::coeffs
float coeffs[256]
Definition: atrac9dec.c:45
ATRAC9BlockData::is_signs
int is_signs[30]
Definition: atrac9dec.c:72
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2276
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2225
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:112
HuffmanCodebook::size
const int size
Definition: atrac9tab.h:488
at9_band_ext_scales_m2
static const float at9_band_ext_scales_m2[]
Definition: atrac9tab.h:284
read_scalefactors
static int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb, int channel_idx, int first_in_pkt)
Definition: atrac9dec.c:254
ch
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);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
count
void INT64 INT64 count
Definition: avisynth_c.h:767
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
at9_tab_samplerates
static const int at9_tab_samplerates[]
Definition: atrac9tab.h:129
av_clip_uintp2_c
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
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
M_PI_2
#define M_PI_2
Definition: mathematics.h:55
init_vlc
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
ATRAC9BlockData::has_band_ext
int has_band_ext
Definition: atrac9dec.c:61
calc_codebook_idx
static void calc_codebook_idx(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:353
b
#define b
Definition: input.c:41
ATRAC9ChannelData
Definition: atrac9dec.c:29
data
const char data[16]
Definition: mxf.c:91
at9_block_layout
static const ATRAC9BlockConfig at9_block_layout[]
Definition: atrac9tab.h:42
ff_mdct_init
#define ff_mdct_init
Definition: fft.h:169
get_vlc2
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
base
uint8_t base
Definition: vp3data.h:202
ATRAC9ChannelData::precision_fine
int precision_fine[30]
Definition: atrac9dec.c:37
HuffmanCodebook::value_cnt
const int value_cnt
Definition: atrac9tab.h:489
ATRAC9Context::frame_count
int frame_count
Definition: atrac9dec.c:88
ATRAC9Context::avctx
AVCodecContext * avctx
Definition: atrac9dec.c:79
ATRAC9Context::lfg
AVLFG lfg
Definition: atrac9dec.c:83
ATRAC9ChannelData::precision_mask
int precision_mask[30]
Definition: atrac9dec.c:38
read_coeffs_fine
static void read_coeffs_fine(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
Definition: atrac9dec.c:433
HuffmanCodebook::codes
const uint16_t * codes
Definition: atrac9tab.h:487
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
ATRAC9Context
Definition: atrac9dec.c:78
ATRAC9Context::imdct
FFTContext imdct
Definition: atrac9dec.c:81
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
at9_tab_band_ext_cnt
static const uint8_t at9_tab_band_ext_cnt[][6]
Definition: atrac9tab.h:134
calc_precision
static void calc_precision(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:148
start
void INT64 start
Definition: avisynth_c.h:767
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
GetBitContext
Definition: get_bits.h:61
ATRAC9Context::block
ATRAC9BlockData block[5]
Definition: atrac9dec.c:82
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:11135
ATRAC9BlockData::grad_mode
int grad_mode
Definition: atrac9dec.c:66
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1645
ATRAC9Context::alloc_curve
uint8_t alloc_curve[48][48]
Definition: atrac9dec.c:95
src
#define src
Definition: vp8dsp.c:254
HuffmanCodebook::value_cnt_pow
const int value_cnt_pow
Definition: atrac9tab.h:490
scale_band_ext_coeffs
static void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6], const int s_unit, const int e_unit)
Definition: atrac9dec.c:524
ATRAC9_BLOCK_TYPE_LFE
@ ATRAC9_BLOCK_TYPE_LFE
Definition: atrac9tab.h:32
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
parse_band_ext
static int parse_band_ext(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb, int stereo)
Definition: atrac9dec.c:199
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
at9_tab_sri_max_bands
static const uint8_t at9_tab_sri_max_bands[]
Definition: atrac9tab.h:125
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:1667
s
#define s(width, name)
Definition: cbs_vp9.c:257
ATRAC9Context::coeff_vlc
VLC coeff_vlc[2][8][4]
Definition: atrac9dec.c:94
at9_q_unit_to_codebookidx
const uint8_t at9_q_unit_to_codebookidx[]
Definition: atrac9tab.h:120
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
lfg.h
fill_with_noise
static void fill_with_noise(ATRAC9Context *s, ATRAC9ChannelData *c, int start, int count)
Definition: atrac9dec.c:508
ATRAC9BlockConfig
Definition: atrac9tab.h:35
AV_CODEC_ID_ATRAC9
@ AV_CODEC_ID_ATRAC9
Definition: avcodec.h:653
av_bmg_get
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
HuffmanCodebook::value_bits
const int value_bits
Definition: atrac9tab.h:491
ATRAC9Context::avg_frame_size
int avg_frame_size
Definition: atrac9dec.c:87
get_bits.h
ff_free_vlc
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
ATRAC9BlockData::band_ext_q_unit
int band_ext_q_unit
Definition: atrac9dec.c:63
bands
static const float bands[]
Definition: af_superequalizer.c:56
at9_band_ext_scales_m0
static const float at9_band_ext_scales_m0[][5][32]
Definition: atrac9tab.h:197
f
#define f(width, name)
Definition: cbs_vp9.c:255
version
int version
Definition: avisynth_c.h:858
int32_t
int32_t
Definition: audio_convert.c:194
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
ff_atrac9_decoder
AVCodec ff_atrac9_decoder
Definition: atrac9dec.c:967
ATRAC9Context::sf_vlc
VLC sf_vlc[2][8]
Definition: atrac9dec.c:93
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:500
HuffmanCodebook
Definition: atrac9tab.h:485
HuffmanCodebook::max_bit_size
const int max_bit_size
Definition: atrac9tab.h:492
at9_huffman_sf_unsigned
static const HuffmanCodebook at9_huffman_sf_unsigned[]
Definition: atrac9tab.h:495
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
ATRAC9Context::frame_log2
int frame_log2
Definition: atrac9dec.c:86
atrac9_decode_init
static av_cold int atrac9_decode_init(AVCodecContext *avctx)
Definition: atrac9dec.c:845
ATRAC9ChannelData::q_coeffs_fine
int32_t q_coeffs_fine[256]
Definition: atrac9dec.c:43
parse_gradient
static int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb)
Definition: atrac9dec.c:101
ATRAC9BlockData::q_unit_cnt_prev
int q_unit_cnt_prev
Definition: atrac9dec.c:55
sinf
#define sinf(x)
Definition: libm.h:419
ff_init_vlc_sparse
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
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ATRAC9BlockData::gradient
int gradient[31]
Definition: atrac9dec.c:68
av_get_channel_layout_nb_channels
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: channel_layout.c:220
float_dsp.h
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1965
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
apply_band_extension
static void apply_band_extension(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:535
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
ATRAC9ChannelData::band_ext
int band_ext
Definition: atrac9dec.c:30
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2233
ATRAC9Context::fdsp
AVFloatDSPContext * fdsp
Definition: atrac9dec.c:80
ff_mdct_end
#define ff_mdct_end
Definition: fft.h:170
ATRAC9_BLOCK_TYPE_CPE
@ ATRAC9_BLOCK_TYPE_CPE
Definition: atrac9tab.h:31
avg
#define avg(a, b, c, d)
Definition: colorspacedsp_template.c:28
AVFloatDSPContext
Definition: float_dsp.h:24
val
const char const char void * val
Definition: avisynth_c.h:863
ATRAC9ChannelData::band_ext_data
int band_ext_data[4]
Definition: atrac9dec.c:32
dequantize
static void dequantize(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:451
ATRAC9BlockData::grad_boundary
int grad_boundary
Definition: atrac9dec.c:67
at9_tab_band_q_unit_map
static const uint8_t at9_tab_band_q_unit_map[]
Definition: atrac9tab.h:106
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
at9_huffman_sf_signed
static const HuffmanCodebook at9_huffman_sf_signed[]
Definition: atrac9tab.h:505
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
atrac9_decode_frame
static int atrac9_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: atrac9dec.c:782
ATRAC9ChannelData::scalefactors
int32_t scalefactors[31]
Definition: atrac9dec.c:33
at9_q_unit_to_coeff_idx
static const int at9_q_unit_to_coeff_idx[]
Definition: atrac9tab.h:115
M_PI
#define M_PI
Definition: mathematics.h:52
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2226
at9_quant_step_coarse
static const float at9_quant_step_coarse[]
Definition: atrac9tab.h:319
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:112
ATRAC9BlockData::stereo_q_unit
int stereo_q_unit
Definition: atrac9dec.c:58
ATRAC9ChannelData::scalefactors_prev
int32_t scalefactors_prev[31]
Definition: atrac9dec.c:34
ATRAC9BlockData::cpe_base_channel
int cpe_base_channel
Definition: atrac9dec.c:71
ATRAC9Context::block_config
const ATRAC9BlockConfig * block_config
Definition: atrac9dec.c:90
FFTContext
Definition: fft.h:88
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
at9_tab_band_ext_lengths
static const uint8_t at9_tab_band_ext_lengths[][6][4]
Definition: atrac9tab.h:154
ATRAC9Context::imdct_win
float imdct_win[256]
Definition: atrac9dec.c:96
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
FF_CODEC_CAP_INIT_CLEANUP
#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
atrac9_decode_block
static int atrac9_decode_block(ATRAC9Context *s, GetBitContext *gb, ATRAC9BlockData *b, AVFrame *frame, int frame_idx, int block_idx)
Definition: atrac9dec.c:637
ATRAC9Context::temp
float temp[256]
Definition: atrac9dec.c:98
delta
float delta
Definition: vorbis_enc_data.h:457
at9_band_ext_scales_m3
static const float at9_band_ext_scales_m3[][2]
Definition: atrac9tab.h:303
HuffmanCodebook::bits
const uint8_t * bits
Definition: atrac9tab.h:486
at9_scalefactor_c
static const float at9_scalefactor_c[]
Definition: atrac9tab.h:337
at9_band_ext_scales_m4
static const float at9_band_ext_scales_m4[]
Definition: atrac9tab.h:314
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
len
int len
Definition: vorbis_enc_data.h:452
ATRAC9BlockData::reuseable
int reuseable
Definition: atrac9dec.c:74
ATRAC9ChannelData::precision_coarse
int precision_coarse[30]
Definition: atrac9dec.c:36
ATRAC9ChannelData::codebookset
int codebookset[30]
Definition: atrac9dec.c:40
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:2262
frame
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 For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:693
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
ATRAC9BlockData
Definition: atrac9dec.c:49
fft.h
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
ATRAC9BlockData::has_band_ext_data
int has_band_ext_data
Definition: atrac9dec.c:62
atrac9tab.h
mode
mode
Definition: ebur128.h:83
VLC
Definition: vlc.h:26
at9_quant_step_fine
static const float at9_quant_step_fine[]
Definition: atrac9tab.h:328
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
atrac9_decode_flush
static void atrac9_decode_flush(AVCodecContext *avctx)
Definition: atrac9dec.c:812
apply_scalefactors
static void apply_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:492
values
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 For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
Definition: filter_design.txt:263
ATRAC9Context::samplerate_idx
int samplerate_idx
Definition: atrac9dec.c:89
read_coeffs_coarse
static void read_coeffs_coarse(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
Definition: atrac9dec.c:397
at9_tab_sf_weights
static const uint8_t at9_tab_sf_weights[][32]
Definition: atrac9tab.h:348
ATRAC9ChannelData::q_unit_cnt
int q_unit_cnt
Definition: atrac9dec.c:31
at9_tab_band_ext_group
static const uint8_t at9_tab_band_ext_group[][3]
Definition: atrac9tab.h:143
AV_CODEC_CAP_SUBFRAMES
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
Definition: avcodec.h:1024
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:908
ATRAC9BlockData::q_unit_cnt
int q_unit_cnt
Definition: atrac9dec.c:54
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ATRAC9BlockData::band_count
int band_count
Definition: atrac9dec.c:53
at9_tab_b_dist
static const uint8_t at9_tab_b_dist[]
Definition: atrac9tab.h:383
channel
channel
Definition: ebur128.h:39
ATRAC9ChannelData::prev_win
float prev_win[128]
Definition: atrac9dec.c:46
at9_huffman_coeffs
static const HuffmanCodebook at9_huffman_coeffs[][8][4]
Definition: atrac9tab.h:1550
at9_tab_sri_frame_log2
static const uint8_t at9_tab_sri_frame_log2[]
Definition: atrac9tab.h:102
apply_intensity_stereo
static void apply_intensity_stereo(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:471
min
float min
Definition: vorbis_enc_data.h:456
at9_q_unit_to_coeff_cnt
static const uint8_t at9_q_unit_to_coeff_cnt[]
Definition: atrac9tab.h:110