Mercurial > libavcodec.hg
annotate dca.c @ 6829:97e57c7986fd libavcodec
Cosmetics: rename variable
author | vitor |
---|---|
date | Sat, 17 May 2008 14:39:47 +0000 |
parents | a4104482ceef |
children | e943e1409077 |
rev | line source |
---|---|
4599 | 1 /* |
2 * DCA compatible decoder | |
3 * Copyright (C) 2004 Gildas Bazin | |
4 * Copyright (C) 2004 Benjamin Zores | |
5 * Copyright (C) 2006 Benjamin Larsson | |
6 * Copyright (C) 2007 Konstantin Shishkov | |
7 * | |
8 * This file is part of FFmpeg. | |
9 * | |
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. | |
14 * | |
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. | |
19 * | |
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 | |
23 */ | |
24 | |
25 /** | |
26 * @file dca.c | |
27 */ | |
28 | |
29 #include <math.h> | |
30 #include <stddef.h> | |
31 #include <stdio.h> | |
32 | |
33 #include "avcodec.h" | |
34 #include "dsputil.h" | |
35 #include "bitstream.h" | |
36 #include "dcadata.h" | |
37 #include "dcahuff.h" | |
4899 | 38 #include "dca.h" |
4599 | 39 |
40 //#define TRACE | |
41 | |
42 #define DCA_PRIM_CHANNELS_MAX (5) | |
43 #define DCA_SUBBANDS (32) | |
44 #define DCA_ABITS_MAX (32) /* Should be 28 */ | |
45 #define DCA_SUBSUBFAMES_MAX (4) | |
46 #define DCA_LFE_MAX (3) | |
47 | |
48 enum DCAMode { | |
49 DCA_MONO = 0, | |
50 DCA_CHANNEL, | |
51 DCA_STEREO, | |
52 DCA_STEREO_SUMDIFF, | |
53 DCA_STEREO_TOTAL, | |
54 DCA_3F, | |
55 DCA_2F1R, | |
56 DCA_3F1R, | |
57 DCA_2F2R, | |
58 DCA_3F2R, | |
59 DCA_4F2R | |
60 }; | |
61 | |
62 #define DCA_DOLBY 101 /* FIXME */ | |
63 | |
64 #define DCA_CHANNEL_BITS 6 | |
65 #define DCA_CHANNEL_MASK 0x3F | |
66 | |
67 #define DCA_LFE 0x80 | |
68 | |
69 #define HEADER_SIZE 14 | |
70 #define CONVERT_BIAS 384 | |
71 | |
72 #define DCA_MAX_FRAME_SIZE 16383 | |
73 | |
74 /** Bit allocation */ | |
75 typedef struct { | |
76 int offset; ///< code values offset | |
77 int maxbits[8]; ///< max bits in VLC | |
78 int wrap; ///< wrap for get_vlc2() | |
79 VLC vlc[8]; ///< actual codes | |
80 } BitAlloc; | |
81 | |
82 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select | |
83 static BitAlloc dca_tmode; ///< transition mode VLCs | |
84 static BitAlloc dca_scalefactor; ///< scalefactor VLCs | |
85 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs | |
86 | |
87 /** Pre-calculated cosine modulation coefs for the QMF */ | |
88 static float cos_mod[544]; | |
89 | |
4908
777f250df232
Fix multiple "¡Æinline/static¡Ç is not at beginning of declaration" warnings.
diego
parents:
4899
diff
changeset
|
90 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx) |
4599 | 91 { |
92 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset; | |
93 } | |
94 | |
95 typedef struct { | |
96 AVCodecContext *avctx; | |
97 /* Frame header */ | |
98 int frame_type; ///< type of the current frame | |
99 int samples_deficit; ///< deficit sample count | |
100 int crc_present; ///< crc is present in the bitstream | |
101 int sample_blocks; ///< number of PCM sample blocks | |
102 int frame_size; ///< primary frame byte size | |
103 int amode; ///< audio channels arrangement | |
104 int sample_rate; ///< audio sampling rate | |
105 int bit_rate; ///< transmission bit rate | |
106 | |
107 int downmix; ///< embedded downmix enabled | |
108 int dynrange; ///< embedded dynamic range flag | |
109 int timestamp; ///< embedded time stamp flag | |
110 int aux_data; ///< auxiliary data flag | |
111 int hdcd; ///< source material is mastered in HDCD | |
112 int ext_descr; ///< extension audio descriptor flag | |
113 int ext_coding; ///< extended coding flag | |
114 int aspf; ///< audio sync word insertion flag | |
115 int lfe; ///< low frequency effects flag | |
116 int predictor_history; ///< predictor history flag | |
117 int header_crc; ///< header crc check bytes | |
118 int multirate_inter; ///< multirate interpolator switch | |
119 int version; ///< encoder software revision | |
120 int copy_history; ///< copy history | |
121 int source_pcm_res; ///< source pcm resolution | |
122 int front_sum; ///< front sum/difference flag | |
123 int surround_sum; ///< surround sum/difference flag | |
124 int dialog_norm; ///< dialog normalisation parameter | |
125 | |
126 /* Primary audio coding header */ | |
127 int subframes; ///< number of subframes | |
6463 | 128 int total_channels; ///< number of channels including extensions |
4599 | 129 int prim_channels; ///< number of primary audio channels |
130 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count | |
131 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband | |
132 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index | |
133 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book | |
134 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book | |
135 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select | |
136 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select | |
137 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment | |
138 | |
139 /* Primary audio coding side information */ | |
140 int subsubframes; ///< number of subsubframes | |
141 int partial_samples; ///< partial subsubframe samples count | |
142 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not) | |
143 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs | |
144 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index | |
145 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients) | |
146 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient) | |
147 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook | |
148 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors | |
149 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients | |
150 int dynrange_coef; ///< dynamic range coefficient | |
151 | |
152 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands | |
153 | |
154 float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX * | |
155 2 /*history */ ]; ///< Low frequency effect data | |
156 int lfe_scale_factor; | |
157 | |
158 /* Subband samples history (for ADPCM) */ | |
159 float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4]; | |
160 float subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512]; | |
161 float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][64]; | |
162 | |
163 int output; ///< type of output | |
164 int bias; ///< output bias | |
165 | |
166 DECLARE_ALIGNED_16(float, samples[1536]); /* 6 * 256 = 1536, might only need 5 */ | |
167 DECLARE_ALIGNED_16(int16_t, tsamples[1536]); | |
168 | |
169 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE]; | |
170 int dca_buffer_size; ///< how much data is in the dca_buffer | |
171 | |
172 GetBitContext gb; | |
173 /* Current position in DCA frame */ | |
174 int current_subframe; | |
175 int current_subsubframe; | |
176 | |
177 int debug_flag; ///< used for suppressing repeated error messages output | |
178 DSPContext dsp; | |
179 } DCAContext; | |
180 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6463
diff
changeset
|
181 static av_cold void dca_init_vlcs(void) |
4599 | 182 { |
6350 | 183 static int vlcs_initialized = 0; |
4599 | 184 int i, j; |
185 | |
6350 | 186 if (vlcs_initialized) |
4599 | 187 return; |
188 | |
189 dca_bitalloc_index.offset = 1; | |
5070 | 190 dca_bitalloc_index.wrap = 2; |
4599 | 191 for (i = 0; i < 5; i++) |
192 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12, | |
193 bitalloc_12_bits[i], 1, 1, | |
194 bitalloc_12_codes[i], 2, 2, 1); | |
195 dca_scalefactor.offset = -64; | |
196 dca_scalefactor.wrap = 2; | |
197 for (i = 0; i < 5; i++) | |
198 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129, | |
199 scales_bits[i], 1, 1, | |
200 scales_codes[i], 2, 2, 1); | |
201 dca_tmode.offset = 0; | |
202 dca_tmode.wrap = 1; | |
203 for (i = 0; i < 4; i++) | |
204 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4, | |
205 tmode_bits[i], 1, 1, | |
206 tmode_codes[i], 2, 2, 1); | |
207 | |
208 for(i = 0; i < 10; i++) | |
209 for(j = 0; j < 7; j++){ | |
210 if(!bitalloc_codes[i][j]) break; | |
211 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i]; | |
212 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4); | |
213 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j], | |
214 bitalloc_sizes[i], | |
215 bitalloc_bits[i][j], 1, 1, | |
216 bitalloc_codes[i][j], 2, 2, 1); | |
217 } | |
6350 | 218 vlcs_initialized = 1; |
4599 | 219 } |
220 | |
221 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits) | |
222 { | |
223 while(len--) | |
224 *dst++ = get_bits(gb, bits); | |
225 } | |
226 | |
227 static int dca_parse_frame_header(DCAContext * s) | |
228 { | |
229 int i, j; | |
230 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 }; | |
231 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 }; | |
232 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 }; | |
233 | |
234 s->bias = CONVERT_BIAS; | |
235 | |
236 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8); | |
237 | |
238 /* Sync code */ | |
239 get_bits(&s->gb, 32); | |
240 | |
241 /* Frame header */ | |
242 s->frame_type = get_bits(&s->gb, 1); | |
243 s->samples_deficit = get_bits(&s->gb, 5) + 1; | |
244 s->crc_present = get_bits(&s->gb, 1); | |
245 s->sample_blocks = get_bits(&s->gb, 7) + 1; | |
246 s->frame_size = get_bits(&s->gb, 14) + 1; | |
247 if (s->frame_size < 95) | |
248 return -1; | |
249 s->amode = get_bits(&s->gb, 6); | |
250 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)]; | |
251 if (!s->sample_rate) | |
252 return -1; | |
253 s->bit_rate = dca_bit_rates[get_bits(&s->gb, 5)]; | |
254 if (!s->bit_rate) | |
255 return -1; | |
256 | |
257 s->downmix = get_bits(&s->gb, 1); | |
258 s->dynrange = get_bits(&s->gb, 1); | |
259 s->timestamp = get_bits(&s->gb, 1); | |
260 s->aux_data = get_bits(&s->gb, 1); | |
261 s->hdcd = get_bits(&s->gb, 1); | |
262 s->ext_descr = get_bits(&s->gb, 3); | |
263 s->ext_coding = get_bits(&s->gb, 1); | |
264 s->aspf = get_bits(&s->gb, 1); | |
265 s->lfe = get_bits(&s->gb, 2); | |
266 s->predictor_history = get_bits(&s->gb, 1); | |
267 | |
268 /* TODO: check CRC */ | |
269 if (s->crc_present) | |
270 s->header_crc = get_bits(&s->gb, 16); | |
271 | |
272 s->multirate_inter = get_bits(&s->gb, 1); | |
273 s->version = get_bits(&s->gb, 4); | |
274 s->copy_history = get_bits(&s->gb, 2); | |
275 s->source_pcm_res = get_bits(&s->gb, 3); | |
276 s->front_sum = get_bits(&s->gb, 1); | |
277 s->surround_sum = get_bits(&s->gb, 1); | |
278 s->dialog_norm = get_bits(&s->gb, 4); | |
279 | |
280 /* FIXME: channels mixing levels */ | |
4893 | 281 s->output = s->amode; |
282 if(s->lfe) s->output |= DCA_LFE; | |
4599 | 283 |
284 #ifdef TRACE | |
285 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type); | |
286 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit); | |
287 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present); | |
288 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n", | |
289 s->sample_blocks, s->sample_blocks * 32); | |
290 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size); | |
291 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n", | |
292 s->amode, dca_channels[s->amode]); | |
293 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i (%i Hz)\n", | |
294 s->sample_rate, dca_sample_rates[s->sample_rate]); | |
295 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i (%i bits/s)\n", | |
296 s->bit_rate, dca_bit_rates[s->bit_rate]); | |
297 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix); | |
298 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange); | |
299 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp); | |
300 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data); | |
301 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd); | |
302 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr); | |
303 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding); | |
304 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf); | |
305 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe); | |
306 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n", | |
307 s->predictor_history); | |
308 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc); | |
309 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n", | |
310 s->multirate_inter); | |
311 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version); | |
312 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history); | |
313 av_log(s->avctx, AV_LOG_DEBUG, | |
314 "source pcm resolution: %i (%i bits/sample)\n", | |
315 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]); | |
316 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum); | |
317 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum); | |
318 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm); | |
319 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
320 #endif | |
321 | |
322 /* Primary audio coding header */ | |
323 s->subframes = get_bits(&s->gb, 4) + 1; | |
6463 | 324 s->total_channels = get_bits(&s->gb, 3) + 1; |
325 s->prim_channels = s->total_channels; | |
326 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX) | |
327 s->prim_channels = DCA_PRIM_CHANNELS_MAX; /* We only support DTS core */ | |
4599 | 328 |
329 | |
330 for (i = 0; i < s->prim_channels; i++) { | |
331 s->subband_activity[i] = get_bits(&s->gb, 5) + 2; | |
332 if (s->subband_activity[i] > DCA_SUBBANDS) | |
333 s->subband_activity[i] = DCA_SUBBANDS; | |
334 } | |
335 for (i = 0; i < s->prim_channels; i++) { | |
336 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1; | |
337 if (s->vq_start_subband[i] > DCA_SUBBANDS) | |
338 s->vq_start_subband[i] = DCA_SUBBANDS; | |
339 } | |
340 get_array(&s->gb, s->joint_intensity, s->prim_channels, 3); | |
341 get_array(&s->gb, s->transient_huffman, s->prim_channels, 2); | |
342 get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3); | |
343 get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3); | |
344 | |
345 /* Get codebooks quantization indexes */ | |
346 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman)); | |
347 for (j = 1; j < 11; j++) | |
348 for (i = 0; i < s->prim_channels; i++) | |
349 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]); | |
350 | |
351 /* Get scale factor adjustment */ | |
352 for (j = 0; j < 11; j++) | |
353 for (i = 0; i < s->prim_channels; i++) | |
354 s->scalefactor_adj[i][j] = 1; | |
355 | |
356 for (j = 1; j < 11; j++) | |
357 for (i = 0; i < s->prim_channels; i++) | |
358 if (s->quant_index_huffman[i][j] < thr[j]) | |
359 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)]; | |
360 | |
361 if (s->crc_present) { | |
362 /* Audio header CRC check */ | |
363 get_bits(&s->gb, 16); | |
364 } | |
365 | |
366 s->current_subframe = 0; | |
367 s->current_subsubframe = 0; | |
368 | |
369 #ifdef TRACE | |
370 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes); | |
371 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels); | |
372 for(i = 0; i < s->prim_channels; i++){ | |
373 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]); | |
374 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]); | |
375 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]); | |
376 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]); | |
377 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]); | |
378 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]); | |
379 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:"); | |
380 for (j = 0; j < 11; j++) | |
381 av_log(s->avctx, AV_LOG_DEBUG, " %i", | |
382 s->quant_index_huffman[i][j]); | |
383 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
384 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:"); | |
385 for (j = 0; j < 11; j++) | |
386 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]); | |
387 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
388 } | |
389 #endif | |
390 | |
391 return 0; | |
392 } | |
393 | |
394 | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
395 static inline int get_scale(GetBitContext *gb, int level, int value) |
4599 | 396 { |
397 if (level < 5) { | |
398 /* huffman encoded */ | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
399 value += get_bitalloc(gb, &dca_scalefactor, level); |
4599 | 400 } else if(level < 8) |
401 value = get_bits(gb, level + 1); | |
402 return value; | |
403 } | |
404 | |
405 static int dca_subframe_header(DCAContext * s) | |
406 { | |
407 /* Primary audio coding side information */ | |
408 int j, k; | |
409 | |
410 s->subsubframes = get_bits(&s->gb, 2) + 1; | |
411 s->partial_samples = get_bits(&s->gb, 3); | |
412 for (j = 0; j < s->prim_channels; j++) { | |
413 for (k = 0; k < s->subband_activity[j]; k++) | |
414 s->prediction_mode[j][k] = get_bits(&s->gb, 1); | |
415 } | |
416 | |
417 /* Get prediction codebook */ | |
418 for (j = 0; j < s->prim_channels; j++) { | |
419 for (k = 0; k < s->subband_activity[j]; k++) { | |
420 if (s->prediction_mode[j][k] > 0) { | |
421 /* (Prediction coefficient VQ address) */ | |
422 s->prediction_vq[j][k] = get_bits(&s->gb, 12); | |
423 } | |
424 } | |
425 } | |
426 | |
427 /* Bit allocation index */ | |
428 for (j = 0; j < s->prim_channels; j++) { | |
429 for (k = 0; k < s->vq_start_subband[j]; k++) { | |
430 if (s->bitalloc_huffman[j] == 6) | |
431 s->bitalloc[j][k] = get_bits(&s->gb, 5); | |
432 else if (s->bitalloc_huffman[j] == 5) | |
433 s->bitalloc[j][k] = get_bits(&s->gb, 4); | |
6463 | 434 else if (s->bitalloc_huffman[j] == 7) { |
435 av_log(s->avctx, AV_LOG_ERROR, | |
436 "Invalid bit allocation index\n"); | |
437 return -1; | |
438 } else { | |
4599 | 439 s->bitalloc[j][k] = |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
440 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]); |
4599 | 441 } |
442 | |
443 if (s->bitalloc[j][k] > 26) { | |
444 // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n", | |
445 // j, k, s->bitalloc[j][k]); | |
446 return -1; | |
447 } | |
448 } | |
449 } | |
450 | |
451 /* Transition mode */ | |
452 for (j = 0; j < s->prim_channels; j++) { | |
453 for (k = 0; k < s->subband_activity[j]; k++) { | |
454 s->transition_mode[j][k] = 0; | |
455 if (s->subsubframes > 1 && | |
456 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) { | |
457 s->transition_mode[j][k] = | |
458 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]); | |
459 } | |
460 } | |
461 } | |
462 | |
463 for (j = 0; j < s->prim_channels; j++) { | |
6214 | 464 const uint32_t *scale_table; |
4599 | 465 int scale_sum; |
466 | |
467 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2); | |
468 | |
469 if (s->scalefactor_huffman[j] == 6) | |
6214 | 470 scale_table = scale_factor_quant7; |
4599 | 471 else |
6214 | 472 scale_table = scale_factor_quant6; |
4599 | 473 |
474 /* When huffman coded, only the difference is encoded */ | |
475 scale_sum = 0; | |
476 | |
477 for (k = 0; k < s->subband_activity[j]; k++) { | |
478 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) { | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
479 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); |
4599 | 480 s->scale_factor[j][k][0] = scale_table[scale_sum]; |
481 } | |
482 | |
483 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) { | |
484 /* Get second scale factor */ | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
485 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); |
4599 | 486 s->scale_factor[j][k][1] = scale_table[scale_sum]; |
487 } | |
488 } | |
489 } | |
490 | |
491 /* Joint subband scale factor codebook select */ | |
492 for (j = 0; j < s->prim_channels; j++) { | |
493 /* Transmitted only if joint subband coding enabled */ | |
494 if (s->joint_intensity[j] > 0) | |
495 s->joint_huff[j] = get_bits(&s->gb, 3); | |
496 } | |
497 | |
498 /* Scale factors for joint subband coding */ | |
499 for (j = 0; j < s->prim_channels; j++) { | |
500 int source_channel; | |
501 | |
502 /* Transmitted only if joint subband coding enabled */ | |
503 if (s->joint_intensity[j] > 0) { | |
504 int scale = 0; | |
505 source_channel = s->joint_intensity[j] - 1; | |
506 | |
507 /* When huffman coded, only the difference is encoded | |
508 * (is this valid as well for joint scales ???) */ | |
509 | |
510 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) { | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
511 scale = get_scale(&s->gb, s->joint_huff[j], 0); |
4599 | 512 scale += 64; /* bias */ |
513 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */ | |
514 } | |
515 | |
516 if (!s->debug_flag & 0x02) { | |
517 av_log(s->avctx, AV_LOG_DEBUG, | |
518 "Joint stereo coding not supported\n"); | |
519 s->debug_flag |= 0x02; | |
520 } | |
521 } | |
522 } | |
523 | |
524 /* Stereo downmix coefficients */ | |
4894 | 525 if (s->prim_channels > 2) { |
526 if(s->downmix) { | |
4895 | 527 for (j = 0; j < s->prim_channels; j++) { |
528 s->downmix_coef[j][0] = get_bits(&s->gb, 7); | |
529 s->downmix_coef[j][1] = get_bits(&s->gb, 7); | |
530 } | |
4894 | 531 } else { |
532 int am = s->amode & DCA_CHANNEL_MASK; | |
533 for (j = 0; j < s->prim_channels; j++) { | |
534 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0]; | |
535 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1]; | |
536 } | |
537 } | |
4599 | 538 } |
539 | |
540 /* Dynamic range coefficient */ | |
541 if (s->dynrange) | |
542 s->dynrange_coef = get_bits(&s->gb, 8); | |
543 | |
544 /* Side information CRC check word */ | |
545 if (s->crc_present) { | |
546 get_bits(&s->gb, 16); | |
547 } | |
548 | |
549 /* | |
550 * Primary audio data arrays | |
551 */ | |
552 | |
553 /* VQ encoded high frequency subbands */ | |
554 for (j = 0; j < s->prim_channels; j++) | |
555 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) | |
556 /* 1 vector -> 32 samples */ | |
557 s->high_freq_vq[j][k] = get_bits(&s->gb, 10); | |
558 | |
559 /* Low frequency effect data */ | |
560 if (s->lfe) { | |
561 /* LFE samples */ | |
562 int lfe_samples = 2 * s->lfe * s->subsubframes; | |
563 float lfe_scale; | |
564 | |
565 for (j = lfe_samples; j < lfe_samples * 2; j++) { | |
566 /* Signed 8 bits int */ | |
567 s->lfe_data[j] = get_sbits(&s->gb, 8); | |
568 } | |
569 | |
570 /* Scale factor index */ | |
571 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)]; | |
572 | |
573 /* Quantization step size * scale factor */ | |
574 lfe_scale = 0.035 * s->lfe_scale_factor; | |
575 | |
576 for (j = lfe_samples; j < lfe_samples * 2; j++) | |
577 s->lfe_data[j] *= lfe_scale; | |
578 } | |
579 | |
580 #ifdef TRACE | |
581 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes); | |
582 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n", | |
583 s->partial_samples); | |
584 for (j = 0; j < s->prim_channels; j++) { | |
585 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:"); | |
586 for (k = 0; k < s->subband_activity[j]; k++) | |
587 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]); | |
588 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
589 } | |
590 for (j = 0; j < s->prim_channels; j++) { | |
591 for (k = 0; k < s->subband_activity[j]; k++) | |
592 av_log(s->avctx, AV_LOG_DEBUG, | |
593 "prediction coefs: %f, %f, %f, %f\n", | |
594 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192, | |
595 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192, | |
596 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192, | |
597 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192); | |
598 } | |
599 for (j = 0; j < s->prim_channels; j++) { | |
600 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: "); | |
601 for (k = 0; k < s->vq_start_subband[j]; k++) | |
602 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]); | |
603 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
604 } | |
605 for (j = 0; j < s->prim_channels; j++) { | |
606 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:"); | |
607 for (k = 0; k < s->subband_activity[j]; k++) | |
608 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]); | |
609 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
610 } | |
611 for (j = 0; j < s->prim_channels; j++) { | |
612 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:"); | |
613 for (k = 0; k < s->subband_activity[j]; k++) { | |
614 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) | |
615 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]); | |
616 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) | |
617 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]); | |
618 } | |
619 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
620 } | |
621 for (j = 0; j < s->prim_channels; j++) { | |
622 if (s->joint_intensity[j] > 0) { | |
5069 | 623 int source_channel = s->joint_intensity[j] - 1; |
4599 | 624 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n"); |
625 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) | |
626 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]); | |
627 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
628 } | |
629 } | |
630 if (s->prim_channels > 2 && s->downmix) { | |
631 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n"); | |
632 for (j = 0; j < s->prim_channels; j++) { | |
633 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]); | |
634 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]); | |
635 } | |
636 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
637 } | |
638 for (j = 0; j < s->prim_channels; j++) | |
639 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) | |
640 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]); | |
641 if(s->lfe){ | |
5069 | 642 int lfe_samples = 2 * s->lfe * s->subsubframes; |
4599 | 643 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n"); |
644 for (j = lfe_samples; j < lfe_samples * 2; j++) | |
645 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]); | |
646 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
647 } | |
648 #endif | |
649 | |
650 return 0; | |
651 } | |
652 | |
653 static void qmf_32_subbands(DCAContext * s, int chans, | |
654 float samples_in[32][8], float *samples_out, | |
655 float scale, float bias) | |
656 { | |
5974
ae05d6d12f12
Use the correct "const float *" type for variable instead of casting const away.
reimar
parents:
5645
diff
changeset
|
657 const float *prCoeff; |
4599 | 658 int i, j, k; |
659 float praXin[33], *raXin = &praXin[1]; | |
660 | |
661 float *subband_fir_hist = s->subband_fir_hist[chans]; | |
662 float *subband_fir_hist2 = s->subband_fir_noidea[chans]; | |
663 | |
664 int chindex = 0, subindex; | |
665 | |
666 praXin[0] = 0.0; | |
667 | |
668 /* Select filter */ | |
669 if (!s->multirate_inter) /* Non-perfect reconstruction */ | |
5974
ae05d6d12f12
Use the correct "const float *" type for variable instead of casting const away.
reimar
parents:
5645
diff
changeset
|
670 prCoeff = fir_32bands_nonperfect; |
4599 | 671 else /* Perfect reconstruction */ |
5974
ae05d6d12f12
Use the correct "const float *" type for variable instead of casting const away.
reimar
parents:
5645
diff
changeset
|
672 prCoeff = fir_32bands_perfect; |
4599 | 673 |
674 /* Reconstructed channel sample index */ | |
675 for (subindex = 0; subindex < 8; subindex++) { | |
676 float t1, t2, sum[16], diff[16]; | |
677 | |
678 /* Load in one sample from each subband and clear inactive subbands */ | |
679 for (i = 0; i < s->subband_activity[chans]; i++) | |
680 raXin[i] = samples_in[i][subindex]; | |
681 for (; i < 32; i++) | |
682 raXin[i] = 0.0; | |
683 | |
684 /* Multiply by cosine modulation coefficients and | |
685 * create temporary arrays SUM and DIFF */ | |
686 for (j = 0, k = 0; k < 16; k++) { | |
687 t1 = 0.0; | |
688 t2 = 0.0; | |
689 for (i = 0; i < 16; i++, j++){ | |
690 t1 += (raXin[2 * i] + raXin[2 * i + 1]) * cos_mod[j]; | |
691 t2 += (raXin[2 * i] + raXin[2 * i - 1]) * cos_mod[j + 256]; | |
692 } | |
693 sum[k] = t1 + t2; | |
694 diff[k] = t1 - t2; | |
695 } | |
696 | |
697 j = 512; | |
698 /* Store history */ | |
699 for (k = 0; k < 16; k++) | |
700 subband_fir_hist[k] = cos_mod[j++] * sum[k]; | |
701 for (k = 0; k < 16; k++) | |
702 subband_fir_hist[32-k-1] = cos_mod[j++] * diff[k]; | |
703 | |
704 /* Multiply by filter coefficients */ | |
705 for (k = 31, i = 0; i < 32; i++, k--) | |
706 for (j = 0; j < 512; j += 64){ | |
707 subband_fir_hist2[i] += prCoeff[i+j] * ( subband_fir_hist[i+j] - subband_fir_hist[j+k]); | |
708 subband_fir_hist2[i+32] += prCoeff[i+j+32]*(-subband_fir_hist[i+j] - subband_fir_hist[j+k]); | |
709 } | |
710 | |
711 /* Create 32 PCM output samples */ | |
712 for (i = 0; i < 32; i++) | |
713 samples_out[chindex++] = subband_fir_hist2[i] * scale + bias; | |
714 | |
715 /* Update working arrays */ | |
716 memmove(&subband_fir_hist[32], &subband_fir_hist[0], (512 - 32) * sizeof(float)); | |
717 memmove(&subband_fir_hist2[0], &subband_fir_hist2[32], 32 * sizeof(float)); | |
718 memset(&subband_fir_hist2[32], 0, 32 * sizeof(float)); | |
719 } | |
720 } | |
721 | |
722 static void lfe_interpolation_fir(int decimation_select, | |
723 int num_deci_sample, float *samples_in, | |
724 float *samples_out, float scale, | |
725 float bias) | |
726 { | |
727 /* samples_in: An array holding decimated samples. | |
728 * Samples in current subframe starts from samples_in[0], | |
729 * while samples_in[-1], samples_in[-2], ..., stores samples | |
730 * from last subframe as history. | |
731 * | |
732 * samples_out: An array holding interpolated samples | |
733 */ | |
734 | |
735 int decifactor, k, j; | |
736 const float *prCoeff; | |
737 | |
738 int interp_index = 0; /* Index to the interpolated samples */ | |
739 int deciindex; | |
740 | |
741 /* Select decimation filter */ | |
742 if (decimation_select == 1) { | |
743 decifactor = 128; | |
744 prCoeff = lfe_fir_128; | |
745 } else { | |
746 decifactor = 64; | |
747 prCoeff = lfe_fir_64; | |
748 } | |
749 /* Interpolation */ | |
750 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) { | |
751 /* One decimated sample generates decifactor interpolated ones */ | |
752 for (k = 0; k < decifactor; k++) { | |
753 float rTmp = 0.0; | |
754 //FIXME the coeffs are symetric, fix that | |
755 for (j = 0; j < 512 / decifactor; j++) | |
756 rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor]; | |
757 samples_out[interp_index++] = rTmp / scale + bias; | |
758 } | |
759 } | |
760 } | |
761 | |
762 /* downmixing routines */ | |
4894 | 763 #define MIX_REAR1(samples, si1, rs, coef) \ |
764 samples[i] += samples[si1] * coef[rs][0]; \ | |
765 samples[i+256] += samples[si1] * coef[rs][1]; | |
4599 | 766 |
4894 | 767 #define MIX_REAR2(samples, si1, si2, rs, coef) \ |
768 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \ | |
769 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1]; | |
4599 | 770 |
4894 | 771 #define MIX_FRONT3(samples, coef) \ |
4599 | 772 t = samples[i]; \ |
4894 | 773 samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \ |
774 samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1]; | |
4599 | 775 |
776 #define DOWNMIX_TO_STEREO(op1, op2) \ | |
777 for(i = 0; i < 256; i++){ \ | |
778 op1 \ | |
779 op2 \ | |
780 } | |
781 | |
4894 | 782 static void dca_downmix(float *samples, int srcfmt, |
783 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]) | |
4599 | 784 { |
785 int i; | |
786 float t; | |
4894 | 787 float coef[DCA_PRIM_CHANNELS_MAX][2]; |
788 | |
789 for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) { | |
790 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]]; | |
791 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]]; | |
792 } | |
4599 | 793 |
794 switch (srcfmt) { | |
795 case DCA_MONO: | |
796 case DCA_CHANNEL: | |
797 case DCA_STEREO_TOTAL: | |
798 case DCA_STEREO_SUMDIFF: | |
799 case DCA_4F2R: | |
800 av_log(NULL, 0, "Not implemented!\n"); | |
801 break; | |
802 case DCA_STEREO: | |
803 break; | |
804 case DCA_3F: | |
4894 | 805 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),); |
4599 | 806 break; |
807 case DCA_2F1R: | |
4894 | 808 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),); |
4599 | 809 break; |
810 case DCA_3F1R: | |
4894 | 811 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), |
812 MIX_REAR1(samples, i + 768, 3, coef)); | |
4599 | 813 break; |
814 case DCA_2F2R: | |
4894 | 815 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),); |
4599 | 816 break; |
817 case DCA_3F2R: | |
4894 | 818 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), |
819 MIX_REAR2(samples, i + 768, i + 1024, 3, coef)); | |
4599 | 820 break; |
821 } | |
822 } | |
823 | |
824 | |
825 /* Very compact version of the block code decoder that does not use table | |
826 * look-up but is slightly slower */ | |
827 static int decode_blockcode(int code, int levels, int *values) | |
828 { | |
829 int i; | |
830 int offset = (levels - 1) >> 1; | |
831 | |
832 for (i = 0; i < 4; i++) { | |
833 values[i] = (code % levels) - offset; | |
834 code /= levels; | |
835 } | |
836 | |
837 if (code == 0) | |
838 return 0; | |
839 else { | |
840 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n"); | |
841 return -1; | |
842 } | |
843 } | |
844 | |
845 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 }; | |
846 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 }; | |
847 | |
848 static int dca_subsubframe(DCAContext * s) | |
849 { | |
850 int k, l; | |
851 int subsubframe = s->current_subsubframe; | |
852 | |
6214 | 853 const float *quant_step_table; |
4599 | 854 |
855 /* FIXME */ | |
856 float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]; | |
857 | |
858 /* | |
859 * Audio data | |
860 */ | |
861 | |
862 /* Select quantization step size table */ | |
863 if (s->bit_rate == 0x1f) | |
6214 | 864 quant_step_table = lossless_quant_d; |
4599 | 865 else |
6214 | 866 quant_step_table = lossy_quant_d; |
4599 | 867 |
868 for (k = 0; k < s->prim_channels; k++) { | |
869 for (l = 0; l < s->vq_start_subband[k]; l++) { | |
870 int m; | |
871 | |
872 /* Select the mid-tread linear quantizer */ | |
873 int abits = s->bitalloc[k][l]; | |
874 | |
875 float quant_step_size = quant_step_table[abits]; | |
876 float rscale; | |
877 | |
878 /* | |
879 * Determine quantization index code book and its type | |
880 */ | |
881 | |
882 /* Select quantization index code book */ | |
883 int sel = s->quant_index_huffman[k][abits]; | |
884 | |
885 /* | |
886 * Extract bits from the bit stream | |
887 */ | |
888 if(!abits){ | |
889 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0])); | |
890 }else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){ | |
891 if(abits <= 7){ | |
892 /* Block code */ | |
893 int block_code1, block_code2, size, levels; | |
894 int block[8]; | |
895 | |
896 size = abits_sizes[abits-1]; | |
897 levels = abits_levels[abits-1]; | |
898 | |
899 block_code1 = get_bits(&s->gb, size); | |
900 /* FIXME Should test return value */ | |
901 decode_blockcode(block_code1, levels, block); | |
902 block_code2 = get_bits(&s->gb, size); | |
903 decode_blockcode(block_code2, levels, &block[4]); | |
904 for (m = 0; m < 8; m++) | |
905 subband_samples[k][l][m] = block[m]; | |
906 }else{ | |
907 /* no coding */ | |
908 for (m = 0; m < 8; m++) | |
909 subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3); | |
910 } | |
911 }else{ | |
912 /* Huffman coded */ | |
913 for (m = 0; m < 8; m++) | |
914 subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel); | |
915 } | |
916 | |
917 /* Deal with transients */ | |
918 if (s->transition_mode[k][l] && | |
919 subsubframe >= s->transition_mode[k][l]) | |
920 rscale = quant_step_size * s->scale_factor[k][l][1]; | |
921 else | |
922 rscale = quant_step_size * s->scale_factor[k][l][0]; | |
923 | |
924 rscale *= s->scalefactor_adj[k][sel]; | |
925 | |
926 for (m = 0; m < 8; m++) | |
927 subband_samples[k][l][m] *= rscale; | |
928 | |
929 /* | |
930 * Inverse ADPCM if in prediction mode | |
931 */ | |
932 if (s->prediction_mode[k][l]) { | |
933 int n; | |
934 for (m = 0; m < 8; m++) { | |
935 for (n = 1; n <= 4; n++) | |
936 if (m >= n) | |
937 subband_samples[k][l][m] += | |
938 (adpcm_vb[s->prediction_vq[k][l]][n - 1] * | |
939 subband_samples[k][l][m - n] / 8192); | |
940 else if (s->predictor_history) | |
941 subband_samples[k][l][m] += | |
942 (adpcm_vb[s->prediction_vq[k][l]][n - 1] * | |
943 s->subband_samples_hist[k][l][m - n + | |
944 4] / 8192); | |
945 } | |
946 } | |
947 } | |
948 | |
949 /* | |
950 * Decode VQ encoded high frequencies | |
951 */ | |
952 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) { | |
953 /* 1 vector -> 32 samples but we only need the 8 samples | |
954 * for this subsubframe. */ | |
955 int m; | |
956 | |
957 if (!s->debug_flag & 0x01) { | |
958 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n"); | |
959 s->debug_flag |= 0x01; | |
960 } | |
961 | |
962 for (m = 0; m < 8; m++) { | |
963 subband_samples[k][l][m] = | |
964 high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 + | |
965 m] | |
966 * (float) s->scale_factor[k][l][0] / 16.0; | |
967 } | |
968 } | |
969 } | |
970 | |
971 /* Check for DSYNC after subsubframe */ | |
972 if (s->aspf || subsubframe == s->subsubframes - 1) { | |
973 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */ | |
974 #ifdef TRACE | |
975 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n"); | |
976 #endif | |
977 } else { | |
978 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n"); | |
979 } | |
980 } | |
981 | |
982 /* Backup predictor history for adpcm */ | |
983 for (k = 0; k < s->prim_channels; k++) | |
984 for (l = 0; l < s->vq_start_subband[k]; l++) | |
985 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4], | |
986 4 * sizeof(subband_samples[0][0][0])); | |
987 | |
988 /* 32 subbands QMF */ | |
989 for (k = 0; k < s->prim_channels; k++) { | |
990 /* static float pcm_to_double[8] = | |
991 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/ | |
992 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * k], | |
993 2.0 / 3 /*pcm_to_double[s->source_pcm_res] */ , | |
994 0 /*s->bias */ ); | |
995 } | |
996 | |
997 /* Down mixing */ | |
998 | |
999 if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) { | |
4894 | 1000 dca_downmix(s->samples, s->amode, s->downmix_coef); |
4599 | 1001 } |
1002 | |
1003 /* Generate LFE samples for this subsubframe FIXME!!! */ | |
1004 if (s->output & DCA_LFE) { | |
1005 int lfe_samples = 2 * s->lfe * s->subsubframes; | |
1006 int i_channels = dca_channels[s->output & DCA_CHANNEL_MASK]; | |
1007 | |
1008 lfe_interpolation_fir(s->lfe, 2 * s->lfe, | |
1009 s->lfe_data + lfe_samples + | |
1010 2 * s->lfe * subsubframe, | |
1011 &s->samples[256 * i_channels], | |
6107
99e470e62bad
Enable proper output from the LFE channel, based on patch from Mathias Rauen dear dot madshi at net
banan
parents:
6022
diff
changeset
|
1012 256.0, 0 /* s->bias */); |
4599 | 1013 /* Outputs 20bits pcm samples */ |
1014 } | |
1015 | |
1016 return 0; | |
1017 } | |
1018 | |
1019 | |
1020 static int dca_subframe_footer(DCAContext * s) | |
1021 { | |
1022 int aux_data_count = 0, i; | |
1023 int lfe_samples; | |
1024 | |
1025 /* | |
1026 * Unpack optional information | |
1027 */ | |
1028 | |
1029 if (s->timestamp) | |
1030 get_bits(&s->gb, 32); | |
1031 | |
1032 if (s->aux_data) | |
1033 aux_data_count = get_bits(&s->gb, 6); | |
1034 | |
1035 for (i = 0; i < aux_data_count; i++) | |
1036 get_bits(&s->gb, 8); | |
1037 | |
1038 if (s->crc_present && (s->downmix || s->dynrange)) | |
1039 get_bits(&s->gb, 16); | |
1040 | |
1041 lfe_samples = 2 * s->lfe * s->subsubframes; | |
1042 for (i = 0; i < lfe_samples; i++) { | |
1043 s->lfe_data[i] = s->lfe_data[i + lfe_samples]; | |
1044 } | |
1045 | |
1046 return 0; | |
1047 } | |
1048 | |
1049 /** | |
1050 * Decode a dca frame block | |
1051 * | |
1052 * @param s pointer to the DCAContext | |
1053 */ | |
1054 | |
1055 static int dca_decode_block(DCAContext * s) | |
1056 { | |
1057 | |
1058 /* Sanity check */ | |
1059 if (s->current_subframe >= s->subframes) { | |
1060 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i", | |
1061 s->current_subframe, s->subframes); | |
1062 return -1; | |
1063 } | |
1064 | |
1065 if (!s->current_subsubframe) { | |
1066 #ifdef TRACE | |
1067 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n"); | |
1068 #endif | |
1069 /* Read subframe header */ | |
1070 if (dca_subframe_header(s)) | |
1071 return -1; | |
1072 } | |
1073 | |
1074 /* Read subsubframe */ | |
1075 #ifdef TRACE | |
1076 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n"); | |
1077 #endif | |
1078 if (dca_subsubframe(s)) | |
1079 return -1; | |
1080 | |
1081 /* Update state */ | |
1082 s->current_subsubframe++; | |
1083 if (s->current_subsubframe >= s->subsubframes) { | |
1084 s->current_subsubframe = 0; | |
1085 s->current_subframe++; | |
1086 } | |
1087 if (s->current_subframe >= s->subframes) { | |
1088 #ifdef TRACE | |
1089 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n"); | |
1090 #endif | |
1091 /* Read subframe footer */ | |
1092 if (dca_subframe_footer(s)) | |
1093 return -1; | |
1094 } | |
1095 | |
1096 return 0; | |
1097 } | |
1098 | |
1099 /** | |
1100 * Convert bitstream to one representation based on sync marker | |
1101 */ | |
6214 | 1102 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst, |
4599 | 1103 int max_size) |
1104 { | |
1105 uint32_t mrk; | |
1106 int i, tmp; | |
6214 | 1107 const uint16_t *ssrc = (const uint16_t *) src; |
1108 uint16_t *sdst = (uint16_t *) dst; | |
4599 | 1109 PutBitContext pb; |
1110 | |
5027 | 1111 if((unsigned)src_size > (unsigned)max_size) { |
1112 av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n"); | |
4883 | 1113 return -1; |
5027 | 1114 } |
4883 | 1115 |
4599 | 1116 mrk = AV_RB32(src); |
1117 switch (mrk) { | |
1118 case DCA_MARKER_RAW_BE: | |
1119 memcpy(dst, src, FFMIN(src_size, max_size)); | |
1120 return FFMIN(src_size, max_size); | |
1121 case DCA_MARKER_RAW_LE: | |
1122 for (i = 0; i < (FFMIN(src_size, max_size) + 1) >> 1; i++) | |
1123 *sdst++ = bswap_16(*ssrc++); | |
1124 return FFMIN(src_size, max_size); | |
1125 case DCA_MARKER_14B_BE: | |
1126 case DCA_MARKER_14B_LE: | |
1127 init_put_bits(&pb, dst, max_size); | |
1128 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) { | |
1129 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF; | |
1130 put_bits(&pb, 14, tmp); | |
1131 } | |
1132 flush_put_bits(&pb); | |
1133 return (put_bits_count(&pb) + 7) >> 3; | |
1134 default: | |
1135 return -1; | |
1136 } | |
1137 } | |
1138 | |
1139 /** | |
1140 * Main frame decoding function | |
1141 * FIXME add arguments | |
1142 */ | |
1143 static int dca_decode_frame(AVCodecContext * avctx, | |
1144 void *data, int *data_size, | |
6214 | 1145 const uint8_t * buf, int buf_size) |
4599 | 1146 { |
1147 | |
1148 int i, j, k; | |
1149 int16_t *samples = data; | |
1150 DCAContext *s = avctx->priv_data; | |
1151 int channels; | |
1152 | |
1153 | |
1154 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE); | |
1155 if (s->dca_buffer_size == -1) { | |
5027 | 1156 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n"); |
4599 | 1157 return -1; |
1158 } | |
1159 | |
1160 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8); | |
1161 if (dca_parse_frame_header(s) < 0) { | |
1162 //seems like the frame is corrupt, try with the next one | |
5645 | 1163 *data_size=0; |
4599 | 1164 return buf_size; |
1165 } | |
1166 //set AVCodec values with parsed data | |
1167 avctx->sample_rate = s->sample_rate; | |
1168 avctx->bit_rate = s->bit_rate; | |
1169 | |
4893 | 1170 channels = s->prim_channels + !!s->lfe; |
6022
6dd429a5d0be
Make DCA decoder honor avctx->request_channels in a more advisory way.
andoma
parents:
5974
diff
changeset
|
1171 if(avctx->request_channels == 2 && s->prim_channels > 2) { |
6dd429a5d0be
Make DCA decoder honor avctx->request_channels in a more advisory way.
andoma
parents:
5974
diff
changeset
|
1172 channels = 2; |
4893 | 1173 s->output = DCA_STEREO; |
1174 } | |
1175 | |
6577
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1176 /* There is nothing that prevents a dts frame to change channel configuration |
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1177 but FFmpeg doesn't support that so only set the channels if it is previously |
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1178 unset. Ideally during the first probe for channels the crc should be checked |
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1179 and only set avctx->channels when the crc is ok. Right now the decoder could |
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1180 set the channels based on a broken first frame.*/ |
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1181 if (!avctx->channels) |
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1182 avctx->channels = channels; |
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1183 |
4599 | 1184 if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels) |
1185 return -1; | |
1186 *data_size = 0; | |
1187 for (i = 0; i < (s->sample_blocks / 8); i++) { | |
1188 dca_decode_block(s); | |
1189 s->dsp.float_to_int16(s->tsamples, s->samples, 256 * channels); | |
1190 /* interleave samples */ | |
1191 for (j = 0; j < 256; j++) { | |
1192 for (k = 0; k < channels; k++) | |
1193 samples[k] = s->tsamples[j + k * 256]; | |
1194 samples += channels; | |
1195 } | |
1196 *data_size += 256 * sizeof(int16_t) * channels; | |
1197 } | |
1198 | |
1199 return buf_size; | |
1200 } | |
1201 | |
1202 | |
1203 | |
1204 /** | |
1205 * Build the cosine modulation tables for the QMF | |
1206 * | |
1207 * @param s pointer to the DCAContext | |
1208 */ | |
1209 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6463
diff
changeset
|
1210 static av_cold void pre_calc_cosmod(DCAContext * s) |
4599 | 1211 { |
1212 int i, j, k; | |
6350 | 1213 static int cosmod_initialized = 0; |
4599 | 1214 |
6350 | 1215 if(cosmod_initialized) return; |
4599 | 1216 for (j = 0, k = 0; k < 16; k++) |
1217 for (i = 0; i < 16; i++) | |
1218 cos_mod[j++] = cos((2 * i + 1) * (2 * k + 1) * M_PI / 64); | |
1219 | |
1220 for (k = 0; k < 16; k++) | |
1221 for (i = 0; i < 16; i++) | |
1222 cos_mod[j++] = cos((i) * (2 * k + 1) * M_PI / 32); | |
1223 | |
1224 for (k = 0; k < 16; k++) | |
1225 cos_mod[j++] = 0.25 / (2 * cos((2 * k + 1) * M_PI / 128)); | |
1226 | |
1227 for (k = 0; k < 16; k++) | |
1228 cos_mod[j++] = -0.25 / (2.0 * sin((2 * k + 1) * M_PI / 128)); | |
1229 | |
6350 | 1230 cosmod_initialized = 1; |
4599 | 1231 } |
1232 | |
1233 | |
1234 /** | |
1235 * DCA initialization | |
1236 * | |
1237 * @param avctx pointer to the AVCodecContext | |
1238 */ | |
1239 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6463
diff
changeset
|
1240 static av_cold int dca_decode_init(AVCodecContext * avctx) |
4599 | 1241 { |
1242 DCAContext *s = avctx->priv_data; | |
1243 | |
1244 s->avctx = avctx; | |
1245 dca_init_vlcs(); | |
1246 pre_calc_cosmod(s); | |
1247 | |
1248 dsputil_init(&s->dsp, avctx); | |
6120 | 1249 |
1250 /* allow downmixing to stereo */ | |
1251 if (avctx->channels > 0 && avctx->request_channels < avctx->channels && | |
1252 avctx->request_channels == 2) { | |
1253 avctx->channels = avctx->request_channels; | |
1254 } | |
1255 | |
4599 | 1256 return 0; |
1257 } | |
1258 | |
1259 | |
1260 AVCodec dca_decoder = { | |
1261 .name = "dca", | |
1262 .type = CODEC_TYPE_AUDIO, | |
1263 .id = CODEC_ID_DTS, | |
1264 .priv_data_size = sizeof(DCAContext), | |
1265 .init = dca_decode_init, | |
1266 .decode = dca_decode_frame, | |
6710 | 1267 .long_name = "DCA (DTS Coherent Acoustics)", |
4599 | 1268 }; |