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
8 * This file is part of Libav.
10 * Libav 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.
15 * Libav 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.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/samplefmt.h"
44 #include "fmtconvert.h"
49 #include "synth_filter.h"
57 #define DCA_PRIM_CHANNELS_MAX (7)
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)
78 /* these are unconfirmed but should be mostly correct */
79 enum DCAExSSSpeakerMask {
80 DCA_EXSS_FRONT_CENTER = 0x0001,
81 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
82 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
83 DCA_EXSS_LFE = 0x0008,
84 DCA_EXSS_REAR_CENTER = 0x0010,
85 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
86 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
87 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
88 DCA_EXSS_OVERHEAD = 0x0100,
89 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
90 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
91 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
92 DCA_EXSS_LFE2 = 0x1000,
93 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
94 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
95 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
98 enum DCAExtensionMask {
99 DCA_EXT_CORE = 0x001, ///< core in core substream
100 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
101 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
102 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
103 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
104 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
105 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
106 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
107 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
108 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
111 /* -1 are reserved or unknown */
112 static const int dca_ext_audio_descr_mask[] = {
116 DCA_EXT_XCH | DCA_EXT_X96,
123 /* extensions that reside in core substream */
124 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
126 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
127 * Some compromises have been made for special configurations. Most configurations
128 * are never used so complete accuracy is not needed.
130 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
131 * S -> side, when both rear and back are configured move one of them to the side channel
133 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
135 static const uint64_t dca_core_channel_layout[] = {
136 AV_CH_FRONT_CENTER, ///< 1, A
137 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
138 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
139 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
140 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
141 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
142 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
143 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
144 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
146 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
147 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
149 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
150 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
152 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
153 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
155 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
156 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
157 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
159 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
160 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
161 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
163 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
164 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
165 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
167 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
168 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
169 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
172 static const int8_t dca_lfe_index[] = {
173 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
176 static const int8_t dca_channel_reorder_lfe[][9] = {
177 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
178 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
179 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
180 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
181 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
182 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
183 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
184 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
185 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
186 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
187 { 3, 4, 0, 1, 5, 6, -1, -1, -1 },
188 { 2, 0, 1, 4, 5, 6, -1, -1, -1 },
189 { 0, 6, 4, 5, 2, 3, -1, -1, -1 },
190 { 4, 2, 5, 0, 1, 6, 7, -1, -1 },
191 { 5, 6, 0, 1, 7, 3, 8, 4, -1 },
192 { 4, 2, 5, 0, 1, 6, 8, 7, -1 },
195 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
196 { 0, 2, -1, -1, -1, -1, -1, -1, -1 },
197 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
198 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
199 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
200 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
201 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
202 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
203 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
204 { 0, 1, 4, 5, 3, -1, -1, -1, -1 },
205 { 2, 0, 1, 5, 6, 4, -1, -1, -1 },
206 { 3, 4, 0, 1, 6, 7, 5, -1, -1 },
207 { 2, 0, 1, 4, 5, 6, 7, -1, -1 },
208 { 0, 6, 4, 5, 2, 3, 7, -1, -1 },
209 { 4, 2, 5, 0, 1, 7, 8, 6, -1 },
210 { 5, 6, 0, 1, 8, 3, 9, 4, 7 },
211 { 4, 2, 5, 0, 1, 6, 9, 8, 7 },
214 static const int8_t dca_channel_reorder_nolfe[][9] = {
215 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
216 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
217 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
218 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
219 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
220 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
221 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
222 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
223 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
224 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
225 { 2, 3, 0, 1, 4, 5, -1, -1, -1 },
226 { 2, 0, 1, 3, 4, 5, -1, -1, -1 },
227 { 0, 5, 3, 4, 1, 2, -1, -1, -1 },
228 { 3, 2, 4, 0, 1, 5, 6, -1, -1 },
229 { 4, 5, 0, 1, 6, 2, 7, 3, -1 },
230 { 3, 2, 4, 0, 1, 5, 7, 6, -1 },
233 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
234 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
235 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
236 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
237 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
238 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
239 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
240 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
241 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
242 { 0, 1, 3, 4, 2, -1, -1, -1, -1 },
243 { 2, 0, 1, 4, 5, 3, -1, -1, -1 },
244 { 2, 3, 0, 1, 5, 6, 4, -1, -1 },
245 { 2, 0, 1, 3, 4, 5, 6, -1, -1 },
246 { 0, 5, 3, 4, 1, 2, 6, -1, -1 },
247 { 3, 2, 4, 0, 1, 6, 7, 5, -1 },
248 { 4, 5, 0, 1, 7, 2, 8, 3, 6 },
249 { 3, 2, 4, 0, 1, 5, 8, 7, 6 },
252 #define DCA_DOLBY 101 /* FIXME */
254 #define DCA_CHANNEL_BITS 6
255 #define DCA_CHANNEL_MASK 0x3F
259 #define HEADER_SIZE 14
261 #define DCA_MAX_FRAME_SIZE 16384
262 #define DCA_MAX_EXSS_HEADER_SIZE 4096
264 #define DCA_BUFFER_PADDING_SIZE 1024
266 #define DCA_NSYNCAUX 0x9A1105A0
268 /** Bit allocation */
270 int offset; ///< code values offset
271 int maxbits[8]; ///< max bits in VLC
272 int wrap; ///< wrap for get_vlc2()
273 VLC vlc[8]; ///< actual codes
276 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
277 static BitAlloc dca_tmode; ///< transition mode VLCs
278 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
279 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
281 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
284 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
289 AVClass *class; ///< class for AVOptions
290 AVCodecContext *avctx;
292 int frame_type; ///< type of the current frame
293 int samples_deficit; ///< deficit sample count
294 int crc_present; ///< crc is present in the bitstream
295 int sample_blocks; ///< number of PCM sample blocks
296 int frame_size; ///< primary frame byte size
297 int amode; ///< audio channels arrangement
298 int sample_rate; ///< audio sampling rate
299 int bit_rate; ///< transmission bit rate
300 int bit_rate_index; ///< transmission bit rate index
302 int dynrange; ///< embedded dynamic range flag
303 int timestamp; ///< embedded time stamp flag
304 int aux_data; ///< auxiliary data flag
305 int hdcd; ///< source material is mastered in HDCD
306 int ext_descr; ///< extension audio descriptor flag
307 int ext_coding; ///< extended coding flag
308 int aspf; ///< audio sync word insertion flag
309 int lfe; ///< low frequency effects flag
310 int predictor_history; ///< predictor history flag
311 int header_crc; ///< header crc check bytes
312 int multirate_inter; ///< multirate interpolator switch
313 int version; ///< encoder software revision
314 int copy_history; ///< copy history
315 int source_pcm_res; ///< source pcm resolution
316 int front_sum; ///< front sum/difference flag
317 int surround_sum; ///< surround sum/difference flag
318 int dialog_norm; ///< dialog normalisation parameter
320 /* Primary audio coding header */
321 int subframes; ///< number of subframes
322 int total_channels; ///< number of channels including extensions
323 int prim_channels; ///< number of primary audio channels
324 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
325 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
326 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
327 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
328 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
329 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
330 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
331 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
333 /* Primary audio coding side information */
334 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
335 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
336 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
337 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
338 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
339 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
340 int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
341 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
342 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
343 float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
344 int dynrange_coef; ///< dynamic range coefficient
346 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
347 * Input: primary audio channels (incl. LFE if present)
348 * Output: downmix audio channels (up to 4, no LFE) */
349 uint8_t core_downmix; ///< embedded downmix coefficients available
350 uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
351 uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
353 int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
355 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
356 int lfe_scale_factor;
358 /* Subband samples history (for ADPCM) */
359 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
360 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
361 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
362 int hist_index[DCA_PRIM_CHANNELS_MAX];
363 DECLARE_ALIGNED(32, float, raXin)[32];
365 int output; ///< type of output
367 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
368 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
369 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
370 uint8_t *extra_channels_buffer;
371 unsigned int extra_channels_buffer_size;
373 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
374 int dca_buffer_size; ///< how much data is in the dca_buffer
376 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
378 /* Current position in DCA frame */
379 int current_subframe;
380 int current_subsubframe;
382 int core_ext_mask; ///< present extensions in the core substream
384 /* XCh extension information */
385 int xch_present; ///< XCh extension present and valid
386 int xch_base_channel; ///< index of first (only) channel containing XCH data
387 int xch_disable; ///< whether the XCh extension should be decoded or not
389 /* ExSS header parser */
390 int static_fields; ///< static fields present
391 int mix_metadata; ///< mixing metadata present
392 int num_mix_configs; ///< number of mix out configurations
393 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
397 int debug_flag; ///< used for suppressing repeated error messages output
398 AVFloatDSPContext fdsp;
400 SynthFilterContext synth;
401 DCADSPContext dcadsp;
402 FmtConvertContext fmt_conv;
405 static const uint16_t dca_vlc_offs[] = {
406 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
407 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
408 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
409 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
410 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
411 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
414 static av_cold void dca_init_vlcs(void)
416 static int vlcs_initialized = 0;
418 static VLC_TYPE dca_table[23622][2];
420 if (vlcs_initialized)
423 dca_bitalloc_index.offset = 1;
424 dca_bitalloc_index.wrap = 2;
425 for (i = 0; i < 5; i++) {
426 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
427 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
428 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
429 bitalloc_12_bits[i], 1, 1,
430 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
432 dca_scalefactor.offset = -64;
433 dca_scalefactor.wrap = 2;
434 for (i = 0; i < 5; i++) {
435 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
436 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
437 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
438 scales_bits[i], 1, 1,
439 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
441 dca_tmode.offset = 0;
443 for (i = 0; i < 4; i++) {
444 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
445 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
446 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
448 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
451 for (i = 0; i < 10; i++)
452 for (j = 0; j < 7; j++) {
453 if (!bitalloc_codes[i][j])
455 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
456 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
457 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
458 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
460 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
462 bitalloc_bits[i][j], 1, 1,
463 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
466 vlcs_initialized = 1;
469 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
472 *dst++ = get_bits(gb, bits);
475 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
478 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
479 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
480 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
482 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
483 s->prim_channels = s->total_channels;
485 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
486 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
488 for (i = base_channel; i < s->prim_channels; i++) {
489 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
490 if (s->subband_activity[i] > DCA_SUBBANDS)
491 s->subband_activity[i] = DCA_SUBBANDS;
493 for (i = base_channel; i < s->prim_channels; i++) {
494 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
495 if (s->vq_start_subband[i] > DCA_SUBBANDS)
496 s->vq_start_subband[i] = DCA_SUBBANDS;
498 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
499 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
500 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
501 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
503 /* Get codebooks quantization indexes */
505 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
506 for (j = 1; j < 11; j++)
507 for (i = base_channel; i < s->prim_channels; i++)
508 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
510 /* Get scale factor adjustment */
511 for (j = 0; j < 11; j++)
512 for (i = base_channel; i < s->prim_channels; i++)
513 s->scalefactor_adj[i][j] = 1;
515 for (j = 1; j < 11; j++)
516 for (i = base_channel; i < s->prim_channels; i++)
517 if (s->quant_index_huffman[i][j] < thr[j])
518 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
520 if (s->crc_present) {
521 /* Audio header CRC check */
522 get_bits(&s->gb, 16);
525 s->current_subframe = 0;
526 s->current_subsubframe = 0;
529 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
530 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
531 for (i = base_channel; i < s->prim_channels; i++) {
532 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
533 s->subband_activity[i]);
534 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
535 s->vq_start_subband[i]);
536 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
537 s->joint_intensity[i]);
538 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
539 s->transient_huffman[i]);
540 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
541 s->scalefactor_huffman[i]);
542 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
543 s->bitalloc_huffman[i]);
544 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
545 for (j = 0; j < 11; j++)
546 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
547 av_log(s->avctx, AV_LOG_DEBUG, "\n");
548 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
549 for (j = 0; j < 11; j++)
550 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
551 av_log(s->avctx, AV_LOG_DEBUG, "\n");
558 static int dca_parse_frame_header(DCAContext *s)
560 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
563 skip_bits_long(&s->gb, 32);
566 s->frame_type = get_bits(&s->gb, 1);
567 s->samples_deficit = get_bits(&s->gb, 5) + 1;
568 s->crc_present = get_bits(&s->gb, 1);
569 s->sample_blocks = get_bits(&s->gb, 7) + 1;
570 s->frame_size = get_bits(&s->gb, 14) + 1;
571 if (s->frame_size < 95)
572 return AVERROR_INVALIDDATA;
573 s->amode = get_bits(&s->gb, 6);
574 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
576 return AVERROR_INVALIDDATA;
577 s->bit_rate_index = get_bits(&s->gb, 5);
578 s->bit_rate = dca_bit_rates[s->bit_rate_index];
580 return AVERROR_INVALIDDATA;
582 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
583 s->dynrange = get_bits(&s->gb, 1);
584 s->timestamp = get_bits(&s->gb, 1);
585 s->aux_data = get_bits(&s->gb, 1);
586 s->hdcd = get_bits(&s->gb, 1);
587 s->ext_descr = get_bits(&s->gb, 3);
588 s->ext_coding = get_bits(&s->gb, 1);
589 s->aspf = get_bits(&s->gb, 1);
590 s->lfe = get_bits(&s->gb, 2);
591 s->predictor_history = get_bits(&s->gb, 1);
594 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
595 return AVERROR_INVALIDDATA;
598 /* TODO: check CRC */
600 s->header_crc = get_bits(&s->gb, 16);
602 s->multirate_inter = get_bits(&s->gb, 1);
603 s->version = get_bits(&s->gb, 4);
604 s->copy_history = get_bits(&s->gb, 2);
605 s->source_pcm_res = get_bits(&s->gb, 3);
606 s->front_sum = get_bits(&s->gb, 1);
607 s->surround_sum = get_bits(&s->gb, 1);
608 s->dialog_norm = get_bits(&s->gb, 4);
610 /* FIXME: channels mixing levels */
611 s->output = s->amode;
613 s->output |= DCA_LFE;
616 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
617 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
618 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
619 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
620 s->sample_blocks, s->sample_blocks * 32);
621 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
622 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
623 s->amode, dca_channels[s->amode]);
624 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
626 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
628 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
629 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
630 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
631 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
632 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
633 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
634 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
635 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
636 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
637 s->predictor_history);
638 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
639 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
641 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
642 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
643 av_log(s->avctx, AV_LOG_DEBUG,
644 "source pcm resolution: %i (%i bits/sample)\n",
645 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
646 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
647 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
648 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
649 av_log(s->avctx, AV_LOG_DEBUG, "\n");
652 /* Primary audio coding header */
653 s->subframes = get_bits(&s->gb, 4) + 1;
655 return dca_parse_audio_coding_header(s, 0);
658 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
661 /* huffman encoded */
662 value += get_bitalloc(gb, &dca_scalefactor, level);
663 value = av_clip(value, 0, (1 << log2range) - 1);
664 } else if (level < 8) {
665 if (level + 1 > log2range) {
666 skip_bits(gb, level + 1 - log2range);
667 value = get_bits(gb, log2range);
669 value = get_bits(gb, level + 1);
675 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
677 /* Primary audio coding side information */
680 if (get_bits_left(&s->gb) < 0)
681 return AVERROR_INVALIDDATA;
684 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
685 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
688 for (j = base_channel; j < s->prim_channels; j++) {
689 for (k = 0; k < s->subband_activity[j]; k++)
690 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
693 /* Get prediction codebook */
694 for (j = base_channel; j < s->prim_channels; j++) {
695 for (k = 0; k < s->subband_activity[j]; k++) {
696 if (s->prediction_mode[j][k] > 0) {
697 /* (Prediction coefficient VQ address) */
698 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
703 /* Bit allocation index */
704 for (j = base_channel; j < s->prim_channels; j++) {
705 for (k = 0; k < s->vq_start_subband[j]; k++) {
706 if (s->bitalloc_huffman[j] == 6)
707 s->bitalloc[j][k] = get_bits(&s->gb, 5);
708 else if (s->bitalloc_huffman[j] == 5)
709 s->bitalloc[j][k] = get_bits(&s->gb, 4);
710 else if (s->bitalloc_huffman[j] == 7) {
711 av_log(s->avctx, AV_LOG_ERROR,
712 "Invalid bit allocation index\n");
713 return AVERROR_INVALIDDATA;
716 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
719 if (s->bitalloc[j][k] > 26) {
720 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
721 j, k, s->bitalloc[j][k]);
722 return AVERROR_INVALIDDATA;
727 /* Transition mode */
728 for (j = base_channel; j < s->prim_channels; j++) {
729 for (k = 0; k < s->subband_activity[j]; k++) {
730 s->transition_mode[j][k] = 0;
731 if (s->subsubframes[s->current_subframe] > 1 &&
732 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
733 s->transition_mode[j][k] =
734 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
739 if (get_bits_left(&s->gb) < 0)
740 return AVERROR_INVALIDDATA;
742 for (j = base_channel; j < s->prim_channels; j++) {
743 const uint32_t *scale_table;
744 int scale_sum, log_size;
746 memset(s->scale_factor[j], 0,
747 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
749 if (s->scalefactor_huffman[j] == 6) {
750 scale_table = scale_factor_quant7;
753 scale_table = scale_factor_quant6;
757 /* When huffman coded, only the difference is encoded */
760 for (k = 0; k < s->subband_activity[j]; k++) {
761 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
762 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
763 s->scale_factor[j][k][0] = scale_table[scale_sum];
766 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
767 /* Get second scale factor */
768 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
769 s->scale_factor[j][k][1] = scale_table[scale_sum];
774 /* Joint subband scale factor codebook select */
775 for (j = base_channel; j < s->prim_channels; j++) {
776 /* Transmitted only if joint subband coding enabled */
777 if (s->joint_intensity[j] > 0)
778 s->joint_huff[j] = get_bits(&s->gb, 3);
781 if (get_bits_left(&s->gb) < 0)
782 return AVERROR_INVALIDDATA;
784 /* Scale factors for joint subband coding */
785 for (j = base_channel; j < s->prim_channels; j++) {
788 /* Transmitted only if joint subband coding enabled */
789 if (s->joint_intensity[j] > 0) {
791 source_channel = s->joint_intensity[j] - 1;
793 /* When huffman coded, only the difference is encoded
794 * (is this valid as well for joint scales ???) */
796 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
797 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
798 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
801 if (!(s->debug_flag & 0x02)) {
802 av_log(s->avctx, AV_LOG_DEBUG,
803 "Joint stereo coding not supported\n");
804 s->debug_flag |= 0x02;
809 /* Dynamic range coefficient */
810 if (!base_channel && s->dynrange)
811 s->dynrange_coef = get_bits(&s->gb, 8);
813 /* Side information CRC check word */
814 if (s->crc_present) {
815 get_bits(&s->gb, 16);
819 * Primary audio data arrays
822 /* VQ encoded high frequency subbands */
823 for (j = base_channel; j < s->prim_channels; j++)
824 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
825 /* 1 vector -> 32 samples */
826 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
828 /* Low frequency effect data */
829 if (!base_channel && s->lfe) {
831 int lfe_samples = 2 * s->lfe * (4 + block_index);
832 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
835 for (j = lfe_samples; j < lfe_end_sample; j++) {
836 /* Signed 8 bits int */
837 s->lfe_data[j] = get_sbits(&s->gb, 8);
840 /* Scale factor index */
841 skip_bits(&s->gb, 1);
842 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 7)];
844 /* Quantization step size * scale factor */
845 lfe_scale = 0.035 * s->lfe_scale_factor;
847 for (j = lfe_samples; j < lfe_end_sample; j++)
848 s->lfe_data[j] *= lfe_scale;
852 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
853 s->subsubframes[s->current_subframe]);
854 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
855 s->partial_samples[s->current_subframe]);
857 for (j = base_channel; j < s->prim_channels; j++) {
858 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
859 for (k = 0; k < s->subband_activity[j]; k++)
860 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
861 av_log(s->avctx, AV_LOG_DEBUG, "\n");
863 for (j = base_channel; j < s->prim_channels; j++) {
864 for (k = 0; k < s->subband_activity[j]; k++)
865 av_log(s->avctx, AV_LOG_DEBUG,
866 "prediction coefs: %f, %f, %f, %f\n",
867 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
868 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
869 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
870 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
872 for (j = base_channel; j < s->prim_channels; j++) {
873 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
874 for (k = 0; k < s->vq_start_subband[j]; k++)
875 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
876 av_log(s->avctx, AV_LOG_DEBUG, "\n");
878 for (j = base_channel; j < s->prim_channels; j++) {
879 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
880 for (k = 0; k < s->subband_activity[j]; k++)
881 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
882 av_log(s->avctx, AV_LOG_DEBUG, "\n");
884 for (j = base_channel; j < s->prim_channels; j++) {
885 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
886 for (k = 0; k < s->subband_activity[j]; k++) {
887 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
888 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
889 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
890 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
892 av_log(s->avctx, AV_LOG_DEBUG, "\n");
894 for (j = base_channel; j < s->prim_channels; j++) {
895 if (s->joint_intensity[j] > 0) {
896 int source_channel = s->joint_intensity[j] - 1;
897 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
898 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
899 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
900 av_log(s->avctx, AV_LOG_DEBUG, "\n");
903 for (j = base_channel; j < s->prim_channels; j++)
904 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
905 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
906 if (!base_channel && s->lfe) {
907 int lfe_samples = 2 * s->lfe * (4 + block_index);
908 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
910 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
911 for (j = lfe_samples; j < lfe_end_sample; j++)
912 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
913 av_log(s->avctx, AV_LOG_DEBUG, "\n");
920 static void qmf_32_subbands(DCAContext *s, int chans,
921 float samples_in[32][8], float *samples_out,
924 const float *prCoeff;
926 int sb_act = s->subband_activity[chans];
928 scale *= sqrt(1 / 8.0);
931 if (!s->multirate_inter) /* Non-perfect reconstruction */
932 prCoeff = fir_32bands_nonperfect;
933 else /* Perfect reconstruction */
934 prCoeff = fir_32bands_perfect;
936 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
937 s->subband_fir_hist[chans],
938 &s->hist_index[chans],
939 s->subband_fir_noidea[chans], prCoeff,
940 samples_out, s->raXin, scale);
943 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
944 int num_deci_sample, float *samples_in,
947 /* samples_in: An array holding decimated samples.
948 * Samples in current subframe starts from samples_in[0],
949 * while samples_in[-1], samples_in[-2], ..., stores samples
950 * from last subframe as history.
952 * samples_out: An array holding interpolated samples
956 const float *prCoeff;
959 /* Select decimation filter */
960 if (decimation_select == 1) {
962 prCoeff = lfe_fir_128;
965 prCoeff = lfe_fir_64;
968 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
969 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
971 samples_out += 2 * 32 * (1 + idx);
975 /* downmixing routines */
976 #define MIX_REAR1(samples, s1, rs, coef) \
977 samples[0][i] += samples[s1][i] * coef[rs][0]; \
978 samples[1][i] += samples[s1][i] * coef[rs][1];
980 #define MIX_REAR2(samples, s1, s2, rs, coef) \
981 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
982 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
984 #define MIX_FRONT3(samples, coef) \
988 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
989 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
991 #define DOWNMIX_TO_STEREO(op1, op2) \
992 for (i = 0; i < 256; i++) { \
997 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
998 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
999 const int8_t *channel_mapping)
1001 int c, l, r, sl, sr, s;
1008 av_log(NULL, 0, "Not implemented!\n");
1012 case DCA_STEREO_TOTAL:
1013 case DCA_STEREO_SUMDIFF:
1016 c = channel_mapping[0];
1017 l = channel_mapping[1];
1018 r = channel_mapping[2];
1019 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1022 s = channel_mapping[2];
1023 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1026 c = channel_mapping[0];
1027 l = channel_mapping[1];
1028 r = channel_mapping[2];
1029 s = channel_mapping[3];
1030 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1031 MIX_REAR1(samples, s, 3, coef));
1034 sl = channel_mapping[2];
1035 sr = channel_mapping[3];
1036 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1039 c = channel_mapping[0];
1040 l = channel_mapping[1];
1041 r = channel_mapping[2];
1042 sl = channel_mapping[3];
1043 sr = channel_mapping[4];
1044 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1045 MIX_REAR2(samples, sl, sr, 3, coef));
1049 int lf_buf = dca_lfe_index[srcfmt];
1050 int lf_idx = dca_channels[srcfmt];
1051 for (i = 0; i < 256; i++) {
1052 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1053 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1058 #ifndef decode_blockcodes
1059 /* Very compact version of the block code decoder that does not use table
1060 * look-up but is slightly slower */
1061 static int decode_blockcode(int code, int levels, int32_t *values)
1064 int offset = (levels - 1) >> 1;
1066 for (i = 0; i < 4; i++) {
1067 int div = FASTDIV(code, levels);
1068 values[i] = code - offset - div * levels;
1075 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1077 return decode_blockcode(code1, levels, values) |
1078 decode_blockcode(code2, levels, values + 4);
1082 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1083 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1085 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1088 int subsubframe = s->current_subsubframe;
1090 const float *quant_step_table;
1093 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1094 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
1100 /* Select quantization step size table */
1101 if (s->bit_rate_index == 0x1f)
1102 quant_step_table = lossless_quant_d;
1104 quant_step_table = lossy_quant_d;
1106 for (k = base_channel; k < s->prim_channels; k++) {
1107 float rscale[DCA_SUBBANDS];
1109 if (get_bits_left(&s->gb) < 0)
1110 return AVERROR_INVALIDDATA;
1112 for (l = 0; l < s->vq_start_subband[k]; l++) {
1115 /* Select the mid-tread linear quantizer */
1116 int abits = s->bitalloc[k][l];
1118 float quant_step_size = quant_step_table[abits];
1121 * Determine quantization index code book and its type
1124 /* Select quantization index code book */
1125 int sel = s->quant_index_huffman[k][abits];
1128 * Extract bits from the bit stream
1132 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1134 /* Deal with transients */
1135 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1136 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1137 s->scalefactor_adj[k][sel];
1139 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1142 int block_code1, block_code2, size, levels, err;
1144 size = abits_sizes[abits - 1];
1145 levels = abits_levels[abits - 1];
1147 block_code1 = get_bits(&s->gb, size);
1148 block_code2 = get_bits(&s->gb, size);
1149 err = decode_blockcodes(block_code1, block_code2,
1150 levels, block + 8 * l);
1152 av_log(s->avctx, AV_LOG_ERROR,
1153 "ERROR: block code look-up failed\n");
1154 return AVERROR_INVALIDDATA;
1158 for (m = 0; m < 8; m++)
1159 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1163 for (m = 0; m < 8; m++)
1164 block[8 * l + m] = get_bitalloc(&s->gb,
1165 &dca_smpl_bitalloc[abits], sel);
1170 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1171 block, rscale, 8 * s->vq_start_subband[k]);
1173 for (l = 0; l < s->vq_start_subband[k]; l++) {
1176 * Inverse ADPCM if in prediction mode
1178 if (s->prediction_mode[k][l]) {
1180 if (s->predictor_history)
1181 subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
1182 s->subband_samples_hist[k][l][3] +
1183 adpcm_vb[s->prediction_vq[k][l]][1] *
1184 s->subband_samples_hist[k][l][2] +
1185 adpcm_vb[s->prediction_vq[k][l]][2] *
1186 s->subband_samples_hist[k][l][1] +
1187 adpcm_vb[s->prediction_vq[k][l]][3] *
1188 s->subband_samples_hist[k][l][0]) *
1190 for (m = 1; m < 8; m++) {
1191 float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
1192 subband_samples[k][l][m - 1];
1193 for (n = 2; n <= 4; n++)
1195 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1196 subband_samples[k][l][m - n];
1197 else if (s->predictor_history)
1198 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1199 s->subband_samples_hist[k][l][m - n + 4];
1200 subband_samples[k][l][m] += sum * 1.0f / 8192;
1206 * Decode VQ encoded high frequencies
1208 if (s->subband_activity[k] > s->vq_start_subband[k]) {
1209 if (!s->debug_flag & 0x01) {
1210 av_log(s->avctx, AV_LOG_DEBUG,
1211 "Stream with high frequencies VQ coding\n");
1212 s->debug_flag |= 0x01;
1214 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
1215 high_freq_vq, subsubframe * 8,
1216 s->scale_factor[k], s->vq_start_subband[k],
1217 s->subband_activity[k]);
1221 /* Check for DSYNC after subsubframe */
1222 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1223 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1225 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1228 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1229 return AVERROR_INVALIDDATA;
1233 /* Backup predictor history for adpcm */
1234 for (k = base_channel; k < s->prim_channels; k++)
1235 for (l = 0; l < s->vq_start_subband[k]; l++)
1236 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1241 static int dca_filter_channels(DCAContext *s, int block_index)
1243 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1246 /* 32 subbands QMF */
1247 for (k = 0; k < s->prim_channels; k++) {
1248 if (s->channel_order_tab[k] >= 0)
1249 qmf_32_subbands(s, k, subband_samples[k],
1250 s->samples_chanptr[s->channel_order_tab[k]],
1251 M_SQRT1_2 / 32768.0);
1254 /* Generate LFE samples for this subsubframe FIXME!!! */
1256 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1257 s->lfe_data + 2 * s->lfe * (block_index + 4),
1258 s->samples_chanptr[dca_lfe_index[s->amode]]);
1259 /* Outputs 20bits pcm samples */
1262 /* Downmixing to Stereo */
1263 if (s->prim_channels + !!s->lfe > 2 &&
1264 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1265 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1266 s->channel_order_tab);
1272 static int dca_subframe_footer(DCAContext *s, int base_channel)
1274 int in, out, aux_data_count, aux_data_end, reserved;
1278 * Unpack optional information
1281 /* presumably optional information only appears in the core? */
1282 if (!base_channel) {
1284 skip_bits_long(&s->gb, 32);
1287 aux_data_count = get_bits(&s->gb, 6);
1290 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1292 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1294 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1295 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1297 return AVERROR_INVALIDDATA;
1300 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1301 avpriv_request_sample(s->avctx,
1302 "Auxiliary Decode Time Stamp Flag");
1304 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1305 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1306 skip_bits_long(&s->gb, 44);
1309 if ((s->core_downmix = get_bits1(&s->gb))) {
1310 int am = get_bits(&s->gb, 3);
1313 s->core_downmix_amode = DCA_MONO;
1316 s->core_downmix_amode = DCA_STEREO;
1319 s->core_downmix_amode = DCA_STEREO_TOTAL;
1322 s->core_downmix_amode = DCA_3F;
1325 s->core_downmix_amode = DCA_2F1R;
1328 s->core_downmix_amode = DCA_2F2R;
1331 s->core_downmix_amode = DCA_3F1R;
1334 av_log(s->avctx, AV_LOG_ERROR,
1335 "Invalid mode %d for embedded downmix coefficients\n",
1337 return AVERROR_INVALIDDATA;
1339 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1340 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1341 uint16_t tmp = get_bits(&s->gb, 9);
1342 if ((tmp & 0xFF) > 241) {
1343 av_log(s->avctx, AV_LOG_ERROR,
1344 "Invalid downmix coefficient code %"PRIu16"\n",
1346 return AVERROR_INVALIDDATA;
1348 s->core_downmix_codes[in][out] = tmp;
1353 align_get_bits(&s->gb); // byte align
1354 skip_bits(&s->gb, 16); // nAUXCRC16
1356 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1357 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1358 av_log(s->avctx, AV_LOG_ERROR,
1359 "Overread auxiliary data by %d bits\n", -reserved);
1360 return AVERROR_INVALIDDATA;
1361 } else if (reserved) {
1362 avpriv_request_sample(s->avctx,
1363 "Core auxiliary data reserved content");
1364 skip_bits_long(&s->gb, reserved);
1368 if (s->crc_present && s->dynrange)
1369 get_bits(&s->gb, 16);
1376 * Decode a dca frame block
1378 * @param s pointer to the DCAContext
1381 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1386 if (s->current_subframe >= s->subframes) {
1387 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1388 s->current_subframe, s->subframes);
1389 return AVERROR_INVALIDDATA;
1392 if (!s->current_subsubframe) {
1394 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1396 /* Read subframe header */
1397 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1401 /* Read subsubframe */
1403 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1405 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1409 s->current_subsubframe++;
1410 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1411 s->current_subsubframe = 0;
1412 s->current_subframe++;
1414 if (s->current_subframe >= s->subframes) {
1416 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1418 /* Read subframe footer */
1419 if ((ret = dca_subframe_footer(s, base_channel)))
1427 * Return the number of channels in an ExSS speaker mask (HD)
1429 static int dca_exss_mask2count(int mask)
1431 /* count bits that mean speaker pairs twice */
1432 return av_popcount(mask) +
1433 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1434 DCA_EXSS_FRONT_LEFT_RIGHT |
1435 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1436 DCA_EXSS_WIDE_LEFT_RIGHT |
1437 DCA_EXSS_SIDE_LEFT_RIGHT |
1438 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1439 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1440 DCA_EXSS_REAR_LEFT_RIGHT |
1441 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1445 * Skip mixing coefficients of a single mix out configuration (HD)
1447 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1451 for (i = 0; i < channels; i++) {
1452 int mix_map_mask = get_bits(gb, out_ch);
1453 int num_coeffs = av_popcount(mix_map_mask);
1454 skip_bits_long(gb, num_coeffs * 6);
1459 * Parse extension substream asset header (HD)
1461 static int dca_exss_parse_asset_header(DCAContext *s)
1463 int header_pos = get_bits_count(&s->gb);
1466 int embedded_stereo = 0;
1467 int embedded_6ch = 0;
1468 int drc_code_present;
1469 int extensions_mask;
1472 if (get_bits_left(&s->gb) < 16)
1475 /* We will parse just enough to get to the extensions bitmask with which
1476 * we can set the profile value. */
1478 header_size = get_bits(&s->gb, 9) + 1;
1479 skip_bits(&s->gb, 3); // asset index
1481 if (s->static_fields) {
1482 if (get_bits1(&s->gb))
1483 skip_bits(&s->gb, 4); // asset type descriptor
1484 if (get_bits1(&s->gb))
1485 skip_bits_long(&s->gb, 24); // language descriptor
1487 if (get_bits1(&s->gb)) {
1488 /* How can one fit 1024 bytes of text here if the maximum value
1489 * for the asset header size field above was 512 bytes? */
1490 int text_length = get_bits(&s->gb, 10) + 1;
1491 if (get_bits_left(&s->gb) < text_length * 8)
1493 skip_bits_long(&s->gb, text_length * 8); // info text
1496 skip_bits(&s->gb, 5); // bit resolution - 1
1497 skip_bits(&s->gb, 4); // max sample rate code
1498 channels = get_bits(&s->gb, 8) + 1;
1500 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1501 int spkr_remap_sets;
1502 int spkr_mask_size = 16;
1506 embedded_stereo = get_bits1(&s->gb);
1508 embedded_6ch = get_bits1(&s->gb);
1510 if (get_bits1(&s->gb)) {
1511 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1512 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1515 spkr_remap_sets = get_bits(&s->gb, 3);
1517 for (i = 0; i < spkr_remap_sets; i++) {
1518 /* std layout mask for each remap set */
1519 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1522 for (i = 0; i < spkr_remap_sets; i++) {
1523 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1524 if (get_bits_left(&s->gb) < 0)
1527 for (j = 0; j < num_spkrs[i]; j++) {
1528 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1529 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1530 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1534 skip_bits(&s->gb, 3); // representation type
1538 drc_code_present = get_bits1(&s->gb);
1539 if (drc_code_present)
1540 get_bits(&s->gb, 8); // drc code
1542 if (get_bits1(&s->gb))
1543 skip_bits(&s->gb, 5); // dialog normalization code
1545 if (drc_code_present && embedded_stereo)
1546 get_bits(&s->gb, 8); // drc stereo code
1548 if (s->mix_metadata && get_bits1(&s->gb)) {
1549 skip_bits(&s->gb, 1); // external mix
1550 skip_bits(&s->gb, 6); // post mix gain code
1552 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1553 skip_bits(&s->gb, 3); // drc limit
1555 skip_bits(&s->gb, 8); // custom drc code
1557 if (get_bits1(&s->gb)) // channel specific scaling
1558 for (i = 0; i < s->num_mix_configs; i++)
1559 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1561 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1563 for (i = 0; i < s->num_mix_configs; i++) {
1564 if (get_bits_left(&s->gb) < 0)
1566 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1568 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1569 if (embedded_stereo)
1570 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1574 switch (get_bits(&s->gb, 2)) {
1576 extensions_mask = get_bits(&s->gb, 12);
1579 extensions_mask = DCA_EXT_EXSS_XLL;
1582 extensions_mask = DCA_EXT_EXSS_LBR;
1585 extensions_mask = 0; /* aux coding */
1589 /* not parsed further, we were only interested in the extensions mask */
1591 if (get_bits_left(&s->gb) < 0)
1594 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1595 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1598 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1600 if (extensions_mask & DCA_EXT_EXSS_XLL)
1601 s->profile = FF_PROFILE_DTS_HD_MA;
1602 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1604 s->profile = FF_PROFILE_DTS_HD_HRA;
1606 if (!(extensions_mask & DCA_EXT_CORE))
1607 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1608 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1609 av_log(s->avctx, AV_LOG_WARNING,
1610 "DTS extensions detection mismatch (%d, %d)\n",
1611 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1617 * Parse extension substream header (HD)
1619 static void dca_exss_parse_header(DCAContext *s)
1625 int active_ss_mask[8];
1628 if (get_bits_left(&s->gb) < 52)
1631 skip_bits(&s->gb, 8); // user data
1632 ss_index = get_bits(&s->gb, 2);
1634 blownup = get_bits1(&s->gb);
1635 skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1636 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1638 s->static_fields = get_bits1(&s->gb);
1639 if (s->static_fields) {
1640 skip_bits(&s->gb, 2); // reference clock code
1641 skip_bits(&s->gb, 3); // frame duration code
1643 if (get_bits1(&s->gb))
1644 skip_bits_long(&s->gb, 36); // timestamp
1646 /* a single stream can contain multiple audio assets that can be
1647 * combined to form multiple audio presentations */
1649 num_audiop = get_bits(&s->gb, 3) + 1;
1650 if (num_audiop > 1) {
1651 avpriv_request_sample(s->avctx,
1652 "Multiple DTS-HD audio presentations");
1653 /* ignore such streams for now */
1657 num_assets = get_bits(&s->gb, 3) + 1;
1658 if (num_assets > 1) {
1659 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
1660 /* ignore such streams for now */
1664 for (i = 0; i < num_audiop; i++)
1665 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1667 for (i = 0; i < num_audiop; i++)
1668 for (j = 0; j <= ss_index; j++)
1669 if (active_ss_mask[i] & (1 << j))
1670 skip_bits(&s->gb, 8); // active asset mask
1672 s->mix_metadata = get_bits1(&s->gb);
1673 if (s->mix_metadata) {
1674 int mix_out_mask_size;
1676 skip_bits(&s->gb, 2); // adjustment level
1677 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1678 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
1680 for (i = 0; i < s->num_mix_configs; i++) {
1681 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
1682 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
1687 for (i = 0; i < num_assets; i++)
1688 skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
1690 for (i = 0; i < num_assets; i++) {
1691 if (dca_exss_parse_asset_header(s))
1695 /* not parsed further, we were only interested in the extensions mask
1696 * from the asset header */
1699 static float dca_dmix_code(unsigned code)
1701 int sign = (code >> 8) - 1;
1703 return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
1707 * Main frame decoding function
1708 * FIXME add arguments
1710 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1711 int *got_frame_ptr, AVPacket *avpkt)
1713 AVFrame *frame = data;
1714 const uint8_t *buf = avpkt->data;
1715 int buf_size = avpkt->size;
1718 int num_core_channels = 0;
1720 float **samples_flt;
1721 DCAContext *s = avctx->priv_data;
1722 int channels, full_channels;
1727 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1728 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1729 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1730 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1731 return AVERROR_INVALIDDATA;
1734 if ((ret = dca_parse_frame_header(s)) < 0) {
1735 // seems like the frame is corrupt, try with the next one
1738 // set AVCodec values with parsed data
1739 avctx->sample_rate = s->sample_rate;
1740 avctx->bit_rate = s->bit_rate;
1742 s->profile = FF_PROFILE_DTS;
1744 for (i = 0; i < (s->sample_blocks / 8); i++) {
1745 if ((ret = dca_decode_block(s, 0, i))) {
1746 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1751 /* record number of core channels incase less than max channels are requested */
1752 num_core_channels = s->prim_channels;
1755 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1757 s->core_ext_mask = 0;
1759 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1761 /* only scan for extensions if ext_descr was unknown or indicated a
1762 * supported XCh extension */
1763 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1764 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1765 * extensions scan can fill it up */
1766 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1768 /* extensions start at 32-bit boundaries into bitstream */
1769 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1771 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1772 uint32_t bits = get_bits_long(&s->gb, 32);
1776 int ext_amode, xch_fsize;
1778 s->xch_base_channel = s->prim_channels;
1780 /* validate sync word using XCHFSIZE field */
1781 xch_fsize = show_bits(&s->gb, 10);
1782 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1783 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1786 /* skip length-to-end-of-frame field for the moment */
1787 skip_bits(&s->gb, 10);
1789 s->core_ext_mask |= DCA_EXT_XCH;
1791 /* extension amode(number of channels in extension) should be 1 */
1792 /* AFAIK XCh is not used for more channels */
1793 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1794 av_log(avctx, AV_LOG_ERROR,
1795 "XCh extension amode %d not supported!\n",
1800 /* much like core primary audio coding header */
1801 dca_parse_audio_coding_header(s, s->xch_base_channel);
1803 for (i = 0; i < (s->sample_blocks / 8); i++)
1804 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1805 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1813 /* XXCh: extended channels */
1814 /* usually found either in core or HD part in DTS-HD HRA streams,
1815 * but not in DTS-ES which contains XCh extensions instead */
1816 s->core_ext_mask |= DCA_EXT_XXCH;
1820 int fsize96 = show_bits(&s->gb, 12) + 1;
1821 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1824 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1825 get_bits_count(&s->gb));
1826 skip_bits(&s->gb, 12);
1827 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1828 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1830 s->core_ext_mask |= DCA_EXT_X96;
1835 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1838 /* no supported extensions, skip the rest of the core substream */
1839 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1842 if (s->core_ext_mask & DCA_EXT_X96)
1843 s->profile = FF_PROFILE_DTS_96_24;
1844 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1845 s->profile = FF_PROFILE_DTS_ES;
1847 /* check for ExSS (HD part) */
1848 if (s->dca_buffer_size - s->frame_size > 32 &&
1849 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1850 dca_exss_parse_header(s);
1852 avctx->profile = s->profile;
1854 full_channels = channels = s->prim_channels + !!s->lfe;
1856 if (s->amode < 16) {
1857 avctx->channel_layout = dca_core_channel_layout[s->amode];
1859 if (s->prim_channels + !!s->lfe > 2 &&
1860 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1862 * Neither the core's auxiliary data nor our default tables contain
1863 * downmix coefficients for the additional channel coded in the XCh
1864 * extension, so when we're doing a Stereo downmix, don't decode it.
1869 #if FF_API_REQUEST_CHANNELS
1870 FF_DISABLE_DEPRECATION_WARNINGS
1871 if (s->xch_present && !s->xch_disable &&
1872 (!avctx->request_channels ||
1873 avctx->request_channels > num_core_channels + !!s->lfe)) {
1874 FF_ENABLE_DEPRECATION_WARNINGS
1876 if (s->xch_present && !s->xch_disable) {
1878 avctx->channel_layout |= AV_CH_BACK_CENTER;
1880 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1881 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1883 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1886 channels = num_core_channels + !!s->lfe;
1887 s->xch_present = 0; /* disable further xch processing */
1889 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1890 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1892 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1895 if (channels > !!s->lfe &&
1896 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1897 return AVERROR_INVALIDDATA;
1899 if (num_core_channels + !!s->lfe > 2 &&
1900 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1902 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1903 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1905 /* Stereo downmix coefficients
1907 * The decoder can only downmix to 2-channel, so we need to ensure
1908 * embedded downmix coefficients are actually targeting 2-channel.
1910 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1911 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
1912 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1913 /* Range checked earlier */
1914 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1915 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1917 s->output = s->core_downmix_amode;
1919 int am = s->amode & DCA_CHANNEL_MASK;
1920 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
1921 av_log(s->avctx, AV_LOG_ERROR,
1922 "Invalid channel mode %d\n", am);
1923 return AVERROR_INVALIDDATA;
1925 if (num_core_channels + !!s->lfe >
1926 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
1927 avpriv_request_sample(s->avctx, "Downmixing %d channels",
1928 s->prim_channels + !!s->lfe);
1929 return AVERROR_PATCHWELCOME;
1931 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1932 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
1933 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
1936 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
1937 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1938 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
1939 s->downmix_coef[i][0]);
1940 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
1941 s->downmix_coef[i][1]);
1943 av_dlog(s->avctx, "\n");
1946 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1947 return AVERROR_INVALIDDATA;
1949 avctx->channels = channels;
1951 /* get output buffer */
1952 frame->nb_samples = 256 * (s->sample_blocks / 8);
1953 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1954 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1957 samples_flt = (float **) frame->extended_data;
1959 /* allocate buffer for extra channels if downmixing */
1960 if (avctx->channels < full_channels) {
1961 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1963 avctx->sample_fmt, 0);
1967 av_fast_malloc(&s->extra_channels_buffer,
1968 &s->extra_channels_buffer_size, ret);
1969 if (!s->extra_channels_buffer)
1970 return AVERROR(ENOMEM);
1972 ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
1973 s->extra_channels_buffer,
1974 full_channels - channels,
1975 frame->nb_samples, avctx->sample_fmt, 0);
1980 /* filter to get final output */
1981 for (i = 0; i < (s->sample_blocks / 8); i++) {
1984 for (ch = 0; ch < channels; ch++)
1985 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
1986 for (; ch < full_channels; ch++)
1987 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
1989 dca_filter_channels(s, i);
1991 /* If this was marked as a DTS-ES stream we need to subtract back- */
1992 /* channel from SL & SR to remove matrixed back-channel signal */
1993 if ((s->source_pcm_res & 1) && s->xch_present) {
1994 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1995 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1996 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1997 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1998 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2002 /* update lfe history */
2003 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2004 for (i = 0; i < 2 * s->lfe * 4; i++)
2005 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2009 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2010 ret = ff_side_data_update_matrix_encoding(frame,
2011 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2012 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
2022 * DCA initialization
2024 * @param avctx pointer to the AVCodecContext
2027 static av_cold int dca_decode_init(AVCodecContext *avctx)
2029 DCAContext *s = avctx->priv_data;
2034 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2035 ff_mdct_init(&s->imdct, 6, 1, 1.0);
2036 ff_synth_filter_init(&s->synth);
2037 ff_dcadsp_init(&s->dcadsp);
2038 ff_fmt_convert_init(&s->fmt_conv, avctx);
2040 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2042 /* allow downmixing to stereo */
2043 #if FF_API_REQUEST_CHANNELS
2044 FF_DISABLE_DEPRECATION_WARNINGS
2045 if (avctx->request_channels == 2)
2046 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
2047 FF_ENABLE_DEPRECATION_WARNINGS
2049 if (avctx->channels > 2 &&
2050 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
2051 avctx->channels = 2;
2056 static av_cold int dca_decode_end(AVCodecContext *avctx)
2058 DCAContext *s = avctx->priv_data;
2059 ff_mdct_end(&s->imdct);
2060 av_freep(&s->extra_channels_buffer);
2064 static const AVProfile profiles[] = {
2065 { FF_PROFILE_DTS, "DTS" },
2066 { FF_PROFILE_DTS_ES, "DTS-ES" },
2067 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2068 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2069 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2070 { FF_PROFILE_UNKNOWN },
2073 static const AVOption options[] = {
2074 { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
2078 static const AVClass dca_decoder_class = {
2079 .class_name = "DCA decoder",
2080 .item_name = av_default_item_name,
2082 .version = LIBAVUTIL_VERSION_INT,
2085 AVCodec ff_dca_decoder = {
2087 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2088 .type = AVMEDIA_TYPE_AUDIO,
2089 .id = AV_CODEC_ID_DTS,
2090 .priv_data_size = sizeof(DCAContext),
2091 .init = dca_decode_init,
2092 .decode = dca_decode_frame,
2093 .close = dca_decode_end,
2094 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2095 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2096 AV_SAMPLE_FMT_NONE },
2097 .profiles = NULL_IF_CONFIG_SMALL(profiles),
2098 .priv_class = &dca_decoder_class,