dcadec.c
Go to the documentation of this file.
1 /*
2  * DCA compatible decoder
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include <math.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 
30 #include "libavutil/common.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/samplefmt.h"
36 #include "avcodec.h"
37 #include "fft.h"
38 #include "get_bits.h"
39 #include "put_bits.h"
40 #include "dcadata.h"
41 #include "dcahuff.h"
42 #include "dca.h"
43 #include "mathops.h"
44 #include "synth_filter.h"
45 #include "dcadsp.h"
46 #include "fmtconvert.h"
47 #include "internal.h"
48 
49 #if ARCH_ARM
50 # include "arm/dca.h"
51 #endif
52 
53 //#define TRACE
54 
55 #define DCA_PRIM_CHANNELS_MAX (7)
56 #define DCA_SUBBANDS (64)
57 #define DCA_ABITS_MAX (32) /* Should be 28 */
58 #define DCA_SUBSUBFRAMES_MAX (4)
59 #define DCA_SUBFRAMES_MAX (16)
60 #define DCA_BLOCKS_MAX (16)
61 #define DCA_LFE_MAX (3)
62 #define DCA_CHSETS_MAX (4)
63 #define DCA_CHSET_CHANS_MAX (8)
64 
65 enum DCAMode {
66  DCA_MONO = 0,
77 };
78 
79 /* these are unconfirmed but should be mostly correct */
84  DCA_EXSS_LFE = 0x0008,
93  DCA_EXSS_LFE2 = 0x1000,
97 };
98 
101  DCA_XXCH_FRONT_LEFT = 0x0000002,
102  DCA_XXCH_FRONT_RIGHT = 0x0000004,
105  DCA_XXCH_LFE1 = 0x0000020,
106  DCA_XXCH_REAR_CENTER = 0x0000040,
116  DCA_XXCH_LFE2 = 0x0010000,
119  DCA_XXCH_OVERHEAD = 0x0080000,
128 };
129 
130 static const uint32_t map_xxch_to_native[28] = {
140  AV_CH_SIDE_LEFT, /* side surround left -- dup sur side L */
141  AV_CH_SIDE_RIGHT, /* side surround right -- dup sur side R */
147  AV_CH_LOW_FREQUENCY, /* lfe2 -- duplicate lfe1 position */
148  AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
149  AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
150  AV_CH_TOP_CENTER, /* overhead */
151  AV_CH_TOP_FRONT_LEFT, /* side high left -- dup */
152  AV_CH_TOP_FRONT_RIGHT, /* side high right -- dup */
156  AV_CH_BACK_CENTER, /* rear low center -- dup */
157  AV_CH_BACK_LEFT, /* rear low left -- dup */
158  AV_CH_BACK_RIGHT /* read low right -- dup */
159 };
160 
162  DCA_EXT_CORE = 0x001, ///< core in core substream
163  DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
164  DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
165  DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
166  DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
167  DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
168  DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
169  DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
170  DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
171  DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
172 };
173 
174 /* -1 are reserved or unknown */
175 static const int dca_ext_audio_descr_mask[] = {
176  DCA_EXT_XCH,
177  -1,
178  DCA_EXT_X96,
180  -1,
181  -1,
182  DCA_EXT_XXCH,
183  -1,
184 };
185 
186 /* extensions that reside in core substream */
187 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
188 
189 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
190  * Some compromises have been made for special configurations. Most configurations
191  * are never used so complete accuracy is not needed.
192  *
193  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
194  * S -> side, when both rear and back are configured move one of them to the side channel
195  * OV -> center back
196  * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
197  */
198 static const uint64_t dca_core_channel_layout[] = {
199  AV_CH_FRONT_CENTER, ///< 1, A
200  AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
201  AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
202  AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
203  AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
204  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
205  AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
206  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
207  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
208 
209  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
210  AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
211 
212  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
213  AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
214 
215  AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
216  AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
217 
220  AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
221 
223  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
224  AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
225 
227  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
228  AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
229 
231  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
232  AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
233 };
234 
235 static const int8_t dca_lfe_index[] = {
236  1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
237 };
238 
239 static const int8_t dca_channel_reorder_lfe[][9] = {
240  { 0, -1, -1, -1, -1, -1, -1, -1, -1},
241  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
242  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
243  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
244  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
245  { 2, 0, 1, -1, -1, -1, -1, -1, -1},
246  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
247  { 2, 0, 1, 4, -1, -1, -1, -1, -1},
248  { 0, 1, 3, 4, -1, -1, -1, -1, -1},
249  { 2, 0, 1, 4, 5, -1, -1, -1, -1},
250  { 3, 4, 0, 1, 5, 6, -1, -1, -1},
251  { 2, 0, 1, 4, 5, 6, -1, -1, -1},
252  { 0, 6, 4, 5, 2, 3, -1, -1, -1},
253  { 4, 2, 5, 0, 1, 6, 7, -1, -1},
254  { 5, 6, 0, 1, 7, 3, 8, 4, -1},
255  { 4, 2, 5, 0, 1, 6, 8, 7, -1},
256 };
257 
258 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
259  { 0, 2, -1, -1, -1, -1, -1, -1, -1},
260  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
261  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
262  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
263  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
264  { 2, 0, 1, 4, -1, -1, -1, -1, -1},
265  { 0, 1, 3, 4, -1, -1, -1, -1, -1},
266  { 2, 0, 1, 4, 5, -1, -1, -1, -1},
267  { 0, 1, 4, 5, 3, -1, -1, -1, -1},
268  { 2, 0, 1, 5, 6, 4, -1, -1, -1},
269  { 3, 4, 0, 1, 6, 7, 5, -1, -1},
270  { 2, 0, 1, 4, 5, 6, 7, -1, -1},
271  { 0, 6, 4, 5, 2, 3, 7, -1, -1},
272  { 4, 2, 5, 0, 1, 7, 8, 6, -1},
273  { 5, 6, 0, 1, 8, 3, 9, 4, 7},
274  { 4, 2, 5, 0, 1, 6, 9, 8, 7},
275 };
276 
277 static const int8_t dca_channel_reorder_nolfe[][9] = {
278  { 0, -1, -1, -1, -1, -1, -1, -1, -1},
279  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
280  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
281  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
282  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
283  { 2, 0, 1, -1, -1, -1, -1, -1, -1},
284  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
285  { 2, 0, 1, 3, -1, -1, -1, -1, -1},
286  { 0, 1, 2, 3, -1, -1, -1, -1, -1},
287  { 2, 0, 1, 3, 4, -1, -1, -1, -1},
288  { 2, 3, 0, 1, 4, 5, -1, -1, -1},
289  { 2, 0, 1, 3, 4, 5, -1, -1, -1},
290  { 0, 5, 3, 4, 1, 2, -1, -1, -1},
291  { 3, 2, 4, 0, 1, 5, 6, -1, -1},
292  { 4, 5, 0, 1, 6, 2, 7, 3, -1},
293  { 3, 2, 4, 0, 1, 5, 7, 6, -1},
294 };
295 
296 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
297  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
298  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
299  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
300  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
301  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
302  { 2, 0, 1, 3, -1, -1, -1, -1, -1},
303  { 0, 1, 2, 3, -1, -1, -1, -1, -1},
304  { 2, 0, 1, 3, 4, -1, -1, -1, -1},
305  { 0, 1, 3, 4, 2, -1, -1, -1, -1},
306  { 2, 0, 1, 4, 5, 3, -1, -1, -1},
307  { 2, 3, 0, 1, 5, 6, 4, -1, -1},
308  { 2, 0, 1, 3, 4, 5, 6, -1, -1},
309  { 0, 5, 3, 4, 1, 2, 6, -1, -1},
310  { 3, 2, 4, 0, 1, 6, 7, 5, -1},
311  { 4, 5, 0, 1, 7, 2, 8, 3, 6},
312  { 3, 2, 4, 0, 1, 5, 8, 7, 6},
313 };
314 
315 #define DCA_DOLBY 101 /* FIXME */
316 
317 #define DCA_CHANNEL_BITS 6
318 #define DCA_CHANNEL_MASK 0x3F
319 
320 #define DCA_LFE 0x80
321 
322 #define HEADER_SIZE 14
323 
324 #define DCA_MAX_FRAME_SIZE 16384
325 #define DCA_MAX_EXSS_HEADER_SIZE 4096
326 
327 #define DCA_BUFFER_PADDING_SIZE 1024
328 
329 /** Bit allocation */
330 typedef struct {
331  int offset; ///< code values offset
332  int maxbits[8]; ///< max bits in VLC
333  int wrap; ///< wrap for get_vlc2()
334  VLC vlc[8]; ///< actual codes
335 } BitAlloc;
336 
337 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
338 static BitAlloc dca_tmode; ///< transition mode VLCs
339 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
340 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
341 
343  int idx)
344 {
345  return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
346  ba->offset;
347 }
348 
349 typedef struct {
351  /* Frame header */
352  int frame_type; ///< type of the current frame
353  int samples_deficit; ///< deficit sample count
354  int crc_present; ///< crc is present in the bitstream
355  int sample_blocks; ///< number of PCM sample blocks
356  int frame_size; ///< primary frame byte size
357  int amode; ///< audio channels arrangement
358  int sample_rate; ///< audio sampling rate
359  int bit_rate; ///< transmission bit rate
360  int bit_rate_index; ///< transmission bit rate index
361 
362  int downmix; ///< embedded downmix enabled
363  int dynrange; ///< embedded dynamic range flag
364  int timestamp; ///< embedded time stamp flag
365  int aux_data; ///< auxiliary data flag
366  int hdcd; ///< source material is mastered in HDCD
367  int ext_descr; ///< extension audio descriptor flag
368  int ext_coding; ///< extended coding flag
369  int aspf; ///< audio sync word insertion flag
370  int lfe; ///< low frequency effects flag
371  int predictor_history; ///< predictor history flag
372  int header_crc; ///< header crc check bytes
373  int multirate_inter; ///< multirate interpolator switch
374  int version; ///< encoder software revision
375  int copy_history; ///< copy history
376  int source_pcm_res; ///< source pcm resolution
377  int front_sum; ///< front sum/difference flag
378  int surround_sum; ///< surround sum/difference flag
379  int dialog_norm; ///< dialog normalisation parameter
380 
381  /* Primary audio coding header */
382  int subframes; ///< number of subframes
383  int total_channels; ///< number of channels including extensions
384  int prim_channels; ///< number of primary audio channels
385  int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
386  int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
387  int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
388  int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
389  int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
390  int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
391  int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
392  float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
393 
394  /* Primary audio coding side information */
395  int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
396  int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
397  int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
398  int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
399  int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
400  int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
401  int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
402  int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
403  int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
404  int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
405  int dynrange_coef; ///< dynamic range coefficient
406 
407  int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
408 
409  float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
411 
412  /* Subband samples history (for ADPCM) */
413  DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
414  DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
415  DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
416  int hist_index[DCA_PRIM_CHANNELS_MAX];
417  DECLARE_ALIGNED(32, float, raXin)[32];
418 
419  int output; ///< type of output
420 
421  DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
422  float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
423  float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
424  uint8_t *extra_channels_buffer;
425  unsigned int extra_channels_buffer_size;
426 
428  int dca_buffer_size; ///< how much data is in the dca_buffer
429 
430  const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
431  GetBitContext gb;
432  /* Current position in DCA frame */
433  int current_subframe;
434  int current_subsubframe;
435 
436  int core_ext_mask; ///< present extensions in the core substream
437 
438  /* XCh extension information */
439  int xch_present; ///< XCh extension present and valid
440  int xch_base_channel; ///< index of first (only) channel containing XCH data
441 
442  /* XXCH extension information */
443  int xxch_chset;
444  int xxch_nbits_spk_mask;
445  uint32_t xxch_core_spkmask;
446  uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
447  int xxch_chset_nch[4];
448  float xxch_dmix_sf[DCA_CHSETS_MAX];
449 
450  uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
451  float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
452 
453  int8_t xxch_order_tab[32];
454  int8_t lfe_index;
455 
456  /* ExSS header parser */
457  int static_fields; ///< static fields present
458  int mix_metadata; ///< mixing metadata present
459  int num_mix_configs; ///< number of mix out configurations
460  int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
461 
462  int profile;
463 
464  int debug_flag; ///< used for suppressing repeated error messages output
465  AVFloatDSPContext fdsp;
466  FFTContext imdct;
467  SynthFilterContext synth;
468  DCADSPContext dcadsp;
469  FmtConvertContext fmt_conv;
470 } DCAContext;
471 
472 static const uint16_t dca_vlc_offs[] = {
473  0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
474  5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
475  5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
476  7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
477  12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
478  18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
479 };
480 
481 static av_cold void dca_init_vlcs(void)
482 {
483  static int vlcs_initialized = 0;
484  int i, j, c = 14;
485  static VLC_TYPE dca_table[23622][2];
486 
487  if (vlcs_initialized)
488  return;
489 
490  dca_bitalloc_index.offset = 1;
491  dca_bitalloc_index.wrap = 2;
492  for (i = 0; i < 5; i++) {
493  dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
494  dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
495  init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
496  bitalloc_12_bits[i], 1, 1,
498  }
499  dca_scalefactor.offset = -64;
500  dca_scalefactor.wrap = 2;
501  for (i = 0; i < 5; i++) {
502  dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
503  dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
504  init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
505  scales_bits[i], 1, 1,
507  }
508  dca_tmode.offset = 0;
509  dca_tmode.wrap = 1;
510  for (i = 0; i < 4; i++) {
511  dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
512  dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
513  init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
514  tmode_bits[i], 1, 1,
516  }
517 
518  for (i = 0; i < 10; i++)
519  for (j = 0; j < 7; j++) {
520  if (!bitalloc_codes[i][j])
521  break;
522  dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
523  dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
524  dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
525  dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
526 
527  init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
528  bitalloc_sizes[i],
529  bitalloc_bits[i][j], 1, 1,
531  c++;
532  }
533  vlcs_initialized = 1;
534 }
535 
536 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
537 {
538  while (len--)
539  *dst++ = get_bits(gb, bits);
540 }
541 
542 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
543 {
544  int i, base, mask;
545 
546  /* locate channel set containing the channel */
547  for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
548  i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
549  base += av_popcount(mask);
550 
551  return base + av_popcount(mask & (xxch_ch - 1));
552 }
553 
554 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
555  int xxch)
556 {
557  int i, j;
558  static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
559  static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
560  static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
561  int hdr_pos = 0, hdr_size = 0;
562  float sign, mag, scale_factor;
563  int this_chans, acc_mask;
564  int embedded_downmix;
565  int nchans, mask[8];
566  int coeff, ichan;
567 
568  /* xxch has arbitrary sized audio coding headers */
569  if (xxch) {
570  hdr_pos = get_bits_count(&s->gb);
571  hdr_size = get_bits(&s->gb, 7) + 1;
572  }
573 
574  nchans = get_bits(&s->gb, 3) + 1;
575  s->total_channels = nchans + base_channel;
577 
578  /* obtain speaker layout mask & downmix coefficients for XXCH */
579  if (xxch) {
580  acc_mask = s->xxch_core_spkmask;
581 
582  this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
583  s->xxch_spk_masks[s->xxch_chset] = this_chans;
584  s->xxch_chset_nch[s->xxch_chset] = nchans;
585 
586  for (i = 0; i <= s->xxch_chset; i++)
587  acc_mask |= s->xxch_spk_masks[i];
588 
589  /* check for downmixing information */
590  if (get_bits1(&s->gb)) {
591  embedded_downmix = get_bits1(&s->gb);
592  scale_factor =
593  1.0f / dca_downmix_scale_factors[(get_bits(&s->gb, 6) - 1) << 2];
594 
595  s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
596 
597  for (i = base_channel; i < s->prim_channels; i++) {
598  mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
599  }
600 
601  for (j = base_channel; j < s->prim_channels; j++) {
602  memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
603  s->xxch_dmix_embedded |= (embedded_downmix << j);
604  for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
605  if (mask[j] & (1 << i)) {
606  if ((1 << i) == DCA_XXCH_LFE1) {
608  "DCA-XXCH: dmix to LFE1 not supported.\n");
609  continue;
610  }
611 
612  coeff = get_bits(&s->gb, 7);
613  sign = (coeff & 64) ? 1.0 : -1.0;
614  mag = dca_downmix_scale_factors[((coeff & 63) - 1) << 2];
615  ichan = dca_xxch2index(s, 1 << i);
616  s->xxch_dmix_coeff[j][ichan] = sign * mag;
617  }
618  }
619  }
620  }
621  }
622 
625 
626 
627  for (i = base_channel; i < s->prim_channels; i++) {
628  s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
629  if (s->subband_activity[i] > DCA_SUBBANDS)
631  }
632  for (i = base_channel; i < s->prim_channels; i++) {
633  s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
634  if (s->vq_start_subband[i] > DCA_SUBBANDS)
636  }
637  get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
638  get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
639  get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
640  get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
641 
642  /* Get codebooks quantization indexes */
643  if (!base_channel)
644  memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
645  for (j = 1; j < 11; j++)
646  for (i = base_channel; i < s->prim_channels; i++)
647  s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
648 
649  /* Get scale factor adjustment */
650  for (j = 0; j < 11; j++)
651  for (i = base_channel; i < s->prim_channels; i++)
652  s->scalefactor_adj[i][j] = 1;
653 
654  for (j = 1; j < 11; j++)
655  for (i = base_channel; i < s->prim_channels; i++)
656  if (s->quant_index_huffman[i][j] < thr[j])
657  s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
658 
659  if (!xxch) {
660  if (s->crc_present) {
661  /* Audio header CRC check */
662  get_bits(&s->gb, 16);
663  }
664  } else {
665  /* Skip to the end of the header, also ignore CRC if present */
666  i = get_bits_count(&s->gb);
667  if (hdr_pos + 8 * hdr_size > i)
668  skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
669  }
670 
671  s->current_subframe = 0;
672  s->current_subsubframe = 0;
673 
674 #ifdef TRACE
675  av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
676  av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
677  for (i = base_channel; i < s->prim_channels; i++) {
678  av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
679  s->subband_activity[i]);
680  av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
681  s->vq_start_subband[i]);
682  av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
683  s->joint_intensity[i]);
684  av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
685  s->transient_huffman[i]);
686  av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
687  s->scalefactor_huffman[i]);
688  av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
689  s->bitalloc_huffman[i]);
690  av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
691  for (j = 0; j < 11; j++)
692  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
693  av_log(s->avctx, AV_LOG_DEBUG, "\n");
694  av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
695  for (j = 0; j < 11; j++)
696  av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
697  av_log(s->avctx, AV_LOG_DEBUG, "\n");
698  }
699 #endif
700 
701  return 0;
702 }
703 
705 {
706  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
707 
708  /* Sync code */
709  skip_bits_long(&s->gb, 32);
710 
711  /* Frame header */
712  s->frame_type = get_bits(&s->gb, 1);
713  s->samples_deficit = get_bits(&s->gb, 5) + 1;
714  s->crc_present = get_bits(&s->gb, 1);
715  s->sample_blocks = get_bits(&s->gb, 7) + 1;
716  s->frame_size = get_bits(&s->gb, 14) + 1;
717  if (s->frame_size < 95)
718  return AVERROR_INVALIDDATA;
719  s->amode = get_bits(&s->gb, 6);
721  if (!s->sample_rate)
722  return AVERROR_INVALIDDATA;
723  s->bit_rate_index = get_bits(&s->gb, 5);
725  if (!s->bit_rate)
726  return AVERROR_INVALIDDATA;
727 
728  s->downmix = get_bits(&s->gb, 1); /* note: this is FixedBit == 0 */
729  s->dynrange = get_bits(&s->gb, 1);
730  s->timestamp = get_bits(&s->gb, 1);
731  s->aux_data = get_bits(&s->gb, 1);
732  s->hdcd = get_bits(&s->gb, 1);
733  s->ext_descr = get_bits(&s->gb, 3);
734  s->ext_coding = get_bits(&s->gb, 1);
735  s->aspf = get_bits(&s->gb, 1);
736  s->lfe = get_bits(&s->gb, 2);
737  s->predictor_history = get_bits(&s->gb, 1);
738 
739  if (s->lfe == 3) {
740  s->lfe = 0;
741  avpriv_request_sample(s->avctx, "LFE = 3");
742  return AVERROR_PATCHWELCOME;
743  }
744 
745  /* TODO: check CRC */
746  if (s->crc_present)
747  s->header_crc = get_bits(&s->gb, 16);
748 
749  s->multirate_inter = get_bits(&s->gb, 1);
750  s->version = get_bits(&s->gb, 4);
751  s->copy_history = get_bits(&s->gb, 2);
752  s->source_pcm_res = get_bits(&s->gb, 3);
753  s->front_sum = get_bits(&s->gb, 1);
754  s->surround_sum = get_bits(&s->gb, 1);
755  s->dialog_norm = get_bits(&s->gb, 4);
756 
757  /* FIXME: channels mixing levels */
758  s->output = s->amode;
759  if (s->lfe)
760  s->output |= DCA_LFE;
761 
762 #ifdef TRACE
763  av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
764  av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
765  av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
766  av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
767  s->sample_blocks, s->sample_blocks * 32);
768  av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
769  av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
770  s->amode, dca_channels[s->amode]);
771  av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
772  s->sample_rate);
773  av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
774  s->bit_rate);
775  av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
776  av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
777  av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
778  av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
779  av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
780  av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
781  av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
782  av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
783  av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
784  av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
785  s->predictor_history);
786  av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
787  av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
788  s->multirate_inter);
789  av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
790  av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
792  "source pcm resolution: %i (%i bits/sample)\n",
794  av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
795  av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
796  av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
797  av_log(s->avctx, AV_LOG_DEBUG, "\n");
798 #endif
799 
800  /* Primary audio coding header */
801  s->subframes = get_bits(&s->gb, 4) + 1;
802 
803  return dca_parse_audio_coding_header(s, 0, 0);
804 }
805 
806 
807 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
808 {
809  if (level < 5) {
810  /* huffman encoded */
811  value += get_bitalloc(gb, &dca_scalefactor, level);
812  value = av_clip(value, 0, (1 << log2range) - 1);
813  } else if (level < 8) {
814  if (level + 1 > log2range) {
815  skip_bits(gb, level + 1 - log2range);
816  value = get_bits(gb, log2range);
817  } else {
818  value = get_bits(gb, level + 1);
819  }
820  }
821  return value;
822 }
823 
824 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
825 {
826  /* Primary audio coding side information */
827  int j, k;
828 
829  if (get_bits_left(&s->gb) < 0)
830  return AVERROR_INVALIDDATA;
831 
832  if (!base_channel) {
833  s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
834  s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
835  }
836 
837  for (j = base_channel; j < s->prim_channels; j++) {
838  for (k = 0; k < s->subband_activity[j]; k++)
839  s->prediction_mode[j][k] = get_bits(&s->gb, 1);
840  }
841 
842  /* Get prediction codebook */
843  for (j = base_channel; j < s->prim_channels; j++) {
844  for (k = 0; k < s->subband_activity[j]; k++) {
845  if (s->prediction_mode[j][k] > 0) {
846  /* (Prediction coefficient VQ address) */
847  s->prediction_vq[j][k] = get_bits(&s->gb, 12);
848  }
849  }
850  }
851 
852  /* Bit allocation index */
853  for (j = base_channel; j < s->prim_channels; j++) {
854  for (k = 0; k < s->vq_start_subband[j]; k++) {
855  if (s->bitalloc_huffman[j] == 6)
856  s->bitalloc[j][k] = get_bits(&s->gb, 5);
857  else if (s->bitalloc_huffman[j] == 5)
858  s->bitalloc[j][k] = get_bits(&s->gb, 4);
859  else if (s->bitalloc_huffman[j] == 7) {
861  "Invalid bit allocation index\n");
862  return AVERROR_INVALIDDATA;
863  } else {
864  s->bitalloc[j][k] =
865  get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
866  }
867 
868  if (s->bitalloc[j][k] > 26) {
869  av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
870  j, k, s->bitalloc[j][k]);
871  return AVERROR_INVALIDDATA;
872  }
873  }
874  }
875 
876  /* Transition mode */
877  for (j = base_channel; j < s->prim_channels; j++) {
878  for (k = 0; k < s->subband_activity[j]; k++) {
879  s->transition_mode[j][k] = 0;
880  if (s->subsubframes[s->current_subframe] > 1 &&
881  k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
882  s->transition_mode[j][k] =
883  get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
884  }
885  }
886  }
887 
888  if (get_bits_left(&s->gb) < 0)
889  return AVERROR_INVALIDDATA;
890 
891  for (j = base_channel; j < s->prim_channels; j++) {
892  const uint32_t *scale_table;
893  int scale_sum, log_size;
894 
895  memset(s->scale_factor[j], 0,
896  s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
897 
898  if (s->scalefactor_huffman[j] == 6) {
899  scale_table = scale_factor_quant7;
900  log_size = 7;
901  } else {
902  scale_table = scale_factor_quant6;
903  log_size = 6;
904  }
905 
906  /* When huffman coded, only the difference is encoded */
907  scale_sum = 0;
908 
909  for (k = 0; k < s->subband_activity[j]; k++) {
910  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
911  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
912  s->scale_factor[j][k][0] = scale_table[scale_sum];
913  }
914 
915  if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
916  /* Get second scale factor */
917  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
918  s->scale_factor[j][k][1] = scale_table[scale_sum];
919  }
920  }
921  }
922 
923  /* Joint subband scale factor codebook select */
924  for (j = base_channel; j < s->prim_channels; j++) {
925  /* Transmitted only if joint subband coding enabled */
926  if (s->joint_intensity[j] > 0)
927  s->joint_huff[j] = get_bits(&s->gb, 3);
928  }
929 
930  if (get_bits_left(&s->gb) < 0)
931  return AVERROR_INVALIDDATA;
932 
933  /* Scale factors for joint subband coding */
934  for (j = base_channel; j < s->prim_channels; j++) {
935  int source_channel;
936 
937  /* Transmitted only if joint subband coding enabled */
938  if (s->joint_intensity[j] > 0) {
939  int scale = 0;
940  source_channel = s->joint_intensity[j] - 1;
941 
942  /* When huffman coded, only the difference is encoded
943  * (is this valid as well for joint scales ???) */
944 
945  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
946  scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
947  s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
948  }
949 
950  if (!(s->debug_flag & 0x02)) {
952  "Joint stereo coding not supported\n");
953  s->debug_flag |= 0x02;
954  }
955  }
956  }
957 
958  /* Stereo downmix coefficients */
959  if (!base_channel && s->prim_channels > 2) {
960  if (s->downmix) {
961  for (j = base_channel; j < s->prim_channels; j++) {
962  s->downmix_coef[j][0] = get_bits(&s->gb, 7);
963  s->downmix_coef[j][1] = get_bits(&s->gb, 7);
964  }
965  } else {
966  int am = s->amode & DCA_CHANNEL_MASK;
967  if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
969  "Invalid channel mode %d\n", am);
970  return AVERROR_INVALIDDATA;
971  }
972  for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
973  s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
974  s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
975  }
976  }
977  }
978 
979  /* Dynamic range coefficient */
980  if (!base_channel && s->dynrange)
981  s->dynrange_coef = get_bits(&s->gb, 8);
982 
983  /* Side information CRC check word */
984  if (s->crc_present) {
985  get_bits(&s->gb, 16);
986  }
987 
988  /*
989  * Primary audio data arrays
990  */
991 
992  /* VQ encoded high frequency subbands */
993  for (j = base_channel; j < s->prim_channels; j++)
994  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
995  /* 1 vector -> 32 samples */
996  s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
997 
998  /* Low frequency effect data */
999  if (!base_channel && s->lfe) {
1000  int quant7;
1001  /* LFE samples */
1002  int lfe_samples = 2 * s->lfe * (4 + block_index);
1003  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1004  float lfe_scale;
1005 
1006  for (j = lfe_samples; j < lfe_end_sample; j++) {
1007  /* Signed 8 bits int */
1008  s->lfe_data[j] = get_sbits(&s->gb, 8);
1009  }
1010 
1011  /* Scale factor index */
1012  quant7 = get_bits(&s->gb, 8);
1013  if (quant7 > 127) {
1014  avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
1015  return AVERROR_INVALIDDATA;
1016  }
1018 
1019  /* Quantization step size * scale factor */
1020  lfe_scale = 0.035 * s->lfe_scale_factor;
1021 
1022  for (j = lfe_samples; j < lfe_end_sample; j++)
1023  s->lfe_data[j] *= lfe_scale;
1024  }
1025 
1026 #ifdef TRACE
1027  av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1028  s->subsubframes[s->current_subframe]);
1029  av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1030  s->partial_samples[s->current_subframe]);
1031 
1032  for (j = base_channel; j < s->prim_channels; j++) {
1033  av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1034  for (k = 0; k < s->subband_activity[j]; k++)
1035  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1036  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1037  }
1038  for (j = base_channel; j < s->prim_channels; j++) {
1039  for (k = 0; k < s->subband_activity[j]; k++)
1041  "prediction coefs: %f, %f, %f, %f\n",
1042  (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1043  (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1044  (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1045  (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1046  }
1047  for (j = base_channel; j < s->prim_channels; j++) {
1048  av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1049  for (k = 0; k < s->vq_start_subband[j]; k++)
1050  av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1051  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1052  }
1053  for (j = base_channel; j < s->prim_channels; j++) {
1054  av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1055  for (k = 0; k < s->subband_activity[j]; k++)
1056  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1057  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1058  }
1059  for (j = base_channel; j < s->prim_channels; j++) {
1060  av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1061  for (k = 0; k < s->subband_activity[j]; k++) {
1062  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1063  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1064  if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1065  av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1066  }
1067  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1068  }
1069  for (j = base_channel; j < s->prim_channels; j++) {
1070  if (s->joint_intensity[j] > 0) {
1071  int source_channel = s->joint_intensity[j] - 1;
1072  av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1073  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1074  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1075  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1076  }
1077  }
1078  if (!base_channel && s->prim_channels > 2 && s->downmix) {
1079  av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
1080  for (j = 0; j < s->prim_channels; j++) {
1081  av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
1082  dca_downmix_coeffs[s->downmix_coef[j][0]]);
1083  av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
1084  dca_downmix_coeffs[s->downmix_coef[j][1]]);
1085  }
1086  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1087  }
1088  for (j = base_channel; j < s->prim_channels; j++)
1089  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1090  av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1091  if (!base_channel && s->lfe) {
1092  int lfe_samples = 2 * s->lfe * (4 + block_index);
1093  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1094 
1095  av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1096  for (j = lfe_samples; j < lfe_end_sample; j++)
1097  av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1098  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1099  }
1100 #endif
1101 
1102  return 0;
1103 }
1104 
1105 static void qmf_32_subbands(DCAContext *s, int chans,
1106  float samples_in[32][8], float *samples_out,
1107  float scale)
1108 {
1109  const float *prCoeff;
1110  int i;
1111 
1112  int sb_act = s->subband_activity[chans];
1113  int subindex;
1114 
1115  scale *= sqrt(1 / 8.0);
1116 
1117  /* Select filter */
1118  if (!s->multirate_inter) /* Non-perfect reconstruction */
1119  prCoeff = fir_32bands_nonperfect;
1120  else /* Perfect reconstruction */
1121  prCoeff = fir_32bands_perfect;
1122 
1123  for (i = sb_act; i < 32; i++)
1124  s->raXin[i] = 0.0;
1125 
1126  /* Reconstructed channel sample index */
1127  for (subindex = 0; subindex < 8; subindex++) {
1128  /* Load in one sample from each subband and clear inactive subbands */
1129  for (i = 0; i < sb_act; i++) {
1130  unsigned sign = (i - 1) & 2;
1131  uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
1132  AV_WN32A(&s->raXin[i], v);
1133  }
1134 
1135  s->synth.synth_filter_float(&s->imdct,
1136  s->subband_fir_hist[chans],
1137  &s->hist_index[chans],
1138  s->subband_fir_noidea[chans], prCoeff,
1139  samples_out, s->raXin, scale);
1140  samples_out += 32;
1141  }
1142 }
1143 
1144 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1145  int num_deci_sample, float *samples_in,
1146  float *samples_out, float scale)
1147 {
1148  /* samples_in: An array holding decimated samples.
1149  * Samples in current subframe starts from samples_in[0],
1150  * while samples_in[-1], samples_in[-2], ..., stores samples
1151  * from last subframe as history.
1152  *
1153  * samples_out: An array holding interpolated samples
1154  */
1155 
1156  int decifactor;
1157  const float *prCoeff;
1158  int deciindex;
1159 
1160  /* Select decimation filter */
1161  if (decimation_select == 1) {
1162  decifactor = 64;
1163  prCoeff = lfe_fir_128;
1164  } else {
1165  decifactor = 32;
1166  prCoeff = lfe_fir_64;
1167  }
1168  /* Interpolation */
1169  for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1170  s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1171  samples_in++;
1172  samples_out += 2 * decifactor;
1173  }
1174 }
1175 
1176 /* downmixing routines */
1177 #define MIX_REAR1(samples, s1, rs, coef) \
1178  samples[0][i] += samples[s1][i] * coef[rs][0]; \
1179  samples[1][i] += samples[s1][i] * coef[rs][1];
1180 
1181 #define MIX_REAR2(samples, s1, s2, rs, coef) \
1182  samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1183  samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1184 
1185 #define MIX_FRONT3(samples, coef) \
1186  t = samples[c][i]; \
1187  u = samples[l][i]; \
1188  v = samples[r][i]; \
1189  samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1190  samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1191 
1192 #define DOWNMIX_TO_STEREO(op1, op2) \
1193  for (i = 0; i < 256; i++) { \
1194  op1 \
1195  op2 \
1196  }
1197 
1198 static void dca_downmix(float **samples, int srcfmt,
1199  int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1200  const int8_t *channel_mapping)
1201 {
1202  int c, l, r, sl, sr, s;
1203  int i;
1204  float t, u, v;
1205  float coef[DCA_PRIM_CHANNELS_MAX][2];
1206 
1207  for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1208  coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1209  coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1210  }
1211 
1212  switch (srcfmt) {
1213  case DCA_MONO:
1214  case DCA_CHANNEL:
1215  case DCA_STEREO_TOTAL:
1216  case DCA_STEREO_SUMDIFF:
1217  case DCA_4F2R:
1218  av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1219  break;
1220  case DCA_STEREO:
1221  break;
1222  case DCA_3F:
1223  c = channel_mapping[0];
1224  l = channel_mapping[1];
1225  r = channel_mapping[2];
1226  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1227  break;
1228  case DCA_2F1R:
1229  s = channel_mapping[2];
1230  DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1231  break;
1232  case DCA_3F1R:
1233  c = channel_mapping[0];
1234  l = channel_mapping[1];
1235  r = channel_mapping[2];
1236  s = channel_mapping[3];
1237  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1238  MIX_REAR1(samples, s, 3, coef));
1239  break;
1240  case DCA_2F2R:
1241  sl = channel_mapping[2];
1242  sr = channel_mapping[3];
1243  DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1244  break;
1245  case DCA_3F2R:
1246  c = channel_mapping[0];
1247  l = channel_mapping[1];
1248  r = channel_mapping[2];
1249  sl = channel_mapping[3];
1250  sr = channel_mapping[4];
1251  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1252  MIX_REAR2(samples, sl, sr, 3, coef));
1253  break;
1254  }
1255 }
1256 
1257 
1258 #ifndef decode_blockcodes
1259 /* Very compact version of the block code decoder that does not use table
1260  * look-up but is slightly slower */
1261 static int decode_blockcode(int code, int levels, int *values)
1262 {
1263  int i;
1264  int offset = (levels - 1) >> 1;
1265 
1266  for (i = 0; i < 4; i++) {
1267  int div = FASTDIV(code, levels);
1268  values[i] = code - offset - div * levels;
1269  code = div;
1270  }
1271 
1272  return code;
1273 }
1274 
1275 static int decode_blockcodes(int code1, int code2, int levels, int *values)
1276 {
1277  return decode_blockcode(code1, levels, values) |
1278  decode_blockcode(code2, levels, values + 4);
1279 }
1280 #endif
1281 
1282 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1283 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1284 
1285 #ifndef int8x8_fmul_int32
1286 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1287 {
1288  float fscale = scale / 16.0;
1289  int i;
1290  for (i = 0; i < 8; i++)
1291  dst[i] = src[i] * fscale;
1292 }
1293 #endif
1294 
1295 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1296 {
1297  int k, l;
1298  int subsubframe = s->current_subsubframe;
1299 
1300  const float *quant_step_table;
1301 
1302  /* FIXME */
1303  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1304  LOCAL_ALIGNED_16(int, block, [8]);
1305 
1306  /*
1307  * Audio data
1308  */
1309 
1310  /* Select quantization step size table */
1311  if (s->bit_rate_index == 0x1f)
1312  quant_step_table = lossless_quant_d;
1313  else
1314  quant_step_table = lossy_quant_d;
1315 
1316  for (k = base_channel; k < s->prim_channels; k++) {
1317  if (get_bits_left(&s->gb) < 0)
1318  return AVERROR_INVALIDDATA;
1319 
1320  for (l = 0; l < s->vq_start_subband[k]; l++) {
1321  int m;
1322 
1323  /* Select the mid-tread linear quantizer */
1324  int abits = s->bitalloc[k][l];
1325 
1326  float quant_step_size = quant_step_table[abits];
1327 
1328  /*
1329  * Determine quantization index code book and its type
1330  */
1331 
1332  /* Select quantization index code book */
1333  int sel = s->quant_index_huffman[k][abits];
1334 
1335  /*
1336  * Extract bits from the bit stream
1337  */
1338  if (!abits) {
1339  memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1340  } else {
1341  /* Deal with transients */
1342  int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1343  float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
1344  s->scalefactor_adj[k][sel];
1345 
1346  if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1347  if (abits <= 7) {
1348  /* Block code */
1349  int block_code1, block_code2, size, levels, err;
1350 
1351  size = abits_sizes[abits - 1];
1352  levels = abits_levels[abits - 1];
1353 
1354  block_code1 = get_bits(&s->gb, size);
1355  block_code2 = get_bits(&s->gb, size);
1356  err = decode_blockcodes(block_code1, block_code2,
1357  levels, block);
1358  if (err) {
1360  "ERROR: block code look-up failed\n");
1361  return AVERROR_INVALIDDATA;
1362  }
1363  } else {
1364  /* no coding */
1365  for (m = 0; m < 8; m++)
1366  block[m] = get_sbits(&s->gb, abits - 3);
1367  }
1368  } else {
1369  /* Huffman coded */
1370  for (m = 0; m < 8; m++)
1371  block[m] = get_bitalloc(&s->gb,
1372  &dca_smpl_bitalloc[abits], sel);
1373  }
1374 
1375  s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1376  block, rscale, 8);
1377  }
1378 
1379  /*
1380  * Inverse ADPCM if in prediction mode
1381  */
1382  if (s->prediction_mode[k][l]) {
1383  int n;
1384  for (m = 0; m < 8; m++) {
1385  for (n = 1; n <= 4; n++)
1386  if (m >= n)
1387  subband_samples[k][l][m] +=
1388  (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1389  subband_samples[k][l][m - n] / 8192);
1390  else if (s->predictor_history)
1391  subband_samples[k][l][m] +=
1392  (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1393  s->subband_samples_hist[k][l][m - n + 4] / 8192);
1394  }
1395  }
1396  }
1397 
1398  /*
1399  * Decode VQ encoded high frequencies
1400  */
1401  for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1402  /* 1 vector -> 32 samples but we only need the 8 samples
1403  * for this subsubframe. */
1404  int hfvq = s->high_freq_vq[k][l];
1405 
1406  if (!s->debug_flag & 0x01) {
1408  "Stream with high frequencies VQ coding\n");
1409  s->debug_flag |= 0x01;
1410  }
1411 
1412  int8x8_fmul_int32(subband_samples[k][l],
1413  &high_freq_vq[hfvq][subsubframe * 8],
1414  s->scale_factor[k][l][0]);
1415  }
1416  }
1417 
1418  /* Check for DSYNC after subsubframe */
1419  if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1420  if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1421 #ifdef TRACE
1422  av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1423 #endif
1424  } else {
1425  av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1426  }
1427  }
1428 
1429  /* Backup predictor history for adpcm */
1430  for (k = base_channel; k < s->prim_channels; k++)
1431  for (l = 0; l < s->vq_start_subband[k]; l++)
1432  memcpy(s->subband_samples_hist[k][l],
1433  &subband_samples[k][l][4],
1434  4 * sizeof(subband_samples[0][0][0]));
1435 
1436  return 0;
1437 }
1438 
1439 static int dca_filter_channels(DCAContext *s, int block_index)
1440 {
1441  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1442  int k;
1443 
1444  /* 32 subbands QMF */
1445  for (k = 0; k < s->prim_channels; k++) {
1446 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1447  0, 8388608.0, 8388608.0 };*/
1448  if (s->channel_order_tab[k] >= 0)
1449  qmf_32_subbands(s, k, subband_samples[k],
1450  s->samples_chanptr[s->channel_order_tab[k]],
1451  M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1452  }
1453 
1454  /* Down mixing */
1455  if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1456  dca_downmix(s->samples_chanptr, s->amode, s->downmix_coef, s->channel_order_tab);
1457  }
1458 
1459  /* Generate LFE samples for this subsubframe FIXME!!! */
1460  if (s->output & DCA_LFE) {
1461  lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1462  s->lfe_data + 2 * s->lfe * (block_index + 4),
1463  s->samples_chanptr[s->lfe_index],
1464  1.0 / (256.0 * 32768.0));
1465  /* Outputs 20bits pcm samples */
1466  }
1467 
1468  return 0;
1469 }
1470 
1471 
1472 static int dca_subframe_footer(DCAContext *s, int base_channel)
1473 {
1474  int aux_data_count = 0, i;
1475 
1476  /*
1477  * Unpack optional information
1478  */
1479 
1480  /* presumably optional information only appears in the core? */
1481  if (!base_channel) {
1482  if (s->timestamp)
1483  skip_bits_long(&s->gb, 32);
1484 
1485  if (s->aux_data)
1486  aux_data_count = get_bits(&s->gb, 6);
1487 
1488  for (i = 0; i < aux_data_count; i++)
1489  get_bits(&s->gb, 8);
1490 
1491  if (s->crc_present && (s->downmix || s->dynrange))
1492  get_bits(&s->gb, 16);
1493  }
1494 
1495  return 0;
1496 }
1497 
1498 /**
1499  * Decode a dca frame block
1500  *
1501  * @param s pointer to the DCAContext
1502  */
1503 
1504 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1505 {
1506  int ret;
1507 
1508  /* Sanity check */
1509  if (s->current_subframe >= s->subframes) {
1510  av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1511  s->current_subframe, s->subframes);
1512  return AVERROR_INVALIDDATA;
1513  }
1514 
1515  if (!s->current_subsubframe) {
1516 #ifdef TRACE
1517  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1518 #endif
1519  /* Read subframe header */
1520  if ((ret = dca_subframe_header(s, base_channel, block_index)))
1521  return ret;
1522  }
1523 
1524  /* Read subsubframe */
1525 #ifdef TRACE
1526  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1527 #endif
1528  if ((ret = dca_subsubframe(s, base_channel, block_index)))
1529  return ret;
1530 
1531  /* Update state */
1532  s->current_subsubframe++;
1533  if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1534  s->current_subsubframe = 0;
1535  s->current_subframe++;
1536  }
1537  if (s->current_subframe >= s->subframes) {
1538 #ifdef TRACE
1539  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1540 #endif
1541  /* Read subframe footer */
1542  if ((ret = dca_subframe_footer(s, base_channel)))
1543  return ret;
1544  }
1545 
1546  return 0;
1547 }
1548 
1549 /**
1550  * Return the number of channels in an ExSS speaker mask (HD)
1551  */
1552 static int dca_exss_mask2count(int mask)
1553 {
1554  /* count bits that mean speaker pairs twice */
1555  return av_popcount(mask) +
1556  av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1565 }
1566 
1567 /**
1568  * Skip mixing coefficients of a single mix out configuration (HD)
1569  */
1570 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1571 {
1572  int i;
1573 
1574  for (i = 0; i < channels; i++) {
1575  int mix_map_mask = get_bits(gb, out_ch);
1576  int num_coeffs = av_popcount(mix_map_mask);
1577  skip_bits_long(gb, num_coeffs * 6);
1578  }
1579 }
1580 
1581 /**
1582  * Parse extension substream asset header (HD)
1583  */
1585 {
1586  int header_pos = get_bits_count(&s->gb);
1587  int header_size;
1588  int channels = 0;
1589  int embedded_stereo = 0;
1590  int embedded_6ch = 0;
1591  int drc_code_present;
1592  int av_uninit(extensions_mask);
1593  int i, j;
1594 
1595  if (get_bits_left(&s->gb) < 16)
1596  return -1;
1597 
1598  /* We will parse just enough to get to the extensions bitmask with which
1599  * we can set the profile value. */
1600 
1601  header_size = get_bits(&s->gb, 9) + 1;
1602  skip_bits(&s->gb, 3); // asset index
1603 
1604  if (s->static_fields) {
1605  if (get_bits1(&s->gb))
1606  skip_bits(&s->gb, 4); // asset type descriptor
1607  if (get_bits1(&s->gb))
1608  skip_bits_long(&s->gb, 24); // language descriptor
1609 
1610  if (get_bits1(&s->gb)) {
1611  /* How can one fit 1024 bytes of text here if the maximum value
1612  * for the asset header size field above was 512 bytes? */
1613  int text_length = get_bits(&s->gb, 10) + 1;
1614  if (get_bits_left(&s->gb) < text_length * 8)
1615  return -1;
1616  skip_bits_long(&s->gb, text_length * 8); // info text
1617  }
1618 
1619  skip_bits(&s->gb, 5); // bit resolution - 1
1620  skip_bits(&s->gb, 4); // max sample rate code
1621  channels = get_bits(&s->gb, 8) + 1;
1622 
1623  if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1624  int spkr_remap_sets;
1625  int spkr_mask_size = 16;
1626  int num_spkrs[7];
1627 
1628  if (channels > 2)
1629  embedded_stereo = get_bits1(&s->gb);
1630  if (channels > 6)
1631  embedded_6ch = get_bits1(&s->gb);
1632 
1633  if (get_bits1(&s->gb)) {
1634  spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1635  skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1636  }
1637 
1638  spkr_remap_sets = get_bits(&s->gb, 3);
1639 
1640  for (i = 0; i < spkr_remap_sets; i++) {
1641  /* std layout mask for each remap set */
1642  num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1643  }
1644 
1645  for (i = 0; i < spkr_remap_sets; i++) {
1646  int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1647  if (get_bits_left(&s->gb) < 0)
1648  return -1;
1649 
1650  for (j = 0; j < num_spkrs[i]; j++) {
1651  int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1652  int num_dec_ch = av_popcount(remap_dec_ch_mask);
1653  skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1654  }
1655  }
1656 
1657  } else {
1658  skip_bits(&s->gb, 3); // representation type
1659  }
1660  }
1661 
1662  drc_code_present = get_bits1(&s->gb);
1663  if (drc_code_present)
1664  get_bits(&s->gb, 8); // drc code
1665 
1666  if (get_bits1(&s->gb))
1667  skip_bits(&s->gb, 5); // dialog normalization code
1668 
1669  if (drc_code_present && embedded_stereo)
1670  get_bits(&s->gb, 8); // drc stereo code
1671 
1672  if (s->mix_metadata && get_bits1(&s->gb)) {
1673  skip_bits(&s->gb, 1); // external mix
1674  skip_bits(&s->gb, 6); // post mix gain code
1675 
1676  if (get_bits(&s->gb, 2) != 3) // mixer drc code
1677  skip_bits(&s->gb, 3); // drc limit
1678  else
1679  skip_bits(&s->gb, 8); // custom drc code
1680 
1681  if (get_bits1(&s->gb)) // channel specific scaling
1682  for (i = 0; i < s->num_mix_configs; i++)
1683  skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1684  else
1685  skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1686 
1687  for (i = 0; i < s->num_mix_configs; i++) {
1688  if (get_bits_left(&s->gb) < 0)
1689  return -1;
1690  dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1691  if (embedded_6ch)
1692  dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1693  if (embedded_stereo)
1694  dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1695  }
1696  }
1697 
1698  switch (get_bits(&s->gb, 2)) {
1699  case 0: extensions_mask = get_bits(&s->gb, 12); break;
1700  case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1701  case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1702  case 3: extensions_mask = 0; /* aux coding */ break;
1703  }
1704 
1705  /* not parsed further, we were only interested in the extensions mask */
1706 
1707  if (get_bits_left(&s->gb) < 0)
1708  return -1;
1709 
1710  if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1711  av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1712  return -1;
1713  }
1714  skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1715 
1716  if (extensions_mask & DCA_EXT_EXSS_XLL)
1717  s->profile = FF_PROFILE_DTS_HD_MA;
1718  else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1720  s->profile = FF_PROFILE_DTS_HD_HRA;
1721 
1722  if (!(extensions_mask & DCA_EXT_CORE))
1723  av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1724  if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1726  "DTS extensions detection mismatch (%d, %d)\n",
1727  extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1728 
1729  return 0;
1730 }
1731 
1733 {
1734  int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1735  int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1736  int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1737  int anctemp[DCA_CHSET_CHANS_MAX];
1738  int chset_fsize[DCA_CHSETS_MAX];
1739  int n_xbr_ch[DCA_CHSETS_MAX];
1740  int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1741  int i, j, k, l, chset, chan_base;
1742 
1743  av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1744 
1745  /* get bit position of sync header */
1746  hdr_pos = get_bits_count(&s->gb) - 32;
1747 
1748  hdr_size = get_bits(&s->gb, 6) + 1;
1749  num_chsets = get_bits(&s->gb, 2) + 1;
1750 
1751  for(i = 0; i < num_chsets; i++)
1752  chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1753 
1754  xbr_tmode = get_bits1(&s->gb);
1755 
1756  for(i = 0; i < num_chsets; i++) {
1757  n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1758  k = get_bits(&s->gb, 2) + 5;
1759  for(j = 0; j < n_xbr_ch[i]; j++)
1760  active_bands[i][j] = get_bits(&s->gb, k) + 1;
1761  }
1762 
1763  /* skip to the end of the header */
1764  i = get_bits_count(&s->gb);
1765  if(hdr_pos + hdr_size * 8 > i)
1766  skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1767 
1768  /* loop over the channel data sets */
1769  /* only decode as many channels as we've decoded base data for */
1770  for(chset = 0, chan_base = 0;
1771  chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1772  chan_base += n_xbr_ch[chset++]) {
1773  int start_posn = get_bits_count(&s->gb);
1774  int subsubframe = 0;
1775  int subframe = 0;
1776 
1777  /* loop over subframes */
1778  for (k = 0; k < (s->sample_blocks / 8); k++) {
1779  /* parse header if we're on first subsubframe of a block */
1780  if(subsubframe == 0) {
1781  /* Parse subframe header */
1782  for(i = 0; i < n_xbr_ch[chset]; i++) {
1783  anctemp[i] = get_bits(&s->gb, 2) + 2;
1784  }
1785 
1786  for(i = 0; i < n_xbr_ch[chset]; i++) {
1787  get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1788  }
1789 
1790  for(i = 0; i < n_xbr_ch[chset]; i++) {
1791  anctemp[i] = get_bits(&s->gb, 3);
1792  if(anctemp[i] < 1) {
1793  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1794  return AVERROR_INVALIDDATA;
1795  }
1796  }
1797 
1798  /* generate scale factors */
1799  for(i = 0; i < n_xbr_ch[chset]; i++) {
1800  const uint32_t *scale_table;
1801  int nbits;
1802 
1803  if (s->scalefactor_huffman[chan_base+i] == 6) {
1804  scale_table = scale_factor_quant7;
1805  } else {
1806  scale_table = scale_factor_quant6;
1807  }
1808 
1809  nbits = anctemp[i];
1810 
1811  for(j = 0; j < active_bands[chset][i]; j++) {
1812  if(abits_high[i][j] > 0) {
1813  scale_table_high[i][j][0] =
1814  scale_table[get_bits(&s->gb, nbits)];
1815 
1816  if(xbr_tmode && s->transition_mode[i][j]) {
1817  scale_table_high[i][j][1] =
1818  scale_table[get_bits(&s->gb, nbits)];
1819  }
1820  }
1821  }
1822  }
1823  }
1824 
1825  /* decode audio array for this block */
1826  for(i = 0; i < n_xbr_ch[chset]; i++) {
1827  for(j = 0; j < active_bands[chset][i]; j++) {
1828  const int xbr_abits = abits_high[i][j];
1829  const float quant_step_size = lossless_quant_d[xbr_abits];
1830  const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1831  const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1832  float *subband_samples = s->subband_samples[k][chan_base+i][j];
1833  int block[8];
1834 
1835  if(xbr_abits <= 0)
1836  continue;
1837 
1838  if(xbr_abits > 7) {
1839  get_array(&s->gb, block, 8, xbr_abits - 3);
1840  } else {
1841  int block_code1, block_code2, size, levels, err;
1842 
1843  size = abits_sizes[xbr_abits - 1];
1844  levels = abits_levels[xbr_abits - 1];
1845 
1846  block_code1 = get_bits(&s->gb, size);
1847  block_code2 = get_bits(&s->gb, size);
1848  err = decode_blockcodes(block_code1, block_code2,
1849  levels, block);
1850  if (err) {
1852  "ERROR: DTS-XBR: block code look-up failed\n");
1853  return AVERROR_INVALIDDATA;
1854  }
1855  }
1856 
1857  /* scale & sum into subband */
1858  for(l = 0; l < 8; l++)
1859  subband_samples[l] += (float)block[l] * rscale;
1860  }
1861  }
1862 
1863  /* check DSYNC marker */
1864  if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1865  if(get_bits(&s->gb, 16) != 0xffff) {
1866  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1867  return AVERROR_INVALIDDATA;
1868  }
1869  }
1870 
1871  /* advance sub-sub-frame index */
1872  if(++subsubframe >= s->subsubframes[subframe]) {
1873  subsubframe = 0;
1874  subframe++;
1875  }
1876  }
1877 
1878  /* skip to next channel set */
1879  i = get_bits_count(&s->gb);
1880  if(start_posn + chset_fsize[chset] * 8 != i) {
1881  j = start_posn + chset_fsize[chset] * 8 - i;
1882  if(j < 0 || j >= 8)
1883  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1884  " skipping further than expected (%d bits)\n", j);
1885  skip_bits_long(&s->gb, j);
1886  }
1887  }
1888 
1889  return 0;
1890 }
1891 
1892 /* parse initial header for XXCH and dump details */
1894 {
1895  int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1896  int i, chset, base_channel, chstart, fsize[8];
1897 
1898  /* assume header word has already been parsed */
1899  hdr_pos = get_bits_count(&s->gb) - 32;
1900  hdr_size = get_bits(&s->gb, 6) + 1;
1901  /*chhdr_crc =*/ skip_bits1(&s->gb);
1902  spkmsk_bits = get_bits(&s->gb, 5) + 1;
1903  num_chsets = get_bits(&s->gb, 2) + 1;
1904 
1905  for (i = 0; i < num_chsets; i++)
1906  fsize[i] = get_bits(&s->gb, 14) + 1;
1907 
1908  core_spk = get_bits(&s->gb, spkmsk_bits);
1909  s->xxch_core_spkmask = core_spk;
1910  s->xxch_nbits_spk_mask = spkmsk_bits;
1911  s->xxch_dmix_embedded = 0;
1912 
1913  /* skip to the end of the header */
1914  i = get_bits_count(&s->gb);
1915  if (hdr_pos + hdr_size * 8 > i)
1916  skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1917 
1918  for (chset = 0; chset < num_chsets; chset++) {
1919  chstart = get_bits_count(&s->gb);
1920  base_channel = s->prim_channels;
1921  s->xxch_chset = chset;
1922 
1923  /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1924  5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1925  dca_parse_audio_coding_header(s, base_channel, 1);
1926 
1927  /* decode channel data */
1928  for (i = 0; i < (s->sample_blocks / 8); i++) {
1929  if (dca_decode_block(s, base_channel, i)) {
1931  "Error decoding DTS-XXCH extension\n");
1932  continue;
1933  }
1934  }
1935 
1936  /* skip to end of this section */
1937  i = get_bits_count(&s->gb);
1938  if (chstart + fsize[chset] * 8 > i)
1939  skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1940  }
1941  s->xxch_chset = num_chsets;
1942 
1943  return 0;
1944 }
1945 
1946 /**
1947  * Parse extension substream header (HD)
1948  */
1950 {
1951  int asset_size[8];
1952  int ss_index;
1953  int blownup;
1954  int num_audiop = 1;
1955  int num_assets = 1;
1956  int active_ss_mask[8];
1957  int i, j;
1958  int start_posn;
1959  int hdrsize;
1960  uint32_t mkr;
1961 
1962  if (get_bits_left(&s->gb) < 52)
1963  return;
1964 
1965  start_posn = get_bits_count(&s->gb) - 32;
1966 
1967  skip_bits(&s->gb, 8); // user data
1968  ss_index = get_bits(&s->gb, 2);
1969 
1970  blownup = get_bits1(&s->gb);
1971  hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
1972  skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1973 
1974  s->static_fields = get_bits1(&s->gb);
1975  if (s->static_fields) {
1976  skip_bits(&s->gb, 2); // reference clock code
1977  skip_bits(&s->gb, 3); // frame duration code
1978 
1979  if (get_bits1(&s->gb))
1980  skip_bits_long(&s->gb, 36); // timestamp
1981 
1982  /* a single stream can contain multiple audio assets that can be
1983  * combined to form multiple audio presentations */
1984 
1985  num_audiop = get_bits(&s->gb, 3) + 1;
1986  if (num_audiop > 1) {
1988  "Multiple DTS-HD audio presentations");
1989  /* ignore such streams for now */
1990  return;
1991  }
1992 
1993  num_assets = get_bits(&s->gb, 3) + 1;
1994  if (num_assets > 1) {
1995  avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
1996  /* ignore such streams for now */
1997  return;
1998  }
1999 
2000  for (i = 0; i < num_audiop; i++)
2001  active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
2002 
2003  for (i = 0; i < num_audiop; i++)
2004  for (j = 0; j <= ss_index; j++)
2005  if (active_ss_mask[i] & (1 << j))
2006  skip_bits(&s->gb, 8); // active asset mask
2007 
2008  s->mix_metadata = get_bits1(&s->gb);
2009  if (s->mix_metadata) {
2010  int mix_out_mask_size;
2011 
2012  skip_bits(&s->gb, 2); // adjustment level
2013  mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2014  s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2015 
2016  for (i = 0; i < s->num_mix_configs; i++) {
2017  int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2018  s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2019  }
2020  }
2021  }
2022 
2023  for (i = 0; i < num_assets; i++)
2024  asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2025 
2026  for (i = 0; i < num_assets; i++) {
2028  return;
2029  }
2030 
2031  /* not parsed further, we were only interested in the extensions mask
2032  * from the asset header */
2033 
2034  if (num_assets > 0) {
2035  j = get_bits_count(&s->gb);
2036  if (start_posn + hdrsize * 8 > j)
2037  skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2038 
2039  for (i = 0; i < num_assets; i++) {
2040  start_posn = get_bits_count(&s->gb);
2041  mkr = get_bits_long(&s->gb, 32);
2042 
2043  /* parse extensions that we know about */
2044  if (mkr == 0x655e315e) {
2046  } else if (mkr == 0x47004a03) {
2048  s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2049  } else {
2051  "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2052  }
2053 
2054  /* skip to end of block */
2055  j = get_bits_count(&s->gb);
2056  if (start_posn + asset_size[i] * 8 > j)
2057  skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2058  }
2059  }
2060 }
2061 
2062 /**
2063  * Main frame decoding function
2064  * FIXME add arguments
2065  */
2066 static int dca_decode_frame(AVCodecContext *avctx, void *data,
2067  int *got_frame_ptr, AVPacket *avpkt)
2068 {
2069  AVFrame *frame = data;
2070  const uint8_t *buf = avpkt->data;
2071  int buf_size = avpkt->size;
2072  int channel_mask;
2073  int channel_layout;
2074  int lfe_samples;
2075  int num_core_channels = 0;
2076  int i, ret;
2077  float **samples_flt;
2078  float *src_chan;
2079  float *dst_chan;
2080  DCAContext *s = avctx->priv_data;
2081  int core_ss_end;
2082  int channels, full_channels;
2083  float scale;
2084  int achan;
2085  int chset;
2086  int mask;
2087  int lavc;
2088  int posn;
2089  int j, k;
2090  int endch;
2091 
2092  s->xch_present = 0;
2093 
2094  s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2096  if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2097  av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2098  return AVERROR_INVALIDDATA;
2099  }
2100 
2101  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
2102  if ((ret = dca_parse_frame_header(s)) < 0) {
2103  //seems like the frame is corrupt, try with the next one
2104  return ret;
2105  }
2106  //set AVCodec values with parsed data
2107  avctx->sample_rate = s->sample_rate;
2108  avctx->bit_rate = s->bit_rate;
2109 
2110  s->profile = FF_PROFILE_DTS;
2111 
2112  for (i = 0; i < (s->sample_blocks / 8); i++) {
2113  if ((ret = dca_decode_block(s, 0, i))) {
2114  av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2115  return ret;
2116  }
2117  }
2118 
2119  /* record number of core channels incase less than max channels are requested */
2120  num_core_channels = s->prim_channels;
2121 
2122  if (s->ext_coding)
2123  s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2124  else
2125  s->core_ext_mask = 0;
2126 
2127  core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2128 
2129  /* only scan for extensions if ext_descr was unknown or indicated a
2130  * supported XCh extension */
2131  if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2132 
2133  /* if ext_descr was unknown, clear s->core_ext_mask so that the
2134  * extensions scan can fill it up */
2135  s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2136 
2137  /* extensions start at 32-bit boundaries into bitstream */
2138  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2139 
2140  while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2141  uint32_t bits = get_bits_long(&s->gb, 32);
2142 
2143  switch (bits) {
2144  case 0x5a5a5a5a: {
2145  int ext_amode, xch_fsize;
2146 
2147  s->xch_base_channel = s->prim_channels;
2148 
2149  /* validate sync word using XCHFSIZE field */
2150  xch_fsize = show_bits(&s->gb, 10);
2151  if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2152  (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2153  continue;
2154 
2155  /* skip length-to-end-of-frame field for the moment */
2156  skip_bits(&s->gb, 10);
2157 
2158  s->core_ext_mask |= DCA_EXT_XCH;
2159 
2160  /* extension amode(number of channels in extension) should be 1 */
2161  /* AFAIK XCh is not used for more channels */
2162  if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2163  av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2164  " supported!\n", ext_amode);
2165  continue;
2166  }
2167 
2168  if (s->xch_base_channel < 2) {
2169  avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
2170  continue;
2171  }
2172 
2173  /* much like core primary audio coding header */
2174  dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2175 
2176  for (i = 0; i < (s->sample_blocks / 8); i++)
2177  if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2178  av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2179  continue;
2180  }
2181 
2182  s->xch_present = 1;
2183  break;
2184  }
2185  case 0x47004a03:
2186  /* XXCh: extended channels */
2187  /* usually found either in core or HD part in DTS-HD HRA streams,
2188  * but not in DTS-ES which contains XCh extensions instead */
2189  s->core_ext_mask |= DCA_EXT_XXCH;
2191  break;
2192 
2193  case 0x1d95f262: {
2194  int fsize96 = show_bits(&s->gb, 12) + 1;
2195  if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2196  continue;
2197 
2198  av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2199  get_bits_count(&s->gb));
2200  skip_bits(&s->gb, 12);
2201  av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2202  av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2203 
2204  s->core_ext_mask |= DCA_EXT_X96;
2205  break;
2206  }
2207  }
2208 
2209  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2210  }
2211  } else {
2212  /* no supported extensions, skip the rest of the core substream */
2213  skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2214  }
2215 
2216  if (s->core_ext_mask & DCA_EXT_X96)
2217  s->profile = FF_PROFILE_DTS_96_24;
2218  else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2219  s->profile = FF_PROFILE_DTS_ES;
2220 
2221  /* check for ExSS (HD part) */
2222  if (s->dca_buffer_size - s->frame_size > 32 &&
2223  get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2225 
2226  avctx->profile = s->profile;
2227 
2228  full_channels = channels = s->prim_channels + !!s->lfe;
2229 
2230  /* If we have XXCH then the channel layout is managed differently */
2231  /* note that XLL will also have another way to do things */
2232  if (!(s->core_ext_mask & DCA_EXT_XXCH)
2233  || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2234  && avctx->request_channels
2235  < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2236  { /* xxx should also do MA extensions */
2237  if (s->amode < 16) {
2239 
2240  if (s->xch_present && (!avctx->request_channels ||
2241  avctx->request_channels
2242  > num_core_channels + !!s->lfe)) {
2244  if (s->lfe) {
2247  } else {
2249  }
2250  if (s->channel_order_tab[s->xch_base_channel] < 0)
2251  return AVERROR_INVALIDDATA;
2252  } else {
2253  channels = num_core_channels + !!s->lfe;
2254  s->xch_present = 0; /* disable further xch processing */
2255  if (s->lfe) {
2258  } else
2260  }
2261 
2262  if (channels > !!s->lfe &&
2263  s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2264  return AVERROR_INVALIDDATA;
2265 
2266  if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2267  av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2268  return AVERROR_INVALIDDATA;
2269  }
2270 
2271  if (avctx->request_channels == 2 && s->prim_channels > 2) {
2272  channels = 2;
2273  s->output = DCA_STEREO;
2275  }
2276  else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2277  static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2278  s->channel_order_tab = dca_channel_order_native;
2279  }
2280  s->lfe_index = dca_lfe_index[s->amode];
2281  } else {
2282  av_log(avctx, AV_LOG_ERROR,
2283  "Non standard configuration %d !\n", s->amode);
2284  return AVERROR_INVALIDDATA;
2285  }
2286 
2287  s->xxch_dmix_embedded = 0;
2288  } else {
2289  /* we only get here if an XXCH channel set can be added to the mix */
2290  channel_mask = s->xxch_core_spkmask;
2291 
2292  if (avctx->request_channels > 0
2293  && avctx->request_channels < s->prim_channels) {
2294  channels = num_core_channels + !!s->lfe;
2295  for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2296  <= avctx->request_channels; i++) {
2297  channels += s->xxch_chset_nch[i];
2298  channel_mask |= s->xxch_spk_masks[i];
2299  }
2300  } else {
2301  channels = s->prim_channels + !!s->lfe;
2302  for (i = 0; i < s->xxch_chset; i++) {
2303  channel_mask |= s->xxch_spk_masks[i];
2304  }
2305  }
2306 
2307  /* Given the DTS spec'ed channel mask, generate an avcodec version */
2308  channel_layout = 0;
2309  for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2310  if (channel_mask & (1 << i)) {
2311  channel_layout |= map_xxch_to_native[i];
2312  }
2313  }
2314 
2315  /* make sure that we have managed to get equivelant dts/avcodec channel
2316  * masks in some sense -- unfortunately some channels could overlap */
2317  if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2318  av_log(avctx, AV_LOG_DEBUG,
2319  "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
2320  return AVERROR_INVALIDDATA;
2321  }
2322 
2323  avctx->channel_layout = channel_layout;
2324 
2325  if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2326  /* Estimate DTS --> avcodec ordering table */
2327  for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2328  mask = chset >= 0 ? s->xxch_spk_masks[chset]
2329  : s->xxch_core_spkmask;
2330  for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2331  if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2332  lavc = map_xxch_to_native[i];
2333  posn = av_popcount(channel_layout & (lavc - 1));
2334  s->xxch_order_tab[j++] = posn;
2335  }
2336  }
2337  }
2338 
2339  s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2340  } else { /* native ordering */
2341  for (i = 0; i < channels; i++)
2342  s->xxch_order_tab[i] = i;
2343 
2344  s->lfe_index = channels - 1;
2345  }
2346 
2347  s->channel_order_tab = s->xxch_order_tab;
2348  }
2349 
2350  if (avctx->channels != channels) {
2351  if (avctx->channels)
2352  av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2353  avctx->channels = channels;
2354  }
2355 
2356  /* get output buffer */
2357  frame->nb_samples = 256 * (s->sample_blocks / 8);
2358  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2359  return ret;
2360  samples_flt = (float **)frame->extended_data;
2361 
2362  /* allocate buffer for extra channels if downmixing */
2363  if (avctx->channels < full_channels) {
2364  ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2365  frame->nb_samples,
2366  avctx->sample_fmt, 0);
2367  if (ret < 0)
2368  return ret;
2369 
2370  av_fast_malloc(&s->extra_channels_buffer,
2371  &s->extra_channels_buffer_size, ret);
2372  if (!s->extra_channels_buffer)
2373  return AVERROR(ENOMEM);
2374 
2375  ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
2376  s->extra_channels_buffer,
2377  full_channels - channels,
2378  frame->nb_samples, avctx->sample_fmt, 0);
2379  if (ret < 0)
2380  return ret;
2381  }
2382 
2383  /* filter to get final output */
2384  for (i = 0; i < (s->sample_blocks / 8); i++) {
2385  int ch;
2386 
2387  for (ch = 0; ch < channels; ch++)
2388  s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2389  for (; ch < full_channels; ch++)
2390  s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2391 
2392  dca_filter_channels(s, i);
2393 
2394  /* If this was marked as a DTS-ES stream we need to subtract back- */
2395  /* channel from SL & SR to remove matrixed back-channel signal */
2396  if ((s->source_pcm_res & 1) && s->xch_present) {
2397  float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2398  float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2399  float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
2400  s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2401  s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2402  }
2403 
2404  /* If stream contains XXCH, we might need to undo an embedded downmix */
2405  if (s->xxch_dmix_embedded) {
2406  /* Loop over channel sets in turn */
2407  ch = num_core_channels;
2408  for (chset = 0; chset < s->xxch_chset; chset++) {
2409  endch = ch + s->xxch_chset_nch[chset];
2410  mask = s->xxch_dmix_embedded;
2411 
2412  /* undo downmix */
2413  for (j = ch; j < endch; j++) {
2414  if (mask & (1 << j)) { /* this channel has been mixed-out */
2415  src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2416  for (k = 0; k < endch; k++) {
2417  achan = s->channel_order_tab[k];
2418  scale = s->xxch_dmix_coeff[j][k];
2419  if (scale != 0.0) {
2420  dst_chan = s->samples_chanptr[achan];
2421  s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2422  -scale, 256);
2423  }
2424  }
2425  }
2426  }
2427 
2428  /* if a downmix has been embedded then undo the pre-scaling */
2429  if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2430  scale = s->xxch_dmix_sf[chset];
2431 
2432  for (j = 0; j < ch; j++) {
2433  src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2434  for (k = 0; k < 256; k++)
2435  src_chan[k] *= scale;
2436  }
2437 
2438  /* LFE channel is always part of core, scale if it exists */
2439  if (s->lfe) {
2440  src_chan = s->samples_chanptr[s->lfe_index];
2441  for (k = 0; k < 256; k++)
2442  src_chan[k] *= scale;
2443  }
2444  }
2445 
2446  ch = endch;
2447  }
2448 
2449  }
2450  }
2451 
2452  /* update lfe history */
2453  lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2454  for (i = 0; i < 2 * s->lfe * 4; i++)
2455  s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2456 
2457  *got_frame_ptr = 1;
2458 
2459  return buf_size;
2460 }
2461 
2462 
2463 
2464 /**
2465  * DCA initialization
2466  *
2467  * @param avctx pointer to the AVCodecContext
2468  */
2469 
2471 {
2472  DCAContext *s = avctx->priv_data;
2473 
2474  s->avctx = avctx;
2475  dca_init_vlcs();
2476 
2477  avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2478  ff_mdct_init(&s->imdct, 6, 1, 1.0);
2479  ff_synth_filter_init(&s->synth);
2480  ff_dcadsp_init(&s->dcadsp);
2481  ff_fmt_convert_init(&s->fmt_conv, avctx);
2482 
2483  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2484 
2485  /* allow downmixing to stereo */
2486  if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
2487  avctx->request_channels == 2) {
2488  avctx->channels = avctx->request_channels;
2489  }
2490 
2491  return 0;
2492 }
2493 
2495 {
2496  DCAContext *s = avctx->priv_data;
2497  ff_mdct_end(&s->imdct);
2498  av_freep(&s->extra_channels_buffer);
2499  return 0;
2500 }
2501 
2502 static const AVProfile profiles[] = {
2503  { FF_PROFILE_DTS, "DTS" },
2504  { FF_PROFILE_DTS_ES, "DTS-ES" },
2505  { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2506  { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2507  { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2508  { FF_PROFILE_UNKNOWN },
2509 };
2510 
2512  .name = "dca",
2513  .type = AVMEDIA_TYPE_AUDIO,
2514  .id = AV_CODEC_ID_DTS,
2515  .priv_data_size = sizeof(DCAContext),
2516  .init = dca_decode_init,
2518  .close = dca_decode_end,
2519  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2520  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2521  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2523  .profiles = NULL_IF_CONFIG_SMALL(profiles),
2524 };
int wrap
wrap for get_vlc2()
Definition: dcadec.c:333
int ext_descr
extension audio descriptor flag
Definition: dcadec.c:367
static const int8_t bitalloc_offsets[10]
Definition: dcahuff.h:1024
float v
const char * s
Definition: avisynth_c.h:668
int downmix
embedded downmix enabled
Definition: dcadec.c:362
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
quantization index codebook select
Definition: dcadec.c:391
#define DCA_BLOCKS_MAX
Definition: dcadec.c:60
int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]
bit allocation quantizer select
Definition: dcadec.c:390
int crc_present
crc is present in the bitstream
Definition: dcadec.c:354
#define DCA_CHSET_CHANS_MAX
Definition: dcadec.c:63
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
low bitrate component in ExSS
Definition: dcadec.c:170
#define AV_CH_TOP_FRONT_RIGHT
int timestamp
embedded time stamp flag
Definition: dcadec.c:364
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:125
int amode
audio channels arrangement
Definition: dcadec.c:357
static const AVProfile profiles[]
Definition: dcadec.c:2502
static const uint16_t tmode_codes[TMODE_COUNT][4]
Definition: dcahuff.h:31
int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]
stereo downmix coefficients
Definition: dcadec.c:404
int transient_huffman[DCA_PRIM_CHANNELS_MAX]
transient mode code book
Definition: dcadec.c:388
int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]
scale factors (2 if transient)
Definition: dcadec.c:401
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static const uint32_t scale_factor_quant6[64]
Definition: dcadata.h:4157
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int dca_parse_frame_header(DCAContext *s)
Definition: dcadec.c:704
static int dca_exss_parse_asset_header(DCAContext *s)
Parse extension substream asset header (HD)
Definition: dcadec.c:1584
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
Skip mixing coefficients of a single mix out configuration (HD)
Definition: dcadec.c:1570
#define AV_CH_TOP_FRONT_LEFT
#define M_SQRT1_2
Definition: mathematics.h:49
int vq_start_subband[DCA_PRIM_CHANNELS_MAX]
high frequency vq start subband
Definition: dcadec.c:386
XXCh channels extension in core substream.
Definition: dcadec.c:163
#define AV_CH_TOP_FRONT_CENTER
void ff_dcadsp_init(DCADSPContext *s)
Definition: dcadsp.c:47
int samples_deficit
deficit sample count
Definition: dcadec.c:353
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:59
Definition: dcadec.c:71
static const float lossless_quant_d[32]
Definition: dcadata.h:4210
int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
Convert bitstream to one representation based on sync marker.
Definition: dca.c:37
int dynrange
embedded dynamic range flag
Definition: dcadec.c:363
int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
joint subband scale factors
Definition: dcadec.c:403
int version
encoder software revision
Definition: dcadec.c:374
static const int8_t dca_channel_reorder_lfe_xch[][9]
Definition: dcadec.c:258
#define VLC_TYPE
Definition: get_bits.h:61
#define FF_ARRAY_ELEMS(a)
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
core in core substream
Definition: dcadec.c:162
#define AV_CH_LAYOUT_STEREO
static BitAlloc dca_scalefactor
scalefactor VLCs
Definition: dcadec.c:339
#define AV_WN32A(p, v)
Definition: intreadwrite.h:530
#define DCA_BUFFER_PADDING_SIZE
Definition: dcadec.c:327
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:225
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define DCA_HD_MARKER
DCA-HD specific block starts with this marker.
Definition: dca.h:38
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
#define AV_RN32A(p)
Definition: intreadwrite.h:518
static int dca_subframe_footer(DCAContext *s, int base_channel)
Definition: dcadec.c:1472
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
static BitAlloc dca_tmode
transition mode VLCs
Definition: dcadec.c:338
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Fill plane data pointers and linesize for samples with sample format sample_fmt.
Definition: samplefmt.c:155
#define MIX_FRONT3(samples, coef)
Definition: dcadec.c:1185
#define FF_PROFILE_DTS_ES
static int dca_exss_mask2count(int mask)
Return the number of channels in an ExSS speaker mask (HD)
Definition: dcadec.c:1552
static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:824
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]
scale factor code book
Definition: dcadec.c:389
static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
Decode a dca frame block.
Definition: dcadec.c:1504
static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:1295
uint8_t bits
Definition: crc.c:216
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
static const uint16_t dca_vlc_offs[]
Definition: dcadec.c:472
#define DCA_LFE
Definition: dcadec.c:320
window constants for m
#define AV_CH_TOP_BACK_LEFT
#define DCA_SUBBANDS
Definition: dcadec.c:56
static int dca_parse_audio_coding_header(DCAContext *s, int base_channel, int xxch)
Definition: dcadec.c:554
static const uint32_t scale_factor_quant7[128]
Definition: dcadata.h:4168
#define MIX_REAR2(samples, s1, s2, rs, coef)
Definition: dcadec.c:1181
static void get_array(GetBitContext *gb, int *dst, int len, int bits)
Definition: dcadec.c:536
#define AV_CH_TOP_BACK_CENTER
#define FF_PROFILE_UNKNOWN
static av_cold int dca_decode_end(AVCodecContext *avctx)
Definition: dcadec.c:2494
#define AV_CH_LOW_FREQUENCY
int header_crc
header crc check bytes
Definition: dcadec.c:372
DCAMode
Definition: dcadec.c:65
#define FF_PROFILE_DTS_96_24
int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
transition mode (transients)
Definition: dcadec.c:400
static int dca_xxch2index(DCAContext *s, int xxch_ch)
Definition: dcadec.c:542
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
int sample_rate
audio sampling rate
Definition: dcadec.c:358
bitstream reader API header.
#define AV_CH_BACK_LEFT
integer sqrt
Definition: avutil.txt:2
AVCodecContext * avctx
Definition: dcadec.c:350
XCh channel extension in core substream.
Definition: dcadec.c:165
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
static BitAlloc dca_bitalloc_index
indexes for samples VLC select
Definition: dcadec.c:337
int lfe
low frequency effects flag
Definition: dcadec.c:370
static BitAlloc dca_smpl_bitalloc[11]
samples VLCs
Definition: dcadec.c:340
XXCh channels extension in ExSS.
Definition: dcadec.c:168
static const uint8_t bitalloc_sizes[10]
Definition: dcahuff.h:1022
float, planar
Definition: samplefmt.h:60
frame
Definition: stft.m:14
#define DCA_CHANNEL_MASK
Definition: dcadec.c:318
int predictor_history
predictor history flag
Definition: dcadec.c:371
static int dca_xbr_parse_frame(DCAContext *s)
Definition: dcadec.c:1732
int dynrange_coef
dynamic range coefficient
Definition: dcadec.c:405
int joint_huff[DCA_PRIM_CHANNELS_MAX]
joint subband scale factors codebook
Definition: dcadec.c:402
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:557
static const uint16_t mask[17]
Definition: lzw.c:37
#define MIX_REAR1(samples, s1, rs, coef)
Definition: dcadec.c:1177
#define DCA_PRIM_CHANNELS_MAX
Definition: dcadec.c:55
static const uint16_t bitalloc_12_codes[BITALLOC_12_COUNT][12]
Definition: dcahuff.h:50
#define FF_PROFILE_DTS
static const struct endianess table[]
int hdcd
source material is mastered in HDCD
Definition: dcadec.c:366
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
const char * r
Definition: vf_curves.c:94
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int flags
CODEC_FLAG_*.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static const uint8_t bitalloc_12_bits[BITALLOC_12_COUNT][12]
Definition: dcahuff.h:72
const char * name
Name of the codec implementation.
int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
bit allocation index
Definition: dcadec.c:399
static int dca_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Main frame decoding function FIXME add arguments.
Definition: dcadec.c:2066
#define ff_mdct_init
Definition: fft.h:147
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
#define FFMAX(a, b)
Definition: common.h:56
external API header
#define DCA_CORE_EXTS
Definition: dcadec.c:187
DCAExtensionMask
Definition: dcadec.c:161
int size
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
static const int8_t dca_lfe_index[]
Definition: dcadec.c:235
const int8_t * channel_order_tab
channel reordering table, lfe and non lfe
Definition: dcaenc.c:109
float lfe_data[2 *DCA_LFE_MAX *(DCA_BLOCKS_MAX+4)]
Low frequency effect data.
Definition: dcadec.c:409
common internal API header
static const uint8_t tmode_bits[TMODE_COUNT][4]
Definition: dcahuff.h:37
static void qmf_32_subbands(DCAContext *s, int chans, float samples_in[32][8], float *samples_out, float scale)
Definition: dcadec.c:1105
int front_sum
front sum/difference flag
Definition: dcadec.c:377
#define DCA_ABITS_MAX
Definition: dcadec.c:57
static const float dca_downmix_coeffs[65]
Definition: dcadata.h:7511
int source_pcm_res
source pcm resolution
Definition: dcadec.c:376
static int dca_xxch_decode_frame(DCAContext *s)
Definition: dcadec.c:1893
static const uint32_t map_xxch_to_native[28]
Definition: dcadec.c:130
#define AV_CH_TOP_CENTER
#define FF_PROFILE_DTS_HD_HRA
Definition: fft.h:62
int bit_rate
the average bitrate
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:58
static const int8_t dca_channel_reorder_lfe[][9]
Definition: dcadec.c:239
static const int8_t dca_channel_reorder_nolfe[][9]
Definition: dcadec.c:277
ret
Definition: avfilter.c:821
int surround_sum
surround sum/difference flag
Definition: dcadec.c:378
static av_cold void dca_init_vlcs(void)
Definition: dcadec.c:481
static const float dca_downmix_scale_factors[241]
Definition: dcadata.h:7525
av_cold void ff_synth_filter_init(SynthFilterContext *c)
Definition: synth_filter.c:59
t
Definition: genspecsines3.m:6
static const uint8_t scales_bits[SCALES_COUNT][129]
Definition: dcahuff.h:181
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:255
int joint_intensity[DCA_PRIM_CHANNELS_MAX]
joint intensity coding index
Definition: dcadec.c:387
static const int16_t adpcm_vb[4096][4]
Definition: dcadata.h:55
static const uint16_t *const bitalloc_codes[10][8]
Definition: dcahuff.h:1040
int multirate_inter
multirate interpolator switch
Definition: dcadec.c:373
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:524
static const uint8_t dca_channels[16]
Definition: dcadata.h:41
#define AV_CH_FRONT_LEFT_OF_CENTER
float u
Bit allocation.
Definition: dcadec.c:330
lossless extension in ExSS
Definition: dcadec.c:171
#define AV_CH_FRONT_CENTER
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:31
FIXME Range Coding of cr are level
Definition: snow.txt:367
int bit_rate
transmission bit rate
Definition: dcadec.c:359
static const uint8_t *const bitalloc_bits[10][8]
Definition: dcahuff.h:1058
#define INIT_VLC_USE_NEW_STATIC
Definition: get_bits.h:443
int offset
code values offset
Definition: dcadec.c:331
static const uint8_t abits_levels[7]
Definition: dcadec.c:1283
#define AV_CH_FRONT_RIGHT_OF_CENTER
int bits
Definition: get_bits.h:64
#define SCALES_VLC_BITS
Definition: dcahuff.h:82
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int table_allocated
Definition: get_bits.h:66
int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
VQ encoded high frequency subbands.
Definition: dcadec.c:407
int lfe_scale_factor
Definition: dcadec.c:410
for k
NULL
Definition: eval.c:55
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source code
#define DCA_MAX_FRAME_SIZE
Definition: dcadec.c:324
AVS_Value src
Definition: avisynth_c.h:523
int aux_data
auxiliary data flag
Definition: dcadec.c:365
int sample_rate
samples per second
int ext_coding
extended coding flag
Definition: dcadec.c:368
#define AV_CH_LAYOUT_NATIVE
Channel mask value used for AVCodecContext.request_channel_layout to indicate that the user requests ...
int subband_activity[DCA_PRIM_CHANNELS_MAX]
subband activity count
Definition: dcadec.c:385
main external API structure.
#define AV_CH_FRONT_LEFT
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define FASTDIV(a, b)
Definition: mathops.h:195
float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
scale factor adjustment
Definition: dcadec.c:392
int copy_history
copy history
Definition: dcadec.c:375
static void dca_exss_parse_header(DCAContext *s)
Parse extension substream header (HD)
Definition: dcadec.c:1949
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
Definition: dcadec.c:1286
mag
Definition: lab5.m:14
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:426
void * buf
Definition: avisynth_c.h:594
DCAXxchSpeakerMask
Definition: dcadec.c:99
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:298
VLC vlc[8]
actual codes
Definition: dcadec.c:334
double value
Definition: eval.c:82
#define AV_CH_TOP_BACK_RIGHT
core in ExSS (extension substream)
Definition: dcadec.c:166
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
static const uint64_t dca_core_channel_layout[]
Definition: dcadec.c:198
static void dca_downmix(float **samples, int srcfmt, int downmix_coef[DCA_PRIM_CHANNELS_MAX][2], const int8_t *channel_mapping)
Definition: dcadec.c:1198
static int decode_blockcodes(int code1, int code2, int levels, int *values)
Definition: dcadec.c:1275
static const float lossy_quant_d[32]
Definition: dcadata.h:4195
extended bitrate extension in ExSS
Definition: dcadec.c:167
DCAExSSSpeakerMask
Definition: dcadec.c:80
static int decode_blockcode(int code, int levels, int *values)
Definition: dcadec.c:1261
int sample_blocks
number of PCM sample blocks
Definition: dcadec.c:355
static const double coeff[2][5]
Definition: vf_ow.c:64
static const uint8_t abits_sizes[7]
Definition: dcadec.c:1282
static const int8_t dca_channel_reorder_nolfe_xch[][9]
Definition: dcadec.c:296
AVCodec ff_dca_decoder
Definition: dcadec.c:2511
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:306
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define AV_CH_BACK_CENTER
#define AV_CH_SIDE_RIGHT
int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
prediction mode (ADPCM used or not)
Definition: dcadec.c:397
static av_cold int dca_decode_init(AVCodecContext *avctx)
DCA initialization.
Definition: dcadec.c:2470
static const uint8_t dca_default_coeffs[10][5][2]
Definition: dcadata.h:7559
96/24 extension in core substream
Definition: dcadec.c:164
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
common internal and external API header
static void lfe_interpolation_fir(DCAContext *s, int decimation_select, int num_deci_sample, float *samples_in, float *samples_out, float scale)
Definition: dcadec.c:1144
#define DCA_LFE_MAX
Definition: dcadec.c:61
#define CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
#define ff_mdct_end
Definition: fft.h:148
int total_channels
number of channels including extensions
Definition: dcadec.c:383
static double c[64]
static const uint16_t scales_codes[SCALES_COUNT][129]
Definition: dcahuff.h:83
static int dca_filter_channels(DCAContext *s, int block_index)
Definition: dcadec.c:1439
int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
prediction VQ coefs
Definition: dcadec.c:398
int dialog_norm
dialog normalisation parameter
Definition: dcadec.c:379
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
AVProfile.
these buffered frames must be flushed immediately if a new input produces new output(Example:frame rate-doubling filter:filter_frame must(1) flush the second copy of the previous frame, if it is still there,(2) push the first copy of the incoming frame,(3) keep the second copy for later.) If the input frame is not enough to produce output
#define FF_PROFILE_DTS_HD_MA
Same thing on a dB scale
int bit_rate_index
transmission bit rate index
Definition: dcadec.c:360
static const uint8_t bitalloc_maxbits[10][7]
Definition: dcahuff.h:1027
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
Definition: fmtconvert.c:79
int len
int channels
number of audio channels
static const uint32_t dca_bit_rates[32]
Definition: dcadata.h:31
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int subsubframes[DCA_SUBFRAMES_MAX]
number of subsubframes
Definition: dcadec.c:395
#define DCA_MAX_EXSS_HEADER_SIZE
Definition: dcadec.c:325
static const int dca_ext_audio_descr_mask[]
Definition: dcadec.c:175
96/24 extension in ExSS
Definition: dcadec.c:169
#define DOWNMIX_TO_STEREO(op1, op2)
Definition: dcadec.c:1192
int frame_size
primary frame byte size
Definition: dcadec.c:356
#define DCA_SUBFRAMES_MAX
Definition: dcadec.c:59
#define av_uninit(x)
Definition: attributes.h:137
#define AV_CH_FRONT_RIGHT
#define AV_LOG_INFO
Definition: log.h:156
int aspf
audio sync word insertion flag
Definition: dcadec.c:369
Filter the word “frame” indicates either a video frame or a group of audio samples
#define LOCAL_ALIGNED_16(t, v,...)
#define av_always_inline
Definition: attributes.h:41
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
int prim_channels
number of primary audio channels
Definition: dcadec.c:384
#define DCA_CHSETS_MAX
Definition: dcadec.c:62
#define AV_CH_SIDE_LEFT
void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:118
static const uint8_t bitalloc_12_vlc_bits[BITALLOC_12_COUNT]
Definition: dcahuff.h:47
static int get_scale(GetBitContext *gb, int level, int value, int log2range)
Definition: dcadec.c:807
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
int partial_samples[DCA_SUBFRAMES_MAX]
partial subsubframe samples count
Definition: dcadec.c:396
This structure stores compressed data.
int subframes
number of subframes
Definition: dcadec.c:382
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
for(j=16;j >0;--j)
static const uint8_t tmode_vlc_bits[TMODE_COUNT]
Definition: dcahuff.h:30
static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
Definition: dcadec.c:342
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 This method is called when a frame is wanted on an output For an 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
#define AV_CH_BACK_RIGHT
int frame_type
type of the current frame
Definition: dcadec.c:352
static const uint8_t dca_bits_per_sample[7]
Definition: dcadata.h:46
bitstream writer API