FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_usac_mps212.c
Date: 2026-03-13 22:30:28
Exec Total Coverage
Lines: 0 552 0.0%
Functions: 0 22 0.0%
Branches: 0 398 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]][0] = esc_data[0][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_pair[0];
468 p_data[1] = data_pair[1];
469 } else {
470 p_data[0] = data_pair[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;
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, p_data[0], quant_offset, attach_lsb_flag,
538 nb_bands, p_data[0]);
539 if (pair)
540 attach_lsb(gb, p_data[1], quant_offset, attach_lsb_flag,
541 nb_bands, p_data[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 for (int i = 0; i <= data_bands; i++)
595 freq_strides[i] = av_clip_uintp2(freq_strides[i], 2);
596
597 return data_bands;
598 }
599
600 static const int stride_table[4] = { 1, 2, 5, 28 };
601
602 int ff_aac_ec_data_dec(GetBitContext *gb, AACMPSLosslessData *ld,
603 enum AACMPSDataType data_type,
604 int default_val,
605 int start_band, int end_band, int frame_indep_flag,
606 int indep_flag, int nb_param_sets)
607 {
608 for (int i = 0; i < nb_param_sets; i++) {
609 ld->data_mode[i] = get_bits(gb, 2);
610 /* Error checking */
611 if ((indep_flag && !i && (ld->data_mode[i] == 1 || ld->data_mode[i] == 2)) ||
612 ((i == (nb_param_sets - 1) && (ld->data_mode[i] == 2)))) {
613 return AVERROR(EINVAL);
614 }
615 }
616
617 int set_idx = 0;
618 int data_pair = 0;
619 bool old_coarse = ld->quant_coarse_prev;
620
621 for (int i = 0; i < nb_param_sets; i++) {
622 if (!ld->data_mode[i]) {
623 for (int j = start_band; j < end_band; j++)
624 ld->last_data[j] = default_val;
625 old_coarse = 0;
626 }
627
628 if (ld->data_mode[i] != 3) {
629 continue;
630 } else if (data_pair) {
631 data_pair = 0;
632 continue;
633 }
634
635 data_pair = get_bits1(gb);
636 ld->coarse_quant[set_idx] = get_bits1(gb);
637 ld->freq_res[set_idx] = get_bits(gb, 2);
638
639 if (ld->coarse_quant[set_idx] != old_coarse) {
640 if (old_coarse)
641 coarse_to_fine(ld->last_data, data_type, start_band, end_band);
642 else
643 fine_to_coarse(ld->last_data, data_type, start_band, end_band);
644 }
645
646 int data_bands = get_freq_strides(ld->freq_res,
647 stride_table[ld->freq_res[set_idx]],
648 start_band, end_band);
649
650 if (set_idx + data_pair > MPS_MAX_PARAM_SETS)
651 return AVERROR(EINVAL);
652
653 for (int j = 0; j < data_bands; j++)
654 ld->last_data[start_band + j] = ld->last_data[ld->freq_res[j]];
655
656 int err = ec_pair_dec(gb,
657 ld->data[set_idx + 0], ld->data[set_idx + 1],
658 ld->last_data, data_type, start_band, end_band - start_band,
659 data_pair, ld->coarse_quant[set_idx],
660 !(indep_flag && (i == 0)) || (set_idx > 0));
661 if (err < 0)
662 return err;
663
664 if (data_type == MPS_IPD) {
665 const int mask = ld->coarse_quant[set_idx] ? 0x7 : 0xF;
666 for (int j = 0; j < data_bands; j++)
667 for (int k = ld->freq_res[j + 0]; k < ld->freq_res[j + 1]; k++)
668 ld->last_data[k] = ld->data[set_idx + data_pair][start_band + j] & mask;
669 } else {
670 for (int j = 0; j < data_bands; j++)
671 for (int k = ld->freq_res[j + 0]; k < ld->freq_res[j + 1]; k++)
672 ld->last_data[k] = ld->data[set_idx + data_pair][start_band + j];
673 }
674
675 old_coarse = ld->coarse_quant[set_idx];
676 if (data_pair) {
677 ld->coarse_quant[set_idx + 1] = ld->coarse_quant[set_idx];
678 ld->freq_res[set_idx + 1] = ld->freq_res[set_idx];
679 }
680 set_idx += data_pair + 1;
681 }
682
683 ld->quant_coarse_prev = old_coarse;
684
685 return 0;
686 }
687
688 int ff_aac_huff_dec_reshape(GetBitContext *gb, int16_t *out_data,
689 int nb_val)
690 {
691 int val, len;
692 int val_received = 0;
693 int16_t rl_data[2] = { 0 };
694
695 while (val_received < nb_val) {
696 huff_dec_2D(gb, ff_aac_hcod2D_reshape, rl_data);
697 val = rl_data[0];
698 len = rl_data[1] + 1;
699 if (val_received + len > nb_val)
700 return AVERROR(EINVAL);
701 for (int i = val_received; i < val_received + len; i++)
702 out_data[i] = val;
703 val_received += len;
704 }
705
706 return 0;
707 }
708
709 static void create_mapping(int map[MPS_MAX_PARAM_BANDS + 1],
710 int start_band, int stop_band, int stride)
711 {
712 int diff[MPS_MAX_PARAM_BANDS + 1];
713 int src_bands = stop_band - start_band;
714 int dst_bands = (src_bands - 1) / stride + 1;
715
716 if (dst_bands < 1)
717 dst_bands = 1;
718
719 int bands_achived = dst_bands * stride;
720 int bands_diff = src_bands - bands_achived;
721 for (int i = 0; i < dst_bands; i++)
722 diff[i] = stride;
723
724 int incr, k;
725 if (bands_diff > 0) {
726 incr = -1;
727 k = dst_bands - 1;
728 } else {
729 incr = 1;
730 k = 0;
731 }
732
733 while (bands_diff != 0) {
734 diff[k] = diff[k] - incr;
735 k = k + incr;
736 bands_diff = bands_diff + incr;
737 if (k >= dst_bands) {
738 if (bands_diff > 0) {
739 k = dst_bands - 1;
740 } else if (bands_diff < 0) {
741 k = 0;
742 }
743 }
744 }
745
746 map[0] = start_band;
747 for (int i = 0; i < dst_bands; i++)
748 map[i + 1] = map[i] + diff[i];
749 }
750
751 static void map_freq(int16_t *dst, const int16_t *src,
752 int *map, int nb_bands)
753 {
754 for (int i = 0; i < nb_bands; i++) {
755 int value = src[i + map[0]];
756 int start_band = map[i];
757 int stop_band = map[i + 1];
758 for (int j = start_band; j < stop_band; j++) {
759 dst[j] = value;
760 }
761 }
762 }
763
764 static int deq_idx(int value, enum AACMPSDataType data_type)
765 {
766 int idx = -1;
767
768 switch (data_type) {
769 case MPS_CLD:
770 if (((value + 15) >= 0) && ((value + 15) < 31))
771 idx = (value + 15);
772 break;
773 case MPS_ICC:
774 if ((value >= 0) && (value < 8))
775 idx = value;
776 break;
777 case MPS_IPD:
778 /* (+/-)15 * MAX_PARAMETER_BANDS for differential coding in frequency
779 * domain (according to rbl) */
780 if ((value >= -420) && (value <= 420))
781 idx = (value & 0xf);
782 break;
783 }
784
785 return idx;
786 }
787
788 int ff_aac_map_index_data(AACMPSLosslessData *ld,
789 enum AACMPSDataType data_type,
790 int dst_idx[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS],
791 int default_value, int start_band, int stop_band,
792 int nb_param_sets, const int *param_set_idx,
793 int extend_frame)
794 {
795 if (nb_param_sets > MPS_MAX_PARAM_SETS)
796 return AVERROR(EINVAL);
797
798 int data_mode_3_idx[MPS_MAX_PARAM_SETS] = { 0 };
799 int nb_data_mode_3 = 0;
800 for (int i = 0; i < nb_param_sets; i++) {
801 if (ld->data_mode[i] == 3) {
802 data_mode_3_idx[nb_data_mode_3] = i;
803 nb_data_mode_3++;
804 }
805 }
806
807 int set_idx = 0;
808
809 /* Prepare data */
810 int interpolate[MPS_MAX_PARAM_SETS] = { 0 };
811 int16_t tmp_idx_data[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS] = { 0 };
812 for (int i = 0; i < nb_param_sets; i++) {
813 if (ld->data_mode[i] == 0) {
814 ld->coarse_quant_no[i] = 0;
815 for (int band = start_band; band < stop_band; band++)
816 tmp_idx_data[i][band] = default_value;
817 for (int band = start_band; band < stop_band; band++)
818 ld->last_data[band] = tmp_idx_data[i][band];
819 ld->quant_coarse_prev = 0;
820 }
821
822 if (ld->data_mode[i] == 1) {
823 for (int band = start_band; band < stop_band; band++)
824 tmp_idx_data[i][band] = ld->last_data[band];
825 ld->coarse_quant_no[i] = ld->quant_coarse_prev;
826 }
827
828 if (ld->data_mode[i] == 2) {
829 for (int band = start_band; band < stop_band; band++)
830 tmp_idx_data[i][band] = ld->last_data[band];
831 ld->coarse_quant_no[i] = ld->quant_coarse_prev;
832 interpolate[i] = 1;
833 } else {
834 interpolate[i] = 0;
835 }
836
837 if (ld->data_mode[i] == 3) {
838 int stride;
839
840 int parmSlot = data_mode_3_idx[set_idx];
841 stride = stride_table[ld->freq_res[set_idx]];
842 int dataBands = (stop_band - start_band - 1) / stride + 1;
843
844 int tmp[MPS_MAX_PARAM_BANDS + 1];
845 create_mapping(tmp, start_band, stop_band, stride);
846 map_freq(tmp_idx_data[parmSlot], ld->data[set_idx],
847 tmp, dataBands);
848
849 for (int band = start_band; band < stop_band; band++)
850 ld->last_data[band] = tmp_idx_data[parmSlot][band];
851
852 ld->quant_coarse_prev = ld->coarse_quant[set_idx];
853 ld->coarse_quant_no[i] = ld->coarse_quant[set_idx];
854
855 set_idx++;
856 }
857 }
858
859 /* Map all coarse data to fine */
860 for (int i = 0; i < nb_param_sets; i++) {
861 if (ld->coarse_quant_no[i] == 1) {
862 coarse_to_fine(tmp_idx_data[i], data_type, start_band,
863 stop_band - start_band);
864 ld->coarse_quant_no[i] = 0;
865 }
866 }
867
868 /* Interpolate */
869 int i1 = 0;
870 for (int i = 0; i < nb_param_sets; i++) {
871 if (interpolate[i] != 1) {
872 i1 = i;
873 } else {
874 int xi, i2, x1, x2;
875
876 for (i2 = i; i2 < nb_param_sets; i2++)
877 if (interpolate[i2] != 1)
878 break;
879 if (i2 >= nb_param_sets)
880 return AVERROR(EINVAL);
881
882 x1 = param_set_idx[i1];
883 xi = param_set_idx[i];
884 x2 = param_set_idx[i2];
885
886 for (int band = start_band; band < stop_band; band++) {
887 int yi, y1, y2;
888 y1 = tmp_idx_data[i1][band];
889 y2 = tmp_idx_data[i2][band];
890 if (x1 != x2) {
891 yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
892 } else {
893 yi = y1 /*+ (xi-x1)*(y2-y1)/1e-12*/;
894 }
895 tmp_idx_data[i][band] = yi;
896 }
897 }
898 }
899
900 /* Dequantize data and apply factorCLD if necessary */
901 for (int ps = 0; ps < nb_param_sets; ps++) {
902 /* Dequantize data */
903 for (int band = start_band; band < stop_band; band++) {
904 dst_idx[ps][band] = deq_idx(tmp_idx_data[ps][band],
905 data_type);
906 if (dst_idx[ps][band] == -1)
907 dst_idx[ps][band] = default_value;
908 }
909 }
910
911 if (extend_frame) {
912 if (data_type == MPS_IPD)
913 ld->coarse_quant[nb_param_sets] = ld->coarse_quant[nb_param_sets - 1];
914 for (int band = start_band; band < stop_band; band++)
915 dst_idx[nb_param_sets][band] = dst_idx[nb_param_sets - 1][band];
916 }
917
918 return 0;
919 }
920