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