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