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"
40
/** DCA syncwords, also used for bitstream type detection */
42
#define DCA_MARKER_RAW_BE 0x7FFE8001
43
#define DCA_MARKER_RAW_LE 0xFE7F0180
44
#define DCA_MARKER_14B_BE 0x1FFFE800
45
#define DCA_MARKER_14B_LE 0xFF1F00E8
50
#define DCA_PRIM_CHANNELS_MAX (5)
51
#define DCA_SUBBANDS (32)
52
#define DCA_ABITS_MAX (32) /* Should be 28 */
53
#define DCA_SUBSUBFAMES_MAX (4)
54
#define DCA_LFE_MAX (3)
70
#define DCA_DOLBY 101 /* FIXME */
72
#define DCA_CHANNEL_BITS 6
73
#define DCA_CHANNEL_MASK 0x3F
77
#define HEADER_SIZE 14
78
#define CONVERT_BIAS 384
80
#define DCA_MAX_FRAME_SIZE 16383
84
int offset; ///< code values offset
85
int maxbits[8]; ///< max bits in VLC
86
int wrap; ///< wrap for get_vlc2()
87
VLC vlc[8]; ///< actual codes
90
static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
91
static BitAlloc dca_tmode; ///< transition mode VLCs
92
static BitAlloc dca_scalefactor; ///< scalefactor VLCs
93
static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
95
/** Pre-calculated cosine modulation coefs for the QMF */
96
static float cos_mod[544];
98
static int av_always_inline get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
100
return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
104
AVCodecContext *avctx;
106
int frame_type; ///< type of the current frame
107
int samples_deficit; ///< deficit sample count
108
int crc_present; ///< crc is present in the bitstream
109
int sample_blocks; ///< number of PCM sample blocks
110
int frame_size; ///< primary frame byte size
111
int amode; ///< audio channels arrangement
112
int sample_rate; ///< audio sampling rate
113
int bit_rate; ///< transmission bit rate
115
int downmix; ///< embedded downmix enabled
116
int dynrange; ///< embedded dynamic range flag
117
int timestamp; ///< embedded time stamp flag
118
int aux_data; ///< auxiliary data flag
119
int hdcd; ///< source material is mastered in HDCD
120
int ext_descr; ///< extension audio descriptor flag
121
int ext_coding; ///< extended coding flag
122
int aspf; ///< audio sync word insertion flag
123
int lfe; ///< low frequency effects flag
124
int predictor_history; ///< predictor history flag
125
int header_crc; ///< header crc check bytes
126
int multirate_inter; ///< multirate interpolator switch
127
int version; ///< encoder software revision
128
int copy_history; ///< copy history
129
int source_pcm_res; ///< source pcm resolution
130
int front_sum; ///< front sum/difference flag
131
int surround_sum; ///< surround sum/difference flag
132
int dialog_norm; ///< dialog normalisation parameter
134
/* Primary audio coding header */
135
int subframes; ///< number of subframes
136
int prim_channels; ///< number of primary audio channels
137
int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
138
int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
139
int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
140
int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
141
int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
142
int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
143
int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
144
float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
146
/* Primary audio coding side information */
147
int subsubframes; ///< number of subsubframes
148
int partial_samples; ///< partial subsubframe samples count
149
int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
150
int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
151
int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
152
int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
153
int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
154
int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
155
int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
156
int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
157
int dynrange_coef; ///< dynamic range coefficient
159
int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
161
float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX *
162
2 /*history */ ]; ///< Low frequency effect data
163
int lfe_scale_factor;
165
/* Subband samples history (for ADPCM) */
166
float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
167
float subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512];
168
float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][64];
170
int output; ///< type of output
171
int bias; ///< output bias
173
DECLARE_ALIGNED_16(float, samples[1536]); /* 6 * 256 = 1536, might only need 5 */
174
DECLARE_ALIGNED_16(int16_t, tsamples[1536]);
176
uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
177
int dca_buffer_size; ///< how much data is in the dca_buffer
180
/* Current position in DCA frame */
181
int current_subframe;
182
int current_subsubframe;
184
int debug_flag; ///< used for suppressing repeated error messages output
188
static void dca_init_vlcs()
190
static int vlcs_inited = 0;
196
dca_bitalloc_index.offset = 1;
197
dca_bitalloc_index.wrap = 1;
198
for (i = 0; i < 5; i++)
199
init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
200
bitalloc_12_bits[i], 1, 1,
201
bitalloc_12_codes[i], 2, 2, 1);
202
dca_scalefactor.offset = -64;
203
dca_scalefactor.wrap = 2;
204
for (i = 0; i < 5; i++)
205
init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
206
scales_bits[i], 1, 1,
207
scales_codes[i], 2, 2, 1);
208
dca_tmode.offset = 0;
210
for (i = 0; i < 4; i++)
211
init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
213
tmode_codes[i], 2, 2, 1);
215
for(i = 0; i < 10; i++)
216
for(j = 0; j < 7; j++){
217
if(!bitalloc_codes[i][j]) break;
218
dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
219
dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
220
init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
222
bitalloc_bits[i][j], 1, 1,
223
bitalloc_codes[i][j], 2, 2, 1);
228
static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
231
*dst++ = get_bits(gb, bits);
234
static int dca_parse_frame_header(DCAContext * s)
237
static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
238
static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
239
static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
241
s->bias = CONVERT_BIAS;
243
init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
246
get_bits(&s->gb, 32);
249
s->frame_type = get_bits(&s->gb, 1);
250
s->samples_deficit = get_bits(&s->gb, 5) + 1;
251
s->crc_present = get_bits(&s->gb, 1);
252
s->sample_blocks = get_bits(&s->gb, 7) + 1;
253
s->frame_size = get_bits(&s->gb, 14) + 1;
254
if (s->frame_size < 95)
256
s->amode = get_bits(&s->gb, 6);
257
s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
260
s->bit_rate = dca_bit_rates[get_bits(&s->gb, 5)];
264
s->downmix = get_bits(&s->gb, 1);
265
s->dynrange = get_bits(&s->gb, 1);
266
s->timestamp = get_bits(&s->gb, 1);
267
s->aux_data = get_bits(&s->gb, 1);
268
s->hdcd = get_bits(&s->gb, 1);
269
s->ext_descr = get_bits(&s->gb, 3);
270
s->ext_coding = get_bits(&s->gb, 1);
271
s->aspf = get_bits(&s->gb, 1);
272
s->lfe = get_bits(&s->gb, 2);
273
s->predictor_history = get_bits(&s->gb, 1);
275
/* TODO: check CRC */
277
s->header_crc = get_bits(&s->gb, 16);
279
s->multirate_inter = get_bits(&s->gb, 1);
280
s->version = get_bits(&s->gb, 4);
281
s->copy_history = get_bits(&s->gb, 2);
282
s->source_pcm_res = get_bits(&s->gb, 3);
283
s->front_sum = get_bits(&s->gb, 1);
284
s->surround_sum = get_bits(&s->gb, 1);
285
s->dialog_norm = get_bits(&s->gb, 4);
287
/* FIXME: channels mixing levels */
288
s->output = DCA_STEREO;
291
av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
292
av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
293
av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
294
av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
295
s->sample_blocks, s->sample_blocks * 32);
296
av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
297
av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
298
s->amode, dca_channels[s->amode]);
299
av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i (%i Hz)\n",
300
s->sample_rate, dca_sample_rates[s->sample_rate]);
301
av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i (%i bits/s)\n",
302
s->bit_rate, dca_bit_rates[s->bit_rate]);
303
av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
304
av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
305
av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
306
av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
307
av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
308
av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
309
av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
310
av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
311
av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
312
av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
313
s->predictor_history);
314
av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
315
av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
317
av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
318
av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
319
av_log(s->avctx, AV_LOG_DEBUG,
320
"source pcm resolution: %i (%i bits/sample)\n",
321
s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
322
av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
323
av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
324
av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
325
av_log(s->avctx, AV_LOG_DEBUG, "\n");
328
/* Primary audio coding header */
329
s->subframes = get_bits(&s->gb, 4) + 1;
330
s->prim_channels = get_bits(&s->gb, 3) + 1;
333
for (i = 0; i < s->prim_channels; i++) {
334
s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
335
if (s->subband_activity[i] > DCA_SUBBANDS)
336
s->subband_activity[i] = DCA_SUBBANDS;
338
for (i = 0; i < s->prim_channels; i++) {
339
s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
340
if (s->vq_start_subband[i] > DCA_SUBBANDS)
341
s->vq_start_subband[i] = DCA_SUBBANDS;
343
get_array(&s->gb, s->joint_intensity, s->prim_channels, 3);
344
get_array(&s->gb, s->transient_huffman, s->prim_channels, 2);
345
get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3);
346
get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3);
348
/* Get codebooks quantization indexes */
349
memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
350
for (j = 1; j < 11; j++)
351
for (i = 0; i < s->prim_channels; i++)
352
s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
354
/* Get scale factor adjustment */
355
for (j = 0; j < 11; j++)
356
for (i = 0; i < s->prim_channels; i++)
357
s->scalefactor_adj[i][j] = 1;
359
for (j = 1; j < 11; j++)
360
for (i = 0; i < s->prim_channels; i++)
361
if (s->quant_index_huffman[i][j] < thr[j])
362
s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
364
if (s->crc_present) {
365
/* Audio header CRC check */
366
get_bits(&s->gb, 16);
369
s->current_subframe = 0;
370
s->current_subsubframe = 0;
373
av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
374
av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
375
for(i = 0; i < s->prim_channels; i++){
376
av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
377
av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
378
av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
379
av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
380
av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
381
av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
382
av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
383
for (j = 0; j < 11; j++)
384
av_log(s->avctx, AV_LOG_DEBUG, " %i",
385
s->quant_index_huffman[i][j]);
386
av_log(s->avctx, AV_LOG_DEBUG, "\n");
387
av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
388
for (j = 0; j < 11; j++)
389
av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
390
av_log(s->avctx, AV_LOG_DEBUG, "\n");
398
static inline int get_scale(GetBitContext *gb, int level, int index, int value)
401
/* huffman encoded */
402
value += get_bitalloc(gb, &dca_scalefactor, index);
404
value = get_bits(gb, level + 1);
408
static int dca_subframe_header(DCAContext * s)
410
/* Primary audio coding side information */
413
s->subsubframes = get_bits(&s->gb, 2) + 1;
414
s->partial_samples = get_bits(&s->gb, 3);
415
for (j = 0; j < s->prim_channels; j++) {
416
for (k = 0; k < s->subband_activity[j]; k++)
417
s->prediction_mode[j][k] = get_bits(&s->gb, 1);
420
/* Get prediction codebook */
421
for (j = 0; j < s->prim_channels; j++) {
422
for (k = 0; k < s->subband_activity[j]; k++) {
423
if (s->prediction_mode[j][k] > 0) {
424
/* (Prediction coefficient VQ address) */
425
s->prediction_vq[j][k] = get_bits(&s->gb, 12);
430
/* Bit allocation index */
431
for (j = 0; j < s->prim_channels; j++) {
432
for (k = 0; k < s->vq_start_subband[j]; k++) {
433
if (s->bitalloc_huffman[j] == 6)
434
s->bitalloc[j][k] = get_bits(&s->gb, 5);
435
else if (s->bitalloc_huffman[j] == 5)
436
s->bitalloc[j][k] = get_bits(&s->gb, 4);
439
get_bitalloc(&s->gb, &dca_bitalloc_index, j);
442
if (s->bitalloc[j][k] > 26) {
443
// av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
444
// j, k, s->bitalloc[j][k]);
450
/* Transition mode */
451
for (j = 0; j < s->prim_channels; j++) {
452
for (k = 0; k < s->subband_activity[j]; k++) {
453
s->transition_mode[j][k] = 0;
454
if (s->subsubframes > 1 &&
455
k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
456
s->transition_mode[j][k] =
457
get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
462
for (j = 0; j < s->prim_channels; j++) {
463
uint32_t *scale_table;
466
memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
468
if (s->scalefactor_huffman[j] == 6)
469
scale_table = (uint32_t *) scale_factor_quant7;
471
scale_table = (uint32_t *) scale_factor_quant6;
473
/* When huffman coded, only the difference is encoded */
476
for (k = 0; k < s->subband_activity[j]; k++) {
477
if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
478
scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], j, scale_sum);
479
s->scale_factor[j][k][0] = scale_table[scale_sum];
482
if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
483
/* Get second scale factor */
484
scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], j, scale_sum);
485
s->scale_factor[j][k][1] = scale_table[scale_sum];
490
/* Joint subband scale factor codebook select */
491
for (j = 0; j < s->prim_channels; j++) {
492
/* Transmitted only if joint subband coding enabled */
493
if (s->joint_intensity[j] > 0)
494
s->joint_huff[j] = get_bits(&s->gb, 3);
497
/* Scale factors for joint subband coding */
498
for (j = 0; j < s->prim_channels; j++) {
501
/* Transmitted only if joint subband coding enabled */
502
if (s->joint_intensity[j] > 0) {
504
source_channel = s->joint_intensity[j] - 1;
506
/* When huffman coded, only the difference is encoded
507
* (is this valid as well for joint scales ???) */
509
for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
510
scale = get_scale(&s->gb, s->joint_huff[j], j, 0);
511
scale += 64; /* bias */
512
s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
515
if (!s->debug_flag & 0x02) {
516
av_log(s->avctx, AV_LOG_DEBUG,
517
"Joint stereo coding not supported\n");
518
s->debug_flag |= 0x02;
523
/* Stereo downmix coefficients */
524
if (s->prim_channels > 2 && s->downmix) {
525
for (j = 0; j < s->prim_channels; j++) {
526
s->downmix_coef[j][0] = get_bits(&s->gb, 7);
527
s->downmix_coef[j][1] = get_bits(&s->gb, 7);
531
/* Dynamic range coefficient */
533
s->dynrange_coef = get_bits(&s->gb, 8);
535
/* Side information CRC check word */
536
if (s->crc_present) {
537
get_bits(&s->gb, 16);
541
* Primary audio data arrays
544
/* VQ encoded high frequency subbands */
545
for (j = 0; j < s->prim_channels; j++)
546
for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
547
/* 1 vector -> 32 samples */
548
s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
550
/* Low frequency effect data */
553
int lfe_samples = 2 * s->lfe * s->subsubframes;
556
for (j = lfe_samples; j < lfe_samples * 2; j++) {
557
/* Signed 8 bits int */
558
s->lfe_data[j] = get_sbits(&s->gb, 8);
561
/* Scale factor index */
562
s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
564
/* Quantization step size * scale factor */
565
lfe_scale = 0.035 * s->lfe_scale_factor;
567
for (j = lfe_samples; j < lfe_samples * 2; j++)
568
s->lfe_data[j] *= lfe_scale;
572
av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes);
573
av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
575
for (j = 0; j < s->prim_channels; j++) {
576
av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
577
for (k = 0; k < s->subband_activity[j]; k++)
578
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
579
av_log(s->avctx, AV_LOG_DEBUG, "\n");
581
for (j = 0; j < s->prim_channels; j++) {
582
for (k = 0; k < s->subband_activity[j]; k++)
583
av_log(s->avctx, AV_LOG_DEBUG,
584
"prediction coefs: %f, %f, %f, %f\n",
585
(float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
586
(float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
587
(float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
588
(float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
590
for (j = 0; j < s->prim_channels; j++) {
591
av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
592
for (k = 0; k < s->vq_start_subband[j]; k++)
593
av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
594
av_log(s->avctx, AV_LOG_DEBUG, "\n");
596
for (j = 0; j < s->prim_channels; j++) {
597
av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
598
for (k = 0; k < s->subband_activity[j]; k++)
599
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
600
av_log(s->avctx, AV_LOG_DEBUG, "\n");
602
for (j = 0; j < s->prim_channels; j++) {
603
av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
604
for (k = 0; k < s->subband_activity[j]; k++) {
605
if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
606
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
607
if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
608
av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
610
av_log(s->avctx, AV_LOG_DEBUG, "\n");
612
for (j = 0; j < s->prim_channels; j++) {
613
if (s->joint_intensity[j] > 0) {
614
av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
615
for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
616
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
617
av_log(s->avctx, AV_LOG_DEBUG, "\n");
620
if (s->prim_channels > 2 && s->downmix) {
621
av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
622
for (j = 0; j < s->prim_channels; j++) {
623
av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
624
av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
626
av_log(s->avctx, AV_LOG_DEBUG, "\n");
628
for (j = 0; j < s->prim_channels; j++)
629
for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
630
av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
632
av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
633
for (j = lfe_samples; j < lfe_samples * 2; j++)
634
av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
635
av_log(s->avctx, AV_LOG_DEBUG, "\n");
642
static void qmf_32_subbands(DCAContext * s, int chans,
643
float samples_in[32][8], float *samples_out,
644
float scale, float bias)
648
float praXin[33], *raXin = &praXin[1];
650
float *subband_fir_hist = s->subband_fir_hist[chans];
651
float *subband_fir_hist2 = s->subband_fir_noidea[chans];
653
int chindex = 0, subindex;
658
if (!s->multirate_inter) /* Non-perfect reconstruction */
659
prCoeff = (float *) fir_32bands_nonperfect;
660
else /* Perfect reconstruction */
661
prCoeff = (float *) fir_32bands_perfect;
663
/* Reconstructed channel sample index */
664
for (subindex = 0; subindex < 8; subindex++) {
665
float t1, t2, sum[16], diff[16];
667
/* Load in one sample from each subband and clear inactive subbands */
668
for (i = 0; i < s->subband_activity[chans]; i++)
669
raXin[i] = samples_in[i][subindex];
673
/* Multiply by cosine modulation coefficients and
674
* create temporary arrays SUM and DIFF */
675
for (j = 0, k = 0; k < 16; k++) {
678
for (i = 0; i < 16; i++, j++){
679
t1 += (raXin[2 * i] + raXin[2 * i + 1]) * cos_mod[j];
680
t2 += (raXin[2 * i] + raXin[2 * i - 1]) * cos_mod[j + 256];
688
for (k = 0; k < 16; k++)
689
subband_fir_hist[k] = cos_mod[j++] * sum[k];
690
for (k = 0; k < 16; k++)
691
subband_fir_hist[32-k-1] = cos_mod[j++] * diff[k];
693
/* Multiply by filter coefficients */
694
for (k = 31, i = 0; i < 32; i++, k--)
695
for (j = 0; j < 512; j += 64){
696
subband_fir_hist2[i] += prCoeff[i+j] * ( subband_fir_hist[i+j] - subband_fir_hist[j+k]);
697
subband_fir_hist2[i+32] += prCoeff[i+j+32]*(-subband_fir_hist[i+j] - subband_fir_hist[j+k]);
700
/* Create 32 PCM output samples */
701
for (i = 0; i < 32; i++)
702
samples_out[chindex++] = subband_fir_hist2[i] * scale + bias;
704
/* Update working arrays */
705
memmove(&subband_fir_hist[32], &subband_fir_hist[0], (512 - 32) * sizeof(float));
706
memmove(&subband_fir_hist2[0], &subband_fir_hist2[32], 32 * sizeof(float));
707
memset(&subband_fir_hist2[32], 0, 32 * sizeof(float));
711
static void lfe_interpolation_fir(int decimation_select,
712
int num_deci_sample, float *samples_in,
713
float *samples_out, float scale,
716
/* samples_in: An array holding decimated samples.
717
* Samples in current subframe starts from samples_in[0],
718
* while samples_in[-1], samples_in[-2], ..., stores samples
719
* from last subframe as history.
721
* samples_out: An array holding interpolated samples
724
int decifactor, k, j;
725
const float *prCoeff;
727
int interp_index = 0; /* Index to the interpolated samples */
730
/* Select decimation filter */
731
if (decimation_select == 1) {
733
prCoeff = lfe_fir_128;
736
prCoeff = lfe_fir_64;
739
for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
740
/* One decimated sample generates decifactor interpolated ones */
741
for (k = 0; k < decifactor; k++) {
743
//FIXME the coeffs are symetric, fix that
744
for (j = 0; j < 512 / decifactor; j++)
745
rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor];
746
samples_out[interp_index++] = rTmp / scale + bias;
751
/* downmixing routines */
752
#define MIX_REAR1(samples, si1) \
753
samples[i] += samples[si1]; \
754
samples[i+256] += samples[si1];
756
#define MIX_REAR2(samples, si1, si2) \
757
samples[i] += samples[si1]; \
758
samples[i+256] += samples[si2];
760
#define MIX_FRONT3(samples) \
762
samples[i] += samples[i+256]; \
763
samples[i+256] = samples[i+512] + t;
765
#define DOWNMIX_TO_STEREO(op1, op2) \
766
for(i = 0; i < 256; i++){ \
771
static void dca_downmix(float *samples, int srcfmt)
779
case DCA_STEREO_TOTAL:
780
case DCA_STEREO_SUMDIFF:
782
av_log(NULL, 0, "Not implemented!\n");
787
DOWNMIX_TO_STEREO(MIX_FRONT3(samples),);
790
DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512),);
793
DOWNMIX_TO_STEREO(MIX_FRONT3(samples),
794
MIX_REAR1(samples, i + 768));
797
DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768),);
800
DOWNMIX_TO_STEREO(MIX_FRONT3(samples),
801
MIX_REAR2(samples, i + 768, i + 1024));
807
/* Very compact version of the block code decoder that does not use table
808
* look-up but is slightly slower */
809
static int decode_blockcode(int code, int levels, int *values)
812
int offset = (levels - 1) >> 1;
814
for (i = 0; i < 4; i++) {
815
values[i] = (code % levels) - offset;
822
av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
827
static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
828
static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
830
static int dca_subsubframe(DCAContext * s)
833
int subsubframe = s->current_subsubframe;
835
float *quant_step_table;
838
float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
844
/* Select quantization step size table */
845
if (s->bit_rate == 0x1f)
846
quant_step_table = (float *) lossless_quant_d;
848
quant_step_table = (float *) lossy_quant_d;
850
for (k = 0; k < s->prim_channels; k++) {
851
for (l = 0; l < s->vq_start_subband[k]; l++) {
854
/* Select the mid-tread linear quantizer */
855
int abits = s->bitalloc[k][l];
857
float quant_step_size = quant_step_table[abits];
861
* Determine quantization index code book and its type
864
/* Select quantization index code book */
865
int sel = s->quant_index_huffman[k][abits];
868
* Extract bits from the bit stream
871
memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
872
}else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
875
int block_code1, block_code2, size, levels;
878
size = abits_sizes[abits-1];
879
levels = abits_levels[abits-1];
881
block_code1 = get_bits(&s->gb, size);
882
/* FIXME Should test return value */
883
decode_blockcode(block_code1, levels, block);
884
block_code2 = get_bits(&s->gb, size);
885
decode_blockcode(block_code2, levels, &block[4]);
886
for (m = 0; m < 8; m++)
887
subband_samples[k][l][m] = block[m];
890
for (m = 0; m < 8; m++)
891
subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3);
895
for (m = 0; m < 8; m++)
896
subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
899
/* Deal with transients */
900
if (s->transition_mode[k][l] &&
901
subsubframe >= s->transition_mode[k][l])
902
rscale = quant_step_size * s->scale_factor[k][l][1];
904
rscale = quant_step_size * s->scale_factor[k][l][0];
906
rscale *= s->scalefactor_adj[k][sel];
908
for (m = 0; m < 8; m++)
909
subband_samples[k][l][m] *= rscale;
912
* Inverse ADPCM if in prediction mode
914
if (s->prediction_mode[k][l]) {
916
for (m = 0; m < 8; m++) {
917
for (n = 1; n <= 4; n++)
919
subband_samples[k][l][m] +=
920
(adpcm_vb[s->prediction_vq[k][l]][n - 1] *
921
subband_samples[k][l][m - n] / 8192);
922
else if (s->predictor_history)
923
subband_samples[k][l][m] +=
924
(adpcm_vb[s->prediction_vq[k][l]][n - 1] *
925
s->subband_samples_hist[k][l][m - n +
932
* Decode VQ encoded high frequencies
934
for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
935
/* 1 vector -> 32 samples but we only need the 8 samples
936
* for this subsubframe. */
939
if (!s->debug_flag & 0x01) {
940
av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
941
s->debug_flag |= 0x01;
944
for (m = 0; m < 8; m++) {
945
subband_samples[k][l][m] =
946
high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
948
* (float) s->scale_factor[k][l][0] / 16.0;
953
/* Check for DSYNC after subsubframe */
954
if (s->aspf || subsubframe == s->subsubframes - 1) {
955
if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
957
av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
960
av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
964
/* Backup predictor history for adpcm */
965
for (k = 0; k < s->prim_channels; k++)
966
for (l = 0; l < s->vq_start_subband[k]; l++)
967
memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
968
4 * sizeof(subband_samples[0][0][0]));
970
/* 32 subbands QMF */
971
for (k = 0; k < s->prim_channels; k++) {
972
/* static float pcm_to_double[8] =
973
{32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
974
qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * k],
975
2.0 / 3 /*pcm_to_double[s->source_pcm_res] */ ,
981
if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) {
982
dca_downmix(s->samples, s->amode);
985
/* Generate LFE samples for this subsubframe FIXME!!! */
986
if (s->output & DCA_LFE) {
987
int lfe_samples = 2 * s->lfe * s->subsubframes;
988
int i_channels = dca_channels[s->output & DCA_CHANNEL_MASK];
990
lfe_interpolation_fir(s->lfe, 2 * s->lfe,
991
s->lfe_data + lfe_samples +
992
2 * s->lfe * subsubframe,
993
&s->samples[256 * i_channels],
995
/* Outputs 20bits pcm samples */
1002
static int dca_subframe_footer(DCAContext * s)
1004
int aux_data_count = 0, i;
1008
* Unpack optional information
1012
get_bits(&s->gb, 32);
1015
aux_data_count = get_bits(&s->gb, 6);
1017
for (i = 0; i < aux_data_count; i++)
1018
get_bits(&s->gb, 8);
1020
if (s->crc_present && (s->downmix || s->dynrange))
1021
get_bits(&s->gb, 16);
1023
lfe_samples = 2 * s->lfe * s->subsubframes;
1024
for (i = 0; i < lfe_samples; i++) {
1025
s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1032
* Decode a dca frame block
1034
* @param s pointer to the DCAContext
1037
static int dca_decode_block(DCAContext * s)
1041
if (s->current_subframe >= s->subframes) {
1042
av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1043
s->current_subframe, s->subframes);
1047
if (!s->current_subsubframe) {
1049
av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1051
/* Read subframe header */
1052
if (dca_subframe_header(s))
1056
/* Read subsubframe */
1058
av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1060
if (dca_subsubframe(s))
1064
s->current_subsubframe++;
1065
if (s->current_subsubframe >= s->subsubframes) {
1066
s->current_subsubframe = 0;
1067
s->current_subframe++;
1069
if (s->current_subframe >= s->subframes) {
1071
av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1073
/* Read subframe footer */
1074
if (dca_subframe_footer(s))
1082
* Convert bitstream to one representation based on sync marker
1084
static int dca_convert_bitstream(uint8_t * src, int src_size, uint8_t * dst,
1089
uint16_t *ssrc = (uint16_t *) src, *sdst = (uint16_t *) dst;
1094
case DCA_MARKER_RAW_BE:
1095
memcpy(dst, src, FFMIN(src_size, max_size));
1096
return FFMIN(src_size, max_size);
1097
case DCA_MARKER_RAW_LE:
1098
for (i = 0; i < (FFMIN(src_size, max_size) + 1) >> 1; i++)
1099
*sdst++ = bswap_16(*ssrc++);
1100
return FFMIN(src_size, max_size);
1101
case DCA_MARKER_14B_BE:
1102
case DCA_MARKER_14B_LE:
1103
init_put_bits(&pb, dst, max_size);
1104
for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1105
tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1106
put_bits(&pb, 14, tmp);
1108
flush_put_bits(&pb);
1109
return (put_bits_count(&pb) + 7) >> 3;
1116
* Main frame decoding function
1117
* FIXME add arguments
1119
static int dca_decode_frame(AVCodecContext * avctx,
1120
void *data, int *data_size,
1121
uint8_t * buf, int buf_size)
1125
int16_t *samples = data;
1126
DCAContext *s = avctx->priv_data;
1130
s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
1131
if (s->dca_buffer_size == -1) {
1132
av_log(avctx, AV_LOG_ERROR, "Not a DCA frame\n");
1136
init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1137
if (dca_parse_frame_header(s) < 0) {
1138
//seems like the frame is corrupt, try with the next one
1141
//set AVCodec values with parsed data
1142
avctx->sample_rate = s->sample_rate;
1143
avctx->channels = 2; //FIXME
1144
avctx->bit_rate = s->bit_rate;
1146
channels = dca_channels[s->output];
1147
if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
1150
for (i = 0; i < (s->sample_blocks / 8); i++) {
1151
dca_decode_block(s);
1152
s->dsp.float_to_int16(s->tsamples, s->samples, 256 * channels);
1153
/* interleave samples */
1154
for (j = 0; j < 256; j++) {
1155
for (k = 0; k < channels; k++)
1156
samples[k] = s->tsamples[j + k * 256];
1157
samples += channels;
1159
*data_size += 256 * sizeof(int16_t) * channels;
1168
* Build the cosine modulation tables for the QMF
1170
* @param s pointer to the DCAContext
1173
static void pre_calc_cosmod(DCAContext * s)
1176
static int cosmod_inited = 0;
1178
if(cosmod_inited) return;
1179
for (j = 0, k = 0; k < 16; k++)
1180
for (i = 0; i < 16; i++)
1181
cos_mod[j++] = cos((2 * i + 1) * (2 * k + 1) * M_PI / 64);
1183
for (k = 0; k < 16; k++)
1184
for (i = 0; i < 16; i++)
1185
cos_mod[j++] = cos((i) * (2 * k + 1) * M_PI / 32);
1187
for (k = 0; k < 16; k++)
1188
cos_mod[j++] = 0.25 / (2 * cos((2 * k + 1) * M_PI / 128));
1190
for (k = 0; k < 16; k++)
1191
cos_mod[j++] = -0.25 / (2.0 * sin((2 * k + 1) * M_PI / 128));
1198
* DCA initialization
1200
* @param avctx pointer to the AVCodecContext
1203
static int dca_decode_init(AVCodecContext * avctx)
1205
DCAContext *s = avctx->priv_data;
1211
dsputil_init(&s->dsp, avctx);
1216
AVCodec dca_decoder = {
1218
.type = CODEC_TYPE_AUDIO,
1220
.priv_data_size = sizeof(DCAContext),
1221
.init = dca_decode_init,
1222
.decode = dca_decode_frame,
1225
#ifdef CONFIG_DCA_PARSER
1227
typedef struct DCAParseContext {
1229
uint32_t lastmarker;
1232
#define IS_MARKER(state, i, buf, buf_size) \
1233
((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
1234
|| (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
1235
|| state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
1238
* finds the end of the current frame in the bitstream.
1239
* @return the position of the first byte of the next frame, or -1
1241
static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
1246
ParseContext *pc = &pc1->pc;
1248
start_found = pc->frame_start_found;
1253
for (i = 0; i < buf_size; i++) {
1254
state = (state << 8) | buf[i];
1255
if (IS_MARKER(state, i, buf, buf_size)) {
1256
if (pc1->lastmarker && state == pc1->lastmarker) {
1259
} else if (!pc1->lastmarker) {
1261
pc1->lastmarker = state;
1268
for (; i < buf_size; i++) {
1269
state = (state << 8) | buf[i];
1270
if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
1271
pc->frame_start_found = 0;
1277
pc->frame_start_found = start_found;
1279
return END_NOT_FOUND;
1282
static int dca_parse_init(AVCodecParserContext * s)
1284
DCAParseContext *pc1 = s->priv_data;
1286
pc1->lastmarker = 0;
1290
static int dca_parse(AVCodecParserContext * s,
1291
AVCodecContext * avctx,
1292
uint8_t ** poutbuf, int *poutbuf_size,
1293
const uint8_t * buf, int buf_size)
1295
DCAParseContext *pc1 = s->priv_data;
1296
ParseContext *pc = &pc1->pc;
1299
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1302
next = dca_find_frame_end(pc1, buf, buf_size);
1304
if (ff_combine_frame(pc, next, (uint8_t **) & buf, &buf_size) < 0) {
1310
*poutbuf = (uint8_t *) buf;
1311
*poutbuf_size = buf_size;
1315
AVCodecParser dca_parser = {
1317
sizeof(DCAParseContext),
1322
#endif /* CONFIG_DCA_PARSER */