FFmpeg
atrac3plus.c
Go to the documentation of this file.
1 /*
2  * ATRAC3+ compatible decoder
3  *
4  * Copyright (c) 2010-2013 Maxim Poliakovski
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Bitstream parser for ATRAC3+ decoder.
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "atrac3plus.h"
32 #include "atrac3plus_data.h"
33 
34 static VLCElem tables_data[154276];
35 static VLC wl_vlc_tabs[4];
36 static VLC sf_vlc_tabs[8];
37 static VLC ct_vlc_tabs[4];
38 static VLC spec_vlc_tabs[112];
39 static VLC gain_vlc_tabs[11];
40 static VLC tone_vlc_tabs[7];
41 
42 /**
43  * Generate canonical VLC table from given descriptor.
44  *
45  * @param[in] cb ptr to codebook descriptor
46  * @param[in,out] xlat ptr to ptr to translation table
47  * @param[in,out] tab_offset starting offset to the generated vlc table
48  * @param[out] out_vlc ptr to vlc table to be generated
49  */
50 static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t **xlat,
51  int *tab_offset, VLC *out_vlc)
52 {
53  int i, max_len;
54  uint8_t bits[256];
55  int index = 0;
56 
57  for (int b = 1; b <= 12; b++) {
58  for (i = *cb++; i > 0; i--) {
59  av_assert0(index < 256);
60  bits[index] = b;
61  index++;
62  }
63  }
64  max_len = bits[index - 1];
65 
66  out_vlc->table = &tables_data[*tab_offset];
67  out_vlc->table_allocated = 1 << max_len;
68 
69  ff_vlc_init_from_lengths(out_vlc, max_len, index, bits, 1,
70  *xlat, 1, 1, 0, VLC_INIT_USE_STATIC, NULL);
71 
72  *tab_offset += 1 << max_len;
73  *xlat += index;
74 }
75 
77 {
78  int i, tab_offset = 0;
79  const uint8_t *xlats;
80 
81  xlats = atrac3p_wl_ct_xlats;
82  for (int i = 0; i < 4; i++) {
84  &tab_offset, &wl_vlc_tabs[i]);
86  &tab_offset, &ct_vlc_tabs[i]);
87  }
88 
89  xlats = atrac3p_sf_xlats;
90  for (int i = 0; i < 8; i++)
92  &tab_offset, &sf_vlc_tabs[i]);
93 
94  /* build huffman tables for spectrum decoding */
95  xlats = atrac3p_spectra_xlats;
96  for (i = 0; i < 112; i++) {
97  if (atrac3p_spectra_cbs[i][0] >= 0)
99  &xlats, &tab_offset, &spec_vlc_tabs[i]);
100  else /* Reuse already initialized VLC table */
102  }
103 
104  /* build huffman tables for gain data decoding */
105  xlats = atrac3p_gain_xlats;
106  for (i = 0; i < 11; i++)
108  &tab_offset, &gain_vlc_tabs[i]);
109 
110  /* build huffman tables for tone decoding */
111  xlats = atrac3p_tone_xlats;
112  for (i = 0; i < 7; i++)
114  &tab_offset, &tone_vlc_tabs[i]);
115 }
116 
117 /**
118  * Decode number of coded quantization units.
119  *
120  * @param[in] gb the GetBit context
121  * @param[in,out] chan ptr to the channel parameters
122  * @param[in,out] ctx ptr to the channel unit context
123  * @param[in] avctx ptr to the AVCodecContext
124  * @return result code: 0 = OK, otherwise - error code
125  */
128 {
129  chan->fill_mode = get_bits(gb, 2);
130  if (!chan->fill_mode) {
131  chan->num_coded_vals = ctx->num_quant_units;
132  } else {
133  chan->num_coded_vals = get_bits(gb, 5);
134  if (chan->num_coded_vals > ctx->num_quant_units) {
135  av_log(avctx, AV_LOG_ERROR,
136  "Invalid number of transmitted units!\n");
137  return AVERROR_INVALIDDATA;
138  }
139 
140  if (chan->fill_mode == 3)
141  chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;
142  }
143 
144  return 0;
145 }
146 
147 /**
148  * Add weighting coefficients to the decoded word-length information.
149  *
150  * @param[in,out] ctx ptr to the channel unit context
151  * @param[in,out] chan ptr to the channel parameters
152  * @param[in] wtab_idx index of the table of weights
153  * @param[in] avctx ptr to the AVCodecContext
154  * @return result code: 0 = OK, otherwise - error code
155  */
157  Atrac3pChanParams *chan, int wtab_idx,
158  AVCodecContext *avctx)
159 {
160  int i;
161  const int8_t *weights_tab =
162  &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
163 
164  for (i = 0; i < ctx->num_quant_units; i++) {
165  chan->qu_wordlen[i] += weights_tab[i];
166  if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {
167  av_log(avctx, AV_LOG_ERROR,
168  "WL index out of range: pos=%d, val=%d!\n",
169  i, chan->qu_wordlen[i]);
170  return AVERROR_INVALIDDATA;
171  }
172  }
173 
174  return 0;
175 }
176 
177 /**
178  * Subtract weighting coefficients from decoded scalefactors.
179  *
180  * @param[in,out] ctx ptr to the channel unit context
181  * @param[in,out] chan ptr to the channel parameters
182  * @param[in] wtab_idx index of table of weights
183  * @param[in] avctx ptr to the AVCodecContext
184  * @return result code: 0 = OK, otherwise - error code
185  */
187  Atrac3pChanParams *chan, int wtab_idx,
188  AVCodecContext *avctx)
189 {
190  int i;
191  const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
192 
193  for (i = 0; i < ctx->used_quant_units; i++) {
194  chan->qu_sf_idx[i] -= weights_tab[i];
195  if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {
196  av_log(avctx, AV_LOG_ERROR,
197  "SF index out of range: pos=%d, val=%d!\n",
198  i, chan->qu_sf_idx[i]);
199  return AVERROR_INVALIDDATA;
200  }
201  }
202 
203  return 0;
204 }
205 
206 /**
207  * Unpack vector quantization tables.
208  *
209  * @param[in] start_val start value for the unpacked table
210  * @param[in] shape_vec ptr to table to unpack
211  * @param[out] dst ptr to output array
212  * @param[in] num_values number of values to unpack
213  */
214 static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
215  int *dst, int num_values)
216 {
217  int i;
218 
219  if (num_values) {
220  dst[0] = dst[1] = dst[2] = start_val;
221  for (i = 3; i < num_values; i++)
222  dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];
223  }
224 }
225 
226 #define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals) \
227  start_val = get_bits((gb), 6); \
228  unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \
229  (dst), (num_vals))
230 
231 /**
232  * Decode word length for each quantization unit of a channel.
233  *
234  * @param[in] gb the GetBit context
235  * @param[in,out] ctx ptr to the channel unit context
236  * @param[in] ch_num channel to process
237  * @param[in] avctx ptr to the AVCodecContext
238  * @return result code: 0 = OK, otherwise - error code
239  */
241  int ch_num, AVCodecContext *avctx)
242 {
243  int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
244  ret, start_val;
245  VLC *vlc_tab;
246  Atrac3pChanParams *chan = &ctx->channels[ch_num];
247  Atrac3pChanParams *ref_chan = &ctx->channels[0];
248 
249  chan->fill_mode = 0;
250 
251  switch (get_bits(gb, 2)) { /* switch according to coding mode */
252  case 0: /* coded using constant number of bits */
253  for (i = 0; i < ctx->num_quant_units; i++)
254  chan->qu_wordlen[i] = get_bits(gb, 3);
255  break;
256  case 1:
257  if (ch_num) {
258  if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
259  return ret;
260 
261  if (chan->num_coded_vals) {
262  vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
263 
264  for (i = 0; i < chan->num_coded_vals; i++) {
265  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
266  chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
267  }
268  }
269  } else {
270  weight_idx = get_bits(gb, 2);
271  if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
272  return ret;
273 
274  if (chan->num_coded_vals) {
275  pos = get_bits(gb, 5);
276  if (pos > chan->num_coded_vals) {
277  av_log(avctx, AV_LOG_ERROR,
278  "WL mode 1: invalid position!\n");
279  return AVERROR_INVALIDDATA;
280  }
281 
282  delta_bits = get_bits(gb, 2);
283  min_val = get_bits(gb, 3);
284 
285  for (i = 0; i < pos; i++)
286  chan->qu_wordlen[i] = get_bits(gb, 3);
287 
288  for (i = pos; i < chan->num_coded_vals; i++)
289  chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7;
290  }
291  }
292  break;
293  case 2:
294  if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
295  return ret;
296 
297  if (ch_num && chan->num_coded_vals) {
298  vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
299  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
300  chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
301 
302  for (i = 1; i < chan->num_coded_vals; i++) {
303  diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
304  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
305  chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
306  }
307  } else if (chan->num_coded_vals) {
308  flag = get_bits(gb, 1);
309  vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];
310 
311  start_val = get_bits(gb, 3);
312  unpack_vq_shape(start_val,
313  &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],
314  chan->qu_wordlen, chan->num_coded_vals);
315 
316  if (!flag) {
317  for (i = 0; i < chan->num_coded_vals; i++) {
318  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
319  chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
320  }
321  } else {
322  for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
323  if (!get_bits1(gb)) {
324  chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
325  get_vlc2(gb, vlc_tab->table,
326  vlc_tab->bits, 1)) & 7;
327  chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
328  get_vlc2(gb, vlc_tab->table,
329  vlc_tab->bits, 1)) & 7;
330  }
331 
332  if (chan->num_coded_vals & 1)
333  chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
334  get_vlc2(gb, vlc_tab->table,
335  vlc_tab->bits, 1)) & 7;
336  }
337  }
338  break;
339  case 3:
340  weight_idx = get_bits(gb, 2);
341  if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
342  return ret;
343 
344  if (chan->num_coded_vals) {
345  vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
346 
347  /* first coefficient is coded directly */
348  chan->qu_wordlen[0] = get_bits(gb, 3);
349 
350  for (i = 1; i < chan->num_coded_vals; i++) {
351  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
352  chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
353  }
354  }
355  break;
356  }
357 
358  if (chan->fill_mode == 2) {
359  for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
360  chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;
361  } else if (chan->fill_mode == 3) {
362  pos = ch_num ? chan->num_coded_vals + chan->split_point
363  : ctx->num_quant_units - chan->split_point;
364  if (pos > FF_ARRAY_ELEMS(chan->qu_wordlen)) {
365  av_log(avctx, AV_LOG_ERROR, "Split point beyond array\n");
366  pos = FF_ARRAY_ELEMS(chan->qu_wordlen);
367  }
368  for (i = chan->num_coded_vals; i < pos; i++)
369  chan->qu_wordlen[i] = 1;
370  }
371 
372  if (weight_idx)
373  return add_wordlen_weights(ctx, chan, weight_idx, avctx);
374 
375  return 0;
376 }
377 
378 /**
379  * Decode scale factor indexes for each quant unit of a channel.
380  *
381  * @param[in] gb the GetBit context
382  * @param[in,out] ctx ptr to the channel unit context
383  * @param[in] ch_num channel to process
384  * @param[in] avctx ptr to the AVCodecContext
385  * @return result code: 0 = OK, otherwise - error code
386  */
388  int ch_num, AVCodecContext *avctx)
389 {
390  int i, weight_idx = 0, delta, diff, num_long_vals,
391  delta_bits, min_val, vlc_sel, start_val;
392  VLC *vlc_tab;
393  Atrac3pChanParams *chan = &ctx->channels[ch_num];
394  Atrac3pChanParams *ref_chan = &ctx->channels[0];
395 
396  switch (get_bits(gb, 2)) { /* switch according to coding mode */
397  case 0: /* coded using constant number of bits */
398  for (i = 0; i < ctx->used_quant_units; i++)
399  chan->qu_sf_idx[i] = get_bits(gb, 6);
400  break;
401  case 1:
402  if (ch_num) {
403  vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
404 
405  for (i = 0; i < ctx->used_quant_units; i++) {
406  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
407  chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
408  }
409  } else {
410  weight_idx = get_bits(gb, 2);
411  if (weight_idx == 3) {
412  UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
413 
414  num_long_vals = get_bits(gb, 5);
415  delta_bits = get_bits(gb, 2);
416  min_val = get_bits(gb, 4) - 7;
417 
418  for (i = 0; i < num_long_vals; i++)
419  chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
420  get_bits(gb, 4) - 7) & 0x3F;
421 
422  /* all others are: min_val + delta */
423  for (i = num_long_vals; i < ctx->used_quant_units; i++)
424  chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
425  get_bitsz(gb, delta_bits)) & 0x3F;
426  } else {
427  num_long_vals = get_bits(gb, 5);
428  delta_bits = get_bits(gb, 3);
429  min_val = get_bits(gb, 6);
430  if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
431  av_log(avctx, AV_LOG_ERROR,
432  "SF mode 1: invalid parameters!\n");
433  return AVERROR_INVALIDDATA;
434  }
435 
436  /* read full-precision SF indexes */
437  for (i = 0; i < num_long_vals; i++)
438  chan->qu_sf_idx[i] = get_bits(gb, 6);
439 
440  /* all others are: min_val + delta */
441  for (i = num_long_vals; i < ctx->used_quant_units; i++)
442  chan->qu_sf_idx[i] = (min_val +
443  get_bitsz(gb, delta_bits)) & 0x3F;
444  }
445  }
446  break;
447  case 2:
448  if (ch_num) {
449  vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
450 
451  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
452  chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
453 
454  for (i = 1; i < ctx->used_quant_units; i++) {
455  diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
456  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
457  chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
458  }
459  } else {
460  vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];
461 
462  UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
463 
464  for (i = 0; i < ctx->used_quant_units; i++) {
465  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
466  chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
467  sign_extend(delta, 4)) & 0x3F;
468  }
469  }
470  break;
471  case 3:
472  if (ch_num) {
473  /* copy coefficients from reference channel */
474  for (i = 0; i < ctx->used_quant_units; i++)
475  chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
476  } else {
477  weight_idx = get_bits(gb, 2);
478  vlc_sel = get_bits(gb, 2);
479  vlc_tab = &sf_vlc_tabs[vlc_sel];
480 
481  if (weight_idx == 3) {
482  vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
483 
484  UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
485 
486  diff = (get_bits(gb, 4) + 56) & 0x3F;
487  chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
488 
489  for (i = 1; i < ctx->used_quant_units; i++) {
490  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
491  diff = (diff + sign_extend(delta, 4)) & 0x3F;
492  chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
493  }
494  } else {
495  /* 1st coefficient is coded directly */
496  chan->qu_sf_idx[0] = get_bits(gb, 6);
497 
498  for (i = 1; i < ctx->used_quant_units; i++) {
499  delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
500  chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
501  }
502  }
503  }
504  break;
505  }
506 
507  if (weight_idx && weight_idx < 3)
508  return subtract_sf_weights(ctx, chan, weight_idx, avctx);
509 
510  return 0;
511 }
512 
513 /**
514  * Decode word length information for each channel.
515  *
516  * @param[in] gb the GetBit context
517  * @param[in,out] ctx ptr to the channel unit context
518  * @param[in] num_channels number of channels to process
519  * @param[in] avctx ptr to the AVCodecContext
520  * @return result code: 0 = OK, otherwise - error code
521  */
523  int num_channels, AVCodecContext *avctx)
524 {
525  int ch_num, i, ret;
526 
527  for (ch_num = 0; ch_num < num_channels; ch_num++) {
528  memset(ctx->channels[ch_num].qu_wordlen, 0,
529  sizeof(ctx->channels[ch_num].qu_wordlen));
530 
531  if ((ret = decode_channel_wordlen(gb, ctx, ch_num, avctx)) < 0)
532  return ret;
533  }
534 
535  /* scan for last non-zero coeff in both channels and
536  * set number of quant units having coded spectrum */
537  for (i = ctx->num_quant_units - 1; i >= 0; i--)
538  if (ctx->channels[0].qu_wordlen[i] ||
539  (num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
540  break;
541  ctx->used_quant_units = i + 1;
542 
543  return 0;
544 }
545 
546 /**
547  * Decode scale factor indexes for each channel.
548  *
549  * @param[in] gb the GetBit context
550  * @param[in,out] ctx ptr to the channel unit context
551  * @param[in] num_channels number of channels to process
552  * @param[in] avctx ptr to the AVCodecContext
553  * @return result code: 0 = OK, otherwise - error code
554  */
556  int num_channels, AVCodecContext *avctx)
557 {
558  int ch_num, ret;
559 
560  if (!ctx->used_quant_units)
561  return 0;
562 
563  for (ch_num = 0; ch_num < num_channels; ch_num++) {
564  memset(ctx->channels[ch_num].qu_sf_idx, 0,
565  sizeof(ctx->channels[ch_num].qu_sf_idx));
566 
567  if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0)
568  return ret;
569  }
570 
571  return 0;
572 }
573 
574 /**
575  * Decode number of code table values.
576  *
577  * @param[in] gb the GetBit context
578  * @param[in,out] ctx ptr to the channel unit context
579  * @param[in] avctx ptr to the AVCodecContext
580  * @return result code: 0 = OK, otherwise - error code
581  */
583  AVCodecContext *avctx)
584 {
585  int num_coded_vals;
586 
587  if (get_bits1(gb)) {
588  num_coded_vals = get_bits(gb, 5);
589  if (num_coded_vals > ctx->used_quant_units) {
590  av_log(avctx, AV_LOG_ERROR,
591  "Invalid number of code table indexes: %d!\n", num_coded_vals);
592  return AVERROR_INVALIDDATA;
593  }
594  return num_coded_vals;
595  } else
596  return ctx->used_quant_units;
597 }
598 
599 #define DEC_CT_IDX_COMMON(OP) \
600  num_vals = get_num_ct_values(gb, ctx, avctx); \
601  if (num_vals < 0) \
602  return num_vals; \
603  \
604  for (i = 0; i < num_vals; i++) { \
605  if (chan->qu_wordlen[i]) { \
606  chan->qu_tab_idx[i] = OP; \
607  } else if (ch_num && ref_chan->qu_wordlen[i]) \
608  /* get clone master flag */ \
609  chan->qu_tab_idx[i] = get_bits1(gb); \
610  }
611 
612 #define CODING_DIRECT get_bits(gb, num_bits)
613 
614 #define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
615 
616 #define CODING_VLC_DELTA \
617  (!i) ? CODING_VLC \
618  : (pred + get_vlc2(gb, delta_vlc->table, \
619  delta_vlc->bits, 1)) & mask; \
620  pred = chan->qu_tab_idx[i]
621 
622 #define CODING_VLC_DIFF \
623  (ref_chan->qu_tab_idx[i] + \
624  get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
625 
626 /**
627  * Decode code table indexes for each quant unit of a channel.
628  *
629  * @param[in] gb the GetBit context
630  * @param[in,out] ctx ptr to the channel unit context
631  * @param[in] ch_num channel to process
632  * @param[in] avctx ptr to the AVCodecContext
633  * @return result code: 0 = OK, otherwise - error code
634  */
636  int ch_num, AVCodecContext *avctx)
637 {
638  int i, num_vals, num_bits, pred;
639  int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
640  VLC *vlc_tab, *delta_vlc;
641  Atrac3pChanParams *chan = &ctx->channels[ch_num];
642  Atrac3pChanParams *ref_chan = &ctx->channels[0];
643 
644  chan->table_type = get_bits1(gb);
645 
646  switch (get_bits(gb, 2)) { /* switch according to coding mode */
647  case 0: /* directly coded */
648  num_bits = ctx->use_full_table + 2;
650  break;
651  case 1: /* entropy-coded */
652  vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
653  : ct_vlc_tabs;
655  break;
656  case 2: /* entropy-coded delta */
657  if (ctx->use_full_table) {
658  vlc_tab = &ct_vlc_tabs[1];
659  delta_vlc = &ct_vlc_tabs[2];
660  } else {
661  vlc_tab = ct_vlc_tabs;
662  delta_vlc = ct_vlc_tabs;
663  }
664  pred = 0;
666  break;
667  case 3: /* entropy-coded difference to master */
668  if (ch_num) {
669  vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
670  : ct_vlc_tabs;
672  }
673  break;
674  }
675 
676  return 0;
677 }
678 
679 /**
680  * Decode code table indexes for each channel.
681  *
682  * @param[in] gb the GetBit context
683  * @param[in,out] ctx ptr to the channel unit context
684  * @param[in] num_channels number of channels to process
685  * @param[in] avctx ptr to the AVCodecContext
686  * @return result code: 0 = OK, otherwise - error code
687  */
689  int num_channels, AVCodecContext *avctx)
690 {
691  int ch_num, ret;
692 
693  if (!ctx->used_quant_units)
694  return 0;
695 
696  ctx->use_full_table = get_bits1(gb);
697 
698  for (ch_num = 0; ch_num < num_channels; ch_num++) {
699  memset(ctx->channels[ch_num].qu_tab_idx, 0,
700  sizeof(ctx->channels[ch_num].qu_tab_idx));
701 
702  if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0)
703  return ret;
704  }
705 
706  return 0;
707 }
708 
709 /**
710  * Decode huffman-coded spectral lines for a given quant unit.
711  *
712  * This is a generalized version for all known coding modes.
713  * Its speed can be improved by creating separate functions for each mode.
714  *
715  * @param[in] gb the GetBit context
716  * @param[in] tab code table telling how to decode spectral lines
717  * @param[in] vlc_tab ptr to the huffman table associated with the code table
718  * @param[out] out pointer to buffer where decoded data should be stored
719  * @param[in] num_specs number of spectral lines to decode
720  */
722  VLC *vlc_tab, int16_t *out, const int num_specs)
723 {
724  int i, j, pos, cf;
725  int group_size = tab->group_size;
726  int num_coeffs = tab->num_coeffs;
727  int bits = tab->bits;
728  int is_signed = tab->is_signed;
729  unsigned val;
730 
731  for (pos = 0; pos < num_specs;) {
732  if (group_size == 1 || get_bits1(gb)) {
733  for (j = 0; j < group_size; j++) {
734  val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
735 
736  for (i = 0; i < num_coeffs; i++) {
737  cf = av_mod_uintp2(val, bits);
738  if (is_signed)
739  cf = sign_extend(cf, bits);
740  else if (cf && get_bits1(gb))
741  cf = -cf;
742 
743  out[pos++] = cf;
744  val >>= bits;
745  }
746  }
747  } else /* group skipped */
748  pos += group_size * num_coeffs;
749  }
750 }
751 
752 /**
753  * Decode huffman-coded IMDCT spectrum for all channels.
754  *
755  * @param[in] gb the GetBit context
756  * @param[in,out] ctx ptr to the channel unit context
757  * @param[in] num_channels number of channels to process
758  * @param[in] avctx ptr to the AVCodecContext
759  */
761  int num_channels, AVCodecContext *avctx)
762 {
763  int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
764  const Atrac3pSpecCodeTab *tab;
765  Atrac3pChanParams *chan;
766 
767  for (ch_num = 0; ch_num < num_channels; ch_num++) {
768  chan = &ctx->channels[ch_num];
769 
770  memset(chan->spectrum, 0, sizeof(chan->spectrum));
771 
772  /* set power compensation level to disabled */
773  memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
774 
775  for (qu = 0; qu < ctx->used_quant_units; qu++) {
776  num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] -
778 
779  wordlen = chan->qu_wordlen[qu];
780  codetab = chan->qu_tab_idx[qu];
781  if (wordlen) {
782  if (!ctx->use_full_table)
783  codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
784 
785  tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
786  tab = &atrac3p_spectra_tabs[tab_index];
787 
788  decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],
790  num_specs);
791  } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
792  /* copy coefficients from master */
793  memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
794  &ctx->channels[0].spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
795  num_specs *
796  sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
797  chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
798  }
799  }
800 
801  /* Power compensation levels only present in the bitstream
802  * if there are more than 2 quant units. The lowest two units
803  * correspond to the frequencies 0...351 Hz, whose shouldn't
804  * be affected by the power compensation. */
805  if (ctx->used_quant_units > 2) {
806  num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
807  for (i = 0; i < num_specs; i++)
808  chan->power_levs[i] = get_bits(gb, 4);
809  }
810  }
811 }
812 
813 /**
814  * Retrieve specified amount of flag bits from the input bitstream.
815  * The data can be shortened in the case of the following two common conditions:
816  * if all bits are zero then only one signal bit = 0 will be stored,
817  * if all bits are ones then two signal bits = 1,0 will be stored.
818  * Otherwise, all necessary bits will be directly stored
819  * prefixed by two signal bits = 1,1.
820  *
821  * @param[in] gb ptr to the GetBitContext
822  * @param[out] out where to place decoded flags
823  * @param[in] num_flags number of flags to process
824  * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
825  */
826 static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
827 {
828  int i, result;
829 
830  memset(out, 0, num_flags);
831 
832  result = get_bits1(gb);
833  if (result) {
834  if (get_bits1(gb))
835  for (i = 0; i < num_flags; i++)
836  out[i] = get_bits1(gb);
837  else
838  memset(out, 1, num_flags);
839  }
840 
841  return result;
842 }
843 
844 /**
845  * Decode mdct window shape flags for all channels.
846  *
847  * @param[in] gb the GetBit context
848  * @param[in,out] ctx ptr to the channel unit context
849  * @param[in] num_channels number of channels to process
850  */
852  int num_channels)
853 {
854  int ch_num;
855 
856  for (ch_num = 0; ch_num < num_channels; ch_num++)
857  get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,
858  ctx->num_subbands);
859 }
860 
861 /**
862  * Decode number of gain control points.
863  *
864  * @param[in] gb the GetBit context
865  * @param[in,out] ctx ptr to the channel unit context
866  * @param[in] ch_num channel to process
867  * @param[in] coded_subbands number of subbands to process
868  * @return result code: 0 = OK, otherwise - error code
869  */
871  int ch_num, int coded_subbands)
872 {
873  int i, delta, delta_bits, min_val;
874  Atrac3pChanParams *chan = &ctx->channels[ch_num];
875  Atrac3pChanParams *ref_chan = &ctx->channels[0];
876 
877  switch (get_bits(gb, 2)) { /* switch according to coding mode */
878  case 0: /* fixed-length coding */
879  for (i = 0; i < coded_subbands; i++)
880  chan->gain_data[i].num_points = get_bits(gb, 3);
881  break;
882  case 1: /* variable-length coding */
883  for (i = 0; i < coded_subbands; i++)
884  chan->gain_data[i].num_points =
886  gain_vlc_tabs[0].bits, 1);
887  break;
888  case 2:
889  if (ch_num) { /* VLC modulo delta to master channel */
890  for (i = 0; i < coded_subbands; i++) {
892  gain_vlc_tabs[1].bits, 1);
893  chan->gain_data[i].num_points =
894  (ref_chan->gain_data[i].num_points + delta) & 7;
895  }
896  } else { /* VLC modulo delta to previous */
897  chan->gain_data[0].num_points =
899  gain_vlc_tabs[0].bits, 1);
900 
901  for (i = 1; i < coded_subbands; i++) {
903  gain_vlc_tabs[1].bits, 1);
904  chan->gain_data[i].num_points =
905  (chan->gain_data[i - 1].num_points + delta) & 7;
906  }
907  }
908  break;
909  case 3:
910  if (ch_num) { /* copy data from master channel */
911  for (i = 0; i < coded_subbands; i++)
912  chan->gain_data[i].num_points =
913  ref_chan->gain_data[i].num_points;
914  } else { /* shorter delta to min */
915  delta_bits = get_bits(gb, 2);
916  min_val = get_bits(gb, 3);
917 
918  for (i = 0; i < coded_subbands; i++) {
919  chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits);
920  if (chan->gain_data[i].num_points > 7)
921  return AVERROR_INVALIDDATA;
922  }
923  }
924  }
925 
926  return 0;
927 }
928 
929 /**
930  * Implements coding mode 3 (slave) for gain compensation levels.
931  *
932  * @param[out] dst ptr to the output array
933  * @param[in] ref ptr to the reference channel
934  */
936 {
937  int i;
938 
939  for (i = 0; i < dst->num_points; i++)
940  dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
941 }
942 
943 /**
944  * Implements coding mode 1 (master) for gain compensation levels.
945  *
946  * @param[in] gb the GetBit context
947  * @param[in] ctx ptr to the channel unit context
948  * @param[out] dst ptr to the output array
949  */
950 static inline void gainc_level_mode1m(GetBitContext *gb,
952  AtracGainInfo *dst)
953 {
954  int i, delta;
955 
956  if (dst->num_points > 0)
957  dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,
958  gain_vlc_tabs[2].bits, 1);
959 
960  for (i = 1; i < dst->num_points; i++) {
962  gain_vlc_tabs[3].bits, 1);
963  dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
964  }
965 }
966 
967 /**
968  * Decode level code for each gain control point.
969  *
970  * @param[in] gb the GetBit context
971  * @param[in,out] ctx ptr to the channel unit context
972  * @param[in] ch_num channel to process
973  * @param[in] coded_subbands number of subbands to process
974  * @return result code: 0 = OK, otherwise - error code
975  */
977  int ch_num, int coded_subbands)
978 {
979  int sb, i, delta, delta_bits, min_val, pred;
980  Atrac3pChanParams *chan = &ctx->channels[ch_num];
981  Atrac3pChanParams *ref_chan = &ctx->channels[0];
982 
983  switch (get_bits(gb, 2)) { /* switch according to coding mode */
984  case 0: /* fixed-length coding */
985  for (sb = 0; sb < coded_subbands; sb++)
986  for (i = 0; i < chan->gain_data[sb].num_points; i++)
987  chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);
988  break;
989  case 1:
990  if (ch_num) { /* VLC modulo delta to master channel */
991  for (sb = 0; sb < coded_subbands; sb++)
992  for (i = 0; i < chan->gain_data[sb].num_points; i++) {
994  gain_vlc_tabs[5].bits, 1);
995  pred = (i >= ref_chan->gain_data[sb].num_points)
996  ? 7 : ref_chan->gain_data[sb].lev_code[i];
997  chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
998  }
999  } else { /* VLC modulo delta to previous */
1000  for (sb = 0; sb < coded_subbands; sb++)
1001  gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1002  }
1003  break;
1004  case 2:
1005  if (ch_num) { /* VLC modulo delta to previous or clone master */
1006  for (sb = 0; sb < coded_subbands; sb++)
1007  if (chan->gain_data[sb].num_points > 0) {
1008  if (get_bits1(gb))
1009  gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1010  else
1011  gainc_level_mode3s(&chan->gain_data[sb],
1012  &ref_chan->gain_data[sb]);
1013  }
1014  } else { /* VLC modulo delta to lev_codes of previous subband */
1015  if (chan->gain_data[0].num_points > 0)
1016  gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);
1017 
1018  for (sb = 1; sb < coded_subbands; sb++)
1019  for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1020  delta = get_vlc2(gb, gain_vlc_tabs[4].table,
1021  gain_vlc_tabs[4].bits, 1);
1022  pred = (i >= chan->gain_data[sb - 1].num_points)
1023  ? 7 : chan->gain_data[sb - 1].lev_code[i];
1024  chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1025  }
1026  }
1027  break;
1028  case 3:
1029  if (ch_num) { /* clone master */
1030  for (sb = 0; sb < coded_subbands; sb++)
1031  gainc_level_mode3s(&chan->gain_data[sb],
1032  &ref_chan->gain_data[sb]);
1033  } else { /* shorter delta to min */
1034  delta_bits = get_bits(gb, 2);
1035  min_val = get_bits(gb, 4);
1036 
1037  for (sb = 0; sb < coded_subbands; sb++)
1038  for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1039  chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits);
1040  if (chan->gain_data[sb].lev_code[i] > 15)
1041  return AVERROR_INVALIDDATA;
1042  }
1043  }
1044  break;
1045  }
1046 
1047  return 0;
1048 }
1049 
1050 /**
1051  * Implements coding mode 0 for gain compensation locations.
1052  *
1053  * @param[in] gb the GetBit context
1054  * @param[in] ctx ptr to the channel unit context
1055  * @param[out] dst ptr to the output array
1056  * @param[in] pos position of the value to be processed
1057  */
1059  AtracGainInfo *dst, int pos)
1060 {
1061  int delta_bits;
1062 
1063  if (!pos || dst->loc_code[pos - 1] < 15)
1064  dst->loc_code[pos] = get_bits(gb, 5);
1065  else if (dst->loc_code[pos - 1] >= 30)
1066  dst->loc_code[pos] = 31;
1067  else {
1068  delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
1069  dst->loc_code[pos] = dst->loc_code[pos - 1] +
1070  get_bits(gb, delta_bits) + 1;
1071  }
1072 }
1073 
1074 /**
1075  * Implements coding mode 1 for gain compensation locations.
1076  *
1077  * @param[in] gb the GetBit context
1078  * @param[in] ctx ptr to the channel unit context
1079  * @param[out] dst ptr to the output array
1080  */
1082  AtracGainInfo *dst)
1083 {
1084  int i;
1085  VLC *tab;
1086 
1087  if (dst->num_points > 0) {
1088  /* 1st coefficient is stored directly */
1089  dst->loc_code[0] = get_bits(gb, 5);
1090 
1091  for (i = 1; i < dst->num_points; i++) {
1092  /* switch VLC according to the curve direction
1093  * (ascending/descending) */
1094  tab = (dst->lev_code[i] <= dst->lev_code[i - 1])
1095  ? &gain_vlc_tabs[7]
1096  : &gain_vlc_tabs[9];
1097  dst->loc_code[i] = dst->loc_code[i - 1] +
1098  get_vlc2(gb, tab->table, tab->bits, 1);
1099  }
1100  }
1101 }
1102 
1103 /**
1104  * Decode location code for each gain control point.
1105  *
1106  * @param[in] gb the GetBit context
1107  * @param[in,out] ctx ptr to the channel unit context
1108  * @param[in] ch_num channel to process
1109  * @param[in] coded_subbands number of subbands to process
1110  * @param[in] avctx ptr to the AVCodecContext
1111  * @return result code: 0 = OK, otherwise - error code
1112  */
1114  int ch_num, int coded_subbands,
1115  AVCodecContext *avctx)
1116 {
1117  int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
1118  AtracGainInfo *dst, *ref;
1119  VLC *tab;
1120  Atrac3pChanParams *chan = &ctx->channels[ch_num];
1121  Atrac3pChanParams *ref_chan = &ctx->channels[0];
1122 
1123  switch (get_bits(gb, 2)) { /* switch according to coding mode */
1124  case 0: /* sequence of numbers in ascending order */
1125  for (sb = 0; sb < coded_subbands; sb++)
1126  for (i = 0; i < chan->gain_data[sb].num_points; i++)
1127  gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1128  break;
1129  case 1:
1130  if (ch_num) {
1131  for (sb = 0; sb < coded_subbands; sb++) {
1132  if (chan->gain_data[sb].num_points <= 0)
1133  continue;
1134  dst = &chan->gain_data[sb];
1135  ref = &ref_chan->gain_data[sb];
1136 
1137  /* 1st value is vlc-coded modulo delta to master */
1138  delta = get_vlc2(gb, gain_vlc_tabs[10].table,
1139  gain_vlc_tabs[10].bits, 1);
1140  pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
1141  dst->loc_code[0] = (pred + delta) & 0x1F;
1142 
1143  for (i = 1; i < dst->num_points; i++) {
1144  more_than_ref = i >= ref->num_points;
1145  if (dst->lev_code[i] > dst->lev_code[i - 1]) {
1146  /* ascending curve */
1147  if (more_than_ref) {
1148  delta =
1149  get_vlc2(gb, gain_vlc_tabs[9].table,
1150  gain_vlc_tabs[9].bits, 1);
1151  dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1152  } else {
1153  if (get_bits1(gb))
1154  gainc_loc_mode0(gb, ctx, dst, i); // direct coding
1155  else
1156  dst->loc_code[i] = ref->loc_code[i]; // clone master
1157  }
1158  } else { /* descending curve */
1159  tab = more_than_ref ? &gain_vlc_tabs[7]
1160  : &gain_vlc_tabs[10];
1161  delta = get_vlc2(gb, tab->table, tab->bits, 1);
1162  if (more_than_ref)
1163  dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1164  else
1165  dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
1166  }
1167  }
1168  }
1169  } else /* VLC delta to previous */
1170  for (sb = 0; sb < coded_subbands; sb++)
1171  gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);
1172  break;
1173  case 2:
1174  if (ch_num) {
1175  for (sb = 0; sb < coded_subbands; sb++) {
1176  if (chan->gain_data[sb].num_points <= 0)
1177  continue;
1178  dst = &chan->gain_data[sb];
1179  ref = &ref_chan->gain_data[sb];
1180  if (dst->num_points > ref->num_points || get_bits1(gb))
1181  gainc_loc_mode1(gb, ctx, dst);
1182  else /* clone master for the whole subband */
1183  for (i = 0; i < chan->gain_data[sb].num_points; i++)
1184  dst->loc_code[i] = ref->loc_code[i];
1185  }
1186  } else {
1187  /* data for the first subband is coded directly */
1188  for (i = 0; i < chan->gain_data[0].num_points; i++)
1189  gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);
1190 
1191  for (sb = 1; sb < coded_subbands; sb++) {
1192  if (chan->gain_data[sb].num_points <= 0)
1193  continue;
1194  dst = &chan->gain_data[sb];
1195 
1196  /* 1st value is vlc-coded modulo delta to the corresponding
1197  * value of the previous subband if any or zero */
1198  delta = get_vlc2(gb, gain_vlc_tabs[6].table,
1199  gain_vlc_tabs[6].bits, 1);
1200  pred = dst[-1].num_points > 0
1201  ? dst[-1].loc_code[0] : 0;
1202  dst->loc_code[0] = (pred + delta) & 0x1F;
1203 
1204  for (i = 1; i < dst->num_points; i++) {
1205  more_than_ref = i >= dst[-1].num_points;
1206  /* Select VLC table according to curve direction and
1207  * presence of prediction. */
1208  tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
1209  2 + more_than_ref + 6];
1210  delta = get_vlc2(gb, tab->table, tab->bits, 1);
1211  if (more_than_ref)
1212  dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1213  else
1214  dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
1215  }
1216  }
1217  }
1218  break;
1219  case 3:
1220  if (ch_num) { /* clone master or direct or direct coding */
1221  for (sb = 0; sb < coded_subbands; sb++)
1222  for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1223  if (i >= ref_chan->gain_data[sb].num_points)
1224  gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1225  else
1226  chan->gain_data[sb].loc_code[i] =
1227  ref_chan->gain_data[sb].loc_code[i];
1228  }
1229  } else { /* shorter delta to min */
1230  delta_bits = get_bits(gb, 2) + 1;
1231  min_val = get_bits(gb, 5);
1232 
1233  for (sb = 0; sb < coded_subbands; sb++)
1234  for (i = 0; i < chan->gain_data[sb].num_points; i++)
1235  chan->gain_data[sb].loc_code[i] = min_val + i +
1236  get_bits(gb, delta_bits);
1237  }
1238  break;
1239  }
1240 
1241  /* Validate decoded information */
1242  for (sb = 0; sb < coded_subbands; sb++) {
1243  dst = &chan->gain_data[sb];
1244  for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1245  if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
1246  (i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
1247  av_log(avctx, AV_LOG_ERROR,
1248  "Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
1249  ch_num, sb, i, dst->loc_code[i]);
1250  return AVERROR_INVALIDDATA;
1251  }
1252  }
1253  }
1254 
1255  return 0;
1256 }
1257 
1258 /**
1259  * Decode gain control data for all channels.
1260  *
1261  * @param[in] gb the GetBit context
1262  * @param[in,out] ctx ptr to the channel unit context
1263  * @param[in] num_channels number of channels to process
1264  * @param[in] avctx ptr to the AVCodecContext
1265  * @return result code: 0 = OK, otherwise - error code
1266  */
1268  int num_channels, AVCodecContext *avctx)
1269 {
1270  int ch_num, coded_subbands, sb, ret;
1271 
1272  for (ch_num = 0; ch_num < num_channels; ch_num++) {
1273  memset(ctx->channels[ch_num].gain_data, 0,
1274  sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
1275 
1276  if (get_bits1(gb)) { /* gain control data present? */
1277  coded_subbands = get_bits(gb, 4) + 1;
1278  if (get_bits1(gb)) /* is high band gain data replication on? */
1279  ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;
1280  else
1281  ctx->channels[ch_num].num_gain_subbands = coded_subbands;
1282 
1283  if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||
1284  (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 ||
1285  (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0)
1286  return ret;
1287 
1288  if (coded_subbands > 0) { /* propagate gain data if requested */
1289  for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
1290  ctx->channels[ch_num].gain_data[sb] =
1291  ctx->channels[ch_num].gain_data[sb - 1];
1292  }
1293  } else {
1294  ctx->channels[ch_num].num_gain_subbands = 0;
1295  }
1296  }
1297 
1298  return 0;
1299 }
1300 
1301 /**
1302  * Decode envelope for all tones of a channel.
1303  *
1304  * @param[in] gb the GetBit context
1305  * @param[in,out] ctx ptr to the channel unit context
1306  * @param[in] ch_num channel to process
1307  * @param[in] band_has_tones ptr to an array of per-band-flags:
1308  * 1 - tone data present
1309  */
1311  int ch_num, int band_has_tones[])
1312 {
1313  int sb;
1314  Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1315  Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1316 
1317  if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1318  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1319  if (!band_has_tones[sb])
1320  continue;
1321  dst[sb].pend_env.has_start_point = get_bits1(gb);
1322  dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
1323  ? get_bits(gb, 5) : -1;
1324  dst[sb].pend_env.has_stop_point = get_bits1(gb);
1325  dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
1326  ? get_bits(gb, 5) : 32;
1327  }
1328  } else { /* mode 1(slave only): copy master */
1329  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1330  if (!band_has_tones[sb])
1331  continue;
1332  dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
1333  dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;
1334  dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;
1335  dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;
1336  }
1337  }
1338 }
1339 
1340 /**
1341  * Decode number of tones for each subband of a channel.
1342  *
1343  * @param[in] gb the GetBit context
1344  * @param[in,out] ctx ptr to the channel unit context
1345  * @param[in] ch_num channel to process
1346  * @param[in] band_has_tones ptr to an array of per-band-flags:
1347  * 1 - tone data present
1348  * @param[in] avctx ptr to the AVCodecContext
1349  * @return result code: 0 = OK, otherwise - error code
1350  */
1352  int ch_num, int band_has_tones[],
1353  AVCodecContext *avctx)
1354 {
1355  int mode, sb, delta;
1356  Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1357  Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1358 
1359  mode = get_bits(gb, ch_num + 1);
1360  switch (mode) {
1361  case 0: /** fixed-length coding */
1362  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1363  if (band_has_tones[sb])
1364  dst[sb].num_wavs = get_bits(gb, 4);
1365  break;
1366  case 1: /** variable-length coding */
1367  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1368  if (band_has_tones[sb])
1369  dst[sb].num_wavs =
1370  get_vlc2(gb, tone_vlc_tabs[1].table,
1371  tone_vlc_tabs[1].bits, 1);
1372  break;
1373  case 2: /** VLC modulo delta to master (slave only) */
1374  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1375  if (band_has_tones[sb]) {
1376  delta = get_vlc2(gb, tone_vlc_tabs[2].table,
1377  tone_vlc_tabs[2].bits, 1);
1378  delta = sign_extend(delta, 3);
1379  dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
1380  }
1381  break;
1382  case 3: /** copy master (slave only) */
1383  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1384  if (band_has_tones[sb])
1385  dst[sb].num_wavs = ref[sb].num_wavs;
1386  break;
1387  }
1388 
1389  /** initialize start tone index for each subband */
1390  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1391  if (band_has_tones[sb]) {
1392  if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
1393  av_log(avctx, AV_LOG_ERROR,
1394  "Too many tones: %d (max. 48), frame: %"PRId64"!\n",
1395  ctx->waves_info->tones_index + dst[sb].num_wavs,
1396  avctx->frame_num);
1397  return AVERROR_INVALIDDATA;
1398  }
1399  dst[sb].start_index = ctx->waves_info->tones_index;
1400  ctx->waves_info->tones_index += dst[sb].num_wavs;
1401  }
1402 
1403  return 0;
1404 }
1405 
1406 /**
1407  * Decode frequency information for each subband of a channel.
1408  *
1409  * @param[in] gb the GetBit context
1410  * @param[in,out] ctx ptr to the channel unit context
1411  * @param[in] ch_num channel to process
1412  * @param[in] band_has_tones ptr to an array of per-band-flags:
1413  * 1 - tone data present
1414  */
1416  int ch_num, int band_has_tones[])
1417 {
1418  int sb, i, direction, nbits, pred, delta;
1419  Atrac3pWaveParam *iwav, *owav;
1420  Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1421  Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1422 
1423  if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1424  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1425  if (!band_has_tones[sb] || !dst[sb].num_wavs)
1426  continue;
1427  iwav = &ctx->waves_info->waves[dst[sb].start_index];
1428  direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;
1429  if (direction) { /** packed numbers in descending order */
1430  if (dst[sb].num_wavs)
1431  iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);
1432  for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
1433  nbits = av_log2(iwav[i+1].freq_index) + 1;
1434  iwav[i].freq_index = get_bits(gb, nbits);
1435  }
1436  } else { /** packed numbers in ascending order */
1437  for (i = 0; i < dst[sb].num_wavs; i++) {
1438  if (!i || iwav[i - 1].freq_index < 512)
1439  iwav[i].freq_index = get_bits(gb, 10);
1440  else {
1441  nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
1442  iwav[i].freq_index = get_bits(gb, nbits) +
1443  1024 - (1 << nbits);
1444  }
1445  }
1446  }
1447  }
1448  } else { /* mode 1: VLC modulo delta to master (slave only) */
1449  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1450  if (!band_has_tones[sb] || !dst[sb].num_wavs)
1451  continue;
1452  iwav = &ctx->waves_info->waves[ref[sb].start_index];
1453  owav = &ctx->waves_info->waves[dst[sb].start_index];
1454  for (i = 0; i < dst[sb].num_wavs; i++) {
1455  delta = get_vlc2(gb, tone_vlc_tabs[6].table,
1456  tone_vlc_tabs[6].bits, 1);
1457  delta = sign_extend(delta, 8);
1458  pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
1459  (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
1460  owav[i].freq_index = (pred + delta) & 0x3FF;
1461  }
1462  }
1463  }
1464 }
1465 
1466 /**
1467  * Decode amplitude information for each subband of a channel.
1468  *
1469  * @param[in] gb the GetBit context
1470  * @param[in,out] ctx ptr to the channel unit context
1471  * @param[in] ch_num channel to process
1472  * @param[in] band_has_tones ptr to an array of per-band-flags:
1473  * 1 - tone data present
1474  */
1476  int ch_num, int band_has_tones[])
1477 {
1478  int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
1479  Atrac3pWaveParam *wsrc, *wref;
1480  int refwaves[48] = { 0 };
1481  Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1482  Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1483 
1484  if (ch_num) {
1485  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1486  if (!band_has_tones[sb] || !dst[sb].num_wavs)
1487  continue;
1488  wsrc = &ctx->waves_info->waves[dst[sb].start_index];
1489  wref = &ctx->waves_info->waves[ref[sb].start_index];
1490  for (j = 0; j < dst[sb].num_wavs; j++) {
1491  for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
1492  diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
1493  if (diff < maxdiff) {
1494  maxdiff = diff;
1495  fi = i;
1496  }
1497  }
1498 
1499  if (maxdiff < 8)
1500  refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
1501  else if (j < ref[sb].num_wavs)
1502  refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
1503  else
1504  refwaves[dst[sb].start_index + j] = -1;
1505  }
1506  }
1507  }
1508 
1509  mode = get_bits(gb, ch_num + 1);
1510 
1511  switch (mode) {
1512  case 0: /** fixed-length coding */
1513  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1514  if (!band_has_tones[sb] || !dst[sb].num_wavs)
1515  continue;
1516  if (ctx->waves_info->amplitude_mode)
1517  for (i = 0; i < dst[sb].num_wavs; i++)
1518  ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);
1519  else
1520  ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);
1521  }
1522  break;
1523  case 1: /** min + VLC delta */
1524  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1525  if (!band_has_tones[sb] || !dst[sb].num_wavs)
1526  continue;
1527  if (ctx->waves_info->amplitude_mode)
1528  for (i = 0; i < dst[sb].num_wavs; i++)
1529  ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1530  get_vlc2(gb, tone_vlc_tabs[3].table,
1531  tone_vlc_tabs[3].bits, 1) + 20;
1532  else
1533  ctx->waves_info->waves[dst[sb].start_index].amp_sf =
1534  get_vlc2(gb, tone_vlc_tabs[4].table,
1535  tone_vlc_tabs[4].bits, 1) + 24;
1536  }
1537  break;
1538  case 2: /** VLC modulo delta to master (slave only) */
1539  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1540  if (!band_has_tones[sb] || !dst[sb].num_wavs)
1541  continue;
1542  for (i = 0; i < dst[sb].num_wavs; i++) {
1543  delta = get_vlc2(gb, tone_vlc_tabs[5].table,
1544  tone_vlc_tabs[5].bits, 1);
1545  delta = sign_extend(delta, 5);
1546  pred = refwaves[dst[sb].start_index + i] >= 0 ?
1547  ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
1548  ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
1549  }
1550  }
1551  break;
1552  case 3: /** clone master (slave only) */
1553  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1554  if (!band_has_tones[sb])
1555  continue;
1556  for (i = 0; i < dst[sb].num_wavs; i++)
1557  ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1558  refwaves[dst[sb].start_index + i] >= 0
1559  ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
1560  : 32;
1561  }
1562  break;
1563  }
1564 }
1565 
1566 /**
1567  * Decode phase information for each subband of a channel.
1568  *
1569  * @param[in] gb the GetBit context
1570  * @param[in,out] ctx ptr to the channel unit context
1571  * @param[in] ch_num channel to process
1572  * @param[in] band_has_tones ptr to an array of per-band-flags:
1573  * 1 - tone data present
1574  */
1576  int ch_num, int band_has_tones[])
1577 {
1578  int sb, i;
1579  Atrac3pWaveParam *wparam;
1580  Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1581 
1582  for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1583  if (!band_has_tones[sb])
1584  continue;
1585  wparam = &ctx->waves_info->waves[dst[sb].start_index];
1586  for (i = 0; i < dst[sb].num_wavs; i++)
1587  wparam[i].phase_index = get_bits(gb, 5);
1588  }
1589 }
1590 
1591 /**
1592  * Decode tones info for all channels.
1593  *
1594  * @param[in] gb the GetBit context
1595  * @param[in,out] ctx ptr to the channel unit context
1596  * @param[in] num_channels number of channels to process
1597  * @param[in] avctx ptr to the AVCodecContext
1598  * @return result code: 0 = OK, otherwise - error code
1599  */
1601  int num_channels, AVCodecContext *avctx)
1602 {
1603  int ch_num, i, ret;
1604  int band_has_tones[16];
1605 
1606  for (ch_num = 0; ch_num < num_channels; ch_num++)
1607  memset(ctx->channels[ch_num].tones_info, 0,
1608  sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
1609 
1610  ctx->waves_info->tones_present = get_bits1(gb);
1611  if (!ctx->waves_info->tones_present)
1612  return 0;
1613 
1614  memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
1615 
1616  ctx->waves_info->amplitude_mode = get_bits1(gb);
1617  if (!ctx->waves_info->amplitude_mode) {
1618  avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
1619  return AVERROR_PATCHWELCOME;
1620  }
1621 
1622  ctx->waves_info->num_tone_bands =
1623  get_vlc2(gb, tone_vlc_tabs[0].table,
1624  tone_vlc_tabs[0].bits, 1) + 1;
1625 
1626  if (num_channels == 2) {
1627  get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
1628  get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
1629  get_subband_flags(gb, ctx->waves_info->invert_phase, ctx->waves_info->num_tone_bands);
1630  }
1631 
1632  ctx->waves_info->tones_index = 0;
1633 
1634  for (ch_num = 0; ch_num < num_channels; ch_num++) {
1635  for (i = 0; i < ctx->waves_info->num_tone_bands; i++)
1636  band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];
1637 
1638  decode_tones_envelope(gb, ctx, ch_num, band_has_tones);
1639  if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones,
1640  avctx)) < 0)
1641  return ret;
1642 
1643  decode_tones_frequency(gb, ctx, ch_num, band_has_tones);
1644  decode_tones_amplitude(gb, ctx, ch_num, band_has_tones);
1645  decode_tones_phase(gb, ctx, ch_num, band_has_tones);
1646  }
1647 
1648  if (num_channels == 2) {
1649  for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {
1650  if (ctx->waves_info->tone_sharing[i])
1651  ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];
1652 
1653  if (ctx->waves_info->tone_master[i])
1654  FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
1655  ctx->channels[1].tones_info[i]);
1656  }
1657  }
1658 
1659  return 0;
1660 }
1661 
1663  int num_channels, AVCodecContext *avctx)
1664 {
1665  int ret;
1666 
1667  /* parse sound header */
1668  ctx->num_quant_units = get_bits(gb, 5) + 1;
1669  if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {
1670  av_log(avctx, AV_LOG_ERROR,
1671  "Invalid number of quantization units: %d!\n",
1672  ctx->num_quant_units);
1673  return AVERROR_INVALIDDATA;
1674  }
1675 
1676  ctx->mute_flag = get_bits1(gb);
1677 
1678  /* decode various sound parameters */
1679  if ((ret = decode_quant_wordlen(gb, ctx, num_channels, avctx)) < 0)
1680  return ret;
1681 
1682  ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;
1683  ctx->num_coded_subbands = ctx->used_quant_units
1684  ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1
1685  : 0;
1686 
1687  if ((ret = decode_scale_factors(gb, ctx, num_channels, avctx)) < 0)
1688  return ret;
1689 
1690  if ((ret = decode_code_table_indexes(gb, ctx, num_channels, avctx)) < 0)
1691  return ret;
1692 
1693  decode_spectrum(gb, ctx, num_channels, avctx);
1694 
1695  if (num_channels == 2) {
1696  get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands);
1697  get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands);
1698  }
1699 
1700  decode_window_shape(gb, ctx, num_channels);
1701 
1702  if ((ret = decode_gainc_data(gb, ctx, num_channels, avctx)) < 0)
1703  return ret;
1704 
1705  if ((ret = decode_tones_info(gb, ctx, num_channels, avctx)) < 0)
1706  return ret;
1707 
1708  /* decode global noise info */
1709  ctx->noise_present = get_bits1(gb);
1710  if (ctx->noise_present) {
1711  ctx->noise_level_index = get_bits(gb, 4);
1712  ctx->noise_table_index = get_bits(gb, 4);
1713  }
1714 
1715  return 0;
1716 }
atrac3p_spectra_tabs
static const Atrac3pSpecCodeTab atrac3p_spectra_tabs[112]
Definition: atrac3plus_data.h:1414
atrac3p_tone_xlats
static const uint8_t atrac3p_tone_xlats[]
Definition: atrac3plus_data.h:1618
get_num_ct_values
static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
Decode number of code table values.
Definition: atrac3plus.c:582
get_subband_flags
static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
Retrieve specified amount of flag bits from the input bitstream.
Definition: atrac3plus.c:826
ff_vlc_init_from_lengths
int ff_vlc_init_from_lengths(VLC *vlc, 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, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: vlc.c:306
wl_vlc_tabs
static VLC wl_vlc_tabs[4]
Definition: atrac3plus.c:35
out
FILE * out
Definition: movenc.c:54
Atrac3pWaveEnvelope::has_stop_point
int has_stop_point
indicates stop point within the GHA window
Definition: atrac3plus.h:69
CODING_VLC_DELTA
#define CODING_VLC_DELTA
Definition: atrac3plus.c:616
decode_band_numwavs
static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[], AVCodecContext *avctx)
Decode number of tones for each subband of a channel.
Definition: atrac3plus.c:1351
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:241
Atrac3pChanParams::qu_tab_idx
int qu_tab_idx[32]
array of code table indexes for each quant unit
Definition: atrac3plus.h:99
Atrac3pWavesData::num_wavs
int num_wavs
number of sine waves in the group
Definition: atrac3plus.h:78
atrac3p_qu_num_to_seg
static const uint8_t atrac3p_qu_num_to_seg[32]
Ungroup table for word length segments.
Definition: atrac3plus_data.h:135
decode_quant_wordlen
static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx)
Decode word length information for each channel.
Definition: atrac3plus.c:522
Atrac3pChanParams::ch_num
int ch_num
Definition: atrac3plus.h:92
av_mod_uintp2
#define av_mod_uintp2
Definition: common.h:123
decode_tones_info
static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx)
Decode tones info for all channels.
Definition: atrac3plus.c:1600
b
#define b
Definition: input.c:41
table
static const uint16_t table[]
Definition: prosumer.c:205
Atrac3pChanParams::fill_mode
int fill_mode
Definition: atrac3plus.h:94
ct_vlc_tabs
static VLC ct_vlc_tabs[4]
Definition: atrac3plus.c:37
decode_gainc_data
static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx)
Decode gain control data for all channels.
Definition: atrac3plus.c:1267
DEC_CT_IDX_COMMON
#define DEC_CT_IDX_COMMON(OP)
Definition: atrac3plus.c:599
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
atrac3p_sf_cbs
static const uint8_t atrac3p_sf_cbs[][12]
VLC tables for scale factor indexes.
Definition: atrac3plus_data.h:65
subtract_sf_weights
static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx, Atrac3pChanParams *chan, int wtab_idx, AVCodecContext *avctx)
Subtract weighting coefficients from decoded scalefactors.
Definition: atrac3plus.c:186
GetBitContext
Definition: get_bits.h:108
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
CODING_VLC
#define CODING_VLC
Definition: atrac3plus.c:614
val
static double val(void *priv, double ch)
Definition: aeval.c:78
avassert.h
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
num_coded_units
static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan, Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
Decode number of coded quantization units.
Definition: atrac3plus.c:126
mask
static const uint16_t mask[17]
Definition: lzw.c:38
decode_tones_frequency
static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[])
Decode frequency information for each subband of a channel.
Definition: atrac3plus.c:1415
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AtracGainInfo::num_points
int num_points
number of gain control points
Definition: atrac.h:36
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
ff_atrac3p_qu_to_spec_pos
const uint16_t ff_atrac3p_qu_to_spec_pos[33]
Map quant unit number to its position in the spectrum.
Definition: atrac3plusdsp.c:42
decode_channel_code_tab
static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, AVCodecContext *avctx)
Decode code table indexes for each quant unit of a channel.
Definition: atrac3plus.c:635
atrac3p_spectra_cbs
static const int8_t atrac3p_spectra_cbs[][12]
Tables for spectrum coding.
Definition: atrac3plus_data.h:377
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
sf_vlc_tabs
static VLC sf_vlc_tabs[8]
Definition: atrac3plus.c:36
atrac3p_tone_cbs
static const uint8_t atrac3p_tone_cbs[][12]
Definition: atrac3plus_data.h:1608
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
atrac3p_spectra_xlats
static const uint8_t atrac3p_spectra_xlats[]
Definition: atrac3plus_data.h:492
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
atrac3plus_data.h
decode_gainc_loc_codes
static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int coded_subbands, AVCodecContext *avctx)
Decode location code for each gain control point.
Definition: atrac3plus.c:1113
CODING_DIRECT
#define CODING_DIRECT
Definition: atrac3plus.c:612
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
Atrac3pWavesData::pend_env
Atrac3pWaveEnvelope pend_env
pending envelope from the previous frame
Definition: atrac3plus.h:76
atrac3p_gain_cbs
static const uint8_t atrac3p_gain_cbs[][12]
Definition: atrac3plus_data.h:1563
Atrac3pChanParams::spectrum
int16_t spectrum[2048]
decoded IMDCT spectrum
Definition: atrac3plus.h:100
Atrac3pChanParams::num_coded_vals
int num_coded_vals
number of transmitted quant unit values
Definition: atrac3plus.h:93
spec_vlc_tabs
static VLC spec_vlc_tabs[112]
Definition: atrac3plus.c:38
AtracGainInfo
Gain control parameters for one subband.
Definition: atrac.h:35
atrac3plus.h
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
index
int index
Definition: gxfenc.c:89
Atrac3pWaveEnvelope::has_start_point
int has_start_point
indicates start point within the GHA window
Definition: atrac3plus.h:68
VLC::table_allocated
int table_allocated
Definition: vlc.h:36
gainc_loc_mode1
static void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, AtracGainInfo *dst)
Implements coding mode 1 for gain compensation locations.
Definition: atrac3plus.c:1081
decode_tones_envelope
static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[])
Decode envelope for all tones of a channel.
Definition: atrac3plus.c:1310
Atrac3pWavesData::start_index
int start_index
start index into global tones table for that subband
Definition: atrac3plus.h:79
ff_atrac3p_init_vlcs
av_cold void ff_atrac3p_init_vlcs(void)
Initialize VLC tables for bitstream parsing.
Definition: atrac3plus.c:76
decode_gainc_levels
static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int coded_subbands)
Decode level code for each gain control point.
Definition: atrac3plus.c:976
UNPACK_SF_VQ_SHAPE
#define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals)
Definition: atrac3plus.c:226
AtracGainInfo::loc_code
int loc_code[7]
location of gain control points
Definition: atrac.h:38
Atrac3pChanParams::qu_sf_idx
int qu_sf_idx[32]
array of scale factor indexes for each quant unit
Definition: atrac3plus.h:98
VLCElem
Definition: vlc.h:29
decode_gainc_npoints
static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int coded_subbands)
Decode number of gain control points.
Definition: atrac3plus.c:870
atrac3p_wl_ct_xlats
static const uint8_t atrac3p_wl_ct_xlats[]
Definition: atrac3plus_data.h:45
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
atrac3p_subband_to_num_powgrps
static const int atrac3p_subband_to_num_powgrps[16]
Map subband number to number of power compensation groups.
Definition: atrac3plus_data.h:147
build_canonical_huff
static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t **xlat, int *tab_offset, VLC *out_vlc)
Generate canonical VLC table from given descriptor.
Definition: atrac3plus.c:50
atrac3p_ct_cbs
static const uint8_t atrac3p_ct_cbs[][12]
VLC tables for code table indexes.
Definition: atrac3plus_data.h:38
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:164
Atrac3pChanParams
Sound channel parameters.
Definition: atrac3plus.h:91
ff_atrac3p_decode_channel_unit
int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx)
Decode bitstream data of a channel unit.
Definition: atrac3plus.c:1662
gainc_level_mode1m
static void gainc_level_mode1m(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, AtracGainInfo *dst)
Implements coding mode 1 (master) for gain compensation levels.
Definition: atrac3plus.c:950
flag
#define flag(name)
Definition: cbs_av1.c:466
add_wordlen_weights
static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx, Atrac3pChanParams *chan, int wtab_idx, AVCodecContext *avctx)
Add weighting coefficients to the decoded word-length information.
Definition: atrac3plus.c:156
decode_tones_phase
static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[])
Decode phase information for each subband of a channel.
Definition: atrac3plus.c:1575
atrac3p_sf_xlats
static const uint8_t atrac3p_sf_xlats[]
Definition: atrac3plus_data.h:76
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
decode_scale_factors
static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx)
Decode scale factor indexes for each channel.
Definition: atrac3plus.c:555
Atrac3pWaveParam
Parameters of a single sine wave.
Definition: atrac3plus.h:83
delta
float delta
Definition: vorbis_enc_data.h:430
tables_data
static VLCElem tables_data[154276]
Definition: atrac3plus.c:34
atrac3p_wl_weights
static const int8_t atrac3p_wl_weights[6][32]
Definition: atrac3plus_data.h:108
Atrac3pWaveEnvelope::stop_pos
int stop_pos
stop position expressed in n*4 samples
Definition: atrac3plus.h:71
tone_vlc_tabs
static VLC tone_vlc_tabs[7]
Definition: atrac3plus.c:40
atrac3p_wl_shapes
static const int8_t atrac3p_wl_shapes[8][16][9]
3D base shape tables.
Definition: atrac3plus_data.h:155
atrac3p_gain_xlats
static const uint8_t atrac3p_gain_xlats[]
Definition: atrac3plus_data.h:1577
avcodec.h
VLC::bits
int bits
Definition: vlc.h:34
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2118
CODING_VLC_DIFF
#define CODING_VLC_DIFF
Definition: atrac3plus.c:622
ret
ret
Definition: filter_design.txt:187
Atrac3pChanUnitCtx
Channel unit parameters.
Definition: atrac3plus.h:133
pred
static const float pred[4]
Definition: siprdata.h:259
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
pos
unsigned int pos
Definition: spdifenc.c:413
AtracGainInfo::lev_code
int lev_code[7]
level at corresponding control point
Definition: atrac.h:37
Atrac3pChanParams::power_levs
uint8_t power_levs[5]
power compensation levels
Definition: atrac3plus.h:101
decode_tones_amplitude
static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, int band_has_tones[])
Decode amplitude information for each subband of a channel.
Definition: atrac3plus.c:1475
Atrac3pWaveParam::freq_index
int freq_index
wave frequency index
Definition: atrac3plus.h:84
AVCodecContext
main external API structure.
Definition: avcodec.h:441
Atrac3pWavesData
Parameters of a group of sine waves.
Definition: atrac3plus.h:75
mode
mode
Definition: ebur128.h:83
VLC
Definition: vlc.h:33
Atrac3pSpecCodeTab
Tables for spectrum coding.
Definition: atrac3plus_data.h:1407
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:133
atrac3p_wl_cbs
static const uint8_t atrac3p_wl_cbs[][12]
VLC tables for wordlen.
Definition: atrac3plus_data.h:30
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
gain_vlc_tabs
static VLC gain_vlc_tabs[11]
Definition: atrac3plus.c:39
Atrac3pChanParams::table_type
int table_type
table type: 0 - tone?, 1- noise?
Definition: atrac3plus.h:96
VLC::table
VLCElem * table
Definition: vlc.h:35
qu
static const float qu[2]
Definition: sipr16kdata.h:28
gainc_level_mode3s
static void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
Implements coding mode 3 (slave) for gain compensation levels.
Definition: atrac3plus.c:935
Atrac3pChanParams::split_point
int split_point
Definition: atrac3plus.h:95
decode_channel_wordlen
static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, AVCodecContext *avctx)
Decode word length for each quantization unit of a channel.
Definition: atrac3plus.c:240
get_bitsz
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:351
atrac3p_sf_weights
static const int8_t atrac3p_sf_weights[2][32]
Definition: atrac3plus_data.h:126
ATRAC3P_SUBBANDS
#define ATRAC3P_SUBBANDS
Global unit sizes.
Definition: atrac3plus.h:42
decode_qu_spectra
static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab, VLC *vlc_tab, int16_t *out, const int num_specs)
Decode huffman-coded spectral lines for a given quant unit.
Definition: atrac3plus.c:721
decode_channel_sf_idx
static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num, AVCodecContext *avctx)
Decode scale factor indexes for each quant unit of a channel.
Definition: atrac3plus.c:387
atrac3p_ct_restricted_to_full
static const uint8_t atrac3p_ct_restricted_to_full[2][7][4]
Definition: atrac3plus_data.h:356
decode_spectrum
static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx)
Decode huffman-coded IMDCT spectrum for all channels.
Definition: atrac3plus.c:760
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
VLC_INIT_USE_STATIC
#define VLC_INIT_USE_STATIC
Definition: vlc.h:176
Atrac3pChanParams::qu_wordlen
int qu_wordlen[32]
array of word lengths for each quant unit
Definition: atrac3plus.h:97
decode_code_table_indexes
static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels, AVCodecContext *avctx)
Decode code table indexes for each channel.
Definition: atrac3plus.c:688
Atrac3pChanParams::gain_data
AtracGainInfo * gain_data
gain control data for next frame
Definition: atrac3plus.h:110
ATRAC3P_POWER_COMP_OFF
#define ATRAC3P_POWER_COMP_OFF
Global constants.
Definition: atrac3plus.h:49
decode_window_shape
static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int num_channels)
Decode mdct window shape flags for all channels.
Definition: atrac3plus.c:851
Atrac3pWaveEnvelope::start_pos
int start_pos
start position expressed in n*4 samples
Definition: atrac3plus.h:70
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
atrac3p_qu_to_subband
static const uint8_t atrac3p_qu_to_subband[32]
Map quant unit number to subband number.
Definition: atrac3plus_data.h:141
gainc_loc_mode0
static void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, AtracGainInfo *dst, int pos)
Implements coding mode 0 for gain compensation locations.
Definition: atrac3plus.c:1058
unpack_vq_shape
static void unpack_vq_shape(int start_val, const int8_t *shape_vec, int *dst, int num_values)
Unpack vector quantization tables.
Definition: atrac3plus.c:214