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
29 #include "libavutil/intmath.h"
30 #include "libavutil/intreadwrite.h"
39 #include "synth_filter.h"
44 #define DCA_PRIM_CHANNELS_MAX (5)
45 #define DCA_SUBBANDS (32)
46 #define DCA_ABITS_MAX (32) /* Should be 28 */
47 #define DCA_SUBSUBFAMES_MAX (4)
48 #define DCA_LFE_MAX (3)
64 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
65 * Some compromises have been made for special configurations. Most configurations
66 * are never used so complete accuracy is not needed.
68 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
69 * S -> side, when both rear and back are configured move one of them to the side channel
71 * All 2 channel configurations -> CH_LAYOUT_STEREO
74 static const int64_t dca_core_channel_layout[] = {
75 CH_FRONT_CENTER, ///< 1, A
76 CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
77 CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
78 CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference)
79 CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total)
80 CH_LAYOUT_STEREO|CH_FRONT_CENTER, ///< 3, C+L+R
81 CH_LAYOUT_STEREO|CH_BACK_CENTER, ///< 3, L+R+S
82 CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 4, C + L + R+ S
83 CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR
84 CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
85 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
86 CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 6, C + L + R+ LR + RR + OV
87 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
88 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
89 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
90 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
93 static const int8_t dca_lfe_index[] = {
94 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
97 static const int8_t dca_channel_reorder_lfe[][8] = {
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 { 0, 1, -1, -1, -1, -1, -1, -1},
102 { 0, 1, -1, -1, -1, -1, -1, -1},
103 { 2, 0, 1, -1, -1, -1, -1, -1},
104 { 0, 1, 3, -1, -1, -1, -1, -1},
105 { 2, 0, 1, 4, -1, -1, -1, -1},
106 { 0, 1, 3, 4, -1, -1, -1, -1},
107 { 2, 0, 1, 4, 5, -1, -1, -1},
108 { 3, 4, 0, 1, 5, 6, -1, -1},
109 { 2, 0, 1, 4, 5, 6, -1, -1},
110 { 0, 6, 4, 5, 2, 3, -1, -1},
111 { 4, 2, 5, 0, 1, 6, 7, -1},
112 { 5, 6, 0, 1, 7, 3, 8, 4},
113 { 4, 2, 5, 0, 1, 6, 8, 7},
116 static const int8_t dca_channel_reorder_nolfe[][8] = {
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 { 0, 1, -1, -1, -1, -1, -1, -1},
121 { 0, 1, -1, -1, -1, -1, -1, -1},
122 { 2, 0, 1, -1, -1, -1, -1, -1},
123 { 0, 1, 2, -1, -1, -1, -1, -1},
124 { 2, 0, 1, 3, -1, -1, -1, -1},
125 { 0, 1, 2, 3, -1, -1, -1, -1},
126 { 2, 0, 1, 3, 4, -1, -1, -1},
127 { 2, 3, 0, 1, 4, 5, -1, -1},
128 { 2, 0, 1, 3, 4, 5, -1, -1},
129 { 0, 5, 3, 4, 1, 2, -1, -1},
130 { 3, 2, 4, 0, 1, 5, 6, -1},
131 { 4, 5, 0, 1, 6, 2, 7, 3},
132 { 3, 2, 4, 0, 1, 5, 7, 6},
136 #define DCA_DOLBY 101 /* FIXME */
138 #define DCA_CHANNEL_BITS 6
139 #define DCA_CHANNEL_MASK 0x3F
143 #define HEADER_SIZE 14
145 #define DCA_MAX_FRAME_SIZE 16384
147 /** Bit allocation */
149 int offset; ///< code values offset
150 int maxbits[8]; ///< max bits in VLC
151 int wrap; ///< wrap for get_vlc2()
152 VLC vlc[8]; ///< actual codes
155 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
156 static BitAlloc dca_tmode; ///< transition mode VLCs
157 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
158 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
160 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
162 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
166 AVCodecContext *avctx;
168 int frame_type; ///< type of the current frame
169 int samples_deficit; ///< deficit sample count
170 int crc_present; ///< crc is present in the bitstream
171 int sample_blocks; ///< number of PCM sample blocks
172 int frame_size; ///< primary frame byte size
173 int amode; ///< audio channels arrangement
174 int sample_rate; ///< audio sampling rate
175 int bit_rate; ///< transmission bit rate
176 int bit_rate_index; ///< transmission bit rate index
178 int downmix; ///< embedded downmix enabled
179 int dynrange; ///< embedded dynamic range flag
180 int timestamp; ///< embedded time stamp flag
181 int aux_data; ///< auxiliary data flag
182 int hdcd; ///< source material is mastered in HDCD
183 int ext_descr; ///< extension audio descriptor flag
184 int ext_coding; ///< extended coding flag
185 int aspf; ///< audio sync word insertion flag
186 int lfe; ///< low frequency effects flag
187 int predictor_history; ///< predictor history flag
188 int header_crc; ///< header crc check bytes
189 int multirate_inter; ///< multirate interpolator switch
190 int version; ///< encoder software revision
191 int copy_history; ///< copy history
192 int source_pcm_res; ///< source pcm resolution
193 int front_sum; ///< front sum/difference flag
194 int surround_sum; ///< surround sum/difference flag
195 int dialog_norm; ///< dialog normalisation parameter
197 /* Primary audio coding header */
198 int subframes; ///< number of subframes
199 int total_channels; ///< number of channels including extensions
200 int prim_channels; ///< number of primary audio channels
201 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
202 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
203 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
204 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
205 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
206 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
207 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
208 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
210 /* Primary audio coding side information */
211 int subsubframes; ///< number of subsubframes
212 int partial_samples; ///< partial subsubframe samples count
213 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
214 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
215 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
216 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
217 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
218 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
219 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
220 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
221 int dynrange_coef; ///< dynamic range coefficient
223 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
225 float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX *
226 2 /*history */ ]; ///< Low frequency effect data
227 int lfe_scale_factor;
229 /* Subband samples history (for ADPCM) */
230 float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
231 DECLARE_ALIGNED(16, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
232 DECLARE_ALIGNED(16, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
233 int hist_index[DCA_PRIM_CHANNELS_MAX];
234 DECLARE_ALIGNED(16, float, raXin)[32];
236 int output; ///< type of output
237 float add_bias; ///< output bias
238 float scale_bias; ///< output scale
240 DECLARE_ALIGNED(16, float, samples)[1536]; /* 6 * 256 = 1536, might only need 5 */
241 const float *samples_chanptr[6];
243 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
244 int dca_buffer_size; ///< how much data is in the dca_buffer
246 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe
248 /* Current position in DCA frame */
249 int current_subframe;
250 int current_subsubframe;
252 int debug_flag; ///< used for suppressing repeated error messages output
255 SynthFilterContext synth;
256 DCADSPContext dcadsp;
259 static const uint16_t dca_vlc_offs[] = {
260 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
261 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
262 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
263 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
264 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
265 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
268 static av_cold void dca_init_vlcs(void)
270 static int vlcs_initialized = 0;
272 static VLC_TYPE dca_table[23622][2];
274 if (vlcs_initialized)
277 dca_bitalloc_index.offset = 1;
278 dca_bitalloc_index.wrap = 2;
279 for (i = 0; i < 5; i++) {
280 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
281 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
282 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
283 bitalloc_12_bits[i], 1, 1,
284 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
286 dca_scalefactor.offset = -64;
287 dca_scalefactor.wrap = 2;
288 for (i = 0; i < 5; i++) {
289 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
290 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
291 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
292 scales_bits[i], 1, 1,
293 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
295 dca_tmode.offset = 0;
297 for (i = 0; i < 4; i++) {
298 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
299 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
300 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
302 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
305 for(i = 0; i < 10; i++)
306 for(j = 0; j < 7; j++){
307 if(!bitalloc_codes[i][j]) break;
308 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
309 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
310 dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
311 dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
312 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
314 bitalloc_bits[i][j], 1, 1,
315 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
318 vlcs_initialized = 1;
321 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
324 *dst++ = get_bits(gb, bits);
327 static int dca_parse_frame_header(DCAContext * s)
330 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
331 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
332 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
334 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
337 get_bits(&s->gb, 32);
340 s->frame_type = get_bits(&s->gb, 1);
341 s->samples_deficit = get_bits(&s->gb, 5) + 1;
342 s->crc_present = get_bits(&s->gb, 1);
343 s->sample_blocks = get_bits(&s->gb, 7) + 1;
344 s->frame_size = get_bits(&s->gb, 14) + 1;
345 if (s->frame_size < 95)
347 s->amode = get_bits(&s->gb, 6);
348 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
351 s->bit_rate_index = get_bits(&s->gb, 5);
352 s->bit_rate = dca_bit_rates[s->bit_rate_index];
356 s->downmix = get_bits(&s->gb, 1);
357 s->dynrange = get_bits(&s->gb, 1);
358 s->timestamp = get_bits(&s->gb, 1);
359 s->aux_data = get_bits(&s->gb, 1);
360 s->hdcd = get_bits(&s->gb, 1);
361 s->ext_descr = get_bits(&s->gb, 3);
362 s->ext_coding = get_bits(&s->gb, 1);
363 s->aspf = get_bits(&s->gb, 1);
364 s->lfe = get_bits(&s->gb, 2);
365 s->predictor_history = get_bits(&s->gb, 1);
367 /* TODO: check CRC */
369 s->header_crc = get_bits(&s->gb, 16);
371 s->multirate_inter = get_bits(&s->gb, 1);
372 s->version = get_bits(&s->gb, 4);
373 s->copy_history = get_bits(&s->gb, 2);
374 s->source_pcm_res = get_bits(&s->gb, 3);
375 s->front_sum = get_bits(&s->gb, 1);
376 s->surround_sum = get_bits(&s->gb, 1);
377 s->dialog_norm = get_bits(&s->gb, 4);
379 /* FIXME: channels mixing levels */
380 s->output = s->amode;
381 if(s->lfe) s->output |= DCA_LFE;
384 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
385 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
386 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
387 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
388 s->sample_blocks, s->sample_blocks * 32);
389 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
390 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
391 s->amode, dca_channels[s->amode]);
392 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
394 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
396 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
397 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
398 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
399 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
400 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
401 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
402 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
403 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
404 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
405 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
406 s->predictor_history);
407 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
408 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
410 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
411 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
412 av_log(s->avctx, AV_LOG_DEBUG,
413 "source pcm resolution: %i (%i bits/sample)\n",
414 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
415 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
416 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
417 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
418 av_log(s->avctx, AV_LOG_DEBUG, "\n");
421 /* Primary audio coding header */
422 s->subframes = get_bits(&s->gb, 4) + 1;
423 s->total_channels = get_bits(&s->gb, 3) + 1;
424 s->prim_channels = s->total_channels;
425 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
426 s->prim_channels = DCA_PRIM_CHANNELS_MAX; /* We only support DTS core */
429 for (i = 0; i < s->prim_channels; i++) {
430 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
431 if (s->subband_activity[i] > DCA_SUBBANDS)
432 s->subband_activity[i] = DCA_SUBBANDS;
434 for (i = 0; i < s->prim_channels; i++) {
435 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
436 if (s->vq_start_subband[i] > DCA_SUBBANDS)
437 s->vq_start_subband[i] = DCA_SUBBANDS;
439 get_array(&s->gb, s->joint_intensity, s->prim_channels, 3);
440 get_array(&s->gb, s->transient_huffman, s->prim_channels, 2);
441 get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3);
442 get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3);
444 /* Get codebooks quantization indexes */
445 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
446 for (j = 1; j < 11; j++)
447 for (i = 0; i < s->prim_channels; i++)
448 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
450 /* Get scale factor adjustment */
451 for (j = 0; j < 11; j++)
452 for (i = 0; i < s->prim_channels; i++)
453 s->scalefactor_adj[i][j] = 1;
455 for (j = 1; j < 11; j++)
456 for (i = 0; i < s->prim_channels; i++)
457 if (s->quant_index_huffman[i][j] < thr[j])
458 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
460 if (s->crc_present) {
461 /* Audio header CRC check */
462 get_bits(&s->gb, 16);
465 s->current_subframe = 0;
466 s->current_subsubframe = 0;
469 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
470 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
471 for(i = 0; i < s->prim_channels; i++){
472 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
473 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
474 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
475 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
476 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
477 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
478 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
479 for (j = 0; j < 11; j++)
480 av_log(s->avctx, AV_LOG_DEBUG, " %i",
481 s->quant_index_huffman[i][j]);
482 av_log(s->avctx, AV_LOG_DEBUG, "\n");
483 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
484 for (j = 0; j < 11; j++)
485 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
486 av_log(s->avctx, AV_LOG_DEBUG, "\n");
494 static inline int get_scale(GetBitContext *gb, int level, int value)
497 /* huffman encoded */
498 value += get_bitalloc(gb, &dca_scalefactor, level);
500 value = get_bits(gb, level + 1);
504 static int dca_subframe_header(DCAContext * s)
506 /* Primary audio coding side information */
509 s->subsubframes = get_bits(&s->gb, 2) + 1;
510 s->partial_samples = get_bits(&s->gb, 3);
511 for (j = 0; j < s->prim_channels; j++) {
512 for (k = 0; k < s->subband_activity[j]; k++)
513 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
516 /* Get prediction codebook */
517 for (j = 0; j < s->prim_channels; j++) {
518 for (k = 0; k < s->subband_activity[j]; k++) {
519 if (s->prediction_mode[j][k] > 0) {
520 /* (Prediction coefficient VQ address) */
521 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
526 /* Bit allocation index */
527 for (j = 0; j < s->prim_channels; j++) {
528 for (k = 0; k < s->vq_start_subband[j]; k++) {
529 if (s->bitalloc_huffman[j] == 6)
530 s->bitalloc[j][k] = get_bits(&s->gb, 5);
531 else if (s->bitalloc_huffman[j] == 5)
532 s->bitalloc[j][k] = get_bits(&s->gb, 4);
533 else if (s->bitalloc_huffman[j] == 7) {
534 av_log(s->avctx, AV_LOG_ERROR,
535 "Invalid bit allocation index\n");
539 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
542 if (s->bitalloc[j][k] > 26) {
543 // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
544 // j, k, s->bitalloc[j][k]);
550 /* Transition mode */
551 for (j = 0; j < s->prim_channels; j++) {
552 for (k = 0; k < s->subband_activity[j]; k++) {
553 s->transition_mode[j][k] = 0;
554 if (s->subsubframes > 1 &&
555 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
556 s->transition_mode[j][k] =
557 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
562 for (j = 0; j < s->prim_channels; j++) {
563 const uint32_t *scale_table;
566 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
568 if (s->scalefactor_huffman[j] == 6)
569 scale_table = scale_factor_quant7;
571 scale_table = scale_factor_quant6;
573 /* When huffman coded, only the difference is encoded */
576 for (k = 0; k < s->subband_activity[j]; k++) {
577 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
578 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
579 s->scale_factor[j][k][0] = scale_table[scale_sum];
582 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
583 /* Get second scale factor */
584 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
585 s->scale_factor[j][k][1] = scale_table[scale_sum];
590 /* Joint subband scale factor codebook select */
591 for (j = 0; j < s->prim_channels; j++) {
592 /* Transmitted only if joint subband coding enabled */
593 if (s->joint_intensity[j] > 0)
594 s->joint_huff[j] = get_bits(&s->gb, 3);
597 /* Scale factors for joint subband coding */
598 for (j = 0; j < s->prim_channels; j++) {
601 /* Transmitted only if joint subband coding enabled */
602 if (s->joint_intensity[j] > 0) {
604 source_channel = s->joint_intensity[j] - 1;
606 /* When huffman coded, only the difference is encoded
607 * (is this valid as well for joint scales ???) */
609 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
610 scale = get_scale(&s->gb, s->joint_huff[j], 0);
611 scale += 64; /* bias */
612 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
615 if (!(s->debug_flag & 0x02)) {
616 av_log(s->avctx, AV_LOG_DEBUG,
617 "Joint stereo coding not supported\n");
618 s->debug_flag |= 0x02;
623 /* Stereo downmix coefficients */
624 if (s->prim_channels > 2) {
626 for (j = 0; j < s->prim_channels; j++) {
627 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
628 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
631 int am = s->amode & DCA_CHANNEL_MASK;
632 for (j = 0; j < s->prim_channels; j++) {
633 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
634 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
639 /* Dynamic range coefficient */
641 s->dynrange_coef = get_bits(&s->gb, 8);
643 /* Side information CRC check word */
644 if (s->crc_present) {
645 get_bits(&s->gb, 16);
649 * Primary audio data arrays
652 /* VQ encoded high frequency subbands */
653 for (j = 0; j < s->prim_channels; j++)
654 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
655 /* 1 vector -> 32 samples */
656 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
658 /* Low frequency effect data */
661 int lfe_samples = 2 * s->lfe * s->subsubframes;
664 for (j = lfe_samples; j < lfe_samples * 2; j++) {
665 /* Signed 8 bits int */
666 s->lfe_data[j] = get_sbits(&s->gb, 8);
669 /* Scale factor index */
670 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
672 /* Quantization step size * scale factor */
673 lfe_scale = 0.035 * s->lfe_scale_factor;
675 for (j = lfe_samples; j < lfe_samples * 2; j++)
676 s->lfe_data[j] *= lfe_scale;
680 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes);
681 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
683 for (j = 0; j < s->prim_channels; j++) {
684 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
685 for (k = 0; k < s->subband_activity[j]; k++)
686 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
687 av_log(s->avctx, AV_LOG_DEBUG, "\n");
689 for (j = 0; j < s->prim_channels; j++) {
690 for (k = 0; k < s->subband_activity[j]; k++)
691 av_log(s->avctx, AV_LOG_DEBUG,
692 "prediction coefs: %f, %f, %f, %f\n",
693 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
694 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
695 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
696 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
698 for (j = 0; j < s->prim_channels; j++) {
699 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
700 for (k = 0; k < s->vq_start_subband[j]; k++)
701 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
702 av_log(s->avctx, AV_LOG_DEBUG, "\n");
704 for (j = 0; j < s->prim_channels; j++) {
705 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
706 for (k = 0; k < s->subband_activity[j]; k++)
707 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
708 av_log(s->avctx, AV_LOG_DEBUG, "\n");
710 for (j = 0; j < s->prim_channels; j++) {
711 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
712 for (k = 0; k < s->subband_activity[j]; k++) {
713 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
714 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
715 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
716 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
718 av_log(s->avctx, AV_LOG_DEBUG, "\n");
720 for (j = 0; j < s->prim_channels; j++) {
721 if (s->joint_intensity[j] > 0) {
722 int source_channel = s->joint_intensity[j] - 1;
723 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
724 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
725 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
726 av_log(s->avctx, AV_LOG_DEBUG, "\n");
729 if (s->prim_channels > 2 && s->downmix) {
730 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
731 for (j = 0; j < s->prim_channels; j++) {
732 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
733 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
735 av_log(s->avctx, AV_LOG_DEBUG, "\n");
737 for (j = 0; j < s->prim_channels; j++)
738 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
739 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
741 int lfe_samples = 2 * s->lfe * s->subsubframes;
742 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
743 for (j = lfe_samples; j < lfe_samples * 2; j++)
744 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
745 av_log(s->avctx, AV_LOG_DEBUG, "\n");
752 static void qmf_32_subbands(DCAContext * s, int chans,
753 float samples_in[32][8], float *samples_out,
754 float scale, float bias)
756 const float *prCoeff;
759 int sb_act = s->subband_activity[chans];
762 scale *= sqrt(1/8.0);
765 if (!s->multirate_inter) /* Non-perfect reconstruction */
766 prCoeff = fir_32bands_nonperfect;
767 else /* Perfect reconstruction */
768 prCoeff = fir_32bands_perfect;
770 /* Reconstructed channel sample index */
771 for (subindex = 0; subindex < 8; subindex++) {
772 /* Load in one sample from each subband and clear inactive subbands */
773 for (i = 0; i < sb_act; i++){
774 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ ((i-1)&2)<<30;
775 AV_WN32A(&s->raXin[i], v);
780 s->synth.synth_filter_float(&s->imdct,
781 s->subband_fir_hist[chans], &s->hist_index[chans],
782 s->subband_fir_noidea[chans], prCoeff,
783 samples_out, s->raXin, scale, bias);
789 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
790 int num_deci_sample, float *samples_in,
791 float *samples_out, float scale,
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
803 const float *prCoeff;
806 /* Select decimation filter */
807 if (decimation_select == 1) {
809 prCoeff = lfe_fir_128;
812 prCoeff = lfe_fir_64;
815 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
816 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
819 samples_out += 2 * decifactor;
823 /* downmixing routines */
824 #define MIX_REAR1(samples, si1, rs, coef) \
825 samples[i] += samples[si1] * coef[rs][0]; \
826 samples[i+256] += samples[si1] * coef[rs][1];
828 #define MIX_REAR2(samples, si1, si2, rs, coef) \
829 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
830 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
832 #define MIX_FRONT3(samples, coef) \
834 samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
835 samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
837 #define DOWNMIX_TO_STEREO(op1, op2) \
838 for(i = 0; i < 256; i++){ \
843 static void dca_downmix(float *samples, int srcfmt,
844 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
848 float coef[DCA_PRIM_CHANNELS_MAX][2];
850 for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
851 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
852 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
858 case DCA_STEREO_TOTAL:
859 case DCA_STEREO_SUMDIFF:
861 av_log(NULL, 0, "Not implemented!\n");
866 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
869 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
872 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
873 MIX_REAR1(samples, i + 768, 3, coef));
876 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
879 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
880 MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
886 /* Very compact version of the block code decoder that does not use table
887 * look-up but is slightly slower */
888 static int decode_blockcode(int code, int levels, int *values)
891 int offset = (levels - 1) >> 1;
893 for (i = 0; i < 4; i++) {
894 int div = FASTDIV(code, levels);
895 values[i] = code - offset - div*levels;
902 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
907 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
908 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
910 static int dca_subsubframe(DCAContext * s)
913 int subsubframe = s->current_subsubframe;
915 const float *quant_step_table;
918 LOCAL_ALIGNED_16(float, subband_samples, [DCA_PRIM_CHANNELS_MAX], [DCA_SUBBANDS][8]);
919 LOCAL_ALIGNED_16(int, block, [8]);
925 /* Select quantization step size table */
926 if (s->bit_rate_index == 0x1f)
927 quant_step_table = lossless_quant_d;
929 quant_step_table = lossy_quant_d;
931 for (k = 0; k < s->prim_channels; k++) {
932 for (l = 0; l < s->vq_start_subband[k]; l++) {
935 /* Select the mid-tread linear quantizer */
936 int abits = s->bitalloc[k][l];
938 float quant_step_size = quant_step_table[abits];
941 * Determine quantization index code book and its type
944 /* Select quantization index code book */
945 int sel = s->quant_index_huffman[k][abits];
948 * Extract bits from the bit stream
951 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
953 /* Deal with transients */
954 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
955 float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
957 if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
960 int block_code1, block_code2, size, levels;
962 size = abits_sizes[abits-1];
963 levels = abits_levels[abits-1];
965 block_code1 = get_bits(&s->gb, size);
966 /* FIXME Should test return value */
967 decode_blockcode(block_code1, levels, block);
968 block_code2 = get_bits(&s->gb, size);
969 decode_blockcode(block_code2, levels, &block[4]);
972 for (m = 0; m < 8; m++)
973 block[m] = get_sbits(&s->gb, abits - 3);
977 for (m = 0; m < 8; m++)
978 block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
981 s->dsp.int32_to_float_fmul_scalar(subband_samples[k][l],
986 * Inverse ADPCM if in prediction mode
988 if (s->prediction_mode[k][l]) {
990 for (m = 0; m < 8; m++) {
991 for (n = 1; n <= 4; n++)
993 subband_samples[k][l][m] +=
994 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
995 subband_samples[k][l][m - n] / 8192);
996 else if (s->predictor_history)
997 subband_samples[k][l][m] +=
998 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
999 s->subband_samples_hist[k][l][m - n +
1006 * Decode VQ encoded high frequencies
1008 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1009 /* 1 vector -> 32 samples but we only need the 8 samples
1010 * for this subsubframe. */
1013 if (!s->debug_flag & 0x01) {
1014 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
1015 s->debug_flag |= 0x01;
1018 for (m = 0; m < 8; m++) {
1019 subband_samples[k][l][m] =
1020 high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
1022 * (float) s->scale_factor[k][l][0] / 16.0;
1027 /* Check for DSYNC after subsubframe */
1028 if (s->aspf || subsubframe == s->subsubframes - 1) {
1029 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1031 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1034 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1038 /* Backup predictor history for adpcm */
1039 for (k = 0; k < s->prim_channels; k++)
1040 for (l = 0; l < s->vq_start_subband[k]; l++)
1041 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
1042 4 * sizeof(subband_samples[0][0][0]));
1044 /* 32 subbands QMF */
1045 for (k = 0; k < s->prim_channels; k++) {
1046 /* static float pcm_to_double[8] =
1047 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
1048 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
1049 M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
1055 if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) {
1056 dca_downmix(s->samples, s->amode, s->downmix_coef);
1059 /* Generate LFE samples for this subsubframe FIXME!!! */
1060 if (s->output & DCA_LFE) {
1061 int lfe_samples = 2 * s->lfe * s->subsubframes;
1063 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1064 s->lfe_data + lfe_samples +
1065 2 * s->lfe * subsubframe,
1066 &s->samples[256 * dca_lfe_index[s->amode]],
1067 (1.0/256.0)*s->scale_bias, s->add_bias);
1068 /* Outputs 20bits pcm samples */
1075 static int dca_subframe_footer(DCAContext * s)
1077 int aux_data_count = 0, i;
1081 * Unpack optional information
1085 get_bits(&s->gb, 32);
1088 aux_data_count = get_bits(&s->gb, 6);
1090 for (i = 0; i < aux_data_count; i++)
1091 get_bits(&s->gb, 8);
1093 if (s->crc_present && (s->downmix || s->dynrange))
1094 get_bits(&s->gb, 16);
1096 lfe_samples = 2 * s->lfe * s->subsubframes;
1097 for (i = 0; i < lfe_samples; i++) {
1098 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1105 * Decode a dca frame block
1107 * @param s pointer to the DCAContext
1110 static int dca_decode_block(DCAContext * s)
1114 if (s->current_subframe >= s->subframes) {
1115 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1116 s->current_subframe, s->subframes);
1120 if (!s->current_subsubframe) {
1122 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1124 /* Read subframe header */
1125 if (dca_subframe_header(s))
1129 /* Read subsubframe */
1131 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1133 if (dca_subsubframe(s))
1137 s->current_subsubframe++;
1138 if (s->current_subsubframe >= s->subsubframes) {
1139 s->current_subsubframe = 0;
1140 s->current_subframe++;
1142 if (s->current_subframe >= s->subframes) {
1144 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1146 /* Read subframe footer */
1147 if (dca_subframe_footer(s))
1155 * Convert bitstream to one representation based on sync marker
1157 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
1162 const uint16_t *ssrc = (const uint16_t *) src;
1163 uint16_t *sdst = (uint16_t *) dst;
1166 if((unsigned)src_size > (unsigned)max_size) {
1167 // av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
1169 src_size = max_size;
1174 case DCA_MARKER_RAW_BE:
1175 memcpy(dst, src, src_size);
1177 case DCA_MARKER_RAW_LE:
1178 for (i = 0; i < (src_size + 1) >> 1; i++)
1179 *sdst++ = bswap_16(*ssrc++);
1181 case DCA_MARKER_14B_BE:
1182 case DCA_MARKER_14B_LE:
1183 init_put_bits(&pb, dst, max_size);
1184 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1185 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1186 put_bits(&pb, 14, tmp);
1188 flush_put_bits(&pb);
1189 return (put_bits_count(&pb) + 7) >> 3;
1196 * Main frame decoding function
1197 * FIXME add arguments
1199 static int dca_decode_frame(AVCodecContext * avctx,
1200 void *data, int *data_size,
1203 const uint8_t *buf = avpkt->data;
1204 int buf_size = avpkt->size;
1207 int16_t *samples = data;
1208 DCAContext *s = avctx->priv_data;
1212 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
1213 if (s->dca_buffer_size == -1) {
1214 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1218 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1219 if (dca_parse_frame_header(s) < 0) {
1220 //seems like the frame is corrupt, try with the next one
1224 //set AVCodec values with parsed data
1225 avctx->sample_rate = s->sample_rate;
1226 avctx->bit_rate = s->bit_rate;
1228 channels = s->prim_channels + !!s->lfe;
1231 avctx->channel_layout = dca_core_channel_layout[s->amode];
1234 avctx->channel_layout |= CH_LOW_FREQUENCY;
1235 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1237 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1239 if (s->prim_channels > 0 &&
1240 s->channel_order_tab[s->prim_channels - 1] < 0)
1243 if(avctx->request_channels == 2 && s->prim_channels > 2) {
1245 s->output = DCA_STEREO;
1246 avctx->channel_layout = CH_LAYOUT_STEREO;
1249 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
1254 /* There is nothing that prevents a dts frame to change channel configuration
1255 but FFmpeg doesn't support that so only set the channels if it is previously
1256 unset. Ideally during the first probe for channels the crc should be checked
1257 and only set avctx->channels when the crc is ok. Right now the decoder could
1258 set the channels based on a broken first frame.*/
1259 if (!avctx->channels)
1260 avctx->channels = channels;
1262 if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
1264 *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
1265 for (i = 0; i < (s->sample_blocks / 8); i++) {
1266 dca_decode_block(s);
1267 s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
1268 samples += 256 * channels;
1277 * DCA initialization
1279 * @param avctx pointer to the AVCodecContext
1282 static av_cold int dca_decode_init(AVCodecContext * avctx)
1284 DCAContext *s = avctx->priv_data;
1290 dsputil_init(&s->dsp, avctx);
1291 ff_mdct_init(&s->imdct, 6, 1, 1.0);
1292 ff_synth_filter_init(&s->synth);
1293 ff_dcadsp_init(&s->dcadsp);
1295 for(i = 0; i < 6; i++)
1296 s->samples_chanptr[i] = s->samples + i * 256;
1297 avctx->sample_fmt = SAMPLE_FMT_S16;
1299 if(s->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
1300 s->add_bias = 385.0f;
1301 s->scale_bias = 1.0 / 32768.0;
1304 s->scale_bias = 1.0;
1306 /* allow downmixing to stereo */
1307 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1308 avctx->request_channels == 2) {
1309 avctx->channels = avctx->request_channels;
1317 static av_cold int dca_decode_end(AVCodecContext * avctx)
1319 DCAContext *s = avctx->priv_data;
1320 ff_mdct_end(&s->imdct);
1324 AVCodec dca_decoder = {
1326 .type = AVMEDIA_TYPE_AUDIO,
1328 .priv_data_size = sizeof(DCAContext),
1329 .init = dca_decode_init,
1330 .decode = dca_decode_frame,
1331 .close = dca_decode_end,
1332 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),