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