Line | Branch | Exec | Source |
---|---|---|---|
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 | 605 | 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 | 605 | int index = 0; | |
56 | |||
57 |
2/2✓ Branch 0 taken 7260 times.
✓ Branch 1 taken 605 times.
|
7865 | for (int b = 1; b <= 12; b++) { |
58 |
2/2✓ Branch 0 taken 46775 times.
✓ Branch 1 taken 7260 times.
|
54035 | for (i = *cb++; i > 0; i--) { |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46775 times.
|
46775 | av_assert0(index < 256); |
60 | 46775 | bits[index] = b; | |
61 | 46775 | index++; | |
62 | } | ||
63 | } | ||
64 | 605 | max_len = bits[index - 1]; | |
65 | |||
66 | 605 | out_vlc->table = &tables_data[*tab_offset]; | |
67 | 605 | out_vlc->table_allocated = 1 << max_len; | |
68 | |||
69 | 605 | ff_vlc_init_from_lengths(out_vlc, max_len, index, bits, 1, | |
70 | *xlat, 1, 1, 0, VLC_INIT_USE_STATIC, NULL); | ||
71 | |||
72 | 605 | *tab_offset += 1 << max_len; | |
73 | 605 | *xlat += index; | |
74 | 605 | } | |
75 | |||
76 | 5 | av_cold void ff_atrac3p_init_vlcs(void) | |
77 | { | ||
78 | 5 | int i, tab_offset = 0; | |
79 | const uint8_t *xlats; | ||
80 | |||
81 | 5 | xlats = atrac3p_wl_ct_xlats; | |
82 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5 times.
|
25 | for (int i = 0; i < 4; i++) { |
83 | 20 | build_canonical_huff(atrac3p_wl_cbs[i], &xlats, | |
84 | &tab_offset, &wl_vlc_tabs[i]); | ||
85 | 20 | build_canonical_huff(atrac3p_ct_cbs[i], &xlats, | |
86 | &tab_offset, &ct_vlc_tabs[i]); | ||
87 | } | ||
88 | |||
89 | 5 | xlats = atrac3p_sf_xlats; | |
90 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5 times.
|
45 | for (int i = 0; i < 8; i++) |
91 | 40 | build_canonical_huff(atrac3p_sf_cbs[i], &xlats, | |
92 | &tab_offset, &sf_vlc_tabs[i]); | ||
93 | |||
94 | /* build huffman tables for spectrum decoding */ | ||
95 | 5 | xlats = atrac3p_spectra_xlats; | |
96 |
2/2✓ Branch 0 taken 560 times.
✓ Branch 1 taken 5 times.
|
565 | for (i = 0; i < 112; i++) { |
97 |
2/2✓ Branch 0 taken 435 times.
✓ Branch 1 taken 125 times.
|
560 | if (atrac3p_spectra_cbs[i][0] >= 0) |
98 | 435 | build_canonical_huff(atrac3p_spectra_cbs[i], | |
99 | &xlats, &tab_offset, &spec_vlc_tabs[i]); | ||
100 | else /* Reuse already initialized VLC table */ | ||
101 | 125 | spec_vlc_tabs[i] = spec_vlc_tabs[-atrac3p_spectra_cbs[i][0]]; | |
102 | } | ||
103 | |||
104 | /* build huffman tables for gain data decoding */ | ||
105 | 5 | xlats = atrac3p_gain_xlats; | |
106 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 5 times.
|
60 | for (i = 0; i < 11; i++) |
107 | 55 | build_canonical_huff(atrac3p_gain_cbs[i], &xlats, | |
108 | &tab_offset, &gain_vlc_tabs[i]); | ||
109 | |||
110 | /* build huffman tables for tone decoding */ | ||
111 | 5 | xlats = atrac3p_tone_xlats; | |
112 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 5 times.
|
40 | for (i = 0; i < 7; i++) |
113 | 35 | build_canonical_huff(atrac3p_tone_cbs[i], &xlats, | |
114 | &tab_offset, &tone_vlc_tabs[i]); | ||
115 | 5 | } | |
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 | */ | ||
126 | 724 | static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan, | |
127 | Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx) | ||
128 | { | ||
129 | 724 | chan->fill_mode = get_bits(gb, 2); | |
130 |
2/2✓ Branch 0 taken 155 times.
✓ Branch 1 taken 569 times.
|
724 | if (!chan->fill_mode) { |
131 | 155 | chan->num_coded_vals = ctx->num_quant_units; | |
132 | } else { | ||
133 | 569 | chan->num_coded_vals = get_bits(gb, 5); | |
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 569 times.
|
569 | 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 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 435 times.
|
569 | if (chan->fill_mode == 3) |
141 | 134 | chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1; | |
142 | } | ||
143 | |||
144 | 724 | 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 | */ | ||
156 | 110 | static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx, | |
157 | Atrac3pChanParams *chan, int wtab_idx, | ||
158 | AVCodecContext *avctx) | ||
159 | { | ||
160 | int i; | ||
161 | 110 | const int8_t *weights_tab = | |
162 | 110 | &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0]; | |
163 | |||
164 |
2/2✓ Branch 0 taken 2860 times.
✓ Branch 1 taken 110 times.
|
2970 | for (i = 0; i < ctx->num_quant_units; i++) { |
165 | 2860 | chan->qu_wordlen[i] += weights_tab[i]; | |
166 |
2/4✓ Branch 0 taken 2860 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2860 times.
|
2860 | 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 | 110 | 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 | */ | ||
186 | 144 | static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx, | |
187 | Atrac3pChanParams *chan, int wtab_idx, | ||
188 | AVCodecContext *avctx) | ||
189 | { | ||
190 | int i; | ||
191 | 144 | const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0]; | |
192 | |||
193 |
2/2✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 144 times.
|
3888 | for (i = 0; i < ctx->used_quant_units; i++) { |
194 | 3744 | chan->qu_sf_idx[i] -= weights_tab[i]; | |
195 |
2/4✓ Branch 0 taken 3744 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3744 times.
|
3744 | 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 | 144 | 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 | 243 | 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 |
1/2✓ Branch 0 taken 243 times.
✗ Branch 1 not taken.
|
243 | if (num_values) { |
220 | 243 | dst[0] = dst[1] = dst[2] = start_val; | |
221 |
2/2✓ Branch 0 taken 4722 times.
✓ Branch 1 taken 243 times.
|
4965 | for (i = 3; i < num_values; i++) |
222 | 4722 | dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1]; | |
223 | } | ||
224 | 243 | } | |
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 | */ | ||
240 | 1626 | static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
241 | int ch_num, AVCodecContext *avctx) | ||
242 | { | ||
243 | 1626 | int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag, | |
244 | ret, start_val; | ||
245 | VLC *vlc_tab; | ||
246 | 1626 | Atrac3pChanParams *chan = &ctx->channels[ch_num]; | |
247 | 1626 | Atrac3pChanParams *ref_chan = &ctx->channels[0]; | |
248 | |||
249 | 1626 | chan->fill_mode = 0; | |
250 | |||
251 |
4/5✓ Branch 1 taken 902 times.
✓ Branch 2 taken 368 times.
✓ Branch 3 taken 162 times.
✓ Branch 4 taken 194 times.
✗ Branch 5 not taken.
|
1626 | switch (get_bits(gb, 2)) { /* switch according to coding mode */ |
252 | 902 | case 0: /* coded using constant number of bits */ | |
253 |
2/2✓ Branch 0 taken 28864 times.
✓ Branch 1 taken 902 times.
|
29766 | for (i = 0; i < ctx->num_quant_units; i++) |
254 | 28864 | chan->qu_wordlen[i] = get_bits(gb, 3); | |
255 | 902 | break; | |
256 | 368 | case 1: | |
257 |
2/2✓ Branch 0 taken 351 times.
✓ Branch 1 taken 17 times.
|
368 | if (ch_num) { |
258 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 351 times.
|
351 | if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) |
259 | ✗ | return ret; | |
260 | |||
261 |
2/2✓ Branch 0 taken 314 times.
✓ Branch 1 taken 37 times.
|
351 | if (chan->num_coded_vals) { |
262 | 314 | vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)]; | |
263 | |||
264 |
2/2✓ Branch 0 taken 4519 times.
✓ Branch 1 taken 314 times.
|
4833 | for (i = 0; i < chan->num_coded_vals; i++) { |
265 | 4519 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
266 | 4519 | chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7; | |
267 | } | ||
268 | } | ||
269 | } else { | ||
270 | 17 | weight_idx = get_bits(gb, 2); | |
271 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
|
17 | if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) |
272 | ✗ | return ret; | |
273 | |||
274 |
1/2✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 | if (chan->num_coded_vals) { |
275 | 17 | pos = get_bits(gb, 5); | |
276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | 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 | 17 | delta_bits = get_bits(gb, 2); | |
283 | 17 | min_val = get_bits(gb, 3); | |
284 | |||
285 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 17 times.
|
75 | for (i = 0; i < pos; i++) |
286 | 58 | chan->qu_wordlen[i] = get_bits(gb, 3); | |
287 | |||
288 |
2/2✓ Branch 0 taken 346 times.
✓ Branch 1 taken 17 times.
|
363 | for (i = pos; i < chan->num_coded_vals; i++) |
289 | 346 | chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7; | |
290 | } | ||
291 | } | ||
292 | 368 | break; | |
293 | 162 | case 2: | |
294 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 162 times.
|
162 | if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) |
295 | ✗ | return ret; | |
296 | |||
297 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
162 | if (ch_num && chan->num_coded_vals) { |
298 | 11 | vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)]; | |
299 | 11 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
300 | 11 | chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7; | |
301 | |||
302 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 11 times.
|
151 | for (i = 1; i < chan->num_coded_vals; i++) { |
303 | 140 | diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1]; | |
304 | 140 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
305 | 140 | chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7; | |
306 | } | ||
307 |
1/2✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
|
151 | } else if (chan->num_coded_vals) { |
308 | 151 | flag = get_bits(gb, 1); | |
309 | 151 | vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)]; | |
310 | |||
311 | 151 | start_val = get_bits(gb, 3); | |
312 | 151 | unpack_vq_shape(start_val, | |
313 | 151 | &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0], | |
314 | 151 | chan->qu_wordlen, chan->num_coded_vals); | |
315 | |||
316 |
2/2✓ Branch 0 taken 138 times.
✓ Branch 1 taken 13 times.
|
151 | if (!flag) { |
317 |
2/2✓ Branch 0 taken 2826 times.
✓ Branch 1 taken 138 times.
|
2964 | for (i = 0; i < chan->num_coded_vals; i++) { |
318 | 2826 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
319 | 2826 | chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7; | |
320 | } | ||
321 | } else { | ||
322 |
2/2✓ Branch 0 taken 115 times.
✓ Branch 1 taken 13 times.
|
128 | for (i = 0; i < (chan->num_coded_vals & - 2); i += 2) |
323 |
2/2✓ Branch 1 taken 51 times.
✓ Branch 2 taken 64 times.
|
115 | if (!get_bits1(gb)) { |
324 | 102 | chan->qu_wordlen[i] = (chan->qu_wordlen[i] + | |
325 | 51 | get_vlc2(gb, vlc_tab->table, | |
326 | 51 | vlc_tab->bits, 1)) & 7; | |
327 | 102 | chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] + | |
328 | 51 | get_vlc2(gb, vlc_tab->table, | |
329 | 51 | vlc_tab->bits, 1)) & 7; | |
330 | } | ||
331 | |||
332 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
|
13 | if (chan->num_coded_vals & 1) |
333 | 6 | chan->qu_wordlen[i] = (chan->qu_wordlen[i] + | |
334 | 3 | get_vlc2(gb, vlc_tab->table, | |
335 | 3 | vlc_tab->bits, 1)) & 7; | |
336 | } | ||
337 | } | ||
338 | 162 | break; | |
339 | 194 | case 3: | |
340 | 194 | weight_idx = get_bits(gb, 2); | |
341 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 194 times.
|
194 | if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0) |
342 | ✗ | return ret; | |
343 | |||
344 |
2/2✓ Branch 0 taken 157 times.
✓ Branch 1 taken 37 times.
|
194 | if (chan->num_coded_vals) { |
345 | 157 | vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)]; | |
346 | |||
347 | /* first coefficient is coded directly */ | ||
348 | 157 | chan->qu_wordlen[0] = get_bits(gb, 3); | |
349 | |||
350 |
2/2✓ Branch 0 taken 2848 times.
✓ Branch 1 taken 157 times.
|
3005 | for (i = 1; i < chan->num_coded_vals; i++) { |
351 | 2848 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
352 | 2848 | chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7; | |
353 | } | ||
354 | } | ||
355 | 194 | break; | |
356 | } | ||
357 | |||
358 |
2/2✓ Branch 0 taken 169 times.
✓ Branch 1 taken 1457 times.
|
1626 | if (chan->fill_mode == 2) { |
359 |
2/2✓ Branch 0 taken 1978 times.
✓ Branch 1 taken 169 times.
|
2147 | for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++) |
360 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1978 times.
|
1978 | chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1; |
361 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 1323 times.
|
1457 | } else if (chan->fill_mode == 3) { |
362 | 267 | pos = ch_num ? chan->num_coded_vals + chan->split_point | |
363 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 1 times.
|
134 | : ctx->num_quant_units - chan->split_point; |
364 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
|
134 | 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 |
2/2✓ Branch 0 taken 529 times.
✓ Branch 1 taken 134 times.
|
663 | for (i = chan->num_coded_vals; i < pos; i++) |
369 | 529 | chan->qu_wordlen[i] = 1; | |
370 | } | ||
371 | |||
372 |
2/2✓ Branch 0 taken 110 times.
✓ Branch 1 taken 1516 times.
|
1626 | if (weight_idx) |
373 | 110 | return add_wordlen_weights(ctx, chan, weight_idx, avctx); | |
374 | |||
375 | 1516 | 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 | */ | ||
387 | 1552 | static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
388 | int ch_num, AVCodecContext *avctx) | ||
389 | { | ||
390 | 1552 | int i, weight_idx = 0, delta, diff, num_long_vals, | |
391 | delta_bits, min_val, vlc_sel, start_val; | ||
392 | VLC *vlc_tab; | ||
393 | 1552 | Atrac3pChanParams *chan = &ctx->channels[ch_num]; | |
394 | 1552 | Atrac3pChanParams *ref_chan = &ctx->channels[0]; | |
395 | |||
396 |
4/5✓ Branch 1 taken 902 times.
✓ Branch 2 taken 322 times.
✓ Branch 3 taken 163 times.
✓ Branch 4 taken 165 times.
✗ Branch 5 not taken.
|
1552 | switch (get_bits(gb, 2)) { /* switch according to coding mode */ |
397 | 902 | case 0: /* coded using constant number of bits */ | |
398 |
2/2✓ Branch 0 taken 27060 times.
✓ Branch 1 taken 902 times.
|
27962 | for (i = 0; i < ctx->used_quant_units; i++) |
399 | 27060 | chan->qu_sf_idx[i] = get_bits(gb, 6); | |
400 | 902 | break; | |
401 | 322 | case 1: | |
402 |
2/2✓ Branch 0 taken 211 times.
✓ Branch 1 taken 111 times.
|
322 | if (ch_num) { |
403 | 211 | vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)]; | |
404 | |||
405 |
2/2✓ Branch 0 taken 5486 times.
✓ Branch 1 taken 211 times.
|
5697 | for (i = 0; i < ctx->used_quant_units; i++) { |
406 | 5486 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
407 | 5486 | chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F; | |
408 | } | ||
409 | } else { | ||
410 | 111 | weight_idx = get_bits(gb, 2); | |
411 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 69 times.
|
111 | if (weight_idx == 3) { |
412 | 42 | UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units); | |
413 | |||
414 | 42 | num_long_vals = get_bits(gb, 5); | |
415 | 42 | delta_bits = get_bits(gb, 2); | |
416 | 42 | min_val = get_bits(gb, 4) - 7; | |
417 | |||
418 |
2/2✓ Branch 0 taken 832 times.
✓ Branch 1 taken 42 times.
|
874 | for (i = 0; i < num_long_vals; i++) |
419 | 1664 | chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + | |
420 | 832 | get_bits(gb, 4) - 7) & 0x3F; | |
421 | |||
422 | /* all others are: min_val + delta */ | ||
423 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 42 times.
|
302 | for (i = num_long_vals; i < ctx->used_quant_units; i++) |
424 | 520 | chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val + | |
425 | 260 | get_bitsz(gb, delta_bits)) & 0x3F; | |
426 | } else { | ||
427 | 69 | num_long_vals = get_bits(gb, 5); | |
428 | 69 | delta_bits = get_bits(gb, 3); | |
429 | 69 | min_val = get_bits(gb, 6); | |
430 |
2/4✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 69 times.
|
69 | 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 |
2/2✓ Branch 0 taken 650 times.
✓ Branch 1 taken 69 times.
|
719 | for (i = 0; i < num_long_vals; i++) |
438 | 650 | chan->qu_sf_idx[i] = get_bits(gb, 6); | |
439 | |||
440 | /* all others are: min_val + delta */ | ||
441 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 69 times.
|
1213 | for (i = num_long_vals; i < ctx->used_quant_units; i++) |
442 | 1144 | chan->qu_sf_idx[i] = (min_val + | |
443 | 1144 | get_bitsz(gb, delta_bits)) & 0x3F; | |
444 | } | ||
445 | } | ||
446 | 322 | break; | |
447 | 163 | case 2: | |
448 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 49 times.
|
163 | if (ch_num) { |
449 | 114 | vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)]; | |
450 | |||
451 | 114 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
452 | 114 | chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F; | |
453 | |||
454 |
2/2✓ Branch 0 taken 2850 times.
✓ Branch 1 taken 114 times.
|
2964 | for (i = 1; i < ctx->used_quant_units; i++) { |
455 | 2850 | diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1]; | |
456 | 2850 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
457 | 2850 | chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F; | |
458 | } | ||
459 | } else { | ||
460 | 49 | vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4]; | |
461 | |||
462 | 49 | UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units); | |
463 | |||
464 |
2/2✓ Branch 0 taken 1274 times.
✓ Branch 1 taken 49 times.
|
1323 | for (i = 0; i < ctx->used_quant_units; i++) { |
465 | 1274 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
466 | 1274 | chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + | |
467 | 1274 | sign_extend(delta, 4)) & 0x3F; | |
468 | } | ||
469 | } | ||
470 | 163 | break; | |
471 | 165 | case 3: | |
472 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
|
165 | 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 | 165 | weight_idx = get_bits(gb, 2); | |
478 | 165 | vlc_sel = get_bits(gb, 2); | |
479 | 165 | vlc_tab = &sf_vlc_tabs[vlc_sel]; | |
480 | |||
481 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 164 times.
|
165 | if (weight_idx == 3) { |
482 | 1 | vlc_tab = &sf_vlc_tabs[vlc_sel + 4]; | |
483 | |||
484 | 1 | UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units); | |
485 | |||
486 | 1 | diff = (get_bits(gb, 4) + 56) & 0x3F; | |
487 | 1 | chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F; | |
488 | |||
489 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 times.
|
26 | for (i = 1; i < ctx->used_quant_units; i++) { |
490 | 25 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
491 | 25 | diff = (diff + sign_extend(delta, 4)) & 0x3F; | |
492 | 25 | chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F; | |
493 | } | ||
494 | } else { | ||
495 | /* 1st coefficient is coded directly */ | ||
496 | 164 | chan->qu_sf_idx[0] = get_bits(gb, 6); | |
497 | |||
498 |
2/2✓ Branch 0 taken 4100 times.
✓ Branch 1 taken 164 times.
|
4264 | for (i = 1; i < ctx->used_quant_units; i++) { |
499 | 4100 | delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
500 | 4100 | chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F; | |
501 | } | ||
502 | } | ||
503 | } | ||
504 | 165 | break; | |
505 | } | ||
506 | |||
507 |
4/4✓ Branch 0 taken 187 times.
✓ Branch 1 taken 1365 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 43 times.
|
1552 | if (weight_idx && weight_idx < 3) |
508 | 144 | return subtract_sf_weights(ctx, chan, weight_idx, avctx); | |
509 | |||
510 | 1408 | 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 | */ | ||
522 | 813 | static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
523 | int num_channels, AVCodecContext *avctx) | ||
524 | { | ||
525 | int ch_num, i, ret; | ||
526 | |||
527 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch_num = 0; ch_num < num_channels; ch_num++) { |
528 | 1626 | memset(ctx->channels[ch_num].qu_wordlen, 0, | |
529 | sizeof(ctx->channels[ch_num].qu_wordlen)); | ||
530 | |||
531 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1626 times.
|
1626 | 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 |
2/2✓ Branch 0 taken 2640 times.
✓ Branch 1 taken 37 times.
|
2677 | for (i = ctx->num_quant_units - 1; i >= 0; i--) |
538 |
3/4✓ Branch 0 taken 1864 times.
✓ Branch 1 taken 776 times.
✓ Branch 2 taken 1864 times.
✗ Branch 3 not taken.
|
2640 | if (ctx->channels[0].qu_wordlen[i] || |
539 |
1/2✓ Branch 0 taken 1864 times.
✗ Branch 1 not taken.
|
1864 | (num_channels == 2 && ctx->channels[1].qu_wordlen[i])) |
540 | break; | ||
541 | 813 | ctx->used_quant_units = i + 1; | |
542 | |||
543 | 813 | 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 | */ | ||
555 | 813 | static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
556 | int num_channels, AVCodecContext *avctx) | ||
557 | { | ||
558 | int ch_num, ret; | ||
559 | |||
560 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 776 times.
|
813 | if (!ctx->used_quant_units) |
561 | 37 | return 0; | |
562 | |||
563 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 776 times.
|
2328 | for (ch_num = 0; ch_num < num_channels; ch_num++) { |
564 | 1552 | memset(ctx->channels[ch_num].qu_sf_idx, 0, | |
565 | sizeof(ctx->channels[ch_num].qu_sf_idx)); | ||
566 | |||
567 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1552 times.
|
1552 | if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0) |
568 | ✗ | return ret; | |
569 | } | ||
570 | |||
571 | 776 | 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 | */ | ||
582 | 1552 | static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
583 | AVCodecContext *avctx) | ||
584 | { | ||
585 | int num_coded_vals; | ||
586 | |||
587 |
2/2✓ Branch 1 taken 356 times.
✓ Branch 2 taken 1196 times.
|
1552 | if (get_bits1(gb)) { |
588 | 356 | num_coded_vals = get_bits(gb, 5); | |
589 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 356 times.
|
356 | 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 | 356 | return num_coded_vals; | |
595 | } else | ||
596 | 1196 | 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 | */ | ||
635 | 1552 | static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
636 | int ch_num, AVCodecContext *avctx) | ||
637 | { | ||
638 | int i, num_vals, num_bits, pred; | ||
639 |
2/2✓ Branch 0 taken 650 times.
✓ Branch 1 taken 902 times.
|
1552 | int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */ |
640 | VLC *vlc_tab, *delta_vlc; | ||
641 | 1552 | Atrac3pChanParams *chan = &ctx->channels[ch_num]; | |
642 | 1552 | Atrac3pChanParams *ref_chan = &ctx->channels[0]; | |
643 | |||
644 | 1552 | chan->table_type = get_bits1(gb); | |
645 | |||
646 |
4/5✓ Branch 1 taken 980 times.
✓ Branch 2 taken 275 times.
✓ Branch 3 taken 191 times.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
|
1552 | switch (get_bits(gb, 2)) { /* switch according to coding mode */ |
647 | 980 | case 0: /* directly coded */ | |
648 | 980 | num_bits = ctx->use_full_table + 2; | |
649 |
7/10✗ Branch 1 not taken.
✓ Branch 2 taken 980 times.
✓ Branch 3 taken 28404 times.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 28405 times.
✓ Branch 12 taken 980 times.
|
29385 | DEC_CT_IDX_COMMON(CODING_DIRECT); |
650 | 980 | break; | |
651 | 275 | case 1: /* entropy-coded */ | |
652 | 550 | vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1] | |
653 |
1/2✓ Branch 0 taken 275 times.
✗ Branch 1 not taken.
|
275 | : ct_vlc_tabs; |
654 |
7/10✗ Branch 1 not taken.
✓ Branch 2 taken 275 times.
✓ Branch 3 taken 6109 times.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 6110 times.
✓ Branch 12 taken 275 times.
|
6385 | DEC_CT_IDX_COMMON(CODING_VLC); |
655 | 275 | break; | |
656 | 191 | case 2: /* entropy-coded delta */ | |
657 |
1/2✓ Branch 0 taken 191 times.
✗ Branch 1 not taken.
|
191 | if (ctx->use_full_table) { |
658 | 191 | vlc_tab = &ct_vlc_tabs[1]; | |
659 | 191 | delta_vlc = &ct_vlc_tabs[2]; | |
660 | } else { | ||
661 | ✗ | vlc_tab = ct_vlc_tabs; | |
662 | ✗ | delta_vlc = ct_vlc_tabs; | |
663 | } | ||
664 | 191 | pred = 0; | |
665 |
6/12✗ Branch 1 not taken.
✓ Branch 2 taken 191 times.
✓ Branch 3 taken 4310 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 191 times.
✓ Branch 6 taken 4119 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 14 taken 4310 times.
✓ Branch 15 taken 191 times.
|
4501 | DEC_CT_IDX_COMMON(CODING_VLC_DELTA); |
666 | 191 | break; | |
667 | 106 | case 3: /* entropy-coded difference to master */ | |
668 |
1/2✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
|
106 | if (ch_num) { |
669 | 212 | vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3] | |
670 |
1/2✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
|
106 | : ct_vlc_tabs; |
671 |
7/10✗ Branch 1 not taken.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 1646 times.
✓ Branch 4 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1649 times.
✓ Branch 12 taken 106 times.
|
1755 | DEC_CT_IDX_COMMON(CODING_VLC_DIFF); |
672 | } | ||
673 | 106 | break; | |
674 | } | ||
675 | |||
676 | 1552 | 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 | */ | ||
688 | 813 | static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
689 | int num_channels, AVCodecContext *avctx) | ||
690 | { | ||
691 | int ch_num, ret; | ||
692 | |||
693 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 776 times.
|
813 | if (!ctx->used_quant_units) |
694 | 37 | return 0; | |
695 | |||
696 | 776 | ctx->use_full_table = get_bits1(gb); | |
697 | |||
698 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 776 times.
|
2328 | for (ch_num = 0; ch_num < num_channels; ch_num++) { |
699 | 1552 | memset(ctx->channels[ch_num].qu_tab_idx, 0, | |
700 | sizeof(ctx->channels[ch_num].qu_tab_idx)); | ||
701 | |||
702 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1552 times.
|
1552 | if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0) |
703 | ✗ | return ret; | |
704 | } | ||
705 | |||
706 | 776 | 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 | */ | ||
721 | 40703 | static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab, | |
722 | VLC *vlc_tab, int16_t *out, const int num_specs) | ||
723 | { | ||
724 | int i, j, pos, cf; | ||
725 | 40703 | int group_size = tab->group_size; | |
726 | 40703 | int num_coeffs = tab->num_coeffs; | |
727 | 40703 | int bits = tab->bits; | |
728 | 40703 | int is_signed = tab->is_signed; | |
729 | unsigned val; | ||
730 | |||
731 |
2/2✓ Branch 0 taken 1168918 times.
✓ Branch 1 taken 40703 times.
|
1209621 | for (pos = 0; pos < num_specs;) { |
732 |
4/4✓ Branch 0 taken 28050 times.
✓ Branch 1 taken 1140868 times.
✓ Branch 3 taken 11228 times.
✓ Branch 4 taken 16822 times.
|
1168918 | if (group_size == 1 || get_bits1(gb)) { |
733 |
2/2✓ Branch 0 taken 1180390 times.
✓ Branch 1 taken 1152096 times.
|
2332486 | for (j = 0; j < group_size; j++) { |
734 | 1180390 | val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1); | |
735 | |||
736 |
2/2✓ Branch 0 taken 1939004 times.
✓ Branch 1 taken 1180390 times.
|
3119394 | for (i = 0; i < num_coeffs; i++) { |
737 | 1939004 | cf = av_zero_extend(val, bits); | |
738 |
2/2✓ Branch 0 taken 1268212 times.
✓ Branch 1 taken 670792 times.
|
1939004 | if (is_signed) |
739 | 1268212 | cf = sign_extend(cf, bits); | |
740 |
4/4✓ Branch 0 taken 468171 times.
✓ Branch 1 taken 202621 times.
✓ Branch 3 taken 233973 times.
✓ Branch 4 taken 234198 times.
|
670792 | else if (cf && get_bits1(gb)) |
741 | 233973 | cf = -cf; | |
742 | |||
743 | 1939004 | out[pos++] = cf; | |
744 | 1939004 | val >>= bits; | |
745 | } | ||
746 | } | ||
747 | } else /* group skipped */ | ||
748 | 16822 | pos += group_size * num_coeffs; | |
749 | } | ||
750 | 40703 | } | |
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 | */ | ||
760 | 813 | static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
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 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch_num = 0; ch_num < num_channels; ch_num++) { |
768 | 1626 | chan = &ctx->channels[ch_num]; | |
769 | |||
770 | 1626 | memset(chan->spectrum, 0, sizeof(chan->spectrum)); | |
771 | |||
772 | /* set power compensation level to disabled */ | ||
773 | 1626 | memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs)); | |
774 | |||
775 |
2/2✓ Branch 0 taken 43960 times.
✓ Branch 1 taken 1626 times.
|
45586 | for (qu = 0; qu < ctx->used_quant_units; qu++) { |
776 | 43960 | num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] - | |
777 | 43960 | ff_atrac3p_qu_to_spec_pos[qu]; | |
778 | |||
779 | 43960 | wordlen = chan->qu_wordlen[qu]; | |
780 | 43960 | codetab = chan->qu_tab_idx[qu]; | |
781 |
2/2✓ Branch 0 taken 40703 times.
✓ Branch 1 taken 3257 times.
|
43960 | if (wordlen) { |
782 |
2/2✓ Branch 0 taken 27060 times.
✓ Branch 1 taken 13643 times.
|
40703 | if (!ctx->use_full_table) |
783 | 27060 | codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab]; | |
784 | |||
785 | 40703 | tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1; | |
786 | 40703 | tab = &atrac3p_spectra_tabs[tab_index]; | |
787 | |||
788 | 40703 | decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index], | |
789 | 40703 | &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]], | |
790 | num_specs); | ||
791 |
3/6✓ Branch 0 taken 3257 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3257 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3257 times.
✗ Branch 5 not taken.
|
3257 | } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) { |
792 | /* copy coefficients from master */ | ||
793 | 3257 | memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]], | |
794 | 3257 | &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 | 3257 | 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 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 74 times.
|
1626 | if (ctx->used_quant_units > 2) { |
806 | 1552 | num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1]; | |
807 |
2/2✓ Branch 0 taken 7110 times.
✓ Branch 1 taken 1552 times.
|
8662 | for (i = 0; i < num_specs; i++) |
808 | 7110 | chan->power_levs[i] = get_bits(gb, 4); | |
809 | } | ||
810 | } | ||
811 | 813 | } | |
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 | 3252 | static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags) | |
827 | { | ||
828 | int i, result; | ||
829 | |||
830 | 3252 | memset(out, 0, num_flags); | |
831 | |||
832 | 3252 | result = get_bits1(gb); | |
833 |
2/2✓ Branch 0 taken 493 times.
✓ Branch 1 taken 2759 times.
|
3252 | if (result) { |
834 |
2/2✓ Branch 1 taken 419 times.
✓ Branch 2 taken 74 times.
|
493 | if (get_bits1(gb)) |
835 |
2/2✓ Branch 0 taken 4190 times.
✓ Branch 1 taken 419 times.
|
4609 | for (i = 0; i < num_flags; i++) |
836 | 4190 | out[i] = get_bits1(gb); | |
837 | else | ||
838 | 74 | memset(out, 1, num_flags); | |
839 | } | ||
840 | |||
841 | 3252 | 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 | */ | ||
851 | 813 | static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
852 | int num_channels) | ||
853 | { | ||
854 | int ch_num; | ||
855 | |||
856 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch_num = 0; ch_num < num_channels; ch_num++) |
857 | 1626 | get_subband_flags(gb, ctx->channels[ch_num].wnd_shape, | |
858 | ctx->num_subbands); | ||
859 | 813 | } | |
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 | */ | ||
870 | 582 | static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
871 | int ch_num, int coded_subbands) | ||
872 | { | ||
873 | int i, delta, delta_bits, min_val; | ||
874 | 582 | Atrac3pChanParams *chan = &ctx->channels[ch_num]; | |
875 | 582 | Atrac3pChanParams *ref_chan = &ctx->channels[0]; | |
876 | |||
877 |
4/5✓ Branch 1 taken 15 times.
✓ Branch 2 taken 509 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
582 | switch (get_bits(gb, 2)) { /* switch according to coding mode */ |
878 | 15 | case 0: /* fixed-length coding */ | |
879 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 15 times.
|
40 | for (i = 0; i < coded_subbands; i++) |
880 | 25 | chan->gain_data[i].num_points = get_bits(gb, 3); | |
881 | 15 | break; | |
882 | 509 | case 1: /* variable-length coding */ | |
883 |
2/2✓ Branch 0 taken 3577 times.
✓ Branch 1 taken 509 times.
|
4086 | for (i = 0; i < coded_subbands; i++) |
884 | 3577 | chan->gain_data[i].num_points = | |
885 | 3577 | get_vlc2(gb, gain_vlc_tabs[0].table, | |
886 | gain_vlc_tabs[0].bits, 1); | ||
887 | 509 | break; | |
888 | 38 | case 2: | |
889 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 18 times.
|
38 | if (ch_num) { /* VLC modulo delta to master channel */ |
890 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 20 times.
|
162 | for (i = 0; i < coded_subbands; i++) { |
891 | 142 | delta = get_vlc2(gb, gain_vlc_tabs[1].table, | |
892 | gain_vlc_tabs[1].bits, 1); | ||
893 | 142 | chan->gain_data[i].num_points = | |
894 | 142 | (ref_chan->gain_data[i].num_points + delta) & 7; | |
895 | } | ||
896 | } else { /* VLC modulo delta to previous */ | ||
897 | 36 | chan->gain_data[0].num_points = | |
898 | 18 | get_vlc2(gb, gain_vlc_tabs[0].table, | |
899 | gain_vlc_tabs[0].bits, 1); | ||
900 | |||
901 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 18 times.
|
105 | for (i = 1; i < coded_subbands; i++) { |
902 | 87 | delta = get_vlc2(gb, gain_vlc_tabs[1].table, | |
903 | gain_vlc_tabs[1].bits, 1); | ||
904 | 87 | chan->gain_data[i].num_points = | |
905 | 87 | (chan->gain_data[i - 1].num_points + delta) & 7; | |
906 | } | ||
907 | } | ||
908 | 38 | break; | |
909 | 20 | case 3: | |
910 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | if (ch_num) { /* copy data from master channel */ |
911 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 18 times.
|
66 | for (i = 0; i < coded_subbands; i++) |
912 | 48 | chan->gain_data[i].num_points = | |
913 | 48 | ref_chan->gain_data[i].num_points; | |
914 | } else { /* shorter delta to min */ | ||
915 | 2 | delta_bits = get_bits(gb, 2); | |
916 | 2 | min_val = get_bits(gb, 3); | |
917 | |||
918 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2 times.
|
25 | for (i = 0; i < coded_subbands; i++) { |
919 | 23 | chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits); | |
920 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (chan->gain_data[i].num_points > 7) |
921 | ✗ | return AVERROR_INVALIDDATA; | |
922 | } | ||
923 | } | ||
924 | } | ||
925 | |||
926 | 582 | 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 | */ | ||
935 | 1605 | static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref) | |
936 | { | ||
937 | int i; | ||
938 | |||
939 |
2/2✓ Branch 0 taken 395 times.
✓ Branch 1 taken 1605 times.
|
2000 | for (i = 0; i < dst->num_points; i++) |
940 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 317 times.
|
395 | dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i]; |
941 | 1605 | } | |
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 | 2009 | static inline void gainc_level_mode1m(GetBitContext *gb, | |
951 | Atrac3pChanUnitCtx *ctx, | ||
952 | AtracGainInfo *dst) | ||
953 | { | ||
954 | int i, delta; | ||
955 | |||
956 |
2/2✓ Branch 0 taken 501 times.
✓ Branch 1 taken 1508 times.
|
2009 | if (dst->num_points > 0) |
957 | 501 | dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table, | |
958 | gain_vlc_tabs[2].bits, 1); | ||
959 | |||
960 |
2/2✓ Branch 0 taken 101 times.
✓ Branch 1 taken 2009 times.
|
2110 | for (i = 1; i < dst->num_points; i++) { |
961 | 101 | delta = get_vlc2(gb, gain_vlc_tabs[3].table, | |
962 | gain_vlc_tabs[3].bits, 1); | ||
963 | 101 | dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF; | |
964 | } | ||
965 | 2009 | } | |
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 | */ | ||
976 | 582 | static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
977 | int ch_num, int coded_subbands) | ||
978 | { | ||
979 | int sb, i, delta, delta_bits, min_val, pred; | ||
980 | 582 | Atrac3pChanParams *chan = &ctx->channels[ch_num]; | |
981 | 582 | Atrac3pChanParams *ref_chan = &ctx->channels[0]; | |
982 | |||
983 |
4/5✓ Branch 1 taken 9 times.
✓ Branch 2 taken 302 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 235 times.
✗ Branch 5 not taken.
|
582 | switch (get_bits(gb, 2)) { /* switch according to coding mode */ |
984 | 9 | case 0: /* fixed-length coding */ | |
985 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 9 times.
|
62 | for (sb = 0; sb < coded_subbands; sb++) |
986 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 53 times.
|
71 | for (i = 0; i < chan->gain_data[sb].num_points; i++) |
987 | 18 | chan->gain_data[sb].lev_code[i] = get_bits(gb, 4); | |
988 | 9 | break; | |
989 | 302 | case 1: | |
990 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 284 times.
|
302 | if (ch_num) { /* VLC modulo delta to master channel */ |
991 |
2/2✓ Branch 0 taken 94 times.
✓ Branch 1 taken 18 times.
|
112 | for (sb = 0; sb < coded_subbands; sb++) |
992 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 94 times.
|
162 | for (i = 0; i < chan->gain_data[sb].num_points; i++) { |
993 | 68 | delta = get_vlc2(gb, gain_vlc_tabs[5].table, | |
994 | gain_vlc_tabs[5].bits, 1); | ||
995 | 136 | pred = (i >= ref_chan->gain_data[sb].num_points) | |
996 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 25 times.
|
68 | ? 7 : ref_chan->gain_data[sb].lev_code[i]; |
997 | 68 | chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF; | |
998 | } | ||
999 | } else { /* VLC modulo delta to previous */ | ||
1000 |
2/2✓ Branch 0 taken 1958 times.
✓ Branch 1 taken 284 times.
|
2242 | for (sb = 0; sb < coded_subbands; sb++) |
1001 | 1958 | gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]); | |
1002 | } | ||
1003 | 302 | break; | |
1004 | 36 | case 2: | |
1005 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
|
36 | if (ch_num) { /* VLC modulo delta to previous or clone master */ |
1006 |
2/2✓ Branch 0 taken 225 times.
✓ Branch 1 taken 32 times.
|
257 | for (sb = 0; sb < coded_subbands; sb++) |
1007 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 123 times.
|
225 | if (chan->gain_data[sb].num_points > 0) { |
1008 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 53 times.
|
102 | if (get_bits1(gb)) |
1009 | 49 | gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]); | |
1010 | else | ||
1011 | 53 | gainc_level_mode3s(&chan->gain_data[sb], | |
1012 | 53 | &ref_chan->gain_data[sb]); | |
1013 | } | ||
1014 | } else { /* VLC modulo delta to lev_codes of previous subband */ | ||
1015 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (chan->gain_data[0].num_points > 0) |
1016 | 2 | gainc_level_mode1m(gb, ctx, &chan->gain_data[0]); | |
1017 | |||
1018 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 4 times.
|
38 | for (sb = 1; sb < coded_subbands; sb++) |
1019 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 34 times.
|
77 | for (i = 0; i < chan->gain_data[sb].num_points; i++) { |
1020 | 43 | delta = get_vlc2(gb, gain_vlc_tabs[4].table, | |
1021 | gain_vlc_tabs[4].bits, 1); | ||
1022 | 86 | pred = (i >= chan->gain_data[sb - 1].num_points) | |
1023 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 12 times.
|
43 | ? 7 : chan->gain_data[sb - 1].lev_code[i]; |
1024 | 43 | chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF; | |
1025 | } | ||
1026 | } | ||
1027 | 36 | break; | |
1028 | 235 | case 3: | |
1029 |
1/2✓ Branch 0 taken 235 times.
✗ Branch 1 not taken.
|
235 | if (ch_num) { /* clone master */ |
1030 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 235 times.
|
1787 | for (sb = 0; sb < coded_subbands; sb++) |
1031 | 1552 | gainc_level_mode3s(&chan->gain_data[sb], | |
1032 | 1552 | &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 | 235 | break; | |
1045 | } | ||
1046 | |||
1047 | 582 | 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 | */ | ||
1058 | 664 | static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
1059 | AtracGainInfo *dst, int pos) | ||
1060 | { | ||
1061 | int delta_bits; | ||
1062 | |||
1063 |
4/4✓ Branch 0 taken 34 times.
✓ Branch 1 taken 630 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 18 times.
|
664 | if (!pos || dst->loc_code[pos - 1] < 15) |
1064 | 646 | dst->loc_code[pos] = get_bits(gb, 5); | |
1065 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | else if (dst->loc_code[pos - 1] >= 30) |
1066 | ✗ | dst->loc_code[pos] = 31; | |
1067 | else { | ||
1068 | 18 | delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1; | |
1069 | 36 | dst->loc_code[pos] = dst->loc_code[pos - 1] + | |
1070 | 18 | get_bits(gb, delta_bits) + 1; | |
1071 | } | ||
1072 | 664 | } | |
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 | */ | ||
1081 | 111 | static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
1082 | AtracGainInfo *dst) | ||
1083 | { | ||
1084 | int i; | ||
1085 | VLC *tab; | ||
1086 | |||
1087 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 41 times.
|
111 | if (dst->num_points > 0) { |
1088 | /* 1st coefficient is stored directly */ | ||
1089 | 70 | dst->loc_code[0] = get_bits(gb, 5); | |
1090 | |||
1091 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 70 times.
|
114 | for (i = 1; i < dst->num_points; i++) { |
1092 | /* switch VLC according to the curve direction | ||
1093 | * (ascending/descending) */ | ||
1094 | 88 | tab = (dst->lev_code[i] <= dst->lev_code[i - 1]) | |
1095 | ? &gain_vlc_tabs[7] | ||
1096 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | : &gain_vlc_tabs[9]; |
1097 | 44 | dst->loc_code[i] = dst->loc_code[i - 1] + | |
1098 | 44 | get_vlc2(gb, tab->table, tab->bits, 1); | |
1099 | } | ||
1100 | } | ||
1101 | 111 | } | |
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 | */ | ||
1113 | 582 | static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
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 | 582 | Atrac3pChanParams *chan = &ctx->channels[ch_num]; | |
1121 | 582 | Atrac3pChanParams *ref_chan = &ctx->channels[0]; | |
1122 | |||
1123 |
4/5✓ Branch 1 taken 433 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 57 times.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
|
582 | switch (get_bits(gb, 2)) { /* switch according to coding mode */ |
1124 | 433 | case 0: /* sequence of numbers in ascending order */ | |
1125 |
2/2✓ Branch 0 taken 2917 times.
✓ Branch 1 taken 433 times.
|
3350 | for (sb = 0; sb < coded_subbands; sb++) |
1126 |
2/2✓ Branch 0 taken 646 times.
✓ Branch 1 taken 2917 times.
|
3563 | for (i = 0; i < chan->gain_data[sb].num_points; i++) |
1127 | 646 | gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i); | |
1128 | 433 | break; | |
1129 | 64 | case 1: | |
1130 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 11 times.
|
64 | if (ch_num) { |
1131 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 53 times.
|
365 | for (sb = 0; sb < coded_subbands; sb++) { |
1132 |
2/2✓ Branch 0 taken 219 times.
✓ Branch 1 taken 93 times.
|
312 | if (chan->gain_data[sb].num_points <= 0) |
1133 | 219 | continue; | |
1134 | 93 | dst = &chan->gain_data[sb]; | |
1135 | 93 | ref = &ref_chan->gain_data[sb]; | |
1136 | |||
1137 | /* 1st value is vlc-coded modulo delta to master */ | ||
1138 | 93 | delta = get_vlc2(gb, gain_vlc_tabs[10].table, | |
1139 | gain_vlc_tabs[10].bits, 1); | ||
1140 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 51 times.
|
93 | pred = ref->num_points > 0 ? ref->loc_code[0] : 0; |
1141 | 93 | dst->loc_code[0] = (pred + delta) & 0x1F; | |
1142 | |||
1143 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 93 times.
|
113 | for (i = 1; i < dst->num_points; i++) { |
1144 | 20 | more_than_ref = i >= ref->num_points; | |
1145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | 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 | 20 | tab = more_than_ref ? &gain_vlc_tabs[7] | |
1160 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7 times.
|
20 | : &gain_vlc_tabs[10]; |
1161 | 20 | delta = get_vlc2(gb, tab->table, tab->bits, 1); | |
1162 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7 times.
|
20 | if (more_than_ref) |
1163 | 13 | dst->loc_code[i] = dst->loc_code[i - 1] + delta; | |
1164 | else | ||
1165 | 7 | dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F; | |
1166 | } | ||
1167 | } | ||
1168 | } | ||
1169 | } else /* VLC delta to previous */ | ||
1170 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 11 times.
|
78 | for (sb = 0; sb < coded_subbands; sb++) |
1171 | 67 | gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]); | |
1172 | 64 | break; | |
1173 | 57 | case 2: | |
1174 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 41 times.
|
57 | if (ch_num) { |
1175 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 16 times.
|
150 | for (sb = 0; sb < coded_subbands; sb++) { |
1176 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 60 times.
|
134 | if (chan->gain_data[sb].num_points <= 0) |
1177 | 74 | continue; | |
1178 | 60 | dst = &chan->gain_data[sb]; | |
1179 | 60 | ref = &ref_chan->gain_data[sb]; | |
1180 |
4/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 32 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 16 times.
|
60 | if (dst->num_points > ref->num_points || get_bits1(gb)) |
1181 | 44 | gainc_loc_mode1(gb, ctx, dst); | |
1182 | else /* clone master for the whole subband */ | ||
1183 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 16 times.
|
38 | for (i = 0; i < chan->gain_data[sb].num_points; i++) |
1184 | 22 | dst->loc_code[i] = ref->loc_code[i]; | |
1185 | } | ||
1186 | } else { | ||
1187 | /* data for the first subband is coded directly */ | ||
1188 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 41 times.
|
49 | for (i = 0; i < chan->gain_data[0].num_points; i++) |
1189 | 8 | gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i); | |
1190 | |||
1191 |
2/2✓ Branch 0 taken 274 times.
✓ Branch 1 taken 41 times.
|
315 | for (sb = 1; sb < coded_subbands; sb++) { |
1192 |
2/2✓ Branch 0 taken 177 times.
✓ Branch 1 taken 97 times.
|
274 | if (chan->gain_data[sb].num_points <= 0) |
1193 | 177 | continue; | |
1194 | 97 | 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 | 97 | delta = get_vlc2(gb, gain_vlc_tabs[6].table, | |
1199 | gain_vlc_tabs[6].bits, 1); | ||
1200 | 194 | pred = dst[-1].num_points > 0 | |
1201 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 55 times.
|
97 | ? dst[-1].loc_code[0] : 0; |
1202 | 97 | dst->loc_code[0] = (pred + delta) & 0x1F; | |
1203 | |||
1204 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 97 times.
|
123 | for (i = 1; i < dst->num_points; i++) { |
1205 | 26 | more_than_ref = i >= dst[-1].num_points; | |
1206 | /* Select VLC table according to curve direction and | ||
1207 | * presence of prediction. */ | ||
1208 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 21 times.
|
26 | tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) * |
1209 | 26 | 2 + more_than_ref + 6]; | |
1210 | 26 | delta = get_vlc2(gb, tab->table, tab->bits, 1); | |
1211 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 3 times.
|
26 | if (more_than_ref) |
1212 | 23 | dst->loc_code[i] = dst->loc_code[i - 1] + delta; | |
1213 | else | ||
1214 | 3 | dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F; | |
1215 | } | ||
1216 | } | ||
1217 | } | ||
1218 | 57 | break; | |
1219 | 28 | case 3: | |
1220 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 13 times.
|
28 | if (ch_num) { /* clone master or direct or direct coding */ |
1221 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 15 times.
|
84 | for (sb = 0; sb < coded_subbands; sb++) |
1222 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 69 times.
|
102 | for (i = 0; i < chan->gain_data[sb].num_points; i++) { |
1223 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 23 times.
|
33 | if (i >= ref_chan->gain_data[sb].num_points) |
1224 | 10 | gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i); | |
1225 | else | ||
1226 | 23 | chan->gain_data[sb].loc_code[i] = | |
1227 | 23 | ref_chan->gain_data[sb].loc_code[i]; | |
1228 | } | ||
1229 | } else { /* shorter delta to min */ | ||
1230 | 13 | delta_bits = get_bits(gb, 2) + 1; | |
1231 | 13 | min_val = get_bits(gb, 5); | |
1232 | |||
1233 |
2/2✓ Branch 0 taken 106 times.
✓ Branch 1 taken 13 times.
|
119 | for (sb = 0; sb < coded_subbands; sb++) |
1234 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 106 times.
|
173 | for (i = 0; i < chan->gain_data[sb].num_points; i++) |
1235 | 67 | chan->gain_data[sb].loc_code[i] = min_val + i + | |
1236 | 67 | get_bits(gb, delta_bits); | |
1237 | } | ||
1238 | 28 | break; | |
1239 | } | ||
1240 | |||
1241 | /* Validate decoded information */ | ||
1242 |
2/2✓ Branch 0 taken 3920 times.
✓ Branch 1 taken 582 times.
|
4502 | for (sb = 0; sb < coded_subbands; sb++) { |
1243 | 3920 | dst = &chan->gain_data[sb]; | |
1244 |
2/2✓ Branch 0 taken 1126 times.
✓ Branch 1 taken 3920 times.
|
5046 | for (i = 0; i < chan->gain_data[sb].num_points; i++) { |
1245 |
4/6✓ Branch 0 taken 1126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1126 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 151 times.
✓ Branch 5 taken 975 times.
|
1126 | if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 || |
1246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
|
151 | (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 | 582 | 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 | */ | ||
1267 | 813 | static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
1268 | int num_channels, AVCodecContext *avctx) | ||
1269 | { | ||
1270 | int ch_num, coded_subbands, sb, ret; | ||
1271 | |||
1272 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch_num = 0; ch_num < num_channels; ch_num++) { |
1273 | 1626 | memset(ctx->channels[ch_num].gain_data, 0, | |
1274 | sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS); | ||
1275 | |||
1276 |
2/2✓ Branch 1 taken 582 times.
✓ Branch 2 taken 1044 times.
|
1626 | if (get_bits1(gb)) { /* gain control data present? */ |
1277 | 582 | coded_subbands = get_bits(gb, 4) + 1; | |
1278 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 576 times.
|
582 | if (get_bits1(gb)) /* is high band gain data replication on? */ |
1279 | 6 | ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1; | |
1280 | else | ||
1281 | 576 | ctx->channels[ch_num].num_gain_subbands = coded_subbands; | |
1282 | |||
1283 |
2/4✓ Branch 1 taken 582 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 582 times.
✗ Branch 4 not taken.
|
1164 | if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 || |
1284 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 582 times.
|
1164 | (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 || |
1285 | 582 | (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0) | |
1286 | ✗ | return ret; | |
1287 | |||
1288 |
1/2✓ Branch 0 taken 582 times.
✗ Branch 1 not taken.
|
582 | if (coded_subbands > 0) { /* propagate gain data if requested */ |
1289 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 582 times.
|
588 | for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++) |
1290 | 6 | ctx->channels[ch_num].gain_data[sb] = | |
1291 | 6 | ctx->channels[ch_num].gain_data[sb - 1]; | |
1292 | } | ||
1293 | } else { | ||
1294 | 1044 | ctx->channels[ch_num].num_gain_subbands = 0; | |
1295 | } | ||
1296 | } | ||
1297 | |||
1298 | 813 | 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 | */ | ||
1310 | ✗ | static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
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 | */ | ||
1351 | ✗ | static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
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 | */ | ||
1415 | ✗ | static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
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 | */ | ||
1475 | ✗ | static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
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 | */ | ||
1575 | ✗ | static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
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 | */ | ||
1600 | 813 | static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
1601 | int num_channels, AVCodecContext *avctx) | ||
1602 | { | ||
1603 | int ch_num, i, ret; | ||
1604 | int band_has_tones[16]; | ||
1605 | |||
1606 |
2/2✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 813 times.
|
2439 | for (ch_num = 0; ch_num < num_channels; ch_num++) |
1607 | 1626 | memset(ctx->channels[ch_num].tones_info, 0, | |
1608 | sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS); | ||
1609 | |||
1610 | 813 | ctx->waves_info->tones_present = get_bits1(gb); | |
1611 |
1/2✓ Branch 0 taken 813 times.
✗ Branch 1 not taken.
|
813 | if (!ctx->waves_info->tones_present) |
1612 | 813 | 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 | |||
1662 | 813 | int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, | |
1663 | int num_channels, AVCodecContext *avctx) | ||
1664 | { | ||
1665 | int ret; | ||
1666 | |||
1667 | /* parse sound header */ | ||
1668 | 813 | ctx->num_quant_units = get_bits(gb, 5) + 1; | |
1669 |
3/4✓ Branch 0 taken 451 times.
✓ Branch 1 taken 362 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 451 times.
|
813 | 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 | 813 | ctx->mute_flag = get_bits1(gb); | |
1677 | |||
1678 | /* decode various sound parameters */ | ||
1679 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if ((ret = decode_quant_wordlen(gb, ctx, num_channels, avctx)) < 0) |
1680 | ✗ | return ret; | |
1681 | |||
1682 | 813 | ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1; | |
1683 | 1626 | ctx->num_coded_subbands = ctx->used_quant_units | |
1684 | 776 | ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1 | |
1685 |
2/2✓ Branch 0 taken 776 times.
✓ Branch 1 taken 37 times.
|
813 | : 0; |
1686 | |||
1687 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if ((ret = decode_scale_factors(gb, ctx, num_channels, avctx)) < 0) |
1688 | ✗ | return ret; | |
1689 | |||
1690 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if ((ret = decode_code_table_indexes(gb, ctx, num_channels, avctx)) < 0) |
1691 | ✗ | return ret; | |
1692 | |||
1693 | 813 | decode_spectrum(gb, ctx, num_channels, avctx); | |
1694 | |||
1695 |
1/2✓ Branch 0 taken 813 times.
✗ Branch 1 not taken.
|
813 | if (num_channels == 2) { |
1696 | 813 | get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands); | |
1697 | 813 | get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands); | |
1698 | } | ||
1699 | |||
1700 | 813 | decode_window_shape(gb, ctx, num_channels); | |
1701 | |||
1702 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if ((ret = decode_gainc_data(gb, ctx, num_channels, avctx)) < 0) |
1703 | ✗ | return ret; | |
1704 | |||
1705 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 813 times.
|
813 | if ((ret = decode_tones_info(gb, ctx, num_channels, avctx)) < 0) |
1706 | ✗ | return ret; | |
1707 | |||
1708 | /* decode global noise info */ | ||
1709 | 813 | ctx->noise_present = get_bits1(gb); | |
1710 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 813 times.
|
813 | 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 | 813 | return 0; | |
1716 | } | ||
1717 |