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 
23 #include "libavutil/thread.h"
24 
25 #include "codec_internal.h"
26 #include "decode.h"
27 #include "get_bits.h"
28 #include "atrac9tab.h"
29 #include "libavutil/tx.h"
30 #include "libavutil/lfg.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/mem_internal.h"
33 
34 #define ATRAC9_SF_VLC_BITS 8
35 #define ATRAC9_COEFF_VLC_BITS 9
36 
37 typedef struct ATRAC9ChannelData {
38  int band_ext;
40  int band_ext_data[4];
43 
45  int precision_fine[30];
46  int precision_mask[30];
47 
48  int codebookset[30];
49 
52 
53  DECLARE_ALIGNED(32, float, coeffs )[256];
54  DECLARE_ALIGNED(32, float, prev_win)[128];
56 
57 typedef struct ATRAC9BlockData {
59 
60  /* Base */
64 
65  /* Stereo block only */
67 
68  /* Band extension only */
72 
73  /* Gradient */
74  int grad_mode;
76  int gradient[31];
77 
78  /* Stereo */
80  int is_signs[30];
81 
82  int reuseable;
83 
85 
86 typedef struct ATRAC9Context {
93 
94  /* Set on init */
100 
101  /* Generated on init */
102  uint8_t alloc_curve[48][48];
103  DECLARE_ALIGNED(32, float, imdct_win)[256];
104 
105  DECLARE_ALIGNED(32, float, temp)[2048];
106 } ATRAC9Context;
107 
108 static const VLCElem *sf_vlc[2][8]; /* Signed/unsigned, length */
109 static const VLCElem *coeff_vlc[2][8][4]; /* Cookbook, precision, cookbook index */
110 
112  GetBitContext *gb)
113 {
114  int grad_range[2];
115  int grad_value[2];
116  int values, sign, base;
117  uint8_t *curve;
118  float scale;
119 
120  b->grad_mode = get_bits(gb, 2);
121  if (b->grad_mode) {
122  grad_range[0] = get_bits(gb, 5);
123  grad_range[1] = 31;
124  grad_value[0] = get_bits(gb, 5);
125  grad_value[1] = 31;
126  } else {
127  grad_range[0] = get_bits(gb, 6);
128  grad_range[1] = get_bits(gb, 6) + 1;
129  grad_value[0] = get_bits(gb, 5);
130  grad_value[1] = get_bits(gb, 5);
131  }
132  b->grad_boundary = get_bits(gb, 4);
133 
134  if (grad_range[0] >= grad_range[1] || grad_range[1] > 31)
135  return AVERROR_INVALIDDATA;
136 
137  if (b->grad_boundary > b->q_unit_cnt)
138  return AVERROR_INVALIDDATA;
139 
140  values = grad_value[1] - grad_value[0];
141  sign = 1 - 2*(values < 0);
142  base = grad_value[0] + sign;
143  scale = (FFABS(values) - 1) / 31.0f;
144  curve = s->alloc_curve[grad_range[1] - grad_range[0] - 1];
145 
146  for (int i = 0; i <= b->q_unit_cnt; i++)
147  b->gradient[i] = grad_value[i >= grad_range[0]];
148 
149  for (int i = grad_range[0]; i < grad_range[1]; i++)
150  b->gradient[i] = base + sign*((int)(scale*curve[i - grad_range[0]]));
151 
152  return 0;
153 }
154 
157 {
158  memset(c->precision_mask, 0, sizeof(c->precision_mask));
159  for (int i = 1; i < b->q_unit_cnt; i++) {
160  const int delta = FFABS(c->scalefactors[i] - c->scalefactors[i - 1]) - 1;
161  if (delta > 0) {
162  const int neg = c->scalefactors[i - 1] > c->scalefactors[i];
163  c->precision_mask[i - neg] += FFMIN(delta, 5);
164  }
165  }
166 
167  if (b->grad_mode) {
168  for (int i = 0; i < b->q_unit_cnt; i++) {
169  c->precision_coarse[i] = c->scalefactors[i];
170  c->precision_coarse[i] += c->precision_mask[i] - b->gradient[i];
171  if (c->precision_coarse[i] < 0)
172  continue;
173  switch (b->grad_mode) {
174  case 1:
175  c->precision_coarse[i] >>= 1;
176  break;
177  case 2:
178  c->precision_coarse[i] = (3 * c->precision_coarse[i]) >> 3;
179  break;
180  case 3:
181  c->precision_coarse[i] >>= 2;
182  break;
183  }
184  }
185  } else {
186  for (int i = 0; i < b->q_unit_cnt; i++)
187  c->precision_coarse[i] = c->scalefactors[i] - b->gradient[i];
188  }
189 
190 
191  for (int i = 0; i < b->q_unit_cnt; i++)
192  c->precision_coarse[i] = FFMAX(c->precision_coarse[i], 1);
193 
194  for (int i = 0; i < b->grad_boundary; i++)
195  c->precision_coarse[i]++;
196 
197  for (int i = 0; i < b->q_unit_cnt; i++) {
198  c->precision_fine[i] = 0;
199  if (c->precision_coarse[i] > 15) {
200  c->precision_fine[i] = FFMIN(c->precision_coarse[i], 30) - 15;
201  c->precision_coarse[i] = 15;
202  }
203  }
204 }
205 
207  GetBitContext *gb, int stereo)
208 {
209  int ext_band = 0;
210 
211  if (b->has_band_ext) {
212  if (b->q_unit_cnt < 13 || b->q_unit_cnt > 20)
213  return AVERROR_INVALIDDATA;
214  ext_band = at9_tab_band_ext_group[b->q_unit_cnt - 13][2];
215  if (stereo) {
216  b->channel[1].band_ext = get_bits(gb, 2);
217  b->channel[1].band_ext = ext_band > 2 ? b->channel[1].band_ext : 4;
218  } else {
219  skip_bits1(gb);
220  }
221  }
222 
223  b->has_band_ext_data = get_bits1(gb);
224  if (!b->has_band_ext_data)
225  return 0;
226 
227  if (!b->has_band_ext) {
228  skip_bits(gb, 2);
229  skip_bits_long(gb, get_bits(gb, 5));
230  return 0;
231  }
232 
233  b->channel[0].band_ext = get_bits(gb, 2);
234  b->channel[0].band_ext = ext_band > 2 ? b->channel[0].band_ext : 4;
235 
236  if (!get_bits(gb, 5)) {
237  for (int i = 0; i <= stereo; i++) {
238  ATRAC9ChannelData *c = &b->channel[i];
239  const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
240  for (int j = 0; j < count; j++) {
241  int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
242  c->band_ext_data[j] = av_clip_uintp2_c(c->band_ext_data[j], len);
243  }
244  }
245 
246  return 0;
247  }
248 
249  for (int i = 0; i <= stereo; i++) {
250  ATRAC9ChannelData *c = &b->channel[i];
251  const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
252  for (int j = 0; j < count; j++) {
253  int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
254  c->band_ext_data[j] = get_bits(gb, len);
255  }
256  }
257 
258  return 0;
259 }
260 
263  int channel_idx, int first_in_pkt)
264 {
265  static const uint8_t mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
266  const int mode = mode_map[channel_idx][get_bits(gb, 2)];
267 
268  memset(c->scalefactors, 0, sizeof(c->scalefactors));
269 
270  if (first_in_pkt && (mode == 4 || ((mode == 3) && !channel_idx))) {
271  av_log(s->avctx, AV_LOG_ERROR, "Invalid scalefactor coding mode!\n");
272  return AVERROR_INVALIDDATA;
273  }
274 
275  switch (mode) {
276  case 0: { /* VLC delta offset */
277  const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)];
278  const int base = get_bits(gb, 5);
279  const int len = get_bits(gb, 2) + 3;
280  const VLCElem *tab = sf_vlc[0][len];
281 
282  c->scalefactors[0] = get_bits(gb, len);
283 
284  for (int i = 1; i < b->band_ext_q_unit; i++) {
285  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab,
286  ATRAC9_SF_VLC_BITS, 1);
287  c->scalefactors[i] = val & ((1 << len) - 1);
288  }
289 
290  for (int i = 0; i < b->band_ext_q_unit; i++)
291  c->scalefactors[i] += base - sf_weights[i];
292 
293  break;
294  }
295  case 1: { /* CLC offset */
296  const int len = get_bits(gb, 2) + 2;
297  const int base = len < 5 ? get_bits(gb, 5) : 0;
298  for (int i = 0; i < b->band_ext_q_unit; i++)
299  c->scalefactors[i] = base + get_bits(gb, len);
300  break;
301  }
302  case 2:
303  case 4: { /* VLC dist to baseline */
304  const int *baseline = mode == 4 ? c->scalefactors_prev :
305  channel_idx ? b->channel[0].scalefactors :
306  c->scalefactors_prev;
307  const int baseline_len = mode == 4 ? b->q_unit_cnt_prev :
308  channel_idx ? b->band_ext_q_unit :
309  b->q_unit_cnt_prev;
310 
311  const int len = get_bits(gb, 2) + 2;
312  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
313  const VLCElem *tab = sf_vlc[1][len];
314 
315  for (int i = 0; i < unit_cnt; i++) {
316  int dist = get_vlc2(gb, tab, ATRAC9_SF_VLC_BITS, 1);
317  c->scalefactors[i] = baseline[i] + dist;
318  }
319 
320  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
321  c->scalefactors[i] = get_bits(gb, 5);
322 
323  break;
324  }
325  case 3: { /* VLC offset with baseline */
326  const int *baseline = channel_idx ? b->channel[0].scalefactors :
327  c->scalefactors_prev;
328  const int baseline_len = channel_idx ? b->band_ext_q_unit :
329  b->q_unit_cnt_prev;
330 
331  const int base = get_bits(gb, 5) - (1 << (5 - 1));
332  const int len = get_bits(gb, 2) + 1;
333  const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
334  const VLCElem *tab = sf_vlc[0][len];
335 
336  c->scalefactors[0] = get_bits(gb, len);
337 
338  for (int i = 1; i < unit_cnt; i++) {
339  int val = c->scalefactors[i - 1] + get_vlc2(gb, tab,
340  ATRAC9_SF_VLC_BITS, 1);
341  c->scalefactors[i] = val & ((1 << len) - 1);
342  }
343 
344  for (int i = 0; i < unit_cnt; i++)
345  c->scalefactors[i] += base + baseline[i];
346 
347  for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
348  c->scalefactors[i] = get_bits(gb, 5);
349  break;
350  }
351  }
352 
353  for (int i = 0; i < b->band_ext_q_unit; i++)
354  if (c->scalefactors[i] < 0 || c->scalefactors[i] > 31)
355  return AVERROR_INVALIDDATA;
356 
357  memcpy(c->scalefactors_prev, c->scalefactors, sizeof(c->scalefactors));
358 
359  return 0;
360 }
361 
364 {
365  int avg = 0;
366  const int last_sf = c->scalefactors[c->q_unit_cnt];
367 
368  memset(c->codebookset, 0, sizeof(c->codebookset));
369 
370  if (c->q_unit_cnt <= 1)
371  return;
372  if (s->samplerate_idx > 7)
373  return;
374 
375  c->scalefactors[c->q_unit_cnt] = c->scalefactors[c->q_unit_cnt - 1];
376 
377  if (c->q_unit_cnt > 12) {
378  for (int i = 0; i < 12; i++)
379  avg += c->scalefactors[i];
380  avg = (avg + 6) / 12;
381  }
382 
383  for (int i = 8; i < c->q_unit_cnt; i++) {
384  const int prev = c->scalefactors[i - 1];
385  const int cur = c->scalefactors[i ];
386  const int next = c->scalefactors[i + 1];
387  const int min = FFMIN(prev, next);
388  if ((cur - min >= 3 || 2*cur - prev - next >= 3))
389  c->codebookset[i] = 1;
390  }
391 
392 
393  for (int i = 12; i < c->q_unit_cnt; i++) {
394  const int cur = c->scalefactors[i];
395  const int cnd = at9_q_unit_to_coeff_cnt[i] == 16;
396  const int min = FFMIN(c->scalefactors[i + 1], c->scalefactors[i - 1]);
397  if (c->codebookset[i])
398  continue;
399 
400  c->codebookset[i] = (((cur - min) >= 2) && (cur >= (avg - cnd)));
401  }
402 
403  c->scalefactors[c->q_unit_cnt] = last_sf;
404 }
405 
408 {
409  const int max_prec = s->samplerate_idx > 7 ? 1 : 7;
410 
411  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
412 
413  for (int i = 0; i < c->q_unit_cnt; i++) {
414  int *coeffs = &c->q_coeffs_coarse[at9_q_unit_to_coeff_idx[i]];
415  const int bands = at9_q_unit_to_coeff_cnt[i];
416  const int prec = c->precision_coarse[i] + 1;
417 
418  if (prec <= max_prec) {
419  const int cb = c->codebookset[i];
420  const int cbi = at9_q_unit_to_codebookidx[i];
421  const VLCElem *tab = coeff_vlc[cb][prec][cbi];
422  const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi];
423  const int groups = bands >> huff->value_cnt_pow;
424 
425  for (int j = 0; j < groups; j++) {
426  uint16_t val = get_vlc2(gb, tab, ATRAC9_COEFF_VLC_BITS, 2);
427 
428  for (int k = 0; k < huff->value_cnt; k++) {
429  coeffs[k] = sign_extend(val, huff->value_bits);
430  val >>= huff->value_bits;
431  }
432 
433  coeffs += huff->value_cnt;
434  }
435  } else {
436  for (int j = 0; j < bands; j++)
437  coeffs[j] = sign_extend(get_bits(gb, prec), prec);
438  }
439  }
440 }
441 
444 {
445  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
446 
447  for (int i = 0; i < c->q_unit_cnt; i++) {
448  const int start = at9_q_unit_to_coeff_idx[i + 0];
449  const int end = at9_q_unit_to_coeff_idx[i + 1];
450  const int len = c->precision_fine[i] + 1;
451 
452  if (c->precision_fine[i] <= 0)
453  continue;
454 
455  for (int j = start; j < end; j++)
456  c->q_coeffs_fine[j] = sign_extend(get_bits(gb, len), len);
457  }
458 }
459 
462 {
463  memset(c->coeffs, 0, sizeof(c->coeffs));
464 
465  for (int i = 0; i < c->q_unit_cnt; i++) {
466  const int start = at9_q_unit_to_coeff_idx[i + 0];
467  const int end = at9_q_unit_to_coeff_idx[i + 1];
468 
469  const float coarse_c = at9_quant_step_coarse[c->precision_coarse[i]];
470  const float fine_c = at9_quant_step_fine[c->precision_fine[i]];
471 
472  for (int j = start; j < end; j++) {
473  const float vc = c->q_coeffs_coarse[j] * coarse_c;
474  const float vf = c->q_coeffs_fine[j] * fine_c;
475  c->coeffs[j] = vc + vf;
476  }
477  }
478 }
479 
481  const int stereo)
482 {
483  float *src = b->channel[ b->cpe_base_channel].coeffs;
484  float *dst = b->channel[!b->cpe_base_channel].coeffs;
485 
486  if (!stereo)
487  return;
488 
489  if (b->q_unit_cnt <= b->stereo_q_unit)
490  return;
491 
492  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++) {
493  const int sign = b->is_signs[i];
494  const int start = at9_q_unit_to_coeff_idx[i + 0];
495  const int end = at9_q_unit_to_coeff_idx[i + 1];
496  for (int j = start; j < end; j++)
497  dst[j] = sign*src[j];
498  }
499 }
500 
502  const int stereo)
503 {
504  for (int i = 0; i <= stereo; i++) {
505  float *coeffs = b->channel[i].coeffs;
506  for (int j = 0; j < b->q_unit_cnt; j++) {
507  const int start = at9_q_unit_to_coeff_idx[j + 0];
508  const int end = at9_q_unit_to_coeff_idx[j + 1];
509  const int scalefactor = b->channel[i].scalefactors[j];
510  const float scale = at9_scalefactor_c[scalefactor];
511  for (int k = start; k < end; k++)
512  coeffs[k] *= scale;
513  }
514  }
515 }
516 
518  int start, int count)
519 {
520  float maxval = 0.0f;
521  for (int i = 0; i < count; i += 2) {
522  double tmp[2];
523  av_bmg_get(&s->lfg, tmp);
524  c->coeffs[start + i + 0] = tmp[0];
525  c->coeffs[start + i + 1] = tmp[1];
526  maxval = FFMAX(FFMAX(FFABS(tmp[0]), FFABS(tmp[1])), maxval);
527  }
528  /* Normalize */
529  for (int i = 0; i < count; i++)
530  c->coeffs[start + i] /= maxval;
531 }
532 
533 static inline void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6],
534  const int s_unit, const int e_unit)
535 {
536  for (int i = s_unit; i < e_unit; i++) {
537  const int start = at9_q_unit_to_coeff_idx[i + 0];
538  const int end = at9_q_unit_to_coeff_idx[i + 1];
539  for (int j = start; j < end; j++)
540  c->coeffs[j] *= sf[i - s_unit];
541  }
542 }
543 
545  const int stereo)
546 {
547  const int g_units[4] = { /* A, B, C, total units */
548  b->q_unit_cnt,
549  at9_tab_band_ext_group[b->q_unit_cnt - 13][0],
550  at9_tab_band_ext_group[b->q_unit_cnt - 13][1],
551  FFMAX(g_units[2], 22),
552  };
553 
554  const int g_bins[4] = { /* A, B, C, total bins */
555  at9_q_unit_to_coeff_idx[g_units[0]],
556  at9_q_unit_to_coeff_idx[g_units[1]],
557  at9_q_unit_to_coeff_idx[g_units[2]],
558  at9_q_unit_to_coeff_idx[g_units[3]],
559  };
560 
561  for (int ch = 0; ch <= stereo; ch++) {
562  ATRAC9ChannelData *c = &b->channel[ch];
563 
564  /* Mirror the spectrum */
565  for (int i = 0; i < 3; i++)
566  for (int j = 0; j < (g_bins[i + 1] - g_bins[i + 0]); j++)
567  c->coeffs[g_bins[i] + j] = c->coeffs[g_bins[i] - j - 1];
568 
569  switch (c->band_ext) {
570  case 0: {
571  float sf[6] = { 0.0f };
572  const int l = g_units[3] - g_units[0] - 1;
573  const int n_start = at9_q_unit_to_coeff_idx[g_units[3] - 1];
574  const int n_cnt = at9_q_unit_to_coeff_cnt[g_units[3] - 1];
575  switch (at9_tab_band_ext_group[b->q_unit_cnt - 13][2]) {
576  case 3:
577  sf[0] = at9_band_ext_scales_m0[0][0][c->band_ext_data[0]];
578  sf[1] = at9_band_ext_scales_m0[0][1][c->band_ext_data[0]];
579  sf[2] = at9_band_ext_scales_m0[0][2][c->band_ext_data[1]];
580  sf[3] = at9_band_ext_scales_m0[0][3][c->band_ext_data[2]];
581  sf[4] = at9_band_ext_scales_m0[0][4][c->band_ext_data[3]];
582  break;
583  case 4:
584  sf[0] = at9_band_ext_scales_m0[1][0][c->band_ext_data[0]];
585  sf[1] = at9_band_ext_scales_m0[1][1][c->band_ext_data[0]];
586  sf[2] = at9_band_ext_scales_m0[1][2][c->band_ext_data[1]];
587  sf[3] = at9_band_ext_scales_m0[1][3][c->band_ext_data[2]];
588  sf[4] = at9_band_ext_scales_m0[1][4][c->band_ext_data[3]];
589  break;
590  case 5:
591  sf[0] = at9_band_ext_scales_m0[2][0][c->band_ext_data[0]];
592  sf[1] = at9_band_ext_scales_m0[2][1][c->band_ext_data[1]];
593  sf[2] = at9_band_ext_scales_m0[2][2][c->band_ext_data[1]];
594  break;
595  }
596 
597  sf[l] = at9_scalefactor_c[c->scalefactors[g_units[0]]];
598 
599  fill_with_noise(s, c, n_start, n_cnt);
600  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
601  break;
602  }
603  case 1: {
604  float sf[6];
605  for (int i = g_units[0]; i < g_units[3]; i++)
606  sf[i - g_units[0]] = at9_scalefactor_c[c->scalefactors[i]];
607 
608  fill_with_noise(s, c, g_bins[0], g_bins[3] - g_bins[0]);
609  scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
610  break;
611  }
612  case 2: {
613  const float g_sf[2] = {
614  at9_band_ext_scales_m2[c->band_ext_data[0]],
615  at9_band_ext_scales_m2[c->band_ext_data[1]],
616  };
617 
618  for (int i = 0; i < 2; i++)
619  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
620  c->coeffs[j] *= g_sf[i];
621  break;
622  }
623  case 3: {
624  float scale = at9_band_ext_scales_m3[c->band_ext_data[0]][0];
625  float rate = at9_band_ext_scales_m3[c->band_ext_data[1]][1];
626  rate = pow(2, rate);
627  for (int i = g_bins[0]; i < g_bins[3]; i++) {
628  scale *= rate;
629  c->coeffs[i] *= scale;
630  }
631  break;
632  }
633  case 4: {
634  const float m = at9_band_ext_scales_m4[c->band_ext_data[0]];
635  const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
636 
637  for (int i = 0; i < 3; i++)
638  for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
639  c->coeffs[j] *= g_sf[i];
640  break;
641  }
642  }
643  }
644 }
645 
648  int frame_idx, int block_idx)
649 {
650  const int first_in_pkt = !get_bits1(gb);
651  const int reuse_params = get_bits1(gb);
652  const int stereo = s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_CPE;
653 
654  if (s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_LFE) {
655  ATRAC9ChannelData *c = &b->channel[0];
656  const int precision = reuse_params ? 8 : 4;
657  c->q_unit_cnt = b->q_unit_cnt = 2;
658 
659  memset(c->scalefactors, 0, sizeof(c->scalefactors));
660  memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
661  memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
662 
663  for (int i = 0; i < b->q_unit_cnt; i++) {
664  c->scalefactors[i] = get_bits(gb, 5);
665  c->precision_coarse[i] = precision;
666  c->precision_fine[i] = 0;
667  }
668 
669  for (int i = 0; i < c->q_unit_cnt; i++) {
670  const int start = at9_q_unit_to_coeff_idx[i + 0];
671  const int end = at9_q_unit_to_coeff_idx[i + 1];
672  for (int j = start; j < end; j++)
673  c->q_coeffs_coarse[j] = get_bits(gb, c->precision_coarse[i] + 1);
674  }
675 
676  dequantize (s, b, c);
677  apply_scalefactors(s, b, 0);
678 
679  goto imdct;
680  }
681 
682  if (first_in_pkt && reuse_params) {
683  av_log(s->avctx, AV_LOG_ERROR, "Invalid block flags!\n");
684  return AVERROR_INVALIDDATA;
685  }
686 
687  /* Band parameters */
688  if (!reuse_params) {
689  int stereo_band, ext_band;
690  const int min_band_count = s->samplerate_idx > 7 ? 1 : 3;
691  b->reuseable = 0;
692  b->band_count = get_bits(gb, 4) + min_band_count;
693  b->q_unit_cnt = at9_tab_band_q_unit_map[b->band_count];
694 
695  b->band_ext_q_unit = b->stereo_q_unit = b->q_unit_cnt;
696 
697  if (b->band_count > at9_tab_sri_max_bands[s->samplerate_idx]) {
698  av_log(s->avctx, AV_LOG_ERROR, "Invalid band count %i!\n",
699  b->band_count);
700  return AVERROR_INVALIDDATA;
701  }
702 
703  if (stereo) {
704  stereo_band = get_bits(gb, 4) + min_band_count;
705  if (stereo_band > b->band_count) {
706  av_log(s->avctx, AV_LOG_ERROR, "Invalid stereo band %i!\n",
707  stereo_band);
708  return AVERROR_INVALIDDATA;
709  }
710  b->stereo_q_unit = at9_tab_band_q_unit_map[stereo_band];
711  }
712 
713  b->has_band_ext = get_bits1(gb);
714  if (b->has_band_ext) {
715  ext_band = get_bits(gb, 4) + min_band_count;
716  if (ext_band < b->band_count) {
717  av_log(s->avctx, AV_LOG_ERROR, "Invalid extension band %i!\n",
718  ext_band);
719  return AVERROR_INVALIDDATA;
720  }
721  b->band_ext_q_unit = at9_tab_band_q_unit_map[ext_band];
722  }
723  b->reuseable = 1;
724  }
725  if (!b->reuseable) {
726  av_log(s->avctx, AV_LOG_ERROR, "invalid block reused!\n");
727  return AVERROR_INVALIDDATA;
728  }
729 
730  /* Calculate bit alloc gradient */
731  if (parse_gradient(s, b, gb))
732  return AVERROR_INVALIDDATA;
733 
734  /* IS data */
735  b->cpe_base_channel = 0;
736  if (stereo) {
737  b->cpe_base_channel = get_bits1(gb);
738  if (get_bits1(gb)) {
739  for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++)
740  b->is_signs[i] = 1 - 2*get_bits1(gb);
741  } else {
742  for (int i = 0; i < FF_ARRAY_ELEMS(b->is_signs); i++)
743  b->is_signs[i] = 1;
744  }
745  }
746 
747  /* Band extension */
748  if (parse_band_ext(s, b, gb, stereo))
749  return AVERROR_INVALIDDATA;
750 
751  /* Scalefactors */
752  for (int i = 0; i <= stereo; i++) {
753  ATRAC9ChannelData *c = &b->channel[i];
754  c->q_unit_cnt = i == b->cpe_base_channel ? b->q_unit_cnt :
755  b->stereo_q_unit;
756  if (read_scalefactors(s, b, c, gb, i, first_in_pkt))
757  return AVERROR_INVALIDDATA;
758 
759  calc_precision (s, b, c);
760  calc_codebook_idx (s, b, c);
761  read_coeffs_coarse(s, b, c, gb);
762  read_coeffs_fine (s, b, c, gb);
763  dequantize (s, b, c);
764  }
765 
766  b->q_unit_cnt_prev = b->has_band_ext ? b->band_ext_q_unit : b->q_unit_cnt;
767 
768  apply_intensity_stereo(s, b, stereo);
769  apply_scalefactors (s, b, stereo);
770 
771  if (b->has_band_ext && b->has_band_ext_data)
772  apply_band_extension (s, b, stereo);
773 
774 imdct:
775  for (int i = 0; i <= stereo; i++) {
776  ATRAC9ChannelData *c = &b->channel[i];
777  const int dst_idx = s->block_config->plane_map[block_idx][i];
778  const int wsize = 1 << s->frame_log2;
779  const ptrdiff_t offset = wsize*frame_idx*sizeof(float);
780  float *dst = (float *)(frame->extended_data[dst_idx] + offset);
781 
782  s->tx_fn(s->tx, s->temp, c->coeffs, sizeof(float));
783  s->fdsp->vector_fmul_window(dst, c->prev_win, s->temp,
784  s->imdct_win, wsize >> 1);
785  memcpy(c->prev_win, s->temp + (wsize >> 1), sizeof(float)*wsize >> 1);
786  }
787 
788  return 0;
789 }
790 
792  int *got_frame_ptr, AVPacket *avpkt)
793 {
794  int ret;
795  GetBitContext gb;
796  ATRAC9Context *s = avctx->priv_data;
797  const int frames = FFMIN(avpkt->size / s->avg_frame_size, s->frame_count);
798 
799  frame->nb_samples = (1 << s->frame_log2) * frames;
800  ret = ff_get_buffer(avctx, frame, 0);
801  if (ret < 0)
802  return ret;
803 
804  init_get_bits8(&gb, avpkt->data, avpkt->size);
805 
806  for (int i = 0; i < frames; i++) {
807  for (int j = 0; j < s->block_config->count; j++) {
808  ret = atrac9_decode_block(s, &gb, &s->block[j], frame, i, j);
809  if (ret)
810  return ret;
811  align_get_bits(&gb);
812  }
813  }
814 
815  *got_frame_ptr = 1;
816 
817  return avctx->block_align;
818 }
819 
821 {
822  ATRAC9Context *s = avctx->priv_data;
823 
824  for (int j = 0; j < s->block_config->count; j++) {
825  ATRAC9BlockData *b = &s->block[j];
826  const int stereo = s->block_config->type[j] == ATRAC9_BLOCK_TYPE_CPE;
827  for (int i = 0; i <= stereo; i++) {
828  ATRAC9ChannelData *c = &b->channel[i];
829  memset(c->prev_win, 0, sizeof(c->prev_win));
830  }
831  }
832 }
833 
835 {
836  ATRAC9Context *s = avctx->priv_data;
837 
838  av_tx_uninit(&s->tx);
839  av_freep(&s->fdsp);
840 
841  return 0;
842 }
843 
845  int nb_bits, int nb_codes,
846  const uint8_t (**tab)[2], int offset)
847 {
848  const uint8_t (*table)[2] = *tab;
849 
850  *tab += nb_codes;
851  return ff_vlc_init_tables_from_lengths(state, nb_bits, nb_codes,
852  &table[0][1], 2, &table[0][0], 2, 1,
853  offset, 0);
854 }
855 
856 static av_cold void atrac9_init_static(void)
857 {
858  static VLCElem vlc_buf[24812];
859  VLCInitState state = VLC_INIT_STATE(vlc_buf);
860  const uint8_t (*tab)[2];
861 
862  /* Unsigned scalefactor VLCs */
863  tab = at9_sfb_a_tab;
864  for (int i = 1; i < 7; i++) {
866 
868  hf->size, &tab, 0);
869  }
870 
871  /* Signed scalefactor VLCs */
872  tab = at9_sfb_b_tab;
873  for (int i = 2; i < 6; i++) {
875 
876  /* The symbols are signed integers in the range -16..15;
877  * the values in the source table are offset by 16 to make
878  * them fit into an uint8_t; the -16 reverses this shift. */
880  hf->size, &tab, -16);
881  }
882 
883  /* Coefficient VLCs */
885  for (int i = 0; i < 2; i++) {
886  for (int j = 2; j < 8; j++) {
887  for (int k = i; k < 4; k++) {
888  const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
890  hf->size, &tab, 0);
891  }
892  }
893  }
894 }
895 
897 {
898  float scale;
899  static AVOnce static_table_init = AV_ONCE_INIT;
900  GetBitContext gb;
901  ATRAC9Context *s = avctx->priv_data;
902  int err, version, block_config_idx, superframe_idx, alloc_c_len;
903 
904  s->avctx = avctx;
905 
906  av_lfg_init(&s->lfg, 0xFBADF00D);
907 
908  if (avctx->block_align <= 0) {
909  av_log(avctx, AV_LOG_ERROR, "Invalid block align\n");
910  return AVERROR_INVALIDDATA;
911  }
912 
913  if (avctx->extradata_size != 12) {
914  av_log(avctx, AV_LOG_ERROR, "Invalid extradata length!\n");
915  return AVERROR_INVALIDDATA;
916  }
917 
918  version = AV_RL32(avctx->extradata);
919  if (version > 2) {
920  av_log(avctx, AV_LOG_ERROR, "Unsupported version (%i)!\n", version);
921  return AVERROR_INVALIDDATA;
922  }
923 
924  init_get_bits8(&gb, avctx->extradata + 4, avctx->extradata_size);
925 
926  if (get_bits(&gb, 8) != 0xFE) {
927  av_log(avctx, AV_LOG_ERROR, "Incorrect magic byte!\n");
928  return AVERROR_INVALIDDATA;
929  }
930 
931  s->samplerate_idx = get_bits(&gb, 4);
932  avctx->sample_rate = at9_tab_samplerates[s->samplerate_idx];
933 
934  block_config_idx = get_bits(&gb, 3);
935  if (block_config_idx > 5) {
936  av_log(avctx, AV_LOG_ERROR, "Incorrect block config!\n");
937  return AVERROR_INVALIDDATA;
938  }
939  s->block_config = &at9_block_layout[block_config_idx];
940 
942  avctx->ch_layout = s->block_config->channel_layout;
944 
945  if (get_bits1(&gb)) {
946  av_log(avctx, AV_LOG_ERROR, "Incorrect verification bit!\n");
947  return AVERROR_INVALIDDATA;
948  }
949 
950  /* Average frame size in bytes */
951  s->avg_frame_size = get_bits(&gb, 11) + 1;
952 
953  superframe_idx = get_bits(&gb, 2);
954  if (superframe_idx & 1) {
955  av_log(avctx, AV_LOG_ERROR, "Invalid superframe index!\n");
956  return AVERROR_INVALIDDATA;
957  }
958 
959  s->frame_count = 1 << superframe_idx;
960  s->frame_log2 = at9_tab_sri_frame_log2[s->samplerate_idx];
961 
962  scale = 1.0f / 32768.0;
963  err = av_tx_init(&s->tx, &s->tx_fn, AV_TX_FLOAT_MDCT, 1,
964  1 << s->frame_log2, &scale, 0);
965  if (err < 0)
966  return err;
967 
969  if (!s->fdsp)
970  return AVERROR(ENOMEM);
971 
972  /* iMDCT window */
973  for (int i = 0; i < (1 << s->frame_log2); i++) {
974  const int len = 1 << s->frame_log2;
975  const float sidx = ( i + 0.5f) / len;
976  const float eidx = (len - i - 0.5f) / len;
977  const float s_c = sinf(sidx*M_PI - M_PI_2)*0.5f + 0.5f;
978  const float e_c = sinf(eidx*M_PI - M_PI_2)*0.5f + 0.5f;
979  s->imdct_win[i] = s_c / ((s_c * s_c) + (e_c * e_c));
980  }
981 
982  /* Allocation curve */
983  alloc_c_len = FF_ARRAY_ELEMS(at9_tab_b_dist);
984  for (int i = 1; i <= alloc_c_len; i++)
985  for (int j = 0; j < i; j++)
986  s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
987 
988  ff_thread_once(&static_table_init, atrac9_init_static);
989 
990  return 0;
991 }
992 
994  .p.name = "atrac9",
995  CODEC_LONG_NAME("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
996  .p.type = AVMEDIA_TYPE_AUDIO,
997  .p.id = AV_CODEC_ID_ATRAC9,
998  .priv_data_size = sizeof(ATRAC9Context),
1000  .close = atrac9_decode_close,
1002  .flush = atrac9_decode_flush,
1003  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1004  .p.capabilities =
1005 #if FF_API_SUBFRAMES
1006  AV_CODEC_CAP_SUBFRAMES |
1007 #endif
1009 };
atrac9_decode_close
static av_cold int atrac9_decode_close(AVCodecContext *avctx)
Definition: atrac9dec.c:834
ATRAC9ChannelData::q_coeffs_coarse
int32_t q_coeffs_coarse[256]
Definition: atrac9dec.c:50
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
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: codec_internal.h:42
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:53
ATRAC9BlockData::is_signs
int is_signs[30]
Definition: atrac9dec.c:80
mem_internal.h
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
ATRAC9Context::temp
float temp[2048]
Definition: atrac9dec.c:105
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:241
HuffmanCodebook::size
const int size
Definition: atrac9tab.h:425
at9_band_ext_scales_m2
static const float at9_band_ext_scales_m2[]
Definition: atrac9tab.h:271
thread.h
read_scalefactors
static int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb, int channel_idx, int first_in_pkt)
Definition: atrac9dec.c:261
AVTXContext
Definition: tx_priv.h:235
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
atrac9_init_vlc
static const av_cold VLCElem * atrac9_init_vlc(VLCInitState *state, int nb_bits, int nb_codes, const uint8_t(**tab)[2], int offset)
Definition: atrac9dec.c:844
at9_tab_samplerates
static const int at9_tab_samplerates[]
Definition: atrac9tab.h:116
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:278
AVPacket::data
uint8_t * data
Definition: packet.h:522
M_PI_2
#define M_PI_2
Definition: mathematics.h:73
ATRAC9BlockData::has_band_ext
int has_band_ext
Definition: atrac9dec.c:69
calc_codebook_idx
static void calc_codebook_idx(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:362
b
#define b
Definition: input.c:41
ATRAC9ChannelData
Definition: atrac9dec.c:37
table
static const uint16_t table[]
Definition: prosumer.c:205
ATRAC9_COEFF_VLC_BITS
#define ATRAC9_COEFF_VLC_BITS
Definition: atrac9dec.c:35
at9_block_layout
static const ATRAC9BlockConfig at9_block_layout[]
Definition: atrac9tab.h:42
FFCodec
Definition: codec_internal.h:127
base
uint8_t base
Definition: vp3data.h:128
ATRAC9ChannelData::precision_fine
int precision_fine[30]
Definition: atrac9dec.c:45
HuffmanCodebook::value_cnt
const int value_cnt
Definition: atrac9tab.h:426
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
ATRAC9Context::frame_count
int frame_count
Definition: atrac9dec.c:97
ATRAC9Context::avctx
AVCodecContext * avctx
Definition: atrac9dec.c:87
ATRAC9Context::lfg
AVLFG lfg
Definition: atrac9dec.c:92
ATRAC9ChannelData::precision_mask
int precision_mask[30]
Definition: atrac9dec.c:46
av_tx_init
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:902
read_coeffs_fine
static void read_coeffs_fine(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
Definition: atrac9dec.c:442
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
ATRAC9Context
Definition: atrac9dec.c:86
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
at9_tab_band_ext_cnt
static const uint8_t at9_tab_band_ext_cnt[][6]
Definition: atrac9tab.h:121
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
calc_precision
static void calc_precision(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:155
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
GetBitContext
Definition: get_bits.h:108
ATRAC9Context::block
ATRAC9BlockData block[5]
Definition: atrac9dec.c:91
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
ATRAC9BlockData::grad_mode
int grad_mode
Definition: atrac9dec.c:74
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
val
static double val(void *priv, double ch)
Definition: aeval.c:78
ATRAC9Context::alloc_curve
uint8_t alloc_curve[48][48]
Definition: atrac9dec.c:102
HuffmanCodebook::value_cnt_pow
const int value_cnt_pow
Definition: atrac9tab.h:427
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:533
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
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:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
parse_band_ext
static int parse_band_ext(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb, int stereo)
Definition: atrac9dec.c:206
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:151
VLCInitState
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
Definition: vlc.h:209
at9_tab_sri_max_bands
static const uint8_t at9_tab_sri_max_bands[]
Definition: atrac9tab.h:112
float
float
Definition: af_crystalizer.c:121
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
AV_TX_FLOAT_MDCT
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respecively.
Definition: tx.h:68
state
static struct @382 state
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
s
#define s(width, name)
Definition: cbs_vp9.c:198
at9_q_unit_to_codebookidx
static const uint8_t at9_q_unit_to_codebookidx[]
Definition: atrac9tab.h:107
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:517
ATRAC9BlockConfig
Definition: atrac9tab.h:35
AV_CODEC_ID_ATRAC9
@ AV_CODEC_ID_ATRAC9
Definition: codec_id.h:528
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:428
ATRAC9Context::avg_frame_size
int avg_frame_size
Definition: atrac9dec.c:96
decode.h
get_bits.h
ATRAC9BlockData::band_ext_q_unit
int band_ext_q_unit
Definition: atrac9dec.c:71
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:184
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
at9_sfb_a_tab
static const uint8_t at9_sfb_a_tab[][2]
Definition: atrac9tab.h:376
frame
static AVFrame * frame
Definition: demux_decode.c:54
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
ATRAC9_SF_VLC_BITS
#define ATRAC9_SF_VLC_BITS
Definition: atrac9dec.c:34
HuffmanCodebook
Definition: atrac9tab.h:424
ATRAC9Context::tx_fn
av_tx_fn tx_fn
Definition: atrac9dec.c:90
at9_huffman_sf_unsigned
static const HuffmanCodebook at9_huffman_sf_unsigned[]
Definition: atrac9tab.h:431
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
ATRAC9Context::frame_log2
int frame_log2
Definition: atrac9dec.c:95
atrac9_decode_init
static av_cold int atrac9_decode_init(AVCodecContext *avctx)
Definition: atrac9dec.c:896
ATRAC9ChannelData::q_coeffs_fine
int32_t q_coeffs_fine[256]
Definition: atrac9dec.c:51
parse_gradient
static int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb)
Definition: atrac9dec.c:111
ATRAC9BlockData::q_unit_cnt_prev
int q_unit_cnt_prev
Definition: atrac9dec.c:63
sinf
#define sinf(x)
Definition: libm.h:419
sf_vlc
static const VLCElem * sf_vlc[2][8]
Definition: atrac9dec.c:108
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:652
AVOnce
#define AVOnce
Definition: thread.h:202
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:76
float_dsp.h
atrac9_init_static
static av_cold void atrac9_init_static(void)
Definition: atrac9dec.c:856
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
f
f
Definition: af_crystalizer.c:121
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1568
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: vvc_intra.c:291
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:109
apply_band_extension
static void apply_band_extension(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:544
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
ATRAC9ChannelData::band_ext
int band_ext
Definition: atrac9dec.c:38
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
ATRAC9Context::fdsp
AVFloatDSPContext * fdsp
Definition: atrac9dec.c:88
VLCElem
Definition: vlc.h:32
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:22
ATRAC9ChannelData::band_ext_data
int band_ext_data[4]
Definition: atrac9dec.c:40
dequantize
static void dequantize(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c)
Definition: atrac9dec.c:460
ATRAC9BlockData::grad_boundary
int grad_boundary
Definition: atrac9dec.c:75
at9_tab_band_q_unit_map
static const uint8_t at9_tab_band_q_unit_map[]
Definition: atrac9tab.h:93
at9_huffman_sf_signed
static const HuffmanCodebook at9_huffman_sf_signed[]
Definition: atrac9tab.h:441
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:413
version
version
Definition: libkvazaar.c:321
ATRAC9ChannelData::scalefactors
int32_t scalefactors[31]
Definition: atrac9dec.c:41
at9_q_unit_to_coeff_idx
static const int at9_q_unit_to_coeff_idx[]
Definition: atrac9tab.h:102
M_PI
#define M_PI
Definition: mathematics.h:67
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:294
at9_quant_step_coarse
static const float at9_quant_step_coarse[]
Definition: atrac9tab.h:306
ATRAC9BlockData::stereo_q_unit
int stereo_q_unit
Definition: atrac9dec.c:66
ATRAC9ChannelData::scalefactors_prev
int32_t scalefactors_prev[31]
Definition: atrac9dec.c:42
ATRAC9BlockData::cpe_base_channel
int cpe_base_channel
Definition: atrac9dec.c:79
ATRAC9Context::block_config
const ATRAC9BlockConfig * block_config
Definition: atrac9dec.c:99
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
at9_tab_band_ext_lengths
static const uint8_t at9_tab_band_ext_lengths[][6][4]
Definition: atrac9tab.h:141
ATRAC9Context::imdct_win
float imdct_win[256]
Definition: atrac9dec.c:103
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
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:646
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
delta
float delta
Definition: vorbis_enc_data.h:430
at9_band_ext_scales_m3
static const float at9_band_ext_scales_m3[][2]
Definition: atrac9tab.h:290
at9_scalefactor_c
static const float at9_scalefactor_c[]
Definition: atrac9tab.h:324
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
at9_band_ext_scales_m4
static const float at9_band_ext_scales_m4[]
Definition: atrac9tab.h:301
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
ATRAC9BlockData::reuseable
int reuseable
Definition: atrac9dec.c:82
ATRAC9ChannelData::precision_coarse
int precision_coarse[30]
Definition: atrac9dec.c:44
ATRAC9ChannelData::codebookset
int codebookset[30]
Definition: atrac9dec.c:48
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:1083
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:561
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
at9_coeffs_tab
static const uint8_t at9_coeffs_tab[][2]
Definition: atrac9tab.h:450
coeff_vlc
static const VLCElem * coeff_vlc[2][8][4]
Definition: atrac9dec.c:109
ATRAC9BlockData
Definition: atrac9dec.c:57
AVCodecContext
main external API structure.
Definition: avcodec.h:445
ATRAC9BlockData::has_band_ext_data
int has_band_ext_data
Definition: atrac9dec.c:70
atrac9tab.h
channel_layout.h
mode
mode
Definition: ebur128.h:83
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:432
at9_quant_step_fine
static const float at9_quant_step_fine[]
Definition: atrac9tab.h:315
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:133
atrac9_decode_flush
static void atrac9_decode_flush(AVCodecContext *avctx)
Definition: atrac9dec.c:820
apply_scalefactors
static void apply_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:501
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
atrac9_decode_frame
static int atrac9_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: atrac9dec.c:791
ATRAC9Context::samplerate_idx
int samplerate_idx
Definition: atrac9dec.c:98
read_coeffs_coarse
static void read_coeffs_coarse(ATRAC9Context *s, ATRAC9BlockData *b, ATRAC9ChannelData *c, GetBitContext *gb)
Definition: atrac9dec.c:406
at9_tab_sf_weights
static const uint8_t at9_tab_sf_weights[][32]
Definition: atrac9tab.h:335
ATRAC9ChannelData::q_unit_cnt
int q_unit_cnt
Definition: atrac9dec.c:39
ff_vlc_init_tables_from_lengths
const av_cold VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
Definition: vlc.c:366
ATRAC9Context::tx
AVTXContext * tx
Definition: atrac9dec.c:89
at9_tab_band_ext_group
static const uint8_t at9_tab_band_ext_group[][3]
Definition: atrac9tab.h:130
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:342
ATRAC9BlockData::q_unit_cnt
int q_unit_cnt
Definition: atrac9dec.c:62
VLC_INIT_STATE
#define VLC_INIT_STATE(_table)
Definition: vlc.h:214
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
at9_sfb_b_tab
static const uint8_t at9_sfb_b_tab[][2]
Definition: atrac9tab.h:407
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ATRAC9BlockData::band_count
int band_count
Definition: atrac9dec.c:61
at9_tab_b_dist
static const uint8_t at9_tab_b_dist[]
Definition: atrac9tab.h:370
channel
channel
Definition: ebur128.h:39
ff_atrac9_decoder
const FFCodec ff_atrac9_decoder
Definition: atrac9dec.c:993
ATRAC9ChannelData::prev_win
float prev_win[128]
Definition: atrac9dec.c:54
tx.h
at9_huffman_coeffs
static const HuffmanCodebook at9_huffman_coeffs[][8][4]
Definition: atrac9tab.h:1293
at9_tab_sri_frame_log2
static const uint8_t at9_tab_sri_frame_log2[]
Definition: atrac9tab.h:89
apply_intensity_stereo
static void apply_intensity_stereo(ATRAC9Context *s, ATRAC9BlockData *b, const int stereo)
Definition: atrac9dec.c:480
min
float min
Definition: vorbis_enc_data.h:429
at9_q_unit_to_coeff_cnt
static const uint8_t at9_q_unit_to_coeff_cnt[]
Definition: atrac9tab.h:97