2 * ATRAC3+ compatible decoder
4 * Copyright (c) 2010-2013 Maxim Poliakovski
6 * This file is part of FFmpeg.
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.
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.
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
25 * Bitstream parser for ATRAC3+ decoder.
28 #include "libavutil/avassert.h"
31 #include "atrac3plus.h"
32 #include "atrac3plus_data.h"
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];
43 * Generate canonical VLC table from given descriptor.
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
50 static av_cold
void build_canonical_huff(const uint8_t *cb
, const uint8_t **xlat
,
51 int *tab_offset
, VLC
*out_vlc
)
57 for (int b
= 1; b
<= 12; b
++) {
58 for (i
= *cb
++; i
> 0; i
--) {
59 av_assert0(index
< 256);
64 max_len
= bits
[index
- 1];
66 out_vlc
->table
= &tables_data
[*tab_offset
];
67 out_vlc
->table_allocated
= 1 << max_len
;
69 ff_vlc_init_from_lengths(out_vlc
, max_len
, index
, bits
, 1,
70 *xlat
, 1, 1, 0, VLC_INIT_USE_STATIC
, NULL
);
72 *tab_offset
+= 1 << max_len
;
76 av_cold
void ff_atrac3p_init_vlcs(void)
78 int i
, tab_offset
= 0;
81 xlats
= atrac3p_wl_ct_xlats
;
82 for (int i
= 0; i
< 4; i
++) {
83 build_canonical_huff(atrac3p_wl_cbs
[i
], &xlats
,
84 &tab_offset
, &wl_vlc_tabs
[i
]);
85 build_canonical_huff(atrac3p_ct_cbs
[i
], &xlats
,
86 &tab_offset
, &ct_vlc_tabs
[i
]);
89 xlats
= atrac3p_sf_xlats
;
90 for (int i
= 0; i
< 8; i
++)
91 build_canonical_huff(atrac3p_sf_cbs
[i
], &xlats
,
92 &tab_offset
, &sf_vlc_tabs
[i
]);
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)
98 build_canonical_huff(atrac3p_spectra_cbs
[i
],
99 &xlats
, &tab_offset
, &spec_vlc_tabs
[i
]);
100 else /* Reuse already initialized VLC table */
101 spec_vlc_tabs
[i
] = spec_vlc_tabs
[-atrac3p_spectra_cbs
[i
][0]];
104 /* build huffman tables for gain data decoding */
105 xlats
= atrac3p_gain_xlats
;
106 for (i
= 0; i
< 11; i
++)
107 build_canonical_huff(atrac3p_gain_cbs
[i
], &xlats
,
108 &tab_offset
, &gain_vlc_tabs
[i
]);
110 /* build huffman tables for tone decoding */
111 xlats
= atrac3p_tone_xlats
;
112 for (i
= 0; i
< 7; i
++)
113 build_canonical_huff(atrac3p_tone_cbs
[i
], &xlats
,
114 &tab_offset
, &tone_vlc_tabs
[i
]);
118 * Decode number of coded quantization units.
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
126 static int num_coded_units(GetBitContext
*gb
, Atrac3pChanParams
*chan
,
127 Atrac3pChanUnitCtx
*ctx
, AVCodecContext
*avctx
)
129 chan
->fill_mode
= get_bits(gb
, 2);
130 if (!chan
->fill_mode
) {
131 chan
->num_coded_vals
= ctx
->num_quant_units
;
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
;
140 if (chan
->fill_mode
== 3)
141 chan
->split_point
= get_bits(gb
, 2) + (chan
->ch_num
<< 1) + 1;
148 * Add weighting coefficients to the decoded word-length information.
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
156 static int add_wordlen_weights(Atrac3pChanUnitCtx
*ctx
,
157 Atrac3pChanParams
*chan
, int wtab_idx
,
158 AVCodecContext
*avctx
)
161 const int8_t *weights_tab
=
162 &atrac3p_wl_weights
[chan
->ch_num
* 3 + wtab_idx
- 1][0];
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
;
178 * Subtract weighting coefficients from decoded scalefactors.
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
186 static int subtract_sf_weights(Atrac3pChanUnitCtx
*ctx
,
187 Atrac3pChanParams
*chan
, int wtab_idx
,
188 AVCodecContext
*avctx
)
191 const int8_t *weights_tab
= &atrac3p_sf_weights
[wtab_idx
- 1][0];
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
;
207 * Unpack vector quantization tables.
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
214 static inline void unpack_vq_shape(int start_val
, const int8_t *shape_vec
,
215 int *dst
, int 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];
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], \
232 * Decode word length for each quantization unit of a channel.
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
240 static int decode_channel_wordlen(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
241 int ch_num
, AVCodecContext
*avctx
)
243 int i
, weight_idx
= 0, delta
, diff
, pos
, delta_bits
, min_val
, flag
,
246 Atrac3pChanParams
*chan
= &ctx
->channels
[ch_num
];
247 Atrac3pChanParams
*ref_chan
= &ctx
->channels
[0];
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);
258 if ((ret
= num_coded_units(gb
, chan
, ctx
, avctx
)) < 0)
261 if (chan
->num_coded_vals
) {
262 vlc_tab
= &wl_vlc_tabs
[get_bits(gb
, 2)];
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;
270 weight_idx
= get_bits(gb
, 2);
271 if ((ret
= num_coded_units(gb
, chan
, ctx
, avctx
)) < 0)
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
;
282 delta_bits
= get_bits(gb
, 2);
283 min_val
= get_bits(gb
, 3);
285 for (i
= 0; i
< pos
; i
++)
286 chan
->qu_wordlen
[i
] = get_bits(gb
, 3);
288 for (i
= pos
; i
< chan
->num_coded_vals
; i
++)
289 chan
->qu_wordlen
[i
] = (min_val
+ get_bitsz(gb
, delta_bits
)) & 7;
294 if ((ret
= num_coded_units(gb
, chan
, ctx
, avctx
)) < 0)
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;
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;
307 } else if (chan
->num_coded_vals
) {
308 flag
= get_bits(gb
, 1);
309 vlc_tab
= &wl_vlc_tabs
[get_bits(gb
, 1)];
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
);
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;
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;
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;
340 weight_idx
= get_bits(gb
, 2);
341 if ((ret
= num_coded_units(gb
, chan
, ctx
, avctx
)) < 0)
344 if (chan
->num_coded_vals
) {
345 vlc_tab
= &wl_vlc_tabs
[get_bits(gb
, 2)];
347 /* first coefficient is coded directly */
348 chan
->qu_wordlen
[0] = get_bits(gb
, 3);
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;
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
);
368 for (i
= chan
->num_coded_vals
; i
< pos
; i
++)
369 chan
->qu_wordlen
[i
] = 1;
373 return add_wordlen_weights(ctx
, chan
, weight_idx
, avctx
);
379 * Decode scale factor indexes for each quant unit of a channel.
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
387 static int decode_channel_sf_idx(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
388 int ch_num
, AVCodecContext
*avctx
)
390 int i
, weight_idx
= 0, delta
, diff
, num_long_vals
,
391 delta_bits
, min_val
, vlc_sel
, start_val
;
393 Atrac3pChanParams
*chan
= &ctx
->channels
[ch_num
];
394 Atrac3pChanParams
*ref_chan
= &ctx
->channels
[0];
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);
403 vlc_tab
= &sf_vlc_tabs
[get_bits(gb
, 2)];
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;
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
);
414 num_long_vals
= get_bits(gb
, 5);
415 delta_bits
= get_bits(gb
, 2);
416 min_val
= get_bits(gb
, 4) - 7;
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;
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;
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
;
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);
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;
449 vlc_tab
= &sf_vlc_tabs
[get_bits(gb
, 2)];
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;
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;
460 vlc_tab
= &sf_vlc_tabs
[get_bits(gb
, 2) + 4];
462 UNPACK_SF_VQ_SHAPE(gb
, chan
->qu_sf_idx
, ctx
->used_quant_units
);
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;
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
];
477 weight_idx
= get_bits(gb
, 2);
478 vlc_sel
= get_bits(gb
, 2);
479 vlc_tab
= &sf_vlc_tabs
[vlc_sel
];
481 if (weight_idx
== 3) {
482 vlc_tab
= &sf_vlc_tabs
[vlc_sel
+ 4];
484 UNPACK_SF_VQ_SHAPE(gb
, chan
->qu_sf_idx
, ctx
->used_quant_units
);
486 diff
= (get_bits(gb
, 4) + 56) & 0x3F;
487 chan
->qu_sf_idx
[0] = (chan
->qu_sf_idx
[0] + diff
) & 0x3F;
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;
495 /* 1st coefficient is coded directly */
496 chan
->qu_sf_idx
[0] = get_bits(gb
, 6);
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;
507 if (weight_idx
&& weight_idx
< 3)
508 return subtract_sf_weights(ctx
, chan
, weight_idx
, avctx
);
514 * Decode word length information for each channel.
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
522 static int decode_quant_wordlen(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
523 int num_channels
, AVCodecContext
*avctx
)
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
));
531 if ((ret
= decode_channel_wordlen(gb
, ctx
, ch_num
, avctx
)) < 0)
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
]))
541 ctx
->used_quant_units
= i
+ 1;
547 * Decode scale factor indexes for each channel.
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
555 static int decode_scale_factors(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
556 int num_channels
, AVCodecContext
*avctx
)
560 if (!ctx
->used_quant_units
)
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
));
567 if ((ret
= decode_channel_sf_idx(gb
, ctx
, ch_num
, avctx
)) < 0)
575 * Decode number of code table values.
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
582 static int get_num_ct_values(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
583 AVCodecContext
*avctx
)
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
;
594 return num_coded_vals
;
596 return ctx
->used_quant_units
;
599 #define DEC_CT_IDX_COMMON(OP) \
600 num_vals = get_num_ct_values(gb, ctx, avctx); \
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); \
612 #define CODING_DIRECT get_bits(gb, num_bits)
614 #define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
616 #define CODING_VLC_DELTA \
618 : (pred + get_vlc2(gb, delta_vlc->table, \
619 delta_vlc->bits, 1)) & mask; \
620 pred = chan->qu_tab_idx[i]
622 #define CODING_VLC_DIFF \
623 (ref_chan->qu_tab_idx[i] + \
624 get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
627 * Decode code table indexes for each quant unit of a channel.
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
635 static int decode_channel_code_tab(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
636 int ch_num
, AVCodecContext
*avctx
)
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];
644 chan
->table_type
= get_bits1(gb
);
646 switch (get_bits(gb
, 2)) { /* switch according to coding mode */
647 case 0: /* directly coded */
648 num_bits
= ctx
->use_full_table
+ 2;
649 DEC_CT_IDX_COMMON(CODING_DIRECT
);
651 case 1: /* entropy-coded */
652 vlc_tab
= ctx
->use_full_table
? &ct_vlc_tabs
[1]
654 DEC_CT_IDX_COMMON(CODING_VLC
);
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];
661 vlc_tab
= ct_vlc_tabs
;
662 delta_vlc
= ct_vlc_tabs
;
665 DEC_CT_IDX_COMMON(CODING_VLC_DELTA
);
667 case 3: /* entropy-coded difference to master */
669 vlc_tab
= ctx
->use_full_table
? &ct_vlc_tabs
[3]
671 DEC_CT_IDX_COMMON(CODING_VLC_DIFF
);
680 * Decode code table indexes for each channel.
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
688 static int decode_code_table_indexes(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
689 int num_channels
, AVCodecContext
*avctx
)
693 if (!ctx
->used_quant_units
)
696 ctx
->use_full_table
= get_bits1(gb
);
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
));
702 if ((ret
= decode_channel_code_tab(gb
, ctx
, ch_num
, avctx
)) < 0)
710 * Decode huffman-coded spectral lines for a given quant unit.
712 * This is a generalized version for all known coding modes.
713 * Its speed can be improved by creating separate functions for each mode.
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
721 static void decode_qu_spectra(GetBitContext
*gb
, const Atrac3pSpecCodeTab
*tab
,
722 VLC
*vlc_tab
, int16_t *out
, const int num_specs
)
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
;
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);
736 for (i
= 0; i
< num_coeffs
; i
++) {
737 cf
= av_zero_extend(val
, bits
);
739 cf
= sign_extend(cf
, bits
);
740 else if (cf
&& get_bits1(gb
))
747 } else /* group skipped */
748 pos
+= group_size
* num_coeffs
;
753 * Decode huffman-coded IMDCT spectrum for all channels.
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
760 static void decode_spectrum(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
761 int num_channels
, AVCodecContext
*avctx
)
763 int i
, ch_num
, qu
, wordlen
, codetab
, tab_index
, num_specs
;
764 const Atrac3pSpecCodeTab
*tab
;
765 Atrac3pChanParams
*chan
;
767 for (ch_num
= 0; ch_num
< num_channels
; ch_num
++) {
768 chan
= &ctx
->channels
[ch_num
];
770 memset(chan
->spectrum
, 0, sizeof(chan
->spectrum
));
772 /* set power compensation level to disabled */
773 memset(chan
->power_levs
, ATRAC3P_POWER_COMP_OFF
, sizeof(chan
->power_levs
));
775 for (qu
= 0; qu
< ctx
->used_quant_units
; qu
++) {
776 num_specs
= ff_atrac3p_qu_to_spec_pos
[qu
+ 1] -
777 ff_atrac3p_qu_to_spec_pos
[qu
];
779 wordlen
= chan
->qu_wordlen
[qu
];
780 codetab
= chan
->qu_tab_idx
[qu
];
782 if (!ctx
->use_full_table
)
783 codetab
= atrac3p_ct_restricted_to_full
[chan
->table_type
][wordlen
- 1][codetab
];
785 tab_index
= (chan
->table_type
* 8 + codetab
) * 7 + wordlen
- 1;
786 tab
= &atrac3p_spectra_tabs
[tab_index
];
788 decode_qu_spectra(gb
, tab
, &spec_vlc_tabs
[tab_index
],
789 &chan
->spectrum
[ff_atrac3p_qu_to_spec_pos
[qu
]],
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
]],
796 sizeof(chan
->spectrum
[ff_atrac3p_qu_to_spec_pos
[qu
]]));
797 chan
->qu_wordlen
[qu
] = ctx
->channels
[0].qu_wordlen
[qu
];
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);
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.
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
826 static int get_subband_flags(GetBitContext
*gb
, uint8_t *out
, int num_flags
)
830 memset(out
, 0, num_flags
);
832 result
= get_bits1(gb
);
835 for (i
= 0; i
< num_flags
; i
++)
836 out
[i
] = get_bits1(gb
);
838 memset(out
, 1, num_flags
);
845 * Decode mdct window shape flags for all channels.
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
851 static void decode_window_shape(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
856 for (ch_num
= 0; ch_num
< num_channels
; ch_num
++)
857 get_subband_flags(gb
, ctx
->channels
[ch_num
].wnd_shape
,
862 * Decode number of gain control points.
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
870 static int decode_gainc_npoints(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
871 int ch_num
, int coded_subbands
)
873 int i
, delta
, delta_bits
, min_val
;
874 Atrac3pChanParams
*chan
= &ctx
->channels
[ch_num
];
875 Atrac3pChanParams
*ref_chan
= &ctx
->channels
[0];
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);
882 case 1: /* variable-length coding */
883 for (i
= 0; i
< coded_subbands
; i
++)
884 chan
->gain_data
[i
].num_points
=
885 get_vlc2(gb
, gain_vlc_tabs
[0].table
,
886 gain_vlc_tabs
[0].bits
, 1);
889 if (ch_num
) { /* VLC modulo delta to master channel */
890 for (i
= 0; i
< coded_subbands
; i
++) {
891 delta
= get_vlc2(gb
, gain_vlc_tabs
[1].table
,
892 gain_vlc_tabs
[1].bits
, 1);
893 chan
->gain_data
[i
].num_points
=
894 (ref_chan
->gain_data
[i
].num_points
+ delta
) & 7;
896 } else { /* VLC modulo delta to previous */
897 chan
->gain_data
[0].num_points
=
898 get_vlc2(gb
, gain_vlc_tabs
[0].table
,
899 gain_vlc_tabs
[0].bits
, 1);
901 for (i
= 1; i
< coded_subbands
; i
++) {
902 delta
= get_vlc2(gb
, gain_vlc_tabs
[1].table
,
903 gain_vlc_tabs
[1].bits
, 1);
904 chan
->gain_data
[i
].num_points
=
905 (chan
->gain_data
[i
- 1].num_points
+ delta
) & 7;
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);
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
;
930 * Implements coding mode 3 (slave) for gain compensation levels.
932 * @param[out] dst ptr to the output array
933 * @param[in] ref ptr to the reference channel
935 static inline void gainc_level_mode3s(AtracGainInfo
*dst
, AtracGainInfo
*ref
)
939 for (i
= 0; i
< dst
->num_points
; i
++)
940 dst
->lev_code
[i
] = (i
>= ref
->num_points
) ? 7 : ref
->lev_code
[i
];
944 * Implements coding mode 1 (master) for gain compensation levels.
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
950 static inline void gainc_level_mode1m(GetBitContext
*gb
,
951 Atrac3pChanUnitCtx
*ctx
,
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);
960 for (i
= 1; i
< dst
->num_points
; i
++) {
961 delta
= get_vlc2(gb
, gain_vlc_tabs
[3].table
,
962 gain_vlc_tabs
[3].bits
, 1);
963 dst
->lev_code
[i
] = (dst
->lev_code
[i
- 1] + delta
) & 0xF;
968 * Decode level code for each gain control point.
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
976 static int decode_gainc_levels(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
977 int ch_num
, int coded_subbands
)
979 int sb
, i
, delta
, delta_bits
, min_val
, pred
;
980 Atrac3pChanParams
*chan
= &ctx
->channels
[ch_num
];
981 Atrac3pChanParams
*ref_chan
= &ctx
->channels
[0];
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);
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
++) {
993 delta
= get_vlc2(gb
, gain_vlc_tabs
[5].table
,
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;
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
]);
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) {
1009 gainc_level_mode1m(gb
, ctx
, &chan
->gain_data
[sb
]);
1011 gainc_level_mode3s(&chan
->gain_data
[sb
],
1012 &ref_chan
->gain_data
[sb
]);
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]);
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;
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);
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
;
1051 * Implements coding mode 0 for gain compensation locations.
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
1058 static inline void gainc_loc_mode0(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1059 AtracGainInfo
*dst
, int pos
)
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;
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;
1075 * Implements coding mode 1 for gain compensation locations.
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
1081 static inline void gainc_loc_mode1(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1087 if (dst
->num_points
> 0) {
1088 /* 1st coefficient is stored directly */
1089 dst
->loc_code
[0] = get_bits(gb
, 5);
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])
1096 : &gain_vlc_tabs
[9];
1097 dst
->loc_code
[i
] = dst
->loc_code
[i
- 1] +
1098 get_vlc2(gb
, tab
->table
, tab
->bits
, 1);
1104 * Decode location code for each gain control point.
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
1113 static int decode_gainc_loc_codes(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1114 int ch_num
, int coded_subbands
,
1115 AVCodecContext
*avctx
)
1117 int sb
, i
, delta
, delta_bits
, min_val
, pred
, more_than_ref
;
1118 AtracGainInfo
*dst
, *ref
;
1120 Atrac3pChanParams
*chan
= &ctx
->channels
[ch_num
];
1121 Atrac3pChanParams
*ref_chan
= &ctx
->channels
[0];
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
);
1131 for (sb
= 0; sb
< coded_subbands
; sb
++) {
1132 if (chan
->gain_data
[sb
].num_points
<= 0)
1134 dst
= &chan
->gain_data
[sb
];
1135 ref
= &ref_chan
->gain_data
[sb
];
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;
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
) {
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
;
1154 gainc_loc_mode0(gb
, ctx
, dst
, i
); // direct coding
1156 dst
->loc_code
[i
] = ref
->loc_code
[i
]; // clone master
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);
1163 dst
->loc_code
[i
] = dst
->loc_code
[i
- 1] + delta
;
1165 dst
->loc_code
[i
] = (ref
->loc_code
[i
] + delta
) & 0x1F;
1169 } else /* VLC delta to previous */
1170 for (sb
= 0; sb
< coded_subbands
; sb
++)
1171 gainc_loc_mode1(gb
, ctx
, &chan
->gain_data
[sb
]);
1175 for (sb
= 0; sb
< coded_subbands
; sb
++) {
1176 if (chan
->gain_data
[sb
].num_points
<= 0)
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
];
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
);
1191 for (sb
= 1; sb
< coded_subbands
; sb
++) {
1192 if (chan
->gain_data
[sb
].num_points
<= 0)
1194 dst
= &chan
->gain_data
[sb
];
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;
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);
1212 dst
->loc_code
[i
] = dst
->loc_code
[i
- 1] + delta
;
1214 dst
->loc_code
[i
] = (dst
[-1].loc_code
[i
] + delta
) & 0x1F;
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
);
1226 chan
->gain_data
[sb
].loc_code
[i
] =
1227 ref_chan
->gain_data
[sb
].loc_code
[i
];
1229 } else { /* shorter delta to min */
1230 delta_bits
= get_bits(gb
, 2) + 1;
1231 min_val
= get_bits(gb
, 5);
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
);
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
;
1259 * Decode gain control data for all channels.
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
1267 static int decode_gainc_data(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1268 int num_channels
, AVCodecContext
*avctx
)
1270 int ch_num
, coded_subbands
, sb
, ret
;
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
);
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;
1281 ctx
->channels
[ch_num
].num_gain_subbands
= coded_subbands
;
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)
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];
1294 ctx
->channels
[ch_num
].num_gain_subbands
= 0;
1302 * Decode envelope for all tones of a channel.
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
1310 static void decode_tones_envelope(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1311 int ch_num
, int band_has_tones
[])
1314 Atrac3pWavesData
*dst
= ctx
->channels
[ch_num
].tones_info
;
1315 Atrac3pWavesData
*ref
= ctx
->channels
[0].tones_info
;
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
])
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;
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
])
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
;
1341 * Decode number of tones for each subband of a channel.
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
1351 static int decode_band_numwavs(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1352 int ch_num
, int band_has_tones
[],
1353 AVCodecContext
*avctx
)
1355 int mode
, sb
, delta
;
1356 Atrac3pWavesData
*dst
= ctx
->channels
[ch_num
].tones_info
;
1357 Atrac3pWavesData
*ref
= ctx
->channels
[0].tones_info
;
1359 mode
= get_bits(gb
, ch_num
+ 1);
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);
1366 case 1: /** variable-length coding */
1367 for (sb
= 0; sb
< ctx
->waves_info
->num_tone_bands
; sb
++)
1368 if (band_has_tones
[sb
])
1370 get_vlc2(gb
, tone_vlc_tabs
[1].table
,
1371 tone_vlc_tabs
[1].bits
, 1);
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;
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
;
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
,
1397 return AVERROR_INVALIDDATA
;
1399 dst
[sb
].start_index
= ctx
->waves_info
->tones_index
;
1400 ctx
->waves_info
->tones_index
+= dst
[sb
].num_wavs
;
1407 * Decode frequency information for each subband of a channel.
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
1415 static void decode_tones_frequency(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1416 int ch_num
, int band_has_tones
[])
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
;
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
)
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
);
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);
1441 nbits
= av_log2(1023 - iwav
[i
- 1].freq_index
) + 1;
1442 iwav
[i
].freq_index
= get_bits(gb
, nbits
) +
1443 1024 - (1 << nbits
);
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
)
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;
1467 * Decode amplitude information for each subband of a channel.
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
1475 static void decode_tones_amplitude(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1476 int ch_num
, int band_has_tones
[])
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
;
1485 for (sb
= 0; sb
< ctx
->waves_info
->num_tone_bands
; sb
++) {
1486 if (!band_has_tones
[sb
] || !dst
[sb
].num_wavs
)
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
) {
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
;
1504 refwaves
[dst
[sb
].start_index
+ j
] = -1;
1509 mode
= get_bits(gb
, ch_num
+ 1);
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
)
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);
1520 ctx
->waves_info
->waves
[dst
[sb
].start_index
].amp_sf
= get_bits(gb
, 6);
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
)
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;
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;
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
)
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;
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
])
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
1567 * Decode phase information for each subband of a channel.
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
1575 static void decode_tones_phase(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1576 int ch_num
, int band_has_tones
[])
1579 Atrac3pWaveParam
*wparam
;
1580 Atrac3pWavesData
*dst
= ctx
->channels
[ch_num
].tones_info
;
1582 for (sb
= 0; sb
< ctx
->waves_info
->num_tone_bands
; sb
++) {
1583 if (!band_has_tones
[sb
])
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);
1592 * Decode tones info for all channels.
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
1600 static int decode_tones_info(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1601 int num_channels
, AVCodecContext
*avctx
)
1604 int band_has_tones
[16];
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
);
1610 ctx
->waves_info
->tones_present
= get_bits1(gb
);
1611 if (!ctx
->waves_info
->tones_present
)
1614 memset(ctx
->waves_info
->waves
, 0, sizeof(ctx
->waves_info
->waves
));
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
;
1622 ctx
->waves_info
->num_tone_bands
=
1623 get_vlc2(gb
, tone_vlc_tabs
[0].table
,
1624 tone_vlc_tabs
[0].bits
, 1) + 1;
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
);
1632 ctx
->waves_info
->tones_index
= 0;
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
];
1638 decode_tones_envelope(gb
, ctx
, ch_num
, band_has_tones
);
1639 if ((ret
= decode_band_numwavs(gb
, ctx
, ch_num
, band_has_tones
,
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
);
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
];
1653 if (ctx
->waves_info
->tone_master
[i
])
1654 FFSWAP(Atrac3pWavesData
, ctx
->channels
[0].tones_info
[i
],
1655 ctx
->channels
[1].tones_info
[i
]);
1662 int ff_atrac3p_decode_channel_unit(GetBitContext
*gb
, Atrac3pChanUnitCtx
*ctx
,
1663 int num_channels
, AVCodecContext
*avctx
)
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
;
1676 ctx
->mute_flag
= get_bits1(gb
);
1678 /* decode various sound parameters */
1679 if ((ret
= decode_quant_wordlen(gb
, ctx
, num_channels
, avctx
)) < 0)
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
1687 if ((ret
= decode_scale_factors(gb
, ctx
, num_channels
, avctx
)) < 0)
1690 if ((ret
= decode_code_table_indexes(gb
, ctx
, num_channels
, avctx
)) < 0)
1693 decode_spectrum(gb
, ctx
, num_channels
, avctx
);
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
);
1700 decode_window_shape(gb
, ctx
, num_channels
);
1702 if ((ret
= decode_gainc_data(gb
, ctx
, num_channels
, avctx
)) < 0)
1705 if ((ret
= decode_tones_info(gb
, ctx
, num_channels
, avctx
)) < 0)
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);