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