FFmpeg
dca_lbr.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 foo86
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 #define BITSTREAM_READER_LE
22 
24 
25 #include "dcadec.h"
26 #include "dcadata.h"
27 #include "dcahuff.h"
28 #include "dca_syncwords.h"
29 #include "bytestream.h"
30 
31 #define AMP_MAX 56
32 
33 enum LBRFlags {
45 };
46 
49  LBR_CHUNK_PAD = 0x01,
52  LBR_CHUNK_LFE = 0x0a,
53  LBR_CHUNK_ECS = 0x0b,
56  LBR_CHUNK_SCF = 0x0e,
78 };
79 
80 typedef struct LBRChunk {
81  int id, len;
82  const uint8_t *data;
83 } LBRChunk;
84 
85 static const int8_t channel_reorder_nolfe[7][5] = {
86  { 0, -1, -1, -1, -1 }, // C
87  { 0, 1, -1, -1, -1 }, // LR
88  { 0, 1, 2, -1, -1 }, // LR C
89  { 0, 1, -1, -1, -1 }, // LsRs
90  { 1, 2, 0, -1, -1 }, // LsRs C
91  { 0, 1, 2, 3, -1 }, // LR LsRs
92  { 0, 1, 3, 4, 2 }, // LR LsRs C
93 };
94 
95 static const int8_t channel_reorder_lfe[7][5] = {
96  { 0, -1, -1, -1, -1 }, // C
97  { 0, 1, -1, -1, -1 }, // LR
98  { 0, 1, 2, -1, -1 }, // LR C
99  { 1, 2, -1, -1, -1 }, // LsRs
100  { 2, 3, 0, -1, -1 }, // LsRs C
101  { 0, 1, 3, 4, -1 }, // LR LsRs
102  { 0, 1, 4, 5, 2 }, // LR LsRs C
103 };
104 
105 static const uint8_t lfe_index[7] = {
106  1, 2, 3, 0, 1, 2, 3
107 };
108 
109 static const uint8_t channel_counts[7] = {
110  1, 2, 3, 2, 3, 4, 5
111 };
112 
113 static const uint16_t channel_layouts[7] = {
121 };
122 
123 static float cos_tab[256];
124 static float lpc_tab[16];
125 
126 static av_cold void init_tables(void)
127 {
128  static int initialized;
129  int i;
130 
131  if (initialized)
132  return;
133 
134  for (i = 0; i < 256; i++)
135  cos_tab[i] = cos(M_PI * i / 128);
136 
137  for (i = 0; i < 16; i++)
138  lpc_tab[i] = sin((i - 8) * (M_PI / ((i < 8) ? 17 : 15)));
139 
140  initialized = 1;
141 }
142 
144 {
145  int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_24) - 1;
146  int i, ps, si, code, step_i;
147  float step, value, delta;
148 
149  ps = get_bits(&s->gb, 24);
150  si = ps >> 23;
151 
152  value = (((ps & 0x7fffff) ^ -si) + si) * (1.0f / 0x7fffff);
153 
154  step_i = get_bits(&s->gb, 8);
155  if (step_i > step_max) {
156  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
157  return -1;
158  }
159 
160  step = ff_dca_lfe_step_size_24[step_i];
161 
162  for (i = 0; i < 64; i++) {
163  code = get_bits(&s->gb, 6);
164 
165  delta = step * 0.03125f;
166  if (code & 16)
167  delta += step;
168  if (code & 8)
169  delta += step * 0.5f;
170  if (code & 4)
171  delta += step * 0.25f;
172  if (code & 2)
173  delta += step * 0.125f;
174  if (code & 1)
175  delta += step * 0.0625f;
176 
177  if (code & 32) {
178  value -= delta;
179  if (value < -3.0f)
180  value = -3.0f;
181  } else {
182  value += delta;
183  if (value > 3.0f)
184  value = 3.0f;
185  }
186 
187  step_i += ff_dca_lfe_delta_index_24[code & 31];
188  step_i = av_clip(step_i, 0, step_max);
189 
190  step = ff_dca_lfe_step_size_24[step_i];
191  s->lfe_data[i] = value * s->lfe_scale;
192  }
193 
194  return 0;
195 }
196 
198 {
199  int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_16) - 1;
200  int i, ps, si, code, step_i;
201  float step, value, delta;
202 
203  ps = get_bits(&s->gb, 16);
204  si = ps >> 15;
205 
206  value = (((ps & 0x7fff) ^ -si) + si) * (1.0f / 0x7fff);
207 
208  step_i = get_bits(&s->gb, 8);
209  if (step_i > step_max) {
210  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
211  return -1;
212  }
213 
214  step = ff_dca_lfe_step_size_16[step_i];
215 
216  for (i = 0; i < 64; i++) {
217  code = get_bits(&s->gb, 4);
218 
219  delta = step * 0.125f;
220  if (code & 4)
221  delta += step;
222  if (code & 2)
223  delta += step * 0.5f;
224  if (code & 1)
225  delta += step * 0.25f;
226 
227  if (code & 8) {
228  value -= delta;
229  if (value < -3.0f)
230  value = -3.0f;
231  } else {
232  value += delta;
233  if (value > 3.0f)
234  value = 3.0f;
235  }
236 
237  step_i += ff_dca_lfe_delta_index_16[code & 7];
238  step_i = av_clip(step_i, 0, step_max);
239 
240  step = ff_dca_lfe_step_size_16[step_i];
241  s->lfe_data[i] = value * s->lfe_scale;
242  }
243 
244  return 0;
245 }
246 
248 {
249  if (!(s->flags & LBR_FLAG_LFE_PRESENT))
250  return 0;
251 
252  if (!chunk->len)
253  return 0;
254 
255  if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
256  return -1;
257 
258  // Determine bit depth from chunk size
259  if (chunk->len >= 52)
260  return parse_lfe_24(s);
261  if (chunk->len >= 35)
262  return parse_lfe_16(s);
263 
264  av_log(s->avctx, AV_LOG_ERROR, "LFE chunk too short\n");
265  return -1;
266 }
267 
268 static inline int parse_vlc(GetBitContext *s, VLC *vlc, int max_depth)
269 {
270  int v = get_vlc2(s, vlc->table, vlc->bits, max_depth);
271  if (v > 0)
272  return v - 1;
273  // Rare value
274  return get_bits(s, get_bits(s, 3) + 1);
275 }
276 
277 static int parse_tonal(DCALbrDecoder *s, int group)
278 {
279  unsigned int amp[DCA_LBR_CHANNELS_TOTAL];
280  unsigned int phs[DCA_LBR_CHANNELS_TOTAL];
281  unsigned int diff, main_amp, shift;
282  int sf, sf_idx, ch, main_ch, freq;
283  int ch_nbits = av_ceil_log2(s->nchannels_total);
284 
285  // Parse subframes for this group
286  for (sf = 0; sf < 1 << group; sf += diff ? 8 : 1) {
287  sf_idx = ((s->framenum << group) + sf) & 31;
288  s->tonal_bounds[group][sf_idx][0] = s->ntones;
289 
290  // Parse tones for this subframe
291  for (freq = 1;; freq++) {
292  if (get_bits_left(&s->gb) < 1) {
293  av_log(s->avctx, AV_LOG_ERROR, "Tonal group chunk too short\n");
294  return -1;
295  }
296 
297  diff = parse_vlc(&s->gb, &ff_dca_vlc_tnl_grp[group], 2);
298  if (diff >= FF_ARRAY_ELEMS(ff_dca_fst_amp)) {
299  av_log(s->avctx, AV_LOG_ERROR, "Invalid tonal frequency diff\n");
300  return -1;
301  }
302 
303  diff = get_bitsz(&s->gb, diff >> 2) + ff_dca_fst_amp[diff];
304  if (diff <= 1)
305  break; // End of subframe
306 
307  freq += diff - 2;
308  if (freq >> (5 - group) > s->nsubbands * 4 - 6) {
309  av_log(s->avctx, AV_LOG_ERROR, "Invalid spectral line offset\n");
310  return -1;
311  }
312 
313  // Main channel
314  main_ch = get_bitsz(&s->gb, ch_nbits);
315  main_amp = parse_vlc(&s->gb, &ff_dca_vlc_tnl_scf, 2)
316  + s->tonal_scf[ff_dca_freq_to_sb[freq >> (7 - group)]]
317  + s->limited_range - 2;
318  amp[main_ch] = main_amp < AMP_MAX ? main_amp : 0;
319  phs[main_ch] = get_bits(&s->gb, 3);
320 
321  // Secondary channels
322  for (ch = 0; ch < s->nchannels_total; ch++) {
323  if (ch == main_ch)
324  continue;
325  if (get_bits1(&s->gb)) {
326  amp[ch] = amp[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_damp, 1);
327  phs[ch] = phs[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_dph, 1);
328  } else {
329  amp[ch] = 0;
330  phs[ch] = 0;
331  }
332  }
333 
334  if (amp[main_ch]) {
335  // Allocate new tone
336  DCALbrTone *t = &s->tones[s->ntones];
337  s->ntones = (s->ntones + 1) & (DCA_LBR_TONES - 1);
338 
339  t->x_freq = freq >> (5 - group);
340  t->f_delt = (freq & ((1 << (5 - group)) - 1)) << group;
341  t->ph_rot = 256 - (t->x_freq & 1) * 128 - t->f_delt * 4;
342 
343  shift = ff_dca_ph0_shift[(t->x_freq & 3) * 2 + (freq & 1)]
344  - ((t->ph_rot << (5 - group)) - t->ph_rot);
345 
346  for (ch = 0; ch < s->nchannels; ch++) {
347  t->amp[ch] = amp[ch] < AMP_MAX ? amp[ch] : 0;
348  t->phs[ch] = 128 - phs[ch] * 32 + shift;
349  }
350  }
351  }
352 
353  s->tonal_bounds[group][sf_idx][1] = s->ntones;
354  }
355 
356  return 0;
357 }
358 
360 {
361  int sb, group;
362 
363  if (!chunk->len)
364  return 0;
365 
366  if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
367  return -1;
368 
369  // Scale factors
370  if (chunk->id == LBR_CHUNK_SCF || chunk->id == LBR_CHUNK_TONAL_SCF) {
371  if (get_bits_left(&s->gb) < 36) {
372  av_log(s->avctx, AV_LOG_ERROR, "Tonal scale factor chunk too short\n");
373  return -1;
374  }
375  for (sb = 0; sb < 6; sb++)
376  s->tonal_scf[sb] = get_bits(&s->gb, 6);
377  }
378 
379  // Tonal groups
380  if (chunk->id == LBR_CHUNK_TONAL || chunk->id == LBR_CHUNK_TONAL_SCF)
381  for (group = 0; group < 5; group++)
382  if (parse_tonal(s, group) < 0)
383  return -1;
384 
385  return 0;
386 }
387 
389 {
390  if (!chunk->len)
391  return 0;
392 
393  if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
394  return -1;
395 
396  return parse_tonal(s, chunk->id);
397 }
398 
399 /**
400  * Check point to ensure that enough bits are left. Aborts decoding
401  * by skipping to the end of chunk otherwise.
402  */
403 static int ensure_bits(GetBitContext *s, int n)
404 {
405  int left = get_bits_left(s);
406  if (left < 0)
407  return -1;
408  if (left < n) {
409  skip_bits_long(s, left);
410  return 1;
411  }
412  return 0;
413 }
414 
416 {
417  int i, sf, prev, next, dist;
418 
419  // Truncated scale factors remain zero
420  if (ensure_bits(&s->gb, 20))
421  return 0;
422 
423  // Initial scale factor
424  prev = parse_vlc(&s->gb, &ff_dca_vlc_fst_rsd_amp, 2);
425 
426  for (sf = 0; sf < 7; sf += dist) {
427  scf[sf] = prev; // Store previous value
428 
429  if (ensure_bits(&s->gb, 20))
430  return 0;
431 
432  // Interpolation distance
433  dist = parse_vlc(&s->gb, &ff_dca_vlc_rsd_apprx, 1) + 1;
434  if (dist > 7 - sf) {
435  av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor distance\n");
436  return -1;
437  }
438 
439  if (ensure_bits(&s->gb, 20))
440  return 0;
441 
442  // Final interpolation point
443  next = parse_vlc(&s->gb, &ff_dca_vlc_rsd_amp, 2);
444 
445  if (next & 1)
446  next = prev + ((next + 1) >> 1);
447  else
448  next = prev - ( next >> 1);
449 
450  // Interpolate
451  switch (dist) {
452  case 2:
453  if (next > prev)
454  scf[sf + 1] = prev + ((next - prev) >> 1);
455  else
456  scf[sf + 1] = prev - ((prev - next) >> 1);
457  break;
458 
459  case 4:
460  if (next > prev) {
461  scf[sf + 1] = prev + ( (next - prev) >> 2);
462  scf[sf + 2] = prev + ( (next - prev) >> 1);
463  scf[sf + 3] = prev + (((next - prev) * 3) >> 2);
464  } else {
465  scf[sf + 1] = prev - ( (prev - next) >> 2);
466  scf[sf + 2] = prev - ( (prev - next) >> 1);
467  scf[sf + 3] = prev - (((prev - next) * 3) >> 2);
468  }
469  break;
470 
471  default:
472  for (i = 1; i < dist; i++)
473  scf[sf + i] = prev + (next - prev) * i / dist;
474  break;
475  }
476 
477  prev = next;
478  }
479 
480  scf[sf] = next; // Store final value
481 
482  return 0;
483 }
484 
485 static int parse_st_code(GetBitContext *s, int min_v)
486 {
487  unsigned int v = parse_vlc(s, &ff_dca_vlc_st_grid, 2) + min_v;
488 
489  if (v & 1)
490  v = 16 + (v >> 1);
491  else
492  v = 16 - (v >> 1);
493 
495  v = 16;
496  return v;
497 }
498 
499 static int parse_grid_1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
500 {
501  int ch, sb, sf, nsubbands;
502 
503  if (!chunk->len)
504  return 0;
505 
506  if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
507  return -1;
508 
509  // Scale factors
510  nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
511  for (sb = 2; sb < nsubbands; sb++) {
512  if (parse_scale_factors(s, s->grid_1_scf[ch1][sb]) < 0)
513  return -1;
514  if (ch1 != ch2 && ff_dca_grid_1_to_scf[sb] < s->min_mono_subband
515  && parse_scale_factors(s, s->grid_1_scf[ch2][sb]) < 0)
516  return -1;
517  }
518 
519  if (get_bits_left(&s->gb) < 1)
520  return 0; // Should not happen, but a sample exists that proves otherwise
521 
522  // Average values for third grid
523  for (sb = 0; sb < s->nsubbands - 4; sb++) {
524  s->grid_3_avg[ch1][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
525  if (ch1 != ch2) {
526  if (sb + 4 < s->min_mono_subband)
527  s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
528  else
529  s->grid_3_avg[ch2][sb] = s->grid_3_avg[ch1][sb];
530  }
531  }
532 
533  if (get_bits_left(&s->gb) < 0) {
534  av_log(s->avctx, AV_LOG_ERROR, "First grid chunk too short\n");
535  return -1;
536  }
537 
538  // Stereo image for partial mono mode
539  if (ch1 != ch2) {
540  int min_v[2];
541 
542  if (ensure_bits(&s->gb, 8))
543  return 0;
544 
545  min_v[0] = get_bits(&s->gb, 4);
546  min_v[1] = get_bits(&s->gb, 4);
547 
548  nsubbands = (s->nsubbands - s->min_mono_subband + 3) / 4;
549  for (sb = 0; sb < nsubbands; sb++)
550  for (ch = ch1; ch <= ch2; ch++)
551  for (sf = 1; sf <= 4; sf++)
552  s->part_stereo[ch][sb][sf] = parse_st_code(&s->gb, min_v[ch - ch1]);
553 
554  if (get_bits_left(&s->gb) >= 0)
555  s->part_stereo_pres |= 1 << ch1;
556  }
557 
558  // Low resolution spatial information is not decoded
559 
560  return 0;
561 }
562 
563 static int parse_grid_1_sec_ch(DCALbrDecoder *s, int ch2)
564 {
565  int sb, nsubbands;
566 
567  // Scale factors
568  nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
569  for (sb = 2; sb < nsubbands; sb++) {
571  && parse_scale_factors(s, s->grid_1_scf[ch2][sb]) < 0)
572  return -1;
573  }
574 
575  // Average values for third grid
576  for (sb = 0; sb < s->nsubbands - 4; sb++) {
577  if (sb + 4 >= s->min_mono_subband) {
578  if (ensure_bits(&s->gb, 20))
579  return 0;
580  s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
581  }
582  }
583 
584  return 0;
585 }
586 
587 static void parse_grid_3(DCALbrDecoder *s, int ch1, int ch2, int sb, int flag)
588 {
589  int i, ch;
590 
591  for (ch = ch1; ch <= ch2; ch++) {
592  if ((ch != ch1 && sb + 4 >= s->min_mono_subband) != flag)
593  continue;
594 
595  if (s->grid_3_pres[ch] & (1U << sb))
596  continue; // Already parsed
597 
598  for (i = 0; i < 8; i++) {
599  if (ensure_bits(&s->gb, 20))
600  return;
601  s->grid_3_scf[ch][sb][i] = parse_vlc(&s->gb, &ff_dca_vlc_grid_3, 2) - 16;
602  }
603 
604  // Flag scale factors for this subband parsed
605  s->grid_3_pres[ch] |= 1U << sb;
606  }
607 }
608 
609 static float lbr_rand(DCALbrDecoder *s, int sb)
610 {
611  s->lbr_rand = 1103515245U * s->lbr_rand + 12345U;
612  return s->lbr_rand * s->sb_scf[sb];
613 }
614 
615 /**
616  * Parse time samples for one subband, filling truncated samples with randomness
617  */
618 static void parse_ch(DCALbrDecoder *s, int ch, int sb, int quant_level, int flag)
619 {
620  float *samples = s->time_samples[ch][sb];
621  int i, j, code, nblocks, coding_method;
622 
623  if (ensure_bits(&s->gb, 20))
624  return; // Too few bits left
625 
626  coding_method = get_bits1(&s->gb);
627 
628  switch (quant_level) {
629  case 1:
630  nblocks = FFMIN(get_bits_left(&s->gb) / 8, DCA_LBR_TIME_SAMPLES / 8);
631  for (i = 0; i < nblocks; i++, samples += 8) {
632  code = get_bits(&s->gb, 8);
633  for (j = 0; j < 8; j++)
634  samples[j] = ff_dca_rsd_level_2a[(code >> j) & 1];
635  }
636  i = nblocks * 8;
637  break;
638 
639  case 2:
640  if (coding_method) {
641  for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 2; i++) {
642  if (get_bits1(&s->gb))
643  samples[i] = ff_dca_rsd_level_2b[get_bits1(&s->gb)];
644  else
645  samples[i] = 0;
646  }
647  } else {
648  nblocks = FFMIN(get_bits_left(&s->gb) / 8, (DCA_LBR_TIME_SAMPLES + 4) / 5);
649  for (i = 0; i < nblocks; i++, samples += 5) {
650  code = ff_dca_rsd_pack_5_in_8[get_bits(&s->gb, 8)];
651  for (j = 0; j < 5; j++)
652  samples[j] = ff_dca_rsd_level_3[(code >> j * 2) & 3];
653  }
654  i = nblocks * 5;
655  }
656  break;
657 
658  case 3:
659  nblocks = FFMIN(get_bits_left(&s->gb) / 7, (DCA_LBR_TIME_SAMPLES + 2) / 3);
660  for (i = 0; i < nblocks; i++, samples += 3) {
661  code = get_bits(&s->gb, 7);
662  for (j = 0; j < 3; j++)
663  samples[j] = ff_dca_rsd_level_5[ff_dca_rsd_pack_3_in_7[code][j]];
664  }
665  i = nblocks * 3;
666  break;
667 
668  case 4:
669  for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 6; i++)
670  samples[i] = ff_dca_rsd_level_8[get_vlc2(&s->gb, ff_dca_vlc_rsd.table, 6, 1)];
671  break;
672 
673  case 5:
674  nblocks = FFMIN(get_bits_left(&s->gb) / 4, DCA_LBR_TIME_SAMPLES);
675  for (i = 0; i < nblocks; i++)
676  samples[i] = ff_dca_rsd_level_16[get_bits(&s->gb, 4)];
677  break;
678 
679  default:
680  av_assert0(0);
681  }
682 
683  if (flag && get_bits_left(&s->gb) < 20)
684  return; // Skip incomplete mono subband
685 
686  for (; i < DCA_LBR_TIME_SAMPLES; i++)
687  s->time_samples[ch][sb][i] = lbr_rand(s, sb);
688 
689  s->ch_pres[ch] |= 1U << sb;
690 }
691 
692 static int parse_ts(DCALbrDecoder *s, int ch1, int ch2,
693  int start_sb, int end_sb, int flag)
694 {
695  int sb, sb_g3, sb_reorder, quant_level;
696 
697  for (sb = start_sb; sb < end_sb; sb++) {
698  // Subband number before reordering
699  if (sb < 6) {
700  sb_reorder = sb;
701  } else if (flag && sb < s->max_mono_subband) {
702  sb_reorder = s->sb_indices[sb];
703  } else {
704  if (ensure_bits(&s->gb, 28))
705  break;
706  sb_reorder = get_bits(&s->gb, s->limited_range + 3);
707  if (sb_reorder < 6)
708  sb_reorder = 6;
709  s->sb_indices[sb] = sb_reorder;
710  }
711  if (sb_reorder >= s->nsubbands)
712  return -1;
713 
714  // Third grid scale factors
715  if (sb == 12) {
716  for (sb_g3 = 0; sb_g3 < s->g3_avg_only_start_sb - 4; sb_g3++)
717  parse_grid_3(s, ch1, ch2, sb_g3, flag);
718  } else if (sb < 12 && sb_reorder >= 4) {
719  parse_grid_3(s, ch1, ch2, sb_reorder - 4, flag);
720  }
721 
722  // Secondary channel flags
723  if (ch1 != ch2) {
724  if (ensure_bits(&s->gb, 20))
725  break;
726  if (!flag || sb_reorder >= s->max_mono_subband)
727  s->sec_ch_sbms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
728  if (flag && sb_reorder >= s->min_mono_subband)
729  s->sec_ch_lrms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
730  }
731 
732  quant_level = s->quant_levels[ch1 / 2][sb];
733  if (!quant_level)
734  return -1;
735 
736  // Time samples for one or both channels
737  if (sb < s->max_mono_subband && sb_reorder >= s->min_mono_subband) {
738  if (!flag)
739  parse_ch(s, ch1, sb_reorder, quant_level, 0);
740  else if (ch1 != ch2)
741  parse_ch(s, ch2, sb_reorder, quant_level, 1);
742  } else {
743  parse_ch(s, ch1, sb_reorder, quant_level, 0);
744  if (ch1 != ch2)
745  parse_ch(s, ch2, sb_reorder, quant_level, 0);
746  }
747  }
748 
749  return 0;
750 }
751 
752 /**
753  * Convert from reflection coefficients to direct form coefficients
754  */
755 static void convert_lpc(float *coeff, const int *codes)
756 {
757  int i, j;
758 
759  for (i = 0; i < 8; i++) {
760  float rc = lpc_tab[codes[i]];
761  for (j = 0; j < (i + 1) / 2; j++) {
762  float tmp1 = coeff[ j ];
763  float tmp2 = coeff[i - j - 1];
764  coeff[ j ] = tmp1 + rc * tmp2;
765  coeff[i - j - 1] = tmp2 + rc * tmp1;
766  }
767  coeff[i] = rc;
768  }
769 }
770 
771 static int parse_lpc(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb)
772 {
773  int f = s->framenum & 1;
774  int i, sb, ch, codes[16];
775 
776  // First two subbands have two sets of coefficients, third subband has one
777  for (sb = start_sb; sb < end_sb; sb++) {
778  int ncodes = 8 * (1 + (sb < 2));
779  for (ch = ch1; ch <= ch2; ch++) {
780  if (ensure_bits(&s->gb, 4 * ncodes))
781  return 0;
782  for (i = 0; i < ncodes; i++)
783  codes[i] = get_bits(&s->gb, 4);
784  for (i = 0; i < ncodes / 8; i++)
785  convert_lpc(s->lpc_coeff[f][ch][sb][i], &codes[i * 8]);
786  }
787  }
788 
789  return 0;
790 }
791 
792 static int parse_high_res_grid(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
793 {
794  int quant_levels[DCA_LBR_SUBBANDS];
795  int sb, ch, ol, st, max_sb, profile;
796 
797  if (!chunk->len)
798  return 0;
799 
800  if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
801  return -1;
802 
803  // Quantizer profile
804  profile = get_bits(&s->gb, 8);
805  // Overall level
806  ol = (profile >> 3) & 7;
807  // Steepness
808  st = profile >> 6;
809  // Max energy subband
810  max_sb = profile & 7;
811 
812  // Calculate quantization levels
813  for (sb = 0; sb < s->nsubbands; sb++) {
814  int f = sb * s->limited_rate / s->nsubbands;
815  int a = 18000 / (12 * f / 1000 + 100 + 40 * st) + 20 * ol;
816  if (a <= 95)
817  quant_levels[sb] = 1;
818  else if (a <= 140)
819  quant_levels[sb] = 2;
820  else if (a <= 180)
821  quant_levels[sb] = 3;
822  else if (a <= 230)
823  quant_levels[sb] = 4;
824  else
825  quant_levels[sb] = 5;
826  }
827 
828  // Reorder quantization levels for lower subbands
829  for (sb = 0; sb < 8; sb++)
830  s->quant_levels[ch1 / 2][sb] = quant_levels[ff_dca_sb_reorder[max_sb][sb]];
831  for (; sb < s->nsubbands; sb++)
832  s->quant_levels[ch1 / 2][sb] = quant_levels[sb];
833 
834  // LPC for the first two subbands
835  if (parse_lpc(s, ch1, ch2, 0, 2) < 0)
836  return -1;
837 
838  // Time-samples for the first two subbands of main channel
839  if (parse_ts(s, ch1, ch2, 0, 2, 0) < 0)
840  return -1;
841 
842  // First two bands of the first grid
843  for (sb = 0; sb < 2; sb++)
844  for (ch = ch1; ch <= ch2; ch++)
845  if (parse_scale_factors(s, s->grid_1_scf[ch][sb]) < 0)
846  return -1;
847 
848  return 0;
849 }
850 
851 static int parse_grid_2(DCALbrDecoder *s, int ch1, int ch2,
852  int start_sb, int end_sb, int flag)
853 {
854  int i, j, sb, ch, nsubbands;
855 
856  nsubbands = ff_dca_scf_to_grid_2[s->nsubbands - 1] + 1;
857  if (end_sb > nsubbands)
858  end_sb = nsubbands;
859 
860  for (sb = start_sb; sb < end_sb; sb++) {
861  for (ch = ch1; ch <= ch2; ch++) {
862  uint8_t *g2_scf = s->grid_2_scf[ch][sb];
863 
864  if ((ch != ch1 && ff_dca_grid_2_to_scf[sb] >= s->min_mono_subband) != flag) {
865  if (!flag)
866  memcpy(g2_scf, s->grid_2_scf[ch1][sb], 64);
867  continue;
868  }
869 
870  // Scale factors in groups of 8
871  for (i = 0; i < 8; i++, g2_scf += 8) {
872  if (get_bits_left(&s->gb) < 1) {
873  memset(g2_scf, 0, 64 - i * 8);
874  break;
875  }
876  // Bit indicating if whole group has zero values
877  if (get_bits1(&s->gb)) {
878  for (j = 0; j < 8; j++) {
879  if (ensure_bits(&s->gb, 20))
880  break;
881  g2_scf[j] = parse_vlc(&s->gb, &ff_dca_vlc_grid_2, 2);
882  }
883  } else {
884  memset(g2_scf, 0, 8);
885  }
886  }
887  }
888  }
889 
890  return 0;
891 }
892 
893 static int parse_ts1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
894 {
895  if (!chunk->len)
896  return 0;
897  if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
898  return -1;
899  if (parse_lpc(s, ch1, ch2, 2, 3) < 0)
900  return -1;
901  if (parse_ts(s, ch1, ch2, 2, 4, 0) < 0)
902  return -1;
903  if (parse_grid_2(s, ch1, ch2, 0, 1, 0) < 0)
904  return -1;
905  if (parse_ts(s, ch1, ch2, 4, 6, 0) < 0)
906  return -1;
907  return 0;
908 }
909 
910 static int parse_ts2_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
911 {
912  if (!chunk->len)
913  return 0;
914  if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
915  return -1;
916  if (parse_grid_2(s, ch1, ch2, 1, 3, 0) < 0)
917  return -1;
918  if (parse_ts(s, ch1, ch2, 6, s->max_mono_subband, 0) < 0)
919  return -1;
920  if (ch1 != ch2) {
921  if (parse_grid_1_sec_ch(s, ch2) < 0)
922  return -1;
923  if (parse_grid_2(s, ch1, ch2, 0, 3, 1) < 0)
924  return -1;
925  }
926  if (parse_ts(s, ch1, ch2, s->min_mono_subband, s->nsubbands, 1) < 0)
927  return -1;
928  return 0;
929 }
930 
932 {
933  double scale = (-1.0 / (1 << 17)) * sqrt(1 << (2 - s->limited_range));
934  int i, br_per_ch = s->bit_rate_scaled / s->nchannels_total;
935 
936  ff_mdct_end(&s->imdct);
937 
938  if (ff_mdct_init(&s->imdct, s->freq_range + 6, 1, scale) < 0)
939  return -1;
940 
941  for (i = 0; i < 32 << s->freq_range; i++)
942  s->window[i] = ff_dca_long_window[i << (2 - s->freq_range)];
943 
944  if (br_per_ch < 14000)
945  scale = 0.85;
946  else if (br_per_ch < 32000)
947  scale = (br_per_ch - 14000) * (1.0 / 120000) + 0.85;
948  else
949  scale = 1.0;
950 
951  scale *= 1.0 / INT_MAX;
952 
953  for (i = 0; i < s->nsubbands; i++) {
954  if (i < 2)
955  s->sb_scf[i] = 0; // The first two subbands are always zero
956  else if (i < 5)
957  s->sb_scf[i] = (i - 1) * 0.25 * 0.785 * scale;
958  else
959  s->sb_scf[i] = 0.785 * scale;
960  }
961 
962  s->lfe_scale = (16 << s->freq_range) * 0.0000078265894;
963 
964  return 0;
965 }
966 
968 {
969  // Reserve space for history and padding
970  int nchsamples = DCA_LBR_TIME_SAMPLES + DCA_LBR_TIME_HISTORY * 2;
971  int nsamples = nchsamples * s->nchannels * s->nsubbands;
972  int ch, sb;
973  float *ptr;
974 
975  // Reallocate time sample buffer
976  av_fast_mallocz(&s->ts_buffer, &s->ts_size, nsamples * sizeof(float));
977  if (!s->ts_buffer)
978  return -1;
979 
980  ptr = s->ts_buffer + DCA_LBR_TIME_HISTORY;
981  for (ch = 0; ch < s->nchannels; ch++) {
982  for (sb = 0; sb < s->nsubbands; sb++) {
983  s->time_samples[ch][sb] = ptr;
984  ptr += nchsamples;
985  }
986  }
987 
988  return 0;
989 }
990 
992 {
993  int old_rate = s->sample_rate;
994  int old_band_limit = s->band_limit;
995  int old_nchannels = s->nchannels;
996  int version, bit_rate_hi;
997  unsigned int sr_code;
998 
999  // Sample rate of LBR audio
1000  sr_code = bytestream2_get_byte(gb);
1001  if (sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs)) {
1002  av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sample rate\n");
1003  return AVERROR_INVALIDDATA;
1004  }
1005  s->sample_rate = ff_dca_sampling_freqs[sr_code];
1006  if (s->sample_rate > 48000) {
1007  avpriv_report_missing_feature(s->avctx, "%d Hz LBR sample rate", s->sample_rate);
1008  return AVERROR_PATCHWELCOME;
1009  }
1010 
1011  // LBR speaker mask
1012  s->ch_mask = bytestream2_get_le16(gb);
1013  if (!(s->ch_mask & 0x7)) {
1014  avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1015  return AVERROR_PATCHWELCOME;
1016  }
1017  if ((s->ch_mask & 0xfff0) && !(s->warned & 1)) {
1018  avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1019  s->warned |= 1;
1020  }
1021 
1022  // LBR bitstream version
1023  version = bytestream2_get_le16(gb);
1024  if ((version & 0xff00) != 0x0800) {
1025  avpriv_report_missing_feature(s->avctx, "LBR stream version %#x", version);
1026  return AVERROR_PATCHWELCOME;
1027  }
1028 
1029  // Flags for LBR decoder initialization
1030  s->flags = bytestream2_get_byte(gb);
1031  if (s->flags & LBR_FLAG_DMIX_MULTI_CH) {
1032  avpriv_report_missing_feature(s->avctx, "LBR multi-channel downmix");
1033  return AVERROR_PATCHWELCOME;
1034  }
1035  if ((s->flags & LBR_FLAG_LFE_PRESENT) && s->sample_rate != 48000) {
1036  if (!(s->warned & 2)) {
1037  avpriv_report_missing_feature(s->avctx, "%d Hz LFE interpolation", s->sample_rate);
1038  s->warned |= 2;
1039  }
1040  s->flags &= ~LBR_FLAG_LFE_PRESENT;
1041  }
1042 
1043  // Most significant bit rate nibbles
1044  bit_rate_hi = bytestream2_get_byte(gb);
1045 
1046  // Least significant original bit rate word
1047  s->bit_rate_orig = bytestream2_get_le16(gb) | ((bit_rate_hi & 0x0F) << 16);
1048 
1049  // Least significant scaled bit rate word
1050  s->bit_rate_scaled = bytestream2_get_le16(gb) | ((bit_rate_hi & 0xF0) << 12);
1051 
1052  // Setup number of fullband channels
1055 
1056  // Setup band limit
1057  switch (s->flags & LBR_FLAG_BAND_LIMIT_MASK) {
1059  s->band_limit = 0;
1060  break;
1062  s->band_limit = 1;
1063  break;
1065  s->band_limit = 2;
1066  break;
1067  default:
1068  avpriv_report_missing_feature(s->avctx, "LBR band limit %#x", s->flags & LBR_FLAG_BAND_LIMIT_MASK);
1069  return AVERROR_PATCHWELCOME;
1070  }
1071 
1072  // Setup frequency range
1073  s->freq_range = ff_dca_freq_ranges[sr_code];
1074 
1075  // Setup resolution profile
1076  if (s->bit_rate_orig >= 44000 * (s->nchannels_total + 2))
1077  s->res_profile = 2;
1078  else if (s->bit_rate_orig >= 25000 * (s->nchannels_total + 2))
1079  s->res_profile = 1;
1080  else
1081  s->res_profile = 0;
1082 
1083  // Setup limited sample rate, number of subbands, etc
1084  s->limited_rate = s->sample_rate >> s->band_limit;
1085  s->limited_range = s->freq_range - s->band_limit;
1086  if (s->limited_range < 0) {
1087  av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR band limit for frequency range\n");
1088  return AVERROR_INVALIDDATA;
1089  }
1090 
1091  s->nsubbands = 8 << s->limited_range;
1092 
1094  if (s->g3_avg_only_start_sb > s->nsubbands)
1096 
1097  s->min_mono_subband = s->nsubbands * 2000 / (s->limited_rate / 2);
1098  if (s->min_mono_subband > s->nsubbands)
1099  s->min_mono_subband = s->nsubbands;
1100 
1101  s->max_mono_subband = s->nsubbands * 14000 / (s->limited_rate / 2);
1102  if (s->max_mono_subband > s->nsubbands)
1103  s->max_mono_subband = s->nsubbands;
1104 
1105  // Handle change of sample rate
1106  if ((old_rate != s->sample_rate || old_band_limit != s->band_limit) && init_sample_rate(s) < 0)
1107  return AVERROR(ENOMEM);
1108 
1109  // Setup stereo downmix
1110  if (s->flags & LBR_FLAG_DMIX_STEREO) {
1111  DCAContext *dca = s->avctx->priv_data;
1112 
1113  if (s->nchannels_total < 3 || s->nchannels_total > DCA_LBR_CHANNELS_TOTAL - 2) {
1114  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels for LBR stereo downmix\n");
1115  return AVERROR_INVALIDDATA;
1116  }
1117 
1118  // This decoder doesn't support ECS chunk
1119  if (dca->request_channel_layout != DCA_SPEAKER_LAYOUT_STEREO && !(s->warned & 4)) {
1120  avpriv_report_missing_feature(s->avctx, "Embedded LBR stereo downmix");
1121  s->warned |= 4;
1122  }
1123 
1124  // Account for extra downmixed channel pair
1125  s->nchannels_total += 2;
1126  s->nchannels = 2;
1128  s->flags &= ~LBR_FLAG_LFE_PRESENT;
1129  }
1130 
1131  // Handle change of sample rate or number of channels
1132  if (old_rate != s->sample_rate
1133  || old_band_limit != s->band_limit
1134  || old_nchannels != s->nchannels) {
1135  if (alloc_sample_buffer(s) < 0)
1136  return AVERROR(ENOMEM);
1137  ff_dca_lbr_flush(s);
1138  }
1139 
1140  return 0;
1141 }
1142 
1144 {
1145  struct {
1146  LBRChunk lfe;
1147  LBRChunk tonal;
1148  LBRChunk tonal_grp[5];
1149  LBRChunk grid1[DCA_LBR_CHANNELS / 2];
1150  LBRChunk hr_grid[DCA_LBR_CHANNELS / 2];
1151  LBRChunk ts1[DCA_LBR_CHANNELS / 2];
1152  LBRChunk ts2[DCA_LBR_CHANNELS / 2];
1153  } chunk = { {0} };
1154 
1155  GetByteContext gb;
1156 
1157  int i, ch, sb, sf, ret, group, chunk_id, chunk_len;
1158 
1159  bytestream2_init(&gb, data + asset->lbr_offset, asset->lbr_size);
1160 
1161  // LBR sync word
1162  if (bytestream2_get_be32(&gb) != DCA_SYNCWORD_LBR) {
1163  av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sync word\n");
1164  return AVERROR_INVALIDDATA;
1165  }
1166 
1167  // LBR header type
1168  switch (bytestream2_get_byte(&gb)) {
1170  if (!s->sample_rate) {
1171  av_log(s->avctx, AV_LOG_ERROR, "LBR decoder not initialized\n");
1172  return AVERROR_INVALIDDATA;
1173  }
1174  break;
1176  if ((ret = parse_decoder_init(s, &gb)) < 0) {
1177  s->sample_rate = 0;
1178  return ret;
1179  }
1180  break;
1181  default:
1182  av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR header type\n");
1183  return AVERROR_INVALIDDATA;
1184  }
1185 
1186  // LBR frame chunk header
1187  chunk_id = bytestream2_get_byte(&gb);
1188  chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1189 
1190  if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1191  chunk_len = bytestream2_get_bytes_left(&gb);
1192  av_log(s->avctx, AV_LOG_WARNING, "LBR frame chunk was truncated\n");
1194  return AVERROR_INVALIDDATA;
1195  }
1196 
1197  bytestream2_init(&gb, gb.buffer, chunk_len);
1198 
1199  switch (chunk_id & 0x7f) {
1200  case LBR_CHUNK_FRAME:
1202  int checksum = bytestream2_get_be16(&gb);
1203  uint16_t res = chunk_id;
1204  res += (chunk_len >> 8) & 0xff;
1205  res += chunk_len & 0xff;
1206  for (i = 0; i < chunk_len - 2; i++)
1207  res += gb.buffer[i];
1208  if (checksum != res) {
1209  av_log(s->avctx, AV_LOG_WARNING, "Invalid LBR checksum\n");
1211  return AVERROR_INVALIDDATA;
1212  }
1213  } else {
1214  bytestream2_skip(&gb, 2);
1215  }
1216  break;
1218  break;
1219  default:
1220  av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR frame chunk ID\n");
1221  return AVERROR_INVALIDDATA;
1222  }
1223 
1224  // Clear current frame
1225  memset(s->quant_levels, 0, sizeof(s->quant_levels));
1226  memset(s->sb_indices, 0xff, sizeof(s->sb_indices));
1227  memset(s->sec_ch_sbms, 0, sizeof(s->sec_ch_sbms));
1228  memset(s->sec_ch_lrms, 0, sizeof(s->sec_ch_lrms));
1229  memset(s->ch_pres, 0, sizeof(s->ch_pres));
1230  memset(s->grid_1_scf, 0, sizeof(s->grid_1_scf));
1231  memset(s->grid_2_scf, 0, sizeof(s->grid_2_scf));
1232  memset(s->grid_3_avg, 0, sizeof(s->grid_3_avg));
1233  memset(s->grid_3_scf, 0, sizeof(s->grid_3_scf));
1234  memset(s->grid_3_pres, 0, sizeof(s->grid_3_pres));
1235  memset(s->tonal_scf, 0, sizeof(s->tonal_scf));
1236  memset(s->lfe_data, 0, sizeof(s->lfe_data));
1237  s->part_stereo_pres = 0;
1238  s->framenum = (s->framenum + 1) & 31;
1239 
1240  for (ch = 0; ch < s->nchannels; ch++) {
1241  for (sb = 0; sb < s->nsubbands / 4; sb++) {
1242  s->part_stereo[ch][sb][0] = s->part_stereo[ch][sb][4];
1243  s->part_stereo[ch][sb][4] = 16;
1244  }
1245  }
1246 
1247  memset(s->lpc_coeff[s->framenum & 1], 0, sizeof(s->lpc_coeff[0]));
1248 
1249  for (group = 0; group < 5; group++) {
1250  for (sf = 0; sf < 1 << group; sf++) {
1251  int sf_idx = ((s->framenum << group) + sf) & 31;
1252  s->tonal_bounds[group][sf_idx][0] =
1253  s->tonal_bounds[group][sf_idx][1] = s->ntones;
1254  }
1255  }
1256 
1257  // Parse chunk headers
1258  while (bytestream2_get_bytes_left(&gb) > 0) {
1259  chunk_id = bytestream2_get_byte(&gb);
1260  chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1261  chunk_id &= 0x7f;
1262 
1263  if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1264  chunk_len = bytestream2_get_bytes_left(&gb);
1265  av_log(s->avctx, AV_LOG_WARNING, "LBR chunk %#x was truncated\n", chunk_id);
1267  return AVERROR_INVALIDDATA;
1268  }
1269 
1270  switch (chunk_id) {
1271  case LBR_CHUNK_LFE:
1272  chunk.lfe.len = chunk_len;
1273  chunk.lfe.data = gb.buffer;
1274  break;
1275 
1276  case LBR_CHUNK_SCF:
1277  case LBR_CHUNK_TONAL:
1278  case LBR_CHUNK_TONAL_SCF:
1279  chunk.tonal.id = chunk_id;
1280  chunk.tonal.len = chunk_len;
1281  chunk.tonal.data = gb.buffer;
1282  break;
1283 
1284  case LBR_CHUNK_TONAL_GRP_1:
1285  case LBR_CHUNK_TONAL_GRP_2:
1286  case LBR_CHUNK_TONAL_GRP_3:
1287  case LBR_CHUNK_TONAL_GRP_4:
1288  case LBR_CHUNK_TONAL_GRP_5:
1289  i = LBR_CHUNK_TONAL_GRP_5 - chunk_id;
1290  chunk.tonal_grp[i].id = i;
1291  chunk.tonal_grp[i].len = chunk_len;
1292  chunk.tonal_grp[i].data = gb.buffer;
1293  break;
1294 
1300  i = LBR_CHUNK_TONAL_SCF_GRP_5 - chunk_id;
1301  chunk.tonal_grp[i].id = i;
1302  chunk.tonal_grp[i].len = chunk_len;
1303  chunk.tonal_grp[i].data = gb.buffer;
1304  break;
1305 
1306  case LBR_CHUNK_RES_GRID_LR:
1307  case LBR_CHUNK_RES_GRID_LR + 1:
1308  case LBR_CHUNK_RES_GRID_LR + 2:
1309  i = chunk_id - LBR_CHUNK_RES_GRID_LR;
1310  chunk.grid1[i].len = chunk_len;
1311  chunk.grid1[i].data = gb.buffer;
1312  break;
1313 
1314  case LBR_CHUNK_RES_GRID_HR:
1315  case LBR_CHUNK_RES_GRID_HR + 1:
1316  case LBR_CHUNK_RES_GRID_HR + 2:
1317  i = chunk_id - LBR_CHUNK_RES_GRID_HR;
1318  chunk.hr_grid[i].len = chunk_len;
1319  chunk.hr_grid[i].data = gb.buffer;
1320  break;
1321 
1322  case LBR_CHUNK_RES_TS_1:
1323  case LBR_CHUNK_RES_TS_1 + 1:
1324  case LBR_CHUNK_RES_TS_1 + 2:
1325  i = chunk_id - LBR_CHUNK_RES_TS_1;
1326  chunk.ts1[i].len = chunk_len;
1327  chunk.ts1[i].data = gb.buffer;
1328  break;
1329 
1330  case LBR_CHUNK_RES_TS_2:
1331  case LBR_CHUNK_RES_TS_2 + 1:
1332  case LBR_CHUNK_RES_TS_2 + 2:
1333  i = chunk_id - LBR_CHUNK_RES_TS_2;
1334  chunk.ts2[i].len = chunk_len;
1335  chunk.ts2[i].data = gb.buffer;
1336  break;
1337  }
1338 
1339  bytestream2_skip(&gb, chunk_len);
1340  }
1341 
1342  // Parse the chunks
1343  ret = parse_lfe_chunk(s, &chunk.lfe);
1344 
1345  ret |= parse_tonal_chunk(s, &chunk.tonal);
1346 
1347  for (i = 0; i < 5; i++)
1348  ret |= parse_tonal_group(s, &chunk.tonal_grp[i]);
1349 
1350  for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1351  int ch1 = i * 2;
1352  int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1353 
1354  if (parse_grid_1_chunk (s, &chunk.grid1 [i], ch1, ch2) < 0 ||
1355  parse_high_res_grid(s, &chunk.hr_grid[i], ch1, ch2) < 0) {
1356  ret = -1;
1357  continue;
1358  }
1359 
1360  // TS chunks depend on both grids. TS_2 depends on TS_1.
1361  if (!chunk.grid1[i].len || !chunk.hr_grid[i].len || !chunk.ts1[i].len)
1362  continue;
1363 
1364  if (parse_ts1_chunk(s, &chunk.ts1[i], ch1, ch2) < 0 ||
1365  parse_ts2_chunk(s, &chunk.ts2[i], ch1, ch2) < 0) {
1366  ret = -1;
1367  continue;
1368  }
1369  }
1370 
1371  if (ret < 0 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1372  return AVERROR_INVALIDDATA;
1373 
1374  return 0;
1375 }
1376 
1377 /**
1378  * Reconstruct high-frequency resolution grid from first and third grids
1379  */
1380 static void decode_grid(DCALbrDecoder *s, int ch1, int ch2)
1381 {
1382  int i, ch, sb;
1383 
1384  for (ch = ch1; ch <= ch2; ch++) {
1385  for (sb = 0; sb < s->nsubbands; sb++) {
1386  int g1_sb = ff_dca_scf_to_grid_1[sb];
1387 
1388  uint8_t *g1_scf_a = s->grid_1_scf[ch][g1_sb ];
1389  uint8_t *g1_scf_b = s->grid_1_scf[ch][g1_sb + 1];
1390 
1391  int w1 = ff_dca_grid_1_weights[g1_sb ][sb];
1392  int w2 = ff_dca_grid_1_weights[g1_sb + 1][sb];
1393 
1394  uint8_t *hr_scf = s->high_res_scf[ch][sb];
1395 
1396  if (sb < 4) {
1397  for (i = 0; i < 8; i++) {
1398  int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1399  hr_scf[i] = scf >> 7;
1400  }
1401  } else {
1402  int8_t *g3_scf = s->grid_3_scf[ch][sb - 4];
1403  int g3_avg = s->grid_3_avg[ch][sb - 4];
1404 
1405  for (i = 0; i < 8; i++) {
1406  int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1407  hr_scf[i] = (scf >> 7) - g3_avg - g3_scf[i];
1408  }
1409  }
1410  }
1411  }
1412 }
1413 
1414 /**
1415  * Fill unallocated subbands with randomness
1416  */
1417 static void random_ts(DCALbrDecoder *s, int ch1, int ch2)
1418 {
1419  int i, j, k, ch, sb;
1420 
1421  for (ch = ch1; ch <= ch2; ch++) {
1422  for (sb = 0; sb < s->nsubbands; sb++) {
1423  float *samples = s->time_samples[ch][sb];
1424 
1425  if (s->ch_pres[ch] & (1U << sb))
1426  continue; // Skip allocated subband
1427 
1428  if (sb < 2) {
1429  // The first two subbands are always zero
1430  memset(samples, 0, DCA_LBR_TIME_SAMPLES * sizeof(float));
1431  } else if (sb < 10) {
1432  for (i = 0; i < DCA_LBR_TIME_SAMPLES; i++)
1433  samples[i] = lbr_rand(s, sb);
1434  } else {
1435  for (i = 0; i < DCA_LBR_TIME_SAMPLES / 8; i++, samples += 8) {
1436  float accum[8] = { 0 };
1437 
1438  // Modulate by subbands 2-5 in blocks of 8
1439  for (k = 2; k < 6; k++) {
1440  float *other = &s->time_samples[ch][k][i * 8];
1441  for (j = 0; j < 8; j++)
1442  accum[j] += fabs(other[j]);
1443  }
1444 
1445  for (j = 0; j < 8; j++)
1446  samples[j] = (accum[j] * 0.25f + 0.5f) * lbr_rand(s, sb);
1447  }
1448  }
1449  }
1450  }
1451 }
1452 
1453 static void predict(float *samples, const float *coeff, int nsamples)
1454 {
1455  int i, j;
1456 
1457  for (i = 0; i < nsamples; i++) {
1458  float res = 0;
1459  for (j = 0; j < 8; j++)
1460  res += coeff[j] * samples[i - j - 1];
1461  samples[i] -= res;
1462  }
1463 }
1464 
1465 static void synth_lpc(DCALbrDecoder *s, int ch1, int ch2, int sb)
1466 {
1467  int f = s->framenum & 1;
1468  int ch;
1469 
1470  for (ch = ch1; ch <= ch2; ch++) {
1471  float *samples = s->time_samples[ch][sb];
1472 
1473  if (!(s->ch_pres[ch] & (1U << sb)))
1474  continue;
1475 
1476  if (sb < 2) {
1477  predict(samples, s->lpc_coeff[f^1][ch][sb][1], 16);
1478  predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 64);
1479  predict(samples + 80, s->lpc_coeff[f ][ch][sb][1], 48);
1480  } else {
1481  predict(samples, s->lpc_coeff[f^1][ch][sb][0], 16);
1482  predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 112);
1483  }
1484  }
1485 }
1486 
1487 static void filter_ts(DCALbrDecoder *s, int ch1, int ch2)
1488 {
1489  int i, j, sb, ch;
1490 
1491  for (sb = 0; sb < s->nsubbands; sb++) {
1492  // Scale factors
1493  for (ch = ch1; ch <= ch2; ch++) {
1494  float *samples = s->time_samples[ch][sb];
1495  uint8_t *hr_scf = s->high_res_scf[ch][sb];
1496  if (sb < 4) {
1497  for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++, samples += 16) {
1498  unsigned int scf = hr_scf[i];
1499  if (scf > AMP_MAX)
1500  scf = AMP_MAX;
1501  for (j = 0; j < 16; j++)
1502  samples[j] *= ff_dca_quant_amp[scf];
1503  }
1504  } else {
1505  uint8_t *g2_scf = s->grid_2_scf[ch][ff_dca_scf_to_grid_2[sb]];
1506  for (i = 0; i < DCA_LBR_TIME_SAMPLES / 2; i++, samples += 2) {
1507  unsigned int scf = hr_scf[i / 8] - g2_scf[i];
1508  if (scf > AMP_MAX)
1509  scf = AMP_MAX;
1510  samples[0] *= ff_dca_quant_amp[scf];
1511  samples[1] *= ff_dca_quant_amp[scf];
1512  }
1513  }
1514  }
1515 
1516  // Mid-side stereo
1517  if (ch1 != ch2) {
1518  float *samples_l = s->time_samples[ch1][sb];
1519  float *samples_r = s->time_samples[ch2][sb];
1520  int ch2_pres = s->ch_pres[ch2] & (1U << sb);
1521 
1522  for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++) {
1523  int sbms = (s->sec_ch_sbms[ch1 / 2][sb] >> i) & 1;
1524  int lrms = (s->sec_ch_lrms[ch1 / 2][sb] >> i) & 1;
1525 
1526  if (sb >= s->min_mono_subband) {
1527  if (lrms && ch2_pres) {
1528  if (sbms) {
1529  for (j = 0; j < 16; j++) {
1530  float tmp = samples_l[j];
1531  samples_l[j] = samples_r[j];
1532  samples_r[j] = -tmp;
1533  }
1534  } else {
1535  for (j = 0; j < 16; j++) {
1536  float tmp = samples_l[j];
1537  samples_l[j] = samples_r[j];
1538  samples_r[j] = tmp;
1539  }
1540  }
1541  } else if (!ch2_pres) {
1542  if (sbms && (s->part_stereo_pres & (1 << ch1))) {
1543  for (j = 0; j < 16; j++)
1544  samples_r[j] = -samples_l[j];
1545  } else {
1546  for (j = 0; j < 16; j++)
1547  samples_r[j] = samples_l[j];
1548  }
1549  }
1550  } else if (sbms && ch2_pres) {
1551  for (j = 0; j < 16; j++) {
1552  float tmp = samples_l[j];
1553  samples_l[j] = (tmp + samples_r[j]) * 0.5f;
1554  samples_r[j] = (tmp - samples_r[j]) * 0.5f;
1555  }
1556  }
1557 
1558  samples_l += 16;
1559  samples_r += 16;
1560  }
1561  }
1562 
1563  // Inverse prediction
1564  if (sb < 3)
1565  synth_lpc(s, ch1, ch2, sb);
1566  }
1567 }
1568 
1569 /**
1570  * Modulate by interpolated partial stereo coefficients
1571  */
1572 static void decode_part_stereo(DCALbrDecoder *s, int ch1, int ch2)
1573 {
1574  int i, ch, sb, sf;
1575 
1576  for (ch = ch1; ch <= ch2; ch++) {
1577  for (sb = s->min_mono_subband; sb < s->nsubbands; sb++) {
1578  uint8_t *pt_st = s->part_stereo[ch][(sb - s->min_mono_subband) / 4];
1579  float *samples = s->time_samples[ch][sb];
1580 
1581  if (s->ch_pres[ch2] & (1U << sb))
1582  continue;
1583 
1584  for (sf = 1; sf <= 4; sf++, samples += 32) {
1585  float prev = ff_dca_st_coeff[pt_st[sf - 1]];
1586  float next = ff_dca_st_coeff[pt_st[sf ]];
1587 
1588  for (i = 0; i < 32; i++)
1589  samples[i] *= (32 - i) * prev + i * next;
1590  }
1591  }
1592  }
1593 }
1594 
1595 /**
1596  * Synthesise tones in the given group for the given tonal subframe
1597  */
1598 static void synth_tones(DCALbrDecoder *s, int ch, float *values,
1599  int group, int group_sf, int synth_idx)
1600 {
1601  int i, start, count;
1602 
1603  if (synth_idx < 0)
1604  return;
1605 
1606  start = s->tonal_bounds[group][group_sf][0];
1607  count = (s->tonal_bounds[group][group_sf][1] - start) & (DCA_LBR_TONES - 1);
1608 
1609  for (i = 0; i < count; i++) {
1610  DCALbrTone *t = &s->tones[(start + i) & (DCA_LBR_TONES - 1)];
1611 
1612  if (t->amp[ch]) {
1613  float amp = ff_dca_synth_env[synth_idx] * ff_dca_quant_amp[t->amp[ch]];
1614  float c = amp * cos_tab[(t->phs[ch] ) & 255];
1615  float s = amp * cos_tab[(t->phs[ch] + 64) & 255];
1616  const float *cf = ff_dca_corr_cf[t->f_delt];
1617  int x_freq = t->x_freq;
1618 
1619  switch (x_freq) {
1620  case 0:
1621  goto p0;
1622  case 1:
1623  values[3] += cf[0] * -s;
1624  values[2] += cf[1] * c;
1625  values[1] += cf[2] * s;
1626  values[0] += cf[3] * -c;
1627  goto p1;
1628  case 2:
1629  values[2] += cf[0] * -s;
1630  values[1] += cf[1] * c;
1631  values[0] += cf[2] * s;
1632  goto p2;
1633  case 3:
1634  values[1] += cf[0] * -s;
1635  values[0] += cf[1] * c;
1636  goto p3;
1637  case 4:
1638  values[0] += cf[0] * -s;
1639  goto p4;
1640  }
1641 
1642  values[x_freq - 5] += cf[ 0] * -s;
1643  p4: values[x_freq - 4] += cf[ 1] * c;
1644  p3: values[x_freq - 3] += cf[ 2] * s;
1645  p2: values[x_freq - 2] += cf[ 3] * -c;
1646  p1: values[x_freq - 1] += cf[ 4] * -s;
1647  p0: values[x_freq ] += cf[ 5] * c;
1648  values[x_freq + 1] += cf[ 6] * s;
1649  values[x_freq + 2] += cf[ 7] * -c;
1650  values[x_freq + 3] += cf[ 8] * -s;
1651  values[x_freq + 4] += cf[ 9] * c;
1652  values[x_freq + 5] += cf[10] * s;
1653  }
1654 
1655  t->phs[ch] += t->ph_rot;
1656  }
1657 }
1658 
1659 /**
1660  * Synthesise all tones in all groups for the given residual subframe
1661  */
1662 static void base_func_synth(DCALbrDecoder *s, int ch, float *values, int sf)
1663 {
1664  int group;
1665 
1666  // Tonal vs residual shift is 22 subframes
1667  for (group = 0; group < 5; group++) {
1668  int group_sf = (s->framenum << group) + ((sf - 22) >> (5 - group));
1669  int synth_idx = ((((sf - 22) & 31) << group) & 31) + (1 << group) - 1;
1670 
1671  synth_tones(s, ch, values, group, (group_sf - 1) & 31, 30 - synth_idx);
1672  synth_tones(s, ch, values, group, (group_sf ) & 31, synth_idx);
1673  }
1674 }
1675 
1676 static void transform_channel(DCALbrDecoder *s, int ch, float *output)
1677 {
1678  LOCAL_ALIGNED_32(float, values, [DCA_LBR_SUBBANDS ], [4]);
1679  LOCAL_ALIGNED_32(float, result, [DCA_LBR_SUBBANDS * 2], [4]);
1680  int sf, sb, nsubbands = s->nsubbands, noutsubbands = 8 << s->freq_range;
1681 
1682  // Clear inactive subbands
1683  if (nsubbands < noutsubbands)
1684  memset(values[nsubbands], 0, (noutsubbands - nsubbands) * sizeof(values[0]));
1685 
1686  for (sf = 0; sf < DCA_LBR_TIME_SAMPLES / 4; sf++) {
1687  // Hybrid filterbank
1688  s->dcadsp->lbr_bank(values, s->time_samples[ch],
1689  ff_dca_bank_coeff, sf * 4, nsubbands);
1690 
1691  base_func_synth(s, ch, values[0], sf);
1692 
1693  s->imdct.imdct_calc(&s->imdct, result[0], values[0]);
1694 
1695  // Long window and overlap-add
1696  s->fdsp->vector_fmul_add(output, result[0], s->window,
1697  s->history[ch], noutsubbands * 4);
1698  s->fdsp->vector_fmul_reverse(s->history[ch], result[noutsubbands],
1699  s->window, noutsubbands * 4);
1700  output += noutsubbands * 4;
1701  }
1702 
1703  // Update history for LPC and forward MDCT
1704  for (sb = 0; sb < nsubbands; sb++) {
1705  float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1706  memcpy(samples, samples + DCA_LBR_TIME_SAMPLES, DCA_LBR_TIME_HISTORY * sizeof(float));
1707  }
1708 }
1709 
1711 {
1712  AVCodecContext *avctx = s->avctx;
1713  int i, ret, nchannels, ch_conf = (s->ch_mask & 0x7) - 1;
1714  const int8_t *reorder;
1715 
1716  avctx->channel_layout = channel_layouts[ch_conf];
1717  avctx->channels = nchannels = channel_counts[ch_conf];
1718  avctx->sample_rate = s->sample_rate;
1719  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1720  avctx->bits_per_raw_sample = 0;
1722  avctx->bit_rate = s->bit_rate_scaled;
1723 
1724  if (s->flags & LBR_FLAG_LFE_PRESENT) {
1726  avctx->channels++;
1727  reorder = channel_reorder_lfe[ch_conf];
1728  } else {
1729  reorder = channel_reorder_nolfe[ch_conf];
1730  }
1731 
1732  frame->nb_samples = 1024 << s->freq_range;
1733  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1734  return ret;
1735 
1736  // Filter fullband channels
1737  for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1738  int ch1 = i * 2;
1739  int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1740 
1741  decode_grid(s, ch1, ch2);
1742 
1743  random_ts(s, ch1, ch2);
1744 
1745  filter_ts(s, ch1, ch2);
1746 
1747  if (ch1 != ch2 && (s->part_stereo_pres & (1 << ch1)))
1748  decode_part_stereo(s, ch1, ch2);
1749 
1750  if (ch1 < nchannels)
1751  transform_channel(s, ch1, (float *)frame->extended_data[reorder[ch1]]);
1752 
1753  if (ch1 != ch2 && ch2 < nchannels)
1754  transform_channel(s, ch2, (float *)frame->extended_data[reorder[ch2]]);
1755  }
1756 
1757  // Interpolate LFE channel
1758  if (s->flags & LBR_FLAG_LFE_PRESENT) {
1759  s->dcadsp->lfe_iir((float *)frame->extended_data[lfe_index[ch_conf]],
1761  s->lfe_history, 16 << s->freq_range);
1762  }
1763 
1765  return ret;
1766 
1767  return 0;
1768 }
1769 
1771 {
1772  int ch, sb;
1773 
1774  if (!s->sample_rate)
1775  return;
1776 
1777  // Clear history
1778  memset(s->part_stereo, 16, sizeof(s->part_stereo));
1779  memset(s->lpc_coeff, 0, sizeof(s->lpc_coeff));
1780  memset(s->history, 0, sizeof(s->history));
1781  memset(s->tonal_bounds, 0, sizeof(s->tonal_bounds));
1782  memset(s->lfe_history, 0, sizeof(s->lfe_history));
1783  s->framenum = 0;
1784  s->ntones = 0;
1785 
1786  for (ch = 0; ch < s->nchannels; ch++) {
1787  for (sb = 0; sb < s->nsubbands; sb++) {
1788  float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1789  memset(samples, 0, DCA_LBR_TIME_HISTORY * sizeof(float));
1790  }
1791  }
1792 }
1793 
1795 {
1796  init_tables();
1797 
1798  if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
1799  return -1;
1800 
1801  s->lbr_rand = 1;
1802  return 0;
1803 }
1804 
1806 {
1807  s->sample_rate = 0;
1808 
1809  av_freep(&s->ts_buffer);
1810  s->ts_size = 0;
1811 
1812  av_freep(&s->fdsp);
1813  ff_mdct_end(&s->imdct);
1814 }
static void parse_grid_3(DCALbrDecoder *s, int ch1, int ch2, int sb, int flag)
Definition: dca_lbr.c:587
int ntones
Circular buffer head position.
Definition: dca_lbr.h:122
VLC ff_dca_vlc_avg_g3
Definition: dcahuff.c:1257
float, planar
Definition: samplefmt.h:69
static int parse_grid_1_sec_ch(DCALbrDecoder *s, int ch2)
Definition: dca_lbr.c:563
const float ff_dca_rsd_level_2b[2]
Definition: dcadata.c:8930
static int parse_tonal_chunk(DCALbrDecoder *s, LBRChunk *chunk)
Definition: dca_lbr.c:359
float window[DCA_LBR_SUBBANDS *4]
Long window for IMDCT.
Definition: dca_lbr.h:113
static void synth_lpc(DCALbrDecoder *s, int ch1, int ch2, int sb)
Definition: dca_lbr.c:1465
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
const float ff_dca_st_coeff[34]
Definition: dcadata.c:9049
void(* lbr_bank)(float output[32][4], float **input, const float *coeff, ptrdiff_t ofs, ptrdiff_t len)
Definition: dcadsp.h:88
int bit_rate_scaled
Scaled bit rate.
Definition: dca_lbr.h:66
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset)
Definition: dca_lbr.c:1143
uint8_t grid_2_scf[DCA_LBR_CHANNELS][3][64]
Grid 2 scale factors.
Definition: dca_lbr.h:92
uint32_t ch_pres[DCA_LBR_CHANNELS]
Subband allocation flags.
Definition: dca_lbr.h:89
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1618
const int8_t ff_dca_ph0_shift[8]
Definition: dcadata.c:8753
#define AV_CH_LAYOUT_SURROUND
#define DCA_LBR_CHANNELS_TOTAL
Definition: dca_lbr.h:37
int max_mono_subband
Subband index where mono encoding ends.
Definition: dca_lbr.h:78
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
unsigned int ts_size
Time sample buffer size.
Definition: dca_lbr.h:110
const uint8_t ff_dca_scf_to_grid_1[32]
Definition: dcadata.c:8765
static float cos_tab[256]
Definition: dca_lbr.c:123
static int parse_st_code(GetBitContext *s, int min_v)
Definition: dca_lbr.c:485
int nchannels_total
Total number of fullband channels.
Definition: dca_lbr.h:69
int freq_range
Frequency range of LBR audio.
Definition: dca_lbr.h:70
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
const float ff_dca_lfe_step_size_24[144]
Definition: dcadata.c:9133
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
#define FF_PROFILE_DTS_EXPRESS
Definition: avcodec.h:2928
uint8_t x_freq
Spectral line offset.
Definition: dca_lbr.h:50
const uint16_t ff_dca_rsd_pack_5_in_8[256]
Definition: dcadata.c:8856
static int parse_lfe_16(DCALbrDecoder *s)
Definition: dca_lbr.c:197
int version
Definition: avisynth_c.h:858
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2799
#define AV_CH_LAYOUT_STEREO
int bit_rate_orig
Original bit rate.
Definition: dca_lbr.h:65
VLC ff_dca_vlc_grid_3
Definition: dcahuff.c:1260
static int init_sample_rate(DCALbrDecoder *s)
Definition: dca_lbr.c:931
int profile
profile
Definition: avcodec.h:2901
const float ff_dca_quant_amp[57]
Definition: dcadata.c:9031
#define AV_CH_LAYOUT_5POINT0
static int parse_grid_1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
Definition: dca_lbr.c:499
float * ts_buffer
Time sample buffer base.
Definition: dca_lbr.h:109
const uint8_t ff_dca_freq_ranges[16]
Definition: dca.c:46
float sb_scf[DCA_LBR_SUBBANDS]
Subband randomization scale factors.
Definition: dca_lbr.h:105
float lfe_scale
Scale factor of LFE samples before IIR filter.
Definition: dca_lbr.h:117
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
VLC ff_dca_vlc_st_grid
Definition: dcahuff.c:1258
static int ensure_bits(GetBitContext *s, int n)
Check point to ensure that enough bits are left.
Definition: dca_lbr.c:403
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2236
uint8_t
#define av_cold
Definition: attributes.h:82
float delta
static void filter_ts(DCALbrDecoder *s, int ch1, int ch2)
Definition: dca_lbr.c:1487
int ch_mask
LBR speaker mask.
Definition: dca_lbr.h:63
#define f(width, name)
Definition: cbs_vp9.c:255
#define AMP_MAX
Definition: dca_lbr.c:31
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
uint8_t part_stereo_pres
Partial stereo coefficients presence flags.
Definition: dca_lbr.h:101
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
static void random_ts(DCALbrDecoder *s, int ch1, int ch2)
Fill unallocated subbands with randomness.
Definition: dca_lbr.c:1417
#define AV_CH_LOW_FREQUENCY
static void parse_ch(DCALbrDecoder *s, int ch, int sb, int quant_level, int flag)
Parse time samples for one subband, filling truncated samples with randomness.
Definition: dca_lbr.c:618
DCADSPContext * dcadsp
Definition: dca_lbr.h:126
uint8_t phs[DCA_LBR_CHANNELS]
Per-channel phase.
Definition: dca_lbr.h:55
static int parse_lfe_chunk(DCALbrDecoder *s, LBRChunk *chunk)
Definition: dca_lbr.c:247
const uint8_t * buffer
Definition: bytestream.h:34
static void transform_channel(DCALbrDecoder *s, int ch, float *output)
Definition: dca_lbr.c:1676
int nchannels
Number of fullband channels to decode.
Definition: dca_lbr.h:68
#define av_log(a,...)
int sample_rate
Sample rate of LBR audio.
Definition: dca_lbr.h:62
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static float lbr_rand(DCALbrDecoder *s, int sb)
Definition: dca_lbr.c:609
const int8_t ff_dca_lfe_delta_index_16[8]
Definition: dcadata.c:8847
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
DCALbrTone tones[DCA_LBR_TONES]
Circular buffer of tones.
Definition: dca_lbr.h:121
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
uint8_t tonal_scf[6]
Tonal scale factors.
Definition: dca_lbr.h:119
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
uint8_t ph_rot
Phase rotation.
Definition: dca_lbr.h:52
int warned
Flags for warning suppression.
Definition: dca_lbr.h:82
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
const float ff_dca_rsd_level_5[5]
Definition: dcadata.c:8938
static av_cold void init_tables(void)
Definition: dca_lbr.c:126
float lfe_history[5][2]
LFE IIR filter history.
Definition: dca_lbr.h:116
const uint32_t ff_dca_sampling_freqs[16]
Definition: dca.c:41
uint8_t part_stereo[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS/4][5]
Partial stereo coefficients.
Definition: dca_lbr.h:100
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:107
int lbr_offset
Offset to LBR component from start of substream.
Definition: dca_exss.h:59
#define ff_mdct_init
Definition: fft.h:169
uint8_t amp[DCA_LBR_CHANNELS]
Per-channel amplitude.
Definition: dca_lbr.h:54
int id
Definition: dca_lbr.c:81
#define DCA_SPEAKER_LAYOUT_STEREO
Definition: dca.h:123
GLsizei count
Definition: opengl_enc.c:108
static int parse_tonal_group(DCALbrDecoder *s, LBRChunk *chunk)
Definition: dca_lbr.c:388
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2279
uint16_t tonal_bounds[5][32][2]
Per-group per-subframe start/end positions of tones.
Definition: dca_lbr.h:120
#define DCA_LBR_CHANNELS
Definition: dca_lbr.h:36
#define AV_CH_LAYOUT_2_2
static int alloc_sample_buffer(DCALbrDecoder *s)
Definition: dca_lbr.c:967
const float ff_dca_synth_env[32]
Definition: dcadata.c:8953
static void base_func_synth(DCALbrDecoder *s, int ch, float *values, int sf)
Synthesise all tones in all groups for the given residual subframe.
Definition: dca_lbr.c:1662
audio channel layout utility functions
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2697
const uint8_t * data
Definition: dca_lbr.c:82
void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
Allocate and clear a buffer, reusing the given one if large enough.
Definition: mem.c:505
#define FFMIN(a, b)
Definition: common.h:96
const float ff_dca_rsd_level_8[8]
Definition: dcadata.c:8942
const uint8_t ff_dca_scf_to_grid_2[32]
Definition: dcadata.c:8770
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
const uint16_t ff_dca_avg_g3_freqs[3]
Definition: dcadata.c:8732
int lbr_rand
Seed for subband randomization.
Definition: dca_lbr.h:81
const float ff_dca_rsd_level_16[16]
Definition: dcadata.c:8946
uint8_t sec_ch_sbms[DCA_LBR_CHANNELS/2][DCA_LBR_SUBBANDS]
Right channel inversion or mid/side decoding flags.
Definition: dca_lbr.h:87
int8_t grid_3_avg[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS-4]
Grid 3 average values.
Definition: dca_lbr.h:94
static int initialized
int nsubbands
Number of encoded subbands.
Definition: dca_lbr.h:75
int len
Definition: dca_lbr.c:81
av_cold void ff_dca_lbr_flush(DCALbrDecoder *s)
Definition: dca_lbr.c:1770
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
GetBitContext gb
Definition: dca_lbr.h:60
#define DCA_SYNCWORD_LBR
Definition: dca_syncwords.h:30
#define s(width, name)
Definition: cbs_vp9.c:257
av_cold void ff_dca_lbr_close(DCALbrDecoder *s)
Definition: dca_lbr.c:1805
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
#define DCA_LBR_TONES
Definition: dca_lbr.h:39
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2708
int n
Definition: avisynth_c.h:760
static int parse_lpc(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb)
Definition: dca_lbr.c:771
static int parse_ts(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb, int flag)
Definition: dca_lbr.c:692
#define AV_CH_FRONT_CENTER
AVCodecContext * avctx
Definition: dca_lbr.h:59
uint8_t grid_1_scf[DCA_LBR_CHANNELS][12][8]
Grid 1 scale factors.
Definition: dca_lbr.h:91
static volatile int checksum
Definition: adler32.c:30
VLC ff_dca_vlc_grid_2
Definition: dcahuff.c:1259
LBRChunkTypes
Definition: dca_lbr.c:47
const float ff_dca_bank_coeff[10]
Definition: dcadata.c:9184
VLC ff_dca_vlc_tnl_grp[5]
Definition: dcahuff.c:1250
static const uint8_t channel_counts[7]
Definition: dca_lbr.c:109
int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame)
Definition: dca_lbr.c:1710
static void predict(float *samples, const float *coeff, int nsamples)
Definition: dca_lbr.c:1453
#define FF_ARRAY_ELEMS(a)
static int parse_high_res_grid(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
Definition: dca_lbr.c:792
int ff_side_data_update_matrix_encoding(AVFrame *frame, enum AVMatrixEncoding matrix_encoding)
Add or update AV_FRAME_DATA_MATRIXENCODING side data.
Definition: utils.c:134
int bits
Definition: vlc.h:27
if(ret)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int parse_ts2_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
Definition: dca_lbr.c:910
LBRFlags
Definition: dca_lbr.c:33
float lpc_coeff[2][DCA_LBR_CHANNELS][3][2][8]
Predictor coefficients.
Definition: dca_lbr.h:103
static const int8_t channel_reorder_lfe[7][5]
Definition: dca_lbr.c:95
const float ff_dca_corr_cf[32][11]
Definition: dcadata.c:8964
float history[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS *4]
IMDCT history.
Definition: dca_lbr.h:112
const float ff_dca_lfe_iir[5][4]
Definition: dcadata.c:9190
int8_t grid_3_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS-4][8]
Grid 3 scale factors.
Definition: dca_lbr.h:95
int sample_rate
samples per second
Definition: avcodec.h:2228
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
int band_limit
Band limit factor.
Definition: dca_lbr.h:71
main external API structure.
Definition: avcodec.h:1568
static int parse_ts1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
Definition: dca_lbr.c:893
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1968
int lbr_size
Size of LBR component in extension substream.
Definition: dca_exss.h:60
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:206
FFTContext imdct
Definition: dca_lbr.h:124
const uint8_t ff_dca_freq_to_sb[32]
Definition: dcadata.c:8748
#define AV_EF_CAREFUL
consider things that violate the spec, are fast to calculate and have not been seen in the wild as er...
Definition: avcodec.h:2711
static int parse_tonal(DCALbrDecoder *s, int group)
Definition: dca_lbr.c:277
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:113
int res_profile
Resolution profile.
Definition: dca_lbr.h:74
static float lpc_tab[16]
Definition: dca_lbr.c:124
const float ff_dca_long_window[128]
Definition: dcadata.c:9061
static void convert_lpc(float *coeff, const int *codes)
Convert from reflection coefficients to direct form coefficients.
Definition: dca_lbr.c:755
uint8_t sb_indices[DCA_LBR_SUBBANDS]
Subband reordering indices.
Definition: dca_lbr.h:85
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data...
Definition: avcodec.h:2705
VLC ff_dca_vlc_tnl_scf
Definition: dcahuff.c:1251
VLC ff_dca_vlc_rsd_amp
Definition: dcahuff.c:1256
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63)))#define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};static void cpy1(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, len);}static void cpy2(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 2 *len);}static void cpy4(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 4 *len);}static void cpy8(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;}void swri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len){int ch;int off=0;const int os=(out->planar?1:out->ch_count)*out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){int planes=in->planar?in->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){int planes=out->planar?out->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){int planes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
const uint8_t ff_dca_rsd_pack_3_in_7[128][3]
Definition: dcadata.c:8891
mfxU16 profile
Definition: qsvenc.c:44
int limited_range
Band limited frequency range.
Definition: dca_lbr.h:73
static int parse_lfe_24(DCALbrDecoder *s)
Definition: dca_lbr.c:143
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
const float ff_dca_rsd_level_3[3]
Definition: dcadata.c:8934
static int ff_dca_count_chs_for_mask(unsigned int mask)
Return number of individual channels in DCASpeakerPair mask.
Definition: dca.h:159
VLC ff_dca_vlc_dph
Definition: dcahuff.c:1253
#define AV_CH_SIDE_RIGHT
uint8_t f_delt
Difference between original and center frequency.
Definition: dca_lbr.h:51
#define DCA_LBR_TIME_HISTORY
Definition: dca_lbr.h:42
#define LOCAL_ALIGNED_32(t, v,...)
Definition: internal.h:137
int framenum
Lower 5 bits of current frame number.
Definition: dca_lbr.h:80
int limited_rate
Band limited sample rate.
Definition: dca_lbr.h:72
uint8_t sec_ch_lrms[DCA_LBR_CHANNELS/2][DCA_LBR_SUBBANDS]
Flags indicating if left/right channel are swapped.
Definition: dca_lbr.h:88
const uint8_t ff_dca_grid_1_to_scf[11]
Definition: dcadata.c:8757
float lfe_data[64]
Decimated LFE samples.
Definition: dca_lbr.h:115
#define DCA_LBR_TIME_SAMPLES
Definition: dca_lbr.h:41
uint32_t grid_3_pres[DCA_LBR_CHANNELS]
Grid 3 scale factors presence flags.
Definition: dca_lbr.h:96
#define ff_mdct_end
Definition: fft.h:170
#define flag(name)
Definition: cbs_av1.c:553
const float ff_dca_rsd_level_2a[2]
Definition: dcadata.c:8926
const uint16_t ff_dca_fst_amp[44]
Definition: dcadata.c:8734
const float ff_dca_lfe_step_size_16[101]
Definition: dcadata.c:9096
#define DCA_LBR_SUBBANDS
Definition: dca_lbr.h:38
void(* vector_fmul_add)(float *dst, const float *src0, const float *src1, const float *src2, int len)
Calculate the entry wise product of two vectors of floats, add a third vector of floats and store the...
Definition: float_dsp.h:137
int request_channel_layout
Converted from avctx.request_channel_layout.
Definition: dcadec.h:64
static int parse_grid_2(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb, int flag)
Definition: dca_lbr.c:851
static int parse_vlc(GetBitContext *s, VLC *vlc, int max_depth)
Definition: dca_lbr.c:268
float * time_samples[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS]
Time samples.
Definition: dca_lbr.h:107
VLC ff_dca_vlc_fst_rsd_amp
Definition: dcahuff.c:1254
void * priv_data
Definition: avcodec.h:1595
static av_always_inline int diff(const uint32_t a, const uint32_t b)
AVFloatDSPContext * fdsp
Definition: dca_lbr.h:125
int channels
number of audio channels
Definition: avcodec.h:2229
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static int parse_scale_factors(DCALbrDecoder *s, uint8_t *scf)
Definition: dca_lbr.c:415
VLC ff_dca_vlc_rsd_apprx
Definition: dcahuff.c:1255
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
const uint8_t ff_dca_sb_reorder[8][8]
Definition: dcadata.c:8836
static const int8_t channel_reorder_nolfe[7][5]
Definition: dca_lbr.c:85
static void decode_part_stereo(DCALbrDecoder *s, int ch1, int ch2)
Modulate by interpolated partial stereo coefficients.
Definition: dca_lbr.c:1572
and forward the result(frame or status change) to the corresponding input.If nothing is possible
Filter the word “frame” indicates either a video frame or a group of audio samples
int flags
Flags for LBR decoder initialization.
Definition: dca_lbr.h:64
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:766
static void synth_tones(DCALbrDecoder *s, int ch, float *values, int group, int group_sf, int synth_idx)
Synthesise tones in the given group for the given tonal subframe.
Definition: dca_lbr.c:1598
#define M_PI
Definition: mathematics.h:52
av_cold int ff_dca_lbr_init(DCALbrDecoder *s)
Definition: dca_lbr.c:1794
#define AV_CH_SIDE_LEFT
uint8_t quant_levels[DCA_LBR_CHANNELS/2][DCA_LBR_SUBBANDS]
Quantization levels.
Definition: dca_lbr.h:84
const int8_t ff_dca_lfe_delta_index_24[32]
Definition: dcadata.c:8851
static const uint8_t lfe_index[7]
Definition: dca_lbr.c:105
int min_mono_subband
Subband index where mono encoding starts.
Definition: dca_lbr.h:77
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
const uint8_t ff_dca_grid_2_to_scf[3]
Definition: dcadata.c:8761
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:342
#define AV_CH_LAYOUT_MONO
void(* lfe_iir)(float *output, const float *input, const float iir[5][4], float hist[5][2], ptrdiff_t factor)
Definition: dcadsp.h:91
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
int g3_avg_only_start_sb
Subband index where grid 3 scale factors end.
Definition: dca_lbr.h:76
for(j=16;j >0;--j)
static int parse_decoder_init(DCALbrDecoder *s, GetByteContext *gb)
Definition: dca_lbr.c:991
VLC ff_dca_vlc_rsd
Definition: dcahuff.c:1261
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:415
uint8_t high_res_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS][8]
High-frequency resolution scale factors.
Definition: dca_lbr.h:98
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
static void decode_grid(DCALbrDecoder *s, int ch1, int ch2)
Reconstruct high-frequency resolution grid from first and third grids.
Definition: dca_lbr.c:1380
VLC ff_dca_vlc_damp
Definition: dcahuff.c:1252
const uint8_t ff_dca_grid_1_weights[12][32]
Definition: dcadata.c:8775
void(* vector_fmul_reverse)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats, and store the result in a vector of floats...
Definition: float_dsp.h:154
static uint8_t tmp[11]
Definition: aes_ctr.c:26