Verify FSIZE96 when encountering X96 sync code.
[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 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
01ca9ac3
KS
25#include <math.h>
26#include <stddef.h>
27#include <stdio.h>
28
843c7aa8 29#include "libavutil/intmath.h"
0dc7df28 30#include "libavutil/intreadwrite.h"
63e8d976 31#include "libavcore/audioconvert.h"
01ca9ac3
KS
32#include "avcodec.h"
33#include "dsputil.h"
1429224b 34#include "fft.h"
9106a698 35#include "get_bits.h"
b2755007 36#include "put_bits.h"
01ca9ac3
KS
37#include "dcadata.h"
38#include "dcahuff.h"
98c98e04 39#include "dca.h"
4f99c31c 40#include "synth_filter.h"
309d16a4 41#include "dcadsp.h"
01ca9ac3
KS
42
43//#define TRACE
44
774e9acf 45#define DCA_PRIM_CHANNELS_MAX (7)
01ca9ac3
KS
46#define DCA_SUBBANDS (32)
47#define DCA_ABITS_MAX (32) /* Should be 28 */
63c3b716 48#define DCA_SUBSUBFRAMES_MAX (4)
774e9acf 49#define DCA_SUBFRAMES_MAX (16)
77b4b7c3 50#define DCA_BLOCKS_MAX (16)
01ca9ac3
KS
51#define DCA_LFE_MAX (3)
52
53enum DCAMode {
54 DCA_MONO = 0,
55 DCA_CHANNEL,
56 DCA_STEREO,
57 DCA_STEREO_SUMDIFF,
58 DCA_STEREO_TOTAL,
59 DCA_3F,
60 DCA_2F1R,
61 DCA_3F1R,
62 DCA_2F2R,
63 DCA_3F2R,
64 DCA_4F2R
65};
66
87c3b9bc
BL
67/* Tables for mapping dts channel configurations to libavcodec multichannel api.
68 * Some compromises have been made for special configurations. Most configurations
69 * are never used so complete accuracy is not needed.
70 *
71 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
e22192ec 72 * S -> side, when both rear and back are configured move one of them to the side channel
87c3b9bc 73 * OV -> center back
9d9b9d32 74 * All 2 channel configurations -> CH_LAYOUT_STEREO
87c3b9bc
BL
75 */
76
77static const int64_t dca_core_channel_layout[] = {
63e8d976
SS
78 AV_CH_FRONT_CENTER, ///< 1, A
79 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
80 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
81 AV_CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference)
82 AV_CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total)
83 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER, ///< 3, C+L+R
84 AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER, ///< 3, L+R+S
85 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|CH_BACK_CENTER, ///< 4, C + L + R+ S
86 AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR
87 AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
88 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
89 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
90 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
91 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
92 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
93 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
94};
95
92765276
BL
96static const int8_t dca_lfe_index[] = {
97 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
98};
99
3d5a9ba1
NB
100static const int8_t dca_channel_reorder_lfe[][9] = {
101 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
102 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
103 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
104 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
105 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
106 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
107 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
108 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
109 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
110 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
111 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
112 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
113 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
114 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
115 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
116 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
92765276
BL
117};
118
3d5a9ba1
NB
119static const int8_t dca_channel_reorder_lfe_xch[][9] = {
120 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
121 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
122 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
123 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
124 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
125 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
126 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
127 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
128 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
129 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
130 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
131 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
132 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
133 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
134 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
135 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
92765276
BL
136};
137
3d5a9ba1
NB
138static const int8_t dca_channel_reorder_nolfe[][9] = {
139 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
140 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
141 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
142 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
143 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
144 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
145 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
146 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
147 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
148 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
149 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
150 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
151 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
152 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
153 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
154 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
155};
156
157static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
158 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
159 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
160 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
161 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
162 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
163 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
164 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
165 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
166 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
167 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
168 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
169 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
170 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
171 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
172 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
173 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
174};
87c3b9bc 175
01ca9ac3
KS
176#define DCA_DOLBY 101 /* FIXME */
177
178#define DCA_CHANNEL_BITS 6
179#define DCA_CHANNEL_MASK 0x3F
180
181#define DCA_LFE 0x80
182
183#define HEADER_SIZE 14
01ca9ac3 184
4df7beb3 185#define DCA_MAX_FRAME_SIZE 16384
01ca9ac3
KS
186
187/** Bit allocation */
188typedef struct {
189 int offset; ///< code values offset
190 int maxbits[8]; ///< max bits in VLC
191 int wrap; ///< wrap for get_vlc2()
192 VLC vlc[8]; ///< actual codes
193} BitAlloc;
194
195static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
196static BitAlloc dca_tmode; ///< transition mode VLCs
197static BitAlloc dca_scalefactor; ///< scalefactor VLCs
198static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
199
5a6a6cc7 200static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
01ca9ac3
KS
201{
202 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
203}
204
205typedef struct {
206 AVCodecContext *avctx;
207 /* Frame header */
208 int frame_type; ///< type of the current frame
209 int samples_deficit; ///< deficit sample count
210 int crc_present; ///< crc is present in the bitstream
211 int sample_blocks; ///< number of PCM sample blocks
212 int frame_size; ///< primary frame byte size
213 int amode; ///< audio channels arrangement
214 int sample_rate; ///< audio sampling rate
215 int bit_rate; ///< transmission bit rate
9ed73b48 216 int bit_rate_index; ///< transmission bit rate index
01ca9ac3
KS
217
218 int downmix; ///< embedded downmix enabled
219 int dynrange; ///< embedded dynamic range flag
220 int timestamp; ///< embedded time stamp flag
221 int aux_data; ///< auxiliary data flag
222 int hdcd; ///< source material is mastered in HDCD
223 int ext_descr; ///< extension audio descriptor flag
224 int ext_coding; ///< extended coding flag
225 int aspf; ///< audio sync word insertion flag
226 int lfe; ///< low frequency effects flag
227 int predictor_history; ///< predictor history flag
228 int header_crc; ///< header crc check bytes
229 int multirate_inter; ///< multirate interpolator switch
230 int version; ///< encoder software revision
231 int copy_history; ///< copy history
232 int source_pcm_res; ///< source pcm resolution
233 int front_sum; ///< front sum/difference flag
234 int surround_sum; ///< surround sum/difference flag
235 int dialog_norm; ///< dialog normalisation parameter
236
237 /* Primary audio coding header */
238 int subframes; ///< number of subframes
ebf71dbd 239 int total_channels; ///< number of channels including extensions
01ca9ac3
KS
240 int prim_channels; ///< number of primary audio channels
241 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
242 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
243 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
244 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
245 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
246 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
247 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
248 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
249
250 /* Primary audio coding side information */
774e9acf
NB
251 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
252 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
01ca9ac3
KS
253 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
254 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
255 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
256 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
257 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
258 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
259 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
260 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
261 int dynrange_coef; ///< dynamic range coefficient
262
263 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
264
6baef06e 265 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
01ca9ac3
KS
266 int lfe_scale_factor;
267
268 /* Subband samples history (for ADPCM) */
269 float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
84dc2d8a 270 DECLARE_ALIGNED(16, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
38d52f3e 271 DECLARE_ALIGNED(16, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
47f0e052 272 int hist_index[DCA_PRIM_CHANNELS_MAX];
84dc2d8a 273 DECLARE_ALIGNED(16, float, raXin)[32];
01ca9ac3
KS
274
275 int output; ///< type of output
1402ee72
BL
276 float add_bias; ///< output bias
277 float scale_bias; ///< output scale
01ca9ac3 278
77b4b7c3 279 DECLARE_ALIGNED(16, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
774e9acf
NB
280 DECLARE_ALIGNED(16, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256];
281 const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1];
01ca9ac3
KS
282
283 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
284 int dca_buffer_size; ///< how much data is in the dca_buffer
285
92765276 286 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe
01ca9ac3
KS
287 GetBitContext gb;
288 /* Current position in DCA frame */
289 int current_subframe;
290 int current_subsubframe;
291
d0a18850
NB
292 /* XCh extension information */
293 int xch_present;
294 int xch_base_channel; ///< index of first (only) channel containing XCH data
295
01ca9ac3
KS
296 int debug_flag; ///< used for suppressing repeated error messages output
297 DSPContext dsp;
01b22147 298 FFTContext imdct;
f462ed1f 299 SynthFilterContext synth;
309d16a4 300 DCADSPContext dcadsp;
01ca9ac3
KS
301} DCAContext;
302
0cfa85dd
KS
303static const uint16_t dca_vlc_offs[] = {
304 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
305 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
306 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
307 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
308 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
309 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
310};
311
98a6fff9 312static av_cold void dca_init_vlcs(void)
01ca9ac3 313{
5e534865 314 static int vlcs_initialized = 0;
0cfa85dd
KS
315 int i, j, c = 14;
316 static VLC_TYPE dca_table[23622][2];
01ca9ac3 317
5e534865 318 if (vlcs_initialized)
01ca9ac3
KS
319 return;
320
321 dca_bitalloc_index.offset = 1;
32dd6a9c 322 dca_bitalloc_index.wrap = 2;
0cfa85dd
KS
323 for (i = 0; i < 5; i++) {
324 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
325 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
01ca9ac3
KS
326 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
327 bitalloc_12_bits[i], 1, 1,
0cfa85dd
KS
328 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
329 }
01ca9ac3
KS
330 dca_scalefactor.offset = -64;
331 dca_scalefactor.wrap = 2;
0cfa85dd
KS
332 for (i = 0; i < 5; i++) {
333 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
334 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
01ca9ac3
KS
335 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
336 scales_bits[i], 1, 1,
0cfa85dd
KS
337 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
338 }
01ca9ac3
KS
339 dca_tmode.offset = 0;
340 dca_tmode.wrap = 1;
0cfa85dd
KS
341 for (i = 0; i < 4; i++) {
342 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
343 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
01ca9ac3
KS
344 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
345 tmode_bits[i], 1, 1,
0cfa85dd
KS
346 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
347 }
01ca9ac3 348
d1177cb5
NB
349 for (i = 0; i < 10; i++)
350 for (j = 0; j < 7; j++){
351 if (!bitalloc_codes[i][j]) break;
01ca9ac3
KS
352 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
353 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
0cfa85dd
KS
354 dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
355 dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
01ca9ac3
KS
356 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
357 bitalloc_sizes[i],
358 bitalloc_bits[i][j], 1, 1,
0cfa85dd
KS
359 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
360 c++;
01ca9ac3 361 }
5e534865 362 vlcs_initialized = 1;
01ca9ac3
KS
363}
364
365static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
366{
367 while(len--)
368 *dst++ = get_bits(gb, bits);
369}
370
774e9acf 371static int dca_parse_audio_coding_header(DCAContext * s, int base_channel)
01ca9ac3
KS
372{
373 int i, j;
374 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
375 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
376 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
377
774e9acf 378 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
6baef06e 379 s->prim_channels = s->total_channels;
774e9acf 380
6baef06e 381 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
774e9acf 382 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
6baef06e
NB
383
384
774e9acf 385 for (i = base_channel; i < s->prim_channels; i++) {
6baef06e
NB
386 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
387 if (s->subband_activity[i] > DCA_SUBBANDS)
388 s->subband_activity[i] = DCA_SUBBANDS;
389 }
774e9acf 390 for (i = base_channel; i < s->prim_channels; i++) {
6baef06e
NB
391 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
392 if (s->vq_start_subband[i] > DCA_SUBBANDS)
393 s->vq_start_subband[i] = DCA_SUBBANDS;
394 }
774e9acf
NB
395 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
396 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
397 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
398 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
6baef06e
NB
399
400 /* Get codebooks quantization indexes */
774e9acf
NB
401 if (!base_channel)
402 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
6baef06e 403 for (j = 1; j < 11; j++)
774e9acf 404 for (i = base_channel; i < s->prim_channels; i++)
6baef06e
NB
405 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
406
407 /* Get scale factor adjustment */
408 for (j = 0; j < 11; j++)
774e9acf 409 for (i = base_channel; i < s->prim_channels; i++)
6baef06e
NB
410 s->scalefactor_adj[i][j] = 1;
411
412 for (j = 1; j < 11; j++)
774e9acf 413 for (i = base_channel; i < s->prim_channels; i++)
6baef06e
NB
414 if (s->quant_index_huffman[i][j] < thr[j])
415 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
416
417 if (s->crc_present) {
418 /* Audio header CRC check */
419 get_bits(&s->gb, 16);
420 }
421
422 s->current_subframe = 0;
423 s->current_subsubframe = 0;
424
425#ifdef TRACE
426 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
427 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
774e9acf 428 for (i = base_channel; i < s->prim_channels; i++){
6baef06e
NB
429 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
430 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
431 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
432 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
433 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
434 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
435 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
436 for (j = 0; j < 11; j++)
437 av_log(s->avctx, AV_LOG_DEBUG, " %i",
438 s->quant_index_huffman[i][j]);
439 av_log(s->avctx, AV_LOG_DEBUG, "\n");
440 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
441 for (j = 0; j < 11; j++)
442 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
443 av_log(s->avctx, AV_LOG_DEBUG, "\n");
444 }
445#endif
446
447 return 0;
448}
449
450static int dca_parse_frame_header(DCAContext * s)
451{
01ca9ac3
KS
452 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
453
454 /* Sync code */
455 get_bits(&s->gb, 32);
456
457 /* Frame header */
458 s->frame_type = get_bits(&s->gb, 1);
459 s->samples_deficit = get_bits(&s->gb, 5) + 1;
460 s->crc_present = get_bits(&s->gb, 1);
461 s->sample_blocks = get_bits(&s->gb, 7) + 1;
462 s->frame_size = get_bits(&s->gb, 14) + 1;
463 if (s->frame_size < 95)
464 return -1;
465 s->amode = get_bits(&s->gb, 6);
466 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
467 if (!s->sample_rate)
468 return -1;
d5b3a863 469 s->bit_rate_index = get_bits(&s->gb, 5);
9ed73b48 470 s->bit_rate = dca_bit_rates[s->bit_rate_index];
01ca9ac3
KS
471 if (!s->bit_rate)
472 return -1;
473
474 s->downmix = get_bits(&s->gb, 1);
475 s->dynrange = get_bits(&s->gb, 1);
476 s->timestamp = get_bits(&s->gb, 1);
477 s->aux_data = get_bits(&s->gb, 1);
478 s->hdcd = get_bits(&s->gb, 1);
479 s->ext_descr = get_bits(&s->gb, 3);
480 s->ext_coding = get_bits(&s->gb, 1);
481 s->aspf = get_bits(&s->gb, 1);
482 s->lfe = get_bits(&s->gb, 2);
483 s->predictor_history = get_bits(&s->gb, 1);
484
485 /* TODO: check CRC */
486 if (s->crc_present)
487 s->header_crc = get_bits(&s->gb, 16);
488
489 s->multirate_inter = get_bits(&s->gb, 1);
490 s->version = get_bits(&s->gb, 4);
491 s->copy_history = get_bits(&s->gb, 2);
492 s->source_pcm_res = get_bits(&s->gb, 3);
493 s->front_sum = get_bits(&s->gb, 1);
494 s->surround_sum = get_bits(&s->gb, 1);
495 s->dialog_norm = get_bits(&s->gb, 4);
496
497 /* FIXME: channels mixing levels */
cc826626 498 s->output = s->amode;
d1177cb5 499 if (s->lfe) s->output |= DCA_LFE;
01ca9ac3
KS
500
501#ifdef TRACE
502 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
503 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
504 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
505 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
506 s->sample_blocks, s->sample_blocks * 32);
507 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
508 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
509 s->amode, dca_channels[s->amode]);
49c91c34
BL
510 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
511 s->sample_rate);
512 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
513 s->bit_rate);
01ca9ac3
KS
514 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
515 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
516 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
517 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
518 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
519 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
520 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
521 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
522 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
523 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
524 s->predictor_history);
525 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
526 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
527 s->multirate_inter);
528 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
529 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
530 av_log(s->avctx, AV_LOG_DEBUG,
531 "source pcm resolution: %i (%i bits/sample)\n",
532 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
533 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
534 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
535 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
536 av_log(s->avctx, AV_LOG_DEBUG, "\n");
537#endif
538
539 /* Primary audio coding header */
540 s->subframes = get_bits(&s->gb, 4) + 1;
01ca9ac3 541
774e9acf 542 return dca_parse_audio_coding_header(s, 0);
01ca9ac3
KS
543}
544
545
c5d13492 546static inline int get_scale(GetBitContext *gb, int level, int value)
01ca9ac3
KS
547{
548 if (level < 5) {
549 /* huffman encoded */
c5d13492 550 value += get_bitalloc(gb, &dca_scalefactor, level);
d1177cb5 551 } else if (level < 8)
01ca9ac3
KS
552 value = get_bits(gb, level + 1);
553 return value;
554}
555
774e9acf 556static int dca_subframe_header(DCAContext * s, int base_channel, int block_index)
01ca9ac3
KS
557{
558 /* Primary audio coding side information */
559 int j, k;
560
774e9acf
NB
561 if (!base_channel) {
562 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
563 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
564 }
565
566 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
567 for (k = 0; k < s->subband_activity[j]; k++)
568 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
569 }
570
571 /* Get prediction codebook */
774e9acf 572 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
573 for (k = 0; k < s->subband_activity[j]; k++) {
574 if (s->prediction_mode[j][k] > 0) {
575 /* (Prediction coefficient VQ address) */
576 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
577 }
578 }
579 }
580
581 /* Bit allocation index */
774e9acf 582 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
583 for (k = 0; k < s->vq_start_subband[j]; k++) {
584 if (s->bitalloc_huffman[j] == 6)
585 s->bitalloc[j][k] = get_bits(&s->gb, 5);
586 else if (s->bitalloc_huffman[j] == 5)
587 s->bitalloc[j][k] = get_bits(&s->gb, 4);
ebf71dbd
BL
588 else if (s->bitalloc_huffman[j] == 7) {
589 av_log(s->avctx, AV_LOG_ERROR,
590 "Invalid bit allocation index\n");
591 return -1;
592 } else {
01ca9ac3 593 s->bitalloc[j][k] =
c5d13492 594 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
01ca9ac3
KS
595 }
596
597 if (s->bitalloc[j][k] > 26) {
598// av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
599// j, k, s->bitalloc[j][k]);
600 return -1;
601 }
602 }
603 }
604
605 /* Transition mode */
774e9acf 606 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
607 for (k = 0; k < s->subband_activity[j]; k++) {
608 s->transition_mode[j][k] = 0;
774e9acf 609 if (s->subsubframes[s->current_subframe] > 1 &&
01ca9ac3
KS
610 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
611 s->transition_mode[j][k] =
612 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
613 }
614 }
615 }
616
774e9acf 617 for (j = base_channel; j < s->prim_channels; j++) {
a9f87158 618 const uint32_t *scale_table;
01ca9ac3
KS
619 int scale_sum;
620
621 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
622
623 if (s->scalefactor_huffman[j] == 6)
a9f87158 624 scale_table = scale_factor_quant7;
01ca9ac3 625 else
a9f87158 626 scale_table = scale_factor_quant6;
01ca9ac3
KS
627
628 /* When huffman coded, only the difference is encoded */
629 scale_sum = 0;
630
631 for (k = 0; k < s->subband_activity[j]; k++) {
632 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
c5d13492 633 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
01ca9ac3
KS
634 s->scale_factor[j][k][0] = scale_table[scale_sum];
635 }
636
637 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
638 /* Get second scale factor */
c5d13492 639 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
01ca9ac3
KS
640 s->scale_factor[j][k][1] = scale_table[scale_sum];
641 }
642 }
643 }
644
645 /* Joint subband scale factor codebook select */
774e9acf 646 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
647 /* Transmitted only if joint subband coding enabled */
648 if (s->joint_intensity[j] > 0)
649 s->joint_huff[j] = get_bits(&s->gb, 3);
650 }
651
652 /* Scale factors for joint subband coding */
774e9acf 653 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
654 int source_channel;
655
656 /* Transmitted only if joint subband coding enabled */
657 if (s->joint_intensity[j] > 0) {
658 int scale = 0;
659 source_channel = s->joint_intensity[j] - 1;
660
661 /* When huffman coded, only the difference is encoded
662 * (is this valid as well for joint scales ???) */
663
664 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
c5d13492 665 scale = get_scale(&s->gb, s->joint_huff[j], 0);
01ca9ac3
KS
666 scale += 64; /* bias */
667 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
668 }
669
268fcbe2 670 if (!(s->debug_flag & 0x02)) {
01ca9ac3
KS
671 av_log(s->avctx, AV_LOG_DEBUG,
672 "Joint stereo coding not supported\n");
673 s->debug_flag |= 0x02;
674 }
675 }
676 }
677
678 /* Stereo downmix coefficients */
774e9acf 679 if (!base_channel && s->prim_channels > 2) {
d1177cb5 680 if (s->downmix) {
774e9acf 681 for (j = base_channel; j < s->prim_channels; j++) {
6369e6eb
JR
682 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
683 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
684 }
c31a76e4
JR
685 } else {
686 int am = s->amode & DCA_CHANNEL_MASK;
774e9acf 687 for (j = base_channel; j < s->prim_channels; j++) {
c31a76e4
JR
688 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
689 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
690 }
691 }
01ca9ac3
KS
692 }
693
694 /* Dynamic range coefficient */
ace7f813 695 if (!base_channel && s->dynrange)
01ca9ac3
KS
696 s->dynrange_coef = get_bits(&s->gb, 8);
697
698 /* Side information CRC check word */
699 if (s->crc_present) {
700 get_bits(&s->gb, 16);
701 }
702
703 /*
704 * Primary audio data arrays
705 */
706
707 /* VQ encoded high frequency subbands */
774e9acf 708 for (j = base_channel; j < s->prim_channels; j++)
01ca9ac3
KS
709 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
710 /* 1 vector -> 32 samples */
711 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
712
713 /* Low frequency effect data */
774e9acf 714 if (!base_channel && s->lfe) {
01ca9ac3 715 /* LFE samples */
6baef06e 716 int lfe_samples = 2 * s->lfe * (4 + block_index);
774e9acf 717 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
01ca9ac3
KS
718 float lfe_scale;
719
6baef06e 720 for (j = lfe_samples; j < lfe_end_sample; j++) {
01ca9ac3
KS
721 /* Signed 8 bits int */
722 s->lfe_data[j] = get_sbits(&s->gb, 8);
723 }
724
725 /* Scale factor index */
726 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
727
728 /* Quantization step size * scale factor */
729 lfe_scale = 0.035 * s->lfe_scale_factor;
730
6baef06e 731 for (j = lfe_samples; j < lfe_end_sample; j++)
01ca9ac3
KS
732 s->lfe_data[j] *= lfe_scale;
733 }
734
735#ifdef TRACE
774e9acf 736 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
01ca9ac3 737 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
774e9acf
NB
738 s->partial_samples[s->current_subframe]);
739 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
740 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
741 for (k = 0; k < s->subband_activity[j]; k++)
742 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
743 av_log(s->avctx, AV_LOG_DEBUG, "\n");
744 }
774e9acf 745 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
746 for (k = 0; k < s->subband_activity[j]; k++)
747 av_log(s->avctx, AV_LOG_DEBUG,
748 "prediction coefs: %f, %f, %f, %f\n",
749 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
750 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
751 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
752 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
753 }
774e9acf 754 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
755 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
756 for (k = 0; k < s->vq_start_subband[j]; k++)
757 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
758 av_log(s->avctx, AV_LOG_DEBUG, "\n");
759 }
774e9acf 760 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
761 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
762 for (k = 0; k < s->subband_activity[j]; k++)
763 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
764 av_log(s->avctx, AV_LOG_DEBUG, "\n");
765 }
774e9acf 766 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3
KS
767 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
768 for (k = 0; k < s->subband_activity[j]; k++) {
769 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
770 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
771 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
772 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
773 }
774 av_log(s->avctx, AV_LOG_DEBUG, "\n");
775 }
774e9acf 776 for (j = base_channel; j < s->prim_channels; j++) {
01ca9ac3 777 if (s->joint_intensity[j] > 0) {
56e4603e 778 int source_channel = s->joint_intensity[j] - 1;
01ca9ac3
KS
779 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
780 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
781 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
782 av_log(s->avctx, AV_LOG_DEBUG, "\n");
783 }
784 }
774e9acf 785 if (!base_channel && s->prim_channels > 2 && s->downmix) {
01ca9ac3
KS
786 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
787 for (j = 0; j < s->prim_channels; j++) {
788 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
789 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
790 }
791 av_log(s->avctx, AV_LOG_DEBUG, "\n");
792 }
774e9acf 793 for (j = base_channel; j < s->prim_channels; j++)
01ca9ac3
KS
794 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
795 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
774e9acf 796 if (!base_channel && s->lfe) {
6baef06e
NB
797 int lfe_samples = 2 * s->lfe * (4 + block_index);
798 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
799
01ca9ac3 800 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
6baef06e 801 for (j = lfe_samples; j < lfe_end_sample; j++)
01ca9ac3
KS
802 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
803 av_log(s->avctx, AV_LOG_DEBUG, "\n");
804 }
805#endif
806
807 return 0;
808}
809
810static void qmf_32_subbands(DCAContext * s, int chans,
811 float samples_in[32][8], float *samples_out,
812 float scale, float bias)
813{
b6398969 814 const float *prCoeff;
d49dded5 815 int i;
01ca9ac3 816
b92d483b 817 int sb_act = s->subband_activity[chans];
89df5e95 818 int subindex;
01ca9ac3 819
89df5e95 820 scale *= sqrt(1/8.0);
01ca9ac3
KS
821
822 /* Select filter */
823 if (!s->multirate_inter) /* Non-perfect reconstruction */
b6398969 824 prCoeff = fir_32bands_nonperfect;
01ca9ac3 825 else /* Perfect reconstruction */
b6398969 826 prCoeff = fir_32bands_perfect;
01ca9ac3
KS
827
828 /* Reconstructed channel sample index */
829 for (subindex = 0; subindex < 8; subindex++) {
01ca9ac3 830 /* Load in one sample from each subband and clear inactive subbands */
b92d483b 831 for (i = 0; i < sb_act; i++){
0dc7df28
MR
832 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ ((i-1)&2)<<30;
833 AV_WN32A(&s->raXin[i], v);
89df5e95 834 }
01ca9ac3 835 for (; i < 32; i++)
6d9d289e 836 s->raXin[i] = 0.0;
01ca9ac3 837
f462ed1f 838 s->synth.synth_filter_float(&s->imdct,
4f99c31c
MN
839 s->subband_fir_hist[chans], &s->hist_index[chans],
840 s->subband_fir_noidea[chans], prCoeff,
841 samples_out, s->raXin, scale, bias);
89df5e95 842 samples_out+= 32;
01ca9ac3 843
01ca9ac3
KS
844 }
845}
846
309d16a4 847static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
01ca9ac3
KS
848 int num_deci_sample, float *samples_in,
849 float *samples_out, float scale,
850 float bias)
851{
852 /* samples_in: An array holding decimated samples.
853 * Samples in current subframe starts from samples_in[0],
854 * while samples_in[-1], samples_in[-2], ..., stores samples
855 * from last subframe as history.
856 *
857 * samples_out: An array holding interpolated samples
858 */
859
309d16a4 860 int decifactor;
01ca9ac3 861 const float *prCoeff;
01ca9ac3
KS
862 int deciindex;
863
864 /* Select decimation filter */
865 if (decimation_select == 1) {
766fefe8 866 decifactor = 64;
01ca9ac3
KS
867 prCoeff = lfe_fir_128;
868 } else {
766fefe8 869 decifactor = 32;
01ca9ac3
KS
870 prCoeff = lfe_fir_64;
871 }
872 /* Interpolation */
873 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
309d16a4
MR
874 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
875 scale, bias);
766fefe8 876 samples_in++;
309d16a4 877 samples_out += 2 * decifactor;
01ca9ac3
KS
878 }
879}
880
881/* downmixing routines */
c31a76e4 882#define MIX_REAR1(samples, si1, rs, coef) \
df984493
NB
883 samples[i] += (samples[si1] - add_bias) * coef[rs][0]; \
884 samples[i+256] += (samples[si1] - add_bias) * coef[rs][1];
01ca9ac3 885
c31a76e4 886#define MIX_REAR2(samples, si1, si2, rs, coef) \
df984493
NB
887 samples[i] += (samples[si1] - add_bias) * coef[rs][0] + (samples[si2] - add_bias) * coef[rs+1][0]; \
888 samples[i+256] += (samples[si1] - add_bias) * coef[rs][1] + (samples[si2] - add_bias) * coef[rs+1][1];
01ca9ac3 889
c31a76e4 890#define MIX_FRONT3(samples, coef) \
df984493
NB
891 t = samples[i+c] - add_bias; \
892 u = samples[i+l] - add_bias; \
893 v = samples[i+r] - add_bias; \
894 samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0] + add_bias; \
895 samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1] + add_bias;
01ca9ac3
KS
896
897#define DOWNMIX_TO_STEREO(op1, op2) \
d1177cb5 898 for (i = 0; i < 256; i++){ \
01ca9ac3
KS
899 op1 \
900 op2 \
901 }
902
c31a76e4 903static void dca_downmix(float *samples, int srcfmt,
df984493
NB
904 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
905 const int8_t *channel_mapping, float add_bias)
01ca9ac3 906{
df984493 907 int c,l,r,sl,sr,s;
01ca9ac3 908 int i;
df984493 909 float t, u, v;
c31a76e4
JR
910 float coef[DCA_PRIM_CHANNELS_MAX][2];
911
d1177cb5 912 for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
c31a76e4
JR
913 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
914 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
915 }
01ca9ac3
KS
916
917 switch (srcfmt) {
918 case DCA_MONO:
919 case DCA_CHANNEL:
920 case DCA_STEREO_TOTAL:
921 case DCA_STEREO_SUMDIFF:
922 case DCA_4F2R:
923 av_log(NULL, 0, "Not implemented!\n");
924 break;
925 case DCA_STEREO:
926 break;
927 case DCA_3F:
df984493
NB
928 c = channel_mapping[0] * 256;
929 l = channel_mapping[1] * 256;
930 r = channel_mapping[2] * 256;
c31a76e4 931 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
01ca9ac3
KS
932 break;
933 case DCA_2F1R:
df984493
NB
934 s = channel_mapping[2] * 256;
935 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef),);
01ca9ac3
KS
936 break;
937 case DCA_3F1R:
df984493
NB
938 c = channel_mapping[0] * 256;
939 l = channel_mapping[1] * 256;
940 r = channel_mapping[2] * 256;
941 s = channel_mapping[3] * 256;
c31a76e4 942 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
df984493 943 MIX_REAR1(samples, i + s, 3, coef));
01ca9ac3
KS
944 break;
945 case DCA_2F2R:
df984493
NB
946 sl = channel_mapping[2] * 256;
947 sr = channel_mapping[3] * 256;
948 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef),);
01ca9ac3
KS
949 break;
950 case DCA_3F2R:
df984493
NB
951 c = channel_mapping[0] * 256;
952 l = channel_mapping[1] * 256;
953 r = channel_mapping[2] * 256;
954 sl = channel_mapping[3] * 256;
955 sr = channel_mapping[4] * 256;
c31a76e4 956 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
df984493 957 MIX_REAR2(samples, i + sl, i + sr, 3, coef));
01ca9ac3
KS
958 break;
959 }
960}
961
962
963/* Very compact version of the block code decoder that does not use table
964 * look-up but is slightly slower */
965static int decode_blockcode(int code, int levels, int *values)
966{
967 int i;
968 int offset = (levels - 1) >> 1;
969
970 for (i = 0; i < 4; i++) {
843c7aa8
MR
971 int div = FASTDIV(code, levels);
972 values[i] = code - offset - div*levels;
973 code = div;
01ca9ac3
KS
974 }
975
976 if (code == 0)
977 return 0;
978 else {
979 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
980 return -1;
981 }
982}
983
984static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
985static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
986
774e9acf 987static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
01ca9ac3
KS
988{
989 int k, l;
990 int subsubframe = s->current_subsubframe;
991
a9f87158 992 const float *quant_step_table;
01ca9ac3
KS
993
994 /* FIXME */
77b4b7c3 995 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
69e17136 996 LOCAL_ALIGNED_16(int, block, [8]);
01ca9ac3
KS
997
998 /*
999 * Audio data
1000 */
1001
1002 /* Select quantization step size table */
9ed73b48 1003 if (s->bit_rate_index == 0x1f)
a9f87158 1004 quant_step_table = lossless_quant_d;
01ca9ac3 1005 else
a9f87158 1006 quant_step_table = lossy_quant_d;
01ca9ac3 1007
774e9acf 1008 for (k = base_channel; k < s->prim_channels; k++) {
01ca9ac3
KS
1009 for (l = 0; l < s->vq_start_subband[k]; l++) {
1010 int m;
1011
1012 /* Select the mid-tread linear quantizer */
1013 int abits = s->bitalloc[k][l];
1014
1015 float quant_step_size = quant_step_table[abits];
01ca9ac3
KS
1016
1017 /*
1018 * Determine quantization index code book and its type
1019 */
1020
1021 /* Select quantization index code book */
1022 int sel = s->quant_index_huffman[k][abits];
1023
1024 /*
1025 * Extract bits from the bit stream
1026 */
d1177cb5 1027 if (!abits){
01ca9ac3 1028 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
69e17136
MR
1029 } else {
1030 /* Deal with transients */
1031 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1032 float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
1033
d1177cb5
NB
1034 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
1035 if (abits <= 7){
2bb29da6
MR
1036 /* Block code */
1037 int block_code1, block_code2, size, levels;
1038
1039 size = abits_sizes[abits-1];
1040 levels = abits_levels[abits-1];
1041
1042 block_code1 = get_bits(&s->gb, size);
1043 /* FIXME Should test return value */
1044 decode_blockcode(block_code1, levels, block);
1045 block_code2 = get_bits(&s->gb, size);
1046 decode_blockcode(block_code2, levels, &block[4]);
1047 }else{
1048 /* no coding */
1049 for (m = 0; m < 8; m++)
1050 block[m] = get_sbits(&s->gb, abits - 3);
1051 }
01ca9ac3 1052 }else{
2bb29da6 1053 /* Huffman coded */
01ca9ac3 1054 for (m = 0; m < 8; m++)
2bb29da6 1055 block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
01ca9ac3 1056 }
01ca9ac3 1057
69e17136
MR
1058 s->dsp.int32_to_float_fmul_scalar(subband_samples[k][l],
1059 block, rscale, 8);
1060 }
01ca9ac3
KS
1061
1062 /*
1063 * Inverse ADPCM if in prediction mode
1064 */
1065 if (s->prediction_mode[k][l]) {
1066 int n;
1067 for (m = 0; m < 8; m++) {
1068 for (n = 1; n <= 4; n++)
1069 if (m >= n)
1070 subband_samples[k][l][m] +=
1071 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1072 subband_samples[k][l][m - n] / 8192);
1073 else if (s->predictor_history)
1074 subband_samples[k][l][m] +=
1075 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1076 s->subband_samples_hist[k][l][m - n +
1077 4] / 8192);
1078 }
1079 }
1080 }
1081
1082 /*
1083 * Decode VQ encoded high frequencies
1084 */
1085 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1086 /* 1 vector -> 32 samples but we only need the 8 samples
1087 * for this subsubframe. */
1088 int m;
1089
1090 if (!s->debug_flag & 0x01) {
1091 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
1092 s->debug_flag |= 0x01;
1093 }
1094
1095 for (m = 0; m < 8; m++) {
1096 subband_samples[k][l][m] =
1097 high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
1098 m]
1099 * (float) s->scale_factor[k][l][0] / 16.0;
1100 }
1101 }
1102 }
1103
1104 /* Check for DSYNC after subsubframe */
774e9acf 1105 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
01ca9ac3
KS
1106 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1107#ifdef TRACE
1108 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1109#endif
1110 } else {
1111 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1112 }
1113 }
1114
1115 /* Backup predictor history for adpcm */
774e9acf 1116 for (k = base_channel; k < s->prim_channels; k++)
01ca9ac3
KS
1117 for (l = 0; l < s->vq_start_subband[k]; l++)
1118 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
1119 4 * sizeof(subband_samples[0][0][0]));
1120
6baef06e
NB
1121 return 0;
1122}
1123
1124static int dca_filter_channels(DCAContext * s, int block_index)
1125{
1126 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1127 int k;
1128
01ca9ac3
KS
1129 /* 32 subbands QMF */
1130 for (k = 0; k < s->prim_channels; k++) {
1131/* static float pcm_to_double[8] =
1132 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
92765276 1133 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
1402ee72
BL
1134 M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
1135 s->add_bias );
01ca9ac3
KS
1136 }
1137
1138 /* Down mixing */
6baef06e 1139 if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
df984493 1140 dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab, s->add_bias);
01ca9ac3
KS
1141 }
1142
1143 /* Generate LFE samples for this subsubframe FIXME!!! */
1144 if (s->output & DCA_LFE) {
309d16a4 1145 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
6baef06e 1146 s->lfe_data + 2 * s->lfe * (block_index + 4),
92765276 1147 &s->samples[256 * dca_lfe_index[s->amode]],
1402ee72 1148 (1.0/256.0)*s->scale_bias, s->add_bias);
01ca9ac3
KS
1149 /* Outputs 20bits pcm samples */
1150 }
1151
1152 return 0;
1153}
1154
1155
774e9acf 1156static int dca_subframe_footer(DCAContext * s, int base_channel)
01ca9ac3
KS
1157{
1158 int aux_data_count = 0, i;
01ca9ac3
KS
1159
1160 /*
1161 * Unpack optional information
1162 */
1163
774e9acf
NB
1164 /* presumably optional information only appears in the core? */
1165 if (!base_channel) {
d1177cb5
NB
1166 if (s->timestamp)
1167 get_bits(&s->gb, 32);
01ca9ac3 1168
d1177cb5
NB
1169 if (s->aux_data)
1170 aux_data_count = get_bits(&s->gb, 6);
01ca9ac3 1171
d1177cb5
NB
1172 for (i = 0; i < aux_data_count; i++)
1173 get_bits(&s->gb, 8);
01ca9ac3 1174
d1177cb5
NB
1175 if (s->crc_present && (s->downmix || s->dynrange))
1176 get_bits(&s->gb, 16);
774e9acf 1177 }
01ca9ac3 1178
01ca9ac3
KS
1179 return 0;
1180}
1181
1182/**
1183 * Decode a dca frame block
1184 *
1185 * @param s pointer to the DCAContext
1186 */
1187
774e9acf 1188static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
01ca9ac3
KS
1189{
1190
1191 /* Sanity check */
1192 if (s->current_subframe >= s->subframes) {
1193 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1194 s->current_subframe, s->subframes);
1195 return -1;
1196 }
1197
1198 if (!s->current_subsubframe) {
1199#ifdef TRACE
1200 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1201#endif
1202 /* Read subframe header */
774e9acf 1203 if (dca_subframe_header(s, base_channel, block_index))
01ca9ac3
KS
1204 return -1;
1205 }
1206
1207 /* Read subsubframe */
1208#ifdef TRACE
1209 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1210#endif
774e9acf 1211 if (dca_subsubframe(s, base_channel, block_index))
01ca9ac3
KS
1212 return -1;
1213
1214 /* Update state */
1215 s->current_subsubframe++;
774e9acf 1216 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
01ca9ac3
KS
1217 s->current_subsubframe = 0;
1218 s->current_subframe++;
1219 }
1220 if (s->current_subframe >= s->subframes) {
1221#ifdef TRACE
1222 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1223#endif
1224 /* Read subframe footer */
774e9acf 1225 if (dca_subframe_footer(s, base_channel))
01ca9ac3
KS
1226 return -1;
1227 }
1228
1229 return 0;
1230}
1231
1232/**
1233 * Convert bitstream to one representation based on sync marker
1234 */
a9f87158 1235static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
01ca9ac3
KS
1236 int max_size)
1237{
1238 uint32_t mrk;
1239 int i, tmp;
a9f87158
MN
1240 const uint16_t *ssrc = (const uint16_t *) src;
1241 uint16_t *sdst = (uint16_t *) dst;
01ca9ac3
KS
1242 PutBitContext pb;
1243
d1177cb5 1244 if ((unsigned)src_size > (unsigned)max_size) {
d041a598
KS
1245// av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
1246// return -1;
1247 src_size = max_size;
56fd7cc5 1248 }
9f1473b3 1249
01ca9ac3
KS
1250 mrk = AV_RB32(src);
1251 switch (mrk) {
1252 case DCA_MARKER_RAW_BE:
56523713
MN
1253 memcpy(dst, src, src_size);
1254 return src_size;
01ca9ac3 1255 case DCA_MARKER_RAW_LE:
56523713 1256 for (i = 0; i < (src_size + 1) >> 1; i++)
8fc0162a 1257 *sdst++ = av_bswap16(*ssrc++);
56523713 1258 return src_size;
01ca9ac3
KS
1259 case DCA_MARKER_14B_BE:
1260 case DCA_MARKER_14B_LE:
1261 init_put_bits(&pb, dst, max_size);
1262 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1263 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1264 put_bits(&pb, 14, tmp);
1265 }
1266 flush_put_bits(&pb);
1267 return (put_bits_count(&pb) + 7) >> 3;
1268 default:
1269 return -1;
1270 }
1271}
1272
1273/**
1274 * Main frame decoding function
1275 * FIXME add arguments
1276 */
1277static int dca_decode_frame(AVCodecContext * avctx,
1278 void *data, int *data_size,
7a00bbad 1279 AVPacket *avpkt)
01ca9ac3 1280{
7a00bbad
TB
1281 const uint8_t *buf = avpkt->data;
1282 int buf_size = avpkt->size;
01ca9ac3 1283
6baef06e 1284 int lfe_samples;
774e9acf 1285 int num_core_channels = 0;
8ed96a5e 1286 int i;
01ca9ac3
KS
1287 int16_t *samples = data;
1288 DCAContext *s = avctx->priv_data;
1289 int channels;
1290
1291
d0a18850 1292 s->xch_present = 0;
01ca9ac3
KS
1293 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
1294 if (s->dca_buffer_size == -1) {
56fd7cc5 1295 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
01ca9ac3
KS
1296 return -1;
1297 }
1298
1299 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1300 if (dca_parse_frame_header(s) < 0) {
1301 //seems like the frame is corrupt, try with the next one
75316e1d 1302 *data_size=0;
01ca9ac3
KS
1303 return buf_size;
1304 }
1305 //set AVCodec values with parsed data
1306 avctx->sample_rate = s->sample_rate;
01ca9ac3
KS
1307 avctx->bit_rate = s->bit_rate;
1308
6baef06e 1309 for (i = 0; i < (s->sample_blocks / 8); i++) {
774e9acf
NB
1310 dca_decode_block(s, 0, i);
1311 }
1312
1313 /* record number of core channels incase less than max channels are requested */
1314 num_core_channels = s->prim_channels;
1315
1316 /* extensions start at 32-bit boundaries into bitstream */
324a94b0 1317 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
774e9acf
NB
1318
1319 while(get_bits_left(&s->gb) >= 32) {
324a94b0 1320 uint32_t bits = get_bits_long(&s->gb, 32);
774e9acf
NB
1321
1322 switch(bits) {
1323 case 0x5a5a5a5a: {
0712c230
NB
1324 int ext_amode, xch_fsize;
1325
d0a18850
NB
1326 s->xch_base_channel = s->prim_channels;
1327
0712c230
NB
1328 /* validate sync word using XCHFSIZE field */
1329 xch_fsize = show_bits(&s->gb, 10);
1330 if((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1331 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1332 continue;
774e9acf
NB
1333
1334 /* skip length-to-end-of-frame field for the moment */
1335 skip_bits(&s->gb, 10);
1336
1337 /* extension amode should == 1, number of channels in extension */
1338 /* AFAIK XCh is not used for more channels */
1339 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1340 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1341 " supported!\n",ext_amode);
1342 continue;
1343 }
1344
1345 /* much like core primary audio coding header */
d0a18850 1346 dca_parse_audio_coding_header(s, s->xch_base_channel);
774e9acf
NB
1347
1348 for (i = 0; i < (s->sample_blocks / 8); i++) {
d0a18850 1349 dca_decode_block(s, s->xch_base_channel, i);
774e9acf
NB
1350 }
1351
d0a18850 1352 s->xch_present = 1;
774e9acf
NB
1353 break;
1354 }
df1c694c
AH
1355 case 0x1d95f262: {
1356 int fsize96 = show_bits(&s->gb, 12) + 1;
1357 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1358 continue;
1359
1360 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n", get_bits_count(&s->gb));
1361 skip_bits(&s->gb, 12);
1362 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
774e9acf
NB
1363 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1364 break;
1365 }
df1c694c 1366 }
774e9acf 1367
324a94b0 1368 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
6baef06e
NB
1369 }
1370
cc826626 1371 channels = s->prim_channels + !!s->lfe;
92765276
BL
1372
1373 if (s->amode<16) {
87c3b9bc 1374 avctx->channel_layout = dca_core_channel_layout[s->amode];
87c3b9bc 1375
d0a18850 1376 if (s->xch_present && (!avctx->request_channels ||
08634e7b 1377 avctx->request_channels > num_core_channels + !!s->lfe)) {
63e8d976 1378 avctx->channel_layout |= AV_CH_BACK_CENTER;
774e9acf 1379 if (s->lfe) {
63e8d976 1380 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
774e9acf
NB
1381 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1382 } else {
1383 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1384 }
1385 } else {
08634e7b
NB
1386 channels = num_core_channels + !!s->lfe;
1387 s->xch_present = 0; /* disable further xch processing */
774e9acf 1388 if (s->lfe) {
63e8d976 1389 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
774e9acf
NB
1390 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1391 } else
1392 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1393 }
92765276 1394
08634e7b
NB
1395 if (channels > !!s->lfe &&
1396 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
4e06acbd
LA
1397 return -1;
1398
d1177cb5 1399 if (avctx->request_channels == 2 && s->prim_channels > 2) {
92765276
BL
1400 channels = 2;
1401 s->output = DCA_STEREO;
63e8d976 1402 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
92765276
BL
1403 }
1404 } else {
1405 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
1406 return -1;
1407 }
1408
cc826626 1409
4a24837e
BL
1410 /* There is nothing that prevents a dts frame to change channel configuration
1411 but FFmpeg doesn't support that so only set the channels if it is previously
1412 unset. Ideally during the first probe for channels the crc should be checked
1413 and only set avctx->channels when the crc is ok. Right now the decoder could
1414 set the channels based on a broken first frame.*/
08634e7b 1415 avctx->channels = channels;
4a24837e 1416
d1177cb5 1417 if (*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
01ca9ac3 1418 return -1;
a7bc9d1b 1419 *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
6baef06e
NB
1420
1421 /* filter to get final output */
01ca9ac3 1422 for (i = 0; i < (s->sample_blocks / 8); i++) {
6baef06e 1423 dca_filter_channels(s, i);
b12b16c5
NB
1424
1425 /* If this was marked as a DTS-ES stream we need to subtract back- */
1426 /* channel from SL & SR to remove matrixed back-channel signal */
1427 if((s->source_pcm_res & 1) && s->xch_present) {
1428 float* back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
1429 float* lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
1430 float* rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
1431 int j;
1432 for(j = 0; j < 256; ++j) {
f2401c21
CG
1433 lt_chan[j] -= (back_chan[j] - s->add_bias) * M_SQRT1_2;
1434 rt_chan[j] -= (back_chan[j] - s->add_bias) * M_SQRT1_2;
b12b16c5
NB
1435 }
1436 }
1437
8ed96a5e
1438 s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
1439 samples += 256 * channels;
01ca9ac3
KS
1440 }
1441
6baef06e
NB
1442 /* update lfe history */
1443 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1444 for (i = 0; i < 2 * s->lfe * 4; i++) {
1445 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1446 }
1447
01ca9ac3
KS
1448 return buf_size;
1449}
1450
1451
1452
1453/**
01ca9ac3
KS
1454 * DCA initialization
1455 *
1456 * @param avctx pointer to the AVCodecContext
1457 */
1458
98a6fff9 1459static av_cold int dca_decode_init(AVCodecContext * avctx)
01ca9ac3
KS
1460{
1461 DCAContext *s = avctx->priv_data;
8ed96a5e 1462 int i;
01ca9ac3
KS
1463
1464 s->avctx = avctx;
1465 dca_init_vlcs();
01ca9ac3
KS
1466
1467 dsputil_init(&s->dsp, avctx);
7d485f16 1468 ff_mdct_init(&s->imdct, 6, 1, 1.0);
f462ed1f 1469 ff_synth_filter_init(&s->synth);
309d16a4 1470 ff_dcadsp_init(&s->dcadsp);
cfec09e9 1471
6baef06e 1472 for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++)
8ed96a5e 1473 s->samples_chanptr[i] = s->samples + i * 256;
5d6e4c16 1474 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1402ee72 1475
d1177cb5 1476 if (s->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
1402ee72
BL
1477 s->add_bias = 385.0f;
1478 s->scale_bias = 1.0 / 32768.0;
1479 } else {
1480 s->add_bias = 0.0f;
1481 s->scale_bias = 1.0;
ba5fc967
BL
1482
1483 /* allow downmixing to stereo */
1484 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1485 avctx->request_channels == 2) {
1486 avctx->channels = avctx->request_channels;
1487 }
1402ee72
BL
1488 }
1489
1490
01ca9ac3
KS
1491 return 0;
1492}
1493
89df5e95
MN
1494static av_cold int dca_decode_end(AVCodecContext * avctx)
1495{
1496 DCAContext *s = avctx->priv_data;
1497 ff_mdct_end(&s->imdct);
1498 return 0;
1499}
01ca9ac3
KS
1500
1501AVCodec dca_decoder = {
1502 .name = "dca",
72415b2a 1503 .type = AVMEDIA_TYPE_AUDIO,
01ca9ac3
KS
1504 .id = CODEC_ID_DTS,
1505 .priv_data_size = sizeof(DCAContext),
1506 .init = dca_decode_init,
1507 .decode = dca_decode_frame,
89df5e95 1508 .close = dca_decode_end,
fe4bf374 1509 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
62784e37 1510 .capabilities = CODEC_CAP_CHANNEL_CONF,
01ca9ac3 1511};