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"
45 #include "synth_filter.h"
47 #include "fmtconvert.h"
59 #define DCA_PRIM_CHANNELS_MAX (7)
60 #define DCA_SUBBANDS (32)
61 #define DCA_ABITS_MAX (32) /* Should be 28 */
62 #define DCA_SUBSUBFRAMES_MAX (4)
63 #define DCA_SUBFRAMES_MAX (16)
64 #define DCA_BLOCKS_MAX (16)
65 #define DCA_LFE_MAX (3)
81 /* these are unconfirmed but should be mostly correct */
82 enum DCAExSSSpeakerMask {
83 DCA_EXSS_FRONT_CENTER = 0x0001,
84 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
85 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
86 DCA_EXSS_LFE = 0x0008,
87 DCA_EXSS_REAR_CENTER = 0x0010,
88 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
89 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
90 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
91 DCA_EXSS_OVERHEAD = 0x0100,
92 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
93 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
94 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
95 DCA_EXSS_LFE2 = 0x1000,
96 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
97 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
98 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
101 enum DCAExtensionMask {
102 DCA_EXT_CORE = 0x001, ///< core in core substream
103 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
104 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
105 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
106 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
107 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
108 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
109 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
110 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
111 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
114 /* -1 are reserved or unknown */
115 static const int dca_ext_audio_descr_mask[] = {
119 DCA_EXT_XCH | DCA_EXT_X96,
126 /* extensions that reside in core substream */
127 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
129 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
130 * Some compromises have been made for special configurations. Most configurations
131 * are never used so complete accuracy is not needed.
133 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
134 * S -> side, when both rear and back are configured move one of them to the side channel
136 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
138 static const uint64_t dca_core_channel_layout[] = {
139 AV_CH_FRONT_CENTER, ///< 1, A
140 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
141 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
142 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
143 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
144 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
145 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
146 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
147 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
149 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
150 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
152 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
153 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
155 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
156 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
158 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
159 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
160 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
162 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
163 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
164 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
166 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
167 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
168 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
170 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
171 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
172 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
175 static const int8_t dca_lfe_index[] = {
176 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
179 static const int8_t dca_channel_reorder_lfe[][9] = {
180 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
181 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
182 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
183 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
184 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
185 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
186 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
187 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
188 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
189 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
190 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
191 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
192 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
193 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
194 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
195 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
198 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
199 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
200 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
201 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
202 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
203 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
204 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
205 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
206 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
207 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
208 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
209 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
210 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
211 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
212 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
213 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
214 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
217 static const int8_t dca_channel_reorder_nolfe[][9] = {
218 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
219 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
220 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
221 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
222 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
223 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
224 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
225 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
226 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
227 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
228 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
229 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
230 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
231 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
232 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
233 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
236 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
237 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
238 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
239 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
240 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
241 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
242 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
243 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
244 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
245 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
246 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
247 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
248 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
249 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
250 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
251 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
252 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
255 #define DCA_DOLBY 101 /* FIXME */
257 #define DCA_CHANNEL_BITS 6
258 #define DCA_CHANNEL_MASK 0x3F
262 #define HEADER_SIZE 14
264 #define DCA_MAX_FRAME_SIZE 16384
265 #define DCA_MAX_EXSS_HEADER_SIZE 4096
267 #define DCA_BUFFER_PADDING_SIZE 1024
269 #define DCA_NSYNCAUX 0x9A1105A0
271 /** Bit allocation */
273 int offset; ///< code values offset
274 int maxbits[8]; ///< max bits in VLC
275 int wrap; ///< wrap for get_vlc2()
276 VLC vlc[8]; ///< actual codes
279 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
280 static BitAlloc dca_tmode; ///< transition mode VLCs
281 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
282 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
284 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
287 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
292 AVClass *class; ///< class for AVOptions
293 AVCodecContext *avctx;
295 int frame_type; ///< type of the current frame
296 int samples_deficit; ///< deficit sample count
297 int crc_present; ///< crc is present in the bitstream
298 int sample_blocks; ///< number of PCM sample blocks
299 int frame_size; ///< primary frame byte size
300 int amode; ///< audio channels arrangement
301 int sample_rate; ///< audio sampling rate
302 int bit_rate; ///< transmission bit rate
303 int bit_rate_index; ///< transmission bit rate index
305 int dynrange; ///< embedded dynamic range flag
306 int timestamp; ///< embedded time stamp flag
307 int aux_data; ///< auxiliary data flag
308 int hdcd; ///< source material is mastered in HDCD
309 int ext_descr; ///< extension audio descriptor flag
310 int ext_coding; ///< extended coding flag
311 int aspf; ///< audio sync word insertion flag
312 int lfe; ///< low frequency effects flag
313 int predictor_history; ///< predictor history flag
314 int header_crc; ///< header crc check bytes
315 int multirate_inter; ///< multirate interpolator switch
316 int version; ///< encoder software revision
317 int copy_history; ///< copy history
318 int source_pcm_res; ///< source pcm resolution
319 int front_sum; ///< front sum/difference flag
320 int surround_sum; ///< surround sum/difference flag
321 int dialog_norm; ///< dialog normalisation parameter
323 /* Primary audio coding header */
324 int subframes; ///< number of subframes
325 int total_channels; ///< number of channels including extensions
326 int prim_channels; ///< number of primary audio channels
327 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
328 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
329 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
330 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
331 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
332 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
333 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
334 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
336 /* Primary audio coding side information */
337 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
338 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
339 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
340 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
341 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
342 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
343 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
344 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
345 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
346 float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
347 int dynrange_coef; ///< dynamic range coefficient
349 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
350 * Input: primary audio channels (incl. LFE if present)
351 * Output: downmix audio channels (up to 4, no LFE) */
352 uint8_t core_downmix; ///< embedded downmix coefficients available
353 uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
354 uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
356 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
358 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
359 int lfe_scale_factor;
361 /* Subband samples history (for ADPCM) */
362 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
363 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
364 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
365 int hist_index[DCA_PRIM_CHANNELS_MAX];
366 DECLARE_ALIGNED(32, float, raXin)[32];
368 int output; ///< type of output
370 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
371 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
372 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
373 uint8_t *extra_channels_buffer;
374 unsigned int extra_channels_buffer_size;
376 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
377 int dca_buffer_size; ///< how much data is in the dca_buffer
379 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
381 /* Current position in DCA frame */
382 int current_subframe;
383 int current_subsubframe;
385 int core_ext_mask; ///< present extensions in the core substream
387 /* XCh extension information */
388 int xch_present; ///< XCh extension present and valid
389 int xch_base_channel; ///< index of first (only) channel containing XCH data
390 int xch_disable; ///< whether the XCh extension should be decoded or not
392 /* ExSS header parser */
393 int static_fields; ///< static fields present
394 int mix_metadata; ///< mixing metadata present
395 int num_mix_configs; ///< number of mix out configurations
396 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
400 int debug_flag; ///< used for suppressing repeated error messages output
401 AVFloatDSPContext fdsp;
403 SynthFilterContext synth;
404 DCADSPContext dcadsp;
405 FmtConvertContext fmt_conv;
408 static const uint16_t dca_vlc_offs[] = {
409 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
410 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
411 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
412 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
413 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
414 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
417 static av_cold void dca_init_vlcs(void)
419 static int vlcs_initialized = 0;
421 static VLC_TYPE dca_table[23622][2];
423 if (vlcs_initialized)
426 dca_bitalloc_index.offset = 1;
427 dca_bitalloc_index.wrap = 2;
428 for (i = 0; i < 5; i++) {
429 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
430 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
431 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
432 bitalloc_12_bits[i], 1, 1,
433 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
435 dca_scalefactor.offset = -64;
436 dca_scalefactor.wrap = 2;
437 for (i = 0; i < 5; i++) {
438 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
439 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
440 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
441 scales_bits[i], 1, 1,
442 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
444 dca_tmode.offset = 0;
446 for (i = 0; i < 4; i++) {
447 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
448 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
449 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
451 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
454 for (i = 0; i < 10; i++)
455 for (j = 0; j < 7; j++) {
456 if (!bitalloc_codes[i][j])
458 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
459 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
460 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
461 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
463 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
465 bitalloc_bits[i][j], 1, 1,
466 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
469 vlcs_initialized = 1;
472 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
475 *dst++ = get_bits(gb, bits);
478 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
481 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
482 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
483 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
485 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
486 s->prim_channels = s->total_channels;
488 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
489 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
492 for (i = base_channel; i < s->prim_channels; i++) {
493 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
494 if (s->subband_activity[i] > DCA_SUBBANDS)
495 s->subband_activity[i] = DCA_SUBBANDS;
497 for (i = base_channel; i < s->prim_channels; i++) {
498 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
499 if (s->vq_start_subband[i] > DCA_SUBBANDS)
500 s->vq_start_subband[i] = DCA_SUBBANDS;
502 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
503 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
504 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
505 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
507 /* Get codebooks quantization indexes */
509 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
510 for (j = 1; j < 11; j++)
511 for (i = base_channel; i < s->prim_channels; i++)
512 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
514 /* Get scale factor adjustment */
515 for (j = 0; j < 11; j++)
516 for (i = base_channel; i < s->prim_channels; i++)
517 s->scalefactor_adj[i][j] = 1;
519 for (j = 1; j < 11; j++)
520 for (i = base_channel; i < s->prim_channels; i++)
521 if (s->quant_index_huffman[i][j] < thr[j])
522 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
524 if (s->crc_present) {
525 /* Audio header CRC check */
526 get_bits(&s->gb, 16);
529 s->current_subframe = 0;
530 s->current_subsubframe = 0;
533 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
534 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
535 for (i = base_channel; i < s->prim_channels; i++) {
536 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
537 s->subband_activity[i]);
538 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
539 s->vq_start_subband[i]);
540 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
541 s->joint_intensity[i]);
542 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
543 s->transient_huffman[i]);
544 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
545 s->scalefactor_huffman[i]);
546 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
547 s->bitalloc_huffman[i]);
548 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
549 for (j = 0; j < 11; j++)
550 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
551 av_log(s->avctx, AV_LOG_DEBUG, "\n");
552 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
553 for (j = 0; j < 11; j++)
554 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
555 av_log(s->avctx, AV_LOG_DEBUG, "\n");
562 static int dca_parse_frame_header(DCAContext *s)
564 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
567 skip_bits_long(&s->gb, 32);
570 s->frame_type = get_bits(&s->gb, 1);
571 s->samples_deficit = get_bits(&s->gb, 5) + 1;
572 s->crc_present = get_bits(&s->gb, 1);
573 s->sample_blocks = get_bits(&s->gb, 7) + 1;
574 s->frame_size = get_bits(&s->gb, 14) + 1;
575 if (s->frame_size < 95)
576 return AVERROR_INVALIDDATA;
577 s->amode = get_bits(&s->gb, 6);
578 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
580 return AVERROR_INVALIDDATA;
581 s->bit_rate_index = get_bits(&s->gb, 5);
582 s->bit_rate = dca_bit_rates[s->bit_rate_index];
584 return AVERROR_INVALIDDATA;
586 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
587 s->dynrange = get_bits(&s->gb, 1);
588 s->timestamp = get_bits(&s->gb, 1);
589 s->aux_data = get_bits(&s->gb, 1);
590 s->hdcd = get_bits(&s->gb, 1);
591 s->ext_descr = get_bits(&s->gb, 3);
592 s->ext_coding = get_bits(&s->gb, 1);
593 s->aspf = get_bits(&s->gb, 1);
594 s->lfe = get_bits(&s->gb, 2);
595 s->predictor_history = get_bits(&s->gb, 1);
598 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
599 return AVERROR_INVALIDDATA;
602 /* TODO: check CRC */
604 s->header_crc = get_bits(&s->gb, 16);
606 s->multirate_inter = get_bits(&s->gb, 1);
607 s->version = get_bits(&s->gb, 4);
608 s->copy_history = get_bits(&s->gb, 2);
609 s->source_pcm_res = get_bits(&s->gb, 3);
610 s->front_sum = get_bits(&s->gb, 1);
611 s->surround_sum = get_bits(&s->gb, 1);
612 s->dialog_norm = get_bits(&s->gb, 4);
614 /* FIXME: channels mixing levels */
615 s->output = s->amode;
617 s->output |= DCA_LFE;
620 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
621 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
622 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
623 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
624 s->sample_blocks, s->sample_blocks * 32);
625 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
626 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
627 s->amode, dca_channels[s->amode]);
628 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
630 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
632 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
633 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
634 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
635 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
636 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
637 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
638 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
639 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
640 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
641 s->predictor_history);
642 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
643 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
645 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
646 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
647 av_log(s->avctx, AV_LOG_DEBUG,
648 "source pcm resolution: %i (%i bits/sample)\n",
649 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
650 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
651 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
652 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
653 av_log(s->avctx, AV_LOG_DEBUG, "\n");
656 /* Primary audio coding header */
657 s->subframes = get_bits(&s->gb, 4) + 1;
659 return dca_parse_audio_coding_header(s, 0);
663 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
666 /* huffman encoded */
667 value += get_bitalloc(gb, &dca_scalefactor, level);
668 value = av_clip(value, 0, (1 << log2range) - 1);
669 } else if (level < 8) {
670 if (level + 1 > log2range) {
671 skip_bits(gb, level + 1 - log2range);
672 value = get_bits(gb, log2range);
674 value = get_bits(gb, level + 1);
680 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
682 /* Primary audio coding side information */
685 if (get_bits_left(&s->gb) < 0)
686 return AVERROR_INVALIDDATA;
689 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
690 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
693 for (j = base_channel; j < s->prim_channels; j++) {
694 for (k = 0; k < s->subband_activity[j]; k++)
695 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
698 /* Get prediction codebook */
699 for (j = base_channel; j < s->prim_channels; j++) {
700 for (k = 0; k < s->subband_activity[j]; k++) {
701 if (s->prediction_mode[j][k] > 0) {
702 /* (Prediction coefficient VQ address) */
703 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
708 /* Bit allocation index */
709 for (j = base_channel; j < s->prim_channels; j++) {
710 for (k = 0; k < s->vq_start_subband[j]; k++) {
711 if (s->bitalloc_huffman[j] == 6)
712 s->bitalloc[j][k] = get_bits(&s->gb, 5);
713 else if (s->bitalloc_huffman[j] == 5)
714 s->bitalloc[j][k] = get_bits(&s->gb, 4);
715 else if (s->bitalloc_huffman[j] == 7) {
716 av_log(s->avctx, AV_LOG_ERROR,
717 "Invalid bit allocation index\n");
718 return AVERROR_INVALIDDATA;
721 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
724 if (s->bitalloc[j][k] > 26) {
725 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
726 j, k, s->bitalloc[j][k]);
727 return AVERROR_INVALIDDATA;
732 /* Transition mode */
733 for (j = base_channel; j < s->prim_channels; j++) {
734 for (k = 0; k < s->subband_activity[j]; k++) {
735 s->transition_mode[j][k] = 0;
736 if (s->subsubframes[s->current_subframe] > 1 &&
737 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
738 s->transition_mode[j][k] =
739 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
744 if (get_bits_left(&s->gb) < 0)
745 return AVERROR_INVALIDDATA;
747 for (j = base_channel; j < s->prim_channels; j++) {
748 const uint32_t *scale_table;
749 int scale_sum, log_size;
751 memset(s->scale_factor[j], 0,
752 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
754 if (s->scalefactor_huffman[j] == 6) {
755 scale_table = scale_factor_quant7;
758 scale_table = scale_factor_quant6;
762 /* When huffman coded, only the difference is encoded */
765 for (k = 0; k < s->subband_activity[j]; k++) {
766 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
767 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
768 s->scale_factor[j][k][0] = scale_table[scale_sum];
771 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
772 /* Get second scale factor */
773 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
774 s->scale_factor[j][k][1] = scale_table[scale_sum];
779 /* Joint subband scale factor codebook select */
780 for (j = base_channel; j < s->prim_channels; j++) {
781 /* Transmitted only if joint subband coding enabled */
782 if (s->joint_intensity[j] > 0)
783 s->joint_huff[j] = get_bits(&s->gb, 3);
786 if (get_bits_left(&s->gb) < 0)
787 return AVERROR_INVALIDDATA;
789 /* Scale factors for joint subband coding */
790 for (j = base_channel; j < s->prim_channels; j++) {
793 /* Transmitted only if joint subband coding enabled */
794 if (s->joint_intensity[j] > 0) {
796 source_channel = s->joint_intensity[j] - 1;
798 /* When huffman coded, only the difference is encoded
799 * (is this valid as well for joint scales ???) */
801 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
802 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
803 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
806 if (!(s->debug_flag & 0x02)) {
807 av_log(s->avctx, AV_LOG_DEBUG,
808 "Joint stereo coding not supported\n");
809 s->debug_flag |= 0x02;
814 /* Dynamic range coefficient */
815 if (!base_channel && s->dynrange)
816 s->dynrange_coef = get_bits(&s->gb, 8);
818 /* Side information CRC check word */
819 if (s->crc_present) {
820 get_bits(&s->gb, 16);
824 * Primary audio data arrays
827 /* VQ encoded high frequency subbands */
828 for (j = base_channel; j < s->prim_channels; j++)
829 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
830 /* 1 vector -> 32 samples */
831 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
833 /* Low frequency effect data */
834 if (!base_channel && s->lfe) {
836 int lfe_samples = 2 * s->lfe * (4 + block_index);
837 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
840 for (j = lfe_samples; j < lfe_end_sample; j++) {
841 /* Signed 8 bits int */
842 s->lfe_data[j] = get_sbits(&s->gb, 8);
845 /* Scale factor index */
846 skip_bits(&s->gb, 1);
847 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 7)];
849 /* Quantization step size * scale factor */
850 lfe_scale = 0.035 * s->lfe_scale_factor;
852 for (j = lfe_samples; j < lfe_end_sample; j++)
853 s->lfe_data[j] *= lfe_scale;
857 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
858 s->subsubframes[s->current_subframe]);
859 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
860 s->partial_samples[s->current_subframe]);
862 for (j = base_channel; j < s->prim_channels; j++) {
863 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
864 for (k = 0; k < s->subband_activity[j]; k++)
865 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
866 av_log(s->avctx, AV_LOG_DEBUG, "\n");
868 for (j = base_channel; j < s->prim_channels; j++) {
869 for (k = 0; k < s->subband_activity[j]; k++)
870 av_log(s->avctx, AV_LOG_DEBUG,
871 "prediction coefs: %f, %f, %f, %f\n",
872 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
873 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
874 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
875 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
877 for (j = base_channel; j < s->prim_channels; j++) {
878 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
879 for (k = 0; k < s->vq_start_subband[j]; k++)
880 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
881 av_log(s->avctx, AV_LOG_DEBUG, "\n");
883 for (j = base_channel; j < s->prim_channels; j++) {
884 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
885 for (k = 0; k < s->subband_activity[j]; k++)
886 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
887 av_log(s->avctx, AV_LOG_DEBUG, "\n");
889 for (j = base_channel; j < s->prim_channels; j++) {
890 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
891 for (k = 0; k < s->subband_activity[j]; k++) {
892 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
893 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
894 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
895 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
897 av_log(s->avctx, AV_LOG_DEBUG, "\n");
899 for (j = base_channel; j < s->prim_channels; j++) {
900 if (s->joint_intensity[j] > 0) {
901 int source_channel = s->joint_intensity[j] - 1;
902 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
903 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
904 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
905 av_log(s->avctx, AV_LOG_DEBUG, "\n");
908 for (j = base_channel; j < s->prim_channels; j++)
909 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
910 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
911 if (!base_channel && s->lfe) {
912 int lfe_samples = 2 * s->lfe * (4 + block_index);
913 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
915 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
916 for (j = lfe_samples; j < lfe_end_sample; j++)
917 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
918 av_log(s->avctx, AV_LOG_DEBUG, "\n");
925 static void qmf_32_subbands(DCAContext *s, int chans,
926 float samples_in[32][8], float *samples_out,
929 const float *prCoeff;
931 int sb_act = s->subband_activity[chans];
933 scale *= sqrt(1 / 8.0);
936 if (!s->multirate_inter) /* Non-perfect reconstruction */
937 prCoeff = fir_32bands_nonperfect;
938 else /* Perfect reconstruction */
939 prCoeff = fir_32bands_perfect;
941 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
942 s->subband_fir_hist[chans],
943 &s->hist_index[chans],
944 s->subband_fir_noidea[chans], prCoeff,
945 samples_out, s->raXin, scale);
948 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
949 int num_deci_sample, float *samples_in,
950 float *samples_out, float scale)
952 /* samples_in: An array holding decimated samples.
953 * Samples in current subframe starts from samples_in[0],
954 * while samples_in[-1], samples_in[-2], ..., stores samples
955 * from last subframe as history.
957 * samples_out: An array holding interpolated samples
961 const float *prCoeff;
964 /* Select decimation filter */
965 if (decimation_select == 1) {
967 prCoeff = lfe_fir_128;
970 prCoeff = lfe_fir_64;
973 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
974 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff, scale);
976 samples_out += 2 * 32 * (1 + idx);
980 /* downmixing routines */
981 #define MIX_REAR1(samples, s1, rs, coef) \
982 samples[0][i] += samples[s1][i] * coef[rs][0]; \
983 samples[1][i] += samples[s1][i] * coef[rs][1];
985 #define MIX_REAR2(samples, s1, s2, rs, coef) \
986 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
987 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
989 #define MIX_FRONT3(samples, coef) \
993 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
994 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
996 #define DOWNMIX_TO_STEREO(op1, op2) \
997 for (i = 0; i < 256; i++) { \
1002 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
1003 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
1004 const int8_t *channel_mapping)
1006 int c, l, r, sl, sr, s;
1013 av_log(NULL, 0, "Not implemented!\n");
1017 case DCA_STEREO_TOTAL:
1018 case DCA_STEREO_SUMDIFF:
1021 c = channel_mapping[0];
1022 l = channel_mapping[1];
1023 r = channel_mapping[2];
1024 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1027 s = channel_mapping[2];
1028 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1031 c = channel_mapping[0];
1032 l = channel_mapping[1];
1033 r = channel_mapping[2];
1034 s = channel_mapping[3];
1035 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1036 MIX_REAR1(samples, s, 3, coef));
1039 sl = channel_mapping[2];
1040 sr = channel_mapping[3];
1041 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1044 c = channel_mapping[0];
1045 l = channel_mapping[1];
1046 r = channel_mapping[2];
1047 sl = channel_mapping[3];
1048 sr = channel_mapping[4];
1049 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1050 MIX_REAR2(samples, sl, sr, 3, coef));
1054 int lf_buf = dca_lfe_index[srcfmt];
1055 int lf_idx = dca_channels [srcfmt];
1056 for (i = 0; i < 256; i++) {
1057 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1058 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1064 #ifndef decode_blockcodes
1065 /* Very compact version of the block code decoder that does not use table
1066 * look-up but is slightly slower */
1067 static int decode_blockcode(int code, int levels, int32_t *values)
1070 int offset = (levels - 1) >> 1;
1072 for (i = 0; i < 4; i++) {
1073 int div = FASTDIV(code, levels);
1074 values[i] = code - offset - div * levels;
1081 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1083 return decode_blockcode(code1, levels, values) |
1084 decode_blockcode(code2, levels, values + 4);
1088 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1089 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1091 #ifndef int8x8_fmul_int32
1092 static inline void int8x8_fmul_int32(DCADSPContext *dsp, float *dst,
1093 const int8_t *src, int scale)
1095 dsp->int8x8_fmul_int32(dst, src, scale);
1099 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1102 int subsubframe = s->current_subsubframe;
1104 const float *quant_step_table;
1107 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1108 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
1114 /* Select quantization step size table */
1115 if (s->bit_rate_index == 0x1f)
1116 quant_step_table = lossless_quant_d;
1118 quant_step_table = lossy_quant_d;
1120 for (k = base_channel; k < s->prim_channels; k++) {
1121 float rscale[DCA_SUBBANDS];
1123 if (get_bits_left(&s->gb) < 0)
1124 return AVERROR_INVALIDDATA;
1126 for (l = 0; l < s->vq_start_subband[k]; l++) {
1129 /* Select the mid-tread linear quantizer */
1130 int abits = s->bitalloc[k][l];
1132 float quant_step_size = quant_step_table[abits];
1135 * Determine quantization index code book and its type
1138 /* Select quantization index code book */
1139 int sel = s->quant_index_huffman[k][abits];
1142 * Extract bits from the bit stream
1146 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1148 /* Deal with transients */
1149 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1150 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1151 s->scalefactor_adj[k][sel];
1153 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1156 int block_code1, block_code2, size, levels, err;
1158 size = abits_sizes[abits - 1];
1159 levels = abits_levels[abits - 1];
1161 block_code1 = get_bits(&s->gb, size);
1162 block_code2 = get_bits(&s->gb, size);
1163 err = decode_blockcodes(block_code1, block_code2,
1164 levels, block + 8 * l);
1166 av_log(s->avctx, AV_LOG_ERROR,
1167 "ERROR: block code look-up failed\n");
1168 return AVERROR_INVALIDDATA;
1172 for (m = 0; m < 8; m++)
1173 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1177 for (m = 0; m < 8; m++)
1178 block[8 * l + m] = get_bitalloc(&s->gb,
1179 &dca_smpl_bitalloc[abits], sel);
1185 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1186 block, rscale, 8 * s->vq_start_subband[k]);
1188 for (l = 0; l < s->vq_start_subband[k]; l++) {
1191 * Inverse ADPCM if in prediction mode
1193 if (s->prediction_mode[k][l]) {
1195 for (m = 0; m < 8; m++) {
1196 for (n = 1; n <= 4; n++)
1198 subband_samples[k][l][m] +=
1199 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1200 subband_samples[k][l][m - n] / 8192);
1201 else if (s->predictor_history)
1202 subband_samples[k][l][m] +=
1203 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1204 s->subband_samples_hist[k][l][m - n + 4] / 8192);
1210 * Decode VQ encoded high frequencies
1212 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1213 /* 1 vector -> 32 samples but we only need the 8 samples
1214 * for this subsubframe. */
1215 int hfvq = s->high_freq_vq[k][l];
1217 if (!s->debug_flag & 0x01) {
1218 av_log(s->avctx, AV_LOG_DEBUG,
1219 "Stream with high frequencies VQ coding\n");
1220 s->debug_flag |= 0x01;
1223 int8x8_fmul_int32(&s->dcadsp, subband_samples[k][l],
1224 &high_freq_vq[hfvq][subsubframe * 8],
1225 s->scale_factor[k][l][0]);
1229 /* Check for DSYNC after subsubframe */
1230 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1231 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1233 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1236 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1237 return AVERROR_INVALIDDATA;
1241 /* Backup predictor history for adpcm */
1242 for (k = base_channel; k < s->prim_channels; k++)
1243 for (l = 0; l < s->vq_start_subband[k]; l++)
1244 memcpy(s->subband_samples_hist[k][l],
1245 &subband_samples[k][l][4],
1246 4 * sizeof(subband_samples[0][0][0]));
1251 static int dca_filter_channels(DCAContext *s, int block_index)
1253 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1256 /* 32 subbands QMF */
1257 for (k = 0; k < s->prim_channels; k++) {
1258 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1259 0, 8388608.0, 8388608.0 };*/
1260 if (s->channel_order_tab[k] >= 0)
1261 qmf_32_subbands(s, k, subband_samples[k],
1262 s->samples_chanptr[s->channel_order_tab[k]],
1263 M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1266 /* Generate LFE samples for this subsubframe FIXME!!! */
1268 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1269 s->lfe_data + 2 * s->lfe * (block_index + 4),
1270 s->samples_chanptr[dca_lfe_index[s->amode]],
1271 1.0 / (256.0 * 32768.0));
1272 /* Outputs 20bits pcm samples */
1275 /* Downmixing to Stereo */
1276 if (s->prim_channels + !!s->lfe > 2 &&
1277 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1278 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1279 s->channel_order_tab);
1286 static int dca_subframe_footer(DCAContext *s, int base_channel)
1288 int in, out, aux_data_count, aux_data_end, reserved;
1292 * Unpack optional information
1295 /* presumably optional information only appears in the core? */
1296 if (!base_channel) {
1298 skip_bits_long(&s->gb, 32);
1301 aux_data_count = get_bits(&s->gb, 6);
1304 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1306 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1308 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1309 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1311 return AVERROR_INVALIDDATA;
1314 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1315 avpriv_request_sample(s->avctx,
1316 "Auxiliary Decode Time Stamp Flag");
1318 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1319 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1320 skip_bits_long(&s->gb, 44);
1323 if ((s->core_downmix = get_bits1(&s->gb))) {
1324 int am = get_bits(&s->gb, 3);
1327 s->core_downmix_amode = DCA_MONO;
1330 s->core_downmix_amode = DCA_STEREO;
1333 s->core_downmix_amode = DCA_STEREO_TOTAL;
1336 s->core_downmix_amode = DCA_3F;
1339 s->core_downmix_amode = DCA_2F1R;
1342 s->core_downmix_amode = DCA_2F2R;
1345 s->core_downmix_amode = DCA_3F1R;
1348 av_log(s->avctx, AV_LOG_ERROR,
1349 "Invalid mode %d for embedded downmix coefficients\n",
1351 return AVERROR_INVALIDDATA;
1353 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1354 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1355 uint16_t tmp = get_bits(&s->gb, 9);
1356 if ((tmp & 0xFF) > 241) {
1357 av_log(s->avctx, AV_LOG_ERROR,
1358 "Invalid downmix coefficient code %"PRIu16"\n",
1360 return AVERROR_INVALIDDATA;
1362 s->core_downmix_codes[in][out] = tmp;
1367 align_get_bits(&s->gb); // byte align
1368 skip_bits(&s->gb, 16); // nAUXCRC16
1370 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1371 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1372 av_log(s->avctx, AV_LOG_ERROR,
1373 "Overread auxiliary data by %d bits\n", -reserved);
1374 return AVERROR_INVALIDDATA;
1375 } else if (reserved) {
1376 avpriv_request_sample(s->avctx,
1377 "Core auxiliary data reserved content");
1378 skip_bits_long(&s->gb, reserved);
1382 if (s->crc_present && s->dynrange)
1383 get_bits(&s->gb, 16);
1390 * Decode a dca frame block
1392 * @param s pointer to the DCAContext
1395 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1400 if (s->current_subframe >= s->subframes) {
1401 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1402 s->current_subframe, s->subframes);
1403 return AVERROR_INVALIDDATA;
1406 if (!s->current_subsubframe) {
1408 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1410 /* Read subframe header */
1411 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1415 /* Read subsubframe */
1417 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1419 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1423 s->current_subsubframe++;
1424 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1425 s->current_subsubframe = 0;
1426 s->current_subframe++;
1428 if (s->current_subframe >= s->subframes) {
1430 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1432 /* Read subframe footer */
1433 if ((ret = dca_subframe_footer(s, base_channel)))
1441 * Return the number of channels in an ExSS speaker mask (HD)
1443 static int dca_exss_mask2count(int mask)
1445 /* count bits that mean speaker pairs twice */
1446 return av_popcount(mask) +
1447 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1448 DCA_EXSS_FRONT_LEFT_RIGHT |
1449 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1450 DCA_EXSS_WIDE_LEFT_RIGHT |
1451 DCA_EXSS_SIDE_LEFT_RIGHT |
1452 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1453 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1454 DCA_EXSS_REAR_LEFT_RIGHT |
1455 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1459 * Skip mixing coefficients of a single mix out configuration (HD)
1461 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1465 for (i = 0; i < channels; i++) {
1466 int mix_map_mask = get_bits(gb, out_ch);
1467 int num_coeffs = av_popcount(mix_map_mask);
1468 skip_bits_long(gb, num_coeffs * 6);
1473 * Parse extension substream asset header (HD)
1475 static int dca_exss_parse_asset_header(DCAContext *s)
1477 int header_pos = get_bits_count(&s->gb);
1480 int embedded_stereo = 0;
1481 int embedded_6ch = 0;
1482 int drc_code_present;
1483 int extensions_mask;
1486 if (get_bits_left(&s->gb) < 16)
1489 /* We will parse just enough to get to the extensions bitmask with which
1490 * we can set the profile value. */
1492 header_size = get_bits(&s->gb, 9) + 1;
1493 skip_bits(&s->gb, 3); // asset index
1495 if (s->static_fields) {
1496 if (get_bits1(&s->gb))
1497 skip_bits(&s->gb, 4); // asset type descriptor
1498 if (get_bits1(&s->gb))
1499 skip_bits_long(&s->gb, 24); // language descriptor
1501 if (get_bits1(&s->gb)) {
1502 /* How can one fit 1024 bytes of text here if the maximum value
1503 * for the asset header size field above was 512 bytes? */
1504 int text_length = get_bits(&s->gb, 10) + 1;
1505 if (get_bits_left(&s->gb) < text_length * 8)
1507 skip_bits_long(&s->gb, text_length * 8); // info text
1510 skip_bits(&s->gb, 5); // bit resolution - 1
1511 skip_bits(&s->gb, 4); // max sample rate code
1512 channels = get_bits(&s->gb, 8) + 1;
1514 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1515 int spkr_remap_sets;
1516 int spkr_mask_size = 16;
1520 embedded_stereo = get_bits1(&s->gb);
1522 embedded_6ch = get_bits1(&s->gb);
1524 if (get_bits1(&s->gb)) {
1525 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1526 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1529 spkr_remap_sets = get_bits(&s->gb, 3);
1531 for (i = 0; i < spkr_remap_sets; i++) {
1532 /* std layout mask for each remap set */
1533 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1536 for (i = 0; i < spkr_remap_sets; i++) {
1537 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1538 if (get_bits_left(&s->gb) < 0)
1541 for (j = 0; j < num_spkrs[i]; j++) {
1542 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1543 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1544 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1549 skip_bits(&s->gb, 3); // representation type
1553 drc_code_present = get_bits1(&s->gb);
1554 if (drc_code_present)
1555 get_bits(&s->gb, 8); // drc code
1557 if (get_bits1(&s->gb))
1558 skip_bits(&s->gb, 5); // dialog normalization code
1560 if (drc_code_present && embedded_stereo)
1561 get_bits(&s->gb, 8); // drc stereo code
1563 if (s->mix_metadata && get_bits1(&s->gb)) {
1564 skip_bits(&s->gb, 1); // external mix
1565 skip_bits(&s->gb, 6); // post mix gain code
1567 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1568 skip_bits(&s->gb, 3); // drc limit
1570 skip_bits(&s->gb, 8); // custom drc code
1572 if (get_bits1(&s->gb)) // channel specific scaling
1573 for (i = 0; i < s->num_mix_configs; i++)
1574 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1576 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1578 for (i = 0; i < s->num_mix_configs; i++) {
1579 if (get_bits_left(&s->gb) < 0)
1581 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1583 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1584 if (embedded_stereo)
1585 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1589 switch (get_bits(&s->gb, 2)) {
1590 case 0: extensions_mask = get_bits(&s->gb, 12); break;
1591 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1592 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1593 case 3: extensions_mask = 0; /* aux coding */ break;
1596 /* not parsed further, we were only interested in the extensions mask */
1598 if (get_bits_left(&s->gb) < 0)
1601 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1602 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1605 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1607 if (extensions_mask & DCA_EXT_EXSS_XLL)
1608 s->profile = FF_PROFILE_DTS_HD_MA;
1609 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1611 s->profile = FF_PROFILE_DTS_HD_HRA;
1613 if (!(extensions_mask & DCA_EXT_CORE))
1614 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1615 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1616 av_log(s->avctx, AV_LOG_WARNING,
1617 "DTS extensions detection mismatch (%d, %d)\n",
1618 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1624 * Parse extension substream header (HD)
1626 static void dca_exss_parse_header(DCAContext *s)
1632 int active_ss_mask[8];
1635 if (get_bits_left(&s->gb) < 52)
1638 skip_bits(&s->gb, 8); // user data
1639 ss_index = get_bits(&s->gb, 2);
1641 blownup = get_bits1(&s->gb);
1642 skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1643 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1645 s->static_fields = get_bits1(&s->gb);
1646 if (s->static_fields) {
1647 skip_bits(&s->gb, 2); // reference clock code
1648 skip_bits(&s->gb, 3); // frame duration code
1650 if (get_bits1(&s->gb))
1651 skip_bits_long(&s->gb, 36); // timestamp
1653 /* a single stream can contain multiple audio assets that can be
1654 * combined to form multiple audio presentations */
1656 num_audiop = get_bits(&s->gb, 3) + 1;
1657 if (num_audiop > 1) {
1658 avpriv_request_sample(s->avctx,
1659 "Multiple DTS-HD audio presentations");
1660 /* ignore such streams for now */
1664 num_assets = get_bits(&s->gb, 3) + 1;
1665 if (num_assets > 1) {
1666 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
1667 /* ignore such streams for now */
1671 for (i = 0; i < num_audiop; i++)
1672 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1674 for (i = 0; i < num_audiop; i++)
1675 for (j = 0; j <= ss_index; j++)
1676 if (active_ss_mask[i] & (1 << j))
1677 skip_bits(&s->gb, 8); // active asset mask
1679 s->mix_metadata = get_bits1(&s->gb);
1680 if (s->mix_metadata) {
1681 int mix_out_mask_size;
1683 skip_bits(&s->gb, 2); // adjustment level
1684 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1685 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
1687 for (i = 0; i < s->num_mix_configs; i++) {
1688 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
1689 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
1694 for (i = 0; i < num_assets; i++)
1695 skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
1697 for (i = 0; i < num_assets; i++) {
1698 if (dca_exss_parse_asset_header(s))
1702 /* not parsed further, we were only interested in the extensions mask
1703 * from the asset header */
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;
1728 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1729 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1730 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1731 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1732 return AVERROR_INVALIDDATA;
1735 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
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) {
1767 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1768 * extensions scan can fill it up */
1769 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1771 /* extensions start at 32-bit boundaries into bitstream */
1772 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1774 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1775 uint32_t bits = get_bits_long(&s->gb, 32);
1779 int ext_amode, xch_fsize;
1781 s->xch_base_channel = s->prim_channels;
1783 /* validate sync word using XCHFSIZE field */
1784 xch_fsize = show_bits(&s->gb, 10);
1785 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1786 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1789 /* skip length-to-end-of-frame field for the moment */
1790 skip_bits(&s->gb, 10);
1792 s->core_ext_mask |= DCA_EXT_XCH;
1794 /* extension amode(number of channels in extension) should be 1 */
1795 /* AFAIK XCh is not used for more channels */
1796 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1797 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1798 " supported!\n", ext_amode);
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 FF_API_REQUEST_CHANNELS
1862 FF_DISABLE_DEPRECATION_WARNINGS
1863 if (s->xch_present && !s->xch_disable &&
1864 (!avctx->request_channels ||
1865 avctx->request_channels > num_core_channels + !!s->lfe)) {
1866 FF_ENABLE_DEPRECATION_WARNINGS
1868 if (s->xch_present && !s->xch_disable) {
1870 avctx->channel_layout |= AV_CH_BACK_CENTER;
1872 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1873 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1875 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1878 channels = num_core_channels + !!s->lfe;
1879 s->xch_present = 0; /* disable further xch processing */
1881 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1882 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1884 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1887 if (channels > !!s->lfe &&
1888 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1889 return AVERROR_INVALIDDATA;
1891 if (s->prim_channels + !!s->lfe > 2 &&
1892 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1894 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1895 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1897 /* Stereo downmix coefficients
1899 * The decoder can only downmix to 2-channel, so we need to ensure
1900 * embedded downmix coefficients are actually targeting 2-channel.
1902 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1903 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
1905 for (i = 0; i < s->prim_channels + !!s->lfe; i++) {
1906 sign = s->core_downmix_codes[i][0] & 0x100 ? 1 : -1;
1907 code = s->core_downmix_codes[i][0] & 0x0FF;
1908 s->downmix_coef[i][0] = (!code ? 0.0f :
1909 sign * dca_dmixtable[code - 1]);
1910 sign = s->core_downmix_codes[i][1] & 0x100 ? 1 : -1;
1911 code = s->core_downmix_codes[i][1] & 0x0FF;
1912 s->downmix_coef[i][1] = (!code ? 0.0f :
1913 sign * dca_dmixtable[code - 1]);
1915 s->output = s->core_downmix_amode;
1917 int am = s->amode & DCA_CHANNEL_MASK;
1918 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
1919 av_log(s->avctx, AV_LOG_ERROR,
1920 "Invalid channel mode %d\n", am);
1921 return AVERROR_INVALIDDATA;
1923 if (s->prim_channels + !!s->lfe >
1924 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
1925 avpriv_request_sample(s->avctx, "Downmixing %d channels",
1926 s->prim_channels + !!s->lfe);
1927 return AVERROR_PATCHWELCOME;
1929 for (i = 0; i < s->prim_channels + !!s->lfe; i++) {
1930 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
1931 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
1934 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
1935 for (i = 0; i < s->prim_channels + !!s->lfe; i++) {
1936 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
1937 s->downmix_coef[i][0]);
1938 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
1939 s->downmix_coef[i][1]);
1941 av_dlog(s->avctx, "\n");
1944 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1945 return AVERROR_INVALIDDATA;
1947 avctx->channels = channels;
1949 /* get output buffer */
1950 frame->nb_samples = 256 * (s->sample_blocks / 8);
1951 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1952 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1955 samples_flt = (float **)frame->extended_data;
1957 /* allocate buffer for extra channels if downmixing */
1958 if (avctx->channels < full_channels) {
1959 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1961 avctx->sample_fmt, 0);
1965 av_fast_malloc(&s->extra_channels_buffer,
1966 &s->extra_channels_buffer_size, ret);
1967 if (!s->extra_channels_buffer)
1968 return AVERROR(ENOMEM);
1970 ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
1971 s->extra_channels_buffer,
1972 full_channels - channels,
1973 frame->nb_samples, avctx->sample_fmt, 0);
1978 /* filter to get final output */
1979 for (i = 0; i < (s->sample_blocks / 8); i++) {
1982 for (ch = 0; ch < channels; ch++)
1983 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
1984 for (; ch < full_channels; ch++)
1985 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
1987 dca_filter_channels(s, i);
1989 /* If this was marked as a DTS-ES stream we need to subtract back- */
1990 /* channel from SL & SR to remove matrixed back-channel signal */
1991 if ((s->source_pcm_res & 1) && s->xch_present) {
1992 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1993 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1994 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1995 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1996 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2000 /* update lfe history */
2001 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2002 for (i = 0; i < 2 * s->lfe * 4; i++)
2003 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2007 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2008 ret = ff_side_data_update_matrix_encoding(frame,
2009 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2010 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,