Mercurial > libavcodec.hg
annotate dca.c @ 11611:a207cc043de8 libavcodec
DCA: use FASTDIV in decode_blockcode()
author | mru |
---|---|
date | Mon, 12 Apr 2010 16:05:10 +0000 |
parents | bd4754d81e42 |
children | bb17732c00ef |
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 /** | |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8226
diff
changeset
|
26 * @file libavcodec/dca.c |
4599 | 27 */ |
28 | |
29 #include <math.h> | |
30 #include <stddef.h> | |
31 #include <stdio.h> | |
32 | |
11611 | 33 #include "libavutil/intmath.h" |
11606 | 34 #include "libavutil/intreadwrite.h" |
4599 | 35 #include "avcodec.h" |
36 #include "dsputil.h" | |
11370 | 37 #include "fft.h" |
9428 | 38 #include "get_bits.h" |
9411
4cb7c65fc775
Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents:
9393
diff
changeset
|
39 #include "put_bits.h" |
4599 | 40 #include "dcadata.h" |
41 #include "dcahuff.h" | |
4899 | 42 #include "dca.h" |
10467 | 43 #include "synth_filter.h" |
4599 | 44 |
45 //#define TRACE | |
46 | |
47 #define DCA_PRIM_CHANNELS_MAX (5) | |
48 #define DCA_SUBBANDS (32) | |
49 #define DCA_ABITS_MAX (32) /* Should be 28 */ | |
50 #define DCA_SUBSUBFAMES_MAX (4) | |
51 #define DCA_LFE_MAX (3) | |
52 | |
53 enum DCAMode { | |
54 DCA_MONO = 0, | |
55 DCA_CHANNEL, | |
56 DCA_STEREO, | |
57 DCA_STEREO_SUMDIFF, | |
58 DCA_STEREO_TOTAL, | |
59 DCA_3F, | |
60 DCA_2F1R, | |
61 DCA_3F1R, | |
62 DCA_2F2R, | |
63 DCA_3F2R, | |
64 DCA_4F2R | |
65 }; | |
66 | |
8100 | 67 /* Tables for mapping dts channel configurations to libavcodec multichannel api. |
68 * Some compromises have been made for special configurations. Most configurations | |
69 * are never used so complete accuracy is not needed. | |
70 * | |
71 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead. | |
8126 | 72 * S -> side, when both rear and back are configured move one of them to the side channel |
8100 | 73 * OV -> center back |
8102
04295cbc0e9b
Change multichannel API define prefix from "CHANNEL_" to "CH_".
andoma
parents:
8101
diff
changeset
|
74 * All 2 channel configurations -> CH_LAYOUT_STEREO |
8100 | 75 */ |
76 | |
77 static const int64_t dca_core_channel_layout[] = { | |
8102
04295cbc0e9b
Change multichannel API define prefix from "CHANNEL_" to "CH_".
andoma
parents:
8101
diff
changeset
|
78 CH_FRONT_CENTER, ///< 1, A |
04295cbc0e9b
Change multichannel API define prefix from "CHANNEL_" to "CH_".
andoma
parents:
8101
diff
changeset
|
79 CH_LAYOUT_STEREO, ///< 2, A + B (dual mono) |
04295cbc0e9b
Change multichannel API define prefix from "CHANNEL_" to "CH_".
andoma
parents:
8101
diff
changeset
|
80 CH_LAYOUT_STEREO, ///< 2, L + R (stereo) |
04295cbc0e9b
Change multichannel API define prefix from "CHANNEL_" to "CH_".
andoma
parents:
8101
diff
changeset
|
81 CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference) |
04295cbc0e9b
Change multichannel API define prefix from "CHANNEL_" to "CH_".
andoma
parents:
8101
diff
changeset
|
82 CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total) |
8103 | 83 CH_LAYOUT_STEREO|CH_FRONT_CENTER, ///< 3, C+L+R |
84 CH_LAYOUT_STEREO|CH_BACK_CENTER, ///< 3, L+R+S | |
85 CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 4, C + L + R+ S | |
86 CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR | |
87 CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR | |
88 CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR | |
89 CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 6, C + L + R+ LR + RR + OV | |
90 CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_FRONT_LEFT_OF_CENTER|CH_BACK_CENTER|CH_BACK_LEFT|CH_BACK_RIGHT, ///< 6, CF+ CR+LF+ RF+LR + RR | |
91 CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR | |
8102
04295cbc0e9b
Change multichannel API define prefix from "CHANNEL_" to "CH_".
andoma
parents:
8101
diff
changeset
|
92 CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_BACK_LEFT|CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2+ SR1 + SR2 |
8103 | 93 CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_BACK_CENTER|CH_SIDE_RIGHT, ///< 8, CL + C+ CR + L + R + SL + S+ SR |
8100 | 94 }; |
95 | |
8148
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
96 static const int8_t dca_lfe_index[] = { |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
97 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3 |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
98 }; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
99 |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
100 static const int8_t dca_channel_reorder_lfe[][8] = { |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
101 { 0, -1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
102 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
103 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
104 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
105 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
106 { 2, 0, 1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
107 { 0, 1, 3, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
108 { 2, 0, 1, 4, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
109 { 0, 1, 3, 4, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
110 { 2, 0, 1, 4, 5, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
111 { 3, 4, 0, 1, 5, 6, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
112 { 2, 0, 1, 4, 5, 6, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
113 { 0, 6, 4, 5, 2, 3, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
114 { 4, 2, 5, 0, 1, 6, 7, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
115 { 5, 6, 0, 1, 7, 3, 8, 4}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
116 { 4, 2, 5, 0, 1, 6, 8, 7}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
117 }; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
118 |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
119 static const int8_t dca_channel_reorder_nolfe[][8] = { |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
120 { 0, -1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
121 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
122 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
123 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
124 { 0, 1, -1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
125 { 2, 0, 1, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
126 { 0, 1, 2, -1, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
127 { 2, 0, 1, 3, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
128 { 0, 1, 2, 3, -1, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
129 { 2, 0, 1, 3, 4, -1, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
130 { 2, 3, 0, 1, 4, 5, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
131 { 2, 0, 1, 3, 4, 5, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
132 { 0, 5, 3, 4, 1, 2, -1, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
133 { 3, 2, 4, 0, 1, 5, 6, -1}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
134 { 4, 5, 0, 1, 6, 2, 7, 3}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
135 { 3, 2, 4, 0, 1, 5, 7, 6}, |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
136 }; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
137 |
8100 | 138 |
4599 | 139 #define DCA_DOLBY 101 /* FIXME */ |
140 | |
141 #define DCA_CHANNEL_BITS 6 | |
142 #define DCA_CHANNEL_MASK 0x3F | |
143 | |
144 #define DCA_LFE 0x80 | |
145 | |
146 #define HEADER_SIZE 14 | |
147 | |
7670
dabe2516abe2
Increase buffer size to 16384 patch by Alexander E. Patrakov" patrakov gmail
michael
parents:
7451
diff
changeset
|
148 #define DCA_MAX_FRAME_SIZE 16384 |
4599 | 149 |
150 /** Bit allocation */ | |
151 typedef struct { | |
152 int offset; ///< code values offset | |
153 int maxbits[8]; ///< max bits in VLC | |
154 int wrap; ///< wrap for get_vlc2() | |
155 VLC vlc[8]; ///< actual codes | |
156 } BitAlloc; | |
157 | |
158 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select | |
159 static BitAlloc dca_tmode; ///< transition mode VLCs | |
160 static BitAlloc dca_scalefactor; ///< scalefactor VLCs | |
161 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs | |
162 | |
4908
777f250df232
Fix multiple "¡Æinline/static¡Ç is not at beginning of declaration" warnings.
diego
parents:
4899
diff
changeset
|
163 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx) |
4599 | 164 { |
165 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset; | |
166 } | |
167 | |
168 typedef struct { | |
169 AVCodecContext *avctx; | |
170 /* Frame header */ | |
171 int frame_type; ///< type of the current frame | |
172 int samples_deficit; ///< deficit sample count | |
173 int crc_present; ///< crc is present in the bitstream | |
174 int sample_blocks; ///< number of PCM sample blocks | |
175 int frame_size; ///< primary frame byte size | |
176 int amode; ///< audio channels arrangement | |
177 int sample_rate; ///< audio sampling rate | |
178 int bit_rate; ///< transmission bit rate | |
8077 | 179 int bit_rate_index; ///< transmission bit rate index |
4599 | 180 |
181 int downmix; ///< embedded downmix enabled | |
182 int dynrange; ///< embedded dynamic range flag | |
183 int timestamp; ///< embedded time stamp flag | |
184 int aux_data; ///< auxiliary data flag | |
185 int hdcd; ///< source material is mastered in HDCD | |
186 int ext_descr; ///< extension audio descriptor flag | |
187 int ext_coding; ///< extended coding flag | |
188 int aspf; ///< audio sync word insertion flag | |
189 int lfe; ///< low frequency effects flag | |
190 int predictor_history; ///< predictor history flag | |
191 int header_crc; ///< header crc check bytes | |
192 int multirate_inter; ///< multirate interpolator switch | |
193 int version; ///< encoder software revision | |
194 int copy_history; ///< copy history | |
195 int source_pcm_res; ///< source pcm resolution | |
196 int front_sum; ///< front sum/difference flag | |
197 int surround_sum; ///< surround sum/difference flag | |
198 int dialog_norm; ///< dialog normalisation parameter | |
199 | |
200 /* Primary audio coding header */ | |
201 int subframes; ///< number of subframes | |
6463 | 202 int total_channels; ///< number of channels including extensions |
4599 | 203 int prim_channels; ///< number of primary audio channels |
204 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count | |
205 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband | |
206 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index | |
207 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book | |
208 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book | |
209 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select | |
210 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select | |
211 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment | |
212 | |
213 /* Primary audio coding side information */ | |
214 int subsubframes; ///< number of subsubframes | |
215 int partial_samples; ///< partial subsubframe samples count | |
216 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not) | |
217 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs | |
218 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index | |
219 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients) | |
220 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient) | |
221 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook | |
222 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors | |
223 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients | |
224 int dynrange_coef; ///< dynamic range coefficient | |
225 | |
226 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands | |
227 | |
228 float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX * | |
229 2 /*history */ ]; ///< Low frequency effect data | |
230 int lfe_scale_factor; | |
231 | |
232 /* Subband samples history (for ADPCM) */ | |
233 float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4]; | |
11369 | 234 DECLARE_ALIGNED(16, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512]; |
11591 | 235 DECLARE_ALIGNED(16, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32]; |
7737 | 236 int hist_index[DCA_PRIM_CHANNELS_MAX]; |
11369 | 237 DECLARE_ALIGNED(16, float, raXin)[32]; |
4599 | 238 |
239 int output; ///< type of output | |
8062
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
240 float add_bias; ///< output bias |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
241 float scale_bias; ///< output scale |
4599 | 242 |
11369 | 243 DECLARE_ALIGNED(16, float, samples)[1536]; /* 6 * 256 = 1536, might only need 5 */ |
7726 | 244 const float *samples_chanptr[6]; |
4599 | 245 |
246 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE]; | |
247 int dca_buffer_size; ///< how much data is in the dca_buffer | |
248 | |
8148
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
249 const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe |
4599 | 250 GetBitContext gb; |
251 /* Current position in DCA frame */ | |
252 int current_subframe; | |
253 int current_subsubframe; | |
254 | |
255 int debug_flag; ///< used for suppressing repeated error messages output | |
256 DSPContext dsp; | |
10199 | 257 FFTContext imdct; |
11592 | 258 SynthFilterContext synth; |
4599 | 259 } DCAContext; |
260 | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
261 static const uint16_t dca_vlc_offs[] = { |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
262 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364, |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
263 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508, |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
264 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564, |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
265 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240, |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
266 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264, |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
267 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622, |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
268 }; |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
269 |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6463
diff
changeset
|
270 static av_cold void dca_init_vlcs(void) |
4599 | 271 { |
6350 | 272 static int vlcs_initialized = 0; |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
273 int i, j, c = 14; |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
274 static VLC_TYPE dca_table[23622][2]; |
4599 | 275 |
6350 | 276 if (vlcs_initialized) |
4599 | 277 return; |
278 | |
279 dca_bitalloc_index.offset = 1; | |
5070 | 280 dca_bitalloc_index.wrap = 2; |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
281 for (i = 0; i < 5; i++) { |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
282 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]]; |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
283 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i]; |
4599 | 284 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12, |
285 bitalloc_12_bits[i], 1, 1, | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
286 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
287 } |
4599 | 288 dca_scalefactor.offset = -64; |
289 dca_scalefactor.wrap = 2; | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
290 for (i = 0; i < 5; i++) { |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
291 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]]; |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
292 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5]; |
4599 | 293 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129, |
294 scales_bits[i], 1, 1, | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
295 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
296 } |
4599 | 297 dca_tmode.offset = 0; |
298 dca_tmode.wrap = 1; | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
299 for (i = 0; i < 4; i++) { |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
300 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]]; |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
301 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10]; |
4599 | 302 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4, |
303 tmode_bits[i], 1, 1, | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
304 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
305 } |
4599 | 306 |
307 for(i = 0; i < 10; i++) | |
308 for(j = 0; j < 7; j++){ | |
309 if(!bitalloc_codes[i][j]) break; | |
310 dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i]; | |
311 dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4); | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
312 dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]]; |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
313 dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c]; |
4599 | 314 init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j], |
315 bitalloc_sizes[i], | |
316 bitalloc_bits[i][j], 1, 1, | |
9526
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
317 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC); |
b9216a975c7f
Make VLC tables in DCA decoder use INIT_VLC_USE_NEW_STATIC
kostya
parents:
9428
diff
changeset
|
318 c++; |
4599 | 319 } |
6350 | 320 vlcs_initialized = 1; |
4599 | 321 } |
322 | |
323 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits) | |
324 { | |
325 while(len--) | |
326 *dst++ = get_bits(gb, bits); | |
327 } | |
328 | |
329 static int dca_parse_frame_header(DCAContext * s) | |
330 { | |
331 int i, j; | |
332 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 }; | |
333 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 }; | |
334 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 }; | |
335 | |
336 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8); | |
337 | |
338 /* Sync code */ | |
339 get_bits(&s->gb, 32); | |
340 | |
341 /* Frame header */ | |
342 s->frame_type = get_bits(&s->gb, 1); | |
343 s->samples_deficit = get_bits(&s->gb, 5) + 1; | |
344 s->crc_present = get_bits(&s->gb, 1); | |
345 s->sample_blocks = get_bits(&s->gb, 7) + 1; | |
346 s->frame_size = get_bits(&s->gb, 14) + 1; | |
347 if (s->frame_size < 95) | |
348 return -1; | |
349 s->amode = get_bits(&s->gb, 6); | |
350 s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)]; | |
351 if (!s->sample_rate) | |
352 return -1; | |
8078 | 353 s->bit_rate_index = get_bits(&s->gb, 5); |
8077 | 354 s->bit_rate = dca_bit_rates[s->bit_rate_index]; |
4599 | 355 if (!s->bit_rate) |
356 return -1; | |
357 | |
358 s->downmix = get_bits(&s->gb, 1); | |
359 s->dynrange = get_bits(&s->gb, 1); | |
360 s->timestamp = get_bits(&s->gb, 1); | |
361 s->aux_data = get_bits(&s->gb, 1); | |
362 s->hdcd = get_bits(&s->gb, 1); | |
363 s->ext_descr = get_bits(&s->gb, 3); | |
364 s->ext_coding = get_bits(&s->gb, 1); | |
365 s->aspf = get_bits(&s->gb, 1); | |
366 s->lfe = get_bits(&s->gb, 2); | |
367 s->predictor_history = get_bits(&s->gb, 1); | |
368 | |
369 /* TODO: check CRC */ | |
370 if (s->crc_present) | |
371 s->header_crc = get_bits(&s->gb, 16); | |
372 | |
373 s->multirate_inter = get_bits(&s->gb, 1); | |
374 s->version = get_bits(&s->gb, 4); | |
375 s->copy_history = get_bits(&s->gb, 2); | |
376 s->source_pcm_res = get_bits(&s->gb, 3); | |
377 s->front_sum = get_bits(&s->gb, 1); | |
378 s->surround_sum = get_bits(&s->gb, 1); | |
379 s->dialog_norm = get_bits(&s->gb, 4); | |
380 | |
381 /* FIXME: channels mixing levels */ | |
4893 | 382 s->output = s->amode; |
383 if(s->lfe) s->output |= DCA_LFE; | |
4599 | 384 |
385 #ifdef TRACE | |
386 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type); | |
387 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit); | |
388 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present); | |
389 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n", | |
390 s->sample_blocks, s->sample_blocks * 32); | |
391 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size); | |
392 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n", | |
393 s->amode, dca_channels[s->amode]); | |
8061 | 394 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n", |
395 s->sample_rate); | |
396 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n", | |
397 s->bit_rate); | |
4599 | 398 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix); |
399 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange); | |
400 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp); | |
401 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data); | |
402 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd); | |
403 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr); | |
404 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding); | |
405 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf); | |
406 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe); | |
407 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n", | |
408 s->predictor_history); | |
409 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc); | |
410 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n", | |
411 s->multirate_inter); | |
412 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version); | |
413 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history); | |
414 av_log(s->avctx, AV_LOG_DEBUG, | |
415 "source pcm resolution: %i (%i bits/sample)\n", | |
416 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]); | |
417 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum); | |
418 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum); | |
419 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm); | |
420 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
421 #endif | |
422 | |
423 /* Primary audio coding header */ | |
424 s->subframes = get_bits(&s->gb, 4) + 1; | |
6463 | 425 s->total_channels = get_bits(&s->gb, 3) + 1; |
426 s->prim_channels = s->total_channels; | |
427 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX) | |
428 s->prim_channels = DCA_PRIM_CHANNELS_MAX; /* We only support DTS core */ | |
4599 | 429 |
430 | |
431 for (i = 0; i < s->prim_channels; i++) { | |
432 s->subband_activity[i] = get_bits(&s->gb, 5) + 2; | |
433 if (s->subband_activity[i] > DCA_SUBBANDS) | |
434 s->subband_activity[i] = DCA_SUBBANDS; | |
435 } | |
436 for (i = 0; i < s->prim_channels; i++) { | |
437 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1; | |
438 if (s->vq_start_subband[i] > DCA_SUBBANDS) | |
439 s->vq_start_subband[i] = DCA_SUBBANDS; | |
440 } | |
441 get_array(&s->gb, s->joint_intensity, s->prim_channels, 3); | |
442 get_array(&s->gb, s->transient_huffman, s->prim_channels, 2); | |
443 get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3); | |
444 get_array(&s->gb, s->bitalloc_huffman, s->prim_channels, 3); | |
445 | |
446 /* Get codebooks quantization indexes */ | |
447 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman)); | |
448 for (j = 1; j < 11; j++) | |
449 for (i = 0; i < s->prim_channels; i++) | |
450 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]); | |
451 | |
452 /* Get scale factor adjustment */ | |
453 for (j = 0; j < 11; j++) | |
454 for (i = 0; i < s->prim_channels; i++) | |
455 s->scalefactor_adj[i][j] = 1; | |
456 | |
457 for (j = 1; j < 11; j++) | |
458 for (i = 0; i < s->prim_channels; i++) | |
459 if (s->quant_index_huffman[i][j] < thr[j]) | |
460 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)]; | |
461 | |
462 if (s->crc_present) { | |
463 /* Audio header CRC check */ | |
464 get_bits(&s->gb, 16); | |
465 } | |
466 | |
467 s->current_subframe = 0; | |
468 s->current_subsubframe = 0; | |
469 | |
470 #ifdef TRACE | |
471 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes); | |
472 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels); | |
473 for(i = 0; i < s->prim_channels; i++){ | |
474 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]); | |
475 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]); | |
476 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]); | |
477 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]); | |
478 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]); | |
479 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]); | |
480 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:"); | |
481 for (j = 0; j < 11; j++) | |
482 av_log(s->avctx, AV_LOG_DEBUG, " %i", | |
483 s->quant_index_huffman[i][j]); | |
484 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
485 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:"); | |
486 for (j = 0; j < 11; j++) | |
487 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]); | |
488 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
489 } | |
490 #endif | |
491 | |
492 return 0; | |
493 } | |
494 | |
495 | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
496 static inline int get_scale(GetBitContext *gb, int level, int value) |
4599 | 497 { |
498 if (level < 5) { | |
499 /* huffman encoded */ | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
500 value += get_bitalloc(gb, &dca_scalefactor, level); |
4599 | 501 } else if(level < 8) |
502 value = get_bits(gb, level + 1); | |
503 return value; | |
504 } | |
505 | |
506 static int dca_subframe_header(DCAContext * s) | |
507 { | |
508 /* Primary audio coding side information */ | |
509 int j, k; | |
510 | |
511 s->subsubframes = get_bits(&s->gb, 2) + 1; | |
512 s->partial_samples = get_bits(&s->gb, 3); | |
513 for (j = 0; j < s->prim_channels; j++) { | |
514 for (k = 0; k < s->subband_activity[j]; k++) | |
515 s->prediction_mode[j][k] = get_bits(&s->gb, 1); | |
516 } | |
517 | |
518 /* Get prediction codebook */ | |
519 for (j = 0; j < s->prim_channels; j++) { | |
520 for (k = 0; k < s->subband_activity[j]; k++) { | |
521 if (s->prediction_mode[j][k] > 0) { | |
522 /* (Prediction coefficient VQ address) */ | |
523 s->prediction_vq[j][k] = get_bits(&s->gb, 12); | |
524 } | |
525 } | |
526 } | |
527 | |
528 /* Bit allocation index */ | |
529 for (j = 0; j < s->prim_channels; j++) { | |
530 for (k = 0; k < s->vq_start_subband[j]; k++) { | |
531 if (s->bitalloc_huffman[j] == 6) | |
532 s->bitalloc[j][k] = get_bits(&s->gb, 5); | |
533 else if (s->bitalloc_huffman[j] == 5) | |
534 s->bitalloc[j][k] = get_bits(&s->gb, 4); | |
6463 | 535 else if (s->bitalloc_huffman[j] == 7) { |
536 av_log(s->avctx, AV_LOG_ERROR, | |
537 "Invalid bit allocation index\n"); | |
538 return -1; | |
539 } else { | |
4599 | 540 s->bitalloc[j][k] = |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
541 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]); |
4599 | 542 } |
543 | |
544 if (s->bitalloc[j][k] > 26) { | |
545 // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n", | |
546 // j, k, s->bitalloc[j][k]); | |
547 return -1; | |
548 } | |
549 } | |
550 } | |
551 | |
552 /* Transition mode */ | |
553 for (j = 0; j < s->prim_channels; j++) { | |
554 for (k = 0; k < s->subband_activity[j]; k++) { | |
555 s->transition_mode[j][k] = 0; | |
556 if (s->subsubframes > 1 && | |
557 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) { | |
558 s->transition_mode[j][k] = | |
559 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]); | |
560 } | |
561 } | |
562 } | |
563 | |
564 for (j = 0; j < s->prim_channels; j++) { | |
6214 | 565 const uint32_t *scale_table; |
4599 | 566 int scale_sum; |
567 | |
568 memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2); | |
569 | |
570 if (s->scalefactor_huffman[j] == 6) | |
6214 | 571 scale_table = scale_factor_quant7; |
4599 | 572 else |
6214 | 573 scale_table = scale_factor_quant6; |
4599 | 574 |
575 /* When huffman coded, only the difference is encoded */ | |
576 scale_sum = 0; | |
577 | |
578 for (k = 0; k < s->subband_activity[j]; k++) { | |
579 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
|
580 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); |
4599 | 581 s->scale_factor[j][k][0] = scale_table[scale_sum]; |
582 } | |
583 | |
584 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) { | |
585 /* Get second scale factor */ | |
4876
384c95879d8b
1000l to myself as used VLC indexes were totally wrong
kostya
parents:
4783
diff
changeset
|
586 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum); |
4599 | 587 s->scale_factor[j][k][1] = scale_table[scale_sum]; |
588 } | |
589 } | |
590 } | |
591 | |
592 /* Joint subband scale factor codebook select */ | |
593 for (j = 0; j < s->prim_channels; j++) { | |
594 /* Transmitted only if joint subband coding enabled */ | |
595 if (s->joint_intensity[j] > 0) | |
596 s->joint_huff[j] = get_bits(&s->gb, 3); | |
597 } | |
598 | |
599 /* Scale factors for joint subband coding */ | |
600 for (j = 0; j < s->prim_channels; j++) { | |
601 int source_channel; | |
602 | |
603 /* Transmitted only if joint subband coding enabled */ | |
604 if (s->joint_intensity[j] > 0) { | |
605 int scale = 0; | |
606 source_channel = s->joint_intensity[j] - 1; | |
607 | |
608 /* When huffman coded, only the difference is encoded | |
609 * (is this valid as well for joint scales ???) */ | |
610 | |
611 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
|
612 scale = get_scale(&s->gb, s->joint_huff[j], 0); |
4599 | 613 scale += 64; /* bias */ |
614 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */ | |
615 } | |
616 | |
10380 | 617 if (!(s->debug_flag & 0x02)) { |
4599 | 618 av_log(s->avctx, AV_LOG_DEBUG, |
619 "Joint stereo coding not supported\n"); | |
620 s->debug_flag |= 0x02; | |
621 } | |
622 } | |
623 } | |
624 | |
625 /* Stereo downmix coefficients */ | |
4894 | 626 if (s->prim_channels > 2) { |
627 if(s->downmix) { | |
4895 | 628 for (j = 0; j < s->prim_channels; j++) { |
629 s->downmix_coef[j][0] = get_bits(&s->gb, 7); | |
630 s->downmix_coef[j][1] = get_bits(&s->gb, 7); | |
631 } | |
4894 | 632 } else { |
633 int am = s->amode & DCA_CHANNEL_MASK; | |
634 for (j = 0; j < s->prim_channels; j++) { | |
635 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0]; | |
636 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1]; | |
637 } | |
638 } | |
4599 | 639 } |
640 | |
641 /* Dynamic range coefficient */ | |
642 if (s->dynrange) | |
643 s->dynrange_coef = get_bits(&s->gb, 8); | |
644 | |
645 /* Side information CRC check word */ | |
646 if (s->crc_present) { | |
647 get_bits(&s->gb, 16); | |
648 } | |
649 | |
650 /* | |
651 * Primary audio data arrays | |
652 */ | |
653 | |
654 /* VQ encoded high frequency subbands */ | |
655 for (j = 0; j < s->prim_channels; j++) | |
656 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) | |
657 /* 1 vector -> 32 samples */ | |
658 s->high_freq_vq[j][k] = get_bits(&s->gb, 10); | |
659 | |
660 /* Low frequency effect data */ | |
661 if (s->lfe) { | |
662 /* LFE samples */ | |
663 int lfe_samples = 2 * s->lfe * s->subsubframes; | |
664 float lfe_scale; | |
665 | |
666 for (j = lfe_samples; j < lfe_samples * 2; j++) { | |
667 /* Signed 8 bits int */ | |
668 s->lfe_data[j] = get_sbits(&s->gb, 8); | |
669 } | |
670 | |
671 /* Scale factor index */ | |
672 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)]; | |
673 | |
674 /* Quantization step size * scale factor */ | |
675 lfe_scale = 0.035 * s->lfe_scale_factor; | |
676 | |
677 for (j = lfe_samples; j < lfe_samples * 2; j++) | |
678 s->lfe_data[j] *= lfe_scale; | |
679 } | |
680 | |
681 #ifdef TRACE | |
682 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes); | |
683 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n", | |
684 s->partial_samples); | |
685 for (j = 0; j < s->prim_channels; j++) { | |
686 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:"); | |
687 for (k = 0; k < s->subband_activity[j]; k++) | |
688 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]); | |
689 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
690 } | |
691 for (j = 0; j < s->prim_channels; j++) { | |
692 for (k = 0; k < s->subband_activity[j]; k++) | |
693 av_log(s->avctx, AV_LOG_DEBUG, | |
694 "prediction coefs: %f, %f, %f, %f\n", | |
695 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192, | |
696 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192, | |
697 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192, | |
698 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192); | |
699 } | |
700 for (j = 0; j < s->prim_channels; j++) { | |
701 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: "); | |
702 for (k = 0; k < s->vq_start_subband[j]; k++) | |
703 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]); | |
704 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
705 } | |
706 for (j = 0; j < s->prim_channels; j++) { | |
707 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:"); | |
708 for (k = 0; k < s->subband_activity[j]; k++) | |
709 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]); | |
710 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
711 } | |
712 for (j = 0; j < s->prim_channels; j++) { | |
713 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:"); | |
714 for (k = 0; k < s->subband_activity[j]; k++) { | |
715 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) | |
716 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]); | |
717 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) | |
718 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]); | |
719 } | |
720 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
721 } | |
722 for (j = 0; j < s->prim_channels; j++) { | |
723 if (s->joint_intensity[j] > 0) { | |
5069 | 724 int source_channel = s->joint_intensity[j] - 1; |
4599 | 725 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n"); |
726 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) | |
727 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]); | |
728 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
729 } | |
730 } | |
731 if (s->prim_channels > 2 && s->downmix) { | |
732 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n"); | |
733 for (j = 0; j < s->prim_channels; j++) { | |
734 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]); | |
735 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]); | |
736 } | |
737 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
738 } | |
739 for (j = 0; j < s->prim_channels; j++) | |
740 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++) | |
741 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]); | |
742 if(s->lfe){ | |
5069 | 743 int lfe_samples = 2 * s->lfe * s->subsubframes; |
4599 | 744 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n"); |
745 for (j = lfe_samples; j < lfe_samples * 2; j++) | |
746 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]); | |
747 av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
748 } | |
749 #endif | |
750 | |
751 return 0; | |
752 } | |
753 | |
754 static void qmf_32_subbands(DCAContext * s, int chans, | |
755 float samples_in[32][8], float *samples_out, | |
756 float scale, float bias) | |
757 { | |
5974
ae05d6d12f12
Use the correct "const float *" type for variable instead of casting const away.
reimar
parents:
5645
diff
changeset
|
758 const float *prCoeff; |
10468 | 759 int i; |
4599 | 760 |
11607 | 761 int sb_act = s->subband_activity[chans]; |
7738
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
762 int subindex; |
4599 | 763 |
7738
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
764 scale *= sqrt(1/8.0); |
4599 | 765 |
766 /* Select filter */ | |
767 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
|
768 prCoeff = fir_32bands_nonperfect; |
4599 | 769 else /* Perfect reconstruction */ |
5974
ae05d6d12f12
Use the correct "const float *" type for variable instead of casting const away.
reimar
parents:
5645
diff
changeset
|
770 prCoeff = fir_32bands_perfect; |
4599 | 771 |
772 /* Reconstructed channel sample index */ | |
773 for (subindex = 0; subindex < 8; subindex++) { | |
774 /* Load in one sample from each subband and clear inactive subbands */ | |
11607 | 775 for (i = 0; i < sb_act; i++){ |
11606 | 776 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ ((i-1)&2)<<30; |
777 AV_WN32A(&s->raXin[i], v); | |
7738
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
778 } |
4599 | 779 for (; i < 32; i++) |
10152 | 780 s->raXin[i] = 0.0; |
4599 | 781 |
11592 | 782 s->synth.synth_filter_float(&s->imdct, |
10467 | 783 s->subband_fir_hist[chans], &s->hist_index[chans], |
784 s->subband_fir_noidea[chans], prCoeff, | |
785 samples_out, s->raXin, scale, bias); | |
7738
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
786 samples_out+= 32; |
4599 | 787 |
788 } | |
789 } | |
790 | |
791 static void lfe_interpolation_fir(int decimation_select, | |
792 int num_deci_sample, float *samples_in, | |
793 float *samples_out, float scale, | |
794 float bias) | |
795 { | |
796 /* samples_in: An array holding decimated samples. | |
797 * Samples in current subframe starts from samples_in[0], | |
798 * while samples_in[-1], samples_in[-2], ..., stores samples | |
799 * from last subframe as history. | |
800 * | |
801 * samples_out: An array holding interpolated samples | |
802 */ | |
803 | |
804 int decifactor, k, j; | |
805 const float *prCoeff; | |
806 int deciindex; | |
807 | |
808 /* Select decimation filter */ | |
809 if (decimation_select == 1) { | |
11608 | 810 decifactor = 64; |
4599 | 811 prCoeff = lfe_fir_128; |
812 } else { | |
11608 | 813 decifactor = 32; |
4599 | 814 prCoeff = lfe_fir_64; |
815 } | |
816 /* Interpolation */ | |
817 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) { | |
11608 | 818 float *samples_out2 = samples_out + decifactor; |
819 const float *cf0 = prCoeff; | |
820 const float *cf1 = prCoeff + 256; | |
821 | |
822 /* One decimated sample generates 2*decifactor interpolated ones */ | |
4599 | 823 for (k = 0; k < decifactor; k++) { |
11608 | 824 float v0 = 0.0; |
825 float v1 = 0.0; | |
826 for (j = 0; j < 256 / decifactor; j++) { | |
827 float s = samples_in[-j]; | |
828 v0 += s * *cf0++; | |
829 v1 += s * *--cf1; | |
830 } | |
831 *samples_out++ = (v0 * scale) + bias; | |
832 *samples_out2++ = (v1 * scale) + bias; | |
4599 | 833 } |
11608 | 834 |
835 samples_in++; | |
836 samples_out += decifactor; | |
4599 | 837 } |
838 } | |
839 | |
840 /* downmixing routines */ | |
4894 | 841 #define MIX_REAR1(samples, si1, rs, coef) \ |
842 samples[i] += samples[si1] * coef[rs][0]; \ | |
843 samples[i+256] += samples[si1] * coef[rs][1]; | |
4599 | 844 |
4894 | 845 #define MIX_REAR2(samples, si1, si2, rs, coef) \ |
846 samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \ | |
847 samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1]; | |
4599 | 848 |
4894 | 849 #define MIX_FRONT3(samples, coef) \ |
4599 | 850 t = samples[i]; \ |
4894 | 851 samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \ |
852 samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1]; | |
4599 | 853 |
854 #define DOWNMIX_TO_STEREO(op1, op2) \ | |
855 for(i = 0; i < 256; i++){ \ | |
856 op1 \ | |
857 op2 \ | |
858 } | |
859 | |
4894 | 860 static void dca_downmix(float *samples, int srcfmt, |
861 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]) | |
4599 | 862 { |
863 int i; | |
864 float t; | |
4894 | 865 float coef[DCA_PRIM_CHANNELS_MAX][2]; |
866 | |
867 for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) { | |
868 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]]; | |
869 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]]; | |
870 } | |
4599 | 871 |
872 switch (srcfmt) { | |
873 case DCA_MONO: | |
874 case DCA_CHANNEL: | |
875 case DCA_STEREO_TOTAL: | |
876 case DCA_STEREO_SUMDIFF: | |
877 case DCA_4F2R: | |
878 av_log(NULL, 0, "Not implemented!\n"); | |
879 break; | |
880 case DCA_STEREO: | |
881 break; | |
882 case DCA_3F: | |
4894 | 883 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),); |
4599 | 884 break; |
885 case DCA_2F1R: | |
4894 | 886 DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),); |
4599 | 887 break; |
888 case DCA_3F1R: | |
4894 | 889 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), |
890 MIX_REAR1(samples, i + 768, 3, coef)); | |
4599 | 891 break; |
892 case DCA_2F2R: | |
4894 | 893 DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),); |
4599 | 894 break; |
895 case DCA_3F2R: | |
4894 | 896 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), |
897 MIX_REAR2(samples, i + 768, i + 1024, 3, coef)); | |
4599 | 898 break; |
899 } | |
900 } | |
901 | |
902 | |
903 /* Very compact version of the block code decoder that does not use table | |
904 * look-up but is slightly slower */ | |
905 static int decode_blockcode(int code, int levels, int *values) | |
906 { | |
907 int i; | |
908 int offset = (levels - 1) >> 1; | |
909 | |
910 for (i = 0; i < 4; i++) { | |
11611 | 911 int div = FASTDIV(code, levels); |
912 values[i] = code - offset - div*levels; | |
913 code = div; | |
4599 | 914 } |
915 | |
916 if (code == 0) | |
917 return 0; | |
918 else { | |
919 av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n"); | |
920 return -1; | |
921 } | |
922 } | |
923 | |
924 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 }; | |
925 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 }; | |
926 | |
927 static int dca_subsubframe(DCAContext * s) | |
928 { | |
929 int k, l; | |
930 int subsubframe = s->current_subsubframe; | |
931 | |
6214 | 932 const float *quant_step_table; |
4599 | 933 |
934 /* FIXME */ | |
935 float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]; | |
936 | |
937 /* | |
938 * Audio data | |
939 */ | |
940 | |
941 /* Select quantization step size table */ | |
8077 | 942 if (s->bit_rate_index == 0x1f) |
6214 | 943 quant_step_table = lossless_quant_d; |
4599 | 944 else |
6214 | 945 quant_step_table = lossy_quant_d; |
4599 | 946 |
947 for (k = 0; k < s->prim_channels; k++) { | |
948 for (l = 0; l < s->vq_start_subband[k]; l++) { | |
949 int m; | |
950 | |
951 /* Select the mid-tread linear quantizer */ | |
952 int abits = s->bitalloc[k][l]; | |
953 | |
954 float quant_step_size = quant_step_table[abits]; | |
955 float rscale; | |
956 | |
957 /* | |
958 * Determine quantization index code book and its type | |
959 */ | |
960 | |
961 /* Select quantization index code book */ | |
962 int sel = s->quant_index_huffman[k][abits]; | |
963 | |
964 /* | |
965 * Extract bits from the bit stream | |
966 */ | |
967 if(!abits){ | |
968 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0])); | |
969 }else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){ | |
970 if(abits <= 7){ | |
971 /* Block code */ | |
972 int block_code1, block_code2, size, levels; | |
973 int block[8]; | |
974 | |
975 size = abits_sizes[abits-1]; | |
976 levels = abits_levels[abits-1]; | |
977 | |
978 block_code1 = get_bits(&s->gb, size); | |
979 /* FIXME Should test return value */ | |
980 decode_blockcode(block_code1, levels, block); | |
981 block_code2 = get_bits(&s->gb, size); | |
982 decode_blockcode(block_code2, levels, &block[4]); | |
983 for (m = 0; m < 8; m++) | |
984 subband_samples[k][l][m] = block[m]; | |
985 }else{ | |
986 /* no coding */ | |
987 for (m = 0; m < 8; m++) | |
988 subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3); | |
989 } | |
990 }else{ | |
991 /* Huffman coded */ | |
992 for (m = 0; m < 8; m++) | |
993 subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel); | |
994 } | |
995 | |
996 /* Deal with transients */ | |
997 if (s->transition_mode[k][l] && | |
998 subsubframe >= s->transition_mode[k][l]) | |
999 rscale = quant_step_size * s->scale_factor[k][l][1]; | |
1000 else | |
1001 rscale = quant_step_size * s->scale_factor[k][l][0]; | |
1002 | |
1003 rscale *= s->scalefactor_adj[k][sel]; | |
1004 | |
1005 for (m = 0; m < 8; m++) | |
1006 subband_samples[k][l][m] *= rscale; | |
1007 | |
1008 /* | |
1009 * Inverse ADPCM if in prediction mode | |
1010 */ | |
1011 if (s->prediction_mode[k][l]) { | |
1012 int n; | |
1013 for (m = 0; m < 8; m++) { | |
1014 for (n = 1; n <= 4; n++) | |
1015 if (m >= n) | |
1016 subband_samples[k][l][m] += | |
1017 (adpcm_vb[s->prediction_vq[k][l]][n - 1] * | |
1018 subband_samples[k][l][m - n] / 8192); | |
1019 else if (s->predictor_history) | |
1020 subband_samples[k][l][m] += | |
1021 (adpcm_vb[s->prediction_vq[k][l]][n - 1] * | |
1022 s->subband_samples_hist[k][l][m - n + | |
1023 4] / 8192); | |
1024 } | |
1025 } | |
1026 } | |
1027 | |
1028 /* | |
1029 * Decode VQ encoded high frequencies | |
1030 */ | |
1031 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) { | |
1032 /* 1 vector -> 32 samples but we only need the 8 samples | |
1033 * for this subsubframe. */ | |
1034 int m; | |
1035 | |
1036 if (!s->debug_flag & 0x01) { | |
1037 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n"); | |
1038 s->debug_flag |= 0x01; | |
1039 } | |
1040 | |
1041 for (m = 0; m < 8; m++) { | |
1042 subband_samples[k][l][m] = | |
1043 high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 + | |
1044 m] | |
1045 * (float) s->scale_factor[k][l][0] / 16.0; | |
1046 } | |
1047 } | |
1048 } | |
1049 | |
1050 /* Check for DSYNC after subsubframe */ | |
1051 if (s->aspf || subsubframe == s->subsubframes - 1) { | |
1052 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */ | |
1053 #ifdef TRACE | |
1054 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n"); | |
1055 #endif | |
1056 } else { | |
1057 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n"); | |
1058 } | |
1059 } | |
1060 | |
1061 /* Backup predictor history for adpcm */ | |
1062 for (k = 0; k < s->prim_channels; k++) | |
1063 for (l = 0; l < s->vq_start_subband[k]; l++) | |
1064 memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4], | |
1065 4 * sizeof(subband_samples[0][0][0])); | |
1066 | |
1067 /* 32 subbands QMF */ | |
1068 for (k = 0; k < s->prim_channels; k++) { | |
1069 /* static float pcm_to_double[8] = | |
1070 {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/ | |
8148
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1071 qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]], |
8062
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1072 M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ , |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1073 s->add_bias ); |
4599 | 1074 } |
1075 | |
1076 /* Down mixing */ | |
1077 | |
1078 if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) { | |
4894 | 1079 dca_downmix(s->samples, s->amode, s->downmix_coef); |
4599 | 1080 } |
1081 | |
1082 /* Generate LFE samples for this subsubframe FIXME!!! */ | |
1083 if (s->output & DCA_LFE) { | |
1084 int lfe_samples = 2 * s->lfe * s->subsubframes; | |
1085 | |
1086 lfe_interpolation_fir(s->lfe, 2 * s->lfe, | |
1087 s->lfe_data + lfe_samples + | |
1088 2 * s->lfe * subsubframe, | |
8148
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1089 &s->samples[256 * dca_lfe_index[s->amode]], |
8062
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1090 (1.0/256.0)*s->scale_bias, s->add_bias); |
4599 | 1091 /* Outputs 20bits pcm samples */ |
1092 } | |
1093 | |
1094 return 0; | |
1095 } | |
1096 | |
1097 | |
1098 static int dca_subframe_footer(DCAContext * s) | |
1099 { | |
1100 int aux_data_count = 0, i; | |
1101 int lfe_samples; | |
1102 | |
1103 /* | |
1104 * Unpack optional information | |
1105 */ | |
1106 | |
1107 if (s->timestamp) | |
1108 get_bits(&s->gb, 32); | |
1109 | |
1110 if (s->aux_data) | |
1111 aux_data_count = get_bits(&s->gb, 6); | |
1112 | |
1113 for (i = 0; i < aux_data_count; i++) | |
1114 get_bits(&s->gb, 8); | |
1115 | |
1116 if (s->crc_present && (s->downmix || s->dynrange)) | |
1117 get_bits(&s->gb, 16); | |
1118 | |
1119 lfe_samples = 2 * s->lfe * s->subsubframes; | |
1120 for (i = 0; i < lfe_samples; i++) { | |
1121 s->lfe_data[i] = s->lfe_data[i + lfe_samples]; | |
1122 } | |
1123 | |
1124 return 0; | |
1125 } | |
1126 | |
1127 /** | |
1128 * Decode a dca frame block | |
1129 * | |
1130 * @param s pointer to the DCAContext | |
1131 */ | |
1132 | |
1133 static int dca_decode_block(DCAContext * s) | |
1134 { | |
1135 | |
1136 /* Sanity check */ | |
1137 if (s->current_subframe >= s->subframes) { | |
1138 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i", | |
1139 s->current_subframe, s->subframes); | |
1140 return -1; | |
1141 } | |
1142 | |
1143 if (!s->current_subsubframe) { | |
1144 #ifdef TRACE | |
1145 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n"); | |
1146 #endif | |
1147 /* Read subframe header */ | |
1148 if (dca_subframe_header(s)) | |
1149 return -1; | |
1150 } | |
1151 | |
1152 /* Read subsubframe */ | |
1153 #ifdef TRACE | |
1154 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n"); | |
1155 #endif | |
1156 if (dca_subsubframe(s)) | |
1157 return -1; | |
1158 | |
1159 /* Update state */ | |
1160 s->current_subsubframe++; | |
1161 if (s->current_subsubframe >= s->subsubframes) { | |
1162 s->current_subsubframe = 0; | |
1163 s->current_subframe++; | |
1164 } | |
1165 if (s->current_subframe >= s->subframes) { | |
1166 #ifdef TRACE | |
1167 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n"); | |
1168 #endif | |
1169 /* Read subframe footer */ | |
1170 if (dca_subframe_footer(s)) | |
1171 return -1; | |
1172 } | |
1173 | |
1174 return 0; | |
1175 } | |
1176 | |
1177 /** | |
1178 * Convert bitstream to one representation based on sync marker | |
1179 */ | |
6214 | 1180 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst, |
4599 | 1181 int max_size) |
1182 { | |
1183 uint32_t mrk; | |
1184 int i, tmp; | |
6214 | 1185 const uint16_t *ssrc = (const uint16_t *) src; |
1186 uint16_t *sdst = (uint16_t *) dst; | |
4599 | 1187 PutBitContext pb; |
1188 | |
5027 | 1189 if((unsigned)src_size > (unsigned)max_size) { |
8226
ee1b8c54a603
Add support for parsing and decoding DCA-HD streams.
kostya
parents:
8148
diff
changeset
|
1190 // av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n"); |
ee1b8c54a603
Add support for parsing and decoding DCA-HD streams.
kostya
parents:
8148
diff
changeset
|
1191 // return -1; |
ee1b8c54a603
Add support for parsing and decoding DCA-HD streams.
kostya
parents:
8148
diff
changeset
|
1192 src_size = max_size; |
5027 | 1193 } |
4883 | 1194 |
4599 | 1195 mrk = AV_RB32(src); |
1196 switch (mrk) { | |
1197 case DCA_MARKER_RAW_BE: | |
7671 | 1198 memcpy(dst, src, src_size); |
1199 return src_size; | |
4599 | 1200 case DCA_MARKER_RAW_LE: |
7671 | 1201 for (i = 0; i < (src_size + 1) >> 1; i++) |
4599 | 1202 *sdst++ = bswap_16(*ssrc++); |
7671 | 1203 return src_size; |
4599 | 1204 case DCA_MARKER_14B_BE: |
1205 case DCA_MARKER_14B_LE: | |
1206 init_put_bits(&pb, dst, max_size); | |
1207 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) { | |
1208 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF; | |
1209 put_bits(&pb, 14, tmp); | |
1210 } | |
1211 flush_put_bits(&pb); | |
1212 return (put_bits_count(&pb) + 7) >> 3; | |
1213 default: | |
1214 return -1; | |
1215 } | |
1216 } | |
1217 | |
1218 /** | |
1219 * Main frame decoding function | |
1220 * FIXME add arguments | |
1221 */ | |
1222 static int dca_decode_frame(AVCodecContext * avctx, | |
1223 void *data, int *data_size, | |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1224 AVPacket *avpkt) |
4599 | 1225 { |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1226 const uint8_t *buf = avpkt->data; |
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1227 int buf_size = avpkt->size; |
4599 | 1228 |
7724
ea9aa2aa4caa
dca: Do float -> int16 interleaving in-place using s->dsp.float_to_int16_interleave()
andoma
parents:
7680
diff
changeset
|
1229 int i; |
4599 | 1230 int16_t *samples = data; |
1231 DCAContext *s = avctx->priv_data; | |
1232 int channels; | |
1233 | |
1234 | |
1235 s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE); | |
1236 if (s->dca_buffer_size == -1) { | |
5027 | 1237 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n"); |
4599 | 1238 return -1; |
1239 } | |
1240 | |
1241 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8); | |
1242 if (dca_parse_frame_header(s) < 0) { | |
1243 //seems like the frame is corrupt, try with the next one | |
5645 | 1244 *data_size=0; |
4599 | 1245 return buf_size; |
1246 } | |
1247 //set AVCodec values with parsed data | |
1248 avctx->sample_rate = s->sample_rate; | |
1249 avctx->bit_rate = s->bit_rate; | |
1250 | |
4893 | 1251 channels = s->prim_channels + !!s->lfe; |
8148
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1252 |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1253 if (s->amode<16) { |
8100 | 1254 avctx->channel_layout = dca_core_channel_layout[s->amode]; |
1255 | |
8148
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1256 if (s->lfe) { |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1257 avctx->channel_layout |= CH_LOW_FREQUENCY; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1258 s->channel_order_tab = dca_channel_reorder_lfe[s->amode]; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1259 } else |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1260 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode]; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1261 |
11304
3ef04d1190f0
Fixed a segfault in the DCA decoder with corrupted streams.
fenrir
parents:
10961
diff
changeset
|
1262 if (s->prim_channels > 0 && |
3ef04d1190f0
Fixed a segfault in the DCA decoder with corrupted streams.
fenrir
parents:
10961
diff
changeset
|
1263 s->channel_order_tab[s->prim_channels - 1] < 0) |
3ef04d1190f0
Fixed a segfault in the DCA decoder with corrupted streams.
fenrir
parents:
10961
diff
changeset
|
1264 return -1; |
3ef04d1190f0
Fixed a segfault in the DCA decoder with corrupted streams.
fenrir
parents:
10961
diff
changeset
|
1265 |
8148
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1266 if(avctx->request_channels == 2 && s->prim_channels > 2) { |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1267 channels = 2; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1268 s->output = DCA_STEREO; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1269 avctx->channel_layout = CH_LAYOUT_STEREO; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1270 } |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1271 } else { |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1272 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode); |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1273 return -1; |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1274 } |
146ac82af1e5
Proper channel output reordering for the dca decoder.
banan
parents:
8126
diff
changeset
|
1275 |
4893 | 1276 |
6577
1b90003d4d60
Only set channels in the stream if previously unset, fixes resampling crash on broken dca frames
banan
parents:
6517
diff
changeset
|
1277 /* 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
|
1278 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
|
1279 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
|
1280 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
|
1281 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
|
1282 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
|
1283 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
|
1284 |
4599 | 1285 if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels) |
1286 return -1; | |
7725 | 1287 *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels; |
4599 | 1288 for (i = 0; i < (s->sample_blocks / 8); i++) { |
1289 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
|
1290 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
|
1291 samples += 256 * channels; |
4599 | 1292 } |
1293 | |
1294 return buf_size; | |
1295 } | |
1296 | |
1297 | |
1298 | |
1299 /** | |
1300 * DCA initialization | |
1301 * | |
1302 * @param avctx pointer to the AVCodecContext | |
1303 */ | |
1304 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6463
diff
changeset
|
1305 static av_cold int dca_decode_init(AVCodecContext * avctx) |
4599 | 1306 { |
1307 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
|
1308 int i; |
4599 | 1309 |
1310 s->avctx = avctx; | |
1311 dca_init_vlcs(); | |
1312 | |
1313 dsputil_init(&s->dsp, avctx); | |
9658
67a20f0eb42c
Support for getting (i)MDCT output multiplied by a constant scaling factor.
serge
parents:
9526
diff
changeset
|
1314 ff_mdct_init(&s->imdct, 6, 1, 1.0); |
11592 | 1315 ff_synth_filter_init(&s->synth); |
6120 | 1316 |
7724
ea9aa2aa4caa
dca: Do float -> int16 interleaving in-place using s->dsp.float_to_int16_interleave()
andoma
parents:
7680
diff
changeset
|
1317 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
|
1318 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
|
1319 avctx->sample_fmt = SAMPLE_FMT_S16; |
8062
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1320 |
10377
98816e4d5522
dca and aac decoders use float_to_int16_interleave, so check for
conrad
parents:
10199
diff
changeset
|
1321 if(s->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) { |
8062
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1322 s->add_bias = 385.0f; |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1323 s->scale_bias = 1.0 / 32768.0; |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1324 } else { |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1325 s->add_bias = 0.0f; |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1326 s->scale_bias = 1.0; |
8063
6bc70b15451d
Disable codec downmix when not using simd instead of silently produce silence
banan
parents:
8062
diff
changeset
|
1327 |
6bc70b15451d
Disable codec downmix when not using simd instead of silently produce silence
banan
parents:
8062
diff
changeset
|
1328 /* allow downmixing to stereo */ |
6bc70b15451d
Disable codec downmix when not using simd instead of silently produce silence
banan
parents:
8062
diff
changeset
|
1329 if (avctx->channels > 0 && avctx->request_channels < avctx->channels && |
6bc70b15451d
Disable codec downmix when not using simd instead of silently produce silence
banan
parents:
8062
diff
changeset
|
1330 avctx->request_channels == 2) { |
6bc70b15451d
Disable codec downmix when not using simd instead of silently produce silence
banan
parents:
8062
diff
changeset
|
1331 avctx->channels = avctx->request_channels; |
6bc70b15451d
Disable codec downmix when not using simd instead of silently produce silence
banan
parents:
8062
diff
changeset
|
1332 } |
8062
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1333 } |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1334 |
17aeecee2a97
Fix dca decoder with non simd float2int16 conversion
banan
parents:
8061
diff
changeset
|
1335 |
4599 | 1336 return 0; |
1337 } | |
1338 | |
7738
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
1339 static av_cold int dca_decode_end(AVCodecContext * avctx) |
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
1340 { |
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
1341 DCAContext *s = avctx->priv_data; |
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
1342 ff_mdct_end(&s->imdct); |
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
1343 return 0; |
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
1344 } |
4599 | 1345 |
1346 AVCodec dca_decoder = { | |
1347 .name = "dca", | |
11560
8a4984c5cacc
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
11370
diff
changeset
|
1348 .type = AVMEDIA_TYPE_AUDIO, |
4599 | 1349 .id = CODEC_ID_DTS, |
1350 .priv_data_size = sizeof(DCAContext), | |
1351 .init = dca_decode_init, | |
1352 .decode = dca_decode_frame, | |
7738
93ba37a9098c
Replace obfuscated mdct in qmf_32_subbands() by ff_imdct_half().
michael
parents:
7737
diff
changeset
|
1353 .close = dca_decode_end, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6710
diff
changeset
|
1354 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"), |
4599 | 1355 }; |