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