FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
75 
76 typedef struct ATRAC9Context {
82 
83  /* Set on init */
89 
90  /* Generated on init */
91  VLC sf_vlc[2][8]; /* Signed/unsigned, length */
92  VLC coeff_vlc[2][8][4]; /* Cookbook, precision, cookbook index */
94  DECLARE_ALIGNED(32, float, imdct_win)[256];
95 
96  DECLARE_ALIGNED(32, float, temp)[256];
98 
100  GetBitContext *gb)
101 {
102  int grad_range[2];
103  int grad_value[2];
104  int values, sign, base;
105  uint8_t *curve;
106  float scale;
107 
108  b->grad_mode = get_bits(gb, 2);
109  if (b->grad_mode) {
110  grad_range[0] = get_bits(gb, 5);
111  grad_range[1] = 31;
112  grad_value[0] = get_bits(gb, 5);
113  grad_value[1] = 31;
114  } else {
115  grad_range[0] = get_bits(gb, 6);
116  grad_range[1] = get_bits(gb, 6) + 1;
117  grad_value[0] = get_bits(gb, 5);
118  grad_value[1] = get_bits(gb, 5);
119  }
120  b->grad_boundary = get_bits(gb, 4);
121 
122  if (grad_range[0] >= grad_range[1] || grad_range[1] > 47)
123  return AVERROR_INVALIDDATA;
124 
125  if (grad_value[0] > 31 || grad_value[1] > 31)
126  return AVERROR_INVALIDDATA;
127 
128  if (b->grad_boundary > b->q_unit_cnt)
129  return AVERROR_INVALIDDATA;
130 
131  values = grad_value[1] - grad_value[0];
132  sign = 1 - 2*(values < 0);
133  base = grad_value[0] + sign;
134  scale = (FFABS(values) - 1) / 31.0f;
135  curve = s->alloc_curve[grad_range[1] - grad_range[0] - 1];
136 
137  for (int i = 0; i <= b->q_unit_cnt; i++)
138  b->gradient[i] = grad_value[i >= grad_range[0]];
139 
140  for (int i = grad_range[0]; i < grad_range[1]; i++)
141  b->gradient[i] = base + sign*((int)(scale*curve[i - grad_range[0]]));
142 
143  return 0;
144 }
145 
148 {
149  memset(c->precision_mask, 0, sizeof(c->precision_mask));
150  for (int i = 1; i < b->q_unit_cnt; i++) {
151  const int delta = FFABS(c->scalefactors[i] - c->scalefactors[i - 1]) - 1;
152  if (delta > 0) {
153  const int neg = c->scalefactors[i - 1] > c->scalefactors[i];
154  c->precision_mask[i - neg] += FFMIN(delta, 5);
155  }
156  }
157 
158  if (b->grad_mode) {
159  for (int i = 0; i < b->q_unit_cnt; i++) {
160  c->precision_coarse[i] = c->scalefactors[i];
161  c->precision_coarse[i] += c->precision_mask[i] - b->gradient[i];
162  if (c->precision_coarse[i] < 0)
163  continue;
164  switch (b->grad_mode) {
165  case 1:
166  c->precision_coarse[i] >>= 1;
167  break;
168  case 2:
169  c->precision_coarse[i] = (3 * c->precision_coarse[i]) >> 3;
170  break;
171  case 3:
172  c->precision_coarse[i] >>= 2;
173  break;
174  }
175  }
176  } else {
177  for (int i = 0; i < b->q_unit_cnt; i++)
178  c->precision_coarse[i] = c->scalefactors[i] - b->gradient[i];
179  }
180 
181 
182  for (int i = 0; i < b->q_unit_cnt; i++)
183  c->precision_coarse[i] = FFMAX(c->precision_coarse[i], 1);
184 
185  for (int i = 0; i < b->grad_boundary; i++)
186  c->precision_coarse[i]++;
187 
188  for (int i = 0; i < b->q_unit_cnt; i++) {
189  c->precision_fine[i] = 0;
190  if (c->precision_coarse[i] > 15) {
191  c->precision_fine[i] = c->precision_coarse[i] - 15;
192  c->precision_coarse[i] = 15;
193  }
194  }
195 }
196 
198  GetBitContext *gb, int stereo)
199 {
200  int ext_band = 0;
201 
202  if (b->has_band_ext) {
203  ext_band = at9_tab_band_ext_group[b->q_unit_cnt - 13][2];
204  if (stereo) {
205  b->channel[1].band_ext = get_bits(gb, 2);
206  b->channel[1].band_ext = ext_band > 2 ? b->channel[1].band_ext : 4;
207  } else {
208  skip_bits1(gb);
209  }
210  }
211 
212  b->has_band_ext_data = get_bits1(gb);
213  if (!b->has_band_ext_data)
214  return 0;
215 
216  if (!b->has_band_ext) {
217  skip_bits(gb, 2);
218  skip_bits_long(gb, get_bits(gb, 5));
219  return 0;
220  }
221 
222  b->channel[0].band_ext = get_bits(gb, 2);
223  b->channel[0].band_ext = ext_band > 2 ? b->channel[0].band_ext : 4;
224 
225  if (!get_bits(gb, 5))
226  return 0;
227 
228  for (int i = 0; i <= stereo; i++) {
229  ATRAC9ChannelData *c = &b->channel[i];
230  const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
231  for (int j = 0; j < count; j++) {
232  int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
233  c->band_ext_data[j] = get_bits(gb, len);
234  }
235  }
236 
237  return 0;
238 }
239 
242  int channel_idx, int first_in_pkt)
243 {
244  static const int mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
245  const int mode = mode_map[channel_idx][get_bits(gb, 2)];
246 
247  memset(c->scalefactors, 0, sizeof(c->scalefactors));
248 
249  if (first_in_pkt && (mode == 4 || ((mode == 3) && !channel_idx))) {
250  av_log(s->avctx, AV_LOG_ERROR, "Invalid scalefactor coding mode!\n");
251  return AVERROR_INVALIDDATA;
252  }
253 
254  switch (mode) {
255  case 0: { /* VLC delta offset */
256  const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)];
257  const int base = get_bits(gb, 5);
258  const int len = get_bits(gb, 2) + 3;
259  const VLC *tab = &s->sf_vlc[0][len];
260 
261  c->scalefactors[0] = get_bits(gb, len);
262 
263  for (int i = 1; i < b->band_ext_q_unit; i++) {
264  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
265  c->scalefactors[i] = val & ((1 << len) - 1);
266  }
267 
268  for (int i = 0; i < b->band_ext_q_unit; i++)
269  c->scalefactors[i] += base - sf_weights[i];
270 
271  break;
272  }
273  case 1: { /* CLC offset */
274  const int len = get_bits(gb, 2) + 2;
275  const int base = len < 5 ? get_bits(gb, 5) : 0;
276  for (int i = 0; i < b->band_ext_q_unit; i++)
277  c->scalefactors[i] = base + get_bits(gb, len);
278  break;
279  }
280  case 2:
281  case 4: { /* VLC dist to baseline */
282  const int *baseline = mode == 4 ? c->scalefactors_prev :
283  channel_idx ? b->channel[0].scalefactors :
285  const int baseline_len = mode == 4 ? b->q_unit_cnt_prev :
286  channel_idx ? b->band_ext_q_unit :
287  b->q_unit_cnt_prev;
288 
289  const int len = get_bits(gb, 2) + 2;
290  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
291  const VLC *tab = &s->sf_vlc[1][len];
292 
293  for (int i = 0; i < unit_cnt; i++) {
294  int dist = get_vlc2(gb, tab->table, 9, 2);
295  c->scalefactors[i] = baseline[i] + dist;
296  }
297 
298  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
299  c->scalefactors[i] = get_bits(gb, 5);
300 
301  break;
302  }
303  case 3: { /* VLC offset with baseline */
304  const int *baseline = channel_idx ? b->channel[0].scalefactors :
306  const int baseline_len = channel_idx ? b->band_ext_q_unit :
307  b->q_unit_cnt_prev;
308 
309  const int base = get_bits(gb, 5) - (1 << (5 - 1));
310  const int len = get_bits(gb, 2) + 1;
311  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
312  const VLC *tab = &s->sf_vlc[0][len];
313 
314  c->scalefactors[0] = get_bits(gb, len);
315 
316  for (int i = 1; i < unit_cnt; i++) {
317  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table, 9, 2);
318  c->scalefactors[i] = val & ((1 << len) - 1);
319  }
320 
321  for (int i = 0; i < unit_cnt; i++)
322  c->scalefactors[i] += base + baseline[i];
323 
324  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
325  c->scalefactors[i] = get_bits(gb, 5);
326  break;
327  }
328  }
329 
330  for (int i = 0; i < b->band_ext_q_unit; i++)
331  if (c->scalefactors[i] < 0 || c->scalefactors[i] > 31)
332  return AVERROR_INVALIDDATA;
333 
334  memcpy(c->scalefactors_prev, c->scalefactors, sizeof(c->scalefactors));
335 
336  return 0;
337 }
338 
341 {
342  int avg = 0;
343  const int last_sf = c->scalefactors[c->q_unit_cnt];
344 
345  memset(c->codebookset, 0, sizeof(c->codebookset));
346 
347  if (c->q_unit_cnt <= 1)
348  return;
349  if (s->samplerate_idx > 7)
350  return;
351 
352  c->scalefactors[c->q_unit_cnt] = c->scalefactors[c->q_unit_cnt - 1];
353 
354  if (c->q_unit_cnt > 12) {
355  for (int i = 0; i < 12; i++)
356  avg += c->scalefactors[i];
357  avg = (avg + 6) / 12;
358  }
359 
360  for (int i = 8; i < c->q_unit_cnt; i++) {
361  const int prev = c->scalefactors[i - 1];
362  const int cur = c->scalefactors[i ];
363  const int next = c->scalefactors[i + 1];
364  const int min = FFMIN(prev, next);
365  if ((cur - min >= 3 || 2*cur - prev - next >= 3))
366  c->codebookset[i] = 1;
367  }
368 
369 
370  for (int i = 12; i < c->q_unit_cnt; i++) {
371  const int cur = c->scalefactors[i];
372  const int cnd = at9_q_unit_to_coeff_cnt[i] == 16;
373  const int min = FFMIN(c->scalefactors[i + 1], c->scalefactors[i - 1]);
374  if (c->codebookset[i])
375  continue;
376 
377  c->codebookset[i] = (((cur - min) >= 2) && (cur >= (avg - cnd)));
378  }
379 
380  c->scalefactors[c->q_unit_cnt] = last_sf;
381 }
382 
385 {
386  const int max_prec = s->samplerate_idx > 7 ? 1 : 7;
387 
388  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
389 
390  for (int i = 0; i < c->q_unit_cnt; i++) {
391  int *coeffs = &c->q_coeffs_coarse[at9_q_unit_to_coeff_idx[i]];
392  const int bands = at9_q_unit_to_coeff_cnt[i];
393  const int prec = c->precision_coarse[i] + 1;
394 
395  if (prec <= max_prec) {
396  const int cb = c->codebookset[i];
397  const int cbi = at9_q_unit_to_codebookidx[i];
398  const VLC *tab = &s->coeff_vlc[cb][prec][cbi];
399  const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi];
400  const int groups = bands >> huff->value_cnt_pow;
401 
402  for (int j = 0; j < groups; j++) {
403  uint16_t val = get_vlc2(gb, tab->table, 9, huff->max_bit_size);
404 
405  for (int k = 0; k < huff->value_cnt; k++) {
406  coeffs[k] = sign_extend(val, huff->value_bits);
407  val >>= huff->value_bits;
408  }
409 
410  coeffs += huff->value_cnt;
411  }
412  } else {
413  for (int j = 0; j < bands; j++)
414  coeffs[j] = sign_extend(get_bits(gb, prec), prec);
415  }
416  }
417 }
418 
421 {
422  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
423 
424  for (int i = 0; i < c->q_unit_cnt; i++) {
425  const int start = at9_q_unit_to_coeff_idx[i + 0];
426  const int end = at9_q_unit_to_coeff_idx[i + 1];
427  const int len = c->precision_fine[i] + 1;
428 
429  if (c->precision_fine[i] <= 0)
430  continue;
431 
432  for (int j = start; j < end; j++)
433  c->q_coeffs_fine[j] = sign_extend(get_bits(gb, len), len);
434  }
435 }
436 
439 {
440  memset(c->coeffs, 0, sizeof(c->coeffs));
441 
442  for (int i = 0; i < c->q_unit_cnt; i++) {
443  const int start = at9_q_unit_to_coeff_idx[i + 0];
444  const int end = at9_q_unit_to_coeff_idx[i + 1];
445 
446  const float coarse_c = at9_quant_step_coarse[c->precision_coarse[i]];
447  const float fine_c = at9_quant_step_fine[c->precision_fine[i]];
448 
449  for (int j = start; j < end; j++) {
450  const float vc = c->q_coeffs_coarse[j] * coarse_c;
451  const float vf = c->q_coeffs_fine[j] * fine_c;
452  c->coeffs[j] = vc + vf;
453  }
454  }
455 }
456 
458  const int stereo)
459 {
460  float *src = b->channel[ b->cpe_base_channel].coeffs;
461  float *dst = b->channel[!b->cpe_base_channel].coeffs;
462 
463  if (!stereo)
464  return;
465 
466  if (b->q_unit_cnt <= b->stereo_q_unit)
467  return;
468 
469  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++) {
470  const int sign = b->is_signs[i];
471  const int start = at9_q_unit_to_coeff_idx[i + 0];
472  const int end = at9_q_unit_to_coeff_idx[i + 1];
473  for (int j = start; j < end; j++)
474  dst[j] = sign*src[j];
475  }
476 }
477 
479  const int stereo)
480 {
481  for (int i = 0; i <= stereo; i++) {
482  float *coeffs = b->channel[i].coeffs;
483  for (int j = 0; j < b->q_unit_cnt; j++) {
484  const int start = at9_q_unit_to_coeff_idx[j + 0];
485  const int end = at9_q_unit_to_coeff_idx[j + 1];
486  const int scalefactor = b->channel[i].scalefactors[j];
487  const float scale = at9_scalefactor_c[scalefactor];
488  for (int k = start; k < end; k++)
489  coeffs[k] *= scale;
490  }
491  }
492 }
493 
495  int start, int count)
496 {
497  float maxval = 0.0f;
498  for (int i = 0; i < count; i += 2) {
499  double tmp[2];
500  av_bmg_get(&s->lfg, tmp);
501  c->coeffs[start + i + 0] = tmp[0];
502  c->coeffs[start + i + 1] = tmp[1];
503  maxval = FFMAX(FFMAX(FFABS(tmp[0]), FFABS(tmp[1])), maxval);
504  }
505  /* Normalize */
506  for (int i = 0; i < count; i++)
507  c->coeffs[start + i] /= maxval;
508 }
509 
510 static inline void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6],
511  const int s_unit, const int e_unit)
512 {
513  for (int i = s_unit; i < e_unit; i++) {
514  const int start = at9_q_unit_to_coeff_idx[i + 0];
515  const int end = at9_q_unit_to_coeff_idx[i + 1];
516  for (int j = start; j < end; j++)
517  c->coeffs[j] *= sf[i - s_unit];
518  }
519 }
520 
522  const int stereo)
523 {
524  const int g_units[4] = { /* A, B, C, total units */
525  b->q_unit_cnt,
528  FFMAX(g_units[2], 22),
529  };
530 
531  const int g_bins[4] = { /* A, B, C, total bins */
532  at9_q_unit_to_coeff_idx[g_units[0]],
533  at9_q_unit_to_coeff_idx[g_units[1]],
534  at9_q_unit_to_coeff_idx[g_units[2]],
535  at9_q_unit_to_coeff_idx[g_units[3]],
536  };
537 
538  if (!b->has_band_ext || !b->has_band_ext_data)
539  return;
540 
541  for (int ch = 0; ch <= stereo; ch++) {
542  ATRAC9ChannelData *c = &b->channel[ch];
543 
544  /* Mirror the spectrum */
545  for (int i = 0; i < 3; i++)
546  for (int j = 0; j < (g_bins[i + 1] - g_bins[i + 0]); j++)
547  c->coeffs[g_bins[i] + j] = c->coeffs[g_bins[i] - j - 1];
548 
549  switch (c->band_ext) {
550  case 0: {
551  float sf[6] = { 0.0f };
552  const int l = g_units[3] - g_units[0] - 1;
553  const int n_start = at9_q_unit_to_coeff_idx[g_units[3] - 1];
554  const int n_cnt = at9_q_unit_to_coeff_cnt[g_units[3] - 1];
555  switch (at9_tab_band_ext_group[b->q_unit_cnt - 13][2]) {
556  case 3:
557  sf[0] = at9_band_ext_scales_m0[0][0][c->band_ext_data[0]];
558  sf[1] = at9_band_ext_scales_m0[0][1][c->band_ext_data[0]];
559  sf[2] = at9_band_ext_scales_m0[0][2][c->band_ext_data[1]];
560  sf[3] = at9_band_ext_scales_m0[0][3][c->band_ext_data[2]];
561  sf[4] = at9_band_ext_scales_m0[0][4][c->band_ext_data[3]];
562  break;
563  case 4:
564  sf[0] = at9_band_ext_scales_m0[1][0][c->band_ext_data[0]];
565  sf[1] = at9_band_ext_scales_m0[1][1][c->band_ext_data[0]];
566  sf[2] = at9_band_ext_scales_m0[1][2][c->band_ext_data[1]];
567  sf[3] = at9_band_ext_scales_m0[1][3][c->band_ext_data[2]];
568  sf[4] = at9_band_ext_scales_m0[1][4][c->band_ext_data[3]];
569  break;
570  case 5:
571  sf[0] = at9_band_ext_scales_m0[2][0][c->band_ext_data[0]];
572  sf[1] = at9_band_ext_scales_m0[2][1][c->band_ext_data[1]];
573  sf[2] = at9_band_ext_scales_m0[2][2][c->band_ext_data[1]];
574  break;
575  }
576 
577  sf[l] = at9_scalefactor_c[c->scalefactors[g_units[0]]];
578 
579  fill_with_noise(s, c, n_start, n_cnt);
580  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
581  break;
582  }
583  case 1: {
584  float sf[6];
585  for (int i = g_units[0]; i < g_units[3]; i++)
586  sf[i - g_units[0]] = at9_scalefactor_c[c->scalefactors[i]];
587 
588  fill_with_noise(s, c, g_bins[0], g_bins[3] - g_bins[0]);
589  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
590  break;
591  }
592  case 2: {
593  const float g_sf[2] = {
596  };
597 
598  for (int i = 0; i < 2; i++)
599  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
600  c->coeffs[j] *= g_sf[i];
601  break;
602  }
603  case 3: {
604  float scale = at9_band_ext_scales_m3[c->band_ext_data[0]][0];
605  float rate = at9_band_ext_scales_m3[c->band_ext_data[1]][1];
606  rate = pow(2, rate);
607  for (int i = g_bins[0]; i < g_bins[3]; i++) {
608  scale *= rate;
609  c->coeffs[i] *= scale;
610  }
611  break;
612  }
613  case 4: {
614  const float m = at9_band_ext_scales_m4[c->band_ext_data[0]];
615  const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
616 
617  for (int i = 0; i < 3; i++)
618  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
619  c->coeffs[j] *= g_sf[i];
620  break;
621  }
622  }
623  }
624 }
625 
628  int frame_idx, int block_idx)
629 {
630  const int first_in_pkt = !get_bits1(gb);
631  const int reuse_params = get_bits1(gb);
632  const int stereo = s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_CPE;
633 
634  if (s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_LFE) {
635  ATRAC9ChannelData *c = &b->channel[0];
636  const int precision = reuse_params ? 8 : 4;
637  c->q_unit_cnt = b->q_unit_cnt = 2;
638 
639  memset(c->scalefactors, 0, sizeof(c->scalefactors));
640  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
641  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
642 
643  for (int i = 0; i < b->q_unit_cnt; i++) {
644  c->scalefactors[i] = get_bits(gb, 5);
645  c->precision_coarse[i] = precision;
646  c->precision_fine[i] = 0;
647  }
648 
649  for (int i = 0; i < c->q_unit_cnt; i++) {
650  const int start = at9_q_unit_to_coeff_idx[i + 0];
651  const int end = at9_q_unit_to_coeff_idx[i + 1];
652  for (int j = start; j < end; j++)
653  c->q_coeffs_coarse[j] = get_bits(gb, c->precision_coarse[i] + 1);
654  }
655 
656  dequantize (s, b, c);
657  apply_scalefactors(s, b, 0);
658 
659  goto imdct;
660  }
661 
662  if (first_in_pkt && reuse_params) {
663  av_log(s->avctx, AV_LOG_ERROR, "Invalid block flags!\n");
664  return AVERROR_INVALIDDATA;
665  }
666 
667  /* Band parameters */
668  if (!reuse_params) {
669  int stereo_band, ext_band;
670  const int min_band_count = s->samplerate_idx > 7 ? 1 : 3;
671  b->band_count = get_bits(gb, 4) + min_band_count;
673 
675 
677  av_log(s->avctx, AV_LOG_ERROR, "Invalid band count %i!\n",
678  b->band_count);
679  return AVERROR_INVALIDDATA;
680  }
681 
682  if (stereo) {
683  stereo_band = get_bits(gb, 4) + min_band_count;
684  if (stereo_band > b->band_count) {
685  av_log(s->avctx, AV_LOG_ERROR, "Invalid stereo band %i!\n",
686  stereo_band);
687  return AVERROR_INVALIDDATA;
688  }
689  b->stereo_q_unit = at9_tab_band_q_unit_map[stereo_band];
690  }
691 
692  b->has_band_ext = get_bits1(gb);
693  if (b->has_band_ext) {
694  ext_band = get_bits(gb, 4) + min_band_count;
695  if (ext_band < b->band_count) {
696  av_log(s->avctx, AV_LOG_ERROR, "Invalid extension band %i!\n",
697  ext_band);
698  return AVERROR_INVALIDDATA;
699  }
701  }
702  }
703 
704  /* Calculate bit alloc gradient */
705  if (parse_gradient(s, b, gb))
706  return AVERROR_INVALIDDATA;
707 
708  /* IS data */
709  b->cpe_base_channel = 0;
710  if (stereo) {
711  b->cpe_base_channel = get_bits1(gb);
712  if (get_bits1(gb)) {
713  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++)
714  b->is_signs[i] = 1 - 2*get_bits1(gb);
715  } else {
716  for (int i = 0; i < FF_ARRAY_ELEMS(b->is_signs); i++)
717  b->is_signs[i] = 1;
718  }
719  }
720 
721  /* Band extension */
722  if (parse_band_ext(s, b, gb, stereo))
723  return AVERROR_INVALIDDATA;
724 
725  /* Scalefactors */
726  for (int i = 0; i <= stereo; i++) {
727  ATRAC9ChannelData *c = &b->channel[i];
728  c->q_unit_cnt = i == b->cpe_base_channel ? b->q_unit_cnt :
729  b->stereo_q_unit;
730  if (read_scalefactors(s, b, c, gb, i, first_in_pkt))
731  return AVERROR_INVALIDDATA;
732 
733  calc_precision (s, b, c);
734  calc_codebook_idx (s, b, c);
735  read_coeffs_coarse(s, b, c, gb);
736  read_coeffs_fine (s, b, c, gb);
737  dequantize (s, b, c);
738  }
739 
741 
742  apply_intensity_stereo(s, b, stereo);
743  apply_scalefactors (s, b, stereo);
744  apply_band_extension (s, b, stereo);
745 
746 imdct:
747  for (int i = 0; i <= stereo; i++) {
748  ATRAC9ChannelData *c = &b->channel[i];
749  const int dst_idx = s->block_config->plane_map[block_idx][i];
750  const int wsize = 1 << s->frame_log2;
751  const ptrdiff_t offset = wsize*frame_idx*sizeof(float);
752  float *dst = (float *)(frame->extended_data[dst_idx] + offset);
753 
754  s->imdct.imdct_half(&s->imdct, s->temp, c->coeffs);
755  s->fdsp->vector_fmul_window(dst, c->prev_win, s->temp,
756  s->imdct_win, wsize >> 1);
757  memcpy(c->prev_win, s->temp + (wsize >> 1), sizeof(float)*wsize >> 1);
758  }
759 
760  return 0;
761 }
762 
763 static int atrac9_decode_frame(AVCodecContext *avctx, void *data,
764  int *got_frame_ptr, AVPacket *avpkt)
765 {
766  int ret;
767  GetBitContext gb;
768  AVFrame *frame = data;
769  ATRAC9Context *s = avctx->priv_data;
770  const int frames = FFMIN(avpkt->size / s->avg_frame_size, s->frame_count);
771 
772  frame->nb_samples = (1 << s->frame_log2) * frames;
773  ret = ff_get_buffer(avctx, frame, 0);
774  if (ret < 0)
775  return ret;
776 
777  init_get_bits8(&gb, avpkt->data, avpkt->size);
778 
779  for (int i = 0; i < frames; i++) {
780  for (int j = 0; j < s->block_config->count; j++) {
781  ret = atrac9_decode_block(s, &gb, &s->block[j], frame, i, j);
782  if (ret)
783  return ret;
784  align_get_bits(&gb);
785  }
786  }
787 
788  *got_frame_ptr = 1;
789 
790  return avctx->block_align;
791 }
792 
794 {
795  ATRAC9Context *s = avctx->priv_data;
796 
797  for (int j = 0; j < s->block_config->count; j++) {
798  ATRAC9BlockData *b = &s->block[j];
799  const int stereo = s->block_config->type[j] == ATRAC9_BLOCK_TYPE_CPE;
800  for (int i = 0; i <= stereo; i++) {
801  ATRAC9ChannelData *c = &b->channel[i];
802  memset(c->prev_win, 0, sizeof(c->prev_win));
803  }
804  }
805 }
806 
808 {
809  ATRAC9Context *s = avctx->priv_data;
810 
811  for (int i = 1; i < 7; i++)
812  ff_free_vlc(&s->sf_vlc[0][i]);
813  for (int i = 2; i < 6; i++)
814  ff_free_vlc(&s->sf_vlc[1][i]);
815  for (int i = 0; i < 2; i++)
816  for (int j = 0; j < 8; j++)
817  for (int k = 0; k < 4; k++)
818  ff_free_vlc(&s->coeff_vlc[i][j][k]);
819 
820  ff_mdct_end(&s->imdct);
821  av_free(s->fdsp);
822 
823  return 0;
824 }
825 
827 {
828  GetBitContext gb;
829  ATRAC9Context *s = avctx->priv_data;
830  int version, block_config_idx, superframe_idx, alloc_c_len;
831 
832  s->avctx = avctx;
833 
834  av_lfg_init(&s->lfg, 0xFBADF00D);
835 
836  if (avctx->extradata_size != 12) {
837  av_log(avctx, AV_LOG_ERROR, "Invalid extradata length!\n");
838  return AVERROR_INVALIDDATA;
839  }
840 
841  version = AV_RL32(avctx->extradata);
842  if (version > 2) {
843  av_log(avctx, AV_LOG_ERROR, "Unsupported version (%i)!\n", version);
844  return AVERROR_INVALIDDATA;
845  }
846 
847  init_get_bits8(&gb, avctx->extradata + 4, avctx->extradata_size);
848 
849  if (get_bits(&gb, 8) != 0xFE) {
850  av_log(avctx, AV_LOG_ERROR, "Incorrect magic byte!\n");
851  return AVERROR_INVALIDDATA;
852  }
853 
854  s->samplerate_idx = get_bits(&gb, 4);
856 
857  block_config_idx = get_bits(&gb, 3);
858  if (block_config_idx > 5) {
859  av_log(avctx, AV_LOG_ERROR, "Incorrect block config!\n");
860  return AVERROR_INVALIDDATA;
861  }
862  s->block_config = &at9_block_layout[block_config_idx];
863 
866 
867  if (get_bits1(&gb)) {
868  av_log(avctx, AV_LOG_ERROR, "Incorrect verification bit!\n");
869  return AVERROR_INVALIDDATA;
870  }
871 
872  /* Average frame size in bytes */
873  s->avg_frame_size = get_bits(&gb, 11) + 1;
874 
875  superframe_idx = get_bits(&gb, 2);
876  if (superframe_idx & 1) {
877  av_log(avctx, AV_LOG_ERROR, "Invalid superframe index!\n");
878  return AVERROR_INVALIDDATA;
879  }
880 
881  s->frame_count = 1 << superframe_idx;
883 
884  if (ff_mdct_init(&s->imdct, s->frame_log2 + 1, 1, 1.0f / 32768.0f))
885  return AVERROR(ENOMEM);
886 
888  if (!s->fdsp)
889  return AVERROR(ENOMEM);
890 
891  /* iMDCT window */
892  for (int i = 0; i < (1 << s->frame_log2); i++) {
893  const int len = 1 << s->frame_log2;
894  const float sidx = ( i + 0.5f) / len;
895  const float eidx = (len - i - 0.5f) / len;
896  const float s_c = sinf(sidx*M_PI - M_PI_2)*0.5f + 0.5f;
897  const float e_c = sinf(eidx*M_PI - M_PI_2)*0.5f + 0.5f;
898  s->imdct_win[i] = s_c / ((s_c * s_c) + (e_c * e_c));
899  }
900 
901  /* Allocation curve */
902  alloc_c_len = FF_ARRAY_ELEMS(at9_tab_b_dist);
903  for (int i = 1; i <= alloc_c_len; i++)
904  for (int j = 0; j < i; j++)
905  s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
906 
907  /* Unsigned scalefactor VLCs */
908  for (int i = 1; i < 7; i++) {
910 
911  init_vlc(&s->sf_vlc[0][i], 9, hf->size, hf->bits, 1, 1, hf->codes,
912  2, 2, 0);
913  }
914 
915  /* Signed scalefactor VLCs */
916  for (int i = 2; i < 6; i++) {
917  const HuffmanCodebook *hf = &at9_huffman_sf_signed[i];
918 
919  int nums = hf->size;
920  int16_t sym[32];
921  for (int j = 0; j < nums; j++)
922  sym[j] = sign_extend(j, hf->value_bits);
923 
924  ff_init_vlc_sparse(&s->sf_vlc[1][i], 9, hf->size, hf->bits, 1, 1,
925  hf->codes, 2, 2, sym, sizeof(*sym), sizeof(*sym), 0);
926  }
927 
928  /* Coefficient VLCs */
929  for (int i = 0; i < 2; i++) {
930  for (int j = 0; j < 8; j++) {
931  for (int k = 0; k < 4; k++) {
932  const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
933  init_vlc(&s->coeff_vlc[i][j][k], 9, hf->size, hf->bits, 1, 1,
934  hf->codes, 2, 2, 0);
935  }
936  }
937  }
938 
939  return 0;
940 }
941 
943  .name = "atrac9",
944  .long_name = NULL_IF_CONFIG_SMALL("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
945  .type = AVMEDIA_TYPE_AUDIO,
946  .id = AV_CODEC_ID_ATRAC9,
947  .priv_data_size = sizeof(ATRAC9Context),
949  .close = atrac9_decode_close,
953  .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
954 };
#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
Definition: lfg.h:27
static const uint8_t at9_tab_sri_max_bands[]
Definition: atrac9tab.h:125
float, planar
Definition: samplefmt.h:69
const char const char void * val
Definition: avisynth_c.h:771
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVCodec ff_atrac9_decoder
Definition: atrac9dec.c:942
int precision_mask[30]
Definition: atrac9dec.c:38
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
int precision_coarse[30]
Definition: atrac9dec.c:36
static void apply_band_extension(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:521
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:381
static void dequantize(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:437
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:293
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:268
int size
Definition: avcodec.h:1446
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:383
VLC coeff_vlc[2][8][4]
Definition: atrac9dec.c:92
int version
Definition: avisynth_c.h:766
float coeffs[256]
Definition: atrac9dec.c:45
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3424
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2226
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int is_signs[30]
Definition: atrac9dec.c:72
AVCodecContext * avctx
Definition: atrac9dec.c:77
static const float at9_band_ext_scales_m4[]
Definition: atrac9tab.h:314
uint64_t channel_layout
Definition: atrac9tab.h:36
const int max_bit_size
Definition: atrac9tab.h:492
static const HuffmanCodebook at9_huffman_coeffs[][8][4]
Definition: atrac9tab.h:1550
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:112
static const int at9_tab_samplerates[]
Definition: atrac9tab.h:129
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 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:2197
uint8_t
#define av_cold
Definition: attributes.h:82
float delta
int avg_frame_size
Definition: atrac9dec.c:85
static const OptionGroupDef groups[]
Definition: ffmpeg_opt.c:3210
#define f(width, name)
Definition: cbs_vp9.c:255
float imdct_win[256]
Definition: atrac9dec.c:94
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:1634
static AVFrame * frame
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
uint8_t * data
Definition: avcodec.h:1445
bitstream reader API header.
static void fill_with_noise(ATRAC9Context *s, ATRAC9ChannelData *c, int start, int count)
Definition: atrac9dec.c:494
int frame_count
Definition: atrac9dec.c:86
static int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb, int channel_idx, int first_in_pkt)
Definition: atrac9dec.c:240
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:478
static const uint8_t at9_tab_sf_weights[][32]
Definition: atrac9tab.h:348
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:146
static int atrac9_decode_block(ATRAC9Context *s, GetBitContext *gb, ATRAC9BlockData *b, AVFrame *frame, int frame_idx, int block_idx)
Definition: atrac9dec.c:626
static const float at9_quant_step_fine[]
Definition: atrac9tab.h:328
const uint8_t * bits
Definition: atrac9tab.h:486
#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:419
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: vlc.h:38
static const uint8_t at9_tab_band_q_unit_map[]
Definition: atrac9tab.h:106
#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:120
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1613
static int parse_band_ext(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb, int stereo)
Definition: atrac9dec.c:197
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
const int size
Definition: atrac9tab.h:488
static void calc_codebook_idx(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:339
#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:92
GLsizei count
Definition: opengl_enc.c:109
VLC sf_vlc[2][8]
Definition: atrac9dec.c:91
#define FFMAX(a, b)
Definition: common.h:94
FFTContext imdct
Definition: atrac9dec.c:79
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2240
enum ATRAC9BlockType type[5]
Definition: atrac9tab.h:37
int frame_log2
Definition: atrac9dec.c:84
Definition: fft.h:88
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:895
#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:197
ATRAC9BlockData block[5]
Definition: atrac9dec.c:80
#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:337
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:762
int frames
Definition: movenc.c:65
#define FF_ARRAY_ELEMS(a)
static int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb)
Definition: atrac9dec.c:99
static av_cold int atrac9_decode_init(AVCodecContext *avctx)
Definition: atrac9dec.c:826
#define sinf(x)
Definition: libm.h:419
const int value_bits
Definition: atrac9tab.h:491
int sample_rate
samples per second
Definition: avcodec.h:2189
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:650
int cpe_base_channel
Definition: atrac9dec.c:71
main external API structure.
Definition: avcodec.h:1533
const int value_cnt_pow
Definition: atrac9tab.h:490
static const float bands[]
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1918
void(* imdct_half)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:108
int extradata_size
Definition: avcodec.h:1635
static void apply_intensity_stereo(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:457
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:487
static const uint8_t at9_q_unit_to_coeff_cnt[]
Definition: atrac9tab.h:110
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:523
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:460
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: avcodec.h:1011
uint8_t alloc_curve[48][48]
Definition: atrac9dec.c:93
int samplerate_idx
Definition: atrac9dec.c:87
static av_cold int atrac9_decode_close(AVCodecContext *avctx)
Definition: atrac9dec.c:807
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:143
static const uint8_t at9_tab_band_ext_cnt[][6]
Definition: atrac9tab.h:134
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:284
static void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6], const int s_unit, const int e_unit)
Definition: atrac9dec.c:510
AVFloatDSPContext * fdsp
Definition: atrac9dec.c:78
static const float at9_quant_step_coarse[]
Definition: atrac9tab.h:319
#define avg(a, b, c, d)
static int atrac9_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: atrac9dec.c:763
static const HuffmanCodebook at9_huffman_sf_unsigned[]
Definition: atrac9tab.h:495
common internal api header.
const ATRAC9BlockConfig * block_config
Definition: atrac9dec.c:88
#define ff_mdct_end
Definition: fft.h:170
const int value_cnt
Definition: atrac9tab.h:489
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:102
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:303
const uint16_t * codes
Definition: atrac9tab.h:487
void * priv_data
Definition: avcodec.h:1560
#define av_free(p)
static const HuffmanCodebook at9_huffman_sf_signed[]
Definition: atrac9tab.h:505
int gradient[31]
Definition: atrac9dec.c:68
int len
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:658
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:383
static void atrac9_decode_flush(AVCodecContext *avctx)
Definition: atrac9dec.c:793
void INT64 start
Definition: avisynth_c.h:690
#define M_PI
Definition: mathematics.h:52
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:273
float min
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1422
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
static const int at9_q_unit_to_coeff_idx[]
Definition: atrac9tab.h:115
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:154
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_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);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
float temp[256]
Definition: atrac9dec.c:96
static uint8_t tmp[11]
Definition: aes_ctr.c:26