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 > 2) {
739  s->lfe = 0;
740  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
741  return AVERROR_INVALIDDATA;
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  }
972  avpriv_request_sample(s->avctx, "Downmixing %d channels",
973  s->prim_channels);
974  return AVERROR_PATCHWELCOME;
975  }
976 
977  for (j = base_channel; j < s->prim_channels; j++) {
978  s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
979  s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
980  }
981  }
982  }
983 
984  /* Dynamic range coefficient */
985  if (!base_channel && s->dynrange)
986  s->dynrange_coef = get_bits(&s->gb, 8);
987 
988  /* Side information CRC check word */
989  if (s->crc_present) {
990  get_bits(&s->gb, 16);
991  }
992 
993  /*
994  * Primary audio data arrays
995  */
996 
997  /* VQ encoded high frequency subbands */
998  for (j = base_channel; j < s->prim_channels; j++)
999  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1000  /* 1 vector -> 32 samples */
1001  s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
1002 
1003  /* Low frequency effect data */
1004  if (!base_channel && s->lfe) {
1005  int quant7;
1006  /* LFE samples */
1007  int lfe_samples = 2 * s->lfe * (4 + block_index);
1008  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1009  float lfe_scale;
1010 
1011  for (j = lfe_samples; j < lfe_end_sample; j++) {
1012  /* Signed 8 bits int */
1013  s->lfe_data[j] = get_sbits(&s->gb, 8);
1014  }
1015 
1016  /* Scale factor index */
1017  quant7 = get_bits(&s->gb, 8);
1018  if (quant7 > 127) {
1019  avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
1020  return AVERROR_INVALIDDATA;
1021  }
1023 
1024  /* Quantization step size * scale factor */
1025  lfe_scale = 0.035 * s->lfe_scale_factor;
1026 
1027  for (j = lfe_samples; j < lfe_end_sample; j++)
1028  s->lfe_data[j] *= lfe_scale;
1029  }
1030 
1031 #ifdef TRACE
1032  av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1034  av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1036 
1037  for (j = base_channel; j < s->prim_channels; j++) {
1038  av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1039  for (k = 0; k < s->subband_activity[j]; k++)
1040  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1041  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1042  }
1043  for (j = base_channel; j < s->prim_channels; j++) {
1044  for (k = 0; k < s->subband_activity[j]; k++)
1046  "prediction coefs: %f, %f, %f, %f\n",
1047  (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1048  (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1049  (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1050  (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1051  }
1052  for (j = base_channel; j < s->prim_channels; j++) {
1053  av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1054  for (k = 0; k < s->vq_start_subband[j]; k++)
1055  av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[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, "Transition mode:");
1060  for (k = 0; k < s->subband_activity[j]; k++)
1061  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1062  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1063  }
1064  for (j = base_channel; j < s->prim_channels; j++) {
1065  av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1066  for (k = 0; k < s->subband_activity[j]; k++) {
1067  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1068  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1069  if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1070  av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1071  }
1072  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1073  }
1074  for (j = base_channel; j < s->prim_channels; j++) {
1075  if (s->joint_intensity[j] > 0) {
1076  int source_channel = s->joint_intensity[j] - 1;
1077  av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1078  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1079  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1080  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1081  }
1082  }
1083  if (!base_channel && s->prim_channels > 2 && s->downmix) {
1084  av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
1085  for (j = 0; j < s->prim_channels; j++) {
1086  av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
1087  dca_downmix_coeffs[s->downmix_coef[j][0]]);
1088  av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
1089  dca_downmix_coeffs[s->downmix_coef[j][1]]);
1090  }
1091  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1092  }
1093  for (j = base_channel; j < s->prim_channels; j++)
1094  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1095  av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1096  if (!base_channel && s->lfe) {
1097  int lfe_samples = 2 * s->lfe * (4 + block_index);
1098  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1099 
1100  av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1101  for (j = lfe_samples; j < lfe_end_sample; j++)
1102  av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1103  av_log(s->avctx, AV_LOG_DEBUG, "\n");
1104  }
1105 #endif
1106 
1107  return 0;
1108 }
1109 
1110 static void qmf_32_subbands(DCAContext *s, int chans,
1111  float samples_in[32][8], float *samples_out,
1112  float scale)
1113 {
1114  const float *prCoeff;
1115 
1116  int sb_act = s->subband_activity[chans];
1117 
1118  scale *= sqrt(1 / 8.0);
1119 
1120  /* Select filter */
1121  if (!s->multirate_inter) /* Non-perfect reconstruction */
1122  prCoeff = fir_32bands_nonperfect;
1123  else /* Perfect reconstruction */
1124  prCoeff = fir_32bands_perfect;
1125 
1126  s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
1127  s->subband_fir_hist[chans],
1128  &s->hist_index[chans],
1129  s->subband_fir_noidea[chans], prCoeff,
1130  samples_out, s->raXin, scale);
1131 }
1132 
1133 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1134  int num_deci_sample, float *samples_in,
1135  float *samples_out, float scale)
1136 {
1137  /* samples_in: An array holding decimated samples.
1138  * Samples in current subframe starts from samples_in[0],
1139  * while samples_in[-1], samples_in[-2], ..., stores samples
1140  * from last subframe as history.
1141  *
1142  * samples_out: An array holding interpolated samples
1143  */
1144 
1145  int decifactor;
1146  const float *prCoeff;
1147  int deciindex;
1148 
1149  /* Select decimation filter */
1150  if (decimation_select == 1) {
1151  decifactor = 64;
1152  prCoeff = lfe_fir_128;
1153  } else {
1154  decifactor = 32;
1155  prCoeff = lfe_fir_64;
1156  }
1157  /* Interpolation */
1158  for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1159  s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1160  samples_in++;
1161  samples_out += 2 * decifactor;
1162  }
1163 }
1164 
1165 /* downmixing routines */
1166 #define MIX_REAR1(samples, s1, rs, coef) \
1167  samples[0][i] += samples[s1][i] * coef[rs][0]; \
1168  samples[1][i] += samples[s1][i] * coef[rs][1];
1169 
1170 #define MIX_REAR2(samples, s1, s2, rs, coef) \
1171  samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1172  samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1173 
1174 #define MIX_FRONT3(samples, coef) \
1175  t = samples[c][i]; \
1176  u = samples[l][i]; \
1177  v = samples[r][i]; \
1178  samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1179  samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1180 
1181 #define DOWNMIX_TO_STEREO(op1, op2) \
1182  for (i = 0; i < 256; i++) { \
1183  op1 \
1184  op2 \
1185  }
1186 
1187 static void dca_downmix(float **samples, int srcfmt,
1188  int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1189  const int8_t *channel_mapping)
1190 {
1191  int c, l, r, sl, sr, s;
1192  int i;
1193  float t, u, v;
1194  float coef[DCA_PRIM_CHANNELS_MAX][2];
1195 
1196  for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1197  coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1198  coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1199  }
1200 
1201  switch (srcfmt) {
1202  case DCA_MONO:
1203  case DCA_CHANNEL:
1204  case DCA_STEREO_TOTAL:
1205  case DCA_STEREO_SUMDIFF:
1206  case DCA_4F2R:
1207  av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1208  break;
1209  case DCA_STEREO:
1210  break;
1211  case DCA_3F:
1212  c = channel_mapping[0];
1213  l = channel_mapping[1];
1214  r = channel_mapping[2];
1215  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1216  break;
1217  case DCA_2F1R:
1218  s = channel_mapping[2];
1219  DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1220  break;
1221  case DCA_3F1R:
1222  c = channel_mapping[0];
1223  l = channel_mapping[1];
1224  r = channel_mapping[2];
1225  s = channel_mapping[3];
1226  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1227  MIX_REAR1(samples, s, 3, coef));
1228  break;
1229  case DCA_2F2R:
1230  sl = channel_mapping[2];
1231  sr = channel_mapping[3];
1232  DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1233  break;
1234  case DCA_3F2R:
1235  c = channel_mapping[0];
1236  l = channel_mapping[1];
1237  r = channel_mapping[2];
1238  sl = channel_mapping[3];
1239  sr = channel_mapping[4];
1240  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1241  MIX_REAR2(samples, sl, sr, 3, coef));
1242  break;
1243  }
1244 }
1245 
1246 
1247 #ifndef decode_blockcodes
1248 /* Very compact version of the block code decoder that does not use table
1249  * look-up but is slightly slower */
1250 static int decode_blockcode(int code, int levels, int32_t *values)
1251 {
1252  int i;
1253  int offset = (levels - 1) >> 1;
1254 
1255  for (i = 0; i < 4; i++) {
1256  int div = FASTDIV(code, levels);
1257  values[i] = code - offset - div * levels;
1258  code = div;
1259  }
1260 
1261  return code;
1262 }
1263 
1264 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1265 {
1266  return decode_blockcode(code1, levels, values) |
1267  decode_blockcode(code2, levels, values + 4);
1268 }
1269 #endif
1270 
1271 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1272 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1273 
1274 #ifndef int8x8_fmul_int32
1275 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1276 {
1277  float fscale = scale / 16.0;
1278  int i;
1279  for (i = 0; i < 8; i++)
1280  dst[i] = src[i] * fscale;
1281 }
1282 #endif
1283 
1284 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1285 {
1286  int k, l;
1287  int subsubframe = s->current_subsubframe;
1288 
1289  const float *quant_step_table;
1290 
1291  /* FIXME */
1292  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1294 
1295  /*
1296  * Audio data
1297  */
1298 
1299  /* Select quantization step size table */
1300  if (s->bit_rate_index == 0x1f)
1301  quant_step_table = lossless_quant_d;
1302  else
1303  quant_step_table = lossy_quant_d;
1304 
1305  for (k = base_channel; k < s->prim_channels; k++) {
1306  float rscale[DCA_SUBBANDS];
1307 
1308  if (get_bits_left(&s->gb) < 0)
1309  return AVERROR_INVALIDDATA;
1310 
1311  for (l = 0; l < s->vq_start_subband[k]; l++) {
1312  int m;
1313 
1314  /* Select the mid-tread linear quantizer */
1315  int abits = s->bitalloc[k][l];
1316 
1317  float quant_step_size = quant_step_table[abits];
1318 
1319  /*
1320  * Determine quantization index code book and its type
1321  */
1322 
1323  /* Select quantization index code book */
1324  int sel = s->quant_index_huffman[k][abits];
1325 
1326  /*
1327  * Extract bits from the bit stream
1328  */
1329  if (!abits) {
1330  rscale[l] = 0;
1331  memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1332  } else {
1333  /* Deal with transients */
1334  int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1335  rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1336  s->scalefactor_adj[k][sel];
1337 
1338  if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1339  if (abits <= 7) {
1340  /* Block code */
1341  int block_code1, block_code2, size, levels, err;
1342 
1343  size = abits_sizes[abits - 1];
1344  levels = abits_levels[abits - 1];
1345 
1346  block_code1 = get_bits(&s->gb, size);
1347  block_code2 = get_bits(&s->gb, size);
1348  err = decode_blockcodes(block_code1, block_code2,
1349  levels, block + 8 * l);
1350  if (err) {
1352  "ERROR: block code look-up failed\n");
1353  return AVERROR_INVALIDDATA;
1354  }
1355  } else {
1356  /* no coding */
1357  for (m = 0; m < 8; m++)
1358  block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1359  }
1360  } else {
1361  /* Huffman coded */
1362  for (m = 0; m < 8; m++)
1363  block[8 * l + m] = get_bitalloc(&s->gb,
1364  &dca_smpl_bitalloc[abits], sel);
1365  }
1366 
1367  }
1368  }
1369 
1370  s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1371  block, rscale, 8 * s->vq_start_subband[k]);
1372 
1373  for (l = 0; l < s->vq_start_subband[k]; l++) {
1374  int m;
1375  /*
1376  * Inverse ADPCM if in prediction mode
1377  */
1378  if (s->prediction_mode[k][l]) {
1379  int n;
1380  for (m = 0; m < 8; m++) {
1381  for (n = 1; n <= 4; n++)
1382  if (m >= n)
1383  subband_samples[k][l][m] +=
1384  (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1385  subband_samples[k][l][m - n] / 8192);
1386  else if (s->predictor_history)
1387  subband_samples[k][l][m] +=
1388  (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1389  s->subband_samples_hist[k][l][m - n + 4] / 8192);
1390  }
1391  }
1392  }
1393 
1394  /*
1395  * Decode VQ encoded high frequencies
1396  */
1397  for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1398  /* 1 vector -> 32 samples but we only need the 8 samples
1399  * for this subsubframe. */
1400  int hfvq = s->high_freq_vq[k][l];
1401 
1402  if (!s->debug_flag & 0x01) {
1404  "Stream with high frequencies VQ coding\n");
1405  s->debug_flag |= 0x01;
1406  }
1407 
1408  int8x8_fmul_int32(subband_samples[k][l],
1409  &high_freq_vq[hfvq][subsubframe * 8],
1410  s->scale_factor[k][l][0]);
1411  }
1412  }
1413 
1414  /* Check for DSYNC after subsubframe */
1415  if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1416  if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1417 #ifdef TRACE
1418  av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1419 #endif
1420  } else {
1421  av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1422  return AVERROR_INVALIDDATA;
1423  }
1424  }
1425 
1426  /* Backup predictor history for adpcm */
1427  for (k = base_channel; k < s->prim_channels; k++)
1428  for (l = 0; l < s->vq_start_subband[k]; l++)
1429  memcpy(s->subband_samples_hist[k][l],
1430  &subband_samples[k][l][4],
1431  4 * sizeof(subband_samples[0][0][0]));
1432 
1433  return 0;
1434 }
1435 
1436 static int dca_filter_channels(DCAContext *s, int block_index)
1437 {
1438  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1439  int k;
1440 
1441  /* 32 subbands QMF */
1442  for (k = 0; k < s->prim_channels; k++) {
1443 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1444  0, 8388608.0, 8388608.0 };*/
1445  if (s->channel_order_tab[k] >= 0)
1446  qmf_32_subbands(s, k, subband_samples[k],
1448  M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1449  }
1450 
1451  /* Down mixing */
1452  if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1454  }
1455 
1456  /* Generate LFE samples for this subsubframe FIXME!!! */
1457  if (s->output & DCA_LFE) {
1458  lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1459  s->lfe_data + 2 * s->lfe * (block_index + 4),
1460  s->samples_chanptr[s->lfe_index],
1461  1.0 / (256.0 * 32768.0));
1462  /* Outputs 20bits pcm samples */
1463  }
1464 
1465  return 0;
1466 }
1467 
1468 
1469 static int dca_subframe_footer(DCAContext *s, int base_channel)
1470 {
1471  int aux_data_count = 0, i;
1472 
1473  /*
1474  * Unpack optional information
1475  */
1476 
1477  /* presumably optional information only appears in the core? */
1478  if (!base_channel) {
1479  if (s->timestamp)
1480  skip_bits_long(&s->gb, 32);
1481 
1482  if (s->aux_data)
1483  aux_data_count = get_bits(&s->gb, 6);
1484 
1485  for (i = 0; i < aux_data_count; i++)
1486  get_bits(&s->gb, 8);
1487 
1488  if (s->crc_present && (s->downmix || s->dynrange))
1489  get_bits(&s->gb, 16);
1490  }
1491 
1492  return 0;
1493 }
1494 
1495 /**
1496  * Decode a dca frame block
1497  *
1498  * @param s pointer to the DCAContext
1499  */
1500 
1501 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1502 {
1503  int ret;
1504 
1505  /* Sanity check */
1506  if (s->current_subframe >= s->subframes) {
1507  av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1508  s->current_subframe, s->subframes);
1509  return AVERROR_INVALIDDATA;
1510  }
1511 
1512  if (!s->current_subsubframe) {
1513 #ifdef TRACE
1514  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1515 #endif
1516  /* Read subframe header */
1517  if ((ret = dca_subframe_header(s, base_channel, block_index)))
1518  return ret;
1519  }
1520 
1521  /* Read subsubframe */
1522 #ifdef TRACE
1523  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1524 #endif
1525  if ((ret = dca_subsubframe(s, base_channel, block_index)))
1526  return ret;
1527 
1528  /* Update state */
1529  s->current_subsubframe++;
1531  s->current_subsubframe = 0;
1532  s->current_subframe++;
1533  }
1534  if (s->current_subframe >= s->subframes) {
1535 #ifdef TRACE
1536  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1537 #endif
1538  /* Read subframe footer */
1539  if ((ret = dca_subframe_footer(s, base_channel)))
1540  return ret;
1541  }
1542 
1543  return 0;
1544 }
1545 
1546 /**
1547  * Return the number of channels in an ExSS speaker mask (HD)
1548  */
1549 static int dca_exss_mask2count(int mask)
1550 {
1551  /* count bits that mean speaker pairs twice */
1552  return av_popcount(mask) +
1553  av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1562 }
1563 
1564 /**
1565  * Skip mixing coefficients of a single mix out configuration (HD)
1566  */
1567 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1568 {
1569  int i;
1570 
1571  for (i = 0; i < channels; i++) {
1572  int mix_map_mask = get_bits(gb, out_ch);
1573  int num_coeffs = av_popcount(mix_map_mask);
1574  skip_bits_long(gb, num_coeffs * 6);
1575  }
1576 }
1577 
1578 /**
1579  * Parse extension substream asset header (HD)
1580  */
1582 {
1583  int header_pos = get_bits_count(&s->gb);
1584  int header_size;
1585  int channels = 0;
1586  int embedded_stereo = 0;
1587  int embedded_6ch = 0;
1588  int drc_code_present;
1589  int av_uninit(extensions_mask);
1590  int i, j;
1591 
1592  if (get_bits_left(&s->gb) < 16)
1593  return -1;
1594 
1595  /* We will parse just enough to get to the extensions bitmask with which
1596  * we can set the profile value. */
1597 
1598  header_size = get_bits(&s->gb, 9) + 1;
1599  skip_bits(&s->gb, 3); // asset index
1600 
1601  if (s->static_fields) {
1602  if (get_bits1(&s->gb))
1603  skip_bits(&s->gb, 4); // asset type descriptor
1604  if (get_bits1(&s->gb))
1605  skip_bits_long(&s->gb, 24); // language descriptor
1606 
1607  if (get_bits1(&s->gb)) {
1608  /* How can one fit 1024 bytes of text here if the maximum value
1609  * for the asset header size field above was 512 bytes? */
1610  int text_length = get_bits(&s->gb, 10) + 1;
1611  if (get_bits_left(&s->gb) < text_length * 8)
1612  return -1;
1613  skip_bits_long(&s->gb, text_length * 8); // info text
1614  }
1615 
1616  skip_bits(&s->gb, 5); // bit resolution - 1
1617  skip_bits(&s->gb, 4); // max sample rate code
1618  channels = get_bits(&s->gb, 8) + 1;
1619 
1620  if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1621  int spkr_remap_sets;
1622  int spkr_mask_size = 16;
1623  int num_spkrs[7];
1624 
1625  if (channels > 2)
1626  embedded_stereo = get_bits1(&s->gb);
1627  if (channels > 6)
1628  embedded_6ch = get_bits1(&s->gb);
1629 
1630  if (get_bits1(&s->gb)) {
1631  spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1632  skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1633  }
1634 
1635  spkr_remap_sets = get_bits(&s->gb, 3);
1636 
1637  for (i = 0; i < spkr_remap_sets; i++) {
1638  /* std layout mask for each remap set */
1639  num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1640  }
1641 
1642  for (i = 0; i < spkr_remap_sets; i++) {
1643  int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1644  if (get_bits_left(&s->gb) < 0)
1645  return -1;
1646 
1647  for (j = 0; j < num_spkrs[i]; j++) {
1648  int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1649  int num_dec_ch = av_popcount(remap_dec_ch_mask);
1650  skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1651  }
1652  }
1653 
1654  } else {
1655  skip_bits(&s->gb, 3); // representation type
1656  }
1657  }
1658 
1659  drc_code_present = get_bits1(&s->gb);
1660  if (drc_code_present)
1661  get_bits(&s->gb, 8); // drc code
1662 
1663  if (get_bits1(&s->gb))
1664  skip_bits(&s->gb, 5); // dialog normalization code
1665 
1666  if (drc_code_present && embedded_stereo)
1667  get_bits(&s->gb, 8); // drc stereo code
1668 
1669  if (s->mix_metadata && get_bits1(&s->gb)) {
1670  skip_bits(&s->gb, 1); // external mix
1671  skip_bits(&s->gb, 6); // post mix gain code
1672 
1673  if (get_bits(&s->gb, 2) != 3) // mixer drc code
1674  skip_bits(&s->gb, 3); // drc limit
1675  else
1676  skip_bits(&s->gb, 8); // custom drc code
1677 
1678  if (get_bits1(&s->gb)) // channel specific scaling
1679  for (i = 0; i < s->num_mix_configs; i++)
1680  skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1681  else
1682  skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1683 
1684  for (i = 0; i < s->num_mix_configs; i++) {
1685  if (get_bits_left(&s->gb) < 0)
1686  return -1;
1687  dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1688  if (embedded_6ch)
1690  if (embedded_stereo)
1692  }
1693  }
1694 
1695  switch (get_bits(&s->gb, 2)) {
1696  case 0: extensions_mask = get_bits(&s->gb, 12); break;
1697  case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1698  case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1699  case 3: extensions_mask = 0; /* aux coding */ break;
1700  }
1701 
1702  /* not parsed further, we were only interested in the extensions mask */
1703 
1704  if (get_bits_left(&s->gb) < 0)
1705  return -1;
1706 
1707  if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1708  av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1709  return -1;
1710  }
1711  skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1712 
1713  if (extensions_mask & DCA_EXT_EXSS_XLL)
1715  else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1718 
1719  if (!(extensions_mask & DCA_EXT_CORE))
1720  av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1721  if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1723  "DTS extensions detection mismatch (%d, %d)\n",
1724  extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1725 
1726  return 0;
1727 }
1728 
1730 {
1731  int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1732  int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1733  int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1734  int anctemp[DCA_CHSET_CHANS_MAX];
1735  int chset_fsize[DCA_CHSETS_MAX];
1736  int n_xbr_ch[DCA_CHSETS_MAX];
1737  int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1738  int i, j, k, l, chset, chan_base;
1739 
1740  av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1741 
1742  /* get bit position of sync header */
1743  hdr_pos = get_bits_count(&s->gb) - 32;
1744 
1745  hdr_size = get_bits(&s->gb, 6) + 1;
1746  num_chsets = get_bits(&s->gb, 2) + 1;
1747 
1748  for(i = 0; i < num_chsets; i++)
1749  chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1750 
1751  xbr_tmode = get_bits1(&s->gb);
1752 
1753  for(i = 0; i < num_chsets; i++) {
1754  n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1755  k = get_bits(&s->gb, 2) + 5;
1756  for(j = 0; j < n_xbr_ch[i]; j++)
1757  active_bands[i][j] = get_bits(&s->gb, k) + 1;
1758  }
1759 
1760  /* skip to the end of the header */
1761  i = get_bits_count(&s->gb);
1762  if(hdr_pos + hdr_size * 8 > i)
1763  skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1764 
1765  /* loop over the channel data sets */
1766  /* only decode as many channels as we've decoded base data for */
1767  for(chset = 0, chan_base = 0;
1768  chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1769  chan_base += n_xbr_ch[chset++]) {
1770  int start_posn = get_bits_count(&s->gb);
1771  int subsubframe = 0;
1772  int subframe = 0;
1773 
1774  /* loop over subframes */
1775  for (k = 0; k < (s->sample_blocks / 8); k++) {
1776  /* parse header if we're on first subsubframe of a block */
1777  if(subsubframe == 0) {
1778  /* Parse subframe header */
1779  for(i = 0; i < n_xbr_ch[chset]; i++) {
1780  anctemp[i] = get_bits(&s->gb, 2) + 2;
1781  }
1782 
1783  for(i = 0; i < n_xbr_ch[chset]; i++) {
1784  get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1785  }
1786 
1787  for(i = 0; i < n_xbr_ch[chset]; i++) {
1788  anctemp[i] = get_bits(&s->gb, 3);
1789  if(anctemp[i] < 1) {
1790  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1791  return AVERROR_INVALIDDATA;
1792  }
1793  }
1794 
1795  /* generate scale factors */
1796  for(i = 0; i < n_xbr_ch[chset]; i++) {
1797  const uint32_t *scale_table;
1798  int nbits;
1799 
1800  if (s->scalefactor_huffman[chan_base+i] == 6) {
1801  scale_table = scale_factor_quant7;
1802  } else {
1803  scale_table = scale_factor_quant6;
1804  }
1805 
1806  nbits = anctemp[i];
1807 
1808  for(j = 0; j < active_bands[chset][i]; j++) {
1809  if(abits_high[i][j] > 0) {
1810  scale_table_high[i][j][0] =
1811  scale_table[get_bits(&s->gb, nbits)];
1812 
1813  if(xbr_tmode && s->transition_mode[i][j]) {
1814  scale_table_high[i][j][1] =
1815  scale_table[get_bits(&s->gb, nbits)];
1816  }
1817  }
1818  }
1819  }
1820  }
1821 
1822  /* decode audio array for this block */
1823  for(i = 0; i < n_xbr_ch[chset]; i++) {
1824  for(j = 0; j < active_bands[chset][i]; j++) {
1825  const int xbr_abits = abits_high[i][j];
1826  const float quant_step_size = lossless_quant_d[xbr_abits];
1827  const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1828  const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1829  float *subband_samples = s->subband_samples[k][chan_base+i][j];
1830  int block[8];
1831 
1832  if(xbr_abits <= 0)
1833  continue;
1834 
1835  if(xbr_abits > 7) {
1836  get_array(&s->gb, block, 8, xbr_abits - 3);
1837  } else {
1838  int block_code1, block_code2, size, levels, err;
1839 
1840  size = abits_sizes[xbr_abits - 1];
1841  levels = abits_levels[xbr_abits - 1];
1842 
1843  block_code1 = get_bits(&s->gb, size);
1844  block_code2 = get_bits(&s->gb, size);
1845  err = decode_blockcodes(block_code1, block_code2,
1846  levels, block);
1847  if (err) {
1849  "ERROR: DTS-XBR: block code look-up failed\n");
1850  return AVERROR_INVALIDDATA;
1851  }
1852  }
1853 
1854  /* scale & sum into subband */
1855  for(l = 0; l < 8; l++)
1856  subband_samples[l] += (float)block[l] * rscale;
1857  }
1858  }
1859 
1860  /* check DSYNC marker */
1861  if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1862  if(get_bits(&s->gb, 16) != 0xffff) {
1863  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1864  return AVERROR_INVALIDDATA;
1865  }
1866  }
1867 
1868  /* advance sub-sub-frame index */
1869  if(++subsubframe >= s->subsubframes[subframe]) {
1870  subsubframe = 0;
1871  subframe++;
1872  }
1873  }
1874 
1875  /* skip to next channel set */
1876  i = get_bits_count(&s->gb);
1877  if(start_posn + chset_fsize[chset] * 8 != i) {
1878  j = start_posn + chset_fsize[chset] * 8 - i;
1879  if(j < 0 || j >= 8)
1880  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1881  " skipping further than expected (%d bits)\n", j);
1882  skip_bits_long(&s->gb, j);
1883  }
1884  }
1885 
1886  return 0;
1887 }
1888 
1889 /* parse initial header for XXCH and dump details */
1891 {
1892  int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1893  int i, chset, base_channel, chstart, fsize[8];
1894 
1895  /* assume header word has already been parsed */
1896  hdr_pos = get_bits_count(&s->gb) - 32;
1897  hdr_size = get_bits(&s->gb, 6) + 1;
1898  /*chhdr_crc =*/ skip_bits1(&s->gb);
1899  spkmsk_bits = get_bits(&s->gb, 5) + 1;
1900  num_chsets = get_bits(&s->gb, 2) + 1;
1901 
1902  for (i = 0; i < num_chsets; i++)
1903  fsize[i] = get_bits(&s->gb, 14) + 1;
1904 
1905  core_spk = get_bits(&s->gb, spkmsk_bits);
1906  s->xxch_core_spkmask = core_spk;
1907  s->xxch_nbits_spk_mask = spkmsk_bits;
1908  s->xxch_dmix_embedded = 0;
1909 
1910  /* skip to the end of the header */
1911  i = get_bits_count(&s->gb);
1912  if (hdr_pos + hdr_size * 8 > i)
1913  skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1914 
1915  for (chset = 0; chset < num_chsets; chset++) {
1916  chstart = get_bits_count(&s->gb);
1917  base_channel = s->prim_channels;
1918  s->xxch_chset = chset;
1919 
1920  /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1921  5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1922  dca_parse_audio_coding_header(s, base_channel, 1);
1923 
1924  /* decode channel data */
1925  for (i = 0; i < (s->sample_blocks / 8); i++) {
1926  if (dca_decode_block(s, base_channel, i)) {
1928  "Error decoding DTS-XXCH extension\n");
1929  continue;
1930  }
1931  }
1932 
1933  /* skip to end of this section */
1934  i = get_bits_count(&s->gb);
1935  if (chstart + fsize[chset] * 8 > i)
1936  skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1937  }
1938  s->xxch_chset = num_chsets;
1939 
1940  return 0;
1941 }
1942 
1943 /**
1944  * Parse extension substream header (HD)
1945  */
1947 {
1948  int asset_size[8];
1949  int ss_index;
1950  int blownup;
1951  int num_audiop = 1;
1952  int num_assets = 1;
1953  int active_ss_mask[8];
1954  int i, j;
1955  int start_posn;
1956  int hdrsize;
1957  uint32_t mkr;
1958 
1959  if (get_bits_left(&s->gb) < 52)
1960  return;
1961 
1962  start_posn = get_bits_count(&s->gb) - 32;
1963 
1964  skip_bits(&s->gb, 8); // user data
1965  ss_index = get_bits(&s->gb, 2);
1966 
1967  blownup = get_bits1(&s->gb);
1968  hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
1969  skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1970 
1971  s->static_fields = get_bits1(&s->gb);
1972  if (s->static_fields) {
1973  skip_bits(&s->gb, 2); // reference clock code
1974  skip_bits(&s->gb, 3); // frame duration code
1975 
1976  if (get_bits1(&s->gb))
1977  skip_bits_long(&s->gb, 36); // timestamp
1978 
1979  /* a single stream can contain multiple audio assets that can be
1980  * combined to form multiple audio presentations */
1981 
1982  num_audiop = get_bits(&s->gb, 3) + 1;
1983  if (num_audiop > 1) {
1985  "Multiple DTS-HD audio presentations");
1986  /* ignore such streams for now */
1987  return;
1988  }
1989 
1990  num_assets = get_bits(&s->gb, 3) + 1;
1991  if (num_assets > 1) {
1992  avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
1993  /* ignore such streams for now */
1994  return;
1995  }
1996 
1997  for (i = 0; i < num_audiop; i++)
1998  active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1999 
2000  for (i = 0; i < num_audiop; i++)
2001  for (j = 0; j <= ss_index; j++)
2002  if (active_ss_mask[i] & (1 << j))
2003  skip_bits(&s->gb, 8); // active asset mask
2004 
2005  s->mix_metadata = get_bits1(&s->gb);
2006  if (s->mix_metadata) {
2007  int mix_out_mask_size;
2008 
2009  skip_bits(&s->gb, 2); // adjustment level
2010  mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2011  s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2012 
2013  for (i = 0; i < s->num_mix_configs; i++) {
2014  int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2015  s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2016  }
2017  }
2018  }
2019 
2020  for (i = 0; i < num_assets; i++)
2021  asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2022 
2023  for (i = 0; i < num_assets; i++) {
2025  return;
2026  }
2027 
2028  /* not parsed further, we were only interested in the extensions mask
2029  * from the asset header */
2030 
2031  if (num_assets > 0) {
2032  j = get_bits_count(&s->gb);
2033  if (start_posn + hdrsize * 8 > j)
2034  skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2035 
2036  for (i = 0; i < num_assets; i++) {
2037  start_posn = get_bits_count(&s->gb);
2038  mkr = get_bits_long(&s->gb, 32);
2039 
2040  /* parse extensions that we know about */
2041  if (mkr == 0x655e315e) {
2043  } else if (mkr == 0x47004a03) {
2045  s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2046  } else {
2048  "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2049  }
2050 
2051  /* skip to end of block */
2052  j = get_bits_count(&s->gb);
2053  if (start_posn + asset_size[i] * 8 > j)
2054  skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2055  }
2056  }
2057 }
2058 
2059 /**
2060  * Main frame decoding function
2061  * FIXME add arguments
2062  */
2063 static int dca_decode_frame(AVCodecContext *avctx, void *data,
2064  int *got_frame_ptr, AVPacket *avpkt)
2065 {
2066  AVFrame *frame = data;
2067  const uint8_t *buf = avpkt->data;
2068  int buf_size = avpkt->size;
2069  int channel_mask;
2070  int channel_layout;
2071  int lfe_samples;
2072  int num_core_channels = 0;
2073  int i, ret;
2074  float **samples_flt;
2075  float *src_chan;
2076  float *dst_chan;
2077  DCAContext *s = avctx->priv_data;
2078  int core_ss_end;
2079  int channels, full_channels;
2080  float scale;
2081  int achan;
2082  int chset;
2083  int mask;
2084  int lavc;
2085  int posn;
2086  int j, k;
2087  int endch;
2088 
2089  s->xch_present = 0;
2090 
2091  s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2094  av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2095  return AVERROR_INVALIDDATA;
2096  }
2097 
2098  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
2099  if ((ret = dca_parse_frame_header(s)) < 0) {
2100  //seems like the frame is corrupt, try with the next one
2101  return ret;
2102  }
2103  //set AVCodec values with parsed data
2104  avctx->sample_rate = s->sample_rate;
2105  avctx->bit_rate = s->bit_rate;
2106 
2107  s->profile = FF_PROFILE_DTS;
2108 
2109  for (i = 0; i < (s->sample_blocks / 8); i++) {
2110  if ((ret = dca_decode_block(s, 0, i))) {
2111  av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2112  return ret;
2113  }
2114  }
2115 
2116  /* record number of core channels incase less than max channels are requested */
2117  num_core_channels = s->prim_channels;
2118 
2119  if (s->ext_coding)
2121  else
2122  s->core_ext_mask = 0;
2123 
2124  core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2125 
2126  /* only scan for extensions if ext_descr was unknown or indicated a
2127  * supported XCh extension */
2128  if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2129 
2130  /* if ext_descr was unknown, clear s->core_ext_mask so that the
2131  * extensions scan can fill it up */
2132  s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2133 
2134  /* extensions start at 32-bit boundaries into bitstream */
2135  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2136 
2137  while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2138  uint32_t bits = get_bits_long(&s->gb, 32);
2139 
2140  switch (bits) {
2141  case 0x5a5a5a5a: {
2142  int ext_amode, xch_fsize;
2143 
2145 
2146  /* validate sync word using XCHFSIZE field */
2147  xch_fsize = show_bits(&s->gb, 10);
2148  if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2149  (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2150  continue;
2151 
2152  /* skip length-to-end-of-frame field for the moment */
2153  skip_bits(&s->gb, 10);
2154 
2155  s->core_ext_mask |= DCA_EXT_XCH;
2156 
2157  /* extension amode(number of channels in extension) should be 1 */
2158  /* AFAIK XCh is not used for more channels */
2159  if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2160  av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2161  " supported!\n", ext_amode);
2162  continue;
2163  }
2164 
2165  if (s->xch_base_channel < 2) {
2166  avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
2167  continue;
2168  }
2169 
2170  /* much like core primary audio coding header */
2172 
2173  for (i = 0; i < (s->sample_blocks / 8); i++)
2174  if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2175  av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2176  continue;
2177  }
2178 
2179  s->xch_present = 1;
2180  break;
2181  }
2182  case 0x47004a03:
2183  /* XXCh: extended channels */
2184  /* usually found either in core or HD part in DTS-HD HRA streams,
2185  * but not in DTS-ES which contains XCh extensions instead */
2188  break;
2189 
2190  case 0x1d95f262: {
2191  int fsize96 = show_bits(&s->gb, 12) + 1;
2192  if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2193  continue;
2194 
2195  av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2196  get_bits_count(&s->gb));
2197  skip_bits(&s->gb, 12);
2198  av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2199  av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2200 
2201  s->core_ext_mask |= DCA_EXT_X96;
2202  break;
2203  }
2204  }
2205 
2206  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2207  }
2208  } else {
2209  /* no supported extensions, skip the rest of the core substream */
2210  skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2211  }
2212 
2213  if (s->core_ext_mask & DCA_EXT_X96)
2215  else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2217 
2218  /* check for ExSS (HD part) */
2219  if (s->dca_buffer_size - s->frame_size > 32 &&
2220  get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2222 
2223  avctx->profile = s->profile;
2224 
2225  full_channels = channels = s->prim_channels + !!s->lfe;
2226 
2227  /* If we have XXCH then the channel layout is managed differently */
2228  /* note that XLL will also have another way to do things */
2229  if (!(s->core_ext_mask & DCA_EXT_XXCH)
2230  || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2231  && avctx->request_channels
2232  < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2233  { /* xxx should also do MA extensions */
2234  if (s->amode < 16) {
2236 
2237  if (s->xch_present && (!avctx->request_channels ||
2238  avctx->request_channels
2239  > num_core_channels + !!s->lfe)) {
2241  if (s->lfe) {
2244  } else {
2246  }
2247  if (s->channel_order_tab[s->xch_base_channel] < 0)
2248  return AVERROR_INVALIDDATA;
2249  } else {
2250  channels = num_core_channels + !!s->lfe;
2251  s->xch_present = 0; /* disable further xch processing */
2252  if (s->lfe) {
2255  } else
2257  }
2258 
2259  if (channels > !!s->lfe &&
2260  s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2261  return AVERROR_INVALIDDATA;
2262 
2263  if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2264  av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2265  return AVERROR_INVALIDDATA;
2266  }
2267 
2268  if (avctx->request_channels == 2 && s->prim_channels > 2) {
2269  channels = 2;
2270  s->output = DCA_STEREO;
2272  }
2273  else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2274  static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2275  s->channel_order_tab = dca_channel_order_native;
2276  }
2277  s->lfe_index = dca_lfe_index[s->amode];
2278  } else {
2279  av_log(avctx, AV_LOG_ERROR,
2280  "Non standard configuration %d !\n", s->amode);
2281  return AVERROR_INVALIDDATA;
2282  }
2283 
2284  s->xxch_dmix_embedded = 0;
2285  } else {
2286  /* we only get here if an XXCH channel set can be added to the mix */
2287  channel_mask = s->xxch_core_spkmask;
2288 
2289  if (avctx->request_channels > 0
2290  && avctx->request_channels < s->prim_channels) {
2291  channels = num_core_channels + !!s->lfe;
2292  for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2293  <= avctx->request_channels; i++) {
2294  channels += s->xxch_chset_nch[i];
2295  channel_mask |= s->xxch_spk_masks[i];
2296  }
2297  } else {
2298  channels = s->prim_channels + !!s->lfe;
2299  for (i = 0; i < s->xxch_chset; i++) {
2300  channel_mask |= s->xxch_spk_masks[i];
2301  }
2302  }
2303 
2304  /* Given the DTS spec'ed channel mask, generate an avcodec version */
2305  channel_layout = 0;
2306  for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2307  if (channel_mask & (1 << i)) {
2308  channel_layout |= map_xxch_to_native[i];
2309  }
2310  }
2311 
2312  /* make sure that we have managed to get equivelant dts/avcodec channel
2313  * masks in some sense -- unfortunately some channels could overlap */
2314  if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2315  av_log(avctx, AV_LOG_DEBUG,
2316  "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
2317  return AVERROR_INVALIDDATA;
2318  }
2319 
2320  avctx->channel_layout = channel_layout;
2321 
2322  if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2323  /* Estimate DTS --> avcodec ordering table */
2324  for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2325  mask = chset >= 0 ? s->xxch_spk_masks[chset]
2326  : s->xxch_core_spkmask;
2327  for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2328  if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2329  lavc = map_xxch_to_native[i];
2330  posn = av_popcount(channel_layout & (lavc - 1));
2331  s->xxch_order_tab[j++] = posn;
2332  }
2333  }
2334  }
2335 
2336  s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2337  } else { /* native ordering */
2338  for (i = 0; i < channels; i++)
2339  s->xxch_order_tab[i] = i;
2340 
2341  s->lfe_index = channels - 1;
2342  }
2343 
2345  }
2346 
2347  if (avctx->channels != channels) {
2348  if (avctx->channels)
2349  av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2350  avctx->channels = channels;
2351  }
2352 
2353  /* get output buffer */
2354  frame->nb_samples = 256 * (s->sample_blocks / 8);
2355  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2356  return ret;
2357  samples_flt = (float **)frame->extended_data;
2358 
2359  /* allocate buffer for extra channels if downmixing */
2360  if (avctx->channels < full_channels) {
2361  ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2362  frame->nb_samples,
2363  avctx->sample_fmt, 0);
2364  if (ret < 0)
2365  return ret;
2366 
2368  &s->extra_channels_buffer_size, ret);
2369  if (!s->extra_channels_buffer)
2370  return AVERROR(ENOMEM);
2371 
2372  ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
2374  full_channels - channels,
2375  frame->nb_samples, avctx->sample_fmt, 0);
2376  if (ret < 0)
2377  return ret;
2378  }
2379 
2380  /* filter to get final output */
2381  for (i = 0; i < (s->sample_blocks / 8); i++) {
2382  int ch;
2383 
2384  for (ch = 0; ch < channels; ch++)
2385  s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2386  for (; ch < full_channels; ch++)
2387  s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2388 
2389  dca_filter_channels(s, i);
2390 
2391  /* If this was marked as a DTS-ES stream we need to subtract back- */
2392  /* channel from SL & SR to remove matrixed back-channel signal */
2393  if ((s->source_pcm_res & 1) && s->xch_present) {
2394  float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2395  float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2396  float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
2397  s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2398  s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2399  }
2400 
2401  /* If stream contains XXCH, we might need to undo an embedded downmix */
2402  if (s->xxch_dmix_embedded) {
2403  /* Loop over channel sets in turn */
2404  ch = num_core_channels;
2405  for (chset = 0; chset < s->xxch_chset; chset++) {
2406  endch = ch + s->xxch_chset_nch[chset];
2407  mask = s->xxch_dmix_embedded;
2408 
2409  /* undo downmix */
2410  for (j = ch; j < endch; j++) {
2411  if (mask & (1 << j)) { /* this channel has been mixed-out */
2412  src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2413  for (k = 0; k < endch; k++) {
2414  achan = s->channel_order_tab[k];
2415  scale = s->xxch_dmix_coeff[j][k];
2416  if (scale != 0.0) {
2417  dst_chan = s->samples_chanptr[achan];
2418  s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2419  -scale, 256);
2420  }
2421  }
2422  }
2423  }
2424 
2425  /* if a downmix has been embedded then undo the pre-scaling */
2426  if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2427  scale = s->xxch_dmix_sf[chset];
2428 
2429  for (j = 0; j < ch; j++) {
2430  src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2431  for (k = 0; k < 256; k++)
2432  src_chan[k] *= scale;
2433  }
2434 
2435  /* LFE channel is always part of core, scale if it exists */
2436  if (s->lfe) {
2437  src_chan = s->samples_chanptr[s->lfe_index];
2438  for (k = 0; k < 256; k++)
2439  src_chan[k] *= scale;
2440  }
2441  }
2442 
2443  ch = endch;
2444  }
2445 
2446  }
2447  }
2448 
2449  /* update lfe history */
2450  lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2451  for (i = 0; i < 2 * s->lfe * 4; i++)
2452  s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2453 
2454  *got_frame_ptr = 1;
2455 
2456  return buf_size;
2457 }
2458 
2459 
2460 
2461 /**
2462  * DCA initialization
2463  *
2464  * @param avctx pointer to the AVCodecContext
2465  */
2466 
2468 {
2469  DCAContext *s = avctx->priv_data;
2470 
2471  s->avctx = avctx;
2472  dca_init_vlcs();
2473 
2475  ff_mdct_init(&s->imdct, 6, 1, 1.0);
2477  ff_dcadsp_init(&s->dcadsp);
2478  ff_fmt_convert_init(&s->fmt_conv, avctx);
2479 
2480  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2481 
2482  /* allow downmixing to stereo */
2483  if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
2484  avctx->request_channels == 2) {
2485  avctx->channels = avctx->request_channels;
2486  }
2487 
2488  return 0;
2489 }
2490 
2492 {
2493  DCAContext *s = avctx->priv_data;
2494  ff_mdct_end(&s->imdct);
2496  return 0;
2497 }
2498 
2499 static const AVProfile profiles[] = {
2500  { FF_PROFILE_DTS, "DTS" },
2501  { FF_PROFILE_DTS_ES, "DTS-ES" },
2502  { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2503  { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2504  { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2505  { FF_PROFILE_UNKNOWN },
2506 };
2507 
2509  .name = "dca",
2510  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2511  .type = AVMEDIA_TYPE_AUDIO,
2512  .id = AV_CODEC_ID_DTS,
2513  .priv_data_size = sizeof(DCAContext),
2514  .init = dca_decode_init,
2516  .close = dca_decode_end,
2517  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2518  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2520  .profiles = NULL_IF_CONFIG_SMALL(profiles),
2521 };