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