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