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
35 #include "bitstream.h"
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)
62 #define DCA_DOLBY 101 /* FIXME */
64 #define DCA_CHANNEL_BITS 6
65 #define DCA_CHANNEL_MASK 0x3F
69 #define HEADER_SIZE 14
70 #define CONVERT_BIAS 384
72 #define DCA_MAX_FRAME_SIZE 16384
76 int offset; ///< code values offset
77 int maxbits[8]; ///< max bits in VLC
78 int wrap; ///< wrap for get_vlc2()
79 VLC vlc[8]; ///< actual codes
82 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
83 static BitAlloc dca_tmode; ///< transition mode VLCs
84 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
85 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
87 /** Pre-calculated cosine modulation coefs for the QMF */
88 static float cos_mod[544];
90 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
92 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
96 AVCodecContext *avctx;
98 int frame_type; ///< type of the current frame
99 int samples_deficit; ///< deficit sample count
100 int crc_present; ///< crc is present in the bitstream
101 int sample_blocks; ///< number of PCM sample blocks
102 int frame_size; ///< primary frame byte size
103 int amode; ///< audio channels arrangement
104 int sample_rate; ///< audio sampling rate
105 int bit_rate; ///< transmission bit rate
107 int downmix; ///< embedded downmix enabled
108 int dynrange; ///< embedded dynamic range flag
109 int timestamp; ///< embedded time stamp flag
110 int aux_data; ///< auxiliary data flag
111 int hdcd; ///< source material is mastered in HDCD
112 int ext_descr; ///< extension audio descriptor flag
113 int ext_coding; ///< extended coding flag
114 int aspf; ///< audio sync word insertion flag
115 int lfe; ///< low frequency effects flag
116 int predictor_history; ///< predictor history flag
117 int header_crc; ///< header crc check bytes
118 int multirate_inter; ///< multirate interpolator switch
119 int version; ///< encoder software revision
120 int copy_history; ///< copy history
121 int source_pcm_res; ///< source pcm resolution
122 int front_sum; ///< front sum/difference flag
123 int surround_sum; ///< surround sum/difference flag
124 int dialog_norm; ///< dialog normalisation parameter
126 /* Primary audio coding header */
127 int subframes; ///< number of subframes
128 int total_channels; ///< number of channels including extensions
129 int prim_channels; ///< number of primary audio channels
130 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
131 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
132 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
133 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
134 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
135 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
136 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
137 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
139 /* Primary audio coding side information */
140 int subsubframes; ///< number of subsubframes
141 int partial_samples; ///< partial subsubframe samples count
142 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
143 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
144 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
145 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
146 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
147 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
148 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
149 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
150 int dynrange_coef; ///< dynamic range coefficient
152 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
154 float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX *
155 2 /*history */ ]; ///< Low frequency effect data
156 int lfe_scale_factor;
158 /* Subband samples history (for ADPCM) */
159 float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
160 float subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512];
161 float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][32];
162 int hist_index[DCA_PRIM_CHANNELS_MAX];
164 int output; ///< type of output
165 int bias; ///< output bias
167 DECLARE_ALIGNED_16(float, samples[1536]); /* 6 * 256 = 1536, might only need 5 */
168 const float *samples_chanptr[6];
170 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
171 int dca_buffer_size; ///< how much data is in the dca_buffer
174 /* Current position in DCA frame */
175 int current_subframe;
176 int current_subsubframe;
178 int debug_flag; ///< used for suppressing repeated error messages output
182 static av_cold void dca_init_vlcs(void)
184 static int vlcs_initialized = 0;
187 if (vlcs_initialized)
190 dca_bitalloc_index.offset = 1;
191 dca_bitalloc_index.wrap = 2;
192 for (i = 0; i < 5; i++)
193 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
194 bitalloc_12_bits[i], 1, 1,
195 bitalloc_12_codes[i], 2, 2, 1);
196 dca_scalefactor.offset = -64;
197 dca_scalefactor.wrap = 2;
198 for (i = 0; i < 5; i++)
199 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
200 scales_bits[i], 1, 1,
201 scales_codes[i], 2, 2, 1);
202 dca_tmode.offset = 0;
204 for (i = 0; i < 4; i++)
205 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
207 tmode_codes[i], 2, 2, 1);
209 for(i = 0; i < 10; i++)
210 for(j = 0; j < 7; j++){
211 if(!bitalloc_codes[i][j]) break;
212 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
213 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
214 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
216 bitalloc_bits[i][j], 1, 1,
217 bitalloc_codes[i][j], 2, 2, 1);
219 vlcs_initialized = 1;
222 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
225 *dst++ = get_bits(gb, bits);
228 static int dca_parse_frame_header(DCAContext * s)
231 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
232 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
233 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
235 s->bias = CONVERT_BIAS;
237 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
240 get_bits(&s->gb, 32);
243 s->frame_type = get_bits(&s->gb, 1);
244 s->samples_deficit = get_bits(&s->gb, 5) + 1;
245 s->crc_present = get_bits(&s->gb, 1);
246 s->sample_blocks = get_bits(&s->gb, 7) + 1;
247 s->frame_size = get_bits(&s->gb, 14) + 1;
248 if (s->frame_size < 95)
250 s->amode = get_bits(&s->gb, 6);
251 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
254 s->bit_rate = dca_bit_rates[get_bits(&s->gb, 5)];
258 s->downmix = get_bits(&s->gb, 1);
259 s->dynrange = get_bits(&s->gb, 1);
260 s->timestamp = get_bits(&s->gb, 1);
261 s->aux_data = get_bits(&s->gb, 1);
262 s->hdcd = get_bits(&s->gb, 1);
263 s->ext_descr = get_bits(&s->gb, 3);
264 s->ext_coding = get_bits(&s->gb, 1);
265 s->aspf = get_bits(&s->gb, 1);
266 s->lfe = get_bits(&s->gb, 2);
267 s->predictor_history = get_bits(&s->gb, 1);
269 /* TODO: check CRC */
271 s->header_crc = get_bits(&s->gb, 16);
273 s->multirate_inter = get_bits(&s->gb, 1);
274 s->version = get_bits(&s->gb, 4);
275 s->copy_history = get_bits(&s->gb, 2);
276 s->source_pcm_res = get_bits(&s->gb, 3);
277 s->front_sum = get_bits(&s->gb, 1);
278 s->surround_sum = get_bits(&s->gb, 1);
279 s->dialog_norm = get_bits(&s->gb, 4);
281 /* FIXME: channels mixing levels */
282 s->output = s->amode;
283 if(s->lfe) s->output |= DCA_LFE;
286 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
287 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
288 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
289 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
290 s->sample_blocks, s->sample_blocks * 32);
291 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
292 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
293 s->amode, dca_channels[s->amode]);
294 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i (%i Hz)\n",
295 s->sample_rate, dca_sample_rates[s->sample_rate]);
296 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i (%i bits/s)\n",
297 s->bit_rate, dca_bit_rates[s->bit_rate]);
298 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
299 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
300 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
301 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
302 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
303 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
304 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
305 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
306 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
307 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
308 s->predictor_history);
309 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
310 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
312 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
313 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
314 av_log(s->avctx, AV_LOG_DEBUG,
315 "source pcm resolution: %i (%i bits/sample)\n",
316 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
317 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
318 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
319 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
320 av_log(s->avctx, AV_LOG_DEBUG, "\n");
323 /* Primary audio coding header */
324 s->subframes = get_bits(&s->gb, 4) + 1;
325 s->total_channels = get_bits(&s->gb, 3) + 1;
326 s->prim_channels = s->total_channels;
327 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
328 s->prim_channels = DCA_PRIM_CHANNELS_MAX; /* We only support DTS core */
331 for (i = 0; i < s->prim_channels; i++) {
332 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
333 if (s->subband_activity[i] > DCA_SUBBANDS)
334 s->subband_activity[i] = DCA_SUBBANDS;
336 for (i = 0; i < s->prim_channels; i++) {
337 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
338 if (s->vq_start_subband[i] > DCA_SUBBANDS)
339 s->vq_start_subband[i] = DCA_SUBBANDS;
341 get_array(&s->gb, s->joint_intensity, s->prim_channels, 3);
342 get_array(&s->gb, s->transient_huffman, s->prim_channels, 2);
343 get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3);
344 get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3);
346 /* Get codebooks quantization indexes */
347 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
348 for (j = 1; j < 11; j++)
349 for (i = 0; i < s->prim_channels; i++)
350 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
352 /* Get scale factor adjustment */
353 for (j = 0; j < 11; j++)
354 for (i = 0; i < s->prim_channels; i++)
355 s->scalefactor_adj[i][j] = 1;
357 for (j = 1; j < 11; j++)
358 for (i = 0; i < s->prim_channels; i++)
359 if (s->quant_index_huffman[i][j] < thr[j])
360 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
362 if (s->crc_present) {
363 /* Audio header CRC check */
364 get_bits(&s->gb, 16);
367 s->current_subframe = 0;
368 s->current_subsubframe = 0;
371 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
372 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
373 for(i = 0; i < s->prim_channels; i++){
374 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
375 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
376 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
377 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
378 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
379 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
380 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
381 for (j = 0; j < 11; j++)
382 av_log(s->avctx, AV_LOG_DEBUG, " %i",
383 s->quant_index_huffman[i][j]);
384 av_log(s->avctx, AV_LOG_DEBUG, "\n");
385 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
386 for (j = 0; j < 11; j++)
387 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
388 av_log(s->avctx, AV_LOG_DEBUG, "\n");
396 static inline int get_scale(GetBitContext *gb, int level, int value)
399 /* huffman encoded */
400 value += get_bitalloc(gb, &dca_scalefactor, level);
402 value = get_bits(gb, level + 1);
406 static int dca_subframe_header(DCAContext * s)
408 /* Primary audio coding side information */
411 s->subsubframes = get_bits(&s->gb, 2) + 1;
412 s->partial_samples = get_bits(&s->gb, 3);
413 for (j = 0; j < s->prim_channels; j++) {
414 for (k = 0; k < s->subband_activity[j]; k++)
415 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
418 /* Get prediction codebook */
419 for (j = 0; j < s->prim_channels; j++) {
420 for (k = 0; k < s->subband_activity[j]; k++) {
421 if (s->prediction_mode[j][k] > 0) {
422 /* (Prediction coefficient VQ address) */
423 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
428 /* Bit allocation index */
429 for (j = 0; j < s->prim_channels; j++) {
430 for (k = 0; k < s->vq_start_subband[j]; k++) {
431 if (s->bitalloc_huffman[j] == 6)
432 s->bitalloc[j][k] = get_bits(&s->gb, 5);
433 else if (s->bitalloc_huffman[j] == 5)
434 s->bitalloc[j][k] = get_bits(&s->gb, 4);
435 else if (s->bitalloc_huffman[j] == 7) {
436 av_log(s->avctx, AV_LOG_ERROR,
437 "Invalid bit allocation index\n");
441 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
444 if (s->bitalloc[j][k] > 26) {
445 // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
446 // j, k, s->bitalloc[j][k]);
452 /* Transition mode */
453 for (j = 0; j < s->prim_channels; j++) {
454 for (k = 0; k < s->subband_activity[j]; k++) {
455 s->transition_mode[j][k] = 0;
456 if (s->subsubframes > 1 &&
457 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
458 s->transition_mode[j][k] =
459 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
464 for (j = 0; j < s->prim_channels; j++) {
465 const uint32_t *scale_table;
468 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
470 if (s->scalefactor_huffman[j] == 6)
471 scale_table = scale_factor_quant7;
473 scale_table = scale_factor_quant6;
475 /* When huffman coded, only the difference is encoded */
478 for (k = 0; k < s->subband_activity[j]; k++) {
479 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
480 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
481 s->scale_factor[j][k][0] = scale_table[scale_sum];
484 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
485 /* Get second scale factor */
486 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
487 s->scale_factor[j][k][1] = scale_table[scale_sum];
492 /* Joint subband scale factor codebook select */
493 for (j = 0; j < s->prim_channels; j++) {
494 /* Transmitted only if joint subband coding enabled */
495 if (s->joint_intensity[j] > 0)
496 s->joint_huff[j] = get_bits(&s->gb, 3);
499 /* Scale factors for joint subband coding */
500 for (j = 0; j < s->prim_channels; j++) {
503 /* Transmitted only if joint subband coding enabled */
504 if (s->joint_intensity[j] > 0) {
506 source_channel = s->joint_intensity[j] - 1;
508 /* When huffman coded, only the difference is encoded
509 * (is this valid as well for joint scales ???) */
511 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
512 scale = get_scale(&s->gb, s->joint_huff[j], 0);
513 scale += 64; /* bias */
514 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
517 if (!s->debug_flag & 0x02) {
518 av_log(s->avctx, AV_LOG_DEBUG,
519 "Joint stereo coding not supported\n");
520 s->debug_flag |= 0x02;
525 /* Stereo downmix coefficients */
526 if (s->prim_channels > 2) {
528 for (j = 0; j < s->prim_channels; j++) {
529 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
530 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
533 int am = s->amode & DCA_CHANNEL_MASK;
534 for (j = 0; j < s->prim_channels; j++) {
535 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
536 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
541 /* Dynamic range coefficient */
543 s->dynrange_coef = get_bits(&s->gb, 8);
545 /* Side information CRC check word */
546 if (s->crc_present) {
547 get_bits(&s->gb, 16);
551 * Primary audio data arrays
554 /* VQ encoded high frequency subbands */
555 for (j = 0; j < s->prim_channels; j++)
556 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
557 /* 1 vector -> 32 samples */
558 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
560 /* Low frequency effect data */
563 int lfe_samples = 2 * s->lfe * s->subsubframes;
566 for (j = lfe_samples; j < lfe_samples * 2; j++) {
567 /* Signed 8 bits int */
568 s->lfe_data[j] = get_sbits(&s->gb, 8);
571 /* Scale factor index */
572 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
574 /* Quantization step size * scale factor */
575 lfe_scale = 0.035 * s->lfe_scale_factor;
577 for (j = lfe_samples; j < lfe_samples * 2; j++)
578 s->lfe_data[j] *= lfe_scale;
582 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes);
583 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
585 for (j = 0; j < s->prim_channels; j++) {
586 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
587 for (k = 0; k < s->subband_activity[j]; k++)
588 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
589 av_log(s->avctx, AV_LOG_DEBUG, "\n");
591 for (j = 0; j < s->prim_channels; j++) {
592 for (k = 0; k < s->subband_activity[j]; k++)
593 av_log(s->avctx, AV_LOG_DEBUG,
594 "prediction coefs: %f, %f, %f, %f\n",
595 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
596 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
597 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
598 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
600 for (j = 0; j < s->prim_channels; j++) {
601 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
602 for (k = 0; k < s->vq_start_subband[j]; k++)
603 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
604 av_log(s->avctx, AV_LOG_DEBUG, "\n");
606 for (j = 0; j < s->prim_channels; j++) {
607 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
608 for (k = 0; k < s->subband_activity[j]; k++)
609 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
610 av_log(s->avctx, AV_LOG_DEBUG, "\n");
612 for (j = 0; j < s->prim_channels; j++) {
613 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
614 for (k = 0; k < s->subband_activity[j]; k++) {
615 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
616 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
617 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
618 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
620 av_log(s->avctx, AV_LOG_DEBUG, "\n");
622 for (j = 0; j < s->prim_channels; j++) {
623 if (s->joint_intensity[j] > 0) {
624 int source_channel = s->joint_intensity[j] - 1;
625 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
626 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
627 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
628 av_log(s->avctx, AV_LOG_DEBUG, "\n");
631 if (s->prim_channels > 2 && s->downmix) {
632 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
633 for (j = 0; j < s->prim_channels; j++) {
634 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
635 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
637 av_log(s->avctx, AV_LOG_DEBUG, "\n");
639 for (j = 0; j < s->prim_channels; j++)
640 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
641 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
643 int lfe_samples = 2 * s->lfe * s->subsubframes;
644 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
645 for (j = lfe_samples; j < lfe_samples * 2; j++)
646 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
647 av_log(s->avctx, AV_LOG_DEBUG, "\n");
654 static void qmf_32_subbands(DCAContext * s, int chans,
655 float samples_in[32][8], float *samples_out,
656 float scale, float bias)
658 const float *prCoeff;
660 float praXin[33], *raXin = &praXin[1];
662 int hist_index= s->hist_index[chans];
663 float *subband_fir_hist2 = s->subband_fir_noidea[chans];
665 int chindex = 0, subindex;
670 if (!s->multirate_inter) /* Non-perfect reconstruction */
671 prCoeff = fir_32bands_nonperfect;
672 else /* Perfect reconstruction */
673 prCoeff = fir_32bands_perfect;
675 /* Reconstructed channel sample index */
676 for (subindex = 0; subindex < 8; subindex++) {
677 float *subband_fir_hist = s->subband_fir_hist[chans] + hist_index;
678 /* Load in one sample from each subband and clear inactive subbands */
679 for (i = 0; i < s->subband_activity[chans]; i++)
680 raXin[i] = samples_in[i][subindex];
684 /* Multiply by cosine modulation coefficients and
685 * create temporary arrays SUM and DIFF */
686 for (j = 0, k = 0; k < 16; k++) {
689 for (i = 0; i < 16; i++, j++){
690 t1 += (raXin[2 * i] + raXin[2 * i + 1]) * cos_mod[j];
691 t2 += (raXin[2 * i] + raXin[2 * i - 1]) * cos_mod[j + 256];
693 subband_fir_hist[ k ] = cos_mod[k+512 ] * (t1 + t2);
694 subband_fir_hist[32-k-1] = cos_mod[k+512+16] * (t1 - t2);
697 /* Multiply by filter coefficients */
698 for (k = 31, i = 0; i < 32; i++, k--){
699 float a= subband_fir_hist2[i];
701 for (j = 0; j < 512-hist_index; j += 64){
702 a += prCoeff[i+j ]*( subband_fir_hist[i+j] - subband_fir_hist[j+k]);
703 b += prCoeff[i+j+32]*(-subband_fir_hist[i+j] - subband_fir_hist[j+k]);
705 for ( ; j < 512; j += 64){
706 a += prCoeff[i+j ]*( subband_fir_hist[i+j-512] - subband_fir_hist[j+k-512]);
707 b += prCoeff[i+j+32]*(-subband_fir_hist[i+j-512] - subband_fir_hist[j+k-512]);
709 samples_out[chindex++] = a * scale + bias;
710 subband_fir_hist2[i] = b;
713 hist_index = (hist_index-32)&511;
715 s->hist_index[chans]= hist_index;
718 static void lfe_interpolation_fir(int decimation_select,
719 int num_deci_sample, float *samples_in,
720 float *samples_out, float scale,
723 /* samples_in: An array holding decimated samples.
724 * Samples in current subframe starts from samples_in[0],
725 * while samples_in[-1], samples_in[-2], ..., stores samples
726 * from last subframe as history.
728 * samples_out: An array holding interpolated samples
731 int decifactor, k, j;
732 const float *prCoeff;
734 int interp_index = 0; /* Index to the interpolated samples */
737 /* Select decimation filter */
738 if (decimation_select == 1) {
740 prCoeff = lfe_fir_128;
743 prCoeff = lfe_fir_64;
746 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
747 /* One decimated sample generates decifactor interpolated ones */
748 for (k = 0; k < decifactor; k++) {
750 //FIXME the coeffs are symetric, fix that
751 for (j = 0; j < 512 / decifactor; j++)
752 rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor];
753 samples_out[interp_index++] = rTmp / scale + bias;
758 /* downmixing routines */
759 #define MIX_REAR1(samples, si1, rs, coef) \
760 samples[i] += samples[si1] * coef[rs][0]; \
761 samples[i+256] += samples[si1] * coef[rs][1];
763 #define MIX_REAR2(samples, si1, si2, rs, coef) \
764 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
765 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
767 #define MIX_FRONT3(samples, coef) \
769 samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
770 samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
772 #define DOWNMIX_TO_STEREO(op1, op2) \
773 for(i = 0; i < 256; i++){ \
778 static void dca_downmix(float *samples, int srcfmt,
779 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
783 float coef[DCA_PRIM_CHANNELS_MAX][2];
785 for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
786 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
787 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
793 case DCA_STEREO_TOTAL:
794 case DCA_STEREO_SUMDIFF:
796 av_log(NULL, 0, "Not implemented!\n");
801 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
804 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
807 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
808 MIX_REAR1(samples, i + 768, 3, coef));
811 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
814 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
815 MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
821 /* Very compact version of the block code decoder that does not use table
822 * look-up but is slightly slower */
823 static int decode_blockcode(int code, int levels, int *values)
826 int offset = (levels - 1) >> 1;
828 for (i = 0; i < 4; i++) {
829 values[i] = (code % levels) - offset;
836 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
841 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
842 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
844 static int dca_subsubframe(DCAContext * s)
847 int subsubframe = s->current_subsubframe;
849 const float *quant_step_table;
852 float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
858 /* Select quantization step size table */
859 if (s->bit_rate == 0x1f)
860 quant_step_table = lossless_quant_d;
862 quant_step_table = lossy_quant_d;
864 for (k = 0; k < s->prim_channels; k++) {
865 for (l = 0; l < s->vq_start_subband[k]; l++) {
868 /* Select the mid-tread linear quantizer */
869 int abits = s->bitalloc[k][l];
871 float quant_step_size = quant_step_table[abits];
875 * Determine quantization index code book and its type
878 /* Select quantization index code book */
879 int sel = s->quant_index_huffman[k][abits];
882 * Extract bits from the bit stream
885 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
886 }else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
889 int block_code1, block_code2, size, levels;
892 size = abits_sizes[abits-1];
893 levels = abits_levels[abits-1];
895 block_code1 = get_bits(&s->gb, size);
896 /* FIXME Should test return value */
897 decode_blockcode(block_code1, levels, block);
898 block_code2 = get_bits(&s->gb, size);
899 decode_blockcode(block_code2, levels, &block[4]);
900 for (m = 0; m < 8; m++)
901 subband_samples[k][l][m] = block[m];
904 for (m = 0; m < 8; m++)
905 subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3);
909 for (m = 0; m < 8; m++)
910 subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
913 /* Deal with transients */
914 if (s->transition_mode[k][l] &&
915 subsubframe >= s->transition_mode[k][l])
916 rscale = quant_step_size * s->scale_factor[k][l][1];
918 rscale = quant_step_size * s->scale_factor[k][l][0];
920 rscale *= s->scalefactor_adj[k][sel];
922 for (m = 0; m < 8; m++)
923 subband_samples[k][l][m] *= rscale;
926 * Inverse ADPCM if in prediction mode
928 if (s->prediction_mode[k][l]) {
930 for (m = 0; m < 8; m++) {
931 for (n = 1; n <= 4; n++)
933 subband_samples[k][l][m] +=
934 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
935 subband_samples[k][l][m - n] / 8192);
936 else if (s->predictor_history)
937 subband_samples[k][l][m] +=
938 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
939 s->subband_samples_hist[k][l][m - n +
946 * Decode VQ encoded high frequencies
948 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
949 /* 1 vector -> 32 samples but we only need the 8 samples
950 * for this subsubframe. */
953 if (!s->debug_flag & 0x01) {
954 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
955 s->debug_flag |= 0x01;
958 for (m = 0; m < 8; m++) {
959 subband_samples[k][l][m] =
960 high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
962 * (float) s->scale_factor[k][l][0] / 16.0;
967 /* Check for DSYNC after subsubframe */
968 if (s->aspf || subsubframe == s->subsubframes - 1) {
969 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
971 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
974 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
978 /* Backup predictor history for adpcm */
979 for (k = 0; k < s->prim_channels; k++)
980 for (l = 0; l < s->vq_start_subband[k]; l++)
981 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
982 4 * sizeof(subband_samples[0][0][0]));
984 /* 32 subbands QMF */
985 for (k = 0; k < s->prim_channels; k++) {
986 /* static float pcm_to_double[8] =
987 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
988 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * k],
989 M_SQRT1_2 /*pcm_to_double[s->source_pcm_res] */ ,
995 if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) {
996 dca_downmix(s->samples, s->amode, s->downmix_coef);
999 /* Generate LFE samples for this subsubframe FIXME!!! */
1000 if (s->output & DCA_LFE) {
1001 int lfe_samples = 2 * s->lfe * s->subsubframes;
1002 int i_channels = dca_channels[s->output & DCA_CHANNEL_MASK];
1004 lfe_interpolation_fir(s->lfe, 2 * s->lfe,
1005 s->lfe_data + lfe_samples +
1006 2 * s->lfe * subsubframe,
1007 &s->samples[256 * i_channels],
1008 256.0, 0 /* s->bias */);
1009 /* Outputs 20bits pcm samples */
1016 static int dca_subframe_footer(DCAContext * s)
1018 int aux_data_count = 0, i;
1022 * Unpack optional information
1026 get_bits(&s->gb, 32);
1029 aux_data_count = get_bits(&s->gb, 6);
1031 for (i = 0; i < aux_data_count; i++)
1032 get_bits(&s->gb, 8);
1034 if (s->crc_present && (s->downmix || s->dynrange))
1035 get_bits(&s->gb, 16);
1037 lfe_samples = 2 * s->lfe * s->subsubframes;
1038 for (i = 0; i < lfe_samples; i++) {
1039 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1046 * Decode a dca frame block
1048 * @param s pointer to the DCAContext
1051 static int dca_decode_block(DCAContext * s)
1055 if (s->current_subframe >= s->subframes) {
1056 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1057 s->current_subframe, s->subframes);
1061 if (!s->current_subsubframe) {
1063 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1065 /* Read subframe header */
1066 if (dca_subframe_header(s))
1070 /* Read subsubframe */
1072 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1074 if (dca_subsubframe(s))
1078 s->current_subsubframe++;
1079 if (s->current_subsubframe >= s->subsubframes) {
1080 s->current_subsubframe = 0;
1081 s->current_subframe++;
1083 if (s->current_subframe >= s->subframes) {
1085 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1087 /* Read subframe footer */
1088 if (dca_subframe_footer(s))
1096 * Convert bitstream to one representation based on sync marker
1098 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
1103 const uint16_t *ssrc = (const uint16_t *) src;
1104 uint16_t *sdst = (uint16_t *) dst;
1107 if((unsigned)src_size > (unsigned)max_size) {
1108 av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
1114 case DCA_MARKER_RAW_BE:
1115 memcpy(dst, src, src_size);
1117 case DCA_MARKER_RAW_LE:
1118 for (i = 0; i < (src_size + 1) >> 1; i++)
1119 *sdst++ = bswap_16(*ssrc++);
1121 case DCA_MARKER_14B_BE:
1122 case DCA_MARKER_14B_LE:
1123 init_put_bits(&pb, dst, max_size);
1124 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1125 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1126 put_bits(&pb, 14, tmp);
1128 flush_put_bits(&pb);
1129 return (put_bits_count(&pb) + 7) >> 3;
1136 * Main frame decoding function
1137 * FIXME add arguments
1139 static int dca_decode_frame(AVCodecContext * avctx,
1140 void *data, int *data_size,
1141 const uint8_t * buf, int buf_size)
1145 int16_t *samples = data;
1146 DCAContext *s = avctx->priv_data;
1150 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
1151 if (s->dca_buffer_size == -1) {
1152 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1156 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1157 if (dca_parse_frame_header(s) < 0) {
1158 //seems like the frame is corrupt, try with the next one
1162 //set AVCodec values with parsed data
1163 avctx->sample_rate = s->sample_rate;
1164 avctx->bit_rate = s->bit_rate;
1166 channels = s->prim_channels + !!s->lfe;
1167 if(avctx->request_channels == 2 && s->prim_channels > 2) {
1169 s->output = DCA_STEREO;
1172 /* There is nothing that prevents a dts frame to change channel configuration
1173 but FFmpeg doesn't support that so only set the channels if it is previously
1174 unset. Ideally during the first probe for channels the crc should be checked
1175 and only set avctx->channels when the crc is ok. Right now the decoder could
1176 set the channels based on a broken first frame.*/
1177 if (!avctx->channels)
1178 avctx->channels = channels;
1180 if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
1182 *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
1183 for (i = 0; i < (s->sample_blocks / 8); i++) {
1184 dca_decode_block(s);
1185 s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
1186 samples += 256 * channels;
1195 * Build the cosine modulation tables for the QMF
1197 * @param s pointer to the DCAContext
1200 static av_cold void pre_calc_cosmod(DCAContext * s)
1203 static int cosmod_initialized = 0;
1205 if(cosmod_initialized) return;
1206 for (j = 0, k = 0; k < 16; k++)
1207 for (i = 0; i < 16; i++)
1208 cos_mod[j++] = cos((2 * i + 1) * (2 * k + 1) * M_PI / 64);
1210 for (k = 0; k < 16; k++)
1211 for (i = 0; i < 16; i++)
1212 cos_mod[j++] = cos((i) * (2 * k + 1) * M_PI / 32);
1214 for (k = 0; k < 16; k++)
1215 cos_mod[j++] = 0.25 / (2 * cos((2 * k + 1) * M_PI / 128));
1217 for (k = 0; k < 16; k++)
1218 cos_mod[j++] = -0.25 / (2.0 * sin((2 * k + 1) * M_PI / 128));
1220 cosmod_initialized = 1;
1225 * DCA initialization
1227 * @param avctx pointer to the AVCodecContext
1230 static av_cold int dca_decode_init(AVCodecContext * avctx)
1232 DCAContext *s = avctx->priv_data;
1239 dsputil_init(&s->dsp, avctx);
1241 /* allow downmixing to stereo */
1242 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1243 avctx->request_channels == 2) {
1244 avctx->channels = avctx->request_channels;
1246 for(i = 0; i < 6; i++)
1247 s->samples_chanptr[i] = s->samples + i * 256;
1248 avctx->sample_fmt = SAMPLE_FMT_S16;
1253 AVCodec dca_decoder = {
1255 .type = CODEC_TYPE_AUDIO,
1257 .priv_data_size = sizeof(DCAContext),
1258 .init = dca_decode_init,
1259 .decode = dca_decode_frame,
1260 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),