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 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1249 0, 8388608.0, 8388608.0 };*/
1250 if (s->channel_order_tab[k] >= 0)
1251 qmf_32_subbands(s, k, subband_samples[k],
1252 s->samples_chanptr[s->channel_order_tab[k]],
1253 M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1256 /* Generate LFE samples for this subsubframe FIXME!!! */
1258 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1259 s->lfe_data + 2 * s->lfe * (block_index + 4),
1260 s->samples_chanptr[dca_lfe_index[s->amode]]);
1261 /* Outputs 20bits pcm samples */
1264 /* Downmixing to Stereo */
1265 if (s->prim_channels + !!s->lfe > 2 &&
1266 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1267 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1268 s->channel_order_tab);
1274 static int dca_subframe_footer(DCAContext *s, int base_channel)
1276 int in, out, aux_data_count, aux_data_end, reserved;
1280 * Unpack optional information
1283 /* presumably optional information only appears in the core? */
1284 if (!base_channel) {
1286 skip_bits_long(&s->gb, 32);
1289 aux_data_count = get_bits(&s->gb, 6);
1292 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1294 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1296 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1297 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1299 return AVERROR_INVALIDDATA;
1302 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1303 avpriv_request_sample(s->avctx,
1304 "Auxiliary Decode Time Stamp Flag");
1306 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1307 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1308 skip_bits_long(&s->gb, 44);
1311 if ((s->core_downmix = get_bits1(&s->gb))) {
1312 int am = get_bits(&s->gb, 3);
1315 s->core_downmix_amode = DCA_MONO;
1318 s->core_downmix_amode = DCA_STEREO;
1321 s->core_downmix_amode = DCA_STEREO_TOTAL;
1324 s->core_downmix_amode = DCA_3F;
1327 s->core_downmix_amode = DCA_2F1R;
1330 s->core_downmix_amode = DCA_2F2R;
1333 s->core_downmix_amode = DCA_3F1R;
1336 av_log(s->avctx, AV_LOG_ERROR,
1337 "Invalid mode %d for embedded downmix coefficients\n",
1339 return AVERROR_INVALIDDATA;
1341 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1342 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1343 uint16_t tmp = get_bits(&s->gb, 9);
1344 if ((tmp & 0xFF) > 241) {
1345 av_log(s->avctx, AV_LOG_ERROR,
1346 "Invalid downmix coefficient code %"PRIu16"\n",
1348 return AVERROR_INVALIDDATA;
1350 s->core_downmix_codes[in][out] = tmp;
1355 align_get_bits(&s->gb); // byte align
1356 skip_bits(&s->gb, 16); // nAUXCRC16
1358 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1359 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1360 av_log(s->avctx, AV_LOG_ERROR,
1361 "Overread auxiliary data by %d bits\n", -reserved);
1362 return AVERROR_INVALIDDATA;
1363 } else if (reserved) {
1364 avpriv_request_sample(s->avctx,
1365 "Core auxiliary data reserved content");
1366 skip_bits_long(&s->gb, reserved);
1370 if (s->crc_present && s->dynrange)
1371 get_bits(&s->gb, 16);
1378 * Decode a dca frame block
1380 * @param s pointer to the DCAContext
1383 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1388 if (s->current_subframe >= s->subframes) {
1389 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1390 s->current_subframe, s->subframes);
1391 return AVERROR_INVALIDDATA;
1394 if (!s->current_subsubframe) {
1396 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1398 /* Read subframe header */
1399 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1403 /* Read subsubframe */
1405 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1407 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1411 s->current_subsubframe++;
1412 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1413 s->current_subsubframe = 0;
1414 s->current_subframe++;
1416 if (s->current_subframe >= s->subframes) {
1418 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1420 /* Read subframe footer */
1421 if ((ret = dca_subframe_footer(s, base_channel)))
1429 * Return the number of channels in an ExSS speaker mask (HD)
1431 static int dca_exss_mask2count(int mask)
1433 /* count bits that mean speaker pairs twice */
1434 return av_popcount(mask) +
1435 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1436 DCA_EXSS_FRONT_LEFT_RIGHT |
1437 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1438 DCA_EXSS_WIDE_LEFT_RIGHT |
1439 DCA_EXSS_SIDE_LEFT_RIGHT |
1440 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1441 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1442 DCA_EXSS_REAR_LEFT_RIGHT |
1443 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1447 * Skip mixing coefficients of a single mix out configuration (HD)
1449 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1453 for (i = 0; i < channels; i++) {
1454 int mix_map_mask = get_bits(gb, out_ch);
1455 int num_coeffs = av_popcount(mix_map_mask);
1456 skip_bits_long(gb, num_coeffs * 6);
1461 * Parse extension substream asset header (HD)
1463 static int dca_exss_parse_asset_header(DCAContext *s)
1465 int header_pos = get_bits_count(&s->gb);
1468 int embedded_stereo = 0;
1469 int embedded_6ch = 0;
1470 int drc_code_present;
1471 int extensions_mask;
1474 if (get_bits_left(&s->gb) < 16)
1477 /* We will parse just enough to get to the extensions bitmask with which
1478 * we can set the profile value. */
1480 header_size = get_bits(&s->gb, 9) + 1;
1481 skip_bits(&s->gb, 3); // asset index
1483 if (s->static_fields) {
1484 if (get_bits1(&s->gb))
1485 skip_bits(&s->gb, 4); // asset type descriptor
1486 if (get_bits1(&s->gb))
1487 skip_bits_long(&s->gb, 24); // language descriptor
1489 if (get_bits1(&s->gb)) {
1490 /* How can one fit 1024 bytes of text here if the maximum value
1491 * for the asset header size field above was 512 bytes? */
1492 int text_length = get_bits(&s->gb, 10) + 1;
1493 if (get_bits_left(&s->gb) < text_length * 8)
1495 skip_bits_long(&s->gb, text_length * 8); // info text
1498 skip_bits(&s->gb, 5); // bit resolution - 1
1499 skip_bits(&s->gb, 4); // max sample rate code
1500 channels = get_bits(&s->gb, 8) + 1;
1502 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1503 int spkr_remap_sets;
1504 int spkr_mask_size = 16;
1508 embedded_stereo = get_bits1(&s->gb);
1510 embedded_6ch = get_bits1(&s->gb);
1512 if (get_bits1(&s->gb)) {
1513 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1514 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1517 spkr_remap_sets = get_bits(&s->gb, 3);
1519 for (i = 0; i < spkr_remap_sets; i++) {
1520 /* std layout mask for each remap set */
1521 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1524 for (i = 0; i < spkr_remap_sets; i++) {
1525 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1526 if (get_bits_left(&s->gb) < 0)
1529 for (j = 0; j < num_spkrs[i]; j++) {
1530 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1531 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1532 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1536 skip_bits(&s->gb, 3); // representation type
1540 drc_code_present = get_bits1(&s->gb);
1541 if (drc_code_present)
1542 get_bits(&s->gb, 8); // drc code
1544 if (get_bits1(&s->gb))
1545 skip_bits(&s->gb, 5); // dialog normalization code
1547 if (drc_code_present && embedded_stereo)
1548 get_bits(&s->gb, 8); // drc stereo code
1550 if (s->mix_metadata && get_bits1(&s->gb)) {
1551 skip_bits(&s->gb, 1); // external mix
1552 skip_bits(&s->gb, 6); // post mix gain code
1554 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1555 skip_bits(&s->gb, 3); // drc limit
1557 skip_bits(&s->gb, 8); // custom drc code
1559 if (get_bits1(&s->gb)) // channel specific scaling
1560 for (i = 0; i < s->num_mix_configs; i++)
1561 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1563 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1565 for (i = 0; i < s->num_mix_configs; i++) {
1566 if (get_bits_left(&s->gb) < 0)
1568 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1570 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1571 if (embedded_stereo)
1572 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1576 switch (get_bits(&s->gb, 2)) {
1578 extensions_mask = get_bits(&s->gb, 12);
1581 extensions_mask = DCA_EXT_EXSS_XLL;
1584 extensions_mask = DCA_EXT_EXSS_LBR;
1587 extensions_mask = 0; /* aux coding */
1591 /* not parsed further, we were only interested in the extensions mask */
1593 if (get_bits_left(&s->gb) < 0)
1596 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1597 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1600 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1602 if (extensions_mask & DCA_EXT_EXSS_XLL)
1603 s->profile = FF_PROFILE_DTS_HD_MA;
1604 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1606 s->profile = FF_PROFILE_DTS_HD_HRA;
1608 if (!(extensions_mask & DCA_EXT_CORE))
1609 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1610 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1611 av_log(s->avctx, AV_LOG_WARNING,
1612 "DTS extensions detection mismatch (%d, %d)\n",
1613 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1619 * Parse extension substream header (HD)
1621 static void dca_exss_parse_header(DCAContext *s)
1627 int active_ss_mask[8];
1630 if (get_bits_left(&s->gb) < 52)
1633 skip_bits(&s->gb, 8); // user data
1634 ss_index = get_bits(&s->gb, 2);
1636 blownup = get_bits1(&s->gb);
1637 skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1638 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1640 s->static_fields = get_bits1(&s->gb);
1641 if (s->static_fields) {
1642 skip_bits(&s->gb, 2); // reference clock code
1643 skip_bits(&s->gb, 3); // frame duration code
1645 if (get_bits1(&s->gb))
1646 skip_bits_long(&s->gb, 36); // timestamp
1648 /* a single stream can contain multiple audio assets that can be
1649 * combined to form multiple audio presentations */
1651 num_audiop = get_bits(&s->gb, 3) + 1;
1652 if (num_audiop > 1) {
1653 avpriv_request_sample(s->avctx,
1654 "Multiple DTS-HD audio presentations");
1655 /* ignore such streams for now */
1659 num_assets = get_bits(&s->gb, 3) + 1;
1660 if (num_assets > 1) {
1661 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
1662 /* ignore such streams for now */
1666 for (i = 0; i < num_audiop; i++)
1667 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1669 for (i = 0; i < num_audiop; i++)
1670 for (j = 0; j <= ss_index; j++)
1671 if (active_ss_mask[i] & (1 << j))
1672 skip_bits(&s->gb, 8); // active asset mask
1674 s->mix_metadata = get_bits1(&s->gb);
1675 if (s->mix_metadata) {
1676 int mix_out_mask_size;
1678 skip_bits(&s->gb, 2); // adjustment level
1679 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1680 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
1682 for (i = 0; i < s->num_mix_configs; i++) {
1683 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
1684 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
1689 for (i = 0; i < num_assets; i++)
1690 skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
1692 for (i = 0; i < num_assets; i++) {
1693 if (dca_exss_parse_asset_header(s))
1697 /* not parsed further, we were only interested in the extensions mask
1698 * from the asset header */
1701 static float dca_dmix_code(unsigned code)
1703 int sign = (code >> 8) - 1;
1705 return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
1709 * Main frame decoding function
1710 * FIXME add arguments
1712 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1713 int *got_frame_ptr, AVPacket *avpkt)
1715 AVFrame *frame = data;
1716 const uint8_t *buf = avpkt->data;
1717 int buf_size = avpkt->size;
1720 int num_core_channels = 0;
1722 float **samples_flt;
1723 DCAContext *s = avctx->priv_data;
1724 int channels, full_channels;
1729 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1730 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1731 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1732 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1733 return AVERROR_INVALIDDATA;
1736 if ((ret = dca_parse_frame_header(s)) < 0) {
1737 // seems like the frame is corrupt, try with the next one
1740 // set AVCodec values with parsed data
1741 avctx->sample_rate = s->sample_rate;
1742 avctx->bit_rate = s->bit_rate;
1744 s->profile = FF_PROFILE_DTS;
1746 for (i = 0; i < (s->sample_blocks / 8); i++) {
1747 if ((ret = dca_decode_block(s, 0, i))) {
1748 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1753 /* record number of core channels incase less than max channels are requested */
1754 num_core_channels = s->prim_channels;
1757 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1759 s->core_ext_mask = 0;
1761 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1763 /* only scan for extensions if ext_descr was unknown or indicated a
1764 * supported XCh extension */
1765 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1766 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1767 * extensions scan can fill it up */
1768 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1770 /* extensions start at 32-bit boundaries into bitstream */
1771 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1773 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1774 uint32_t bits = get_bits_long(&s->gb, 32);
1778 int ext_amode, xch_fsize;
1780 s->xch_base_channel = s->prim_channels;
1782 /* validate sync word using XCHFSIZE field */
1783 xch_fsize = show_bits(&s->gb, 10);
1784 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1785 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1788 /* skip length-to-end-of-frame field for the moment */
1789 skip_bits(&s->gb, 10);
1791 s->core_ext_mask |= DCA_EXT_XCH;
1793 /* extension amode(number of channels in extension) should be 1 */
1794 /* AFAIK XCh is not used for more channels */
1795 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1796 av_log(avctx, AV_LOG_ERROR,
1797 "XCh extension amode %d not supported!\n",
1802 /* much like core primary audio coding header */
1803 dca_parse_audio_coding_header(s, s->xch_base_channel);
1805 for (i = 0; i < (s->sample_blocks / 8); i++)
1806 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1807 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1815 /* XXCh: extended channels */
1816 /* usually found either in core or HD part in DTS-HD HRA streams,
1817 * but not in DTS-ES which contains XCh extensions instead */
1818 s->core_ext_mask |= DCA_EXT_XXCH;
1822 int fsize96 = show_bits(&s->gb, 12) + 1;
1823 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1826 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1827 get_bits_count(&s->gb));
1828 skip_bits(&s->gb, 12);
1829 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1830 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1832 s->core_ext_mask |= DCA_EXT_X96;
1837 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1840 /* no supported extensions, skip the rest of the core substream */
1841 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1844 if (s->core_ext_mask & DCA_EXT_X96)
1845 s->profile = FF_PROFILE_DTS_96_24;
1846 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1847 s->profile = FF_PROFILE_DTS_ES;
1849 /* check for ExSS (HD part) */
1850 if (s->dca_buffer_size - s->frame_size > 32 &&
1851 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1852 dca_exss_parse_header(s);
1854 avctx->profile = s->profile;
1856 full_channels = channels = s->prim_channels + !!s->lfe;
1858 if (s->amode < 16) {
1859 avctx->channel_layout = dca_core_channel_layout[s->amode];
1861 if (s->prim_channels + !!s->lfe > 2 &&
1862 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1864 * Neither the core's auxiliary data nor our default tables contain
1865 * downmix coefficients for the additional channel coded in the XCh
1866 * extension, so when we're doing a Stereo downmix, don't decode it.
1871 #if FF_API_REQUEST_CHANNELS
1872 FF_DISABLE_DEPRECATION_WARNINGS
1873 if (s->xch_present && !s->xch_disable &&
1874 (!avctx->request_channels ||
1875 avctx->request_channels > num_core_channels + !!s->lfe)) {
1876 FF_ENABLE_DEPRECATION_WARNINGS
1878 if (s->xch_present && !s->xch_disable) {
1880 avctx->channel_layout |= AV_CH_BACK_CENTER;
1882 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1883 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1885 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1888 channels = num_core_channels + !!s->lfe;
1889 s->xch_present = 0; /* disable further xch processing */
1891 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1892 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1894 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1897 if (channels > !!s->lfe &&
1898 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1899 return AVERROR_INVALIDDATA;
1901 if (num_core_channels + !!s->lfe > 2 &&
1902 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1904 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1905 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1907 /* Stereo downmix coefficients
1909 * The decoder can only downmix to 2-channel, so we need to ensure
1910 * embedded downmix coefficients are actually targeting 2-channel.
1912 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1913 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
1914 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1915 /* Range checked earlier */
1916 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1917 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1919 s->output = s->core_downmix_amode;
1921 int am = s->amode & DCA_CHANNEL_MASK;
1922 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
1923 av_log(s->avctx, AV_LOG_ERROR,
1924 "Invalid channel mode %d\n", am);
1925 return AVERROR_INVALIDDATA;
1927 if (num_core_channels + !!s->lfe >
1928 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
1929 avpriv_request_sample(s->avctx, "Downmixing %d channels",
1930 s->prim_channels + !!s->lfe);
1931 return AVERROR_PATCHWELCOME;
1933 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1934 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
1935 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
1938 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
1939 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1940 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
1941 s->downmix_coef[i][0]);
1942 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
1943 s->downmix_coef[i][1]);
1945 av_dlog(s->avctx, "\n");
1948 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1949 return AVERROR_INVALIDDATA;
1951 avctx->channels = channels;
1953 /* get output buffer */
1954 frame->nb_samples = 256 * (s->sample_blocks / 8);
1955 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1956 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1959 samples_flt = (float **) frame->extended_data;
1961 /* allocate buffer for extra channels if downmixing */
1962 if (avctx->channels < full_channels) {
1963 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1965 avctx->sample_fmt, 0);
1969 av_fast_malloc(&s->extra_channels_buffer,
1970 &s->extra_channels_buffer_size, ret);
1971 if (!s->extra_channels_buffer)
1972 return AVERROR(ENOMEM);
1974 ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
1975 s->extra_channels_buffer,
1976 full_channels - channels,
1977 frame->nb_samples, avctx->sample_fmt, 0);
1982 /* filter to get final output */
1983 for (i = 0; i < (s->sample_blocks / 8); i++) {
1986 for (ch = 0; ch < channels; ch++)
1987 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
1988 for (; ch < full_channels; ch++)
1989 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
1991 dca_filter_channels(s, i);
1993 /* If this was marked as a DTS-ES stream we need to subtract back- */
1994 /* channel from SL & SR to remove matrixed back-channel signal */
1995 if ((s->source_pcm_res & 1) && s->xch_present) {
1996 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1997 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1998 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1999 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2000 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2004 /* update lfe history */
2005 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2006 for (i = 0; i < 2 * s->lfe * 4; i++)
2007 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2011 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2012 ret = ff_side_data_update_matrix_encoding(frame,
2013 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2014 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
2024 * DCA initialization
2026 * @param avctx pointer to the AVCodecContext
2029 static av_cold int dca_decode_init(AVCodecContext *avctx)
2031 DCAContext *s = avctx->priv_data;
2036 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2037 ff_mdct_init(&s->imdct, 6, 1, 1.0);
2038 ff_synth_filter_init(&s->synth);
2039 ff_dcadsp_init(&s->dcadsp);
2040 ff_fmt_convert_init(&s->fmt_conv, avctx);
2042 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2044 /* allow downmixing to stereo */
2045 #if FF_API_REQUEST_CHANNELS
2046 FF_DISABLE_DEPRECATION_WARNINGS
2047 if (avctx->request_channels == 2)
2048 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
2049 FF_ENABLE_DEPRECATION_WARNINGS
2051 if (avctx->channels > 2 &&
2052 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
2053 avctx->channels = 2;
2058 static av_cold int dca_decode_end(AVCodecContext *avctx)
2060 DCAContext *s = avctx->priv_data;
2061 ff_mdct_end(&s->imdct);
2062 av_freep(&s->extra_channels_buffer);
2066 static const AVProfile profiles[] = {
2067 { FF_PROFILE_DTS, "DTS" },
2068 { FF_PROFILE_DTS_ES, "DTS-ES" },
2069 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2070 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2071 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2072 { FF_PROFILE_UNKNOWN },
2075 static const AVOption options[] = {
2076 { "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 },
2080 static const AVClass dca_decoder_class = {
2081 .class_name = "DCA decoder",
2082 .item_name = av_default_item_name,
2084 .version = LIBAVUTIL_VERSION_INT,
2087 AVCodec ff_dca_decoder = {
2089 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2090 .type = AVMEDIA_TYPE_AUDIO,
2091 .id = AV_CODEC_ID_DTS,
2092 .priv_data_size = sizeof(DCAContext),
2093 .init = dca_decode_init,
2094 .decode = dca_decode_frame,
2095 .close = dca_decode_end,
2096 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2097 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2098 AV_SAMPLE_FMT_NONE },
2099 .profiles = NULL_IF_CONFIG_SMALL(profiles),
2100 .priv_class = &dca_decoder_class,