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