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