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"
56 #define DCA_PRIM_CHANNELS_MAX (7)
57 #define DCA_ABITS_MAX (32) /* Should be 28 */
58 #define DCA_SUBSUBFRAMES_MAX (4)
59 #define DCA_SUBFRAMES_MAX (16)
60 #define DCA_BLOCKS_MAX (16)
61 #define DCA_LFE_MAX (3)
77 /* these are unconfirmed but should be mostly correct */
78 enum DCAExSSSpeakerMask {
79 DCA_EXSS_FRONT_CENTER = 0x0001,
80 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
81 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
82 DCA_EXSS_LFE = 0x0008,
83 DCA_EXSS_REAR_CENTER = 0x0010,
84 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
85 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
86 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
87 DCA_EXSS_OVERHEAD = 0x0100,
88 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
89 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
90 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
91 DCA_EXSS_LFE2 = 0x1000,
92 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
93 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
94 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
97 enum DCAExtensionMask {
98 DCA_EXT_CORE = 0x001, ///< core in core substream
99 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
100 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
101 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
102 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
103 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
104 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
105 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
106 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
107 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
110 /* -1 are reserved or unknown */
111 static const int dca_ext_audio_descr_mask[] = {
115 DCA_EXT_XCH | DCA_EXT_X96,
122 /* extensions that reside in core substream */
123 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
125 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
126 * Some compromises have been made for special configurations. Most configurations
127 * are never used so complete accuracy is not needed.
129 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
130 * S -> side, when both rear and back are configured move one of them to the side channel
132 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
134 static const uint64_t dca_core_channel_layout[] = {
135 AV_CH_FRONT_CENTER, ///< 1, A
136 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
137 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
138 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
139 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
140 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
141 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
142 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
143 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
145 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
146 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
148 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
149 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
151 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
152 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
154 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
155 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
156 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
158 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
159 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
160 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
162 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
163 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
164 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
166 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
167 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
168 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
171 static const int8_t dca_lfe_index[] = {
172 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
175 static const int8_t dca_channel_reorder_lfe[][9] = {
176 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
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 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
182 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
183 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
184 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
185 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
186 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
187 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
188 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
189 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
190 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
191 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
194 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
195 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
196 { 0, 1, 3, -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 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
201 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
202 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
203 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
204 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
205 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
206 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
207 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
208 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
209 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
210 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
213 static const int8_t dca_channel_reorder_nolfe[][9] = {
214 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
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 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
220 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
221 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
222 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
223 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
224 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
225 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
226 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
227 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
228 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
229 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
232 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
233 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
234 { 0, 1, 2, -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 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
239 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
240 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
241 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
242 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
243 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
244 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
245 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
246 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
247 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
248 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
251 #define DCA_DOLBY 101 /* FIXME */
253 #define DCA_CHANNEL_BITS 6
254 #define DCA_CHANNEL_MASK 0x3F
258 #define HEADER_SIZE 14
260 #define DCA_MAX_FRAME_SIZE 16384
261 #define DCA_MAX_EXSS_HEADER_SIZE 4096
263 #define DCA_BUFFER_PADDING_SIZE 1024
265 #define DCA_NSYNCAUX 0x9A1105A0
267 /** Bit allocation */
269 int offset; ///< code values offset
270 int maxbits[8]; ///< max bits in VLC
271 int wrap; ///< wrap for get_vlc2()
272 VLC vlc[8]; ///< actual codes
275 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
276 static BitAlloc dca_tmode; ///< transition mode VLCs
277 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
278 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
280 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
283 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
288 AVClass *class; ///< class for AVOptions
289 AVCodecContext *avctx;
291 int frame_type; ///< type of the current frame
292 int samples_deficit; ///< deficit sample count
293 int crc_present; ///< crc is present in the bitstream
294 int sample_blocks; ///< number of PCM sample blocks
295 int frame_size; ///< primary frame byte size
296 int amode; ///< audio channels arrangement
297 int sample_rate; ///< audio sampling rate
298 int bit_rate; ///< transmission bit rate
299 int bit_rate_index; ///< transmission bit rate index
301 int dynrange; ///< embedded dynamic range flag
302 int timestamp; ///< embedded time stamp flag
303 int aux_data; ///< auxiliary data flag
304 int hdcd; ///< source material is mastered in HDCD
305 int ext_descr; ///< extension audio descriptor flag
306 int ext_coding; ///< extended coding flag
307 int aspf; ///< audio sync word insertion flag
308 int lfe; ///< low frequency effects flag
309 int predictor_history; ///< predictor history flag
310 int header_crc; ///< header crc check bytes
311 int multirate_inter; ///< multirate interpolator switch
312 int version; ///< encoder software revision
313 int copy_history; ///< copy history
314 int source_pcm_res; ///< source pcm resolution
315 int front_sum; ///< front sum/difference flag
316 int surround_sum; ///< surround sum/difference flag
317 int dialog_norm; ///< dialog normalisation parameter
319 /* Primary audio coding header */
320 int subframes; ///< number of subframes
321 int total_channels; ///< number of channels including extensions
322 int prim_channels; ///< number of primary audio channels
323 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
324 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
325 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
326 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
327 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
328 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
329 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
330 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
332 /* Primary audio coding side information */
333 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
334 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
335 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
336 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
337 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
338 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
339 int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
340 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
341 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
342 float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
343 int dynrange_coef; ///< dynamic range coefficient
345 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
346 * Input: primary audio channels (incl. LFE if present)
347 * Output: downmix audio channels (up to 4, no LFE) */
348 uint8_t core_downmix; ///< embedded downmix coefficients available
349 uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
350 uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
352 int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
354 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
355 int lfe_scale_factor;
357 /* Subband samples history (for ADPCM) */
358 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
359 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
360 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
361 int hist_index[DCA_PRIM_CHANNELS_MAX];
362 DECLARE_ALIGNED(32, float, raXin)[32];
364 int output; ///< type of output
366 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
367 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
368 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
369 uint8_t *extra_channels_buffer;
370 unsigned int extra_channels_buffer_size;
372 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
373 int dca_buffer_size; ///< how much data is in the dca_buffer
375 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
377 /* Current position in DCA frame */
378 int current_subframe;
379 int current_subsubframe;
381 int core_ext_mask; ///< present extensions in the core substream
383 /* XCh extension information */
384 int xch_present; ///< XCh extension present and valid
385 int xch_base_channel; ///< index of first (only) channel containing XCH data
386 int xch_disable; ///< whether the XCh extension should be decoded or not
388 /* ExSS header parser */
389 int static_fields; ///< static fields present
390 int mix_metadata; ///< mixing metadata present
391 int num_mix_configs; ///< number of mix out configurations
392 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
396 int debug_flag; ///< used for suppressing repeated error messages output
397 AVFloatDSPContext fdsp;
399 SynthFilterContext synth;
400 DCADSPContext dcadsp;
401 FmtConvertContext fmt_conv;
404 static const uint16_t dca_vlc_offs[] = {
405 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
406 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
407 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
408 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
409 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
410 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
413 static av_cold void dca_init_vlcs(void)
415 static int vlcs_initialized = 0;
417 static VLC_TYPE dca_table[23622][2];
419 if (vlcs_initialized)
422 dca_bitalloc_index.offset = 1;
423 dca_bitalloc_index.wrap = 2;
424 for (i = 0; i < 5; i++) {
425 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
426 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
427 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
428 bitalloc_12_bits[i], 1, 1,
429 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
431 dca_scalefactor.offset = -64;
432 dca_scalefactor.wrap = 2;
433 for (i = 0; i < 5; i++) {
434 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
435 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
436 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
437 scales_bits[i], 1, 1,
438 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
440 dca_tmode.offset = 0;
442 for (i = 0; i < 4; i++) {
443 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
444 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
445 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
447 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
450 for (i = 0; i < 10; i++)
451 for (j = 0; j < 7; j++) {
452 if (!bitalloc_codes[i][j])
454 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
455 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
456 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
457 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
459 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
461 bitalloc_bits[i][j], 1, 1,
462 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
465 vlcs_initialized = 1;
468 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
471 *dst++ = get_bits(gb, bits);
474 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
477 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
478 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
479 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
481 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
482 s->prim_channels = s->total_channels;
484 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
485 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);
659 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
662 /* huffman encoded */
663 value += get_bitalloc(gb, &dca_scalefactor, level);
664 value = av_clip(value, 0, (1 << log2range) - 1);
665 } else if (level < 8) {
666 if (level + 1 > log2range) {
667 skip_bits(gb, level + 1 - log2range);
668 value = get_bits(gb, log2range);
670 value = get_bits(gb, level + 1);
676 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
678 /* Primary audio coding side information */
681 if (get_bits_left(&s->gb) < 0)
682 return AVERROR_INVALIDDATA;
685 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
686 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
689 for (j = base_channel; j < s->prim_channels; j++) {
690 for (k = 0; k < s->subband_activity[j]; k++)
691 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
694 /* Get prediction codebook */
695 for (j = base_channel; j < s->prim_channels; j++) {
696 for (k = 0; k < s->subband_activity[j]; k++) {
697 if (s->prediction_mode[j][k] > 0) {
698 /* (Prediction coefficient VQ address) */
699 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
704 /* Bit allocation index */
705 for (j = base_channel; j < s->prim_channels; j++) {
706 for (k = 0; k < s->vq_start_subband[j]; k++) {
707 if (s->bitalloc_huffman[j] == 6)
708 s->bitalloc[j][k] = get_bits(&s->gb, 5);
709 else if (s->bitalloc_huffman[j] == 5)
710 s->bitalloc[j][k] = get_bits(&s->gb, 4);
711 else if (s->bitalloc_huffman[j] == 7) {
712 av_log(s->avctx, AV_LOG_ERROR,
713 "Invalid bit allocation index\n");
714 return AVERROR_INVALIDDATA;
717 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
720 if (s->bitalloc[j][k] > 26) {
721 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
722 j, k, s->bitalloc[j][k]);
723 return AVERROR_INVALIDDATA;
728 /* Transition mode */
729 for (j = base_channel; j < s->prim_channels; j++) {
730 for (k = 0; k < s->subband_activity[j]; k++) {
731 s->transition_mode[j][k] = 0;
732 if (s->subsubframes[s->current_subframe] > 1 &&
733 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
734 s->transition_mode[j][k] =
735 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
740 if (get_bits_left(&s->gb) < 0)
741 return AVERROR_INVALIDDATA;
743 for (j = base_channel; j < s->prim_channels; j++) {
744 const uint32_t *scale_table;
745 int scale_sum, log_size;
747 memset(s->scale_factor[j], 0,
748 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
750 if (s->scalefactor_huffman[j] == 6) {
751 scale_table = scale_factor_quant7;
754 scale_table = scale_factor_quant6;
758 /* When huffman coded, only the difference is encoded */
761 for (k = 0; k < s->subband_activity[j]; k++) {
762 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
763 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
764 s->scale_factor[j][k][0] = scale_table[scale_sum];
767 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
768 /* Get second scale factor */
769 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
770 s->scale_factor[j][k][1] = scale_table[scale_sum];
775 /* Joint subband scale factor codebook select */
776 for (j = base_channel; j < s->prim_channels; j++) {
777 /* Transmitted only if joint subband coding enabled */
778 if (s->joint_intensity[j] > 0)
779 s->joint_huff[j] = get_bits(&s->gb, 3);
782 if (get_bits_left(&s->gb) < 0)
783 return AVERROR_INVALIDDATA;
785 /* Scale factors for joint subband coding */
786 for (j = base_channel; j < s->prim_channels; j++) {
789 /* Transmitted only if joint subband coding enabled */
790 if (s->joint_intensity[j] > 0) {
792 source_channel = s->joint_intensity[j] - 1;
794 /* When huffman coded, only the difference is encoded
795 * (is this valid as well for joint scales ???) */
797 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
798 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
799 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
802 if (!(s->debug_flag & 0x02)) {
803 av_log(s->avctx, AV_LOG_DEBUG,
804 "Joint stereo coding not supported\n");
805 s->debug_flag |= 0x02;
810 /* Dynamic range coefficient */
811 if (!base_channel && s->dynrange)
812 s->dynrange_coef = get_bits(&s->gb, 8);
814 /* Side information CRC check word */
815 if (s->crc_present) {
816 get_bits(&s->gb, 16);
820 * Primary audio data arrays
823 /* VQ encoded high frequency subbands */
824 for (j = base_channel; j < s->prim_channels; j++)
825 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
826 /* 1 vector -> 32 samples */
827 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
829 /* Low frequency effect data */
830 if (!base_channel && s->lfe) {
832 int lfe_samples = 2 * s->lfe * (4 + block_index);
833 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
836 for (j = lfe_samples; j < lfe_end_sample; j++) {
837 /* Signed 8 bits int */
838 s->lfe_data[j] = get_sbits(&s->gb, 8);
841 /* Scale factor index */
842 skip_bits(&s->gb, 1);
843 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 7)];
845 /* Quantization step size * scale factor */
846 lfe_scale = 0.035 * s->lfe_scale_factor;
848 for (j = lfe_samples; j < lfe_end_sample; j++)
849 s->lfe_data[j] *= lfe_scale;
853 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
854 s->subsubframes[s->current_subframe]);
855 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
856 s->partial_samples[s->current_subframe]);
858 for (j = base_channel; j < s->prim_channels; j++) {
859 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
860 for (k = 0; k < s->subband_activity[j]; k++)
861 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
862 av_log(s->avctx, AV_LOG_DEBUG, "\n");
864 for (j = base_channel; j < s->prim_channels; j++) {
865 for (k = 0; k < s->subband_activity[j]; k++)
866 av_log(s->avctx, AV_LOG_DEBUG,
867 "prediction coefs: %f, %f, %f, %f\n",
868 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
869 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
870 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
871 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
873 for (j = base_channel; j < s->prim_channels; j++) {
874 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
875 for (k = 0; k < s->vq_start_subband[j]; k++)
876 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
877 av_log(s->avctx, AV_LOG_DEBUG, "\n");
879 for (j = base_channel; j < s->prim_channels; j++) {
880 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
881 for (k = 0; k < s->subband_activity[j]; k++)
882 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
883 av_log(s->avctx, AV_LOG_DEBUG, "\n");
885 for (j = base_channel; j < s->prim_channels; j++) {
886 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
887 for (k = 0; k < s->subband_activity[j]; k++) {
888 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
889 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
890 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
891 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
893 av_log(s->avctx, AV_LOG_DEBUG, "\n");
895 for (j = base_channel; j < s->prim_channels; j++) {
896 if (s->joint_intensity[j] > 0) {
897 int source_channel = s->joint_intensity[j] - 1;
898 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
899 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
900 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
901 av_log(s->avctx, AV_LOG_DEBUG, "\n");
904 for (j = base_channel; j < s->prim_channels; j++)
905 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
906 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
907 if (!base_channel && s->lfe) {
908 int lfe_samples = 2 * s->lfe * (4 + block_index);
909 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
911 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
912 for (j = lfe_samples; j < lfe_end_sample; j++)
913 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
914 av_log(s->avctx, AV_LOG_DEBUG, "\n");
921 static void qmf_32_subbands(DCAContext *s, int chans,
922 float samples_in[32][8], float *samples_out,
925 const float *prCoeff;
927 int sb_act = s->subband_activity[chans];
929 scale *= sqrt(1 / 8.0);
932 if (!s->multirate_inter) /* Non-perfect reconstruction */
933 prCoeff = fir_32bands_nonperfect;
934 else /* Perfect reconstruction */
935 prCoeff = fir_32bands_perfect;
937 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
938 s->subband_fir_hist[chans],
939 &s->hist_index[chans],
940 s->subband_fir_noidea[chans], prCoeff,
941 samples_out, s->raXin, scale);
944 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
945 int num_deci_sample, float *samples_in,
948 /* samples_in: An array holding decimated samples.
949 * Samples in current subframe starts from samples_in[0],
950 * while samples_in[-1], samples_in[-2], ..., stores samples
951 * from last subframe as history.
953 * samples_out: An array holding interpolated samples
957 const float *prCoeff;
960 /* Select decimation filter */
961 if (decimation_select == 1) {
963 prCoeff = lfe_fir_128;
966 prCoeff = lfe_fir_64;
969 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
970 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
972 samples_out += 2 * 32 * (1 + idx);
976 /* downmixing routines */
977 #define MIX_REAR1(samples, s1, rs, coef) \
978 samples[0][i] += samples[s1][i] * coef[rs][0]; \
979 samples[1][i] += samples[s1][i] * coef[rs][1];
981 #define MIX_REAR2(samples, s1, s2, rs, coef) \
982 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
983 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
985 #define MIX_FRONT3(samples, coef) \
989 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
990 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
992 #define DOWNMIX_TO_STEREO(op1, op2) \
993 for (i = 0; i < 256; i++) { \
998 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
999 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
1000 const int8_t *channel_mapping)
1002 int c, l, r, sl, sr, s;
1009 av_log(NULL, 0, "Not implemented!\n");
1013 case DCA_STEREO_TOTAL:
1014 case DCA_STEREO_SUMDIFF:
1017 c = channel_mapping[0];
1018 l = channel_mapping[1];
1019 r = channel_mapping[2];
1020 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1023 s = channel_mapping[2];
1024 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1027 c = channel_mapping[0];
1028 l = channel_mapping[1];
1029 r = channel_mapping[2];
1030 s = channel_mapping[3];
1031 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1032 MIX_REAR1(samples, s, 3, coef));
1035 sl = channel_mapping[2];
1036 sr = channel_mapping[3];
1037 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1040 c = channel_mapping[0];
1041 l = channel_mapping[1];
1042 r = channel_mapping[2];
1043 sl = channel_mapping[3];
1044 sr = channel_mapping[4];
1045 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1046 MIX_REAR2(samples, sl, sr, 3, coef));
1050 int lf_buf = dca_lfe_index[srcfmt];
1051 int lf_idx = dca_channels [srcfmt];
1052 for (i = 0; i < 256; i++) {
1053 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1054 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1060 #ifndef decode_blockcodes
1061 /* Very compact version of the block code decoder that does not use table
1062 * look-up but is slightly slower */
1063 static int decode_blockcode(int code, int levels, int32_t *values)
1066 int offset = (levels - 1) >> 1;
1068 for (i = 0; i < 4; i++) {
1069 int div = FASTDIV(code, levels);
1070 values[i] = code - offset - div * levels;
1077 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1079 return decode_blockcode(code1, levels, values) |
1080 decode_blockcode(code2, levels, values + 4);
1084 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1085 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1087 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1090 int subsubframe = s->current_subsubframe;
1092 const float *quant_step_table;
1095 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1096 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
1102 /* Select quantization step size table */
1103 if (s->bit_rate_index == 0x1f)
1104 quant_step_table = lossless_quant_d;
1106 quant_step_table = lossy_quant_d;
1108 for (k = base_channel; k < s->prim_channels; k++) {
1109 float rscale[DCA_SUBBANDS];
1111 if (get_bits_left(&s->gb) < 0)
1112 return AVERROR_INVALIDDATA;
1114 for (l = 0; l < s->vq_start_subband[k]; l++) {
1117 /* Select the mid-tread linear quantizer */
1118 int abits = s->bitalloc[k][l];
1120 float quant_step_size = quant_step_table[abits];
1123 * Determine quantization index code book and its type
1126 /* Select quantization index code book */
1127 int sel = s->quant_index_huffman[k][abits];
1130 * Extract bits from the bit stream
1134 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1136 /* Deal with transients */
1137 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1138 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1139 s->scalefactor_adj[k][sel];
1141 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1144 int block_code1, block_code2, size, levels, err;
1146 size = abits_sizes[abits - 1];
1147 levels = abits_levels[abits - 1];
1149 block_code1 = get_bits(&s->gb, size);
1150 block_code2 = get_bits(&s->gb, size);
1151 err = decode_blockcodes(block_code1, block_code2,
1152 levels, block + 8 * l);
1154 av_log(s->avctx, AV_LOG_ERROR,
1155 "ERROR: block code look-up failed\n");
1156 return AVERROR_INVALIDDATA;
1160 for (m = 0; m < 8; m++)
1161 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1165 for (m = 0; m < 8; m++)
1166 block[8 * l + m] = get_bitalloc(&s->gb,
1167 &dca_smpl_bitalloc[abits], sel);
1173 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1174 block, rscale, 8 * s->vq_start_subband[k]);
1176 for (l = 0; l < s->vq_start_subband[k]; l++) {
1179 * Inverse ADPCM if in prediction mode
1181 if (s->prediction_mode[k][l]) {
1183 if (s->predictor_history)
1184 subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
1185 s->subband_samples_hist[k][l][3] +
1186 adpcm_vb[s->prediction_vq[k][l]][1] *
1187 s->subband_samples_hist[k][l][2] +
1188 adpcm_vb[s->prediction_vq[k][l]][2] *
1189 s->subband_samples_hist[k][l][1] +
1190 adpcm_vb[s->prediction_vq[k][l]][3] *
1191 s->subband_samples_hist[k][l][0]) *
1193 for (m = 1; m < 8; m++) {
1194 float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
1195 subband_samples[k][l][m - 1];
1196 for (n = 2; n <= 4; n++)
1198 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1199 subband_samples[k][l][m - n];
1200 else if (s->predictor_history)
1201 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1202 s->subband_samples_hist[k][l][m - n + 4];
1203 subband_samples[k][l][m] += sum * 1.0f / 8192;
1209 * Decode VQ encoded high frequencies
1211 if (s->subband_activity[k] > s->vq_start_subband[k]) {
1212 if (!s->debug_flag & 0x01) {
1213 av_log(s->avctx, AV_LOG_DEBUG,
1214 "Stream with high frequencies VQ coding\n");
1215 s->debug_flag |= 0x01;
1217 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
1218 high_freq_vq, subsubframe * 8,
1219 s->scale_factor[k], s->vq_start_subband[k],
1220 s->subband_activity[k]);
1224 /* Check for DSYNC after subsubframe */
1225 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1226 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1228 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1231 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1232 return AVERROR_INVALIDDATA;
1236 /* Backup predictor history for adpcm */
1237 for (k = base_channel; k < s->prim_channels; k++)
1238 for (l = 0; l < s->vq_start_subband[k]; l++)
1239 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1244 static int dca_filter_channels(DCAContext *s, int block_index)
1246 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1249 /* 32 subbands QMF */
1250 for (k = 0; k < s->prim_channels; k++) {
1251 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1252 0, 8388608.0, 8388608.0 };*/
1253 if (s->channel_order_tab[k] >= 0)
1254 qmf_32_subbands(s, k, subband_samples[k],
1255 s->samples_chanptr[s->channel_order_tab[k]],
1256 M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1259 /* Generate LFE samples for this subsubframe FIXME!!! */
1261 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1262 s->lfe_data + 2 * s->lfe * (block_index + 4),
1263 s->samples_chanptr[dca_lfe_index[s->amode]]);
1264 /* Outputs 20bits pcm samples */
1267 /* Downmixing to Stereo */
1268 if (s->prim_channels + !!s->lfe > 2 &&
1269 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1270 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1271 s->channel_order_tab);
1278 static int dca_subframe_footer(DCAContext *s, int base_channel)
1280 int in, out, aux_data_count, aux_data_end, reserved;
1284 * Unpack optional information
1287 /* presumably optional information only appears in the core? */
1288 if (!base_channel) {
1290 skip_bits_long(&s->gb, 32);
1293 aux_data_count = get_bits(&s->gb, 6);
1296 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1298 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1300 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1301 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1303 return AVERROR_INVALIDDATA;
1306 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1307 avpriv_request_sample(s->avctx,
1308 "Auxiliary Decode Time Stamp Flag");
1310 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1311 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1312 skip_bits_long(&s->gb, 44);
1315 if ((s->core_downmix = get_bits1(&s->gb))) {
1316 int am = get_bits(&s->gb, 3);
1319 s->core_downmix_amode = DCA_MONO;
1322 s->core_downmix_amode = DCA_STEREO;
1325 s->core_downmix_amode = DCA_STEREO_TOTAL;
1328 s->core_downmix_amode = DCA_3F;
1331 s->core_downmix_amode = DCA_2F1R;
1334 s->core_downmix_amode = DCA_2F2R;
1337 s->core_downmix_amode = DCA_3F1R;
1340 av_log(s->avctx, AV_LOG_ERROR,
1341 "Invalid mode %d for embedded downmix coefficients\n",
1343 return AVERROR_INVALIDDATA;
1345 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1346 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1347 uint16_t tmp = get_bits(&s->gb, 9);
1348 if ((tmp & 0xFF) > 241) {
1349 av_log(s->avctx, AV_LOG_ERROR,
1350 "Invalid downmix coefficient code %"PRIu16"\n",
1352 return AVERROR_INVALIDDATA;
1354 s->core_downmix_codes[in][out] = tmp;
1359 align_get_bits(&s->gb); // byte align
1360 skip_bits(&s->gb, 16); // nAUXCRC16
1362 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1363 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1364 av_log(s->avctx, AV_LOG_ERROR,
1365 "Overread auxiliary data by %d bits\n", -reserved);
1366 return AVERROR_INVALIDDATA;
1367 } else if (reserved) {
1368 avpriv_request_sample(s->avctx,
1369 "Core auxiliary data reserved content");
1370 skip_bits_long(&s->gb, reserved);
1374 if (s->crc_present && s->dynrange)
1375 get_bits(&s->gb, 16);
1382 * Decode a dca frame block
1384 * @param s pointer to the DCAContext
1387 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1392 if (s->current_subframe >= s->subframes) {
1393 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1394 s->current_subframe, s->subframes);
1395 return AVERROR_INVALIDDATA;
1398 if (!s->current_subsubframe) {
1400 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1402 /* Read subframe header */
1403 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1407 /* Read subsubframe */
1409 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1411 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1415 s->current_subsubframe++;
1416 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1417 s->current_subsubframe = 0;
1418 s->current_subframe++;
1420 if (s->current_subframe >= s->subframes) {
1422 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1424 /* Read subframe footer */
1425 if ((ret = dca_subframe_footer(s, base_channel)))
1433 * Return the number of channels in an ExSS speaker mask (HD)
1435 static int dca_exss_mask2count(int mask)
1437 /* count bits that mean speaker pairs twice */
1438 return av_popcount(mask) +
1439 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1440 DCA_EXSS_FRONT_LEFT_RIGHT |
1441 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1442 DCA_EXSS_WIDE_LEFT_RIGHT |
1443 DCA_EXSS_SIDE_LEFT_RIGHT |
1444 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1445 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1446 DCA_EXSS_REAR_LEFT_RIGHT |
1447 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1451 * Skip mixing coefficients of a single mix out configuration (HD)
1453 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1457 for (i = 0; i < channels; i++) {
1458 int mix_map_mask = get_bits(gb, out_ch);
1459 int num_coeffs = av_popcount(mix_map_mask);
1460 skip_bits_long(gb, num_coeffs * 6);
1465 * Parse extension substream asset header (HD)
1467 static int dca_exss_parse_asset_header(DCAContext *s)
1469 int header_pos = get_bits_count(&s->gb);
1472 int embedded_stereo = 0;
1473 int embedded_6ch = 0;
1474 int drc_code_present;
1475 int extensions_mask;
1478 if (get_bits_left(&s->gb) < 16)
1481 /* We will parse just enough to get to the extensions bitmask with which
1482 * we can set the profile value. */
1484 header_size = get_bits(&s->gb, 9) + 1;
1485 skip_bits(&s->gb, 3); // asset index
1487 if (s->static_fields) {
1488 if (get_bits1(&s->gb))
1489 skip_bits(&s->gb, 4); // asset type descriptor
1490 if (get_bits1(&s->gb))
1491 skip_bits_long(&s->gb, 24); // language descriptor
1493 if (get_bits1(&s->gb)) {
1494 /* How can one fit 1024 bytes of text here if the maximum value
1495 * for the asset header size field above was 512 bytes? */
1496 int text_length = get_bits(&s->gb, 10) + 1;
1497 if (get_bits_left(&s->gb) < text_length * 8)
1499 skip_bits_long(&s->gb, text_length * 8); // info text
1502 skip_bits(&s->gb, 5); // bit resolution - 1
1503 skip_bits(&s->gb, 4); // max sample rate code
1504 channels = get_bits(&s->gb, 8) + 1;
1506 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1507 int spkr_remap_sets;
1508 int spkr_mask_size = 16;
1512 embedded_stereo = get_bits1(&s->gb);
1514 embedded_6ch = get_bits1(&s->gb);
1516 if (get_bits1(&s->gb)) {
1517 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1518 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1521 spkr_remap_sets = get_bits(&s->gb, 3);
1523 for (i = 0; i < spkr_remap_sets; i++) {
1524 /* std layout mask for each remap set */
1525 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1528 for (i = 0; i < spkr_remap_sets; i++) {
1529 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1530 if (get_bits_left(&s->gb) < 0)
1533 for (j = 0; j < num_spkrs[i]; j++) {
1534 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1535 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1536 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1541 skip_bits(&s->gb, 3); // representation type
1545 drc_code_present = get_bits1(&s->gb);
1546 if (drc_code_present)
1547 get_bits(&s->gb, 8); // drc code
1549 if (get_bits1(&s->gb))
1550 skip_bits(&s->gb, 5); // dialog normalization code
1552 if (drc_code_present && embedded_stereo)
1553 get_bits(&s->gb, 8); // drc stereo code
1555 if (s->mix_metadata && get_bits1(&s->gb)) {
1556 skip_bits(&s->gb, 1); // external mix
1557 skip_bits(&s->gb, 6); // post mix gain code
1559 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1560 skip_bits(&s->gb, 3); // drc limit
1562 skip_bits(&s->gb, 8); // custom drc code
1564 if (get_bits1(&s->gb)) // channel specific scaling
1565 for (i = 0; i < s->num_mix_configs; i++)
1566 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1568 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1570 for (i = 0; i < s->num_mix_configs; i++) {
1571 if (get_bits_left(&s->gb) < 0)
1573 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1575 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1576 if (embedded_stereo)
1577 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1581 switch (get_bits(&s->gb, 2)) {
1582 case 0: extensions_mask = get_bits(&s->gb, 12); break;
1583 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1584 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1585 case 3: extensions_mask = 0; /* aux coding */ break;
1588 /* not parsed further, we were only interested in the extensions mask */
1590 if (get_bits_left(&s->gb) < 0)
1593 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1594 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1597 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1599 if (extensions_mask & DCA_EXT_EXSS_XLL)
1600 s->profile = FF_PROFILE_DTS_HD_MA;
1601 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1603 s->profile = FF_PROFILE_DTS_HD_HRA;
1605 if (!(extensions_mask & DCA_EXT_CORE))
1606 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1607 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1608 av_log(s->avctx, AV_LOG_WARNING,
1609 "DTS extensions detection mismatch (%d, %d)\n",
1610 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1616 * Parse extension substream header (HD)
1618 static void dca_exss_parse_header(DCAContext *s)
1624 int active_ss_mask[8];
1627 if (get_bits_left(&s->gb) < 52)
1630 skip_bits(&s->gb, 8); // user data
1631 ss_index = get_bits(&s->gb, 2);
1633 blownup = get_bits1(&s->gb);
1634 skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1635 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1637 s->static_fields = get_bits1(&s->gb);
1638 if (s->static_fields) {
1639 skip_bits(&s->gb, 2); // reference clock code
1640 skip_bits(&s->gb, 3); // frame duration code
1642 if (get_bits1(&s->gb))
1643 skip_bits_long(&s->gb, 36); // timestamp
1645 /* a single stream can contain multiple audio assets that can be
1646 * combined to form multiple audio presentations */
1648 num_audiop = get_bits(&s->gb, 3) + 1;
1649 if (num_audiop > 1) {
1650 avpriv_request_sample(s->avctx,
1651 "Multiple DTS-HD audio presentations");
1652 /* ignore such streams for now */
1656 num_assets = get_bits(&s->gb, 3) + 1;
1657 if (num_assets > 1) {
1658 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
1659 /* ignore such streams for now */
1663 for (i = 0; i < num_audiop; i++)
1664 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1666 for (i = 0; i < num_audiop; i++)
1667 for (j = 0; j <= ss_index; j++)
1668 if (active_ss_mask[i] & (1 << j))
1669 skip_bits(&s->gb, 8); // active asset mask
1671 s->mix_metadata = get_bits1(&s->gb);
1672 if (s->mix_metadata) {
1673 int mix_out_mask_size;
1675 skip_bits(&s->gb, 2); // adjustment level
1676 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1677 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
1679 for (i = 0; i < s->num_mix_configs; i++) {
1680 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
1681 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
1686 for (i = 0; i < num_assets; i++)
1687 skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
1689 for (i = 0; i < num_assets; i++) {
1690 if (dca_exss_parse_asset_header(s))
1694 /* not parsed further, we were only interested in the extensions mask
1695 * from the asset header */
1698 static float dca_dmix_code(unsigned code)
1700 int sign = (code >> 8) - 1;
1702 return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
1706 * Main frame decoding function
1707 * FIXME add arguments
1709 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1710 int *got_frame_ptr, AVPacket *avpkt)
1712 AVFrame *frame = data;
1713 const uint8_t *buf = avpkt->data;
1714 int buf_size = avpkt->size;
1717 int num_core_channels = 0;
1719 float **samples_flt;
1720 DCAContext *s = avctx->priv_data;
1721 int channels, full_channels;
1727 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1728 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1729 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1730 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1731 return AVERROR_INVALIDDATA;
1734 if ((ret = dca_parse_frame_header(s)) < 0) {
1735 //seems like the frame is corrupt, try with the next one
1738 //set AVCodec values with parsed data
1739 avctx->sample_rate = s->sample_rate;
1740 avctx->bit_rate = s->bit_rate;
1742 s->profile = FF_PROFILE_DTS;
1744 for (i = 0; i < (s->sample_blocks / 8); i++) {
1745 if ((ret = dca_decode_block(s, 0, i))) {
1746 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1751 /* record number of core channels incase less than max channels are requested */
1752 num_core_channels = s->prim_channels;
1755 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1757 s->core_ext_mask = 0;
1759 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1761 /* only scan for extensions if ext_descr was unknown or indicated a
1762 * supported XCh extension */
1763 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1765 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1766 * extensions scan can fill it up */
1767 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1769 /* extensions start at 32-bit boundaries into bitstream */
1770 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1772 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1773 uint32_t bits = get_bits_long(&s->gb, 32);
1777 int ext_amode, xch_fsize;
1779 s->xch_base_channel = s->prim_channels;
1781 /* validate sync word using XCHFSIZE field */
1782 xch_fsize = show_bits(&s->gb, 10);
1783 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1784 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1787 /* skip length-to-end-of-frame field for the moment */
1788 skip_bits(&s->gb, 10);
1790 s->core_ext_mask |= DCA_EXT_XCH;
1792 /* extension amode(number of channels in extension) should be 1 */
1793 /* AFAIK XCh is not used for more channels */
1794 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1795 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1796 " supported!\n", ext_amode);
1800 /* much like core primary audio coding header */
1801 dca_parse_audio_coding_header(s, s->xch_base_channel);
1803 for (i = 0; i < (s->sample_blocks / 8); i++)
1804 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1805 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1813 /* XXCh: extended channels */
1814 /* usually found either in core or HD part in DTS-HD HRA streams,
1815 * but not in DTS-ES which contains XCh extensions instead */
1816 s->core_ext_mask |= DCA_EXT_XXCH;
1820 int fsize96 = show_bits(&s->gb, 12) + 1;
1821 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1824 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1825 get_bits_count(&s->gb));
1826 skip_bits(&s->gb, 12);
1827 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1828 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1830 s->core_ext_mask |= DCA_EXT_X96;
1835 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1838 /* no supported extensions, skip the rest of the core substream */
1839 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1842 if (s->core_ext_mask & DCA_EXT_X96)
1843 s->profile = FF_PROFILE_DTS_96_24;
1844 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1845 s->profile = FF_PROFILE_DTS_ES;
1847 /* check for ExSS (HD part) */
1848 if (s->dca_buffer_size - s->frame_size > 32 &&
1849 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1850 dca_exss_parse_header(s);
1852 avctx->profile = s->profile;
1854 full_channels = channels = s->prim_channels + !!s->lfe;
1856 if (s->amode < 16) {
1857 avctx->channel_layout = dca_core_channel_layout[s->amode];
1859 if (s->prim_channels + !!s->lfe > 2 &&
1860 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1862 * Neither the core's auxiliary data nor our default tables contain
1863 * downmix coefficients for the additional channel coded in the XCh
1864 * extension, so when we're doing a Stereo downmix, don't decode it.
1869 #if FF_API_REQUEST_CHANNELS
1870 FF_DISABLE_DEPRECATION_WARNINGS
1871 if (s->xch_present && !s->xch_disable &&
1872 (!avctx->request_channels ||
1873 avctx->request_channels > num_core_channels + !!s->lfe)) {
1874 FF_ENABLE_DEPRECATION_WARNINGS
1876 if (s->xch_present && !s->xch_disable) {
1878 avctx->channel_layout |= AV_CH_BACK_CENTER;
1880 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1881 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1883 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1886 channels = num_core_channels + !!s->lfe;
1887 s->xch_present = 0; /* disable further xch processing */
1889 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1890 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1892 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1895 if (channels > !!s->lfe &&
1896 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1897 return AVERROR_INVALIDDATA;
1899 if (num_core_channels + !!s->lfe > 2 &&
1900 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1902 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1903 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1905 /* Stereo downmix coefficients
1907 * The decoder can only downmix to 2-channel, so we need to ensure
1908 * embedded downmix coefficients are actually targeting 2-channel.
1910 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1911 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
1912 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1913 /* Range checked earlier */
1914 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1915 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1917 s->output = s->core_downmix_amode;
1919 int am = s->amode & DCA_CHANNEL_MASK;
1920 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
1921 av_log(s->avctx, AV_LOG_ERROR,
1922 "Invalid channel mode %d\n", am);
1923 return AVERROR_INVALIDDATA;
1925 if (num_core_channels + !!s->lfe >
1926 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
1927 avpriv_request_sample(s->avctx, "Downmixing %d channels",
1928 s->prim_channels + !!s->lfe);
1929 return AVERROR_PATCHWELCOME;
1931 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1932 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
1933 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
1936 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
1937 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1938 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
1939 s->downmix_coef[i][0]);
1940 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
1941 s->downmix_coef[i][1]);
1943 av_dlog(s->avctx, "\n");
1946 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1947 return AVERROR_INVALIDDATA;
1949 avctx->channels = channels;
1951 /* get output buffer */
1952 frame->nb_samples = 256 * (s->sample_blocks / 8);
1953 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1954 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1957 samples_flt = (float **)frame->extended_data;
1959 /* allocate buffer for extra channels if downmixing */
1960 if (avctx->channels < full_channels) {
1961 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1963 avctx->sample_fmt, 0);
1967 av_fast_malloc(&s->extra_channels_buffer,
1968 &s->extra_channels_buffer_size, ret);
1969 if (!s->extra_channels_buffer)
1970 return AVERROR(ENOMEM);
1972 ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
1973 s->extra_channels_buffer,
1974 full_channels - channels,
1975 frame->nb_samples, avctx->sample_fmt, 0);
1980 /* filter to get final output */
1981 for (i = 0; i < (s->sample_blocks / 8); i++) {
1984 for (ch = 0; ch < channels; ch++)
1985 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
1986 for (; ch < full_channels; ch++)
1987 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
1989 dca_filter_channels(s, i);
1991 /* If this was marked as a DTS-ES stream we need to subtract back- */
1992 /* channel from SL & SR to remove matrixed back-channel signal */
1993 if ((s->source_pcm_res & 1) && s->xch_present) {
1994 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1995 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1996 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1997 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1998 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2002 /* update lfe history */
2003 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2004 for (i = 0; i < 2 * s->lfe * 4; i++)
2005 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2009 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2010 ret = ff_side_data_update_matrix_encoding(frame,
2011 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2012 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
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,