Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
[ffmpeg-lucabe.git] / libavcodec / dca.c
blob8dfec8e9a913df08d1d635445eea7a52377321d8
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
8 * This file is part of FFmpeg.
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.
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.
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
25 /**
26 * @file libavcodec/dca.c
29 #include <math.h>
30 #include <stddef.h>
31 #include <stdio.h>
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "bitstream.h"
36 #include "dcadata.h"
37 #include "dcahuff.h"
38 #include "dca.h"
40 //#define TRACE
42 #define DCA_PRIM_CHANNELS_MAX (5)
43 #define DCA_SUBBANDS (32)
44 #define DCA_ABITS_MAX (32) /* Should be 28 */
45 #define DCA_SUBSUBFAMES_MAX (4)
46 #define DCA_LFE_MAX (3)
48 enum DCAMode {
49 DCA_MONO = 0,
50 DCA_CHANNEL,
51 DCA_STEREO,
52 DCA_STEREO_SUMDIFF,
53 DCA_STEREO_TOTAL,
54 DCA_3F,
55 DCA_2F1R,
56 DCA_3F1R,
57 DCA_2F2R,
58 DCA_3F2R,
59 DCA_4F2R
62 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
63 * Some compromises have been made for special configurations. Most configurations
64 * are never used so complete accuracy is not needed.
66 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
67 * S -> side, when both rear and back are configured move one of them to the side channel
68 * OV -> center back
69 * All 2 channel configurations -> CH_LAYOUT_STEREO
72 static const int64_t dca_core_channel_layout[] = {
73 CH_FRONT_CENTER, ///< 1, A
74 CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
75 CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
76 CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference)
77 CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total)
78 CH_LAYOUT_STEREO|CH_FRONT_CENTER, ///< 3, C+L+R
79 CH_LAYOUT_STEREO|CH_BACK_CENTER, ///< 3, L+R+S
80 CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 4, C + L + R+ S
81 CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR
82 CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
83 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
84 CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 6, C + L + R+ LR + RR + OV
85 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
86 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
87 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
88 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
91 static const int8_t dca_lfe_index[] = {
92 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
95 static const int8_t dca_channel_reorder_lfe[][8] = {
96 { 0, -1, -1, -1, -1, -1, -1, -1},
97 { 0, 1, -1, -1, -1, -1, -1, -1},
98 { 0, 1, -1, -1, -1, -1, -1, -1},
99 { 0, 1, -1, -1, -1, -1, -1, -1},
100 { 0, 1, -1, -1, -1, -1, -1, -1},
101 { 2, 0, 1, -1, -1, -1, -1, -1},
102 { 0, 1, 3, -1, -1, -1, -1, -1},
103 { 2, 0, 1, 4, -1, -1, -1, -1},
104 { 0, 1, 3, 4, -1, -1, -1, -1},
105 { 2, 0, 1, 4, 5, -1, -1, -1},
106 { 3, 4, 0, 1, 5, 6, -1, -1},
107 { 2, 0, 1, 4, 5, 6, -1, -1},
108 { 0, 6, 4, 5, 2, 3, -1, -1},
109 { 4, 2, 5, 0, 1, 6, 7, -1},
110 { 5, 6, 0, 1, 7, 3, 8, 4},
111 { 4, 2, 5, 0, 1, 6, 8, 7},
114 static const int8_t dca_channel_reorder_nolfe[][8] = {
115 { 0, -1, -1, -1, -1, -1, -1, -1},
116 { 0, 1, -1, -1, -1, -1, -1, -1},
117 { 0, 1, -1, -1, -1, -1, -1, -1},
118 { 0, 1, -1, -1, -1, -1, -1, -1},
119 { 0, 1, -1, -1, -1, -1, -1, -1},
120 { 2, 0, 1, -1, -1, -1, -1, -1},
121 { 0, 1, 2, -1, -1, -1, -1, -1},
122 { 2, 0, 1, 3, -1, -1, -1, -1},
123 { 0, 1, 2, 3, -1, -1, -1, -1},
124 { 2, 0, 1, 3, 4, -1, -1, -1},
125 { 2, 3, 0, 1, 4, 5, -1, -1},
126 { 2, 0, 1, 3, 4, 5, -1, -1},
127 { 0, 5, 3, 4, 1, 2, -1, -1},
128 { 3, 2, 4, 0, 1, 5, 6, -1},
129 { 4, 5, 0, 1, 6, 2, 7, 3},
130 { 3, 2, 4, 0, 1, 5, 7, 6},
134 #define DCA_DOLBY 101 /* FIXME */
136 #define DCA_CHANNEL_BITS 6
137 #define DCA_CHANNEL_MASK 0x3F
139 #define DCA_LFE 0x80
141 #define HEADER_SIZE 14
143 #define DCA_MAX_FRAME_SIZE 16384
145 /** Bit allocation */
146 typedef struct {
147 int offset; ///< code values offset
148 int maxbits[8]; ///< max bits in VLC
149 int wrap; ///< wrap for get_vlc2()
150 VLC vlc[8]; ///< actual codes
151 } BitAlloc;
153 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
154 static BitAlloc dca_tmode; ///< transition mode VLCs
155 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
156 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
158 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
160 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
163 typedef struct {
164 AVCodecContext *avctx;
165 /* Frame header */
166 int frame_type; ///< type of the current frame
167 int samples_deficit; ///< deficit sample count
168 int crc_present; ///< crc is present in the bitstream
169 int sample_blocks; ///< number of PCM sample blocks
170 int frame_size; ///< primary frame byte size
171 int amode; ///< audio channels arrangement
172 int sample_rate; ///< audio sampling rate
173 int bit_rate; ///< transmission bit rate
174 int bit_rate_index; ///< transmission bit rate index
176 int downmix; ///< embedded downmix enabled
177 int dynrange; ///< embedded dynamic range flag
178 int timestamp; ///< embedded time stamp flag
179 int aux_data; ///< auxiliary data flag
180 int hdcd; ///< source material is mastered in HDCD
181 int ext_descr; ///< extension audio descriptor flag
182 int ext_coding; ///< extended coding flag
183 int aspf; ///< audio sync word insertion flag
184 int lfe; ///< low frequency effects flag
185 int predictor_history; ///< predictor history flag
186 int header_crc; ///< header crc check bytes
187 int multirate_inter; ///< multirate interpolator switch
188 int version; ///< encoder software revision
189 int copy_history; ///< copy history
190 int source_pcm_res; ///< source pcm resolution
191 int front_sum; ///< front sum/difference flag
192 int surround_sum; ///< surround sum/difference flag
193 int dialog_norm; ///< dialog normalisation parameter
195 /* Primary audio coding header */
196 int subframes; ///< number of subframes
197 int total_channels; ///< number of channels including extensions
198 int prim_channels; ///< number of primary audio channels
199 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
200 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
201 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
202 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
203 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
204 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
205 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
206 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
208 /* Primary audio coding side information */
209 int subsubframes; ///< number of subsubframes
210 int partial_samples; ///< partial subsubframe samples count
211 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
212 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
213 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
214 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
215 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
216 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
217 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
218 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
219 int dynrange_coef; ///< dynamic range coefficient
221 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
223 float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX *
224 2 /*history */ ]; ///< Low frequency effect data
225 int lfe_scale_factor;
227 /* Subband samples history (for ADPCM) */
228 float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
229 DECLARE_ALIGNED_16(float, subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512]);
230 float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][32];
231 int hist_index[DCA_PRIM_CHANNELS_MAX];
233 int output; ///< type of output
234 float add_bias; ///< output bias
235 float scale_bias; ///< output scale
237 DECLARE_ALIGNED_16(float, samples[1536]); /* 6 * 256 = 1536, might only need 5 */
238 const float *samples_chanptr[6];
240 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
241 int dca_buffer_size; ///< how much data is in the dca_buffer
243 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe
244 GetBitContext gb;
245 /* Current position in DCA frame */
246 int current_subframe;
247 int current_subsubframe;
249 int debug_flag; ///< used for suppressing repeated error messages output
250 DSPContext dsp;
251 MDCTContext imdct;
252 } DCAContext;
254 static av_cold void dca_init_vlcs(void)
256 static int vlcs_initialized = 0;
257 int i, j;
259 if (vlcs_initialized)
260 return;
262 dca_bitalloc_index.offset = 1;
263 dca_bitalloc_index.wrap = 2;
264 for (i = 0; i < 5; i++)
265 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
266 bitalloc_12_bits[i], 1, 1,
267 bitalloc_12_codes[i], 2, 2, 1);
268 dca_scalefactor.offset = -64;
269 dca_scalefactor.wrap = 2;
270 for (i = 0; i < 5; i++)
271 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
272 scales_bits[i], 1, 1,
273 scales_codes[i], 2, 2, 1);
274 dca_tmode.offset = 0;
275 dca_tmode.wrap = 1;
276 for (i = 0; i < 4; i++)
277 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
278 tmode_bits[i], 1, 1,
279 tmode_codes[i], 2, 2, 1);
281 for(i = 0; i < 10; i++)
282 for(j = 0; j < 7; j++){
283 if(!bitalloc_codes[i][j]) break;
284 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
285 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
286 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
287 bitalloc_sizes[i],
288 bitalloc_bits[i][j], 1, 1,
289 bitalloc_codes[i][j], 2, 2, 1);
291 vlcs_initialized = 1;
294 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
296 while(len--)
297 *dst++ = get_bits(gb, bits);
300 static int dca_parse_frame_header(DCAContext * s)
302 int i, j;
303 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
304 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
305 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
307 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
309 /* Sync code */
310 get_bits(&s->gb, 32);
312 /* Frame header */
313 s->frame_type = get_bits(&s->gb, 1);
314 s->samples_deficit = get_bits(&s->gb, 5) + 1;
315 s->crc_present = get_bits(&s->gb, 1);
316 s->sample_blocks = get_bits(&s->gb, 7) + 1;
317 s->frame_size = get_bits(&s->gb, 14) + 1;
318 if (s->frame_size < 95)
319 return -1;
320 s->amode = get_bits(&s->gb, 6);
321 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
322 if (!s->sample_rate)
323 return -1;
324 s->bit_rate_index = get_bits(&s->gb, 5);
325 s->bit_rate = dca_bit_rates[s->bit_rate_index];
326 if (!s->bit_rate)
327 return -1;
329 s->downmix = get_bits(&s->gb, 1);
330 s->dynrange = get_bits(&s->gb, 1);
331 s->timestamp = get_bits(&s->gb, 1);
332 s->aux_data = get_bits(&s->gb, 1);
333 s->hdcd = get_bits(&s->gb, 1);
334 s->ext_descr = get_bits(&s->gb, 3);
335 s->ext_coding = get_bits(&s->gb, 1);
336 s->aspf = get_bits(&s->gb, 1);
337 s->lfe = get_bits(&s->gb, 2);
338 s->predictor_history = get_bits(&s->gb, 1);
340 /* TODO: check CRC */
341 if (s->crc_present)
342 s->header_crc = get_bits(&s->gb, 16);
344 s->multirate_inter = get_bits(&s->gb, 1);
345 s->version = get_bits(&s->gb, 4);
346 s->copy_history = get_bits(&s->gb, 2);
347 s->source_pcm_res = get_bits(&s->gb, 3);
348 s->front_sum = get_bits(&s->gb, 1);
349 s->surround_sum = get_bits(&s->gb, 1);
350 s->dialog_norm = get_bits(&s->gb, 4);
352 /* FIXME: channels mixing levels */
353 s->output = s->amode;
354 if(s->lfe) s->output |= DCA_LFE;
356 #ifdef TRACE
357 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
358 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
359 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
360 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
361 s->sample_blocks, s->sample_blocks * 32);
362 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
363 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
364 s->amode, dca_channels[s->amode]);
365 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
366 s->sample_rate);
367 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
368 s->bit_rate);
369 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
370 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
371 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
372 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
373 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
374 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
375 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
376 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
377 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
378 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
379 s->predictor_history);
380 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
381 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
382 s->multirate_inter);
383 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
384 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
385 av_log(s->avctx, AV_LOG_DEBUG,
386 "source pcm resolution: %i (%i bits/sample)\n",
387 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
388 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
389 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
390 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
391 av_log(s->avctx, AV_LOG_DEBUG, "\n");
392 #endif
394 /* Primary audio coding header */
395 s->subframes = get_bits(&s->gb, 4) + 1;
396 s->total_channels = get_bits(&s->gb, 3) + 1;
397 s->prim_channels = s->total_channels;
398 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
399 s->prim_channels = DCA_PRIM_CHANNELS_MAX; /* We only support DTS core */
402 for (i = 0; i < s->prim_channels; i++) {
403 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
404 if (s->subband_activity[i] > DCA_SUBBANDS)
405 s->subband_activity[i] = DCA_SUBBANDS;
407 for (i = 0; i < s->prim_channels; i++) {
408 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
409 if (s->vq_start_subband[i] > DCA_SUBBANDS)
410 s->vq_start_subband[i] = DCA_SUBBANDS;
412 get_array(&s->gb, s->joint_intensity, s->prim_channels, 3);
413 get_array(&s->gb, s->transient_huffman, s->prim_channels, 2);
414 get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3);
415 get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3);
417 /* Get codebooks quantization indexes */
418 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
419 for (j = 1; j < 11; j++)
420 for (i = 0; i < s->prim_channels; i++)
421 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
423 /* Get scale factor adjustment */
424 for (j = 0; j < 11; j++)
425 for (i = 0; i < s->prim_channels; i++)
426 s->scalefactor_adj[i][j] = 1;
428 for (j = 1; j < 11; j++)
429 for (i = 0; i < s->prim_channels; i++)
430 if (s->quant_index_huffman[i][j] < thr[j])
431 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
433 if (s->crc_present) {
434 /* Audio header CRC check */
435 get_bits(&s->gb, 16);
438 s->current_subframe = 0;
439 s->current_subsubframe = 0;
441 #ifdef TRACE
442 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
443 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
444 for(i = 0; i < s->prim_channels; i++){
445 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
446 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
447 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
448 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
449 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
450 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
451 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
452 for (j = 0; j < 11; j++)
453 av_log(s->avctx, AV_LOG_DEBUG, " %i",
454 s->quant_index_huffman[i][j]);
455 av_log(s->avctx, AV_LOG_DEBUG, "\n");
456 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
457 for (j = 0; j < 11; j++)
458 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
459 av_log(s->avctx, AV_LOG_DEBUG, "\n");
461 #endif
463 return 0;
467 static inline int get_scale(GetBitContext *gb, int level, int value)
469 if (level < 5) {
470 /* huffman encoded */
471 value += get_bitalloc(gb, &dca_scalefactor, level);
472 } else if(level < 8)
473 value = get_bits(gb, level + 1);
474 return value;
477 static int dca_subframe_header(DCAContext * s)
479 /* Primary audio coding side information */
480 int j, k;
482 s->subsubframes = get_bits(&s->gb, 2) + 1;
483 s->partial_samples = get_bits(&s->gb, 3);
484 for (j = 0; j < s->prim_channels; j++) {
485 for (k = 0; k < s->subband_activity[j]; k++)
486 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
489 /* Get prediction codebook */
490 for (j = 0; j < s->prim_channels; j++) {
491 for (k = 0; k < s->subband_activity[j]; k++) {
492 if (s->prediction_mode[j][k] > 0) {
493 /* (Prediction coefficient VQ address) */
494 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
499 /* Bit allocation index */
500 for (j = 0; j < s->prim_channels; j++) {
501 for (k = 0; k < s->vq_start_subband[j]; k++) {
502 if (s->bitalloc_huffman[j] == 6)
503 s->bitalloc[j][k] = get_bits(&s->gb, 5);
504 else if (s->bitalloc_huffman[j] == 5)
505 s->bitalloc[j][k] = get_bits(&s->gb, 4);
506 else if (s->bitalloc_huffman[j] == 7) {
507 av_log(s->avctx, AV_LOG_ERROR,
508 "Invalid bit allocation index\n");
509 return -1;
510 } else {
511 s->bitalloc[j][k] =
512 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
515 if (s->bitalloc[j][k] > 26) {
516 // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
517 // j, k, s->bitalloc[j][k]);
518 return -1;
523 /* Transition mode */
524 for (j = 0; j < s->prim_channels; j++) {
525 for (k = 0; k < s->subband_activity[j]; k++) {
526 s->transition_mode[j][k] = 0;
527 if (s->subsubframes > 1 &&
528 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
529 s->transition_mode[j][k] =
530 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
535 for (j = 0; j < s->prim_channels; j++) {
536 const uint32_t *scale_table;
537 int scale_sum;
539 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
541 if (s->scalefactor_huffman[j] == 6)
542 scale_table = scale_factor_quant7;
543 else
544 scale_table = scale_factor_quant6;
546 /* When huffman coded, only the difference is encoded */
547 scale_sum = 0;
549 for (k = 0; k < s->subband_activity[j]; k++) {
550 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
551 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
552 s->scale_factor[j][k][0] = scale_table[scale_sum];
555 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
556 /* Get second scale factor */
557 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
558 s->scale_factor[j][k][1] = scale_table[scale_sum];
563 /* Joint subband scale factor codebook select */
564 for (j = 0; j < s->prim_channels; j++) {
565 /* Transmitted only if joint subband coding enabled */
566 if (s->joint_intensity[j] > 0)
567 s->joint_huff[j] = get_bits(&s->gb, 3);
570 /* Scale factors for joint subband coding */
571 for (j = 0; j < s->prim_channels; j++) {
572 int source_channel;
574 /* Transmitted only if joint subband coding enabled */
575 if (s->joint_intensity[j] > 0) {
576 int scale = 0;
577 source_channel = s->joint_intensity[j] - 1;
579 /* When huffman coded, only the difference is encoded
580 * (is this valid as well for joint scales ???) */
582 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
583 scale = get_scale(&s->gb, s->joint_huff[j], 0);
584 scale += 64; /* bias */
585 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
588 if (!s->debug_flag & 0x02) {
589 av_log(s->avctx, AV_LOG_DEBUG,
590 "Joint stereo coding not supported\n");
591 s->debug_flag |= 0x02;
596 /* Stereo downmix coefficients */
597 if (s->prim_channels > 2) {
598 if(s->downmix) {
599 for (j = 0; j < s->prim_channels; j++) {
600 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
601 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
603 } else {
604 int am = s->amode & DCA_CHANNEL_MASK;
605 for (j = 0; j < s->prim_channels; j++) {
606 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
607 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
612 /* Dynamic range coefficient */
613 if (s->dynrange)
614 s->dynrange_coef = get_bits(&s->gb, 8);
616 /* Side information CRC check word */
617 if (s->crc_present) {
618 get_bits(&s->gb, 16);
622 * Primary audio data arrays
625 /* VQ encoded high frequency subbands */
626 for (j = 0; j < s->prim_channels; j++)
627 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
628 /* 1 vector -> 32 samples */
629 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
631 /* Low frequency effect data */
632 if (s->lfe) {
633 /* LFE samples */
634 int lfe_samples = 2 * s->lfe * s->subsubframes;
635 float lfe_scale;
637 for (j = lfe_samples; j < lfe_samples * 2; j++) {
638 /* Signed 8 bits int */
639 s->lfe_data[j] = get_sbits(&s->gb, 8);
642 /* Scale factor index */
643 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
645 /* Quantization step size * scale factor */
646 lfe_scale = 0.035 * s->lfe_scale_factor;
648 for (j = lfe_samples; j < lfe_samples * 2; j++)
649 s->lfe_data[j] *= lfe_scale;
652 #ifdef TRACE
653 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes);
654 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
655 s->partial_samples);
656 for (j = 0; j < s->prim_channels; j++) {
657 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
658 for (k = 0; k < s->subband_activity[j]; k++)
659 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
660 av_log(s->avctx, AV_LOG_DEBUG, "\n");
662 for (j = 0; j < s->prim_channels; j++) {
663 for (k = 0; k < s->subband_activity[j]; k++)
664 av_log(s->avctx, AV_LOG_DEBUG,
665 "prediction coefs: %f, %f, %f, %f\n",
666 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
667 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
668 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
669 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
671 for (j = 0; j < s->prim_channels; j++) {
672 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
673 for (k = 0; k < s->vq_start_subband[j]; k++)
674 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
675 av_log(s->avctx, AV_LOG_DEBUG, "\n");
677 for (j = 0; j < s->prim_channels; j++) {
678 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
679 for (k = 0; k < s->subband_activity[j]; k++)
680 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
681 av_log(s->avctx, AV_LOG_DEBUG, "\n");
683 for (j = 0; j < s->prim_channels; j++) {
684 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
685 for (k = 0; k < s->subband_activity[j]; k++) {
686 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
687 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
688 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
689 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
691 av_log(s->avctx, AV_LOG_DEBUG, "\n");
693 for (j = 0; j < s->prim_channels; j++) {
694 if (s->joint_intensity[j] > 0) {
695 int source_channel = s->joint_intensity[j] - 1;
696 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
697 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
698 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
699 av_log(s->avctx, AV_LOG_DEBUG, "\n");
702 if (s->prim_channels > 2 && s->downmix) {
703 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
704 for (j = 0; j < s->prim_channels; j++) {
705 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
706 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
708 av_log(s->avctx, AV_LOG_DEBUG, "\n");
710 for (j = 0; j < s->prim_channels; j++)
711 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
712 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
713 if(s->lfe){
714 int lfe_samples = 2 * s->lfe * s->subsubframes;
715 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
716 for (j = lfe_samples; j < lfe_samples * 2; j++)
717 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
718 av_log(s->avctx, AV_LOG_DEBUG, "\n");
720 #endif
722 return 0;
725 static void qmf_32_subbands(DCAContext * s, int chans,
726 float samples_in[32][8], float *samples_out,
727 float scale, float bias)
729 const float *prCoeff;
730 int i, j;
731 DECLARE_ALIGNED_16(float, raXin[32]);
733 int hist_index= s->hist_index[chans];
734 float *subband_fir_hist2 = s->subband_fir_noidea[chans];
736 int subindex;
738 scale *= sqrt(1/8.0);
740 /* Select filter */
741 if (!s->multirate_inter) /* Non-perfect reconstruction */
742 prCoeff = fir_32bands_nonperfect;
743 else /* Perfect reconstruction */
744 prCoeff = fir_32bands_perfect;
746 /* Reconstructed channel sample index */
747 for (subindex = 0; subindex < 8; subindex++) {
748 float *subband_fir_hist = s->subband_fir_hist[chans] + hist_index;
749 /* Load in one sample from each subband and clear inactive subbands */
750 for (i = 0; i < s->subband_activity[chans]; i++){
751 if((i-1)&2) raXin[i] = -samples_in[i][subindex];
752 else raXin[i] = samples_in[i][subindex];
754 for (; i < 32; i++)
755 raXin[i] = 0.0;
757 ff_imdct_half(&s->imdct, subband_fir_hist, raXin);
759 /* Multiply by filter coefficients */
760 for (i = 0; i < 16; i++){
761 float a= subband_fir_hist2[i ];
762 float b= subband_fir_hist2[i+16];
763 float c= 0;
764 float d= 0;
765 for (j = 0; j < 512-hist_index; j += 64){
766 a += prCoeff[i+j ]*(-subband_fir_hist[15-i+j]);
767 b += prCoeff[i+j+16]*( subband_fir_hist[ i+j]);
768 c += prCoeff[i+j+32]*( subband_fir_hist[16+i+j]);
769 d += prCoeff[i+j+48]*( subband_fir_hist[31-i+j]);
771 for ( ; j < 512; j += 64){
772 a += prCoeff[i+j ]*(-subband_fir_hist[15-i+j-512]);
773 b += prCoeff[i+j+16]*( subband_fir_hist[ i+j-512]);
774 c += prCoeff[i+j+32]*( subband_fir_hist[16+i+j-512]);
775 d += prCoeff[i+j+48]*( subband_fir_hist[31-i+j-512]);
777 samples_out[i ] = a * scale + bias;
778 samples_out[i+16] = b * scale + bias;
779 subband_fir_hist2[i ] = c;
780 subband_fir_hist2[i+16] = d;
782 samples_out+= 32;
784 hist_index = (hist_index-32)&511;
786 s->hist_index[chans]= hist_index;
789 static void lfe_interpolation_fir(int decimation_select,
790 int num_deci_sample, float *samples_in,
791 float *samples_out, float scale,
792 float bias)
794 /* samples_in: An array holding decimated samples.
795 * Samples in current subframe starts from samples_in[0],
796 * while samples_in[-1], samples_in[-2], ..., stores samples
797 * from last subframe as history.
799 * samples_out: An array holding interpolated samples
802 int decifactor, k, j;
803 const float *prCoeff;
805 int interp_index = 0; /* Index to the interpolated samples */
806 int deciindex;
808 /* Select decimation filter */
809 if (decimation_select == 1) {
810 decifactor = 128;
811 prCoeff = lfe_fir_128;
812 } else {
813 decifactor = 64;
814 prCoeff = lfe_fir_64;
816 /* Interpolation */
817 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
818 /* One decimated sample generates decifactor interpolated ones */
819 for (k = 0; k < decifactor; k++) {
820 float rTmp = 0.0;
821 //FIXME the coeffs are symetric, fix that
822 for (j = 0; j < 512 / decifactor; j++)
823 rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor];
824 samples_out[interp_index++] = (rTmp * scale) + bias;
829 /* downmixing routines */
830 #define MIX_REAR1(samples, si1, rs, coef) \
831 samples[i] += samples[si1] * coef[rs][0]; \
832 samples[i+256] += samples[si1] * coef[rs][1];
834 #define MIX_REAR2(samples, si1, si2, rs, coef) \
835 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
836 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
838 #define MIX_FRONT3(samples, coef) \
839 t = samples[i]; \
840 samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
841 samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
843 #define DOWNMIX_TO_STEREO(op1, op2) \
844 for(i = 0; i < 256; i++){ \
845 op1 \
846 op2 \
849 static void dca_downmix(float *samples, int srcfmt,
850 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
852 int i;
853 float t;
854 float coef[DCA_PRIM_CHANNELS_MAX][2];
856 for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
857 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
858 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
861 switch (srcfmt) {
862 case DCA_MONO:
863 case DCA_CHANNEL:
864 case DCA_STEREO_TOTAL:
865 case DCA_STEREO_SUMDIFF:
866 case DCA_4F2R:
867 av_log(NULL, 0, "Not implemented!\n");
868 break;
869 case DCA_STEREO:
870 break;
871 case DCA_3F:
872 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
873 break;
874 case DCA_2F1R:
875 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
876 break;
877 case DCA_3F1R:
878 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
879 MIX_REAR1(samples, i + 768, 3, coef));
880 break;
881 case DCA_2F2R:
882 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
883 break;
884 case DCA_3F2R:
885 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
886 MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
887 break;
892 /* Very compact version of the block code decoder that does not use table
893 * look-up but is slightly slower */
894 static int decode_blockcode(int code, int levels, int *values)
896 int i;
897 int offset = (levels - 1) >> 1;
899 for (i = 0; i < 4; i++) {
900 values[i] = (code % levels) - offset;
901 code /= levels;
904 if (code == 0)
905 return 0;
906 else {
907 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
908 return -1;
912 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
913 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
915 static int dca_subsubframe(DCAContext * s)
917 int k, l;
918 int subsubframe = s->current_subsubframe;
920 const float *quant_step_table;
922 /* FIXME */
923 float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
926 * Audio data
929 /* Select quantization step size table */
930 if (s->bit_rate_index == 0x1f)
931 quant_step_table = lossless_quant_d;
932 else
933 quant_step_table = lossy_quant_d;
935 for (k = 0; k < s->prim_channels; k++) {
936 for (l = 0; l < s->vq_start_subband[k]; l++) {
937 int m;
939 /* Select the mid-tread linear quantizer */
940 int abits = s->bitalloc[k][l];
942 float quant_step_size = quant_step_table[abits];
943 float rscale;
946 * Determine quantization index code book and its type
949 /* Select quantization index code book */
950 int sel = s->quant_index_huffman[k][abits];
953 * Extract bits from the bit stream
955 if(!abits){
956 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
957 }else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
958 if(abits <= 7){
959 /* Block code */
960 int block_code1, block_code2, size, levels;
961 int block[8];
963 size = abits_sizes[abits-1];
964 levels = abits_levels[abits-1];
966 block_code1 = get_bits(&s->gb, size);
967 /* FIXME Should test return value */
968 decode_blockcode(block_code1, levels, block);
969 block_code2 = get_bits(&s->gb, size);
970 decode_blockcode(block_code2, levels, &block[4]);
971 for (m = 0; m < 8; m++)
972 subband_samples[k][l][m] = block[m];
973 }else{
974 /* no coding */
975 for (m = 0; m < 8; m++)
976 subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3);
978 }else{
979 /* Huffman coded */
980 for (m = 0; m < 8; m++)
981 subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
984 /* Deal with transients */
985 if (s->transition_mode[k][l] &&
986 subsubframe >= s->transition_mode[k][l])
987 rscale = quant_step_size * s->scale_factor[k][l][1];
988 else
989 rscale = quant_step_size * s->scale_factor[k][l][0];
991 rscale *= s->scalefactor_adj[k][sel];
993 for (m = 0; m < 8; m++)
994 subband_samples[k][l][m] *= rscale;
997 * Inverse ADPCM if in prediction mode
999 if (s->prediction_mode[k][l]) {
1000 int n;
1001 for (m = 0; m < 8; m++) {
1002 for (n = 1; n <= 4; n++)
1003 if (m >= n)
1004 subband_samples[k][l][m] +=
1005 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1006 subband_samples[k][l][m - n] / 8192);
1007 else if (s->predictor_history)
1008 subband_samples[k][l][m] +=
1009 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1010 s->subband_samples_hist[k][l][m - n +
1011 4] / 8192);
1017 * Decode VQ encoded high frequencies
1019 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1020 /* 1 vector -> 32 samples but we only need the 8 samples
1021 * for this subsubframe. */
1022 int m;
1024 if (!s->debug_flag & 0x01) {
1025 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
1026 s->debug_flag |= 0x01;
1029 for (m = 0; m < 8; m++) {
1030 subband_samples[k][l][m] =
1031 high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
1033 * (float) s->scale_factor[k][l][0] / 16.0;
1038 /* Check for DSYNC after subsubframe */
1039 if (s->aspf || subsubframe == s->subsubframes - 1) {
1040 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1041 #ifdef TRACE
1042 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1043 #endif
1044 } else {
1045 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1049 /* Backup predictor history for adpcm */
1050 for (k = 0; k < s->prim_channels; k++)
1051 for (l = 0; l < s->vq_start_subband[k]; l++)
1052 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
1053 4 * sizeof(subband_samples[0][0][0]));
1055 /* 32 subbands QMF */
1056 for (k = 0; k < s->prim_channels; k++) {
1057 /* static float pcm_to_double[8] =
1058 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
1059 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
1060 M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
1061 s->add_bias );
1064 /* Down mixing */
1066 if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) {
1067 dca_downmix(s->samples, s->amode, s->downmix_coef);
1070 /* Generate LFE samples for this subsubframe FIXME!!! */
1071 if (s->output & DCA_LFE) {
1072 int lfe_samples = 2 * s->lfe * s->subsubframes;
1074 lfe_interpolation_fir(s->lfe, 2 * s->lfe,
1075 s->lfe_data + lfe_samples +
1076 2 * s->lfe * subsubframe,
1077 &s->samples[256 * dca_lfe_index[s->amode]],
1078 (1.0/256.0)*s->scale_bias, s->add_bias);
1079 /* Outputs 20bits pcm samples */
1082 return 0;
1086 static int dca_subframe_footer(DCAContext * s)
1088 int aux_data_count = 0, i;
1089 int lfe_samples;
1092 * Unpack optional information
1095 if (s->timestamp)
1096 get_bits(&s->gb, 32);
1098 if (s->aux_data)
1099 aux_data_count = get_bits(&s->gb, 6);
1101 for (i = 0; i < aux_data_count; i++)
1102 get_bits(&s->gb, 8);
1104 if (s->crc_present && (s->downmix || s->dynrange))
1105 get_bits(&s->gb, 16);
1107 lfe_samples = 2 * s->lfe * s->subsubframes;
1108 for (i = 0; i < lfe_samples; i++) {
1109 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1112 return 0;
1116 * Decode a dca frame block
1118 * @param s pointer to the DCAContext
1121 static int dca_decode_block(DCAContext * s)
1124 /* Sanity check */
1125 if (s->current_subframe >= s->subframes) {
1126 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1127 s->current_subframe, s->subframes);
1128 return -1;
1131 if (!s->current_subsubframe) {
1132 #ifdef TRACE
1133 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1134 #endif
1135 /* Read subframe header */
1136 if (dca_subframe_header(s))
1137 return -1;
1140 /* Read subsubframe */
1141 #ifdef TRACE
1142 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1143 #endif
1144 if (dca_subsubframe(s))
1145 return -1;
1147 /* Update state */
1148 s->current_subsubframe++;
1149 if (s->current_subsubframe >= s->subsubframes) {
1150 s->current_subsubframe = 0;
1151 s->current_subframe++;
1153 if (s->current_subframe >= s->subframes) {
1154 #ifdef TRACE
1155 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1156 #endif
1157 /* Read subframe footer */
1158 if (dca_subframe_footer(s))
1159 return -1;
1162 return 0;
1166 * Convert bitstream to one representation based on sync marker
1168 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
1169 int max_size)
1171 uint32_t mrk;
1172 int i, tmp;
1173 const uint16_t *ssrc = (const uint16_t *) src;
1174 uint16_t *sdst = (uint16_t *) dst;
1175 PutBitContext pb;
1177 if((unsigned)src_size > (unsigned)max_size) {
1178 // av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
1179 // return -1;
1180 src_size = max_size;
1183 mrk = AV_RB32(src);
1184 switch (mrk) {
1185 case DCA_MARKER_RAW_BE:
1186 memcpy(dst, src, src_size);
1187 return src_size;
1188 case DCA_MARKER_RAW_LE:
1189 for (i = 0; i < (src_size + 1) >> 1; i++)
1190 *sdst++ = bswap_16(*ssrc++);
1191 return src_size;
1192 case DCA_MARKER_14B_BE:
1193 case DCA_MARKER_14B_LE:
1194 init_put_bits(&pb, dst, max_size);
1195 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1196 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1197 put_bits(&pb, 14, tmp);
1199 flush_put_bits(&pb);
1200 return (put_bits_count(&pb) + 7) >> 3;
1201 default:
1202 return -1;
1207 * Main frame decoding function
1208 * FIXME add arguments
1210 static int dca_decode_frame(AVCodecContext * avctx,
1211 void *data, int *data_size,
1212 AVPacket *avpkt)
1214 const uint8_t *buf = avpkt->data;
1215 int buf_size = avpkt->size;
1217 int i;
1218 int16_t *samples = data;
1219 DCAContext *s = avctx->priv_data;
1220 int channels;
1223 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
1224 if (s->dca_buffer_size == -1) {
1225 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1226 return -1;
1229 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1230 if (dca_parse_frame_header(s) < 0) {
1231 //seems like the frame is corrupt, try with the next one
1232 *data_size=0;
1233 return buf_size;
1235 //set AVCodec values with parsed data
1236 avctx->sample_rate = s->sample_rate;
1237 avctx->bit_rate = s->bit_rate;
1239 channels = s->prim_channels + !!s->lfe;
1241 if (s->amode<16) {
1242 avctx->channel_layout = dca_core_channel_layout[s->amode];
1244 if (s->lfe) {
1245 avctx->channel_layout |= CH_LOW_FREQUENCY;
1246 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1247 } else
1248 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1250 if(avctx->request_channels == 2 && s->prim_channels > 2) {
1251 channels = 2;
1252 s->output = DCA_STEREO;
1253 avctx->channel_layout = CH_LAYOUT_STEREO;
1255 } else {
1256 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
1257 return -1;
1261 /* There is nothing that prevents a dts frame to change channel configuration
1262 but FFmpeg doesn't support that so only set the channels if it is previously
1263 unset. Ideally during the first probe for channels the crc should be checked
1264 and only set avctx->channels when the crc is ok. Right now the decoder could
1265 set the channels based on a broken first frame.*/
1266 if (!avctx->channels)
1267 avctx->channels = channels;
1269 if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
1270 return -1;
1271 *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
1272 for (i = 0; i < (s->sample_blocks / 8); i++) {
1273 dca_decode_block(s);
1274 s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
1275 samples += 256 * channels;
1278 return buf_size;
1284 * DCA initialization
1286 * @param avctx pointer to the AVCodecContext
1289 static av_cold int dca_decode_init(AVCodecContext * avctx)
1291 DCAContext *s = avctx->priv_data;
1292 int i;
1294 s->avctx = avctx;
1295 dca_init_vlcs();
1297 dsputil_init(&s->dsp, avctx);
1298 ff_mdct_init(&s->imdct, 6, 1);
1300 for(i = 0; i < 6; i++)
1301 s->samples_chanptr[i] = s->samples + i * 256;
1302 avctx->sample_fmt = SAMPLE_FMT_S16;
1304 if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
1305 s->add_bias = 385.0f;
1306 s->scale_bias = 1.0 / 32768.0;
1307 } else {
1308 s->add_bias = 0.0f;
1309 s->scale_bias = 1.0;
1311 /* allow downmixing to stereo */
1312 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1313 avctx->request_channels == 2) {
1314 avctx->channels = avctx->request_channels;
1319 return 0;
1322 static av_cold int dca_decode_end(AVCodecContext * avctx)
1324 DCAContext *s = avctx->priv_data;
1325 ff_mdct_end(&s->imdct);
1326 return 0;
1329 AVCodec dca_decoder = {
1330 .name = "dca",
1331 .type = CODEC_TYPE_AUDIO,
1332 .id = CODEC_ID_DTS,
1333 .priv_data_size = sizeof(DCAContext),
1334 .init = dca_decode_init,
1335 .decode = dca_decode_frame,
1336 .close = dca_decode_end,
1337 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),