FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_usac_mps212.c
Date: 2026-06-06 18:10:07
Exec Total Coverage
Lines: 0 551 0.0%
Functions: 0 22 0.0%
Branches: 0 396 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2025 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "aacdec_tab.h"
22 #include "libavcodec/get_bits.h"
23 #include "libavutil/macros.h"
24 #include "libavutil/avassert.h"
25
26 #include "aacdec_usac_mps212.h"
27
28 static int huff_dec_1D(GetBitContext *gb, const int16_t (*tab)[2])
29 {
30 int idx = 0;
31 do {
32 /* Overreads are not possible here, the array forms a closed set */
33 idx = tab[idx][get_bits1(gb)];
34 } while (idx > 0);
35 return idx;
36 }
37
38 static int huff_dec_2D(GetBitContext *gb, const int16_t (*tab)[2], int16_t ret[2])
39 {
40 int idx = huff_dec_1D(gb, tab);
41 if (!idx) { /* Escape */
42 ret[0] = 0;
43 ret[1] = 1;
44 return 1;
45 }
46
47 idx = -(idx + 1);
48 ret[0] = idx >> 4;
49 ret[1] = idx & 0xf;
50 return 0;
51 }
52
53 static int huff_data_1d(GetBitContext *gb, int16_t *data, int data_bands,
54 enum AACMPSDataType data_type, int diff_freq, int p0_flag)
55 {
56 const int16_t (*hcod_first_band)[2];
57 const int16_t (*hcod1D)[2];
58
59 switch (data_type) {
60 case MPS_CLD:
61 hcod_first_band = ff_aac_hcod_firstband_CLD;
62 hcod1D = ff_aac_hcod1D_CLD[diff_freq];
63 break;
64 case MPS_ICC:
65 hcod_first_band = ff_aac_hcod_firstband_ICC;
66 hcod1D = ff_aac_hcod1D_ICC;
67 break;
68 case MPS_IPD:
69 hcod_first_band = ff_aac_hcod_firstband_IPD;
70 hcod1D = ff_aac_hcod1D_IPD[diff_freq];
71 if (data_bands == 1)
72 hcod1D = ff_aac_hcod1D_IPD[!diff_freq];
73 break;
74 default:
75 av_unreachable("Invalid data type");
76 }
77
78 if (p0_flag)
79 data[0] = -(huff_dec_1D(gb, hcod_first_band) + 1);
80
81 for (int off = diff_freq; off < data_bands; off++) {
82 int16_t val = -(huff_dec_1D(gb, hcod1D) + 1);
83 if (val && data_type != MPS_IPD)
84 val = get_bits1(gb) ? -val : val;
85 data[off] = val;
86 }
87
88 return 0;
89 }
90
91 static void symmetry_data(GetBitContext *gb, int16_t data[2],
92 uint8_t lav, enum AACMPSDataType data_type)
93 {
94 int16_t sum = data[0] + data[1];
95 int16_t diff = data[0] - data[1];
96
97 if (sum > lav) {
98 data[0] = -sum + (2*lav + 1);
99 data[1] = -diff;
100 } else {
101 data[0] = sum;
102 data[1] = diff;
103 }
104
105 if ((data_type != MPS_IPD) && (data[0] + data[1])) {
106 int sym = get_bits1(gb) ? -1 : 1;
107 data[0] *= sym;
108 data[1] *= sym;
109 }
110
111 if (data[0] - data[1]) {
112 if (get_bits1(gb))
113 FFSWAP(int16_t, data[0], data[1]);
114 }
115 }
116
117 /* NB: NOT a standard integer log2! */
118 static int mps_log2(int s) {
119 if (s)
120 s--;
121 int v = 0;
122 while (s) {
123 s >>= 1;
124 v++;
125 }
126 return v;
127 }
128
129 static void pcm_decode(GetBitContext *gb, int16_t *data0, int16_t *data1,
130 int16_t offset, int nb_pcm_data_bands,
131 int nb_quant_steps, int nb_levels)
132 {
133 int max_group_len;
134 switch (nb_levels) {
135 case 3: max_group_len = 5; break;
136 case 7: max_group_len = 6; break;
137 case 11: max_group_len = 2; break;
138 case 13: max_group_len = 4; break;
139 case 19: max_group_len = 4; break;
140 case 25: max_group_len = 3; break;
141 case 51: max_group_len = 4; break;
142 case 4: case 8: case 15: case 16: case 26: case 31:
143 max_group_len = 1;
144 break;
145 default:
146 return;
147 };
148
149 av_assert1(data0 || data1);
150
151 int pcm_chunk_size[7] = { 0 };
152
153 int tmp = 1;
154 for (int i = 1; i <= max_group_len; i++) {
155 tmp *= nb_levels;
156 pcm_chunk_size[i] = mps_log2(tmp);
157 }
158
159 for (int i = 0; i < nb_pcm_data_bands; i+= max_group_len) {
160 int group_len = FFMIN(max_group_len, nb_pcm_data_bands - i);
161
162 int pcm = get_bits(gb, pcm_chunk_size[group_len]);
163 for (int j = 0; j < group_len; j++) {
164 int idx = i + (group_len - 1) - j;
165 int val = pcm % nb_levels;
166 if (data0 && data1) {
167 if (idx % 2)
168 data1[idx / 2] = val - offset;
169 else
170 data0[idx / 2] = val - offset;
171 } else if (!data1) {
172 data0[idx] = val - offset;
173 } else if (!data0) {
174 data1[idx] = val - offset;
175 }
176 pcm = (pcm - val) / nb_levels;
177 }
178 }
179 }
180
181 static void huff_data_2d(GetBitContext *gb, int16_t *part0_data[2], int16_t (*data)[2],
182 int data_bands, int stride, enum AACMPSDataType data_type,
183 int diff_freq, int freq_pair)
184 {
185 int16_t lav_idx = huff_dec_1D(gb, ff_aac_hcod_lav_idx);
186 uint8_t lav = ff_aac_lav_tab_XXX[data_type][-(lav_idx + 1)];
187
188 const int16_t (*hcod1D)[2];
189 const int16_t (*hcod2D)[2];
190 switch (data_type) {
191 case MPS_CLD:
192 hcod1D = ff_aac_hcod_firstband_CLD;
193 switch (lav) {
194 case 3: hcod2D = ff_aac_hcod2D_CLD_03[freq_pair][diff_freq]; break;
195 case 5: hcod2D = ff_aac_hcod2D_CLD_05[freq_pair][diff_freq]; break;
196 case 7: hcod2D = ff_aac_hcod2D_CLD_07[freq_pair][diff_freq]; break;
197 case 9: hcod2D = ff_aac_hcod2D_CLD_09[freq_pair][diff_freq]; break;
198 }
199 break;
200 case MPS_ICC:
201 hcod1D = ff_aac_hcod_firstband_ICC;
202 switch (lav) {
203 case 1: hcod2D = ff_aac_hcod2D_ICC_01[freq_pair][diff_freq]; break;
204 case 3: hcod2D = ff_aac_hcod2D_ICC_03[freq_pair][diff_freq]; break;
205 case 5: hcod2D = ff_aac_hcod2D_ICC_05[freq_pair][diff_freq]; break;
206 case 7: hcod2D = ff_aac_hcod2D_ICC_07[freq_pair][diff_freq]; break;
207 }
208 break;
209 case MPS_IPD:
210 hcod1D = ff_aac_hcod_firstband_IPD;
211 switch (lav) {
212 case 1: hcod2D = ff_aac_hcod2D_IPD_01[freq_pair][diff_freq]; break;
213 case 3: hcod2D = ff_aac_hcod2D_IPD_03[freq_pair][diff_freq]; break;
214 case 5: hcod2D = ff_aac_hcod2D_IPD_05[freq_pair][diff_freq]; break;
215 case 7: hcod2D = ff_aac_hcod2D_IPD_07[freq_pair][diff_freq]; break;
216 }
217 break;
218 default:
219 av_unreachable("Invalid data type");
220 }
221
222 if (part0_data[0])
223 part0_data[0][0] = -(huff_dec_1D(gb, hcod1D) + 1);
224 if (part0_data[1])
225 part0_data[1][0] = -(huff_dec_1D(gb, hcod1D) + 1);
226
227 int i = 0;
228 int esc_cnt = 0;
229 int16_t esc_data[2][28];
230 int esc_idx[28];
231 for (; i < data_bands; i += stride) {
232 if (huff_dec_2D(gb, hcod2D, data[i]))
233 esc_idx[esc_cnt++] = i; /* Escape */
234 else
235 symmetry_data(gb, data[i], lav, data_type);
236 }
237
238 if (esc_cnt) {
239 pcm_decode(gb, esc_data[0], esc_data[1],
240 0, 2*esc_cnt, 0, (2*lav + 1));
241 for (i = 0; i < esc_cnt; i++) {
242 data[esc_idx[i]][0] = esc_data[0][i] - lav;
243 data[esc_idx[i]][1] = esc_data[1][i] - lav;
244 }
245 }
246 }
247
248 static int huff_decode(GetBitContext *gb, int16_t *data[2],
249 enum AACMPSDataType data_type, int diff_freq[2],
250 int num_val, int *time_pair)
251 {
252 int16_t pair_vec[28][2];
253 int num_val_ch[2] = { num_val, num_val };
254 int16_t *p0_data[2][2] = { 0 };
255 int df_rest_flag[2] = { 0, 0 };
256
257 /* Coding scheme */
258 int dim = get_bits1(gb);
259 if (dim) { /* 2D */
260 *time_pair = 0;
261 if (data[0] && data[1])
262 *time_pair = get_bits1(gb);
263
264 if (*time_pair) {
265 if (diff_freq[0] || diff_freq[1]) {
266 p0_data[0][0] = data[0];
267 p0_data[0][1] = data[1];
268
269 data[0] += 1;
270 data[1] += 1;
271
272 num_val_ch[0] -= 1;
273 }
274
275 int diff_mode = 1;
276 if (!diff_freq[0] || !diff_freq[1])
277 diff_mode = 0; // time
278
279 huff_data_2d(gb, p0_data[0], pair_vec, num_val_ch[0], 1, data_type,
280 diff_mode, 0);
281
282 for (int i = 0; i < num_val_ch[0]; i++) {
283 data[0][i] = pair_vec[i][0];
284 data[1][i] = pair_vec[i][1];
285 }
286 } else {
287 if (data[0]) {
288 if (diff_freq[0]) {
289 p0_data[0][0] = data[0];
290 p0_data[0][1] = NULL;
291
292 num_val_ch[0] -= 1;
293 data[0]++;
294 }
295 df_rest_flag[0] = num_val_ch[0] % 2;
296 if (df_rest_flag[0])
297 num_val_ch[0] -= 1;
298 if (num_val_ch[0] < 0)
299 return AVERROR(EINVAL);
300 }
301
302 if (data[1]) {
303 if (diff_freq[1]) {
304 p0_data[1][0] = NULL;
305 p0_data[1][1] = data[1];
306
307 num_val_ch[1] -= 1;
308 data[1]++;
309 }
310 df_rest_flag[1] = num_val_ch[1] % 2;
311 if (df_rest_flag[1])
312 num_val_ch[1] -= 1;
313 if (num_val_ch[1] < 0)
314 return AVERROR(EINVAL);
315 }
316
317 if (data[0]) {
318 huff_data_2d(gb, p0_data[0], pair_vec, num_val_ch[0], 2, data_type,
319 diff_freq[0], 1);
320 if (df_rest_flag[0])
321 huff_data_1d(gb, data[0] + num_val_ch[0], 1,
322 data_type, !diff_freq[0], 0);
323 }
324 if (data[1]) {
325 huff_data_2d(gb, p0_data[1], pair_vec + 1, num_val_ch[1], 2, data_type,
326 diff_freq[1], 1);
327 if (df_rest_flag[1])
328 huff_data_1d(gb, data[1] + num_val_ch[1], 1,
329 data_type, !diff_freq[1], 0);
330 }
331 }
332 } else { /* 1D */
333 if (data[0])
334 huff_data_1d(gb, data[0], num_val, data_type, diff_freq[0], diff_freq[0]);
335 if (data[1])
336 huff_data_1d(gb, data[1], num_val, data_type, diff_freq[1], diff_freq[1]);
337 }
338
339 return 0;
340 }
341
342 static void diff_freq_decode(const int16_t *diff, int16_t *out, int nb_val)
343 {
344 int i = 0;
345 out[0] = diff[0];
346 for (i = 1; i < nb_val; i++)
347 out[i] = out[i - 1] + diff[i];
348 }
349
350 static void diff_time_decode_backwards(const int16_t *prev, const int16_t *diff,
351 int16_t *out, const int mixed_diff_type,
352 const int nb_val)
353 {
354 if (mixed_diff_type)
355 out[0] = diff[0];
356 for (int i = mixed_diff_type; i < nb_val; i++)
357 out[i] = prev[i] + diff[i];
358 }
359
360 static void diff_time_decode_forwards(const int16_t *prev, const int16_t *diff,
361 int16_t *out, const int mixed_diff_type,
362 const int nb_val)
363 {
364 if (mixed_diff_type)
365 out[0] = diff[0];
366 for (int i = mixed_diff_type; i < nb_val; i++)
367 out[i] = prev[i] - diff[i];
368 }
369
370 static void attach_lsb(GetBitContext *gb, int16_t *data_msb,
371 int offset, int nb_lsb, int nb_val,
372 int16_t *data)
373 {
374 for (int i = 0; i < nb_val; i++) {
375 int msb = data_msb[i];
376 if (nb_lsb > 0) {
377 uint32_t lsb = get_bits(gb, nb_lsb);
378 data[i] = ((msb << nb_lsb) | lsb) - offset;
379 } else {
380 data[i] = msb - offset;
381 }
382 }
383 }
384
385 static int ec_pair_dec(GetBitContext *gb,
386 int16_t set1[MPS_MAX_PARAM_BANDS],
387 int16_t set2[MPS_MAX_PARAM_BANDS], int16_t *last,
388 enum AACMPSDataType data_type, int start_band, int nb_bands,
389 int pair, int coarse,
390 int diff_time_back)
391 {
392 int attach_lsb_flag = 0;
393 int quant_levels = 0;
394 int quant_offset = 0;
395
396 switch (data_type) {
397 case MPS_CLD:
398 if (coarse) {
399 attach_lsb_flag = 0;
400 quant_levels = 15;
401 quant_offset = 7;
402 } else {
403 attach_lsb_flag = 0;
404 quant_levels = 31;
405 quant_offset = 15;
406 }
407 break;
408 case MPS_ICC:
409 if (coarse) {
410 attach_lsb_flag = 0;
411 quant_levels = 4;
412 quant_offset = 0;
413 } else {
414 attach_lsb_flag = 0;
415 quant_levels = 8;
416 quant_offset = 0;
417 }
418 break;
419 case MPS_IPD:
420 if (!coarse) {
421 attach_lsb_flag = 1;
422 quant_levels = 16;
423 quant_offset = 0;
424 } else {
425 attach_lsb_flag = 0;
426 quant_levels = 8;
427 quant_offset = 0;
428 }
429 break;
430 }
431
432 int16_t last_msb[28] = { 0 };
433 int16_t data_pair[2][28] = { 0 };
434 int16_t data_diff[2][28] = { 0 };
435 int16_t *p_data[2];
436
437 int pcm_coding = get_bits1(gb);
438 if (pcm_coding) { /* bsPcmCoding */
439 int nb_pcm_vals;
440 if (pair) {
441 p_data[0] = data_pair[0];
442 p_data[1] = data_pair[1];
443 nb_pcm_vals = 2 * nb_bands;
444 } else {
445 p_data[0] = data_pair[0];
446 p_data[1] = NULL;
447 nb_pcm_vals = nb_bands;
448 }
449
450 int nb_quant_steps;
451 switch (data_type) {
452 case MPS_CLD: nb_quant_steps = coarse ? 15 : 31; break;
453 case MPS_ICC: nb_quant_steps = coarse ? 4 : 8; break;
454 case MPS_IPD: nb_quant_steps = coarse ? 8 : 16; break;
455 }
456 pcm_decode(gb, p_data[0], p_data[1], quant_offset, nb_pcm_vals,
457 nb_quant_steps, quant_levels);
458
459 memcpy(&set1[start_band], data_pair[0], 2*nb_bands);
460 if (pair)
461 memcpy(&set2[start_band], data_pair[1], 2*nb_bands);
462
463 return 0;
464 }
465
466 if (pair) {
467 p_data[0] = data_diff[0];
468 p_data[1] = data_diff[1];
469 } else {
470 p_data[0] = data_diff[0];
471 p_data[1] = NULL;
472 }
473
474 int diff_freq[2] = { 1, 1 };
475 int backwards = 1;
476
477 if (pair || diff_time_back)
478 diff_freq[0] = !get_bits1(gb);
479
480 if (pair && (diff_freq[0] || diff_time_back))
481 diff_freq[1] = !get_bits1(gb);
482
483 int time_pair = 0;
484 huff_decode(gb, p_data, data_type, diff_freq,
485 nb_bands, &time_pair);
486
487 /* Differential decoding */
488 if (!diff_freq[0] || !diff_freq[1]) {
489 if (0 /* 1 if SAOC */) {
490 backwards = 1;
491 } else {
492 if (pair) {
493 if (!diff_freq[0] && !diff_time_back)
494 backwards = 0;
495 else if (!diff_freq[1])
496 backwards = 1;
497 else
498 backwards = !get_bits1(gb);
499 } else {
500 backwards = 1;
501 }
502 }
503 }
504
505 int mixed_time_pair = (diff_freq[0] != diff_freq[1]) && time_pair;
506
507 if (backwards) {
508 if (diff_freq[0]) {
509 diff_freq_decode(data_diff[0], data_pair[0], nb_bands);
510 } else {
511 for (int i = 0; i < nb_bands; i++) {
512 last_msb[i] = last[i + start_band] + quant_offset;
513 if (attach_lsb_flag) {
514 last_msb[i] >>= 1;
515 }
516 }
517 diff_time_decode_backwards(last_msb, data_diff[0], data_pair[0],
518 mixed_time_pair, nb_bands);
519 }
520
521 if (diff_freq[1])
522 diff_freq_decode(data_diff[1], data_pair[1], nb_bands);
523 else
524 diff_time_decode_backwards(data_pair[0], data_diff[1],
525 data_pair[1], mixed_time_pair, nb_bands);
526 } else {
527 diff_freq_decode(data_diff[1], data_pair[1], nb_bands);
528
529 if (diff_freq[0])
530 diff_freq_decode(data_diff[0], data_pair[0], nb_bands);
531 else
532 diff_time_decode_forwards(data_pair[1], data_diff[0], data_pair[0],
533 mixed_time_pair, nb_bands);
534 }
535
536 /* Decode LSBs */
537 attach_lsb(gb, data_pair[0], quant_offset, attach_lsb_flag,
538 nb_bands, data_pair[0]);
539 if (pair)
540 attach_lsb(gb, data_pair[1], quant_offset, attach_lsb_flag,
541 nb_bands, data_pair[1]);
542
543 memcpy(&set1[start_band], data_pair[0], 2*nb_bands);
544 if (pair)
545 memcpy(&set2[start_band], data_pair[1], 2*nb_bands);
546
547 return 0;
548 }
549
550 static void coarse_to_fine(int16_t *data, enum AACMPSDataType data_type,
551 int start_band, int end_band)
552 {
553 for (int i = start_band; i < end_band; i++)
554 data[i] *= 2;
555 if (data_type == MPS_CLD) {
556 for (int i = start_band; i < end_band; i++) {
557 if (data[i] == -14)
558 data[i] = -15;
559 else if (data[i] == 14)
560 data[i] = 15;
561 }
562 }
563 }
564
565 static void fine_to_coarse(int16_t *data, enum AACMPSDataType data_type,
566 int start_band, int end_band)
567 {
568 for (int i = start_band; i < end_band; i++) {
569 if (data_type == MPS_CLD)
570 data[i] /= 2;
571 else
572 data[i] >>= 1;
573 }
574 }
575
576 static int get_freq_strides(int16_t *freq_strides, int band_stride,
577 int start_band, int end_band)
578 {
579 int data_bands = (end_band - start_band - 1) / band_stride + 1;
580
581 freq_strides[0] = start_band;
582 for (int i = 1; i <= data_bands; i++)
583 freq_strides[i] = freq_strides[i - 1] + band_stride;
584
585 int offs = 0;
586 while (freq_strides[data_bands] > end_band) {
587 if (offs < data_bands)
588 offs++;
589 for (int i = offs; i <= data_bands; i++) {
590 freq_strides[i]--;
591 }
592 }
593
594 return data_bands;
595 }
596
597 static const int stride_table[4] = { 1, 2, 5, 28 };
598
599 int ff_aac_ec_data_dec(GetBitContext *gb, AACMPSLosslessData *ld,
600 enum AACMPSDataType data_type,
601 int default_val,
602 int start_band, int end_band, int frame_indep_flag,
603 int indep_flag, int nb_param_sets)
604 {
605 for (int i = 0; i < nb_param_sets; i++) {
606 ld->data_mode[i] = get_bits(gb, 2);
607 /* Error checking */
608 if ((indep_flag && !i && (ld->data_mode[i] == 1 || ld->data_mode[i] == 2)) ||
609 ((i == (nb_param_sets - 1) && (ld->data_mode[i] == 2)))) {
610 return AVERROR(EINVAL);
611 }
612 }
613
614 int set_idx = 0;
615 int data_pair = 0;
616 bool old_coarse = ld->quant_coarse_prev;
617
618 for (int i = 0; i < nb_param_sets; i++) {
619 if (!ld->data_mode[i]) {
620 for (int j = start_band; j < end_band; j++)
621 ld->last_data[j] = default_val;
622 old_coarse = 0;
623 }
624
625 if (ld->data_mode[i] != 3) {
626 continue;
627 } else if (data_pair) {
628 data_pair = 0;
629 continue;
630 }
631
632 data_pair = get_bits1(gb);
633 ld->coarse_quant[set_idx] = get_bits1(gb);
634 ld->freq_res[set_idx] = get_bits(gb, 2);
635
636 if (ld->coarse_quant[set_idx] != old_coarse) {
637 if (old_coarse)
638 coarse_to_fine(ld->last_data, data_type, start_band, end_band);
639 else
640 fine_to_coarse(ld->last_data, data_type, start_band, end_band);
641 }
642
643 int16_t freq_stride_map[MPS_MAX_PARAM_BANDS + 1];
644 int data_bands = get_freq_strides(freq_stride_map,
645 stride_table[ld->freq_res[set_idx]],
646 start_band, end_band);
647
648 if (set_idx + data_pair >= MPS_MAX_PARAM_SETS)
649 return AVERROR(EINVAL);
650
651 for (int j = 0; j < data_bands; j++)
652 ld->last_data[start_band + j] = ld->last_data[freq_stride_map[j]];
653
654 int err = ec_pair_dec(gb,
655 ld->data[set_idx + 0], ld->data[set_idx + 1],
656 ld->last_data, data_type, start_band, end_band - start_band,
657 data_pair, ld->coarse_quant[set_idx],
658 !(indep_flag && (i == 0)) || (set_idx > 0));
659 if (err < 0)
660 return err;
661
662 if (data_type == MPS_IPD) {
663 const int mask = ld->coarse_quant[set_idx] ? 0x7 : 0xF;
664 for (int j = 0; j < data_bands; j++)
665 for (int k = freq_stride_map[j + 0]; k < freq_stride_map[j + 1]; k++)
666 ld->last_data[k] = ld->data[set_idx + data_pair][start_band + j] & mask;
667 } else {
668 for (int j = 0; j < data_bands; j++)
669 for (int k = freq_stride_map[j + 0]; k < freq_stride_map[j + 1]; k++)
670 ld->last_data[k] = ld->data[set_idx + data_pair][start_band + j];
671 }
672
673 old_coarse = ld->coarse_quant[set_idx];
674 if (data_pair) {
675 ld->coarse_quant[set_idx + 1] = ld->coarse_quant[set_idx];
676 ld->freq_res[set_idx + 1] = ld->freq_res[set_idx];
677 }
678 set_idx += data_pair + 1;
679 }
680
681 ld->quant_coarse_prev = old_coarse;
682
683 return 0;
684 }
685
686 int ff_aac_huff_dec_reshape(GetBitContext *gb, int16_t *out_data,
687 int nb_val)
688 {
689 int val, len;
690 int val_received = 0;
691 int16_t rl_data[2] = { 0 };
692
693 while (val_received < nb_val) {
694 huff_dec_2D(gb, ff_aac_hcod2D_reshape, rl_data);
695 val = rl_data[0];
696 len = rl_data[1] + 1;
697 if (val_received + len > nb_val)
698 return AVERROR(EINVAL);
699 for (int i = val_received; i < val_received + len; i++)
700 out_data[i] = val;
701 val_received += len;
702 }
703
704 return 0;
705 }
706
707 static void create_mapping(int map[MPS_MAX_PARAM_BANDS + 1],
708 int start_band, int stop_band, int stride)
709 {
710 int diff[MPS_MAX_PARAM_BANDS + 1];
711 int src_bands = stop_band - start_band;
712 int dst_bands = (src_bands - 1) / stride + 1;
713
714 if (dst_bands < 1)
715 dst_bands = 1;
716
717 int bands_achived = dst_bands * stride;
718 int bands_diff = src_bands - bands_achived;
719 for (int i = 0; i < dst_bands; i++)
720 diff[i] = stride;
721
722 int incr, k;
723 if (bands_diff > 0) {
724 incr = -1;
725 k = dst_bands - 1;
726 } else {
727 incr = 1;
728 k = 0;
729 }
730
731 while (bands_diff != 0) {
732 diff[k] = diff[k] - incr;
733 k = k + incr;
734 bands_diff = bands_diff + incr;
735 if (k >= dst_bands) {
736 if (bands_diff > 0) {
737 k = dst_bands - 1;
738 } else if (bands_diff < 0) {
739 k = 0;
740 }
741 }
742 }
743
744 map[0] = start_band;
745 for (int i = 0; i < dst_bands; i++)
746 map[i + 1] = map[i] + diff[i];
747 }
748
749 static void map_freq(int16_t *dst, const int16_t *src,
750 int *map, int nb_bands)
751 {
752 for (int i = 0; i < nb_bands; i++) {
753 int value = src[i + map[0]];
754 int start_band = map[i];
755 int stop_band = map[i + 1];
756 for (int j = start_band; j < stop_band; j++) {
757 dst[j] = value;
758 }
759 }
760 }
761
762 static int deq_idx(int value, enum AACMPSDataType data_type)
763 {
764 int idx = -1;
765
766 switch (data_type) {
767 case MPS_CLD:
768 if (((value + 15) >= 0) && ((value + 15) < 31))
769 idx = (value + 15);
770 break;
771 case MPS_ICC:
772 if ((value >= 0) && (value < 8))
773 idx = value;
774 break;
775 case MPS_IPD:
776 /* (+/-)15 * MAX_PARAMETER_BANDS for differential coding in frequency
777 * domain (according to rbl) */
778 if ((value >= -420) && (value <= 420))
779 idx = (value & 0xf);
780 break;
781 }
782
783 return idx;
784 }
785
786 int ff_aac_map_index_data(AACMPSLosslessData *ld,
787 enum AACMPSDataType data_type,
788 int dst_idx[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS],
789 int default_value, int start_band, int stop_band,
790 int nb_param_sets, const int *param_set_idx,
791 int extend_frame)
792 {
793 if (nb_param_sets > MPS_MAX_PARAM_SETS)
794 return AVERROR(EINVAL);
795
796 int data_mode_3_idx[MPS_MAX_PARAM_SETS] = { 0 };
797 int nb_data_mode_3 = 0;
798 for (int i = 0; i < nb_param_sets; i++) {
799 if (ld->data_mode[i] == 3) {
800 data_mode_3_idx[nb_data_mode_3] = i;
801 nb_data_mode_3++;
802 }
803 }
804
805 int set_idx = 0;
806
807 /* Prepare data */
808 int interpolate[MPS_MAX_PARAM_SETS] = { 0 };
809 int16_t tmp_idx_data[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS] = { 0 };
810 for (int i = 0; i < nb_param_sets; i++) {
811 if (ld->data_mode[i] == 0) {
812 ld->coarse_quant_no[i] = 0;
813 for (int band = start_band; band < stop_band; band++)
814 tmp_idx_data[i][band] = default_value;
815 for (int band = start_band; band < stop_band; band++)
816 ld->last_data[band] = tmp_idx_data[i][band];
817 ld->quant_coarse_prev = 0;
818 }
819
820 if (ld->data_mode[i] == 1) {
821 for (int band = start_band; band < stop_band; band++)
822 tmp_idx_data[i][band] = ld->last_data[band];
823 ld->coarse_quant_no[i] = ld->quant_coarse_prev;
824 }
825
826 if (ld->data_mode[i] == 2) {
827 for (int band = start_band; band < stop_band; band++)
828 tmp_idx_data[i][band] = ld->last_data[band];
829 ld->coarse_quant_no[i] = ld->quant_coarse_prev;
830 interpolate[i] = 1;
831 } else {
832 interpolate[i] = 0;
833 }
834
835 if (ld->data_mode[i] == 3) {
836 int stride;
837
838 int parmSlot = data_mode_3_idx[set_idx];
839 stride = stride_table[ld->freq_res[set_idx]];
840 int dataBands = (stop_band - start_band - 1) / stride + 1;
841
842 int tmp[MPS_MAX_PARAM_BANDS + 1];
843 create_mapping(tmp, start_band, stop_band, stride);
844 map_freq(tmp_idx_data[parmSlot], ld->data[set_idx],
845 tmp, dataBands);
846
847 for (int band = start_band; band < stop_band; band++)
848 ld->last_data[band] = tmp_idx_data[parmSlot][band];
849
850 ld->quant_coarse_prev = ld->coarse_quant[set_idx];
851 ld->coarse_quant_no[i] = ld->coarse_quant[set_idx];
852
853 set_idx++;
854 }
855 }
856
857 /* Map all coarse data to fine */
858 for (int i = 0; i < nb_param_sets; i++) {
859 if (ld->coarse_quant_no[i] == 1) {
860 coarse_to_fine(tmp_idx_data[i], data_type, start_band,
861 stop_band);
862 ld->coarse_quant_no[i] = 0;
863 }
864 }
865
866 /* Interpolate */
867 int i1 = 0;
868 for (int i = 0; i < nb_param_sets; i++) {
869 if (interpolate[i] != 1) {
870 i1 = i;
871 } else {
872 int xi, i2, x1, x2;
873
874 for (i2 = i; i2 < nb_param_sets; i2++)
875 if (interpolate[i2] != 1)
876 break;
877 if (i2 >= nb_param_sets)
878 return AVERROR(EINVAL);
879
880 x1 = param_set_idx[i1];
881 xi = param_set_idx[i];
882 x2 = param_set_idx[i2];
883
884 for (int band = start_band; band < stop_band; band++) {
885 int yi, y1, y2;
886 y1 = tmp_idx_data[i1][band];
887 y2 = tmp_idx_data[i2][band];
888 if (x1 != x2) {
889 yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
890 } else {
891 yi = y1 /*+ (xi-x1)*(y2-y1)/1e-12*/;
892 }
893 tmp_idx_data[i][band] = yi;
894 }
895 }
896 }
897
898 /* Dequantize data and apply factorCLD if necessary */
899 for (int ps = 0; ps < nb_param_sets; ps++) {
900 /* Dequantize data */
901 for (int band = start_band; band < stop_band; band++) {
902 dst_idx[ps][band] = deq_idx(tmp_idx_data[ps][band],
903 data_type);
904 if (dst_idx[ps][band] == -1)
905 dst_idx[ps][band] = default_value;
906 }
907 }
908
909 if (extend_frame) {
910 if (data_type == MPS_IPD)
911 ld->coarse_quant[nb_param_sets] = ld->coarse_quant[nb_param_sets - 1];
912 for (int band = start_band; band < stop_band; band++)
913 dst_idx[nb_param_sets][band] = dst_idx[nb_param_sets - 1][band];
914 }
915
916 return 0;
917 }
918