dca: handle errors from dca_decode_block()
[libav.git] / libavcodec / dca.c
1 /*
2 * DCA compatible decoder
3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
7 *
8 * This file is part of Libav.
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include <math.h>
26 #include <stddef.h>
27 #include <stdio.h>
28
29 #include "libavutil/common.h"
30 #include "libavutil/intmath.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/audioconvert.h"
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "fft.h"
36 #include "get_bits.h"
37 #include "put_bits.h"
38 #include "dcadata.h"
39 #include "dcahuff.h"
40 #include "dca.h"
41 #include "synth_filter.h"
42 #include "dcadsp.h"
43 #include "fmtconvert.h"
44
45 #if ARCH_ARM
46 # include "arm/dca.h"
47 #endif
48
49 //#define TRACE
50
51 #define DCA_PRIM_CHANNELS_MAX (7)
52 #define DCA_SUBBANDS (32)
53 #define DCA_ABITS_MAX (32) /* Should be 28 */
54 #define DCA_SUBSUBFRAMES_MAX (4)
55 #define DCA_SUBFRAMES_MAX (16)
56 #define DCA_BLOCKS_MAX (16)
57 #define DCA_LFE_MAX (3)
58
59 enum DCAMode {
60 DCA_MONO = 0,
61 DCA_CHANNEL,
62 DCA_STEREO,
63 DCA_STEREO_SUMDIFF,
64 DCA_STEREO_TOTAL,
65 DCA_3F,
66 DCA_2F1R,
67 DCA_3F1R,
68 DCA_2F2R,
69 DCA_3F2R,
70 DCA_4F2R
71 };
72
73 /* these are unconfirmed but should be mostly correct */
74 enum DCAExSSSpeakerMask {
75 DCA_EXSS_FRONT_CENTER = 0x0001,
76 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
77 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
78 DCA_EXSS_LFE = 0x0008,
79 DCA_EXSS_REAR_CENTER = 0x0010,
80 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
81 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
82 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
83 DCA_EXSS_OVERHEAD = 0x0100,
84 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
85 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
86 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
87 DCA_EXSS_LFE2 = 0x1000,
88 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
89 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
90 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
91 };
92
93 enum DCAExtensionMask {
94 DCA_EXT_CORE = 0x001, ///< core in core substream
95 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
96 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
97 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
98 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
99 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
100 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
101 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
102 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
103 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
104 };
105
106 /* -1 are reserved or unknown */
107 static const int dca_ext_audio_descr_mask[] = {
108 DCA_EXT_XCH,
109 -1,
110 DCA_EXT_X96,
111 DCA_EXT_XCH | DCA_EXT_X96,
112 -1,
113 -1,
114 DCA_EXT_XXCH,
115 -1,
116 };
117
118 /* extensions that reside in core substream */
119 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
120
121 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
122 * Some compromises have been made for special configurations. Most configurations
123 * are never used so complete accuracy is not needed.
124 *
125 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
126 * S -> side, when both rear and back are configured move one of them to the side channel
127 * OV -> center back
128 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
129 */
130
131 static const int64_t dca_core_channel_layout[] = {
132 AV_CH_FRONT_CENTER, ///< 1, A
133 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
134 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
135 AV_CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference)
136 AV_CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total)
137 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER, ///< 3, C+L+R
138 AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER, ///< 3, L+R+S
139 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER, ///< 4, C + L + R+ S
140 AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR
141 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
142 AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
143 AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER, ///< 6, C + L + R+ LR + RR + OV
144 AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_BACK_CENTER|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, ///< 6, CF+ CR+LF+ RF+LR + RR
145 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
146 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2+ SR1 + SR2
147 AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_BACK_CENTER|AV_CH_SIDE_RIGHT, ///< 8, CL + C+ CR + L + R + SL + S+ SR
148 };
149
150 static const int8_t dca_lfe_index[] = {
151 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
152 };
153
154 static const int8_t dca_channel_reorder_lfe[][9] = {
155 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
156 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
157 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
158 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
159 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
160 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
161 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
162 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
163 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
164 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
165 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
166 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
167 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
168 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
169 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
170 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
171 };
172
173 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
174 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
175 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
176 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
177 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
178 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
179 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
180 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
181 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
182 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
183 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
184 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
185 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
186 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
187 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
188 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
189 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
190 };
191
192 static const int8_t dca_channel_reorder_nolfe[][9] = {
193 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
194 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
195 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
196 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
197 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
198 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
199 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
200 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
201 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
202 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
203 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
204 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
205 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
206 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
207 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
208 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
209 };
210
211 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
212 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
213 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
214 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
215 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
216 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
217 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
218 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
219 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
220 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
221 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
222 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
223 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
224 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
225 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
226 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
227 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
228 };
229
230 #define DCA_DOLBY 101 /* FIXME */
231
232 #define DCA_CHANNEL_BITS 6
233 #define DCA_CHANNEL_MASK 0x3F
234
235 #define DCA_LFE 0x80
236
237 #define HEADER_SIZE 14
238
239 #define DCA_MAX_FRAME_SIZE 16384
240 #define DCA_MAX_EXSS_HEADER_SIZE 4096
241
242 #define DCA_BUFFER_PADDING_SIZE 1024
243
244 /** Bit allocation */
245 typedef struct {
246 int offset; ///< code values offset
247 int maxbits[8]; ///< max bits in VLC
248 int wrap; ///< wrap for get_vlc2()
249 VLC vlc[8]; ///< actual codes
250 } BitAlloc;
251
252 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
253 static BitAlloc dca_tmode; ///< transition mode VLCs
254 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
255 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
256
257 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
258 {
259 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
260 }
261
262 typedef struct {
263 AVCodecContext *avctx;
264 /* Frame header */
265 int frame_type; ///< type of the current frame
266 int samples_deficit; ///< deficit sample count
267 int crc_present; ///< crc is present in the bitstream
268 int sample_blocks; ///< number of PCM sample blocks
269 int frame_size; ///< primary frame byte size
270 int amode; ///< audio channels arrangement
271 int sample_rate; ///< audio sampling rate
272 int bit_rate; ///< transmission bit rate
273 int bit_rate_index; ///< transmission bit rate index
274
275 int downmix; ///< embedded downmix enabled
276 int dynrange; ///< embedded dynamic range flag
277 int timestamp; ///< embedded time stamp flag
278 int aux_data; ///< auxiliary data flag
279 int hdcd; ///< source material is mastered in HDCD
280 int ext_descr; ///< extension audio descriptor flag
281 int ext_coding; ///< extended coding flag
282 int aspf; ///< audio sync word insertion flag
283 int lfe; ///< low frequency effects flag
284 int predictor_history; ///< predictor history flag
285 int header_crc; ///< header crc check bytes
286 int multirate_inter; ///< multirate interpolator switch
287 int version; ///< encoder software revision
288 int copy_history; ///< copy history
289 int source_pcm_res; ///< source pcm resolution
290 int front_sum; ///< front sum/difference flag
291 int surround_sum; ///< surround sum/difference flag
292 int dialog_norm; ///< dialog normalisation parameter
293
294 /* Primary audio coding header */
295 int subframes; ///< number of subframes
296 int is_channels_set; ///< check for if the channel number is already set
297 int total_channels; ///< number of channels including extensions
298 int prim_channels; ///< number of primary audio channels
299 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
300 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
301 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
302 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
303 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
304 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
305 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
306 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
307
308 /* Primary audio coding side information */
309 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
310 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
311 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
312 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
313 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
314 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
315 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
316 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
317 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
318 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
319 int dynrange_coef; ///< dynamic range coefficient
320
321 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
322
323 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
324 int lfe_scale_factor;
325
326 /* Subband samples history (for ADPCM) */
327 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
328 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
329 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
330 int hist_index[DCA_PRIM_CHANNELS_MAX];
331 DECLARE_ALIGNED(32, float, raXin)[32];
332
333 int output; ///< type of output
334 float scale_bias; ///< output scale
335
336 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
337 DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256];
338 const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1];
339
340 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
341 int dca_buffer_size; ///< how much data is in the dca_buffer
342
343 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe
344 GetBitContext gb;
345 /* Current position in DCA frame */
346 int current_subframe;
347 int current_subsubframe;
348
349 int core_ext_mask; ///< present extensions in the core substream
350
351 /* XCh extension information */
352 int xch_present; ///< XCh extension present and valid
353 int xch_base_channel; ///< index of first (only) channel containing XCH data
354
355 /* ExSS header parser */
356 int static_fields; ///< static fields present
357 int mix_metadata; ///< mixing metadata present
358 int num_mix_configs; ///< number of mix out configurations
359 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
360
361 int profile;
362
363 int debug_flag; ///< used for suppressing repeated error messages output
364 DSPContext dsp;
365 FFTContext imdct;
366 SynthFilterContext synth;
367 DCADSPContext dcadsp;
368 FmtConvertContext fmt_conv;
369 } DCAContext;
370
371 static const uint16_t dca_vlc_offs[] = {
372 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
373 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
374 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
375 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
376 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
377 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
378 };
379
380 static av_cold void dca_init_vlcs(void)
381 {
382 static int vlcs_initialized = 0;
383 int i, j, c = 14;
384 static VLC_TYPE dca_table[23622][2];
385
386 if (vlcs_initialized)
387 return;
388
389 dca_bitalloc_index.offset = 1;
390 dca_bitalloc_index.wrap = 2;
391 for (i = 0; i < 5; i++) {
392 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
393 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
394 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
395 bitalloc_12_bits[i], 1, 1,
396 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
397 }
398 dca_scalefactor.offset = -64;
399 dca_scalefactor.wrap = 2;
400 for (i = 0; i < 5; i++) {
401 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
402 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
403 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
404 scales_bits[i], 1, 1,
405 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
406 }
407 dca_tmode.offset = 0;
408 dca_tmode.wrap = 1;
409 for (i = 0; i < 4; i++) {
410 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
411 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
412 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
413 tmode_bits[i], 1, 1,
414 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
415 }
416
417 for (i = 0; i < 10; i++)
418 for (j = 0; j < 7; j++){
419 if (!bitalloc_codes[i][j]) break;
420 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
421 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
422 dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
423 dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
424 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
425 bitalloc_sizes[i],
426 bitalloc_bits[i][j], 1, 1,
427 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
428 c++;
429 }
430 vlcs_initialized = 1;
431 }
432
433 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
434 {
435 while(len--)
436 *dst++ = get_bits(gb, bits);
437 }
438
439 static int dca_parse_audio_coding_header(DCAContext * s, int base_channel)
440 {
441 int i, j;
442 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
443 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
444 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
445
446 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
447 s->prim_channels = s->total_channels;
448
449 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
450 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
451
452
453 for (i = base_channel; i < s->prim_channels; i++) {
454 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
455 if (s->subband_activity[i] > DCA_SUBBANDS)
456 s->subband_activity[i] = DCA_SUBBANDS;
457 }
458 for (i = base_channel; i < s->prim_channels; i++) {
459 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
460 if (s->vq_start_subband[i] > DCA_SUBBANDS)
461 s->vq_start_subband[i] = DCA_SUBBANDS;
462 }
463 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
464 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
465 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
466 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
467
468 /* Get codebooks quantization indexes */
469 if (!base_channel)
470 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
471 for (j = 1; j < 11; j++)
472 for (i = base_channel; i < s->prim_channels; i++)
473 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
474
475 /* Get scale factor adjustment */
476 for (j = 0; j < 11; j++)
477 for (i = base_channel; i < s->prim_channels; i++)
478 s->scalefactor_adj[i][j] = 1;
479
480 for (j = 1; j < 11; j++)
481 for (i = base_channel; i < s->prim_channels; i++)
482 if (s->quant_index_huffman[i][j] < thr[j])
483 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
484
485 if (s->crc_present) {
486 /* Audio header CRC check */
487 get_bits(&s->gb, 16);
488 }
489
490 s->current_subframe = 0;
491 s->current_subsubframe = 0;
492
493 #ifdef TRACE
494 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
495 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
496 for (i = base_channel; i < s->prim_channels; i++){
497 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
498 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
499 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
500 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
501 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
502 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
503 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
504 for (j = 0; j < 11; j++)
505 av_log(s->avctx, AV_LOG_DEBUG, " %i",
506 s->quant_index_huffman[i][j]);
507 av_log(s->avctx, AV_LOG_DEBUG, "\n");
508 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
509 for (j = 0; j < 11; j++)
510 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
511 av_log(s->avctx, AV_LOG_DEBUG, "\n");
512 }
513 #endif
514
515 return 0;
516 }
517
518 static int dca_parse_frame_header(DCAContext * s)
519 {
520 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
521
522 /* Sync code */
523 get_bits(&s->gb, 32);
524
525 /* Frame header */
526 s->frame_type = get_bits(&s->gb, 1);
527 s->samples_deficit = get_bits(&s->gb, 5) + 1;
528 s->crc_present = get_bits(&s->gb, 1);
529 s->sample_blocks = get_bits(&s->gb, 7) + 1;
530 s->frame_size = get_bits(&s->gb, 14) + 1;
531 if (s->frame_size < 95)
532 return AVERROR_INVALIDDATA;
533 s->amode = get_bits(&s->gb, 6);
534 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
535 if (!s->sample_rate)
536 return AVERROR_INVALIDDATA;
537 s->bit_rate_index = get_bits(&s->gb, 5);
538 s->bit_rate = dca_bit_rates[s->bit_rate_index];
539 if (!s->bit_rate)
540 return AVERROR_INVALIDDATA;
541
542 s->downmix = get_bits(&s->gb, 1);
543 s->dynrange = get_bits(&s->gb, 1);
544 s->timestamp = get_bits(&s->gb, 1);
545 s->aux_data = get_bits(&s->gb, 1);
546 s->hdcd = get_bits(&s->gb, 1);
547 s->ext_descr = get_bits(&s->gb, 3);
548 s->ext_coding = get_bits(&s->gb, 1);
549 s->aspf = get_bits(&s->gb, 1);
550 s->lfe = get_bits(&s->gb, 2);
551 s->predictor_history = get_bits(&s->gb, 1);
552
553 /* TODO: check CRC */
554 if (s->crc_present)
555 s->header_crc = get_bits(&s->gb, 16);
556
557 s->multirate_inter = get_bits(&s->gb, 1);
558 s->version = get_bits(&s->gb, 4);
559 s->copy_history = get_bits(&s->gb, 2);
560 s->source_pcm_res = get_bits(&s->gb, 3);
561 s->front_sum = get_bits(&s->gb, 1);
562 s->surround_sum = get_bits(&s->gb, 1);
563 s->dialog_norm = get_bits(&s->gb, 4);
564
565 /* FIXME: channels mixing levels */
566 s->output = s->amode;
567 if (s->lfe) s->output |= DCA_LFE;
568
569 #ifdef TRACE
570 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
571 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
572 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
573 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
574 s->sample_blocks, s->sample_blocks * 32);
575 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
576 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
577 s->amode, dca_channels[s->amode]);
578 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
579 s->sample_rate);
580 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
581 s->bit_rate);
582 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
583 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
584 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
585 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
586 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
587 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
588 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
589 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
590 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
591 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
592 s->predictor_history);
593 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
594 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
595 s->multirate_inter);
596 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
597 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
598 av_log(s->avctx, AV_LOG_DEBUG,
599 "source pcm resolution: %i (%i bits/sample)\n",
600 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
601 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
602 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
603 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
604 av_log(s->avctx, AV_LOG_DEBUG, "\n");
605 #endif
606
607 /* Primary audio coding header */
608 s->subframes = get_bits(&s->gb, 4) + 1;
609
610 return dca_parse_audio_coding_header(s, 0);
611 }
612
613
614 static inline int get_scale(GetBitContext *gb, int level, int value)
615 {
616 if (level < 5) {
617 /* huffman encoded */
618 value += get_bitalloc(gb, &dca_scalefactor, level);
619 } else if (level < 8)
620 value = get_bits(gb, level + 1);
621 return value;
622 }
623
624 static int dca_subframe_header(DCAContext * s, int base_channel, int block_index)
625 {
626 /* Primary audio coding side information */
627 int j, k;
628
629 if (get_bits_left(&s->gb) < 0)
630 return AVERROR_INVALIDDATA;
631
632 if (!base_channel) {
633 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
634 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
635 }
636
637 for (j = base_channel; j < s->prim_channels; j++) {
638 for (k = 0; k < s->subband_activity[j]; k++)
639 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
640 }
641
642 /* Get prediction codebook */
643 for (j = base_channel; j < s->prim_channels; j++) {
644 for (k = 0; k < s->subband_activity[j]; k++) {
645 if (s->prediction_mode[j][k] > 0) {
646 /* (Prediction coefficient VQ address) */
647 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
648 }
649 }
650 }
651
652 /* Bit allocation index */
653 for (j = base_channel; j < s->prim_channels; j++) {
654 for (k = 0; k < s->vq_start_subband[j]; k++) {
655 if (s->bitalloc_huffman[j] == 6)
656 s->bitalloc[j][k] = get_bits(&s->gb, 5);
657 else if (s->bitalloc_huffman[j] == 5)
658 s->bitalloc[j][k] = get_bits(&s->gb, 4);
659 else if (s->bitalloc_huffman[j] == 7) {
660 av_log(s->avctx, AV_LOG_ERROR,
661 "Invalid bit allocation index\n");
662 return AVERROR_INVALIDDATA;
663 } else {
664 s->bitalloc[j][k] =
665 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
666 }
667
668 if (s->bitalloc[j][k] > 26) {
669 // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
670 // j, k, s->bitalloc[j][k]);
671 return AVERROR_INVALIDDATA;
672 }
673 }
674 }
675
676 /* Transition mode */
677 for (j = base_channel; j < s->prim_channels; j++) {
678 for (k = 0; k < s->subband_activity[j]; k++) {
679 s->transition_mode[j][k] = 0;
680 if (s->subsubframes[s->current_subframe] > 1 &&
681 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
682 s->transition_mode[j][k] =
683 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
684 }
685 }
686 }
687
688 if (get_bits_left(&s->gb) < 0)
689 return AVERROR_INVALIDDATA;
690
691 for (j = base_channel; j < s->prim_channels; j++) {
692 const uint32_t *scale_table;
693 int scale_sum;
694
695 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
696
697 if (s->scalefactor_huffman[j] == 6)
698 scale_table = scale_factor_quant7;
699 else
700 scale_table = scale_factor_quant6;
701
702 /* When huffman coded, only the difference is encoded */
703 scale_sum = 0;
704
705 for (k = 0; k < s->subband_activity[j]; k++) {
706 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
707 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
708 s->scale_factor[j][k][0] = scale_table[scale_sum];
709 }
710
711 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
712 /* Get second scale factor */
713 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
714 s->scale_factor[j][k][1] = scale_table[scale_sum];
715 }
716 }
717 }
718
719 /* Joint subband scale factor codebook select */
720 for (j = base_channel; j < s->prim_channels; j++) {
721 /* Transmitted only if joint subband coding enabled */
722 if (s->joint_intensity[j] > 0)
723 s->joint_huff[j] = get_bits(&s->gb, 3);
724 }
725
726 if (get_bits_left(&s->gb) < 0)
727 return AVERROR_INVALIDDATA;
728
729 /* Scale factors for joint subband coding */
730 for (j = base_channel; j < s->prim_channels; j++) {
731 int source_channel;
732
733 /* Transmitted only if joint subband coding enabled */
734 if (s->joint_intensity[j] > 0) {
735 int scale = 0;
736 source_channel = s->joint_intensity[j] - 1;
737
738 /* When huffman coded, only the difference is encoded
739 * (is this valid as well for joint scales ???) */
740
741 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
742 scale = get_scale(&s->gb, s->joint_huff[j], 0);
743 scale += 64; /* bias */
744 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
745 }
746
747 if (!(s->debug_flag & 0x02)) {
748 av_log(s->avctx, AV_LOG_DEBUG,
749 "Joint stereo coding not supported\n");
750 s->debug_flag |= 0x02;
751 }
752 }
753 }
754
755 /* Stereo downmix coefficients */
756 if (!base_channel && s->prim_channels > 2) {
757 if (s->downmix) {
758 for (j = base_channel; j < s->prim_channels; j++) {
759 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
760 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
761 }
762 } else {
763 int am = s->amode & DCA_CHANNEL_MASK;
764 for (j = base_channel; j < s->prim_channels; j++) {
765 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
766 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
767 }
768 }
769 }
770
771 /* Dynamic range coefficient */
772 if (!base_channel && s->dynrange)
773 s->dynrange_coef = get_bits(&s->gb, 8);
774
775 /* Side information CRC check word */
776 if (s->crc_present) {
777 get_bits(&s->gb, 16);
778 }
779
780 /*
781 * Primary audio data arrays
782 */
783
784 /* VQ encoded high frequency subbands */
785 for (j = base_channel; j < s->prim_channels; j++)
786 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
787 /* 1 vector -> 32 samples */
788 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
789
790 /* Low frequency effect data */
791 if (!base_channel && s->lfe) {
792 /* LFE samples */
793 int lfe_samples = 2 * s->lfe * (4 + block_index);
794 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
795 float lfe_scale;
796
797 for (j = lfe_samples; j < lfe_end_sample; j++) {
798 /* Signed 8 bits int */
799 s->lfe_data[j] = get_sbits(&s->gb, 8);
800 }
801
802 /* Scale factor index */
803 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
804
805 /* Quantization step size * scale factor */
806 lfe_scale = 0.035 * s->lfe_scale_factor;
807
808 for (j = lfe_samples; j < lfe_end_sample; j++)
809 s->lfe_data[j] *= lfe_scale;
810 }
811
812 #ifdef TRACE
813 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
814 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
815 s->partial_samples[s->current_subframe]);
816 for (j = base_channel; j < s->prim_channels; j++) {
817 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
818 for (k = 0; k < s->subband_activity[j]; k++)
819 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
820 av_log(s->avctx, AV_LOG_DEBUG, "\n");
821 }
822 for (j = base_channel; j < s->prim_channels; j++) {
823 for (k = 0; k < s->subband_activity[j]; k++)
824 av_log(s->avctx, AV_LOG_DEBUG,
825 "prediction coefs: %f, %f, %f, %f\n",
826 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
827 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
828 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
829 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
830 }
831 for (j = base_channel; j < s->prim_channels; j++) {
832 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
833 for (k = 0; k < s->vq_start_subband[j]; k++)
834 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
835 av_log(s->avctx, AV_LOG_DEBUG, "\n");
836 }
837 for (j = base_channel; j < s->prim_channels; j++) {
838 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
839 for (k = 0; k < s->subband_activity[j]; k++)
840 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
841 av_log(s->avctx, AV_LOG_DEBUG, "\n");
842 }
843 for (j = base_channel; j < s->prim_channels; j++) {
844 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
845 for (k = 0; k < s->subband_activity[j]; k++) {
846 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
847 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
848 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
849 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
850 }
851 av_log(s->avctx, AV_LOG_DEBUG, "\n");
852 }
853 for (j = base_channel; j < s->prim_channels; j++) {
854 if (s->joint_intensity[j] > 0) {
855 int source_channel = s->joint_intensity[j] - 1;
856 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
857 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
858 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
859 av_log(s->avctx, AV_LOG_DEBUG, "\n");
860 }
861 }
862 if (!base_channel && s->prim_channels > 2 && s->downmix) {
863 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
864 for (j = 0; j < s->prim_channels; j++) {
865 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
866 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
867 }
868 av_log(s->avctx, AV_LOG_DEBUG, "\n");
869 }
870 for (j = base_channel; j < s->prim_channels; j++)
871 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
872 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
873 if (!base_channel && s->lfe) {
874 int lfe_samples = 2 * s->lfe * (4 + block_index);
875 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
876
877 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
878 for (j = lfe_samples; j < lfe_end_sample; j++)
879 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
880 av_log(s->avctx, AV_LOG_DEBUG, "\n");
881 }
882 #endif
883
884 return 0;
885 }
886
887 static void qmf_32_subbands(DCAContext * s, int chans,
888 float samples_in[32][8], float *samples_out,
889 float scale)
890 {
891 const float *prCoeff;
892 int i;
893
894 int sb_act = s->subband_activity[chans];
895 int subindex;
896
897 scale *= sqrt(1/8.0);
898
899 /* Select filter */
900 if (!s->multirate_inter) /* Non-perfect reconstruction */
901 prCoeff = fir_32bands_nonperfect;
902 else /* Perfect reconstruction */
903 prCoeff = fir_32bands_perfect;
904
905 for (i = sb_act; i < 32; i++)
906 s->raXin[i] = 0.0;
907
908 /* Reconstructed channel sample index */
909 for (subindex = 0; subindex < 8; subindex++) {
910 /* Load in one sample from each subband and clear inactive subbands */
911 for (i = 0; i < sb_act; i++){
912 unsigned sign = (i - 1) & 2;
913 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
914 AV_WN32A(&s->raXin[i], v);
915 }
916
917 s->synth.synth_filter_float(&s->imdct,
918 s->subband_fir_hist[chans], &s->hist_index[chans],
919 s->subband_fir_noidea[chans], prCoeff,
920 samples_out, s->raXin, scale);
921 samples_out+= 32;
922
923 }
924 }
925
926 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
927 int num_deci_sample, float *samples_in,
928 float *samples_out, float scale)
929 {
930 /* samples_in: An array holding decimated samples.
931 * Samples in current subframe starts from samples_in[0],
932 * while samples_in[-1], samples_in[-2], ..., stores samples
933 * from last subframe as history.
934 *
935 * samples_out: An array holding interpolated samples
936 */
937
938 int decifactor;
939 const float *prCoeff;
940 int deciindex;
941
942 /* Select decimation filter */
943 if (decimation_select == 1) {
944 decifactor = 64;
945 prCoeff = lfe_fir_128;
946 } else {
947 decifactor = 32;
948 prCoeff = lfe_fir_64;
949 }
950 /* Interpolation */
951 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
952 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
953 scale);
954 samples_in++;
955 samples_out += 2 * decifactor;
956 }
957 }
958
959 /* downmixing routines */
960 #define MIX_REAR1(samples, si1, rs, coef) \
961 samples[i] += samples[si1] * coef[rs][0]; \
962 samples[i+256] += samples[si1] * coef[rs][1];
963
964 #define MIX_REAR2(samples, si1, si2, rs, coef) \
965 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
966 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
967
968 #define MIX_FRONT3(samples, coef) \
969 t = samples[i+c]; \
970 u = samples[i+l]; \
971 v = samples[i+r]; \
972 samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
973 samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
974
975 #define DOWNMIX_TO_STEREO(op1, op2) \
976 for (i = 0; i < 256; i++){ \
977 op1 \
978 op2 \
979 }
980
981 static void dca_downmix(float *samples, int srcfmt,
982 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
983 const int8_t *channel_mapping)
984 {
985 int c,l,r,sl,sr,s;
986 int i;
987 float t, u, v;
988 float coef[DCA_PRIM_CHANNELS_MAX][2];
989
990 for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
991 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
992 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
993 }
994
995 switch (srcfmt) {
996 case DCA_MONO:
997 case DCA_CHANNEL:
998 case DCA_STEREO_TOTAL:
999 case DCA_STEREO_SUMDIFF:
1000 case DCA_4F2R:
1001 av_log(NULL, 0, "Not implemented!\n");
1002 break;
1003 case DCA_STEREO:
1004 break;
1005 case DCA_3F:
1006 c = channel_mapping[0] * 256;
1007 l = channel_mapping[1] * 256;
1008 r = channel_mapping[2] * 256;
1009 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
1010 break;
1011 case DCA_2F1R:
1012 s = channel_mapping[2] * 256;
1013 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef),);
1014 break;
1015 case DCA_3F1R:
1016 c = channel_mapping[0] * 256;
1017 l = channel_mapping[1] * 256;
1018 r = channel_mapping[2] * 256;
1019 s = channel_mapping[3] * 256;
1020 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1021 MIX_REAR1(samples, i + s, 3, coef));
1022 break;
1023 case DCA_2F2R:
1024 sl = channel_mapping[2] * 256;
1025 sr = channel_mapping[3] * 256;
1026 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef),);
1027 break;
1028 case DCA_3F2R:
1029 c = channel_mapping[0] * 256;
1030 l = channel_mapping[1] * 256;
1031 r = channel_mapping[2] * 256;
1032 sl = channel_mapping[3] * 256;
1033 sr = channel_mapping[4] * 256;
1034 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1035 MIX_REAR2(samples, i + sl, i + sr, 3, coef));
1036 break;
1037 }
1038 }
1039
1040
1041 /* Very compact version of the block code decoder that does not use table
1042 * look-up but is slightly slower */
1043 static int decode_blockcode(int code, int levels, int *values)
1044 {
1045 int i;
1046 int offset = (levels - 1) >> 1;
1047
1048 for (i = 0; i < 4; i++) {
1049 int div = FASTDIV(code, levels);
1050 values[i] = code - offset - div*levels;
1051 code = div;
1052 }
1053
1054 if (code == 0)
1055 return 0;
1056 else {
1057 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
1058 return AVERROR_INVALIDDATA;
1059 }
1060 }
1061
1062 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1063 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1064
1065 #ifndef int8x8_fmul_int32
1066 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1067 {
1068 float fscale = scale / 16.0;
1069 int i;
1070 for (i = 0; i < 8; i++)
1071 dst[i] = src[i] * fscale;
1072 }
1073 #endif
1074
1075 static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
1076 {
1077 int k, l;
1078 int subsubframe = s->current_subsubframe;
1079
1080 const float *quant_step_table;
1081
1082 /* FIXME */
1083 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1084 LOCAL_ALIGNED_16(int, block, [8]);
1085
1086 /*
1087 * Audio data
1088 */
1089
1090 /* Select quantization step size table */
1091 if (s->bit_rate_index == 0x1f)
1092 quant_step_table = lossless_quant_d;
1093 else
1094 quant_step_table = lossy_quant_d;
1095
1096 for (k = base_channel; k < s->prim_channels; k++) {
1097 if (get_bits_left(&s->gb) < 0)
1098 return AVERROR_INVALIDDATA;
1099
1100 for (l = 0; l < s->vq_start_subband[k]; l++) {
1101 int m;
1102
1103 /* Select the mid-tread linear quantizer */
1104 int abits = s->bitalloc[k][l];
1105
1106 float quant_step_size = quant_step_table[abits];
1107
1108 /*
1109 * Determine quantization index code book and its type
1110 */
1111
1112 /* Select quantization index code book */
1113 int sel = s->quant_index_huffman[k][abits];
1114
1115 /*
1116 * Extract bits from the bit stream
1117 */
1118 if (!abits){
1119 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1120 } else {
1121 /* Deal with transients */
1122 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1123 float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
1124
1125 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
1126 if (abits <= 7){
1127 /* Block code */
1128 int block_code1, block_code2, size, levels;
1129
1130 size = abits_sizes[abits-1];
1131 levels = abits_levels[abits-1];
1132
1133 block_code1 = get_bits(&s->gb, size);
1134 /* FIXME Should test return value */
1135 decode_blockcode(block_code1, levels, block);
1136 block_code2 = get_bits(&s->gb, size);
1137 decode_blockcode(block_code2, levels, &block[4]);
1138 }else{
1139 /* no coding */
1140 for (m = 0; m < 8; m++)
1141 block[m] = get_sbits(&s->gb, abits - 3);
1142 }
1143 }else{
1144 /* Huffman coded */
1145 for (m = 0; m < 8; m++)
1146 block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
1147 }
1148
1149 s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1150 block, rscale, 8);
1151 }
1152
1153 /*
1154 * Inverse ADPCM if in prediction mode
1155 */
1156 if (s->prediction_mode[k][l]) {
1157 int n;
1158 for (m = 0; m < 8; m++) {
1159 for (n = 1; n <= 4; n++)
1160 if (m >= n)
1161 subband_samples[k][l][m] +=
1162 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1163 subband_samples[k][l][m - n] / 8192);
1164 else if (s->predictor_history)
1165 subband_samples[k][l][m] +=
1166 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1167 s->subband_samples_hist[k][l][m - n +
1168 4] / 8192);
1169 }
1170 }
1171 }
1172
1173 /*
1174 * Decode VQ encoded high frequencies
1175 */
1176 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1177 /* 1 vector -> 32 samples but we only need the 8 samples
1178 * for this subsubframe. */
1179 int hfvq = s->high_freq_vq[k][l];
1180
1181 if (!s->debug_flag & 0x01) {
1182 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
1183 s->debug_flag |= 0x01;
1184 }
1185
1186 int8x8_fmul_int32(subband_samples[k][l],
1187 &high_freq_vq[hfvq][subsubframe * 8],
1188 s->scale_factor[k][l][0]);
1189 }
1190 }
1191
1192 /* Check for DSYNC after subsubframe */
1193 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1194 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1195 #ifdef TRACE
1196 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1197 #endif
1198 } else {
1199 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1200 }
1201 }
1202
1203 /* Backup predictor history for adpcm */
1204 for (k = base_channel; k < s->prim_channels; k++)
1205 for (l = 0; l < s->vq_start_subband[k]; l++)
1206 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
1207 4 * sizeof(subband_samples[0][0][0]));
1208
1209 return 0;
1210 }
1211
1212 static int dca_filter_channels(DCAContext * s, int block_index)
1213 {
1214 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1215 int k;
1216
1217 /* 32 subbands QMF */
1218 for (k = 0; k < s->prim_channels; k++) {
1219 /* static float pcm_to_double[8] =
1220 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
1221 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
1222 M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ );
1223 }
1224
1225 /* Down mixing */
1226 if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1227 dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
1228 }
1229
1230 /* Generate LFE samples for this subsubframe FIXME!!! */
1231 if (s->output & DCA_LFE) {
1232 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1233 s->lfe_data + 2 * s->lfe * (block_index + 4),
1234 &s->samples[256 * dca_lfe_index[s->amode]],
1235 (1.0/256.0)*s->scale_bias);
1236 /* Outputs 20bits pcm samples */
1237 }
1238
1239 return 0;
1240 }
1241
1242
1243 static int dca_subframe_footer(DCAContext * s, int base_channel)
1244 {
1245 int aux_data_count = 0, i;
1246
1247 /*
1248 * Unpack optional information
1249 */
1250
1251 /* presumably optional information only appears in the core? */
1252 if (!base_channel) {
1253 if (s->timestamp)
1254 get_bits(&s->gb, 32);
1255
1256 if (s->aux_data)
1257 aux_data_count = get_bits(&s->gb, 6);
1258
1259 for (i = 0; i < aux_data_count; i++)
1260 get_bits(&s->gb, 8);
1261
1262 if (s->crc_present && (s->downmix || s->dynrange))
1263 get_bits(&s->gb, 16);
1264 }
1265
1266 return 0;
1267 }
1268
1269 /**
1270 * Decode a dca frame block
1271 *
1272 * @param s pointer to the DCAContext
1273 */
1274
1275 static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
1276 {
1277 int ret;
1278
1279 /* Sanity check */
1280 if (s->current_subframe >= s->subframes) {
1281 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1282 s->current_subframe, s->subframes);
1283 return AVERROR_INVALIDDATA;
1284 }
1285
1286 if (!s->current_subsubframe) {
1287 #ifdef TRACE
1288 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1289 #endif
1290 /* Read subframe header */
1291 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1292 return ret;
1293 }
1294
1295 /* Read subsubframe */
1296 #ifdef TRACE
1297 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1298 #endif
1299 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1300 return ret;
1301
1302 /* Update state */
1303 s->current_subsubframe++;
1304 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1305 s->current_subsubframe = 0;
1306 s->current_subframe++;
1307 }
1308 if (s->current_subframe >= s->subframes) {
1309 #ifdef TRACE
1310 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1311 #endif
1312 /* Read subframe footer */
1313 if ((ret = dca_subframe_footer(s, base_channel)))
1314 return ret;
1315 }
1316
1317 return 0;
1318 }
1319
1320 /**
1321 * Convert bitstream to one representation based on sync marker
1322 */
1323 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
1324 int max_size)
1325 {
1326 uint32_t mrk;
1327 int i, tmp;
1328 const uint16_t *ssrc = (const uint16_t *) src;
1329 uint16_t *sdst = (uint16_t *) dst;
1330 PutBitContext pb;
1331
1332 if ((unsigned)src_size > (unsigned)max_size) {
1333 // av_log(NULL, AV_LOG_ERROR, "Input frame size larger than DCA_MAX_FRAME_SIZE!\n");
1334 // return -1;
1335 src_size = max_size;
1336 }
1337
1338 mrk = AV_RB32(src);
1339 switch (mrk) {
1340 case DCA_MARKER_RAW_BE:
1341 memcpy(dst, src, src_size);
1342 return src_size;
1343 case DCA_MARKER_RAW_LE:
1344 for (i = 0; i < (src_size + 1) >> 1; i++)
1345 *sdst++ = av_bswap16(*ssrc++);
1346 return src_size;
1347 case DCA_MARKER_14B_BE:
1348 case DCA_MARKER_14B_LE:
1349 init_put_bits(&pb, dst, max_size);
1350 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1351 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1352 put_bits(&pb, 14, tmp);
1353 }
1354 flush_put_bits(&pb);
1355 return (put_bits_count(&pb) + 7) >> 3;
1356 default:
1357 return AVERROR_INVALIDDATA;
1358 }
1359 }
1360
1361 /**
1362 * Return the number of channels in an ExSS speaker mask (HD)
1363 */
1364 static int dca_exss_mask2count(int mask)
1365 {
1366 /* count bits that mean speaker pairs twice */
1367 return av_popcount(mask)
1368 + av_popcount(mask & (
1369 DCA_EXSS_CENTER_LEFT_RIGHT
1370 | DCA_EXSS_FRONT_LEFT_RIGHT
1371 | DCA_EXSS_FRONT_HIGH_LEFT_RIGHT
1372 | DCA_EXSS_WIDE_LEFT_RIGHT
1373 | DCA_EXSS_SIDE_LEFT_RIGHT
1374 | DCA_EXSS_SIDE_HIGH_LEFT_RIGHT
1375 | DCA_EXSS_SIDE_REAR_LEFT_RIGHT
1376 | DCA_EXSS_REAR_LEFT_RIGHT
1377 | DCA_EXSS_REAR_HIGH_LEFT_RIGHT
1378 ));
1379 }
1380
1381 /**
1382 * Skip mixing coefficients of a single mix out configuration (HD)
1383 */
1384 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1385 {
1386 int i;
1387
1388 for (i = 0; i < channels; i++) {
1389 int mix_map_mask = get_bits(gb, out_ch);
1390 int num_coeffs = av_popcount(mix_map_mask);
1391 skip_bits_long(gb, num_coeffs * 6);
1392 }
1393 }
1394
1395 /**
1396 * Parse extension substream asset header (HD)
1397 */
1398 static int dca_exss_parse_asset_header(DCAContext *s)
1399 {
1400 int header_pos = get_bits_count(&s->gb);
1401 int header_size;
1402 int channels;
1403 int embedded_stereo = 0;
1404 int embedded_6ch = 0;
1405 int drc_code_present;
1406 int extensions_mask;
1407 int i, j;
1408
1409 if (get_bits_left(&s->gb) < 16)
1410 return -1;
1411
1412 /* We will parse just enough to get to the extensions bitmask with which
1413 * we can set the profile value. */
1414
1415 header_size = get_bits(&s->gb, 9) + 1;
1416 skip_bits(&s->gb, 3); // asset index
1417
1418 if (s->static_fields) {
1419 if (get_bits1(&s->gb))
1420 skip_bits(&s->gb, 4); // asset type descriptor
1421 if (get_bits1(&s->gb))
1422 skip_bits_long(&s->gb, 24); // language descriptor
1423
1424 if (get_bits1(&s->gb)) {
1425 /* How can one fit 1024 bytes of text here if the maximum value
1426 * for the asset header size field above was 512 bytes? */
1427 int text_length = get_bits(&s->gb, 10) + 1;
1428 if (get_bits_left(&s->gb) < text_length * 8)
1429 return -1;
1430 skip_bits_long(&s->gb, text_length * 8); // info text
1431 }
1432
1433 skip_bits(&s->gb, 5); // bit resolution - 1
1434 skip_bits(&s->gb, 4); // max sample rate code
1435 channels = get_bits(&s->gb, 8) + 1;
1436
1437 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1438 int spkr_remap_sets;
1439 int spkr_mask_size = 16;
1440 int num_spkrs[7];
1441
1442 if (channels > 2)
1443 embedded_stereo = get_bits1(&s->gb);
1444 if (channels > 6)
1445 embedded_6ch = get_bits1(&s->gb);
1446
1447 if (get_bits1(&s->gb)) {
1448 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1449 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1450 }
1451
1452 spkr_remap_sets = get_bits(&s->gb, 3);
1453
1454 for (i = 0; i < spkr_remap_sets; i++) {
1455 /* std layout mask for each remap set */
1456 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1457 }
1458
1459 for (i = 0; i < spkr_remap_sets; i++) {
1460 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1461 if (get_bits_left(&s->gb) < 0)
1462 return -1;
1463
1464 for (j = 0; j < num_spkrs[i]; j++) {
1465 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1466 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1467 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1468 }
1469 }
1470
1471 } else {
1472 skip_bits(&s->gb, 3); // representation type
1473 }
1474 }
1475
1476 drc_code_present = get_bits1(&s->gb);
1477 if (drc_code_present)
1478 get_bits(&s->gb, 8); // drc code
1479
1480 if (get_bits1(&s->gb))
1481 skip_bits(&s->gb, 5); // dialog normalization code
1482
1483 if (drc_code_present && embedded_stereo)
1484 get_bits(&s->gb, 8); // drc stereo code
1485
1486 if (s->mix_metadata && get_bits1(&s->gb)) {
1487 skip_bits(&s->gb, 1); // external mix
1488 skip_bits(&s->gb, 6); // post mix gain code
1489
1490 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1491 skip_bits(&s->gb, 3); // drc limit
1492 else
1493 skip_bits(&s->gb, 8); // custom drc code
1494
1495 if (get_bits1(&s->gb)) // channel specific scaling
1496 for (i = 0; i < s->num_mix_configs; i++)
1497 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1498 else
1499 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1500
1501 for (i = 0; i < s->num_mix_configs; i++) {
1502 if (get_bits_left(&s->gb) < 0)
1503 return -1;
1504 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1505 if (embedded_6ch)
1506 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1507 if (embedded_stereo)
1508 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1509 }
1510 }
1511
1512 switch (get_bits(&s->gb, 2)) {
1513 case 0: extensions_mask = get_bits(&s->gb, 12); break;
1514 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1515 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1516 case 3: extensions_mask = 0; /* aux coding */ break;
1517 }
1518
1519 /* not parsed further, we were only interested in the extensions mask */
1520
1521 if (get_bits_left(&s->gb) < 0)
1522 return -1;
1523
1524 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1525 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1526 return -1;
1527 }
1528 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1529
1530 if (extensions_mask & DCA_EXT_EXSS_XLL)
1531 s->profile = FF_PROFILE_DTS_HD_MA;
1532 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1533 DCA_EXT_EXSS_XXCH))
1534 s->profile = FF_PROFILE_DTS_HD_HRA;
1535
1536 if (!(extensions_mask & DCA_EXT_CORE))
1537 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1538 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1539 av_log(s->avctx, AV_LOG_WARNING, "DTS extensions detection mismatch (%d, %d)\n",
1540 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1541
1542 return 0;
1543 }
1544
1545 /**
1546 * Parse extension substream header (HD)
1547 */
1548 static void dca_exss_parse_header(DCAContext *s)
1549 {
1550 int ss_index;
1551 int blownup;
1552 int num_audiop = 1;
1553 int num_assets = 1;
1554 int active_ss_mask[8];
1555 int i, j;
1556
1557 if (get_bits_left(&s->gb) < 52)
1558 return;
1559
1560 skip_bits(&s->gb, 8); // user data
1561 ss_index = get_bits(&s->gb, 2);
1562
1563 blownup = get_bits1(&s->gb);
1564 skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1565 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1566
1567 s->static_fields = get_bits1(&s->gb);
1568 if (s->static_fields) {
1569 skip_bits(&s->gb, 2); // reference clock code
1570 skip_bits(&s->gb, 3); // frame duration code
1571
1572 if (get_bits1(&s->gb))
1573 skip_bits_long(&s->gb, 36); // timestamp
1574
1575 /* a single stream can contain multiple audio assets that can be
1576 * combined to form multiple audio presentations */
1577
1578 num_audiop = get_bits(&s->gb, 3) + 1;
1579 if (num_audiop > 1) {
1580 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
1581 /* ignore such streams for now */
1582 return;
1583 }
1584
1585 num_assets = get_bits(&s->gb, 3) + 1;
1586 if (num_assets > 1) {
1587 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
1588 /* ignore such streams for now */
1589 return;
1590 }
1591
1592 for (i = 0; i < num_audiop; i++)
1593 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1594
1595 for (i = 0; i < num_audiop; i++)
1596 for (j = 0; j <= ss_index; j++)
1597 if (active_ss_mask[i] & (1 << j))
1598 skip_bits(&s->gb, 8); // active asset mask
1599
1600 s->mix_metadata = get_bits1(&s->gb);
1601 if (s->mix_metadata) {
1602 int mix_out_mask_size;
1603
1604 skip_bits(&s->gb, 2); // adjustment level
1605 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1606 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
1607
1608 for (i = 0; i < s->num_mix_configs; i++) {
1609 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
1610 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
1611 }
1612 }
1613 }
1614
1615 for (i = 0; i < num_assets; i++)
1616 skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
1617
1618 for (i = 0; i < num_assets; i++) {
1619 if (dca_exss_parse_asset_header(s))
1620 return;
1621 }
1622
1623 /* not parsed further, we were only interested in the extensions mask
1624 * from the asset header */
1625 }
1626
1627 /**
1628 * Main frame decoding function
1629 * FIXME add arguments
1630 */
1631 static int dca_decode_frame(AVCodecContext * avctx,
1632 void *data, int *data_size,
1633 AVPacket *avpkt)
1634 {
1635 const uint8_t *buf = avpkt->data;
1636 int buf_size = avpkt->size;
1637
1638 int lfe_samples;
1639 int num_core_channels = 0;
1640 int i, ret;
1641 float *samples_flt = data;
1642 int16_t *samples_s16 = data;
1643 int out_size;
1644 DCAContext *s = avctx->priv_data;
1645 int channels;
1646 int core_ss_end;
1647
1648
1649 s->xch_present = 0;
1650
1651 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1652 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1653 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1654 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1655 return AVERROR_INVALIDDATA;
1656 }
1657
1658 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1659 if ((ret = dca_parse_frame_header(s)) < 0) {
1660 //seems like the frame is corrupt, try with the next one
1661 return ret;
1662 }
1663 //set AVCodec values with parsed data
1664 avctx->sample_rate = s->sample_rate;
1665 avctx->bit_rate = s->bit_rate;
1666 avctx->frame_size = s->sample_blocks * 32;
1667
1668 s->profile = FF_PROFILE_DTS;
1669
1670 for (i = 0; i < (s->sample_blocks / 8); i++) {
1671 if ((ret = dca_decode_block(s, 0, i))) {
1672 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1673 return ret;
1674 }
1675 }
1676
1677 /* record number of core channels incase less than max channels are requested */
1678 num_core_channels = s->prim_channels;
1679
1680 if (s->ext_coding)
1681 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1682 else
1683 s->core_ext_mask = 0;
1684
1685 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1686
1687 /* only scan for extensions if ext_descr was unknown or indicated a
1688 * supported XCh extension */
1689 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1690
1691 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1692 * extensions scan can fill it up */
1693 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1694
1695 /* extensions start at 32-bit boundaries into bitstream */
1696 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1697
1698 while(core_ss_end - get_bits_count(&s->gb) >= 32) {
1699 uint32_t bits = get_bits_long(&s->gb, 32);
1700
1701 switch(bits) {
1702 case 0x5a5a5a5a: {
1703 int ext_amode, xch_fsize;
1704
1705 s->xch_base_channel = s->prim_channels;
1706
1707 /* validate sync word using XCHFSIZE field */
1708 xch_fsize = show_bits(&s->gb, 10);
1709 if((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1710 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1711 continue;
1712
1713 /* skip length-to-end-of-frame field for the moment */
1714 skip_bits(&s->gb, 10);
1715
1716 s->core_ext_mask |= DCA_EXT_XCH;
1717
1718 /* extension amode should == 1, number of channels in extension */
1719 /* AFAIK XCh is not used for more channels */
1720 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1721 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1722 " supported!\n",ext_amode);
1723 continue;
1724 }
1725
1726 /* much like core primary audio coding header */
1727 dca_parse_audio_coding_header(s, s->xch_base_channel);
1728
1729 for (i = 0; i < (s->sample_blocks / 8); i++) {
1730 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1731 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1732 continue;
1733 }
1734 }
1735
1736 s->xch_present = 1;
1737 break;
1738 }
1739 case 0x47004a03:
1740 /* XXCh: extended channels */
1741 /* usually found either in core or HD part in DTS-HD HRA streams,
1742 * but not in DTS-ES which contains XCh extensions instead */
1743 s->core_ext_mask |= DCA_EXT_XXCH;
1744 break;
1745
1746 case 0x1d95f262: {
1747 int fsize96 = show_bits(&s->gb, 12) + 1;
1748 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1749 continue;
1750
1751 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n", get_bits_count(&s->gb));
1752 skip_bits(&s->gb, 12);
1753 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1754 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1755
1756 s->core_ext_mask |= DCA_EXT_X96;
1757 break;
1758 }
1759 }
1760
1761 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1762 }
1763
1764 } else {
1765 /* no supported extensions, skip the rest of the core substream */
1766 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1767 }
1768
1769 if (s->core_ext_mask & DCA_EXT_X96)
1770 s->profile = FF_PROFILE_DTS_96_24;
1771 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1772 s->profile = FF_PROFILE_DTS_ES;
1773
1774 /* check for ExSS (HD part) */
1775 if (s->dca_buffer_size - s->frame_size > 32
1776 && get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1777 dca_exss_parse_header(s);
1778
1779 avctx->profile = s->profile;
1780
1781 channels = s->prim_channels + !!s->lfe;
1782
1783 if (s->amode<16) {
1784 avctx->channel_layout = dca_core_channel_layout[s->amode];
1785
1786 if (s->xch_present && (!avctx->request_channels ||
1787 avctx->request_channels > num_core_channels + !!s->lfe)) {
1788 avctx->channel_layout |= AV_CH_BACK_CENTER;
1789 if (s->lfe) {
1790 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1791 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1792 } else {
1793 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1794 }
1795 } else {
1796 channels = num_core_channels + !!s->lfe;
1797 s->xch_present = 0; /* disable further xch processing */
1798 if (s->lfe) {
1799 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1800 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1801 } else
1802 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1803 }
1804
1805 if (channels > !!s->lfe &&
1806 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1807 return AVERROR_INVALIDDATA;
1808
1809 if (avctx->request_channels == 2 && s->prim_channels > 2) {
1810 channels = 2;
1811 s->output = DCA_STEREO;
1812 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1813 }
1814 } else {
1815 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
1816 return AVERROR_INVALIDDATA;
1817 }
1818
1819
1820 /* There is nothing that prevents a dts frame to change channel configuration
1821 but Libav doesn't support that so only set the channels if it is previously
1822 unset. Ideally during the first probe for channels the crc should be checked
1823 and only set avctx->channels when the crc is ok. Right now the decoder could
1824 set the channels based on a broken first frame.*/
1825 if (s->is_channels_set == 0) {
1826 s->is_channels_set = 1;
1827 avctx->channels = channels;
1828 }
1829 if (avctx->channels != channels) {
1830 av_log(avctx, AV_LOG_ERROR, "DCA decoder does not support number of "
1831 "channels changing in stream. Skipping frame.\n");
1832 return AVERROR_PATCHWELCOME;
1833 }
1834
1835 out_size = 256 / 8 * s->sample_blocks * channels *
1836 av_get_bytes_per_sample(avctx->sample_fmt);
1837 if (*data_size < out_size)
1838 return AVERROR(EINVAL);
1839 *data_size = out_size;
1840
1841 /* filter to get final output */
1842 for (i = 0; i < (s->sample_blocks / 8); i++) {
1843 dca_filter_channels(s, i);
1844
1845 /* If this was marked as a DTS-ES stream we need to subtract back- */
1846 /* channel from SL & SR to remove matrixed back-channel signal */
1847 if((s->source_pcm_res & 1) && s->xch_present) {
1848 float* back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
1849 float* lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
1850 float* rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
1851 s->dsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1852 s->dsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1853 }
1854
1855 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
1856 s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
1857 channels);
1858 samples_flt += 256 * channels;
1859 } else {
1860 s->fmt_conv.float_to_int16_interleave(samples_s16,
1861 s->samples_chanptr, 256,
1862 channels);
1863 samples_s16 += 256 * channels;
1864 }
1865 }
1866
1867 /* update lfe history */
1868 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1869 for (i = 0; i < 2 * s->lfe * 4; i++) {
1870 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1871 }
1872
1873 return buf_size;
1874 }
1875
1876
1877
1878 /**
1879 * DCA initialization
1880 *
1881 * @param avctx pointer to the AVCodecContext
1882 */
1883
1884 static av_cold int dca_decode_init(AVCodecContext * avctx)
1885 {
1886 DCAContext *s = avctx->priv_data;
1887 int i;
1888
1889 s->avctx = avctx;
1890 dca_init_vlcs();
1891
1892 dsputil_init(&s->dsp, avctx);
1893 ff_mdct_init(&s->imdct, 6, 1, 1.0);
1894 ff_synth_filter_init(&s->synth);
1895 ff_dcadsp_init(&s->dcadsp);
1896 ff_fmt_convert_init(&s->fmt_conv, avctx);
1897
1898 for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++)
1899 s->samples_chanptr[i] = s->samples + i * 256;
1900
1901 if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
1902 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
1903 s->scale_bias = 1.0 / 32768.0;
1904 } else {
1905 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1906 s->scale_bias = 1.0;
1907 }
1908
1909 /* allow downmixing to stereo */
1910 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1911 avctx->request_channels == 2) {
1912 avctx->channels = avctx->request_channels;
1913 }
1914
1915 return 0;
1916 }
1917
1918 static av_cold int dca_decode_end(AVCodecContext * avctx)
1919 {
1920 DCAContext *s = avctx->priv_data;
1921 ff_mdct_end(&s->imdct);
1922 return 0;
1923 }
1924
1925 static const AVProfile profiles[] = {
1926 { FF_PROFILE_DTS, "DTS" },
1927 { FF_PROFILE_DTS_ES, "DTS-ES" },
1928 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
1929 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
1930 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
1931 { FF_PROFILE_UNKNOWN },
1932 };
1933
1934 AVCodec ff_dca_decoder = {
1935 .name = "dca",
1936 .type = AVMEDIA_TYPE_AUDIO,
1937 .id = CODEC_ID_DTS,
1938 .priv_data_size = sizeof(DCAContext),
1939 .init = dca_decode_init,
1940 .decode = dca_decode_frame,
1941 .close = dca_decode_end,
1942 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
1943 .capabilities = CODEC_CAP_CHANNEL_CONF,
1944 .sample_fmts = (const enum AVSampleFormat[]) {
1945 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
1946 },
1947 .profiles = NULL_IF_CONFIG_SMALL(profiles),
1948 };