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