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