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
26 * @file libavcodec/dca.c
43 #define DCA_PRIM_CHANNELS_MAX (5)
44 #define DCA_SUBBANDS (32)
45 #define DCA_ABITS_MAX (32) /* Should be 28 */
46 #define DCA_SUBSUBFAMES_MAX (4)
47 #define DCA_LFE_MAX (3)
63 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
64 * Some compromises have been made for special configurations. Most configurations
65 * are never used so complete accuracy is not needed.
67 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
68 * S -> side, when both rear and back are configured move one of them to the side channel
70 * All 2 channel configurations -> CH_LAYOUT_STEREO
73 static const int64_t dca_core_channel_layout
[] = {
74 CH_FRONT_CENTER
, ///< 1, A
75 CH_LAYOUT_STEREO
, ///< 2, A + B (dual mono)
76 CH_LAYOUT_STEREO
, ///< 2, L + R (stereo)
77 CH_LAYOUT_STEREO
, ///< 2, (L+R) + (L-R) (sum-difference)
78 CH_LAYOUT_STEREO
, ///< 2, LT +RT (left and right total)
79 CH_LAYOUT_STEREO
|CH_FRONT_CENTER
, ///< 3, C+L+R
80 CH_LAYOUT_STEREO
|CH_BACK_CENTER
, ///< 3, L+R+S
81 CH_LAYOUT_STEREO
|CH_FRONT_CENTER
|CH_BACK_CENTER
, ///< 4, C + L + R+ S
82 CH_LAYOUT_STEREO
|CH_SIDE_LEFT
|CH_SIDE_RIGHT
, ///< 4, L + R +SL+ SR
83 CH_LAYOUT_STEREO
|CH_FRONT_CENTER
|CH_SIDE_LEFT
|CH_SIDE_RIGHT
, ///< 5, C + L + R+ SL+SR
84 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
85 CH_LAYOUT_STEREO
|CH_BACK_LEFT
|CH_BACK_RIGHT
|CH_FRONT_CENTER
|CH_BACK_CENTER
, ///< 6, C + L + R+ LR + RR + OV
86 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
87 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
88 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
89 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
92 static const int8_t dca_lfe_index
[] = {
93 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
96 static const int8_t dca_channel_reorder_lfe
[][8] = {
97 { 0, -1, -1, -1, -1, -1, -1, -1},
98 { 0, 1, -1, -1, -1, -1, -1, -1},
99 { 0, 1, -1, -1, -1, -1, -1, -1},
100 { 0, 1, -1, -1, -1, -1, -1, -1},
101 { 0, 1, -1, -1, -1, -1, -1, -1},
102 { 2, 0, 1, -1, -1, -1, -1, -1},
103 { 0, 1, 3, -1, -1, -1, -1, -1},
104 { 2, 0, 1, 4, -1, -1, -1, -1},
105 { 0, 1, 3, 4, -1, -1, -1, -1},
106 { 2, 0, 1, 4, 5, -1, -1, -1},
107 { 3, 4, 0, 1, 5, 6, -1, -1},
108 { 2, 0, 1, 4, 5, 6, -1, -1},
109 { 0, 6, 4, 5, 2, 3, -1, -1},
110 { 4, 2, 5, 0, 1, 6, 7, -1},
111 { 5, 6, 0, 1, 7, 3, 8, 4},
112 { 4, 2, 5, 0, 1, 6, 8, 7},
115 static const int8_t dca_channel_reorder_nolfe
[][8] = {
116 { 0, -1, -1, -1, -1, -1, -1, -1},
117 { 0, 1, -1, -1, -1, -1, -1, -1},
118 { 0, 1, -1, -1, -1, -1, -1, -1},
119 { 0, 1, -1, -1, -1, -1, -1, -1},
120 { 0, 1, -1, -1, -1, -1, -1, -1},
121 { 2, 0, 1, -1, -1, -1, -1, -1},
122 { 0, 1, 2, -1, -1, -1, -1, -1},
123 { 2, 0, 1, 3, -1, -1, -1, -1},
124 { 0, 1, 2, 3, -1, -1, -1, -1},
125 { 2, 0, 1, 3, 4, -1, -1, -1},
126 { 2, 3, 0, 1, 4, 5, -1, -1},
127 { 2, 0, 1, 3, 4, 5, -1, -1},
128 { 0, 5, 3, 4, 1, 2, -1, -1},
129 { 3, 2, 4, 0, 1, 5, 6, -1},
130 { 4, 5, 0, 1, 6, 2, 7, 3},
131 { 3, 2, 4, 0, 1, 5, 7, 6},
135 #define DCA_DOLBY 101 /* FIXME */
137 #define DCA_CHANNEL_BITS 6
138 #define DCA_CHANNEL_MASK 0x3F
142 #define HEADER_SIZE 14
144 #define DCA_MAX_FRAME_SIZE 16384
146 /** Bit allocation */
148 int offset
; ///< code values offset
149 int maxbits
[8]; ///< max bits in VLC
150 int wrap
; ///< wrap for get_vlc2()
151 VLC vlc
[8]; ///< actual codes
154 static BitAlloc dca_bitalloc_index
; ///< indexes for samples VLC select
155 static BitAlloc dca_tmode
; ///< transition mode VLCs
156 static BitAlloc dca_scalefactor
; ///< scalefactor VLCs
157 static BitAlloc dca_smpl_bitalloc
[11]; ///< samples VLCs
159 static av_always_inline
int get_bitalloc(GetBitContext
*gb
, BitAlloc
*ba
, int idx
)
161 return get_vlc2(gb
, ba
->vlc
[idx
].table
, ba
->vlc
[idx
].bits
, ba
->wrap
) + ba
->offset
;
165 AVCodecContext
*avctx
;
167 int frame_type
; ///< type of the current frame
168 int samples_deficit
; ///< deficit sample count
169 int crc_present
; ///< crc is present in the bitstream
170 int sample_blocks
; ///< number of PCM sample blocks
171 int frame_size
; ///< primary frame byte size
172 int amode
; ///< audio channels arrangement
173 int sample_rate
; ///< audio sampling rate
174 int bit_rate
; ///< transmission bit rate
175 int bit_rate_index
; ///< transmission bit rate index
177 int downmix
; ///< embedded downmix enabled
178 int dynrange
; ///< embedded dynamic range flag
179 int timestamp
; ///< embedded time stamp flag
180 int aux_data
; ///< auxiliary data flag
181 int hdcd
; ///< source material is mastered in HDCD
182 int ext_descr
; ///< extension audio descriptor flag
183 int ext_coding
; ///< extended coding flag
184 int aspf
; ///< audio sync word insertion flag
185 int lfe
; ///< low frequency effects flag
186 int predictor_history
; ///< predictor history flag
187 int header_crc
; ///< header crc check bytes
188 int multirate_inter
; ///< multirate interpolator switch
189 int version
; ///< encoder software revision
190 int copy_history
; ///< copy history
191 int source_pcm_res
; ///< source pcm resolution
192 int front_sum
; ///< front sum/difference flag
193 int surround_sum
; ///< surround sum/difference flag
194 int dialog_norm
; ///< dialog normalisation parameter
196 /* Primary audio coding header */
197 int subframes
; ///< number of subframes
198 int total_channels
; ///< number of channels including extensions
199 int prim_channels
; ///< number of primary audio channels
200 int subband_activity
[DCA_PRIM_CHANNELS_MAX
]; ///< subband activity count
201 int vq_start_subband
[DCA_PRIM_CHANNELS_MAX
]; ///< high frequency vq start subband
202 int joint_intensity
[DCA_PRIM_CHANNELS_MAX
]; ///< joint intensity coding index
203 int transient_huffman
[DCA_PRIM_CHANNELS_MAX
]; ///< transient mode code book
204 int scalefactor_huffman
[DCA_PRIM_CHANNELS_MAX
]; ///< scale factor code book
205 int bitalloc_huffman
[DCA_PRIM_CHANNELS_MAX
]; ///< bit allocation quantizer select
206 int quant_index_huffman
[DCA_PRIM_CHANNELS_MAX
][DCA_ABITS_MAX
]; ///< quantization index codebook select
207 float scalefactor_adj
[DCA_PRIM_CHANNELS_MAX
][DCA_ABITS_MAX
]; ///< scale factor adjustment
209 /* Primary audio coding side information */
210 int subsubframes
; ///< number of subsubframes
211 int partial_samples
; ///< partial subsubframe samples count
212 int prediction_mode
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< prediction mode (ADPCM used or not)
213 int prediction_vq
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< prediction VQ coefs
214 int bitalloc
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< bit allocation index
215 int transition_mode
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< transition mode (transients)
216 int scale_factor
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
][2]; ///< scale factors (2 if transient)
217 int joint_huff
[DCA_PRIM_CHANNELS_MAX
]; ///< joint subband scale factors codebook
218 int joint_scale_factor
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< joint subband scale factors
219 int downmix_coef
[DCA_PRIM_CHANNELS_MAX
][2]; ///< stereo downmix coefficients
220 int dynrange_coef
; ///< dynamic range coefficient
222 int high_freq_vq
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< VQ encoded high frequency subbands
224 float lfe_data
[2 * DCA_SUBSUBFAMES_MAX
* DCA_LFE_MAX
*
225 2 /*history */ ]; ///< Low frequency effect data
226 int lfe_scale_factor
;
228 /* Subband samples history (for ADPCM) */
229 float subband_samples_hist
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
][4];
230 DECLARE_ALIGNED_16(float, subband_fir_hist
[DCA_PRIM_CHANNELS_MAX
][512]);
231 float subband_fir_noidea
[DCA_PRIM_CHANNELS_MAX
][32];
232 int hist_index
[DCA_PRIM_CHANNELS_MAX
];
233 DECLARE_ALIGNED_16(float, raXin
[32]);
235 int output
; ///< type of output
236 float add_bias
; ///< output bias
237 float scale_bias
; ///< output scale
239 DECLARE_ALIGNED_16(float, samples
[1536]); /* 6 * 256 = 1536, might only need 5 */
240 const float *samples_chanptr
[6];
242 uint8_t dca_buffer
[DCA_MAX_FRAME_SIZE
];
243 int dca_buffer_size
; ///< how much data is in the dca_buffer
245 const int8_t* channel_order_tab
; ///< channel reordering table, lfe and non lfe
247 /* Current position in DCA frame */
248 int current_subframe
;
249 int current_subsubframe
;
251 int debug_flag
; ///< used for suppressing repeated error messages output
256 static const uint16_t dca_vlc_offs
[] = {
257 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
258 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
259 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
260 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
261 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
262 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
265 static av_cold
void dca_init_vlcs(void)
267 static int vlcs_initialized
= 0;
269 static VLC_TYPE dca_table
[23622][2];
271 if (vlcs_initialized
)
274 dca_bitalloc_index
.offset
= 1;
275 dca_bitalloc_index
.wrap
= 2;
276 for (i
= 0; i
< 5; i
++) {
277 dca_bitalloc_index
.vlc
[i
].table
= &dca_table
[dca_vlc_offs
[i
]];
278 dca_bitalloc_index
.vlc
[i
].table_allocated
= dca_vlc_offs
[i
+ 1] - dca_vlc_offs
[i
];
279 init_vlc(&dca_bitalloc_index
.vlc
[i
], bitalloc_12_vlc_bits
[i
], 12,
280 bitalloc_12_bits
[i
], 1, 1,
281 bitalloc_12_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
283 dca_scalefactor
.offset
= -64;
284 dca_scalefactor
.wrap
= 2;
285 for (i
= 0; i
< 5; i
++) {
286 dca_scalefactor
.vlc
[i
].table
= &dca_table
[dca_vlc_offs
[i
+ 5]];
287 dca_scalefactor
.vlc
[i
].table_allocated
= dca_vlc_offs
[i
+ 6] - dca_vlc_offs
[i
+ 5];
288 init_vlc(&dca_scalefactor
.vlc
[i
], SCALES_VLC_BITS
, 129,
289 scales_bits
[i
], 1, 1,
290 scales_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
292 dca_tmode
.offset
= 0;
294 for (i
= 0; i
< 4; i
++) {
295 dca_tmode
.vlc
[i
].table
= &dca_table
[dca_vlc_offs
[i
+ 10]];
296 dca_tmode
.vlc
[i
].table_allocated
= dca_vlc_offs
[i
+ 11] - dca_vlc_offs
[i
+ 10];
297 init_vlc(&dca_tmode
.vlc
[i
], tmode_vlc_bits
[i
], 4,
299 tmode_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
302 for(i
= 0; i
< 10; i
++)
303 for(j
= 0; j
< 7; j
++){
304 if(!bitalloc_codes
[i
][j
]) break;
305 dca_smpl_bitalloc
[i
+1].offset
= bitalloc_offsets
[i
];
306 dca_smpl_bitalloc
[i
+1].wrap
= 1 + (j
> 4);
307 dca_smpl_bitalloc
[i
+1].vlc
[j
].table
= &dca_table
[dca_vlc_offs
[c
]];
308 dca_smpl_bitalloc
[i
+1].vlc
[j
].table_allocated
= dca_vlc_offs
[c
+ 1] - dca_vlc_offs
[c
];
309 init_vlc(&dca_smpl_bitalloc
[i
+1].vlc
[j
], bitalloc_maxbits
[i
][j
],
311 bitalloc_bits
[i
][j
], 1, 1,
312 bitalloc_codes
[i
][j
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
315 vlcs_initialized
= 1;
318 static inline void get_array(GetBitContext
*gb
, int *dst
, int len
, int bits
)
321 *dst
++ = get_bits(gb
, bits
);
324 static int dca_parse_frame_header(DCAContext
* s
)
327 static const float adj_table
[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
328 static const int bitlen
[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
329 static const int thr
[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
331 init_get_bits(&s
->gb
, s
->dca_buffer
, s
->dca_buffer_size
* 8);
334 get_bits(&s
->gb
, 32);
337 s
->frame_type
= get_bits(&s
->gb
, 1);
338 s
->samples_deficit
= get_bits(&s
->gb
, 5) + 1;
339 s
->crc_present
= get_bits(&s
->gb
, 1);
340 s
->sample_blocks
= get_bits(&s
->gb
, 7) + 1;
341 s
->frame_size
= get_bits(&s
->gb
, 14) + 1;
342 if (s
->frame_size
< 95)
344 s
->amode
= get_bits(&s
->gb
, 6);
345 s
->sample_rate
= dca_sample_rates
[get_bits(&s
->gb
, 4)];
348 s
->bit_rate_index
= get_bits(&s
->gb
, 5);
349 s
->bit_rate
= dca_bit_rates
[s
->bit_rate_index
];
353 s
->downmix
= get_bits(&s
->gb
, 1);
354 s
->dynrange
= get_bits(&s
->gb
, 1);
355 s
->timestamp
= get_bits(&s
->gb
, 1);
356 s
->aux_data
= get_bits(&s
->gb
, 1);
357 s
->hdcd
= get_bits(&s
->gb
, 1);
358 s
->ext_descr
= get_bits(&s
->gb
, 3);
359 s
->ext_coding
= get_bits(&s
->gb
, 1);
360 s
->aspf
= get_bits(&s
->gb
, 1);
361 s
->lfe
= get_bits(&s
->gb
, 2);
362 s
->predictor_history
= get_bits(&s
->gb
, 1);
364 /* TODO: check CRC */
366 s
->header_crc
= get_bits(&s
->gb
, 16);
368 s
->multirate_inter
= get_bits(&s
->gb
, 1);
369 s
->version
= get_bits(&s
->gb
, 4);
370 s
->copy_history
= get_bits(&s
->gb
, 2);
371 s
->source_pcm_res
= get_bits(&s
->gb
, 3);
372 s
->front_sum
= get_bits(&s
->gb
, 1);
373 s
->surround_sum
= get_bits(&s
->gb
, 1);
374 s
->dialog_norm
= get_bits(&s
->gb
, 4);
376 /* FIXME: channels mixing levels */
377 s
->output
= s
->amode
;
378 if(s
->lfe
) s
->output
|= DCA_LFE
;
381 av_log(s
->avctx
, AV_LOG_DEBUG
, "frame type: %i\n", s
->frame_type
);
382 av_log(s
->avctx
, AV_LOG_DEBUG
, "samples deficit: %i\n", s
->samples_deficit
);
383 av_log(s
->avctx
, AV_LOG_DEBUG
, "crc present: %i\n", s
->crc_present
);
384 av_log(s
->avctx
, AV_LOG_DEBUG
, "sample blocks: %i (%i samples)\n",
385 s
->sample_blocks
, s
->sample_blocks
* 32);
386 av_log(s
->avctx
, AV_LOG_DEBUG
, "frame size: %i bytes\n", s
->frame_size
);
387 av_log(s
->avctx
, AV_LOG_DEBUG
, "amode: %i (%i channels)\n",
388 s
->amode
, dca_channels
[s
->amode
]);
389 av_log(s
->avctx
, AV_LOG_DEBUG
, "sample rate: %i Hz\n",
391 av_log(s
->avctx
, AV_LOG_DEBUG
, "bit rate: %i bits/s\n",
393 av_log(s
->avctx
, AV_LOG_DEBUG
, "downmix: %i\n", s
->downmix
);
394 av_log(s
->avctx
, AV_LOG_DEBUG
, "dynrange: %i\n", s
->dynrange
);
395 av_log(s
->avctx
, AV_LOG_DEBUG
, "timestamp: %i\n", s
->timestamp
);
396 av_log(s
->avctx
, AV_LOG_DEBUG
, "aux_data: %i\n", s
->aux_data
);
397 av_log(s
->avctx
, AV_LOG_DEBUG
, "hdcd: %i\n", s
->hdcd
);
398 av_log(s
->avctx
, AV_LOG_DEBUG
, "ext descr: %i\n", s
->ext_descr
);
399 av_log(s
->avctx
, AV_LOG_DEBUG
, "ext coding: %i\n", s
->ext_coding
);
400 av_log(s
->avctx
, AV_LOG_DEBUG
, "aspf: %i\n", s
->aspf
);
401 av_log(s
->avctx
, AV_LOG_DEBUG
, "lfe: %i\n", s
->lfe
);
402 av_log(s
->avctx
, AV_LOG_DEBUG
, "predictor history: %i\n",
403 s
->predictor_history
);
404 av_log(s
->avctx
, AV_LOG_DEBUG
, "header crc: %i\n", s
->header_crc
);
405 av_log(s
->avctx
, AV_LOG_DEBUG
, "multirate inter: %i\n",
407 av_log(s
->avctx
, AV_LOG_DEBUG
, "version number: %i\n", s
->version
);
408 av_log(s
->avctx
, AV_LOG_DEBUG
, "copy history: %i\n", s
->copy_history
);
409 av_log(s
->avctx
, AV_LOG_DEBUG
,
410 "source pcm resolution: %i (%i bits/sample)\n",
411 s
->source_pcm_res
, dca_bits_per_sample
[s
->source_pcm_res
]);
412 av_log(s
->avctx
, AV_LOG_DEBUG
, "front sum: %i\n", s
->front_sum
);
413 av_log(s
->avctx
, AV_LOG_DEBUG
, "surround sum: %i\n", s
->surround_sum
);
414 av_log(s
->avctx
, AV_LOG_DEBUG
, "dialog norm: %i\n", s
->dialog_norm
);
415 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
418 /* Primary audio coding header */
419 s
->subframes
= get_bits(&s
->gb
, 4) + 1;
420 s
->total_channels
= get_bits(&s
->gb
, 3) + 1;
421 s
->prim_channels
= s
->total_channels
;
422 if (s
->prim_channels
> DCA_PRIM_CHANNELS_MAX
)
423 s
->prim_channels
= DCA_PRIM_CHANNELS_MAX
; /* We only support DTS core */
426 for (i
= 0; i
< s
->prim_channels
; i
++) {
427 s
->subband_activity
[i
] = get_bits(&s
->gb
, 5) + 2;
428 if (s
->subband_activity
[i
] > DCA_SUBBANDS
)
429 s
->subband_activity
[i
] = DCA_SUBBANDS
;
431 for (i
= 0; i
< s
->prim_channels
; i
++) {
432 s
->vq_start_subband
[i
] = get_bits(&s
->gb
, 5) + 1;
433 if (s
->vq_start_subband
[i
] > DCA_SUBBANDS
)
434 s
->vq_start_subband
[i
] = DCA_SUBBANDS
;
436 get_array(&s
->gb
, s
->joint_intensity
, s
->prim_channels
, 3);
437 get_array(&s
->gb
, s
->transient_huffman
, s
->prim_channels
, 2);
438 get_array(&s
->gb
, s
->scalefactor_huffman
, s
->prim_channels
, 3);
439 get_array(&s
->gb
, s
->bitalloc_huffman
, s
->prim_channels
, 3);
441 /* Get codebooks quantization indexes */
442 memset(s
->quant_index_huffman
, 0, sizeof(s
->quant_index_huffman
));
443 for (j
= 1; j
< 11; j
++)
444 for (i
= 0; i
< s
->prim_channels
; i
++)
445 s
->quant_index_huffman
[i
][j
] = get_bits(&s
->gb
, bitlen
[j
]);
447 /* Get scale factor adjustment */
448 for (j
= 0; j
< 11; j
++)
449 for (i
= 0; i
< s
->prim_channels
; i
++)
450 s
->scalefactor_adj
[i
][j
] = 1;
452 for (j
= 1; j
< 11; j
++)
453 for (i
= 0; i
< s
->prim_channels
; i
++)
454 if (s
->quant_index_huffman
[i
][j
] < thr
[j
])
455 s
->scalefactor_adj
[i
][j
] = adj_table
[get_bits(&s
->gb
, 2)];
457 if (s
->crc_present
) {
458 /* Audio header CRC check */
459 get_bits(&s
->gb
, 16);
462 s
->current_subframe
= 0;
463 s
->current_subsubframe
= 0;
466 av_log(s
->avctx
, AV_LOG_DEBUG
, "subframes: %i\n", s
->subframes
);
467 av_log(s
->avctx
, AV_LOG_DEBUG
, "prim channels: %i\n", s
->prim_channels
);
468 for(i
= 0; i
< s
->prim_channels
; i
++){
469 av_log(s
->avctx
, AV_LOG_DEBUG
, "subband activity: %i\n", s
->subband_activity
[i
]);
470 av_log(s
->avctx
, AV_LOG_DEBUG
, "vq start subband: %i\n", s
->vq_start_subband
[i
]);
471 av_log(s
->avctx
, AV_LOG_DEBUG
, "joint intensity: %i\n", s
->joint_intensity
[i
]);
472 av_log(s
->avctx
, AV_LOG_DEBUG
, "transient mode codebook: %i\n", s
->transient_huffman
[i
]);
473 av_log(s
->avctx
, AV_LOG_DEBUG
, "scale factor codebook: %i\n", s
->scalefactor_huffman
[i
]);
474 av_log(s
->avctx
, AV_LOG_DEBUG
, "bit allocation quantizer: %i\n", s
->bitalloc_huffman
[i
]);
475 av_log(s
->avctx
, AV_LOG_DEBUG
, "quant index huff:");
476 for (j
= 0; j
< 11; j
++)
477 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i",
478 s
->quant_index_huffman
[i
][j
]);
479 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
480 av_log(s
->avctx
, AV_LOG_DEBUG
, "scalefac adj:");
481 for (j
= 0; j
< 11; j
++)
482 av_log(s
->avctx
, AV_LOG_DEBUG
, " %1.3f", s
->scalefactor_adj
[i
][j
]);
483 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
491 static inline int get_scale(GetBitContext
*gb
, int level
, int value
)
494 /* huffman encoded */
495 value
+= get_bitalloc(gb
, &dca_scalefactor
, level
);
497 value
= get_bits(gb
, level
+ 1);
501 static int dca_subframe_header(DCAContext
* s
)
503 /* Primary audio coding side information */
506 s
->subsubframes
= get_bits(&s
->gb
, 2) + 1;
507 s
->partial_samples
= get_bits(&s
->gb
, 3);
508 for (j
= 0; j
< s
->prim_channels
; j
++) {
509 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
510 s
->prediction_mode
[j
][k
] = get_bits(&s
->gb
, 1);
513 /* Get prediction codebook */
514 for (j
= 0; j
< s
->prim_channels
; j
++) {
515 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
516 if (s
->prediction_mode
[j
][k
] > 0) {
517 /* (Prediction coefficient VQ address) */
518 s
->prediction_vq
[j
][k
] = get_bits(&s
->gb
, 12);
523 /* Bit allocation index */
524 for (j
= 0; j
< s
->prim_channels
; j
++) {
525 for (k
= 0; k
< s
->vq_start_subband
[j
]; k
++) {
526 if (s
->bitalloc_huffman
[j
] == 6)
527 s
->bitalloc
[j
][k
] = get_bits(&s
->gb
, 5);
528 else if (s
->bitalloc_huffman
[j
] == 5)
529 s
->bitalloc
[j
][k
] = get_bits(&s
->gb
, 4);
530 else if (s
->bitalloc_huffman
[j
] == 7) {
531 av_log(s
->avctx
, AV_LOG_ERROR
,
532 "Invalid bit allocation index\n");
536 get_bitalloc(&s
->gb
, &dca_bitalloc_index
, s
->bitalloc_huffman
[j
]);
539 if (s
->bitalloc
[j
][k
] > 26) {
540 // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
541 // j, k, s->bitalloc[j][k]);
547 /* Transition mode */
548 for (j
= 0; j
< s
->prim_channels
; j
++) {
549 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
550 s
->transition_mode
[j
][k
] = 0;
551 if (s
->subsubframes
> 1 &&
552 k
< s
->vq_start_subband
[j
] && s
->bitalloc
[j
][k
] > 0) {
553 s
->transition_mode
[j
][k
] =
554 get_bitalloc(&s
->gb
, &dca_tmode
, s
->transient_huffman
[j
]);
559 for (j
= 0; j
< s
->prim_channels
; j
++) {
560 const uint32_t *scale_table
;
563 memset(s
->scale_factor
[j
], 0, s
->subband_activity
[j
] * sizeof(s
->scale_factor
[0][0][0]) * 2);
565 if (s
->scalefactor_huffman
[j
] == 6)
566 scale_table
= scale_factor_quant7
;
568 scale_table
= scale_factor_quant6
;
570 /* When huffman coded, only the difference is encoded */
573 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
574 if (k
>= s
->vq_start_subband
[j
] || s
->bitalloc
[j
][k
] > 0) {
575 scale_sum
= get_scale(&s
->gb
, s
->scalefactor_huffman
[j
], scale_sum
);
576 s
->scale_factor
[j
][k
][0] = scale_table
[scale_sum
];
579 if (k
< s
->vq_start_subband
[j
] && s
->transition_mode
[j
][k
]) {
580 /* Get second scale factor */
581 scale_sum
= get_scale(&s
->gb
, s
->scalefactor_huffman
[j
], scale_sum
);
582 s
->scale_factor
[j
][k
][1] = scale_table
[scale_sum
];
587 /* Joint subband scale factor codebook select */
588 for (j
= 0; j
< s
->prim_channels
; j
++) {
589 /* Transmitted only if joint subband coding enabled */
590 if (s
->joint_intensity
[j
] > 0)
591 s
->joint_huff
[j
] = get_bits(&s
->gb
, 3);
594 /* Scale factors for joint subband coding */
595 for (j
= 0; j
< s
->prim_channels
; j
++) {
598 /* Transmitted only if joint subband coding enabled */
599 if (s
->joint_intensity
[j
] > 0) {
601 source_channel
= s
->joint_intensity
[j
] - 1;
603 /* When huffman coded, only the difference is encoded
604 * (is this valid as well for joint scales ???) */
606 for (k
= s
->subband_activity
[j
]; k
< s
->subband_activity
[source_channel
]; k
++) {
607 scale
= get_scale(&s
->gb
, s
->joint_huff
[j
], 0);
608 scale
+= 64; /* bias */
609 s
->joint_scale_factor
[j
][k
] = scale
; /*joint_scale_table[scale]; */
612 if (!s
->debug_flag
& 0x02) {
613 av_log(s
->avctx
, AV_LOG_DEBUG
,
614 "Joint stereo coding not supported\n");
615 s
->debug_flag
|= 0x02;
620 /* Stereo downmix coefficients */
621 if (s
->prim_channels
> 2) {
623 for (j
= 0; j
< s
->prim_channels
; j
++) {
624 s
->downmix_coef
[j
][0] = get_bits(&s
->gb
, 7);
625 s
->downmix_coef
[j
][1] = get_bits(&s
->gb
, 7);
628 int am
= s
->amode
& DCA_CHANNEL_MASK
;
629 for (j
= 0; j
< s
->prim_channels
; j
++) {
630 s
->downmix_coef
[j
][0] = dca_default_coeffs
[am
][j
][0];
631 s
->downmix_coef
[j
][1] = dca_default_coeffs
[am
][j
][1];
636 /* Dynamic range coefficient */
638 s
->dynrange_coef
= get_bits(&s
->gb
, 8);
640 /* Side information CRC check word */
641 if (s
->crc_present
) {
642 get_bits(&s
->gb
, 16);
646 * Primary audio data arrays
649 /* VQ encoded high frequency subbands */
650 for (j
= 0; j
< s
->prim_channels
; j
++)
651 for (k
= s
->vq_start_subband
[j
]; k
< s
->subband_activity
[j
]; k
++)
652 /* 1 vector -> 32 samples */
653 s
->high_freq_vq
[j
][k
] = get_bits(&s
->gb
, 10);
655 /* Low frequency effect data */
658 int lfe_samples
= 2 * s
->lfe
* s
->subsubframes
;
661 for (j
= lfe_samples
; j
< lfe_samples
* 2; j
++) {
662 /* Signed 8 bits int */
663 s
->lfe_data
[j
] = get_sbits(&s
->gb
, 8);
666 /* Scale factor index */
667 s
->lfe_scale_factor
= scale_factor_quant7
[get_bits(&s
->gb
, 8)];
669 /* Quantization step size * scale factor */
670 lfe_scale
= 0.035 * s
->lfe_scale_factor
;
672 for (j
= lfe_samples
; j
< lfe_samples
* 2; j
++)
673 s
->lfe_data
[j
] *= lfe_scale
;
677 av_log(s
->avctx
, AV_LOG_DEBUG
, "subsubframes: %i\n", s
->subsubframes
);
678 av_log(s
->avctx
, AV_LOG_DEBUG
, "partial samples: %i\n",
680 for (j
= 0; j
< s
->prim_channels
; j
++) {
681 av_log(s
->avctx
, AV_LOG_DEBUG
, "prediction mode:");
682 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
683 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->prediction_mode
[j
][k
]);
684 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
686 for (j
= 0; j
< s
->prim_channels
; j
++) {
687 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
688 av_log(s
->avctx
, AV_LOG_DEBUG
,
689 "prediction coefs: %f, %f, %f, %f\n",
690 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][0] / 8192,
691 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][1] / 8192,
692 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][2] / 8192,
693 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][3] / 8192);
695 for (j
= 0; j
< s
->prim_channels
; j
++) {
696 av_log(s
->avctx
, AV_LOG_DEBUG
, "bitalloc index: ");
697 for (k
= 0; k
< s
->vq_start_subband
[j
]; k
++)
698 av_log(s
->avctx
, AV_LOG_DEBUG
, "%2.2i ", s
->bitalloc
[j
][k
]);
699 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
701 for (j
= 0; j
< s
->prim_channels
; j
++) {
702 av_log(s
->avctx
, AV_LOG_DEBUG
, "Transition mode:");
703 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
704 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->transition_mode
[j
][k
]);
705 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
707 for (j
= 0; j
< s
->prim_channels
; j
++) {
708 av_log(s
->avctx
, AV_LOG_DEBUG
, "Scale factor:");
709 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
710 if (k
>= s
->vq_start_subband
[j
] || s
->bitalloc
[j
][k
] > 0)
711 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->scale_factor
[j
][k
][0]);
712 if (k
< s
->vq_start_subband
[j
] && s
->transition_mode
[j
][k
])
713 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i(t)", s
->scale_factor
[j
][k
][1]);
715 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
717 for (j
= 0; j
< s
->prim_channels
; j
++) {
718 if (s
->joint_intensity
[j
] > 0) {
719 int source_channel
= s
->joint_intensity
[j
] - 1;
720 av_log(s
->avctx
, AV_LOG_DEBUG
, "Joint scale factor index:\n");
721 for (k
= s
->subband_activity
[j
]; k
< s
->subband_activity
[source_channel
]; k
++)
722 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->joint_scale_factor
[j
][k
]);
723 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
726 if (s
->prim_channels
> 2 && s
->downmix
) {
727 av_log(s
->avctx
, AV_LOG_DEBUG
, "Downmix coeffs:\n");
728 for (j
= 0; j
< s
->prim_channels
; j
++) {
729 av_log(s
->avctx
, AV_LOG_DEBUG
, "Channel 0,%d = %f\n", j
, dca_downmix_coeffs
[s
->downmix_coef
[j
][0]]);
730 av_log(s
->avctx
, AV_LOG_DEBUG
, "Channel 1,%d = %f\n", j
, dca_downmix_coeffs
[s
->downmix_coef
[j
][1]]);
732 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
734 for (j
= 0; j
< s
->prim_channels
; j
++)
735 for (k
= s
->vq_start_subband
[j
]; k
< s
->subband_activity
[j
]; k
++)
736 av_log(s
->avctx
, AV_LOG_DEBUG
, "VQ index: %i\n", s
->high_freq_vq
[j
][k
]);
738 int lfe_samples
= 2 * s
->lfe
* s
->subsubframes
;
739 av_log(s
->avctx
, AV_LOG_DEBUG
, "LFE samples:\n");
740 for (j
= lfe_samples
; j
< lfe_samples
* 2; j
++)
741 av_log(s
->avctx
, AV_LOG_DEBUG
, " %f", s
->lfe_data
[j
]);
742 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
749 static void qmf_32_subbands(DCAContext
* s
, int chans
,
750 float samples_in
[32][8], float *samples_out
,
751 float scale
, float bias
)
753 const float *prCoeff
;
756 int hist_index
= s
->hist_index
[chans
];
757 float *subband_fir_hist2
= s
->subband_fir_noidea
[chans
];
761 scale
*= sqrt(1/8.0);
764 if (!s
->multirate_inter
) /* Non-perfect reconstruction */
765 prCoeff
= fir_32bands_nonperfect
;
766 else /* Perfect reconstruction */
767 prCoeff
= fir_32bands_perfect
;
769 /* Reconstructed channel sample index */
770 for (subindex
= 0; subindex
< 8; subindex
++) {
771 float *subband_fir_hist
= s
->subband_fir_hist
[chans
] + hist_index
;
772 /* Load in one sample from each subband and clear inactive subbands */
773 for (i
= 0; i
< s
->subband_activity
[chans
]; i
++){
774 if((i
-1)&2) s
->raXin
[i
] = -samples_in
[i
][subindex
];
775 else s
->raXin
[i
] = samples_in
[i
][subindex
];
780 ff_imdct_half(&s
->imdct
, subband_fir_hist
, s
->raXin
);
782 /* Multiply by filter coefficients */
783 for (i
= 0; i
< 16; i
++){
784 float a
= subband_fir_hist2
[i
];
785 float b
= subband_fir_hist2
[i
+16];
788 for (j
= 0; j
< 512-hist_index
; j
+= 64){
789 a
+= prCoeff
[i
+j
]*(-subband_fir_hist
[15-i
+j
]);
790 b
+= prCoeff
[i
+j
+16]*( subband_fir_hist
[ i
+j
]);
791 c
+= prCoeff
[i
+j
+32]*( subband_fir_hist
[16+i
+j
]);
792 d
+= prCoeff
[i
+j
+48]*( subband_fir_hist
[31-i
+j
]);
794 for ( ; j
< 512; j
+= 64){
795 a
+= prCoeff
[i
+j
]*(-subband_fir_hist
[15-i
+j
-512]);
796 b
+= prCoeff
[i
+j
+16]*( subband_fir_hist
[ i
+j
-512]);
797 c
+= prCoeff
[i
+j
+32]*( subband_fir_hist
[16+i
+j
-512]);
798 d
+= prCoeff
[i
+j
+48]*( subband_fir_hist
[31-i
+j
-512]);
800 samples_out
[i
] = a
* scale
+ bias
;
801 samples_out
[i
+16] = b
* scale
+ bias
;
802 subband_fir_hist2
[i
] = c
;
803 subband_fir_hist2
[i
+16] = d
;
807 hist_index
= (hist_index
-32)&511;
809 s
->hist_index
[chans
]= hist_index
;
812 static void lfe_interpolation_fir(int decimation_select
,
813 int num_deci_sample
, float *samples_in
,
814 float *samples_out
, float scale
,
817 /* samples_in: An array holding decimated samples.
818 * Samples in current subframe starts from samples_in[0],
819 * while samples_in[-1], samples_in[-2], ..., stores samples
820 * from last subframe as history.
822 * samples_out: An array holding interpolated samples
825 int decifactor
, k
, j
;
826 const float *prCoeff
;
828 int interp_index
= 0; /* Index to the interpolated samples */
831 /* Select decimation filter */
832 if (decimation_select
== 1) {
834 prCoeff
= lfe_fir_128
;
837 prCoeff
= lfe_fir_64
;
840 for (deciindex
= 0; deciindex
< num_deci_sample
; deciindex
++) {
841 /* One decimated sample generates decifactor interpolated ones */
842 for (k
= 0; k
< decifactor
; k
++) {
844 //FIXME the coeffs are symetric, fix that
845 for (j
= 0; j
< 512 / decifactor
; j
++)
846 rTmp
+= samples_in
[deciindex
- j
] * prCoeff
[k
+ j
* decifactor
];
847 samples_out
[interp_index
++] = (rTmp
* scale
) + bias
;
852 /* downmixing routines */
853 #define MIX_REAR1(samples, si1, rs, coef) \
854 samples[i] += samples[si1] * coef[rs][0]; \
855 samples[i+256] += samples[si1] * coef[rs][1];
857 #define MIX_REAR2(samples, si1, si2, rs, coef) \
858 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
859 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
861 #define MIX_FRONT3(samples, coef) \
863 samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
864 samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
866 #define DOWNMIX_TO_STEREO(op1, op2) \
867 for(i = 0; i < 256; i++){ \
872 static void dca_downmix(float *samples
, int srcfmt
,
873 int downmix_coef
[DCA_PRIM_CHANNELS_MAX
][2])
877 float coef
[DCA_PRIM_CHANNELS_MAX
][2];
879 for(i
=0; i
<DCA_PRIM_CHANNELS_MAX
; i
++) {
880 coef
[i
][0] = dca_downmix_coeffs
[downmix_coef
[i
][0]];
881 coef
[i
][1] = dca_downmix_coeffs
[downmix_coef
[i
][1]];
887 case DCA_STEREO_TOTAL
:
888 case DCA_STEREO_SUMDIFF
:
890 av_log(NULL
, 0, "Not implemented!\n");
895 DOWNMIX_TO_STEREO(MIX_FRONT3(samples
, coef
),);
898 DOWNMIX_TO_STEREO(MIX_REAR1(samples
, i
+ 512, 2, coef
),);
901 DOWNMIX_TO_STEREO(MIX_FRONT3(samples
, coef
),
902 MIX_REAR1(samples
, i
+ 768, 3, coef
));
905 DOWNMIX_TO_STEREO(MIX_REAR2(samples
, i
+ 512, i
+ 768, 2, coef
),);
908 DOWNMIX_TO_STEREO(MIX_FRONT3(samples
, coef
),
909 MIX_REAR2(samples
, i
+ 768, i
+ 1024, 3, coef
));
915 /* Very compact version of the block code decoder that does not use table
916 * look-up but is slightly slower */
917 static int decode_blockcode(int code
, int levels
, int *values
)
920 int offset
= (levels
- 1) >> 1;
922 for (i
= 0; i
< 4; i
++) {
923 values
[i
] = (code
% levels
) - offset
;
930 av_log(NULL
, AV_LOG_ERROR
, "ERROR: block code look-up failed\n");
935 static const uint8_t abits_sizes
[7] = { 7, 10, 12, 13, 15, 17, 19 };
936 static const uint8_t abits_levels
[7] = { 3, 5, 7, 9, 13, 17, 25 };
938 static int dca_subsubframe(DCAContext
* s
)
941 int subsubframe
= s
->current_subsubframe
;
943 const float *quant_step_table
;
946 float subband_samples
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
][8];
952 /* Select quantization step size table */
953 if (s
->bit_rate_index
== 0x1f)
954 quant_step_table
= lossless_quant_d
;
956 quant_step_table
= lossy_quant_d
;
958 for (k
= 0; k
< s
->prim_channels
; k
++) {
959 for (l
= 0; l
< s
->vq_start_subband
[k
]; l
++) {
962 /* Select the mid-tread linear quantizer */
963 int abits
= s
->bitalloc
[k
][l
];
965 float quant_step_size
= quant_step_table
[abits
];
969 * Determine quantization index code book and its type
972 /* Select quantization index code book */
973 int sel
= s
->quant_index_huffman
[k
][abits
];
976 * Extract bits from the bit stream
979 memset(subband_samples
[k
][l
], 0, 8 * sizeof(subband_samples
[0][0][0]));
980 }else if(abits
>= 11 || !dca_smpl_bitalloc
[abits
].vlc
[sel
].table
){
983 int block_code1
, block_code2
, size
, levels
;
986 size
= abits_sizes
[abits
-1];
987 levels
= abits_levels
[abits
-1];
989 block_code1
= get_bits(&s
->gb
, size
);
990 /* FIXME Should test return value */
991 decode_blockcode(block_code1
, levels
, block
);
992 block_code2
= get_bits(&s
->gb
, size
);
993 decode_blockcode(block_code2
, levels
, &block
[4]);
994 for (m
= 0; m
< 8; m
++)
995 subband_samples
[k
][l
][m
] = block
[m
];
998 for (m
= 0; m
< 8; m
++)
999 subband_samples
[k
][l
][m
] = get_sbits(&s
->gb
, abits
- 3);
1003 for (m
= 0; m
< 8; m
++)
1004 subband_samples
[k
][l
][m
] = get_bitalloc(&s
->gb
, &dca_smpl_bitalloc
[abits
], sel
);
1007 /* Deal with transients */
1008 if (s
->transition_mode
[k
][l
] &&
1009 subsubframe
>= s
->transition_mode
[k
][l
])
1010 rscale
= quant_step_size
* s
->scale_factor
[k
][l
][1];
1012 rscale
= quant_step_size
* s
->scale_factor
[k
][l
][0];
1014 rscale
*= s
->scalefactor_adj
[k
][sel
];
1016 for (m
= 0; m
< 8; m
++)
1017 subband_samples
[k
][l
][m
] *= rscale
;
1020 * Inverse ADPCM if in prediction mode
1022 if (s
->prediction_mode
[k
][l
]) {
1024 for (m
= 0; m
< 8; m
++) {
1025 for (n
= 1; n
<= 4; n
++)
1027 subband_samples
[k
][l
][m
] +=
1028 (adpcm_vb
[s
->prediction_vq
[k
][l
]][n
- 1] *
1029 subband_samples
[k
][l
][m
- n
] / 8192);
1030 else if (s
->predictor_history
)
1031 subband_samples
[k
][l
][m
] +=
1032 (adpcm_vb
[s
->prediction_vq
[k
][l
]][n
- 1] *
1033 s
->subband_samples_hist
[k
][l
][m
- n
+
1040 * Decode VQ encoded high frequencies
1042 for (l
= s
->vq_start_subband
[k
]; l
< s
->subband_activity
[k
]; l
++) {
1043 /* 1 vector -> 32 samples but we only need the 8 samples
1044 * for this subsubframe. */
1047 if (!s
->debug_flag
& 0x01) {
1048 av_log(s
->avctx
, AV_LOG_DEBUG
, "Stream with high frequencies VQ coding\n");
1049 s
->debug_flag
|= 0x01;
1052 for (m
= 0; m
< 8; m
++) {
1053 subband_samples
[k
][l
][m
] =
1054 high_freq_vq
[s
->high_freq_vq
[k
][l
]][subsubframe
* 8 +
1056 * (float) s
->scale_factor
[k
][l
][0] / 16.0;
1061 /* Check for DSYNC after subsubframe */
1062 if (s
->aspf
|| subsubframe
== s
->subsubframes
- 1) {
1063 if (0xFFFF == get_bits(&s
->gb
, 16)) { /* 0xFFFF */
1065 av_log(s
->avctx
, AV_LOG_DEBUG
, "Got subframe DSYNC\n");
1068 av_log(s
->avctx
, AV_LOG_ERROR
, "Didn't get subframe DSYNC\n");
1072 /* Backup predictor history for adpcm */
1073 for (k
= 0; k
< s
->prim_channels
; k
++)
1074 for (l
= 0; l
< s
->vq_start_subband
[k
]; l
++)
1075 memcpy(s
->subband_samples_hist
[k
][l
], &subband_samples
[k
][l
][4],
1076 4 * sizeof(subband_samples
[0][0][0]));
1078 /* 32 subbands QMF */
1079 for (k
= 0; k
< s
->prim_channels
; k
++) {
1080 /* static float pcm_to_double[8] =
1081 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
1082 qmf_32_subbands(s
, k
, subband_samples
[k
], &s
->samples
[256 * s
->channel_order_tab
[k
]],
1083 M_SQRT1_2
*s
->scale_bias
/*pcm_to_double[s->source_pcm_res] */ ,
1089 if (s
->prim_channels
> dca_channels
[s
->output
& DCA_CHANNEL_MASK
]) {
1090 dca_downmix(s
->samples
, s
->amode
, s
->downmix_coef
);
1093 /* Generate LFE samples for this subsubframe FIXME!!! */
1094 if (s
->output
& DCA_LFE
) {
1095 int lfe_samples
= 2 * s
->lfe
* s
->subsubframes
;
1097 lfe_interpolation_fir(s
->lfe
, 2 * s
->lfe
,
1098 s
->lfe_data
+ lfe_samples
+
1099 2 * s
->lfe
* subsubframe
,
1100 &s
->samples
[256 * dca_lfe_index
[s
->amode
]],
1101 (1.0/256.0)*s
->scale_bias
, s
->add_bias
);
1102 /* Outputs 20bits pcm samples */
1109 static int dca_subframe_footer(DCAContext
* s
)
1111 int aux_data_count
= 0, i
;
1115 * Unpack optional information
1119 get_bits(&s
->gb
, 32);
1122 aux_data_count
= get_bits(&s
->gb
, 6);
1124 for (i
= 0; i
< aux_data_count
; i
++)
1125 get_bits(&s
->gb
, 8);
1127 if (s
->crc_present
&& (s
->downmix
|| s
->dynrange
))
1128 get_bits(&s
->gb
, 16);
1130 lfe_samples
= 2 * s
->lfe
* s
->subsubframes
;
1131 for (i
= 0; i
< lfe_samples
; i
++) {
1132 s
->lfe_data
[i
] = s
->lfe_data
[i
+ lfe_samples
];
1139 * Decode a dca frame block
1141 * @param s pointer to the DCAContext
1144 static int dca_decode_block(DCAContext
* s
)
1148 if (s
->current_subframe
>= s
->subframes
) {
1149 av_log(s
->avctx
, AV_LOG_DEBUG
, "check failed: %i>%i",
1150 s
->current_subframe
, s
->subframes
);
1154 if (!s
->current_subsubframe
) {
1156 av_log(s
->avctx
, AV_LOG_DEBUG
, "DSYNC dca_subframe_header\n");
1158 /* Read subframe header */
1159 if (dca_subframe_header(s
))
1163 /* Read subsubframe */
1165 av_log(s
->avctx
, AV_LOG_DEBUG
, "DSYNC dca_subsubframe\n");
1167 if (dca_subsubframe(s
))
1171 s
->current_subsubframe
++;
1172 if (s
->current_subsubframe
>= s
->subsubframes
) {
1173 s
->current_subsubframe
= 0;
1174 s
->current_subframe
++;
1176 if (s
->current_subframe
>= s
->subframes
) {
1178 av_log(s
->avctx
, AV_LOG_DEBUG
, "DSYNC dca_subframe_footer\n");
1180 /* Read subframe footer */
1181 if (dca_subframe_footer(s
))
1189 * Convert bitstream to one representation based on sync marker
1191 static int dca_convert_bitstream(const uint8_t * src
, int src_size
, uint8_t * dst
,
1196 const uint16_t *ssrc
= (const uint16_t *) src
;
1197 uint16_t *sdst
= (uint16_t *) dst
;
1200 if((unsigned)src_size
> (unsigned)max_size
) {
1201 // av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
1203 src_size
= max_size
;
1208 case DCA_MARKER_RAW_BE
:
1209 memcpy(dst
, src
, src_size
);
1211 case DCA_MARKER_RAW_LE
:
1212 for (i
= 0; i
< (src_size
+ 1) >> 1; i
++)
1213 *sdst
++ = bswap_16(*ssrc
++);
1215 case DCA_MARKER_14B_BE
:
1216 case DCA_MARKER_14B_LE
:
1217 init_put_bits(&pb
, dst
, max_size
);
1218 for (i
= 0; i
< (src_size
+ 1) >> 1; i
++, src
+= 2) {
1219 tmp
= ((mrk
== DCA_MARKER_14B_BE
) ? AV_RB16(src
) : AV_RL16(src
)) & 0x3FFF;
1220 put_bits(&pb
, 14, tmp
);
1222 flush_put_bits(&pb
);
1223 return (put_bits_count(&pb
) + 7) >> 3;
1230 * Main frame decoding function
1231 * FIXME add arguments
1233 static int dca_decode_frame(AVCodecContext
* avctx
,
1234 void *data
, int *data_size
,
1237 const uint8_t *buf
= avpkt
->data
;
1238 int buf_size
= avpkt
->size
;
1241 int16_t *samples
= data
;
1242 DCAContext
*s
= avctx
->priv_data
;
1246 s
->dca_buffer_size
= dca_convert_bitstream(buf
, buf_size
, s
->dca_buffer
, DCA_MAX_FRAME_SIZE
);
1247 if (s
->dca_buffer_size
== -1) {
1248 av_log(avctx
, AV_LOG_ERROR
, "Not a valid DCA frame\n");
1252 init_get_bits(&s
->gb
, s
->dca_buffer
, s
->dca_buffer_size
* 8);
1253 if (dca_parse_frame_header(s
) < 0) {
1254 //seems like the frame is corrupt, try with the next one
1258 //set AVCodec values with parsed data
1259 avctx
->sample_rate
= s
->sample_rate
;
1260 avctx
->bit_rate
= s
->bit_rate
;
1262 channels
= s
->prim_channels
+ !!s
->lfe
;
1265 avctx
->channel_layout
= dca_core_channel_layout
[s
->amode
];
1268 avctx
->channel_layout
|= CH_LOW_FREQUENCY
;
1269 s
->channel_order_tab
= dca_channel_reorder_lfe
[s
->amode
];
1271 s
->channel_order_tab
= dca_channel_reorder_nolfe
[s
->amode
];
1273 if(avctx
->request_channels
== 2 && s
->prim_channels
> 2) {
1275 s
->output
= DCA_STEREO
;
1276 avctx
->channel_layout
= CH_LAYOUT_STEREO
;
1279 av_log(avctx
, AV_LOG_ERROR
, "Non standard configuration %d !\n",s
->amode
);
1284 /* There is nothing that prevents a dts frame to change channel configuration
1285 but FFmpeg doesn't support that so only set the channels if it is previously
1286 unset. Ideally during the first probe for channels the crc should be checked
1287 and only set avctx->channels when the crc is ok. Right now the decoder could
1288 set the channels based on a broken first frame.*/
1289 if (!avctx
->channels
)
1290 avctx
->channels
= channels
;
1292 if(*data_size
< (s
->sample_blocks
/ 8) * 256 * sizeof(int16_t) * channels
)
1294 *data_size
= 256 / 8 * s
->sample_blocks
* sizeof(int16_t) * channels
;
1295 for (i
= 0; i
< (s
->sample_blocks
/ 8); i
++) {
1296 dca_decode_block(s
);
1297 s
->dsp
.float_to_int16_interleave(samples
, s
->samples_chanptr
, 256, channels
);
1298 samples
+= 256 * channels
;
1307 * DCA initialization
1309 * @param avctx pointer to the AVCodecContext
1312 static av_cold
int dca_decode_init(AVCodecContext
* avctx
)
1314 DCAContext
*s
= avctx
->priv_data
;
1320 dsputil_init(&s
->dsp
, avctx
);
1321 ff_mdct_init(&s
->imdct
, 6, 1, 1.0);
1323 for(i
= 0; i
< 6; i
++)
1324 s
->samples_chanptr
[i
] = s
->samples
+ i
* 256;
1325 avctx
->sample_fmt
= SAMPLE_FMT_S16
;
1327 if(s
->dsp
.float_to_int16
== ff_float_to_int16_c
) {
1328 s
->add_bias
= 385.0f
;
1329 s
->scale_bias
= 1.0 / 32768.0;
1332 s
->scale_bias
= 1.0;
1334 /* allow downmixing to stereo */
1335 if (avctx
->channels
> 0 && avctx
->request_channels
< avctx
->channels
&&
1336 avctx
->request_channels
== 2) {
1337 avctx
->channels
= avctx
->request_channels
;
1345 static av_cold
int dca_decode_end(AVCodecContext
* avctx
)
1347 DCAContext
*s
= avctx
->priv_data
;
1348 ff_mdct_end(&s
->imdct
);
1352 AVCodec dca_decoder
= {
1354 .type
= CODEC_TYPE_AUDIO
,
1356 .priv_data_size
= sizeof(DCAContext
),
1357 .init
= dca_decode_init
,
1358 .decode
= dca_decode_frame
,
1359 .close
= dca_decode_end
,
1360 .long_name
= NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),