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