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