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