Imported Debian version 2.5.3~trusty1
[deb_ffmpeg.git] / ffmpeg / libavcodec / dcadec.c
CommitLineData
2ba45a60
DM
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 FFmpeg.
9 *
10 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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/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"
f6fa7814 37
2ba45a60 38#include "avcodec.h"
2ba45a60 39#include "dca.h"
f6fa7814 40#include "dcadata.h"
2ba45a60 41#include "dcadsp.h"
f6fa7814
DM
42#include "dcahuff.h"
43#include "fft.h"
2ba45a60 44#include "fmtconvert.h"
f6fa7814 45#include "get_bits.h"
2ba45a60 46#include "internal.h"
f6fa7814
DM
47#include "mathops.h"
48#include "synth_filter.h"
2ba45a60
DM
49
50#if ARCH_ARM
51# include "arm/dca.h"
52#endif
53
54//#define TRACE
55
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)
62#define DCA_CHSETS_MAX (4)
63#define DCA_CHSET_CHANS_MAX (8)
64
65enum DCAMode {
66 DCA_MONO = 0,
67 DCA_CHANNEL,
68 DCA_STEREO,
69 DCA_STEREO_SUMDIFF,
70 DCA_STEREO_TOTAL,
71 DCA_3F,
72 DCA_2F1R,
73 DCA_3F1R,
74 DCA_2F2R,
75 DCA_3F2R,
76 DCA_4F2R
77};
78
79/* these are unconfirmed but should be mostly correct */
80enum DCAExSSSpeakerMask {
81 DCA_EXSS_FRONT_CENTER = 0x0001,
82 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
83 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
84 DCA_EXSS_LFE = 0x0008,
85 DCA_EXSS_REAR_CENTER = 0x0010,
86 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
87 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
88 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
89 DCA_EXSS_OVERHEAD = 0x0100,
90 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
91 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
92 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
93 DCA_EXSS_LFE2 = 0x1000,
94 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
95 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
96 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
97};
98
99enum DCAXxchSpeakerMask {
100 DCA_XXCH_FRONT_CENTER = 0x0000001,
101 DCA_XXCH_FRONT_LEFT = 0x0000002,
102 DCA_XXCH_FRONT_RIGHT = 0x0000004,
103 DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
104 DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
105 DCA_XXCH_LFE1 = 0x0000020,
106 DCA_XXCH_REAR_CENTER = 0x0000040,
107 DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
108 DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
109 DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
110 DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
111 DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
112 DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
113 DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
114 DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
115 DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
116 DCA_XXCH_LFE2 = 0x0010000,
117 DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
118 DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
119 DCA_XXCH_OVERHEAD = 0x0080000,
120 DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
121 DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
122 DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
123 DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
124 DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
125 DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
126 DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
127 DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
128};
129
130static const uint32_t map_xxch_to_native[28] = {
131 AV_CH_FRONT_CENTER,
132 AV_CH_FRONT_LEFT,
133 AV_CH_FRONT_RIGHT,
134 AV_CH_SIDE_LEFT,
135 AV_CH_SIDE_RIGHT,
136 AV_CH_LOW_FREQUENCY,
137 AV_CH_BACK_CENTER,
138 AV_CH_BACK_LEFT,
139 AV_CH_BACK_RIGHT,
140 AV_CH_SIDE_LEFT, /* side surround left -- dup sur side L */
141 AV_CH_SIDE_RIGHT, /* side surround right -- dup sur side R */
142 AV_CH_FRONT_LEFT_OF_CENTER,
143 AV_CH_FRONT_RIGHT_OF_CENTER,
144 AV_CH_TOP_FRONT_LEFT,
145 AV_CH_TOP_FRONT_CENTER,
146 AV_CH_TOP_FRONT_RIGHT,
147 AV_CH_LOW_FREQUENCY, /* lfe2 -- duplicate lfe1 position */
148 AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
149 AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
150 AV_CH_TOP_CENTER, /* overhead */
151 AV_CH_TOP_FRONT_LEFT, /* side high left -- dup */
152 AV_CH_TOP_FRONT_RIGHT, /* side high right -- dup */
153 AV_CH_TOP_BACK_CENTER,
154 AV_CH_TOP_BACK_LEFT,
155 AV_CH_TOP_BACK_RIGHT,
156 AV_CH_BACK_CENTER, /* rear low center -- dup */
157 AV_CH_BACK_LEFT, /* rear low left -- dup */
158 AV_CH_BACK_RIGHT /* read low right -- dup */
159};
160
161enum DCAExtensionMask {
162 DCA_EXT_CORE = 0x001, ///< core in core substream
163 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
164 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
165 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
166 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
167 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
168 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
169 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
170 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
171 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
172};
173
174/* -1 are reserved or unknown */
175static const int dca_ext_audio_descr_mask[] = {
176 DCA_EXT_XCH,
177 -1,
178 DCA_EXT_X96,
179 DCA_EXT_XCH | DCA_EXT_X96,
180 -1,
181 -1,
182 DCA_EXT_XXCH,
183 -1,
184};
185
186/* extensions that reside in core substream */
187#define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
188
189/* Tables for mapping dts channel configurations to libavcodec multichannel api.
190 * Some compromises have been made for special configurations. Most configurations
191 * are never used so complete accuracy is not needed.
192 *
193 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
194 * S -> side, when both rear and back are configured move one of them to the side channel
195 * OV -> center back
196 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
197 */
198static const uint64_t dca_core_channel_layout[] = {
199 AV_CH_FRONT_CENTER, ///< 1, A
200 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
201 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
202 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
203 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
204 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
205 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
206 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
207 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
208
209 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
210 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
211
212 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
213 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
214
215 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
216 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
217
218 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
219 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
220 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
221
222 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
223 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
224 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
225
226 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
227 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
228 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
229
230 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
231 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
232 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
233};
234
235static const int8_t dca_lfe_index[] = {
236 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
237};
238
239static const int8_t dca_channel_reorder_lfe[][9] = {
f6fa7814
DM
240 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
241 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
242 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
243 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
244 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
245 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
246 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
247 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
248 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
249 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
250 { 3, 4, 0, 1, 5, 6, -1, -1, -1 },
251 { 2, 0, 1, 4, 5, 6, -1, -1, -1 },
252 { 0, 6, 4, 5, 2, 3, -1, -1, -1 },
253 { 4, 2, 5, 0, 1, 6, 7, -1, -1 },
254 { 5, 6, 0, 1, 7, 3, 8, 4, -1 },
255 { 4, 2, 5, 0, 1, 6, 8, 7, -1 },
2ba45a60
DM
256};
257
258static const int8_t dca_channel_reorder_lfe_xch[][9] = {
f6fa7814
DM
259 { 0, 2, -1, -1, -1, -1, -1, -1, -1 },
260 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
261 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
262 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
263 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
264 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
265 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
266 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
267 { 0, 1, 4, 5, 3, -1, -1, -1, -1 },
268 { 2, 0, 1, 5, 6, 4, -1, -1, -1 },
269 { 3, 4, 0, 1, 6, 7, 5, -1, -1 },
270 { 2, 0, 1, 4, 5, 6, 7, -1, -1 },
271 { 0, 6, 4, 5, 2, 3, 7, -1, -1 },
272 { 4, 2, 5, 0, 1, 7, 8, 6, -1 },
273 { 5, 6, 0, 1, 8, 3, 9, 4, 7 },
274 { 4, 2, 5, 0, 1, 6, 9, 8, 7 },
2ba45a60
DM
275};
276
277static const int8_t dca_channel_reorder_nolfe[][9] = {
f6fa7814
DM
278 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
279 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
280 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
281 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
282 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
283 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
284 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
285 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
286 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
287 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
288 { 2, 3, 0, 1, 4, 5, -1, -1, -1 },
289 { 2, 0, 1, 3, 4, 5, -1, -1, -1 },
290 { 0, 5, 3, 4, 1, 2, -1, -1, -1 },
291 { 3, 2, 4, 0, 1, 5, 6, -1, -1 },
292 { 4, 5, 0, 1, 6, 2, 7, 3, -1 },
293 { 3, 2, 4, 0, 1, 5, 7, 6, -1 },
2ba45a60
DM
294};
295
296static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
f6fa7814
DM
297 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
298 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
299 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
300 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
301 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
302 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
303 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
304 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
305 { 0, 1, 3, 4, 2, -1, -1, -1, -1 },
306 { 2, 0, 1, 4, 5, 3, -1, -1, -1 },
307 { 2, 3, 0, 1, 5, 6, 4, -1, -1 },
308 { 2, 0, 1, 3, 4, 5, 6, -1, -1 },
309 { 0, 5, 3, 4, 1, 2, 6, -1, -1 },
310 { 3, 2, 4, 0, 1, 6, 7, 5, -1 },
311 { 4, 5, 0, 1, 7, 2, 8, 3, 6 },
312 { 3, 2, 4, 0, 1, 5, 8, 7, 6 },
2ba45a60
DM
313};
314
315#define DCA_DOLBY 101 /* FIXME */
316
317#define DCA_CHANNEL_BITS 6
318#define DCA_CHANNEL_MASK 0x3F
319
320#define DCA_LFE 0x80
321
322#define HEADER_SIZE 14
323
324#define DCA_MAX_FRAME_SIZE 16384
325#define DCA_MAX_EXSS_HEADER_SIZE 4096
326
327#define DCA_BUFFER_PADDING_SIZE 1024
328
329#define DCA_NSYNCAUX 0x9A1105A0
330
331/** Bit allocation */
332typedef struct {
333 int offset; ///< code values offset
334 int maxbits[8]; ///< max bits in VLC
335 int wrap; ///< wrap for get_vlc2()
336 VLC vlc[8]; ///< actual codes
337} BitAlloc;
338
339static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
340static BitAlloc dca_tmode; ///< transition mode VLCs
341static BitAlloc dca_scalefactor; ///< scalefactor VLCs
342static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
343
344static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
345 int idx)
346{
347 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
348 ba->offset;
349}
350
351typedef struct {
352 const AVClass *class; ///< class for AVOptions
353 AVCodecContext *avctx;
354 /* Frame header */
355 int frame_type; ///< type of the current frame
356 int samples_deficit; ///< deficit sample count
357 int crc_present; ///< crc is present in the bitstream
358 int sample_blocks; ///< number of PCM sample blocks
359 int frame_size; ///< primary frame byte size
360 int amode; ///< audio channels arrangement
361 int sample_rate; ///< audio sampling rate
362 int bit_rate; ///< transmission bit rate
363 int bit_rate_index; ///< transmission bit rate index
364
365 int dynrange; ///< embedded dynamic range flag
366 int timestamp; ///< embedded time stamp flag
367 int aux_data; ///< auxiliary data flag
368 int hdcd; ///< source material is mastered in HDCD
369 int ext_descr; ///< extension audio descriptor flag
370 int ext_coding; ///< extended coding flag
371 int aspf; ///< audio sync word insertion flag
372 int lfe; ///< low frequency effects flag
373 int predictor_history; ///< predictor history flag
374 int header_crc; ///< header crc check bytes
375 int multirate_inter; ///< multirate interpolator switch
376 int version; ///< encoder software revision
377 int copy_history; ///< copy history
378 int source_pcm_res; ///< source pcm resolution
379 int front_sum; ///< front sum/difference flag
380 int surround_sum; ///< surround sum/difference flag
381 int dialog_norm; ///< dialog normalisation parameter
382
383 /* Primary audio coding header */
384 int subframes; ///< number of subframes
385 int total_channels; ///< number of channels including extensions
386 int prim_channels; ///< number of primary audio channels
387 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
388 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
389 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
390 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
391 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
392 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
393 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
394 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
395
396 /* Primary audio coding side information */
397 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
398 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
399 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
400 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
401 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
402 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
403 int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
404 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
405 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
406 float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
407 int dynrange_coef; ///< dynamic range coefficient
408
409 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
410 * Input: primary audio channels (incl. LFE if present)
411 * Output: downmix audio channels (up to 4, no LFE) */
412 uint8_t core_downmix; ///< embedded downmix coefficients available
413 uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
414 uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
415
416 int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
417
418 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
419 int lfe_scale_factor;
420
421 /* Subband samples history (for ADPCM) */
422 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
423 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
424 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
425 int hist_index[DCA_PRIM_CHANNELS_MAX];
426 DECLARE_ALIGNED(32, float, raXin)[32];
427
428 int output; ///< type of output
429
430 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
431 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
432 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
433 uint8_t *extra_channels_buffer;
434 unsigned int extra_channels_buffer_size;
435
436 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
437 int dca_buffer_size; ///< how much data is in the dca_buffer
438
439 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
440 GetBitContext gb;
441 /* Current position in DCA frame */
442 int current_subframe;
443 int current_subsubframe;
444
445 int core_ext_mask; ///< present extensions in the core substream
446
447 /* XCh extension information */
448 int xch_present; ///< XCh extension present and valid
449 int xch_base_channel; ///< index of first (only) channel containing XCH data
450 int xch_disable; ///< whether the XCh extension should be decoded or not
451
452 /* XXCH extension information */
453 int xxch_chset;
454 int xxch_nbits_spk_mask;
455 uint32_t xxch_core_spkmask;
456 uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
457 int xxch_chset_nch[4];
458 float xxch_dmix_sf[DCA_CHSETS_MAX];
459
460 uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
461 float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
462
463 int8_t xxch_order_tab[32];
464 int8_t lfe_index;
465
466 /* ExSS header parser */
467 int static_fields; ///< static fields present
468 int mix_metadata; ///< mixing metadata present
469 int num_mix_configs; ///< number of mix out configurations
470 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
471
472 int profile;
473
474 int debug_flag; ///< used for suppressing repeated error messages output
f6fa7814 475 AVFloatDSPContext *fdsp;
2ba45a60
DM
476 FFTContext imdct;
477 SynthFilterContext synth;
478 DCADSPContext dcadsp;
479 FmtConvertContext fmt_conv;
480} DCAContext;
481
482static float dca_dmix_code(unsigned code);
483
484static const uint16_t dca_vlc_offs[] = {
485 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
486 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
487 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
488 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
489 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
490 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
491};
492
493static av_cold void dca_init_vlcs(void)
494{
495 static int vlcs_initialized = 0;
496 int i, j, c = 14;
497 static VLC_TYPE dca_table[23622][2];
498
499 if (vlcs_initialized)
500 return;
501
502 dca_bitalloc_index.offset = 1;
f6fa7814 503 dca_bitalloc_index.wrap = 2;
2ba45a60 504 for (i = 0; i < 5; i++) {
f6fa7814 505 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
2ba45a60
DM
506 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
507 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
508 bitalloc_12_bits[i], 1, 1,
509 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
510 }
511 dca_scalefactor.offset = -64;
f6fa7814 512 dca_scalefactor.wrap = 2;
2ba45a60 513 for (i = 0; i < 5; i++) {
f6fa7814 514 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
2ba45a60
DM
515 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
516 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
517 scales_bits[i], 1, 1,
518 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
519 }
520 dca_tmode.offset = 0;
f6fa7814 521 dca_tmode.wrap = 1;
2ba45a60 522 for (i = 0; i < 4; i++) {
f6fa7814 523 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
2ba45a60
DM
524 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
525 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
526 tmode_bits[i], 1, 1,
527 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
528 }
529
530 for (i = 0; i < 10; i++)
531 for (j = 0; j < 7; j++) {
532 if (!bitalloc_codes[i][j])
533 break;
534 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
535 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
536 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
537 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
538
539 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
540 bitalloc_sizes[i],
541 bitalloc_bits[i][j], 1, 1,
542 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
543 c++;
544 }
545 vlcs_initialized = 1;
546}
547
548static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
549{
550 while (len--)
551 *dst++ = get_bits(gb, bits);
552}
553
554static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
555{
556 int i, base, mask;
557
558 /* locate channel set containing the channel */
559 for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
560 i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
561 base += av_popcount(mask);
562
563 return base + av_popcount(mask & (xxch_ch - 1));
564}
565
566static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
567 int xxch)
568{
569 int i, j;
570 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
571 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
572 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
573 int hdr_pos = 0, hdr_size = 0;
574 float scale_factor;
575 int this_chans, acc_mask;
576 int embedded_downmix;
577 int nchans, mask[8];
578 int coeff, ichan;
579
580 /* xxch has arbitrary sized audio coding headers */
581 if (xxch) {
582 hdr_pos = get_bits_count(&s->gb);
583 hdr_size = get_bits(&s->gb, 7) + 1;
584 }
585
586 nchans = get_bits(&s->gb, 3) + 1;
587 s->total_channels = nchans + base_channel;
588 s->prim_channels = s->total_channels;
589
590 /* obtain speaker layout mask & downmix coefficients for XXCH */
591 if (xxch) {
592 acc_mask = s->xxch_core_spkmask;
593
594 this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
595 s->xxch_spk_masks[s->xxch_chset] = this_chans;
596 s->xxch_chset_nch[s->xxch_chset] = nchans;
597
598 for (i = 0; i <= s->xxch_chset; i++)
599 acc_mask |= s->xxch_spk_masks[i];
600
601 /* check for downmixing information */
602 if (get_bits1(&s->gb)) {
603 embedded_downmix = get_bits1(&s->gb);
604 coeff = get_bits(&s->gb, 6);
605
606 if (coeff<1 || coeff>61) {
607 av_log(s->avctx, AV_LOG_ERROR, "6bit coeff %d is out of range\n", coeff);
608 return AVERROR_INVALIDDATA;
609 }
610
611 scale_factor = -1.0f / dca_dmix_code((coeff<<2)-3);
612
613 s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
614
615 for (i = base_channel; i < s->prim_channels; i++) {
616 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
617 }
618
619 for (j = base_channel; j < s->prim_channels; j++) {
620 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
621 s->xxch_dmix_embedded |= (embedded_downmix << j);
622 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
623 if (mask[j] & (1 << i)) {
624 if ((1 << i) == DCA_XXCH_LFE1) {
625 av_log(s->avctx, AV_LOG_WARNING,
626 "DCA-XXCH: dmix to LFE1 not supported.\n");
627 continue;
628 }
629
630 coeff = get_bits(&s->gb, 7);
631 ichan = dca_xxch2index(s, 1 << i);
632 if ((coeff&63)<1 || (coeff&63)>61) {
633 av_log(s->avctx, AV_LOG_ERROR, "7bit coeff %d is out of range\n", coeff);
634 return AVERROR_INVALIDDATA;
635 }
636 s->xxch_dmix_coeff[j][ichan] = dca_dmix_code((coeff<<2)-3);
637 }
638 }
639 }
640 }
641 }
642
643 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
644 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
645
2ba45a60
DM
646 for (i = base_channel; i < s->prim_channels; i++) {
647 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
648 if (s->subband_activity[i] > DCA_SUBBANDS)
649 s->subband_activity[i] = DCA_SUBBANDS;
650 }
651 for (i = base_channel; i < s->prim_channels; i++) {
652 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
653 if (s->vq_start_subband[i] > DCA_SUBBANDS)
654 s->vq_start_subband[i] = DCA_SUBBANDS;
655 }
656 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
657 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
658 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
659 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
660
661 /* Get codebooks quantization indexes */
662 if (!base_channel)
663 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
664 for (j = 1; j < 11; j++)
665 for (i = base_channel; i < s->prim_channels; i++)
666 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
667
668 /* Get scale factor adjustment */
669 for (j = 0; j < 11; j++)
670 for (i = base_channel; i < s->prim_channels; i++)
671 s->scalefactor_adj[i][j] = 1;
672
673 for (j = 1; j < 11; j++)
674 for (i = base_channel; i < s->prim_channels; i++)
675 if (s->quant_index_huffman[i][j] < thr[j])
676 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
677
678 if (!xxch) {
679 if (s->crc_present) {
680 /* Audio header CRC check */
681 get_bits(&s->gb, 16);
682 }
683 } else {
684 /* Skip to the end of the header, also ignore CRC if present */
685 i = get_bits_count(&s->gb);
686 if (hdr_pos + 8 * hdr_size > i)
687 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
688 }
689
690 s->current_subframe = 0;
691 s->current_subsubframe = 0;
692
693#ifdef TRACE
694 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
695 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
696 for (i = base_channel; i < s->prim_channels; i++) {
697 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
698 s->subband_activity[i]);
699 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
700 s->vq_start_subband[i]);
701 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
702 s->joint_intensity[i]);
703 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
704 s->transient_huffman[i]);
705 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
706 s->scalefactor_huffman[i]);
707 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
708 s->bitalloc_huffman[i]);
709 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
710 for (j = 0; j < 11; j++)
711 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
712 av_log(s->avctx, AV_LOG_DEBUG, "\n");
713 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
714 for (j = 0; j < 11; j++)
715 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
716 av_log(s->avctx, AV_LOG_DEBUG, "\n");
717 }
718#endif
719
720 return 0;
721}
722
723static int dca_parse_frame_header(DCAContext *s)
724{
725 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
726
727 /* Sync code */
728 skip_bits_long(&s->gb, 32);
729
730 /* Frame header */
731 s->frame_type = get_bits(&s->gb, 1);
732 s->samples_deficit = get_bits(&s->gb, 5) + 1;
733 s->crc_present = get_bits(&s->gb, 1);
734 s->sample_blocks = get_bits(&s->gb, 7) + 1;
735 s->frame_size = get_bits(&s->gb, 14) + 1;
736 if (s->frame_size < 95)
737 return AVERROR_INVALIDDATA;
738 s->amode = get_bits(&s->gb, 6);
739 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
740 if (!s->sample_rate)
741 return AVERROR_INVALIDDATA;
742 s->bit_rate_index = get_bits(&s->gb, 5);
743 s->bit_rate = dca_bit_rates[s->bit_rate_index];
744 if (!s->bit_rate)
745 return AVERROR_INVALIDDATA;
746
747 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
748 s->dynrange = get_bits(&s->gb, 1);
749 s->timestamp = get_bits(&s->gb, 1);
750 s->aux_data = get_bits(&s->gb, 1);
751 s->hdcd = get_bits(&s->gb, 1);
752 s->ext_descr = get_bits(&s->gb, 3);
753 s->ext_coding = get_bits(&s->gb, 1);
754 s->aspf = get_bits(&s->gb, 1);
755 s->lfe = get_bits(&s->gb, 2);
756 s->predictor_history = get_bits(&s->gb, 1);
757
758 if (s->lfe > 2) {
759 s->lfe = 0;
760 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
761 return AVERROR_INVALIDDATA;
762 }
763
764 /* TODO: check CRC */
765 if (s->crc_present)
766 s->header_crc = get_bits(&s->gb, 16);
767
768 s->multirate_inter = get_bits(&s->gb, 1);
769 s->version = get_bits(&s->gb, 4);
770 s->copy_history = get_bits(&s->gb, 2);
771 s->source_pcm_res = get_bits(&s->gb, 3);
772 s->front_sum = get_bits(&s->gb, 1);
773 s->surround_sum = get_bits(&s->gb, 1);
774 s->dialog_norm = get_bits(&s->gb, 4);
775
776 /* FIXME: channels mixing levels */
777 s->output = s->amode;
778 if (s->lfe)
779 s->output |= DCA_LFE;
780
781#ifdef TRACE
782 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
783 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
784 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
785 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
786 s->sample_blocks, s->sample_blocks * 32);
787 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
788 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
789 s->amode, dca_channels[s->amode]);
790 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
791 s->sample_rate);
792 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
793 s->bit_rate);
794 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
795 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
796 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
797 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
798 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
799 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
800 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
801 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
802 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
803 s->predictor_history);
804 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
805 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
806 s->multirate_inter);
807 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
808 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
809 av_log(s->avctx, AV_LOG_DEBUG,
810 "source pcm resolution: %i (%i bits/sample)\n",
811 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
812 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
813 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
814 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
815 av_log(s->avctx, AV_LOG_DEBUG, "\n");
816#endif
817
818 /* Primary audio coding header */
f6fa7814 819 s->subframes = get_bits(&s->gb, 4) + 1;
2ba45a60
DM
820
821 return dca_parse_audio_coding_header(s, 0, 0);
822}
823
2ba45a60
DM
824static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
825{
826 if (level < 5) {
827 /* huffman encoded */
828 value += get_bitalloc(gb, &dca_scalefactor, level);
f6fa7814 829 value = av_clip(value, 0, (1 << log2range) - 1);
2ba45a60
DM
830 } else if (level < 8) {
831 if (level + 1 > log2range) {
832 skip_bits(gb, level + 1 - log2range);
833 value = get_bits(gb, log2range);
834 } else {
835 value = get_bits(gb, level + 1);
836 }
837 }
838 return value;
839}
840
841static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
842{
843 /* Primary audio coding side information */
844 int j, k;
845
846 if (get_bits_left(&s->gb) < 0)
847 return AVERROR_INVALIDDATA;
848
849 if (!base_channel) {
850 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
851 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
852 }
853
854 for (j = base_channel; j < s->prim_channels; j++) {
855 for (k = 0; k < s->subband_activity[j]; k++)
856 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
857 }
858
859 /* Get prediction codebook */
860 for (j = base_channel; j < s->prim_channels; j++) {
861 for (k = 0; k < s->subband_activity[j]; k++) {
862 if (s->prediction_mode[j][k] > 0) {
863 /* (Prediction coefficient VQ address) */
864 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
865 }
866 }
867 }
868
869 /* Bit allocation index */
870 for (j = base_channel; j < s->prim_channels; j++) {
871 for (k = 0; k < s->vq_start_subband[j]; k++) {
872 if (s->bitalloc_huffman[j] == 6)
873 s->bitalloc[j][k] = get_bits(&s->gb, 5);
874 else if (s->bitalloc_huffman[j] == 5)
875 s->bitalloc[j][k] = get_bits(&s->gb, 4);
876 else if (s->bitalloc_huffman[j] == 7) {
877 av_log(s->avctx, AV_LOG_ERROR,
878 "Invalid bit allocation index\n");
879 return AVERROR_INVALIDDATA;
880 } else {
881 s->bitalloc[j][k] =
882 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
883 }
884
885 if (s->bitalloc[j][k] > 26) {
886 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
887 j, k, s->bitalloc[j][k]);
888 return AVERROR_INVALIDDATA;
889 }
890 }
891 }
892
893 /* Transition mode */
894 for (j = base_channel; j < s->prim_channels; j++) {
895 for (k = 0; k < s->subband_activity[j]; k++) {
896 s->transition_mode[j][k] = 0;
897 if (s->subsubframes[s->current_subframe] > 1 &&
898 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
899 s->transition_mode[j][k] =
900 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
901 }
902 }
903 }
904
905 if (get_bits_left(&s->gb) < 0)
906 return AVERROR_INVALIDDATA;
907
908 for (j = base_channel; j < s->prim_channels; j++) {
909 const uint32_t *scale_table;
910 int scale_sum, log_size;
911
912 memset(s->scale_factor[j], 0,
913 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
914
915 if (s->scalefactor_huffman[j] == 6) {
916 scale_table = scale_factor_quant7;
f6fa7814 917 log_size = 7;
2ba45a60
DM
918 } else {
919 scale_table = scale_factor_quant6;
f6fa7814 920 log_size = 6;
2ba45a60
DM
921 }
922
923 /* When huffman coded, only the difference is encoded */
924 scale_sum = 0;
925
926 for (k = 0; k < s->subband_activity[j]; k++) {
927 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
928 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
929 s->scale_factor[j][k][0] = scale_table[scale_sum];
930 }
931
932 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
933 /* Get second scale factor */
934 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
935 s->scale_factor[j][k][1] = scale_table[scale_sum];
936 }
937 }
938 }
939
940 /* Joint subband scale factor codebook select */
941 for (j = base_channel; j < s->prim_channels; j++) {
942 /* Transmitted only if joint subband coding enabled */
943 if (s->joint_intensity[j] > 0)
944 s->joint_huff[j] = get_bits(&s->gb, 3);
945 }
946
947 if (get_bits_left(&s->gb) < 0)
948 return AVERROR_INVALIDDATA;
949
950 /* Scale factors for joint subband coding */
951 for (j = base_channel; j < s->prim_channels; j++) {
952 int source_channel;
953
954 /* Transmitted only if joint subband coding enabled */
955 if (s->joint_intensity[j] > 0) {
956 int scale = 0;
957 source_channel = s->joint_intensity[j] - 1;
958
959 /* When huffman coded, only the difference is encoded
960 * (is this valid as well for joint scales ???) */
961
962 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
963 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
964 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
965 }
966
967 if (!(s->debug_flag & 0x02)) {
968 av_log(s->avctx, AV_LOG_DEBUG,
969 "Joint stereo coding not supported\n");
970 s->debug_flag |= 0x02;
971 }
972 }
973 }
974
975 /* Dynamic range coefficient */
976 if (!base_channel && s->dynrange)
977 s->dynrange_coef = get_bits(&s->gb, 8);
978
979 /* Side information CRC check word */
980 if (s->crc_present) {
981 get_bits(&s->gb, 16);
982 }
983
984 /*
985 * Primary audio data arrays
986 */
987
988 /* VQ encoded high frequency subbands */
989 for (j = base_channel; j < s->prim_channels; j++)
990 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
991 /* 1 vector -> 32 samples */
992 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
993
994 /* Low frequency effect data */
995 if (!base_channel && s->lfe) {
996 int quant7;
997 /* LFE samples */
f6fa7814 998 int lfe_samples = 2 * s->lfe * (4 + block_index);
2ba45a60
DM
999 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1000 float lfe_scale;
1001
1002 for (j = lfe_samples; j < lfe_end_sample; j++) {
1003 /* Signed 8 bits int */
1004 s->lfe_data[j] = get_sbits(&s->gb, 8);
1005 }
1006
1007 /* Scale factor index */
1008 quant7 = get_bits(&s->gb, 8);
1009 if (quant7 > 127) {
1010 avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
1011 return AVERROR_INVALIDDATA;
1012 }
1013 s->lfe_scale_factor = scale_factor_quant7[quant7];
1014
1015 /* Quantization step size * scale factor */
1016 lfe_scale = 0.035 * s->lfe_scale_factor;
1017
1018 for (j = lfe_samples; j < lfe_end_sample; j++)
1019 s->lfe_data[j] *= lfe_scale;
1020 }
1021
1022#ifdef TRACE
1023 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1024 s->subsubframes[s->current_subframe]);
1025 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1026 s->partial_samples[s->current_subframe]);
1027
1028 for (j = base_channel; j < s->prim_channels; j++) {
1029 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1030 for (k = 0; k < s->subband_activity[j]; k++)
1031 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1032 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1033 }
1034 for (j = base_channel; j < s->prim_channels; j++) {
1035 for (k = 0; k < s->subband_activity[j]; k++)
1036 av_log(s->avctx, AV_LOG_DEBUG,
1037 "prediction coefs: %f, %f, %f, %f\n",
1038 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1039 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1040 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1041 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1042 }
1043 for (j = base_channel; j < s->prim_channels; j++) {
1044 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1045 for (k = 0; k < s->vq_start_subband[j]; k++)
1046 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1047 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1048 }
1049 for (j = base_channel; j < s->prim_channels; j++) {
1050 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1051 for (k = 0; k < s->subband_activity[j]; k++)
1052 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1053 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1054 }
1055 for (j = base_channel; j < s->prim_channels; j++) {
1056 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1057 for (k = 0; k < s->subband_activity[j]; k++) {
1058 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1059 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1060 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1061 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1062 }
1063 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1064 }
1065 for (j = base_channel; j < s->prim_channels; j++) {
1066 if (s->joint_intensity[j] > 0) {
1067 int source_channel = s->joint_intensity[j] - 1;
1068 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1069 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1070 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1071 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1072 }
1073 }
1074 for (j = base_channel; j < s->prim_channels; j++)
1075 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1076 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1077 if (!base_channel && s->lfe) {
f6fa7814 1078 int lfe_samples = 2 * s->lfe * (4 + block_index);
2ba45a60
DM
1079 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1080
1081 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1082 for (j = lfe_samples; j < lfe_end_sample; j++)
1083 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1084 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1085 }
1086#endif
1087
1088 return 0;
1089}
1090
1091static void qmf_32_subbands(DCAContext *s, int chans,
1092 float samples_in[32][8], float *samples_out,
1093 float scale)
1094{
1095 const float *prCoeff;
1096
1097 int sb_act = s->subband_activity[chans];
1098
1099 scale *= sqrt(1 / 8.0);
1100
1101 /* Select filter */
1102 if (!s->multirate_inter) /* Non-perfect reconstruction */
1103 prCoeff = fir_32bands_nonperfect;
1104 else /* Perfect reconstruction */
1105 prCoeff = fir_32bands_perfect;
1106
1107 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
1108 s->subband_fir_hist[chans],
1109 &s->hist_index[chans],
1110 s->subband_fir_noidea[chans], prCoeff,
1111 samples_out, s->raXin, scale);
1112}
1113
1114static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1115 int num_deci_sample, float *samples_in,
1116 float *samples_out)
1117{
1118 /* samples_in: An array holding decimated samples.
1119 * Samples in current subframe starts from samples_in[0],
1120 * while samples_in[-1], samples_in[-2], ..., stores samples
1121 * from last subframe as history.
1122 *
1123 * samples_out: An array holding interpolated samples
1124 */
1125
1126 int idx;
1127 const float *prCoeff;
1128 int deciindex;
1129
1130 /* Select decimation filter */
1131 if (decimation_select == 1) {
f6fa7814 1132 idx = 1;
2ba45a60
DM
1133 prCoeff = lfe_fir_128;
1134 } else {
f6fa7814 1135 idx = 0;
2ba45a60
DM
1136 prCoeff = lfe_fir_64;
1137 }
1138 /* Interpolation */
1139 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1140 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
1141 samples_in++;
1142 samples_out += 2 * 32 * (1 + idx);
1143 }
1144}
1145
1146/* downmixing routines */
1147#define MIX_REAR1(samples, s1, rs, coef) \
1148 samples[0][i] += samples[s1][i] * coef[rs][0]; \
1149 samples[1][i] += samples[s1][i] * coef[rs][1];
1150
1151#define MIX_REAR2(samples, s1, s2, rs, coef) \
1152 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1153 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1154
1155#define MIX_FRONT3(samples, coef) \
1156 t = samples[c][i]; \
1157 u = samples[l][i]; \
1158 v = samples[r][i]; \
1159 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1160 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1161
1162#define DOWNMIX_TO_STEREO(op1, op2) \
1163 for (i = 0; i < 256; i++) { \
1164 op1 \
1165 op2 \
1166 }
1167
1168static void dca_downmix(float **samples, int srcfmt, int lfe_present,
1169 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
1170 const int8_t *channel_mapping)
1171{
1172 int c, l, r, sl, sr, s;
1173 int i;
1174 float t, u, v;
1175
1176 switch (srcfmt) {
1177 case DCA_MONO:
1178 case DCA_4F2R:
1179 av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1180 break;
1181 case DCA_CHANNEL:
1182 case DCA_STEREO:
1183 case DCA_STEREO_TOTAL:
1184 case DCA_STEREO_SUMDIFF:
1185 break;
1186 case DCA_3F:
1187 c = channel_mapping[0];
1188 l = channel_mapping[1];
1189 r = channel_mapping[2];
1190 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1191 break;
1192 case DCA_2F1R:
1193 s = channel_mapping[2];
1194 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1195 break;
1196 case DCA_3F1R:
1197 c = channel_mapping[0];
1198 l = channel_mapping[1];
1199 r = channel_mapping[2];
1200 s = channel_mapping[3];
1201 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1202 MIX_REAR1(samples, s, 3, coef));
1203 break;
1204 case DCA_2F2R:
1205 sl = channel_mapping[2];
1206 sr = channel_mapping[3];
1207 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1208 break;
1209 case DCA_3F2R:
1210 c = channel_mapping[0];
1211 l = channel_mapping[1];
1212 r = channel_mapping[2];
1213 sl = channel_mapping[3];
1214 sr = channel_mapping[4];
1215 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1216 MIX_REAR2(samples, sl, sr, 3, coef));
1217 break;
1218 }
1219 if (lfe_present) {
1220 int lf_buf = dca_lfe_index[srcfmt];
f6fa7814 1221 int lf_idx = dca_channels[srcfmt];
2ba45a60
DM
1222 for (i = 0; i < 256; i++) {
1223 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1224 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1225 }
1226 }
1227}
1228
2ba45a60
DM
1229#ifndef decode_blockcodes
1230/* Very compact version of the block code decoder that does not use table
1231 * look-up but is slightly slower */
1232static int decode_blockcode(int code, int levels, int32_t *values)
1233{
1234 int i;
1235 int offset = (levels - 1) >> 1;
1236
1237 for (i = 0; i < 4; i++) {
1238 int div = FASTDIV(code, levels);
1239 values[i] = code - offset - div * levels;
f6fa7814 1240 code = div;
2ba45a60
DM
1241 }
1242
1243 return code;
1244}
1245
1246static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1247{
1248 return decode_blockcode(code1, levels, values) |
1249 decode_blockcode(code2, levels, values + 4);
1250}
1251#endif
1252
1253static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1254static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1255
1256static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1257{
1258 int k, l;
1259 int subsubframe = s->current_subsubframe;
1260
1261 const float *quant_step_table;
1262
1263 /* FIXME */
1264 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1265 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
1266
1267 /*
1268 * Audio data
1269 */
1270
1271 /* Select quantization step size table */
1272 if (s->bit_rate_index == 0x1f)
1273 quant_step_table = lossless_quant_d;
1274 else
1275 quant_step_table = lossy_quant_d;
1276
1277 for (k = base_channel; k < s->prim_channels; k++) {
1278 float rscale[DCA_SUBBANDS];
1279
1280 if (get_bits_left(&s->gb) < 0)
1281 return AVERROR_INVALIDDATA;
1282
1283 for (l = 0; l < s->vq_start_subband[k]; l++) {
1284 int m;
1285
1286 /* Select the mid-tread linear quantizer */
1287 int abits = s->bitalloc[k][l];
1288
1289 float quant_step_size = quant_step_table[abits];
1290
1291 /*
1292 * Determine quantization index code book and its type
1293 */
1294
1295 /* Select quantization index code book */
1296 int sel = s->quant_index_huffman[k][abits];
1297
1298 /*
1299 * Extract bits from the bit stream
1300 */
1301 if (!abits) {
1302 rscale[l] = 0;
1303 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1304 } else {
1305 /* Deal with transients */
1306 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1307 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
f6fa7814 1308 s->scalefactor_adj[k][sel];
2ba45a60
DM
1309
1310 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1311 if (abits <= 7) {
1312 /* Block code */
1313 int block_code1, block_code2, size, levels, err;
1314
1315 size = abits_sizes[abits - 1];
1316 levels = abits_levels[abits - 1];
1317
1318 block_code1 = get_bits(&s->gb, size);
1319 block_code2 = get_bits(&s->gb, size);
f6fa7814
DM
1320 err = decode_blockcodes(block_code1, block_code2,
1321 levels, block + 8 * l);
2ba45a60
DM
1322 if (err) {
1323 av_log(s->avctx, AV_LOG_ERROR,
1324 "ERROR: block code look-up failed\n");
1325 return AVERROR_INVALIDDATA;
1326 }
1327 } else {
1328 /* no coding */
1329 for (m = 0; m < 8; m++)
1330 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1331 }
1332 } else {
1333 /* Huffman coded */
1334 for (m = 0; m < 8; m++)
1335 block[8 * l + m] = get_bitalloc(&s->gb,
f6fa7814 1336 &dca_smpl_bitalloc[abits], sel);
2ba45a60 1337 }
2ba45a60
DM
1338 }
1339 }
1340
1341 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1342 block, rscale, 8 * s->vq_start_subband[k]);
1343
1344 for (l = 0; l < s->vq_start_subband[k]; l++) {
1345 int m;
1346 /*
1347 * Inverse ADPCM if in prediction mode
1348 */
1349 if (s->prediction_mode[k][l]) {
1350 int n;
1351 if (s->predictor_history)
1352 subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
1353 s->subband_samples_hist[k][l][3] +
1354 adpcm_vb[s->prediction_vq[k][l]][1] *
1355 s->subband_samples_hist[k][l][2] +
1356 adpcm_vb[s->prediction_vq[k][l]][2] *
1357 s->subband_samples_hist[k][l][1] +
1358 adpcm_vb[s->prediction_vq[k][l]][3] *
1359 s->subband_samples_hist[k][l][0]) *
1360 (1.0f / 8192);
1361 for (m = 1; m < 8; m++) {
1362 float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
1363 subband_samples[k][l][m - 1];
1364 for (n = 2; n <= 4; n++)
1365 if (m >= n)
1366 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1367 subband_samples[k][l][m - n];
1368 else if (s->predictor_history)
1369 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1370 s->subband_samples_hist[k][l][m - n + 4];
1371 subband_samples[k][l][m] += sum * (1.0f / 8192);
1372 }
1373 }
1374 }
1375
1376 /*
1377 * Decode VQ encoded high frequencies
1378 */
1379 if (s->subband_activity[k] > s->vq_start_subband[k]) {
1380 if (!(s->debug_flag & 0x01)) {
1381 av_log(s->avctx, AV_LOG_DEBUG,
1382 "Stream with high frequencies VQ coding\n");
1383 s->debug_flag |= 0x01;
1384 }
1385 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
1386 high_freq_vq, subsubframe * 8,
1387 s->scale_factor[k], s->vq_start_subband[k],
1388 s->subband_activity[k]);
1389 }
1390 }
1391
1392 /* Check for DSYNC after subsubframe */
1393 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1394 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1395#ifdef TRACE
1396 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1397#endif
1398 } else {
1399 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1400 return AVERROR_INVALIDDATA;
1401 }
1402 }
1403
1404 /* Backup predictor history for adpcm */
1405 for (k = base_channel; k < s->prim_channels; k++)
1406 for (l = 0; l < s->vq_start_subband[k]; l++)
1407 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1408
1409 return 0;
1410}
1411
1412static int dca_filter_channels(DCAContext *s, int block_index)
1413{
1414 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1415 int k;
1416
1417 /* 32 subbands QMF */
1418 for (k = 0; k < s->prim_channels; k++) {
2ba45a60
DM
1419 if (s->channel_order_tab[k] >= 0)
1420 qmf_32_subbands(s, k, subband_samples[k],
1421 s->samples_chanptr[s->channel_order_tab[k]],
f6fa7814 1422 M_SQRT1_2 / 32768.0);
2ba45a60
DM
1423 }
1424
1425 /* Generate LFE samples for this subsubframe FIXME!!! */
1426 if (s->lfe) {
1427 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1428 s->lfe_data + 2 * s->lfe * (block_index + 4),
1429 s->samples_chanptr[s->lfe_index]);
1430 /* Outputs 20bits pcm samples */
1431 }
1432
1433 /* Downmixing to Stereo */
1434 if (s->prim_channels + !!s->lfe > 2 &&
1435 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1436 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1437 s->channel_order_tab);
1438 }
1439
1440 return 0;
1441}
1442
2ba45a60
DM
1443static int dca_subframe_footer(DCAContext *s, int base_channel)
1444{
1445 int in, out, aux_data_count, aux_data_end, reserved;
1446 uint32_t nsyncaux;
1447
1448 /*
1449 * Unpack optional information
1450 */
1451
1452 /* presumably optional information only appears in the core? */
1453 if (!base_channel) {
1454 if (s->timestamp)
1455 skip_bits_long(&s->gb, 32);
1456
1457 if (s->aux_data) {
1458 aux_data_count = get_bits(&s->gb, 6);
1459
1460 // align (32-bit)
1461 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1462
1463 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1464
1465 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1466 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1467 nsyncaux);
1468 return AVERROR_INVALIDDATA;
1469 }
1470
1471 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1472 avpriv_request_sample(s->avctx,
1473 "Auxiliary Decode Time Stamp Flag");
1474 // align (4-bit)
1475 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1476 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1477 skip_bits_long(&s->gb, 44);
1478 }
1479
1480 if ((s->core_downmix = get_bits1(&s->gb))) {
1481 int am = get_bits(&s->gb, 3);
1482 switch (am) {
1483 case 0:
1484 s->core_downmix_amode = DCA_MONO;
1485 break;
1486 case 1:
1487 s->core_downmix_amode = DCA_STEREO;
1488 break;
1489 case 2:
1490 s->core_downmix_amode = DCA_STEREO_TOTAL;
1491 break;
1492 case 3:
1493 s->core_downmix_amode = DCA_3F;
1494 break;
1495 case 4:
1496 s->core_downmix_amode = DCA_2F1R;
1497 break;
1498 case 5:
1499 s->core_downmix_amode = DCA_2F2R;
1500 break;
1501 case 6:
1502 s->core_downmix_amode = DCA_3F1R;
1503 break;
1504 default:
1505 av_log(s->avctx, AV_LOG_ERROR,
1506 "Invalid mode %d for embedded downmix coefficients\n",
1507 am);
1508 return AVERROR_INVALIDDATA;
1509 }
1510 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1511 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1512 uint16_t tmp = get_bits(&s->gb, 9);
1513 if ((tmp & 0xFF) > 241) {
1514 av_log(s->avctx, AV_LOG_ERROR,
1515 "Invalid downmix coefficient code %"PRIu16"\n",
1516 tmp);
1517 return AVERROR_INVALIDDATA;
1518 }
1519 s->core_downmix_codes[in][out] = tmp;
1520 }
1521 }
1522 }
1523
1524 align_get_bits(&s->gb); // byte align
1525 skip_bits(&s->gb, 16); // nAUXCRC16
1526
1527 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1528 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
f6fa7814
DM
1529 av_log(s->avctx, AV_LOG_ERROR,
1530 "Overread auxiliary data by %d bits\n", -reserved);
2ba45a60
DM
1531 return AVERROR_INVALIDDATA;
1532 } else if (reserved) {
1533 avpriv_request_sample(s->avctx,
1534 "Core auxiliary data reserved content");
1535 skip_bits_long(&s->gb, reserved);
1536 }
1537 }
1538
1539 if (s->crc_present && s->dynrange)
1540 get_bits(&s->gb, 16);
1541 }
1542
1543 return 0;
1544}
1545
1546/**
1547 * Decode a dca frame block
1548 *
1549 * @param s pointer to the DCAContext
1550 */
1551
1552static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1553{
1554 int ret;
1555
1556 /* Sanity check */
1557 if (s->current_subframe >= s->subframes) {
1558 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1559 s->current_subframe, s->subframes);
1560 return AVERROR_INVALIDDATA;
1561 }
1562
1563 if (!s->current_subsubframe) {
1564#ifdef TRACE
1565 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1566#endif
1567 /* Read subframe header */
1568 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1569 return ret;
1570 }
1571
1572 /* Read subsubframe */
1573#ifdef TRACE
1574 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1575#endif
1576 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1577 return ret;
1578
1579 /* Update state */
1580 s->current_subsubframe++;
1581 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1582 s->current_subsubframe = 0;
1583 s->current_subframe++;
1584 }
1585 if (s->current_subframe >= s->subframes) {
1586#ifdef TRACE
1587 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1588#endif
1589 /* Read subframe footer */
1590 if ((ret = dca_subframe_footer(s, base_channel)))
1591 return ret;
1592 }
1593
1594 return 0;
1595}
1596
1597/**
1598 * Return the number of channels in an ExSS speaker mask (HD)
1599 */
1600static int dca_exss_mask2count(int mask)
1601{
1602 /* count bits that mean speaker pairs twice */
1603 return av_popcount(mask) +
1604 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1605 DCA_EXSS_FRONT_LEFT_RIGHT |
1606 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1607 DCA_EXSS_WIDE_LEFT_RIGHT |
1608 DCA_EXSS_SIDE_LEFT_RIGHT |
1609 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1610 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1611 DCA_EXSS_REAR_LEFT_RIGHT |
1612 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1613}
1614
1615/**
1616 * Skip mixing coefficients of a single mix out configuration (HD)
1617 */
1618static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1619{
1620 int i;
1621
1622 for (i = 0; i < channels; i++) {
1623 int mix_map_mask = get_bits(gb, out_ch);
1624 int num_coeffs = av_popcount(mix_map_mask);
1625 skip_bits_long(gb, num_coeffs * 6);
1626 }
1627}
1628
1629/**
1630 * Parse extension substream asset header (HD)
1631 */
1632static int dca_exss_parse_asset_header(DCAContext *s)
1633{
1634 int header_pos = get_bits_count(&s->gb);
1635 int header_size;
1636 int channels = 0;
1637 int embedded_stereo = 0;
1638 int embedded_6ch = 0;
1639 int drc_code_present;
f6fa7814 1640 int extensions_mask = 0;
2ba45a60
DM
1641 int i, j;
1642
1643 if (get_bits_left(&s->gb) < 16)
1644 return -1;
1645
1646 /* We will parse just enough to get to the extensions bitmask with which
1647 * we can set the profile value. */
1648
1649 header_size = get_bits(&s->gb, 9) + 1;
1650 skip_bits(&s->gb, 3); // asset index
1651
1652 if (s->static_fields) {
1653 if (get_bits1(&s->gb))
1654 skip_bits(&s->gb, 4); // asset type descriptor
1655 if (get_bits1(&s->gb))
1656 skip_bits_long(&s->gb, 24); // language descriptor
1657
1658 if (get_bits1(&s->gb)) {
1659 /* How can one fit 1024 bytes of text here if the maximum value
1660 * for the asset header size field above was 512 bytes? */
1661 int text_length = get_bits(&s->gb, 10) + 1;
1662 if (get_bits_left(&s->gb) < text_length * 8)
1663 return -1;
1664 skip_bits_long(&s->gb, text_length * 8); // info text
1665 }
1666
1667 skip_bits(&s->gb, 5); // bit resolution - 1
1668 skip_bits(&s->gb, 4); // max sample rate code
1669 channels = get_bits(&s->gb, 8) + 1;
1670
1671 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1672 int spkr_remap_sets;
1673 int spkr_mask_size = 16;
1674 int num_spkrs[7];
1675
1676 if (channels > 2)
1677 embedded_stereo = get_bits1(&s->gb);
1678 if (channels > 6)
1679 embedded_6ch = get_bits1(&s->gb);
1680
1681 if (get_bits1(&s->gb)) {
1682 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1683 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1684 }
1685
1686 spkr_remap_sets = get_bits(&s->gb, 3);
1687
1688 for (i = 0; i < spkr_remap_sets; i++) {
1689 /* std layout mask for each remap set */
1690 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1691 }
1692
1693 for (i = 0; i < spkr_remap_sets; i++) {
1694 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1695 if (get_bits_left(&s->gb) < 0)
1696 return -1;
1697
1698 for (j = 0; j < num_spkrs[i]; j++) {
1699 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1700 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1701 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1702 }
1703 }
2ba45a60
DM
1704 } else {
1705 skip_bits(&s->gb, 3); // representation type
1706 }
1707 }
1708
1709 drc_code_present = get_bits1(&s->gb);
1710 if (drc_code_present)
1711 get_bits(&s->gb, 8); // drc code
1712
1713 if (get_bits1(&s->gb))
1714 skip_bits(&s->gb, 5); // dialog normalization code
1715
1716 if (drc_code_present && embedded_stereo)
1717 get_bits(&s->gb, 8); // drc stereo code
1718
1719 if (s->mix_metadata && get_bits1(&s->gb)) {
1720 skip_bits(&s->gb, 1); // external mix
1721 skip_bits(&s->gb, 6); // post mix gain code
1722
1723 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1724 skip_bits(&s->gb, 3); // drc limit
1725 else
1726 skip_bits(&s->gb, 8); // custom drc code
1727
1728 if (get_bits1(&s->gb)) // channel specific scaling
1729 for (i = 0; i < s->num_mix_configs; i++)
1730 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1731 else
1732 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1733
1734 for (i = 0; i < s->num_mix_configs; i++) {
1735 if (get_bits_left(&s->gb) < 0)
1736 return -1;
1737 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1738 if (embedded_6ch)
1739 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1740 if (embedded_stereo)
1741 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1742 }
1743 }
1744
1745 switch (get_bits(&s->gb, 2)) {
f6fa7814
DM
1746 case 0:
1747 extensions_mask = get_bits(&s->gb, 12);
1748 break;
1749 case 1:
1750 extensions_mask = DCA_EXT_EXSS_XLL;
1751 break;
1752 case 2:
1753 extensions_mask = DCA_EXT_EXSS_LBR;
1754 break;
1755 case 3:
1756 extensions_mask = 0; /* aux coding */
1757 break;
2ba45a60
DM
1758 }
1759
1760 /* not parsed further, we were only interested in the extensions mask */
1761
1762 if (get_bits_left(&s->gb) < 0)
1763 return -1;
1764
1765 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1766 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1767 return -1;
1768 }
1769 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1770
1771 if (extensions_mask & DCA_EXT_EXSS_XLL)
1772 s->profile = FF_PROFILE_DTS_HD_MA;
1773 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1774 DCA_EXT_EXSS_XXCH))
1775 s->profile = FF_PROFILE_DTS_HD_HRA;
1776
1777 if (!(extensions_mask & DCA_EXT_CORE))
1778 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1779 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1780 av_log(s->avctx, AV_LOG_WARNING,
1781 "DTS extensions detection mismatch (%d, %d)\n",
1782 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1783
1784 return 0;
1785}
1786
1787static int dca_xbr_parse_frame(DCAContext *s)
1788{
1789 int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1790 int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1791 int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1792 int anctemp[DCA_CHSET_CHANS_MAX];
1793 int chset_fsize[DCA_CHSETS_MAX];
1794 int n_xbr_ch[DCA_CHSETS_MAX];
1795 int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1796 int i, j, k, l, chset, chan_base;
1797
1798 av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1799
1800 /* get bit position of sync header */
1801 hdr_pos = get_bits_count(&s->gb) - 32;
1802
1803 hdr_size = get_bits(&s->gb, 6) + 1;
1804 num_chsets = get_bits(&s->gb, 2) + 1;
1805
1806 for(i = 0; i < num_chsets; i++)
1807 chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1808
1809 xbr_tmode = get_bits1(&s->gb);
1810
1811 for(i = 0; i < num_chsets; i++) {
1812 n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1813 k = get_bits(&s->gb, 2) + 5;
1814 for(j = 0; j < n_xbr_ch[i]; j++)
1815 active_bands[i][j] = get_bits(&s->gb, k) + 1;
1816 }
1817
1818 /* skip to the end of the header */
1819 i = get_bits_count(&s->gb);
1820 if(hdr_pos + hdr_size * 8 > i)
1821 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1822
1823 /* loop over the channel data sets */
1824 /* only decode as many channels as we've decoded base data for */
1825 for(chset = 0, chan_base = 0;
1826 chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1827 chan_base += n_xbr_ch[chset++]) {
1828 int start_posn = get_bits_count(&s->gb);
1829 int subsubframe = 0;
1830 int subframe = 0;
1831
1832 /* loop over subframes */
1833 for (k = 0; k < (s->sample_blocks / 8); k++) {
1834 /* parse header if we're on first subsubframe of a block */
1835 if(subsubframe == 0) {
1836 /* Parse subframe header */
1837 for(i = 0; i < n_xbr_ch[chset]; i++) {
1838 anctemp[i] = get_bits(&s->gb, 2) + 2;
1839 }
1840
1841 for(i = 0; i < n_xbr_ch[chset]; i++) {
1842 get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1843 }
1844
1845 for(i = 0; i < n_xbr_ch[chset]; i++) {
1846 anctemp[i] = get_bits(&s->gb, 3);
1847 if(anctemp[i] < 1) {
1848 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1849 return AVERROR_INVALIDDATA;
1850 }
1851 }
1852
1853 /* generate scale factors */
1854 for(i = 0; i < n_xbr_ch[chset]; i++) {
1855 const uint32_t *scale_table;
1856 int nbits;
1857
1858 if (s->scalefactor_huffman[chan_base+i] == 6) {
1859 scale_table = scale_factor_quant7;
1860 } else {
1861 scale_table = scale_factor_quant6;
1862 }
1863
1864 nbits = anctemp[i];
1865
1866 for(j = 0; j < active_bands[chset][i]; j++) {
1867 if(abits_high[i][j] > 0) {
1868 scale_table_high[i][j][0] =
1869 scale_table[get_bits(&s->gb, nbits)];
1870
1871 if(xbr_tmode && s->transition_mode[i][j]) {
1872 scale_table_high[i][j][1] =
1873 scale_table[get_bits(&s->gb, nbits)];
1874 }
1875 }
1876 }
1877 }
1878 }
1879
1880 /* decode audio array for this block */
1881 for(i = 0; i < n_xbr_ch[chset]; i++) {
1882 for(j = 0; j < active_bands[chset][i]; j++) {
1883 const int xbr_abits = abits_high[i][j];
1884 const float quant_step_size = lossless_quant_d[xbr_abits];
1885 const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1886 const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1887 float *subband_samples = s->subband_samples[k][chan_base+i][j];
1888 int block[8];
1889
1890 if(xbr_abits <= 0)
1891 continue;
1892
1893 if(xbr_abits > 7) {
1894 get_array(&s->gb, block, 8, xbr_abits - 3);
1895 } else {
1896 int block_code1, block_code2, size, levels, err;
1897
1898 size = abits_sizes[xbr_abits - 1];
1899 levels = abits_levels[xbr_abits - 1];
1900
1901 block_code1 = get_bits(&s->gb, size);
1902 block_code2 = get_bits(&s->gb, size);
1903 err = decode_blockcodes(block_code1, block_code2,
1904 levels, block);
1905 if (err) {
1906 av_log(s->avctx, AV_LOG_ERROR,
1907 "ERROR: DTS-XBR: block code look-up failed\n");
1908 return AVERROR_INVALIDDATA;
1909 }
1910 }
1911
1912 /* scale & sum into subband */
1913 for(l = 0; l < 8; l++)
1914 subband_samples[l] += (float)block[l] * rscale;
1915 }
1916 }
1917
1918 /* check DSYNC marker */
1919 if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1920 if(get_bits(&s->gb, 16) != 0xffff) {
1921 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1922 return AVERROR_INVALIDDATA;
1923 }
1924 }
1925
1926 /* advance sub-sub-frame index */
1927 if(++subsubframe >= s->subsubframes[subframe]) {
1928 subsubframe = 0;
1929 subframe++;
1930 }
1931 }
1932
1933 /* skip to next channel set */
1934 i = get_bits_count(&s->gb);
1935 if(start_posn + chset_fsize[chset] * 8 != i) {
1936 j = start_posn + chset_fsize[chset] * 8 - i;
1937 if(j < 0 || j >= 8)
1938 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1939 " skipping further than expected (%d bits)\n", j);
1940 skip_bits_long(&s->gb, j);
1941 }
1942 }
1943
1944 return 0;
1945}
1946
1947/* parse initial header for XXCH and dump details */
1948static int dca_xxch_decode_frame(DCAContext *s)
1949{
1950 int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1951 int i, chset, base_channel, chstart, fsize[8];
1952
1953 /* assume header word has already been parsed */
1954 hdr_pos = get_bits_count(&s->gb) - 32;
1955 hdr_size = get_bits(&s->gb, 6) + 1;
1956 /*chhdr_crc =*/ skip_bits1(&s->gb);
1957 spkmsk_bits = get_bits(&s->gb, 5) + 1;
1958 num_chsets = get_bits(&s->gb, 2) + 1;
1959
1960 for (i = 0; i < num_chsets; i++)
1961 fsize[i] = get_bits(&s->gb, 14) + 1;
1962
1963 core_spk = get_bits(&s->gb, spkmsk_bits);
1964 s->xxch_core_spkmask = core_spk;
1965 s->xxch_nbits_spk_mask = spkmsk_bits;
1966 s->xxch_dmix_embedded = 0;
1967
1968 /* skip to the end of the header */
1969 i = get_bits_count(&s->gb);
1970 if (hdr_pos + hdr_size * 8 > i)
1971 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1972
1973 for (chset = 0; chset < num_chsets; chset++) {
1974 chstart = get_bits_count(&s->gb);
1975 base_channel = s->prim_channels;
1976 s->xxch_chset = chset;
1977
1978 /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1979 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1980 dca_parse_audio_coding_header(s, base_channel, 1);
1981
1982 /* decode channel data */
1983 for (i = 0; i < (s->sample_blocks / 8); i++) {
1984 if (dca_decode_block(s, base_channel, i)) {
1985 av_log(s->avctx, AV_LOG_ERROR,
1986 "Error decoding DTS-XXCH extension\n");
1987 continue;
1988 }
1989 }
1990
1991 /* skip to end of this section */
1992 i = get_bits_count(&s->gb);
1993 if (chstart + fsize[chset] * 8 > i)
1994 skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1995 }
1996 s->xxch_chset = num_chsets;
1997
1998 return 0;
1999}
2000
2001/**
2002 * Parse extension substream header (HD)
2003 */
2004static void dca_exss_parse_header(DCAContext *s)
2005{
2006 int asset_size[8];
2007 int ss_index;
2008 int blownup;
2009 int num_audiop = 1;
2010 int num_assets = 1;
2011 int active_ss_mask[8];
2012 int i, j;
2013 int start_posn;
2014 int hdrsize;
2015 uint32_t mkr;
2016
2017 if (get_bits_left(&s->gb) < 52)
2018 return;
2019
2020 start_posn = get_bits_count(&s->gb) - 32;
2021
2022 skip_bits(&s->gb, 8); // user data
2023 ss_index = get_bits(&s->gb, 2);
2024
2025 blownup = get_bits1(&s->gb);
2026 hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
2027 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
2028
2029 s->static_fields = get_bits1(&s->gb);
2030 if (s->static_fields) {
2031 skip_bits(&s->gb, 2); // reference clock code
2032 skip_bits(&s->gb, 3); // frame duration code
2033
2034 if (get_bits1(&s->gb))
2035 skip_bits_long(&s->gb, 36); // timestamp
2036
2037 /* a single stream can contain multiple audio assets that can be
2038 * combined to form multiple audio presentations */
2039
2040 num_audiop = get_bits(&s->gb, 3) + 1;
2041 if (num_audiop > 1) {
2042 avpriv_request_sample(s->avctx,
2043 "Multiple DTS-HD audio presentations");
2044 /* ignore such streams for now */
2045 return;
2046 }
2047
2048 num_assets = get_bits(&s->gb, 3) + 1;
2049 if (num_assets > 1) {
2050 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
2051 /* ignore such streams for now */
2052 return;
2053 }
2054
2055 for (i = 0; i < num_audiop; i++)
2056 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
2057
2058 for (i = 0; i < num_audiop; i++)
2059 for (j = 0; j <= ss_index; j++)
2060 if (active_ss_mask[i] & (1 << j))
2061 skip_bits(&s->gb, 8); // active asset mask
2062
2063 s->mix_metadata = get_bits1(&s->gb);
2064 if (s->mix_metadata) {
2065 int mix_out_mask_size;
2066
2067 skip_bits(&s->gb, 2); // adjustment level
2068 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2069 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2070
2071 for (i = 0; i < s->num_mix_configs; i++) {
2072 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2073 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2074 }
2075 }
2076 }
2077
2078 av_assert0(num_assets > 0); // silence a warning
2079
2080 for (i = 0; i < num_assets; i++)
2081 asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2082
2083 for (i = 0; i < num_assets; i++) {
2084 if (dca_exss_parse_asset_header(s))
2085 return;
2086 }
2087
2088 /* not parsed further, we were only interested in the extensions mask
2089 * from the asset header */
2090
2091 j = get_bits_count(&s->gb);
2092 if (start_posn + hdrsize * 8 > j)
2093 skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2094
2095 for (i = 0; i < num_assets; i++) {
2096 start_posn = get_bits_count(&s->gb);
2097 mkr = get_bits_long(&s->gb, 32);
2098
2099 /* parse extensions that we know about */
2100 if (mkr == 0x655e315e) {
2101 dca_xbr_parse_frame(s);
2102 } else if (mkr == 0x47004a03) {
2103 dca_xxch_decode_frame(s);
2104 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2105 } else {
2106 av_log(s->avctx, AV_LOG_DEBUG,
2107 "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2108 }
2109
2110 /* skip to end of block */
2111 j = get_bits_count(&s->gb);
2112 if (start_posn + asset_size[i] * 8 > j)
2113 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2114 }
2115}
2116
2117static float dca_dmix_code(unsigned code)
2118{
2119 int sign = (code >> 8) - 1;
2120 code &= 0xff;
2121 return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1 << 15));
2122}
2123
2124/**
2125 * Main frame decoding function
2126 * FIXME add arguments
2127 */
2128static int dca_decode_frame(AVCodecContext *avctx, void *data,
2129 int *got_frame_ptr, AVPacket *avpkt)
2130{
2131 AVFrame *frame = data;
2132 const uint8_t *buf = avpkt->data;
f6fa7814 2133 int buf_size = avpkt->size;
2ba45a60
DM
2134 int channel_mask;
2135 int channel_layout;
2136 int lfe_samples;
2137 int num_core_channels = 0;
2138 int i, ret;
2139 float **samples_flt;
2140 float *src_chan;
2141 float *dst_chan;
2142 DCAContext *s = avctx->priv_data;
2143 int core_ss_end;
2144 int channels, full_channels;
2145 float scale;
2146 int achan;
2147 int chset;
2148 int mask;
2149 int lavc;
2150 int posn;
2151 int j, k;
2152 int endch;
2153
2154 s->xch_present = 0;
2155
2156 s->dca_buffer_size = avpriv_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2157 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2158 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2159 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2160 return AVERROR_INVALIDDATA;
2161 }
2162
2163 if ((ret = dca_parse_frame_header(s)) < 0) {
f6fa7814 2164 // seems like the frame is corrupt, try with the next one
2ba45a60
DM
2165 return ret;
2166 }
f6fa7814 2167 // set AVCodec values with parsed data
2ba45a60
DM
2168 avctx->sample_rate = s->sample_rate;
2169 avctx->bit_rate = s->bit_rate;
2170
2171 s->profile = FF_PROFILE_DTS;
2172
2173 for (i = 0; i < (s->sample_blocks / 8); i++) {
2174 if ((ret = dca_decode_block(s, 0, i))) {
2175 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2176 return ret;
2177 }
2178 }
2179
2180 /* record number of core channels incase less than max channels are requested */
2181 num_core_channels = s->prim_channels;
2182
2183 if (s->prim_channels + !!s->lfe > 2 &&
2184 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2185 /* Stereo downmix coefficients
2186 *
2187 * The decoder can only downmix to 2-channel, so we need to ensure
2188 * embedded downmix coefficients are actually targeting 2-channel.
2189 */
2190 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
2191 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
2192 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2193 /* Range checked earlier */
2194 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
2195 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
2196 }
2197 s->output = s->core_downmix_amode;
2198 } else {
2199 int am = s->amode & DCA_CHANNEL_MASK;
2200 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
2201 av_log(s->avctx, AV_LOG_ERROR,
2202 "Invalid channel mode %d\n", am);
2203 return AVERROR_INVALIDDATA;
2204 }
2205 if (num_core_channels + !!s->lfe >
2206 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
2207 avpriv_request_sample(s->avctx, "Downmixing %d channels",
2208 s->prim_channels + !!s->lfe);
2209 return AVERROR_PATCHWELCOME;
2210 }
2211 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2212 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
2213 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
2214 }
2215 }
2216 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
2217 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2218 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
2219 s->downmix_coef[i][0]);
2220 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
2221 s->downmix_coef[i][1]);
2222 }
2223 av_dlog(s->avctx, "\n");
2224 }
2225
2226 if (s->ext_coding)
2227 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2228 else
2229 s->core_ext_mask = 0;
2230
2231 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2232
2233 /* only scan for extensions if ext_descr was unknown or indicated a
2234 * supported XCh extension */
2235 if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2ba45a60
DM
2236 /* if ext_descr was unknown, clear s->core_ext_mask so that the
2237 * extensions scan can fill it up */
2238 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2239
2240 /* extensions start at 32-bit boundaries into bitstream */
2241 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2242
2243 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2244 uint32_t bits = get_bits_long(&s->gb, 32);
2245
2246 switch (bits) {
2247 case 0x5a5a5a5a: {
2248 int ext_amode, xch_fsize;
2249
2250 s->xch_base_channel = s->prim_channels;
2251
2252 /* validate sync word using XCHFSIZE field */
2253 xch_fsize = show_bits(&s->gb, 10);
2254 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2255 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2256 continue;
2257
2258 /* skip length-to-end-of-frame field for the moment */
2259 skip_bits(&s->gb, 10);
2260
2261 s->core_ext_mask |= DCA_EXT_XCH;
2262
2263 /* extension amode(number of channels in extension) should be 1 */
2264 /* AFAIK XCh is not used for more channels */
2265 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
f6fa7814
DM
2266 av_log(avctx, AV_LOG_ERROR,
2267 "XCh extension amode %d not supported!\n",
2268 ext_amode);
2ba45a60
DM
2269 continue;
2270 }
2271
2272 if (s->xch_base_channel < 2) {
2273 avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
2274 continue;
2275 }
2276
2277 /* much like core primary audio coding header */
2278 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2279
2280 for (i = 0; i < (s->sample_blocks / 8); i++)
2281 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2282 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2283 continue;
2284 }
2285
2286 s->xch_present = 1;
2287 break;
2288 }
2289 case 0x47004a03:
2290 /* XXCh: extended channels */
2291 /* usually found either in core or HD part in DTS-HD HRA streams,
2292 * but not in DTS-ES which contains XCh extensions instead */
2293 s->core_ext_mask |= DCA_EXT_XXCH;
2294 dca_xxch_decode_frame(s);
2295 break;
2296
2297 case 0x1d95f262: {
2298 int fsize96 = show_bits(&s->gb, 12) + 1;
2299 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2300 continue;
2301
2302 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2303 get_bits_count(&s->gb));
2304 skip_bits(&s->gb, 12);
2305 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2306 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2307
2308 s->core_ext_mask |= DCA_EXT_X96;
2309 break;
2310 }
2311 }
2312
2313 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2314 }
2315 } else {
2316 /* no supported extensions, skip the rest of the core substream */
2317 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2318 }
2319
2320 if (s->core_ext_mask & DCA_EXT_X96)
2321 s->profile = FF_PROFILE_DTS_96_24;
2322 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2323 s->profile = FF_PROFILE_DTS_ES;
2324
2325 /* check for ExSS (HD part) */
2326 if (s->dca_buffer_size - s->frame_size > 32 &&
2327 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2328 dca_exss_parse_header(s);
2329
2330 avctx->profile = s->profile;
2331
2332 full_channels = channels = s->prim_channels + !!s->lfe;
2333
2334 /* If we have XXCH then the channel layout is managed differently */
2335 /* note that XLL will also have another way to do things */
2336 if (!(s->core_ext_mask & DCA_EXT_XXCH)
2337 || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2338 && avctx->request_channels
2339 < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2340 { /* xxx should also do MA extensions */
2341 if (s->amode < 16) {
2342 avctx->channel_layout = dca_core_channel_layout[s->amode];
2343
2344 if (s->prim_channels + !!s->lfe > 2 &&
2345 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2346 /*
2347 * Neither the core's auxiliary data nor our default tables contain
2348 * downmix coefficients for the additional channel coded in the XCh
2349 * extension, so when we're doing a Stereo downmix, don't decode it.
2350 */
2351 s->xch_disable = 1;
2352 }
2353
2354#if FF_API_REQUEST_CHANNELS
2355FF_DISABLE_DEPRECATION_WARNINGS
2356 if (s->xch_present && !s->xch_disable &&
2357 (!avctx->request_channels ||
2358 avctx->request_channels > num_core_channels + !!s->lfe)) {
2359FF_ENABLE_DEPRECATION_WARNINGS
2360#else
2361 if (s->xch_present && !s->xch_disable) {
2362#endif
092a9121
DM
2363 if (avctx->channel_layout & AV_CH_BACK_CENTER) {
2364 avpriv_request_sample(avctx, "XCh with Back center channel");
2365 return AVERROR_INVALIDDATA;
2366 }
2ba45a60
DM
2367 avctx->channel_layout |= AV_CH_BACK_CENTER;
2368 if (s->lfe) {
2369 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2370 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2371 } else {
2372 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2373 }
2374 if (s->channel_order_tab[s->xch_base_channel] < 0)
2375 return AVERROR_INVALIDDATA;
2376 } else {
f6fa7814 2377 channels = num_core_channels + !!s->lfe;
2ba45a60
DM
2378 s->xch_present = 0; /* disable further xch processing */
2379 if (s->lfe) {
2380 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2381 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2382 } else
2383 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2384 }
2385
2386 if (channels > !!s->lfe &&
2387 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2388 return AVERROR_INVALIDDATA;
2389
2390 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2391 av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2392 return AVERROR_INVALIDDATA;
2393 }
2394
2395 if (num_core_channels + !!s->lfe > 2 &&
2396 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
f6fa7814
DM
2397 channels = 2;
2398 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
2ba45a60
DM
2399 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2400 }
2401 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2402 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2403 s->channel_order_tab = dca_channel_order_native;
2404 }
2405 s->lfe_index = dca_lfe_index[s->amode];
2406 } else {
2407 av_log(avctx, AV_LOG_ERROR,
2408 "Non standard configuration %d !\n", s->amode);
2409 return AVERROR_INVALIDDATA;
2410 }
2411
2412 s->xxch_dmix_embedded = 0;
2413 } else {
2414 /* we only get here if an XXCH channel set can be added to the mix */
2415 channel_mask = s->xxch_core_spkmask;
2416
2417 if (avctx->request_channels > 0
2418 && avctx->request_channels < s->prim_channels) {
2419 channels = num_core_channels + !!s->lfe;
2420 for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2421 <= avctx->request_channels; i++) {
2422 channels += s->xxch_chset_nch[i];
2423 channel_mask |= s->xxch_spk_masks[i];
2424 }
2425 } else {
2426 channels = s->prim_channels + !!s->lfe;
2427 for (i = 0; i < s->xxch_chset; i++) {
2428 channel_mask |= s->xxch_spk_masks[i];
2429 }
2430 }
2431
2432 /* Given the DTS spec'ed channel mask, generate an avcodec version */
2433 channel_layout = 0;
2434 for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2435 if (channel_mask & (1 << i)) {
2436 channel_layout |= map_xxch_to_native[i];
2437 }
2438 }
2439
2440 /* make sure that we have managed to get equivalent dts/avcodec channel
2441 * masks in some sense -- unfortunately some channels could overlap */
2442 if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2443 av_log(avctx, AV_LOG_DEBUG,
2444 "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
2445 return AVERROR_INVALIDDATA;
2446 }
2447
2448 avctx->channel_layout = channel_layout;
2449
2450 if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2451 /* Estimate DTS --> avcodec ordering table */
2452 for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2453 mask = chset >= 0 ? s->xxch_spk_masks[chset]
2454 : s->xxch_core_spkmask;
2455 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2456 if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2457 lavc = map_xxch_to_native[i];
2458 posn = av_popcount(channel_layout & (lavc - 1));
2459 s->xxch_order_tab[j++] = posn;
2460 }
2461 }
2462
2463 }
2464
2465 s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2466 } else { /* native ordering */
2467 for (i = 0; i < channels; i++)
2468 s->xxch_order_tab[i] = i;
2469
2470 s->lfe_index = channels - 1;
2471 }
2472
2473 s->channel_order_tab = s->xxch_order_tab;
2474 }
2475
2476 if (avctx->channels != channels) {
2477 if (avctx->channels)
2478 av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2479 avctx->channels = channels;
2480 }
2481
2482 /* get output buffer */
2483 frame->nb_samples = 256 * (s->sample_blocks / 8);
2484 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2485 return ret;
f6fa7814 2486 samples_flt = (float **) frame->extended_data;
2ba45a60
DM
2487
2488 /* allocate buffer for extra channels if downmixing */
2489 if (avctx->channels < full_channels) {
2490 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2491 frame->nb_samples,
2492 avctx->sample_fmt, 0);
2493 if (ret < 0)
2494 return ret;
2495
2496 av_fast_malloc(&s->extra_channels_buffer,
2497 &s->extra_channels_buffer_size, ret);
2498 if (!s->extra_channels_buffer)
2499 return AVERROR(ENOMEM);
2500
f6fa7814 2501 ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
2ba45a60
DM
2502 s->extra_channels_buffer,
2503 full_channels - channels,
2504 frame->nb_samples, avctx->sample_fmt, 0);
2505 if (ret < 0)
2506 return ret;
2507 }
2508
2509 /* filter to get final output */
2510 for (i = 0; i < (s->sample_blocks / 8); i++) {
2511 int ch;
2512
2513 for (ch = 0; ch < channels; ch++)
2514 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2515 for (; ch < full_channels; ch++)
2516 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2517
2518 dca_filter_channels(s, i);
2519
2520 /* If this was marked as a DTS-ES stream we need to subtract back- */
2521 /* channel from SL & SR to remove matrixed back-channel signal */
2522 if ((s->source_pcm_res & 1) && s->xch_present) {
2523 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2524 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2525 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
f6fa7814
DM
2526 s->fdsp->vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2527 s->fdsp->vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2ba45a60
DM
2528 }
2529
2530 /* If stream contains XXCH, we might need to undo an embedded downmix */
2531 if (s->xxch_dmix_embedded) {
2532 /* Loop over channel sets in turn */
2533 ch = num_core_channels;
2534 for (chset = 0; chset < s->xxch_chset; chset++) {
2535 endch = ch + s->xxch_chset_nch[chset];
2536 mask = s->xxch_dmix_embedded;
2537
2538 /* undo downmix */
2539 for (j = ch; j < endch; j++) {
2540 if (mask & (1 << j)) { /* this channel has been mixed-out */
2541 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2542 for (k = 0; k < endch; k++) {
2543 achan = s->channel_order_tab[k];
2544 scale = s->xxch_dmix_coeff[j][k];
2545 if (scale != 0.0) {
2546 dst_chan = s->samples_chanptr[achan];
f6fa7814 2547 s->fdsp->vector_fmac_scalar(dst_chan, src_chan,
2ba45a60
DM
2548 -scale, 256);
2549 }
2550 }
2551 }
2552 }
2553
2554 /* if a downmix has been embedded then undo the pre-scaling */
2555 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2556 scale = s->xxch_dmix_sf[chset];
2557
2558 for (j = 0; j < ch; j++) {
2559 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2560 for (k = 0; k < 256; k++)
2561 src_chan[k] *= scale;
2562 }
2563
2564 /* LFE channel is always part of core, scale if it exists */
2565 if (s->lfe) {
2566 src_chan = s->samples_chanptr[s->lfe_index];
2567 for (k = 0; k < 256; k++)
2568 src_chan[k] *= scale;
2569 }
2570 }
2571
2572 ch = endch;
2573 }
2574
2575 }
2576 }
2577
2578 /* update lfe history */
2579 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2580 for (i = 0; i < 2 * s->lfe * 4; i++)
2581 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2582
2583 /* AVMatrixEncoding
2584 *
2585 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2586 ret = ff_side_data_update_matrix_encoding(frame,
2587 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2588 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
2589 if (ret < 0)
2590 return ret;
2591
2592 *got_frame_ptr = 1;
2593
2594 return buf_size;
2595}
2596
2ba45a60
DM
2597/**
2598 * DCA initialization
2599 *
2600 * @param avctx pointer to the AVCodecContext
2601 */
2602
2603static av_cold int dca_decode_init(AVCodecContext *avctx)
2604{
2605 DCAContext *s = avctx->priv_data;
2606
2607 s->avctx = avctx;
2608 dca_init_vlcs();
2609
f6fa7814
DM
2610 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT);
2611 if (!s->fdsp)
2612 return AVERROR(ENOMEM);
2613
2ba45a60
DM
2614 ff_mdct_init(&s->imdct, 6, 1, 1.0);
2615 ff_synth_filter_init(&s->synth);
2616 ff_dcadsp_init(&s->dcadsp);
2617 ff_fmt_convert_init(&s->fmt_conv, avctx);
2618
2619 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2620
2621 /* allow downmixing to stereo */
2622#if FF_API_REQUEST_CHANNELS
2623FF_DISABLE_DEPRECATION_WARNINGS
2624 if (avctx->request_channels == 2)
2625 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
2626FF_ENABLE_DEPRECATION_WARNINGS
2627#endif
2628 if (avctx->channels > 2 &&
2629 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
2630 avctx->channels = 2;
2631
2632 return 0;
2633}
2634
2635static av_cold int dca_decode_end(AVCodecContext *avctx)
2636{
2637 DCAContext *s = avctx->priv_data;
2638 ff_mdct_end(&s->imdct);
2639 av_freep(&s->extra_channels_buffer);
f6fa7814 2640 av_freep(&s->fdsp);
2ba45a60
DM
2641 return 0;
2642}
2643
2644static const AVProfile profiles[] = {
2645 { FF_PROFILE_DTS, "DTS" },
2646 { FF_PROFILE_DTS_ES, "DTS-ES" },
2647 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2648 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2649 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2650 { FF_PROFILE_UNKNOWN },
2651};
2652
2653static const AVOption options[] = {
f6fa7814 2654 { "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 },
2ba45a60
DM
2655 { NULL },
2656};
2657
2658static const AVClass dca_decoder_class = {
2659 .class_name = "DCA decoder",
2660 .item_name = av_default_item_name,
2661 .option = options,
2662 .version = LIBAVUTIL_VERSION_INT,
2663 .category = AV_CLASS_CATEGORY_DECODER,
2664};
2665
2666AVCodec ff_dca_decoder = {
2667 .name = "dca",
2668 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2669 .type = AVMEDIA_TYPE_AUDIO,
2670 .id = AV_CODEC_ID_DTS,
2671 .priv_data_size = sizeof(DCAContext),
2672 .init = dca_decode_init,
2673 .decode = dca_decode_frame,
2674 .close = dca_decode_end,
2675 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2676 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2677 AV_SAMPLE_FMT_NONE },
2678 .profiles = NULL_IF_CONFIG_SMALL(profiles),
2679 .priv_class = &dca_decoder_class,
2680};