FFmpeg
aacdec_usac_mps212.c
Go to the documentation of this file.
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_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  return data_bands;
595 }
596 
597 static const int stride_table[4] = { 1, 2, 5, 28 };
598 
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 
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 
787  enum AACMPSDataType data_type,
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];
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];
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 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
static FILE * out
Definition: movenc.c:55
ff_aac_hcod2D_IPD_03
const int16_t ff_aac_hcod2D_IPD_03[2][2][15][2]
Definition: aacdec_tab.c:628
huff_dec_2D
static int huff_dec_2D(GetBitContext *gb, const int16_t(*tab)[2], int16_t ret[2])
Definition: aacdec_usac_mps212.c:38
mask
int mask
Definition: mediacodecdec_common.c:154
AACMPSLosslessData::last_data
int16_t last_data[MPS_MAX_PARAM_BANDS]
Definition: aacdec_usac_mps212.h:39
aacdec_usac_mps212.h
map_freq
static void map_freq(int16_t *dst, const int16_t *src, int *map, int nb_bands)
Definition: aacdec_usac_mps212.c:749
ff_aac_hcod1D_IPD
const int16_t ff_aac_hcod1D_IPD[2][7][2]
Definition: aacdec_tab.c:614
MPS_ICC
@ MPS_ICC
Definition: aacdec_usac_mps212.h:31
ff_aac_hcod2D_IPD_07
const int16_t ff_aac_hcod2D_IPD_07[2][2][63][2]
Definition: aacdec_tab.c:674
data
const char data[16]
Definition: mxf.c:149
ff_aac_hcod2D_ICC_01
const int16_t ff_aac_hcod2D_ICC_01[2][2][3][2]
Definition: aacdec_tab.c:501
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
ff_aac_lav_tab_XXX
const uint8_t ff_aac_lav_tab_XXX[3][4]
Definition: aacdec_tab.c:744
coarse_to_fine
static void coarse_to_fine(int16_t *data, enum AACMPSDataType data_type, int start_band, int end_band)
Definition: aacdec_usac_mps212.c:550
ff_aac_hcod_lav_idx
const int16_t ff_aac_hcod_lav_idx[3][2]
Definition: aacdec_tab.c:729
macros.h
deq_idx
static int deq_idx(int value, enum AACMPSDataType data_type)
Definition: aacdec_usac_mps212.c:762
MPS_MAX_PARAM_BANDS
#define MPS_MAX_PARAM_BANDS
Definition: aac.h:40
GetBitContext
Definition: get_bits.h:109
ec_pair_dec
static int ec_pair_dec(GetBitContext *gb, int16_t set1[MPS_MAX_PARAM_BANDS], int16_t set2[MPS_MAX_PARAM_BANDS], int16_t *last, enum AACMPSDataType data_type, int start_band, int nb_bands, int pair, int coarse, int diff_time_back)
Definition: aacdec_usac_mps212.c:385
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
val
static double val(void *priv, double ch)
Definition: aeval.c:77
mps_log2
static int mps_log2(int s)
Definition: aacdec_usac_mps212.c:118
ff_aac_ec_data_dec
int ff_aac_ec_data_dec(GetBitContext *gb, AACMPSLosslessData *ld, enum AACMPSDataType data_type, int default_val, int start_band, int end_band, int frame_indep_flag, int indep_flag, int nb_param_sets)
Definition: aacdec_usac_mps212.c:599
avassert.h
ff_aac_hcod2D_CLD_03
const int16_t ff_aac_hcod2D_CLD_03[2][2][15][2]
Definition: aacdec_tab.c:306
ff_aac_hcod2D_IPD_05
const int16_t ff_aac_hcod2D_IPD_05[2][2][35][2]
Definition: aacdec_tab.c:643
AACMPSLosslessData::data_mode
int16_t data_mode[MPS_MAX_PARAM_SETS]
Definition: aacdec_usac_mps212.h:41
stride_table
static const int stride_table[4]
Definition: aacdec_usac_mps212.c:597
s
#define s(width, name)
Definition: cbs_vp9.c:198
AACMPSLosslessData::freq_res
int16_t freq_res[MPS_MAX_PARAM_SETS]
Definition: aacdec_usac_mps212.h:43
ff_aac_hcod2D_reshape
const int16_t ff_aac_hcod2D_reshape[39][2]
Definition: aacdec_tab.c:733
AACMPSLosslessData::coarse_quant_no
int16_t coarse_quant_no[MPS_MAX_PARAM_SETS]
Definition: aacdec_usac_mps212.h:44
get_bits.h
ff_aac_hcod2D_CLD_07
const int16_t ff_aac_hcod2D_CLD_07[2][2][63][2]
Definition: aacdec_tab.c:352
fine_to_coarse
static void fine_to_coarse(int16_t *data, enum AACMPSDataType data_type, int start_band, int end_band)
Definition: aacdec_usac_mps212.c:565
huff_dec_1D
static int huff_dec_1D(GetBitContext *gb, const int16_t(*tab)[2])
Definition: aacdec_usac_mps212.c:28
ff_aac_hcod2D_CLD_05
const int16_t ff_aac_hcod2D_CLD_05[2][2][35][2]
Definition: aacdec_tab.c:321
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
ff_aac_hcod_firstband_CLD
const int16_t ff_aac_hcod_firstband_CLD[30][2]
Definition: aacdec_tab.c:282
symmetry_data
static void symmetry_data(GetBitContext *gb, int16_t data[2], uint8_t lav, enum AACMPSDataType data_type)
Definition: aacdec_usac_mps212.c:91
NULL
#define NULL
Definition: coverity.c:32
ff_aac_hcod2D_ICC_03
const int16_t ff_aac_hcod2D_ICC_03[2][2][15][2]
Definition: aacdec_tab.c:508
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:116
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
AACMPSLosslessData::data
int16_t data[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS]
Definition: aacdec_usac_mps212.h:38
ff_aac_hcod2D_ICC_05
const int16_t ff_aac_hcod2D_ICC_05[2][2][35][2]
Definition: aacdec_tab.c:523
ff_aac_hcod_firstband_ICC
const int16_t ff_aac_hcod_firstband_ICC[7][2]
Definition: aacdec_tab.c:491
pcm_decode
static void pcm_decode(GetBitContext *gb, int16_t *data0, int16_t *data1, int16_t offset, int nb_pcm_data_bands, int nb_quant_steps, int nb_levels)
Definition: aacdec_usac_mps212.c:129
diff_freq_decode
static void diff_freq_decode(const int16_t *diff, int16_t *out, int nb_val)
Definition: aacdec_usac_mps212.c:342
AACMPSDataType
AACMPSDataType
Definition: aacdec_usac_mps212.h:29
MPS_MAX_PARAM_SETS
#define MPS_MAX_PARAM_SETS
Definition: aac.h:39
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
huff_decode
static int huff_decode(GetBitContext *gb, int16_t *data[2], enum AACMPSDataType data_type, int diff_freq[2], int num_val, int *time_pair)
Definition: aacdec_usac_mps212.c:248
diff_time_decode_forwards
static void diff_time_decode_forwards(const int16_t *prev, const int16_t *diff, int16_t *out, const int mixed_diff_type, const int nb_val)
Definition: aacdec_usac_mps212.c:360
AACMPSLosslessData::coarse_quant
bool coarse_quant[MPS_MAX_PARAM_SETS]
Definition: aacdec_usac_mps212.h:42
MPS_CLD
@ MPS_CLD
Definition: aacdec_usac_mps212.h:30
ff_aac_hcod2D_CLD_09
const int16_t ff_aac_hcod2D_CLD_09[2][2][99][2]
Definition: aacdec_tab.c:407
get_freq_strides
static int get_freq_strides(int16_t *freq_strides, int band_stride, int start_band, int end_band)
Definition: aacdec_usac_mps212.c:576
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
AACMPSLosslessData
Definition: aacdec_usac_mps212.h:37
MPS_IPD
@ MPS_IPD
Definition: aacdec_usac_mps212.h:32
ff_aac_hcod1D_ICC
const int16_t ff_aac_hcod1D_ICC[7][2]
Definition: aacdec_tab.c:496
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AACMPSLosslessData::quant_coarse_prev
bool quant_coarse_prev
Definition: aacdec_usac_mps212.h:46
attach_lsb
static void attach_lsb(GetBitContext *gb, int16_t *data_msb, int offset, int nb_lsb, int nb_val, int16_t *data)
Definition: aacdec_usac_mps212.c:370
ff_aac_hcod_firstband_IPD
const int16_t ff_aac_hcod_firstband_IPD[7][2]
Definition: aacdec_tab.c:609
interpolate
static void interpolate(float *out, float v1, float v2, int size)
Definition: twinvq.c:85
create_mapping
static void create_mapping(int map[MPS_MAX_PARAM_BANDS+1], int start_band, int stop_band, int stride)
Definition: aacdec_usac_mps212.c:707
ff_aac_hcod1D_CLD
const int16_t ff_aac_hcod1D_CLD[2][30][2]
Definition: aacdec_tab.c:291
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:58
huff_data_2d
static void huff_data_2d(GetBitContext *gb, int16_t *part0_data[2], int16_t(*data)[2], int data_bands, int stride, enum AACMPSDataType data_type, int diff_freq, int freq_pair)
Definition: aacdec_usac_mps212.c:181
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
len
int len
Definition: vorbis_enc_data.h:426
diff_mode
diff_mode
Definition: vf_paletteuse.c:53
dim
int dim
Definition: vorbis_enc_data.h:425
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
huff_data_1d
static int huff_data_1d(GetBitContext *gb, int16_t *data, int data_bands, enum AACMPSDataType data_type, int diff_freq, int p0_flag)
Definition: aacdec_usac_mps212.c:53
ff_aac_hcod2D_IPD_01
const int16_t ff_aac_hcod2D_IPD_01[2][2][3][2]
Definition: aacdec_tab.c:621
aacdec_tab.h
ff_aac_map_index_data
int ff_aac_map_index_data(AACMPSLosslessData *ld, enum AACMPSDataType data_type, int dst_idx[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS], int default_value, int start_band, int stop_band, int nb_param_sets, const int *param_set_idx, int extend_frame)
Definition: aacdec_usac_mps212.c:786
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
ff_aac_hcod2D_ICC_07
const int16_t ff_aac_hcod2D_ICC_07[2][2][63][2]
Definition: aacdec_tab.c:554
stride
#define stride
Definition: h264pred_template.c:536
diff_time_decode_backwards
static void diff_time_decode_backwards(const int16_t *prev, const int16_t *diff, int16_t *out, const int mixed_diff_type, const int nb_val)
Definition: aacdec_usac_mps212.c:350
xi
#define xi(width, name, var, range_min, range_max, subs,...)
Definition: cbs_h264.c:190
ff_aac_huff_dec_reshape
int ff_aac_huff_dec_reshape(GetBitContext *gb, int16_t *out_data, int nb_val)
Definition: aacdec_usac_mps212.c:686
src
#define src
Definition: vp8dsp.c:248