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