dca: handle errors from dca_decode_block()
[libav.git] / libavcodec / dca.c
CommitLineData
01ca9ac3
KS
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 *
2912e87a 8 * This file is part of Libav.
01ca9ac3 9 *
2912e87a 10 * Libav is free software; you can redistribute it and/or
01ca9ac3
KS
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 *
2912e87a 15 * Libav is distributed in the hope that it will be useful,
01ca9ac3
KS
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
2912e87a 21 * License along with Libav; if not, write to the Free Software
01ca9ac3
KS
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
01ca9ac3
KS
25#include <math.h>
26#include <stddef.h>
27#include <stdio.h>
28
f5a2d285 29#include "libavutil/common.h"
843c7aa8 30#include "libavutil/intmath.h"
0dc7df28 31#include "libavutil/intreadwrite.h"
737eb597 32#include "libavutil/audioconvert.h"
01ca9ac3
KS
33#include "avcodec.h"
34#include "dsputil.h"
1429224b 35#include "fft.h"
9106a698 36#include "get_bits.h"
b2755007 37#include "put_bits.h"
01ca9ac3
KS
38#include "dcadata.h"
39#include "dcahuff.h"
98c98e04 40#include "dca.h"
4f99c31c 41#include "synth_filter.h"
309d16a4 42#include "dcadsp.h"
c73d99e6 43#include "fmtconvert.h"
01ca9ac3 44
bf5d46d8
MR
45#if ARCH_ARM
46# include "arm/dca.h"
47#endif
48
01ca9ac3
KS
49//#define TRACE
50
774e9acf 51#define DCA_PRIM_CHANNELS_MAX (7)
01ca9ac3
KS
52#define DCA_SUBBANDS (32)
53#define DCA_ABITS_MAX (32) /* Should be 28 */
63c3b716 54#define DCA_SUBSUBFRAMES_MAX (4)
774e9acf 55#define DCA_SUBFRAMES_MAX (16)
77b4b7c3 56#define DCA_BLOCKS_MAX (16)
01ca9ac3
KS
57#define DCA_LFE_MAX (3)
58
59enum 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
f5a2d285
AH
73/* these are unconfirmed but should be mostly correct */
74enum 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
93enum 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
7e06e0ed
AH
106/* -1 are reserved or unknown */
107static 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
87c3b9bc
BL
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.
e22192ec 126 * S -> side, when both rear and back are configured move one of them to the side channel
87c3b9bc 127 * OV -> center back
c2fcd0a7 128 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
87c3b9bc
BL
129 */
130
131static const int64_t dca_core_channel_layout[] = {
63e8d976
SS
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
c2fcd0a7 139 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER, ///< 4, C + L + R+ S
63e8d976
SS
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
87c3b9bc
BL
148};
149
92765276
BL
150static 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
3d5a9ba1
NB
154static 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},
92765276
BL
171};
172
3d5a9ba1
NB
173static 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},
92765276
BL
190};
191
3d5a9ba1
NB
192static 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
211static 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};
87c3b9bc 229
01ca9ac3
KS
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
01ca9ac3 238
4df7beb3 239#define DCA_MAX_FRAME_SIZE 16384
f5a2d285 240#define DCA_MAX_EXSS_HEADER_SIZE 4096
01ca9ac3 241
39f4d329
AH
242#define DCA_BUFFER_PADDING_SIZE 1024
243
01ca9ac3
KS
244/** Bit allocation */
245typedef 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
252static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
253static BitAlloc dca_tmode; ///< transition mode VLCs
254static BitAlloc dca_scalefactor; ///< scalefactor VLCs
255static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
256
5a6a6cc7 257static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
01ca9ac3
KS
258{
259 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
260}
261
262typedef 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
9ed73b48 273 int bit_rate_index; ///< transmission bit rate index
01ca9ac3
KS
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
1360f07e 296 int is_channels_set; ///< check for if the channel number is already set
ebf71dbd 297 int total_channels; ///< number of channels including extensions
01ca9ac3
KS
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 */
774e9acf
NB
309 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
310 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
01ca9ac3
KS
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
6baef06e 323 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
01ca9ac3
KS
324 int lfe_scale_factor;
325
326 /* Subband samples history (for ADPCM) */
bf5d46d8 327 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
9d35fa52
VS
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];
47f0e052 330 int hist_index[DCA_PRIM_CHANNELS_MAX];
9d35fa52 331 DECLARE_ALIGNED(32, float, raXin)[32];
01ca9ac3
KS
332
333 int output; ///< type of output
1402ee72 334 float scale_bias; ///< output scale
01ca9ac3 335
9d35fa52
VS
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];
774e9acf 338 const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1];
01ca9ac3 339
39f4d329 340 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
01ca9ac3
KS
341 int dca_buffer_size; ///< how much data is in the dca_buffer
342
92765276 343 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe
01ca9ac3
KS
344 GetBitContext gb;
345 /* Current position in DCA frame */
346 int current_subframe;
347 int current_subsubframe;
348
7e06e0ed
AH
349 int core_ext_mask; ///< present extensions in the core substream
350
d0a18850 351 /* XCh extension information */
7e06e0ed 352 int xch_present; ///< XCh extension present and valid
d0a18850
NB
353 int xch_base_channel; ///< index of first (only) channel containing XCH data
354
f5a2d285
AH
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
01ca9ac3
KS
363 int debug_flag; ///< used for suppressing repeated error messages output
364 DSPContext dsp;
01b22147 365 FFTContext imdct;
f462ed1f 366 SynthFilterContext synth;
309d16a4 367 DCADSPContext dcadsp;
c73d99e6 368 FmtConvertContext fmt_conv;
01ca9ac3
KS
369} DCAContext;
370
0cfa85dd
KS
371static 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
98a6fff9 380static av_cold void dca_init_vlcs(void)
01ca9ac3 381{
5e534865 382 static int vlcs_initialized = 0;
0cfa85dd
KS
383 int i, j, c = 14;
384 static VLC_TYPE dca_table[23622][2];
01ca9ac3 385
5e534865 386 if (vlcs_initialized)
01ca9ac3
KS
387 return;
388
389 dca_bitalloc_index.offset = 1;
32dd6a9c 390 dca_bitalloc_index.wrap = 2;
0cfa85dd
KS
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];
01ca9ac3
KS
394 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
395 bitalloc_12_bits[i], 1, 1,
0cfa85dd
KS
396 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
397 }
01ca9ac3
KS
398 dca_scalefactor.offset = -64;
399 dca_scalefactor.wrap = 2;
0cfa85dd
KS
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];
01ca9ac3
KS
403 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
404 scales_bits[i], 1, 1,
0cfa85dd
KS
405 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
406 }
01ca9ac3
KS
407 dca_tmode.offset = 0;
408 dca_tmode.wrap = 1;
0cfa85dd
KS
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];
01ca9ac3
KS
412 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
413 tmode_bits[i], 1, 1,
0cfa85dd
KS
414 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
415 }
01ca9ac3 416
d1177cb5
NB
417 for (i = 0; i < 10; i++)
418 for (j = 0; j < 7; j++){
419 if (!bitalloc_codes[i][j]) break;
01ca9ac3
KS
420 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
421 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
0cfa85dd
KS
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];
01ca9ac3
KS
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,
0cfa85dd
KS
427 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
428 c++;
01ca9ac3 429 }
5e534865 430 vlcs_initialized = 1;
01ca9ac3
KS
431}
432
433static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
434{
435 while(len--)
436 *dst++ = get_bits(gb, bits);
437}
438
774e9acf 439static int dca_parse_audio_coding_header(DCAContext * s, int base_channel)
01ca9ac3
KS
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
774e9acf 446 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
6baef06e 447 s->prim_channels = s->total_channels;
774e9acf 448
6baef06e 449 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
774e9acf 450 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
6baef06e
NB
451
452
774e9acf 453 for (i = base_channel; i < s->prim_channels; i++) {
6baef06e
NB
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 }
774e9acf 458 for (i = base_channel; i < s->prim_channels; i++) {
6baef06e
NB
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 }
774e9acf
NB
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);
6baef06e
NB
467
468 /* Get codebooks quantization indexes */
774e9acf
NB
469 if (!base_channel)
470 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
6baef06e 471 for (j = 1; j < 11; j++)
774e9acf 472 for (i = base_channel; i < s->prim_channels; i++)
6baef06e
NB
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++)
774e9acf 477 for (i = base_channel; i < s->prim_channels; i++)
6baef06e
NB
478 s->scalefactor_adj[i][j] = 1;
479
480 for (j = 1; j < 11; j++)
774e9acf 481 for (i = base_channel; i < s->prim_channels; i++)
6baef06e
NB
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);
774e9acf 496 for (i = base_channel; i < s->prim_channels; i++){
6baef06e
NB
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
518static int dca_parse_frame_header(DCAContext * s)
519{
01ca9ac3
KS
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)
f44059d2 532 return AVERROR_INVALIDDATA;
01ca9ac3
KS
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)
f44059d2 536 return AVERROR_INVALIDDATA;
d5b3a863 537 s->bit_rate_index = get_bits(&s->gb, 5);
9ed73b48 538 s->bit_rate = dca_bit_rates[s->bit_rate_index];
01ca9ac3 539 if (!s->bit_rate)
f44059d2 540 return AVERROR_INVALIDDATA;
01ca9ac3
KS
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 */
cc826626 566 s->output = s->amode;
d1177cb5 567 if (s->lfe) s->output |= DCA_LFE;
01ca9ac3
KS
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]);
49c91c34
BL
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);
01ca9ac3
KS
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;
01ca9ac3 609
774e9acf 610 return dca_parse_audio_coding_header(s, 0);
01ca9ac3
KS
611}
612
613
c5d13492 614static inline int get_scale(GetBitContext *gb, int level, int value)
01ca9ac3
KS
615{
616 if (level < 5) {
617 /* huffman encoded */
c5d13492 618 value += get_bitalloc(gb, &dca_scalefactor, level);
d1177cb5 619 } else if (level < 8)
01ca9ac3
KS
620 value = get_bits(gb, level + 1);
621 return value;
622}
623
774e9acf 624static int dca_subframe_header(DCAContext * s, int base_channel, int block_index)
01ca9ac3
KS
625{
626 /* Primary audio coding side information */
627 int j, k;
628
39f4d329 629 if (get_bits_left(&s->gb) < 0)
f44059d2 630 return AVERROR_INVALIDDATA;
39f4d329 631
774e9acf
NB
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++) {
01ca9ac3
KS
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 */
774e9acf 643 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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 */
774e9acf 653 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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);
ebf71dbd
BL
659 else if (s->bitalloc_huffman[j] == 7) {
660 av_log(s->avctx, AV_LOG_ERROR,
661 "Invalid bit allocation index\n");
f44059d2 662 return AVERROR_INVALIDDATA;
ebf71dbd 663 } else {
01ca9ac3 664 s->bitalloc[j][k] =
c5d13492 665 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
01ca9ac3
KS
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]);
f44059d2 671 return AVERROR_INVALIDDATA;
01ca9ac3
KS
672 }
673 }
674 }
675
676 /* Transition mode */
774e9acf 677 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
678 for (k = 0; k < s->subband_activity[j]; k++) {
679 s->transition_mode[j][k] = 0;
774e9acf 680 if (s->subsubframes[s->current_subframe] > 1 &&
01ca9ac3
KS
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
39f4d329 688 if (get_bits_left(&s->gb) < 0)
f44059d2 689 return AVERROR_INVALIDDATA;
39f4d329 690
774e9acf 691 for (j = base_channel; j < s->prim_channels; j++) {
a9f87158 692 const uint32_t *scale_table;
01ca9ac3
KS
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)
a9f87158 698 scale_table = scale_factor_quant7;
01ca9ac3 699 else
a9f87158 700 scale_table = scale_factor_quant6;
01ca9ac3
KS
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) {
c5d13492 707 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
01ca9ac3
KS
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 */
c5d13492 713 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
01ca9ac3
KS
714 s->scale_factor[j][k][1] = scale_table[scale_sum];
715 }
716 }
717 }
718
719 /* Joint subband scale factor codebook select */
774e9acf 720 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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
39f4d329 726 if (get_bits_left(&s->gb) < 0)
f44059d2 727 return AVERROR_INVALIDDATA;
39f4d329 728
01ca9ac3 729 /* Scale factors for joint subband coding */
774e9acf 730 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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++) {
c5d13492 742 scale = get_scale(&s->gb, s->joint_huff[j], 0);
01ca9ac3
KS
743 scale += 64; /* bias */
744 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
745 }
746
268fcbe2 747 if (!(s->debug_flag & 0x02)) {
01ca9ac3
KS
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 */
774e9acf 756 if (!base_channel && s->prim_channels > 2) {
d1177cb5 757 if (s->downmix) {
774e9acf 758 for (j = base_channel; j < s->prim_channels; j++) {
6369e6eb
JR
759 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
760 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
761 }
c31a76e4
JR
762 } else {
763 int am = s->amode & DCA_CHANNEL_MASK;
774e9acf 764 for (j = base_channel; j < s->prim_channels; j++) {
c31a76e4
JR
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 }
01ca9ac3
KS
769 }
770
771 /* Dynamic range coefficient */
ace7f813 772 if (!base_channel && s->dynrange)
01ca9ac3
KS
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 */
774e9acf 785 for (j = base_channel; j < s->prim_channels; j++)
01ca9ac3
KS
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 */
774e9acf 791 if (!base_channel && s->lfe) {
01ca9ac3 792 /* LFE samples */
6baef06e 793 int lfe_samples = 2 * s->lfe * (4 + block_index);
774e9acf 794 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
01ca9ac3
KS
795 float lfe_scale;
796
6baef06e 797 for (j = lfe_samples; j < lfe_end_sample; j++) {
01ca9ac3
KS
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
6baef06e 808 for (j = lfe_samples; j < lfe_end_sample; j++)
01ca9ac3
KS
809 s->lfe_data[j] *= lfe_scale;
810 }
811
812#ifdef TRACE
774e9acf 813 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
01ca9ac3 814 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
774e9acf
NB
815 s->partial_samples[s->current_subframe]);
816 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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 }
774e9acf 822 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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 }
774e9acf 831 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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 }
774e9acf 837 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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 }
774e9acf 843 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
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 }
774e9acf 853 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3 854 if (s->joint_intensity[j] > 0) {
56e4603e 855 int source_channel = s->joint_intensity[j] - 1;
01ca9ac3
KS
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 }
774e9acf 862 if (!base_channel && s->prim_channels > 2 && s->downmix) {
01ca9ac3
KS
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 }
774e9acf 870 for (j = base_channel; j < s->prim_channels; j++)
01ca9ac3
KS
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]);
774e9acf 873 if (!base_channel && s->lfe) {
6baef06e
NB
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
01ca9ac3 877 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
6baef06e 878 for (j = lfe_samples; j < lfe_end_sample; j++)
01ca9ac3
KS
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
887static void qmf_32_subbands(DCAContext * s, int chans,
888 float samples_in[32][8], float *samples_out,
9d06d7bc 889 float scale)
01ca9ac3 890{
b6398969 891 const float *prCoeff;
d49dded5 892 int i;
01ca9ac3 893
b92d483b 894 int sb_act = s->subband_activity[chans];
89df5e95 895 int subindex;
01ca9ac3 896
89df5e95 897 scale *= sqrt(1/8.0);
01ca9ac3
KS
898
899 /* Select filter */
900 if (!s->multirate_inter) /* Non-perfect reconstruction */
b6398969 901 prCoeff = fir_32bands_nonperfect;
01ca9ac3 902 else /* Perfect reconstruction */
b6398969 903 prCoeff = fir_32bands_perfect;
01ca9ac3 904
bf00a73a
MR
905 for (i = sb_act; i < 32; i++)
906 s->raXin[i] = 0.0;
907
01ca9ac3
KS
908 /* Reconstructed channel sample index */
909 for (subindex = 0; subindex < 8; subindex++) {
01ca9ac3 910 /* Load in one sample from each subband and clear inactive subbands */
b92d483b 911 for (i = 0; i < sb_act; i++){
559c244d
MR
912 unsigned sign = (i - 1) & 2;
913 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
0dc7df28 914 AV_WN32A(&s->raXin[i], v);
89df5e95 915 }
01ca9ac3 916
f462ed1f 917 s->synth.synth_filter_float(&s->imdct,
4f99c31c
MN
918 s->subband_fir_hist[chans], &s->hist_index[chans],
919 s->subband_fir_noidea[chans], prCoeff,
80ba1ddb 920 samples_out, s->raXin, scale);
89df5e95 921 samples_out+= 32;
01ca9ac3 922
01ca9ac3
KS
923 }
924}
925
309d16a4 926static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
01ca9ac3 927 int num_deci_sample, float *samples_in,
9d06d7bc 928 float *samples_out, float scale)
01ca9ac3
KS
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
309d16a4 938 int decifactor;
01ca9ac3 939 const float *prCoeff;
01ca9ac3
KS
940 int deciindex;
941
942 /* Select decimation filter */
943 if (decimation_select == 1) {
766fefe8 944 decifactor = 64;
01ca9ac3
KS
945 prCoeff = lfe_fir_128;
946 } else {
766fefe8 947 decifactor = 32;
01ca9ac3
KS
948 prCoeff = lfe_fir_64;
949 }
950 /* Interpolation */
951 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
309d16a4 952 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
80ba1ddb 953 scale);
766fefe8 954 samples_in++;
309d16a4 955 samples_out += 2 * decifactor;
01ca9ac3
KS
956 }
957}
958
959/* downmixing routines */
c31a76e4 960#define MIX_REAR1(samples, si1, rs, coef) \
9d06d7bc
JR
961 samples[i] += samples[si1] * coef[rs][0]; \
962 samples[i+256] += samples[si1] * coef[rs][1];
01ca9ac3 963
c31a76e4 964#define MIX_REAR2(samples, si1, si2, rs, coef) \
9d06d7bc
JR
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];
01ca9ac3 967
c31a76e4 968#define MIX_FRONT3(samples, coef) \
9d06d7bc
JR
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];
01ca9ac3
KS
974
975#define DOWNMIX_TO_STEREO(op1, op2) \
d1177cb5 976 for (i = 0; i < 256; i++){ \
01ca9ac3
KS
977 op1 \
978 op2 \
979 }
980
c31a76e4 981static void dca_downmix(float *samples, int srcfmt,
df984493 982 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
9d06d7bc 983 const int8_t *channel_mapping)
01ca9ac3 984{
df984493 985 int c,l,r,sl,sr,s;
01ca9ac3 986 int i;
df984493 987 float t, u, v;
c31a76e4
JR
988 float coef[DCA_PRIM_CHANNELS_MAX][2];
989
d1177cb5 990 for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
c31a76e4
JR
991 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
992 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
993 }
01ca9ac3
KS
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:
df984493
NB
1006 c = channel_mapping[0] * 256;
1007 l = channel_mapping[1] * 256;
1008 r = channel_mapping[2] * 256;
c31a76e4 1009 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
01ca9ac3
KS
1010 break;
1011 case DCA_2F1R:
df984493
NB
1012 s = channel_mapping[2] * 256;
1013 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef),);
01ca9ac3
KS
1014 break;
1015 case DCA_3F1R:
df984493
NB
1016 c = channel_mapping[0] * 256;
1017 l = channel_mapping[1] * 256;
1018 r = channel_mapping[2] * 256;
1019 s = channel_mapping[3] * 256;
c31a76e4 1020 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
df984493 1021 MIX_REAR1(samples, i + s, 3, coef));
01ca9ac3
KS
1022 break;
1023 case DCA_2F2R:
df984493
NB
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),);
01ca9ac3
KS
1027 break;
1028 case DCA_3F2R:
df984493
NB
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;
c31a76e4 1034 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
df984493 1035 MIX_REAR2(samples, i + sl, i + sr, 3, coef));
01ca9ac3
KS
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 */
1043static 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++) {
843c7aa8
MR
1049 int div = FASTDIV(code, levels);
1050 values[i] = code - offset - div*levels;
1051 code = div;
01ca9ac3
KS
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");
f44059d2 1058 return AVERROR_INVALIDDATA;
01ca9ac3
KS
1059 }
1060}
1061
1062static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1063static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1064
bf5d46d8
MR
1065#ifndef int8x8_fmul_int32
1066static 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
774e9acf 1075static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
01ca9ac3
KS
1076{
1077 int k, l;
1078 int subsubframe = s->current_subsubframe;
1079
a9f87158 1080 const float *quant_step_table;
01ca9ac3
KS
1081
1082 /* FIXME */
77b4b7c3 1083 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
69e17136 1084 LOCAL_ALIGNED_16(int, block, [8]);
01ca9ac3
KS
1085
1086 /*
1087 * Audio data
1088 */
1089
1090 /* Select quantization step size table */
9ed73b48 1091 if (s->bit_rate_index == 0x1f)
a9f87158 1092 quant_step_table = lossless_quant_d;
01ca9ac3 1093 else
a9f87158 1094 quant_step_table = lossy_quant_d;
01ca9ac3 1095
774e9acf 1096 for (k = base_channel; k < s->prim_channels; k++) {
39f4d329 1097 if (get_bits_left(&s->gb) < 0)
f44059d2 1098 return AVERROR_INVALIDDATA;
39f4d329 1099
01ca9ac3
KS
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];
01ca9ac3
KS
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 */
d1177cb5 1118 if (!abits){
01ca9ac3 1119 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
69e17136
MR
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
d1177cb5
NB
1125 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
1126 if (abits <= 7){
2bb29da6
MR
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 }
01ca9ac3 1143 }else{
2bb29da6 1144 /* Huffman coded */
01ca9ac3 1145 for (m = 0; m < 8; m++)
2bb29da6 1146 block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
01ca9ac3 1147 }
01ca9ac3 1148
c73d99e6 1149 s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
69e17136
MR
1150 block, rscale, 8);
1151 }
01ca9ac3
KS
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. */
bf5d46d8 1179 int hfvq = s->high_freq_vq[k][l];
01ca9ac3
KS
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
bf5d46d8
MR
1186 int8x8_fmul_int32(subband_samples[k][l],
1187 &high_freq_vq[hfvq][subsubframe * 8],
1188 s->scale_factor[k][l][0]);
01ca9ac3
KS
1189 }
1190 }
1191
1192 /* Check for DSYNC after subsubframe */
774e9acf 1193 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
01ca9ac3
KS
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 */
774e9acf 1204 for (k = base_channel; k < s->prim_channels; k++)
01ca9ac3
KS
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
6baef06e
NB
1209 return 0;
1210}
1211
1212static 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
01ca9ac3
KS
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};*/
92765276 1221 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
9d06d7bc 1222 M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ );
01ca9ac3
KS
1223 }
1224
1225 /* Down mixing */
6baef06e 1226 if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
9d06d7bc 1227 dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
01ca9ac3
KS
1228 }
1229
1230 /* Generate LFE samples for this subsubframe FIXME!!! */
1231 if (s->output & DCA_LFE) {
309d16a4 1232 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
6baef06e 1233 s->lfe_data + 2 * s->lfe * (block_index + 4),
92765276 1234 &s->samples[256 * dca_lfe_index[s->amode]],
9d06d7bc 1235 (1.0/256.0)*s->scale_bias);
01ca9ac3
KS
1236 /* Outputs 20bits pcm samples */
1237 }
1238
1239 return 0;
1240}
1241
1242
774e9acf 1243static int dca_subframe_footer(DCAContext * s, int base_channel)
01ca9ac3
KS
1244{
1245 int aux_data_count = 0, i;
01ca9ac3
KS
1246
1247 /*
1248 * Unpack optional information
1249 */
1250
774e9acf
NB
1251 /* presumably optional information only appears in the core? */
1252 if (!base_channel) {
d1177cb5
NB
1253 if (s->timestamp)
1254 get_bits(&s->gb, 32);
01ca9ac3 1255
d1177cb5
NB
1256 if (s->aux_data)
1257 aux_data_count = get_bits(&s->gb, 6);
01ca9ac3 1258
d1177cb5
NB
1259 for (i = 0; i < aux_data_count; i++)
1260 get_bits(&s->gb, 8);
01ca9ac3 1261
d1177cb5
NB
1262 if (s->crc_present && (s->downmix || s->dynrange))
1263 get_bits(&s->gb, 16);
774e9acf 1264 }
01ca9ac3 1265
01ca9ac3
KS
1266 return 0;
1267}
1268
1269/**
1270 * Decode a dca frame block
1271 *
1272 * @param s pointer to the DCAContext
1273 */
1274
774e9acf 1275static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
01ca9ac3 1276{
f44059d2 1277 int ret;
01ca9ac3
KS
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);
f44059d2 1283 return AVERROR_INVALIDDATA;
01ca9ac3
KS
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 */
f44059d2
JR
1291 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1292 return ret;
01ca9ac3
KS
1293 }
1294
1295 /* Read subsubframe */
1296#ifdef TRACE
1297 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1298#endif
f44059d2
JR
1299 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1300 return ret;
01ca9ac3
KS
1301
1302 /* Update state */
1303 s->current_subsubframe++;
774e9acf 1304 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
01ca9ac3
KS
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 */
f44059d2
JR
1313 if ((ret = dca_subframe_footer(s, base_channel)))
1314 return ret;
01ca9ac3
KS
1315 }
1316
1317 return 0;
1318}
1319
1320/**
1321 * Convert bitstream to one representation based on sync marker
1322 */
a9f87158 1323static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
01ca9ac3
KS
1324 int max_size)
1325{
1326 uint32_t mrk;
1327 int i, tmp;
a9f87158
MN
1328 const uint16_t *ssrc = (const uint16_t *) src;
1329 uint16_t *sdst = (uint16_t *) dst;
01ca9ac3
KS
1330 PutBitContext pb;
1331
d1177cb5 1332 if ((unsigned)src_size > (unsigned)max_size) {
be73d76b 1333// av_log(NULL, AV_LOG_ERROR, "Input frame size larger than DCA_MAX_FRAME_SIZE!\n");
d041a598
KS
1334// return -1;
1335 src_size = max_size;
56fd7cc5 1336 }
9f1473b3 1337
01ca9ac3
KS
1338 mrk = AV_RB32(src);
1339 switch (mrk) {
1340 case DCA_MARKER_RAW_BE:
56523713
MN
1341 memcpy(dst, src, src_size);
1342 return src_size;
01ca9ac3 1343 case DCA_MARKER_RAW_LE:
56523713 1344 for (i = 0; i < (src_size + 1) >> 1; i++)
8fc0162a 1345 *sdst++ = av_bswap16(*ssrc++);
56523713 1346 return src_size;
01ca9ac3
KS
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:
f44059d2 1357 return AVERROR_INVALIDDATA;
01ca9ac3
KS
1358 }
1359}
1360
1361/**
f5a2d285
AH
1362 * Return the number of channels in an ExSS speaker mask (HD)
1363 */
1364static 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 */
1384static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1385{
e86e8581
GT
1386 int i;
1387
1388 for (i = 0; i < channels; i++) {
f5a2d285
AH
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 */
1398static 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;
8f4a5d22
AH
1532 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1533 DCA_EXT_EXSS_XXCH))
f5a2d285 1534 s->profile = FF_PROFILE_DTS_HD_HRA;
f5a2d285
AH
1535
1536 if (!(extensions_mask & DCA_EXT_CORE))
1537 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
7e06e0ed
AH
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);
f5a2d285
AH
1541
1542 return 0;
1543}
1544
1545/**
1546 * Parse extension substream header (HD)
1547 */
1548static void dca_exss_parse_header(DCAContext *s)
1549{
1550 int ss_index;
1551 int blownup;
f5a2d285
AH
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);
e65ab9d9
MR
1564 skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1565 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
f5a2d285
AH
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/**
01ca9ac3
KS
1628 * Main frame decoding function
1629 * FIXME add arguments
1630 */
1631static int dca_decode_frame(AVCodecContext * avctx,
1632 void *data, int *data_size,
7a00bbad 1633 AVPacket *avpkt)
01ca9ac3 1634{
7a00bbad
TB
1635 const uint8_t *buf = avpkt->data;
1636 int buf_size = avpkt->size;
01ca9ac3 1637
6baef06e 1638 int lfe_samples;
774e9acf 1639 int num_core_channels = 0;
aae6eead 1640 int i, ret;
9aa8193a
JR
1641 float *samples_flt = data;
1642 int16_t *samples_s16 = data;
1643 int out_size;
01ca9ac3
KS
1644 DCAContext *s = avctx->priv_data;
1645 int channels;
f5a2d285 1646 int core_ss_end;
01ca9ac3
KS
1647
1648
d0a18850 1649 s->xch_present = 0;
f5a2d285
AH
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);
f44059d2 1653 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
56fd7cc5 1654 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
f44059d2 1655 return AVERROR_INVALIDDATA;
01ca9ac3
KS
1656 }
1657
1658 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
aae6eead 1659 if ((ret = dca_parse_frame_header(s)) < 0) {
01ca9ac3 1660 //seems like the frame is corrupt, try with the next one
aae6eead 1661 return ret;
01ca9ac3
KS
1662 }
1663 //set AVCodec values with parsed data
1664 avctx->sample_rate = s->sample_rate;
01ca9ac3 1665 avctx->bit_rate = s->bit_rate;
49c7006c 1666 avctx->frame_size = s->sample_blocks * 32;
01ca9ac3 1667
f5a2d285
AH
1668 s->profile = FF_PROFILE_DTS;
1669
6baef06e 1670 for (i = 0; i < (s->sample_blocks / 8); i++) {
272fcc32
JR
1671 if ((ret = dca_decode_block(s, 0, i))) {
1672 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1673 return ret;
1674 }
774e9acf
NB
1675 }
1676
1677 /* record number of core channels incase less than max channels are requested */
1678 num_core_channels = s->prim_channels;
1679
7e06e0ed
AH
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;
774e9acf 1684
f5a2d285
AH
1685 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1686
7e06e0ed
AH
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
f5a2d285 1698 while(core_ss_end - get_bits_count(&s->gb) >= 32) {
324a94b0 1699 uint32_t bits = get_bits_long(&s->gb, 32);
774e9acf
NB
1700
1701 switch(bits) {
1702 case 0x5a5a5a5a: {
0712c230
NB
1703 int ext_amode, xch_fsize;
1704
d0a18850
NB
1705 s->xch_base_channel = s->prim_channels;
1706
0712c230
NB
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;
774e9acf
NB
1712
1713 /* skip length-to-end-of-frame field for the moment */
1714 skip_bits(&s->gb, 10);
1715
7e06e0ed 1716 s->core_ext_mask |= DCA_EXT_XCH;
f5a2d285 1717
774e9acf
NB
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 */
d0a18850 1727 dca_parse_audio_coding_header(s, s->xch_base_channel);
774e9acf
NB
1728
1729 for (i = 0; i < (s->sample_blocks / 8); i++) {
272fcc32
JR
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 }
774e9acf
NB
1734 }
1735
d0a18850 1736 s->xch_present = 1;
774e9acf
NB
1737 break;
1738 }
f5a2d285
AH
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 */
7e06e0ed 1743 s->core_ext_mask |= DCA_EXT_XXCH;
f5a2d285
AH
1744 break;
1745
df1c694c
AH
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);
774e9acf 1754 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
f5a2d285 1755
7e06e0ed 1756 s->core_ext_mask |= DCA_EXT_X96;
774e9acf
NB
1757 break;
1758 }
df1c694c 1759 }
774e9acf 1760
324a94b0 1761 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
6baef06e
NB
1762 }
1763
7e06e0ed
AH
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
f5a2d285
AH
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
cc826626 1781 channels = s->prim_channels + !!s->lfe;
92765276
BL
1782
1783 if (s->amode<16) {
87c3b9bc 1784 avctx->channel_layout = dca_core_channel_layout[s->amode];
87c3b9bc 1785
d0a18850 1786 if (s->xch_present && (!avctx->request_channels ||
08634e7b 1787 avctx->request_channels > num_core_channels + !!s->lfe)) {
63e8d976 1788 avctx->channel_layout |= AV_CH_BACK_CENTER;
774e9acf 1789 if (s->lfe) {
63e8d976 1790 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
774e9acf
NB
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 {
08634e7b
NB
1796 channels = num_core_channels + !!s->lfe;
1797 s->xch_present = 0; /* disable further xch processing */
774e9acf 1798 if (s->lfe) {
63e8d976 1799 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
774e9acf
NB
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 }
92765276 1804
08634e7b
NB
1805 if (channels > !!s->lfe &&
1806 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
f44059d2 1807 return AVERROR_INVALIDDATA;
4e06acbd 1808
d1177cb5 1809 if (avctx->request_channels == 2 && s->prim_channels > 2) {
92765276
BL
1810 channels = 2;
1811 s->output = DCA_STEREO;
63e8d976 1812 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
92765276
BL
1813 }
1814 } else {
1815 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
f44059d2 1816 return AVERROR_INVALIDDATA;
92765276
BL
1817 }
1818
cc826626 1819
4a24837e 1820 /* There is nothing that prevents a dts frame to change channel configuration
43fb279f 1821 but Libav doesn't support that so only set the channels if it is previously
4a24837e
BL
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.*/
1360f07e
DK
1825 if (s->is_channels_set == 0) {
1826 s->is_channels_set = 1;
d425a03b 1827 avctx->channels = channels;
1360f07e
DK
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");
f44059d2 1832 return AVERROR_PATCHWELCOME;
1360f07e 1833 }
4a24837e 1834
9aa8193a 1835 out_size = 256 / 8 * s->sample_blocks * channels *
e6c52cee 1836 av_get_bytes_per_sample(avctx->sample_fmt);
9aa8193a 1837 if (*data_size < out_size)
f44059d2 1838 return AVERROR(EINVAL);
9aa8193a 1839 *data_size = out_size;
6baef06e
NB
1840
1841 /* filter to get final output */
01ca9ac3 1842 for (i = 0; i < (s->sample_blocks / 8); i++) {
6baef06e 1843 dca_filter_channels(s, i);
b12b16c5
NB
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;
a92a1b93
MR
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);
b12b16c5
NB
1853 }
1854
9aa8193a
JR
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 }
01ca9ac3
KS
1865 }
1866
6baef06e
NB
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
01ca9ac3
KS
1873 return buf_size;
1874}
1875
1876
1877
1878/**
01ca9ac3
KS
1879 * DCA initialization
1880 *
1881 * @param avctx pointer to the AVCodecContext
1882 */
1883
98a6fff9 1884static av_cold int dca_decode_init(AVCodecContext * avctx)
01ca9ac3
KS
1885{
1886 DCAContext *s = avctx->priv_data;
8ed96a5e 1887 int i;
01ca9ac3
KS
1888
1889 s->avctx = avctx;
1890 dca_init_vlcs();
01ca9ac3
KS
1891
1892 dsputil_init(&s->dsp, avctx);
7d485f16 1893 ff_mdct_init(&s->imdct, 6, 1, 1.0);
f462ed1f 1894 ff_synth_filter_init(&s->synth);
309d16a4 1895 ff_dcadsp_init(&s->dcadsp);
c73d99e6 1896 ff_fmt_convert_init(&s->fmt_conv, avctx);
cfec09e9 1897
6baef06e 1898 for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++)
8ed96a5e 1899 s->samples_chanptr[i] = s->samples + i * 256;
1402ee72 1900
9aa8193a
JR
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 }
1402ee72 1908
b5ec6383
JR
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 }
1402ee72 1914
01ca9ac3
KS
1915 return 0;
1916}
1917
89df5e95
MN
1918static 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}
01ca9ac3 1924
f4096bf6
AH
1925static 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
d36beb3f 1934AVCodec ff_dca_decoder = {
01ca9ac3 1935 .name = "dca",
72415b2a 1936 .type = AVMEDIA_TYPE_AUDIO,
01ca9ac3
KS
1937 .id = CODEC_ID_DTS,
1938 .priv_data_size = sizeof(DCAContext),
1939 .init = dca_decode_init,
1940 .decode = dca_decode_frame,
89df5e95 1941 .close = dca_decode_end,
fe4bf374 1942 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
62784e37 1943 .capabilities = CODEC_CAP_CHANNEL_CONF,
9aa8193a
JR
1944 .sample_fmts = (const enum AVSampleFormat[]) {
1945 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
1946 },
f4096bf6 1947 .profiles = NULL_IF_CONFIG_SMALL(profiles),
01ca9ac3 1948};