Mercurial > libavcodec.hg
annotate ac3enc.c @ 8031:eebc7209c47f libavcodec
Convert asm keyword into __asm__.
Neither the asm() nor the __asm__() keyword is part of the C99
standard, but while GCC accepts the former in C89 syntax, it is not
accepted in C99 unless GNU extensions are turned on (with -fasm). The
latter form is accepted in any syntax as an extension (without
requiring further command-line options).
Sun Studio C99 compiler also does not accept asm() while accepting
__asm__(), albeit reporting warnings that it's not valid C99 syntax.
author | flameeyes |
---|---|
date | Thu, 16 Oct 2008 13:34:09 +0000 |
parents | e5f7a43f63ae |
children | 04423b2f6e0b |
rev | line source |
---|---|
0 | 1 /* |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
2 * The simplest AC-3 encoder |
429 | 3 * Copyright (c) 2000 Fabrice Bellard. |
0 | 4 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
429 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Lesser General Public License for more details. | |
0 | 16 * |
429 | 17 * You should have received a copy of the GNU Lesser General Public |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
1106 | 21 |
22 /** | |
23 * @file ac3enc.c | |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
24 * The simplest AC-3 encoder. |
1106 | 25 */ |
64 | 26 //#define DEBUG |
27 //#define DEBUG_BITALLOC | |
6763 | 28 #include "libavutil/crc.h" |
64 | 29 #include "avcodec.h" |
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2367
diff
changeset
|
30 #include "bitstream.h" |
782 | 31 #include "ac3.h" |
32 | |
33 typedef struct AC3EncodeContext { | |
34 PutBitContext pb; | |
35 int nb_channels; | |
36 int nb_all_channels; | |
37 int lfe_channel; | |
38 int bit_rate; | |
1057 | 39 unsigned int sample_rate; |
6005 | 40 unsigned int bitstream_id; |
1057 | 41 unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */ |
42 unsigned int frame_size; /* current frame size in words */ | |
3246 | 43 unsigned int bits_written; |
44 unsigned int samples_written; | |
6003 | 45 int sr_shift; |
6005 | 46 unsigned int frame_size_code; |
6003 | 47 unsigned int sr_code; /* frequency */ |
6005 | 48 unsigned int channel_mode; |
782 | 49 int lfe; |
6005 | 50 unsigned int bitstream_mode; |
782 | 51 short last_samples[AC3_MAX_CHANNELS][256]; |
1057 | 52 unsigned int chbwcod[AC3_MAX_CHANNELS]; |
782 | 53 int nb_coefs[AC3_MAX_CHANNELS]; |
2967 | 54 |
782 | 55 /* bitrate allocation control */ |
6003 | 56 int slow_gain_code, slow_decay_code, fast_decay_code, db_per_bit_code, floor_code; |
782 | 57 AC3BitAllocParameters bit_alloc; |
6003 | 58 int coarse_snr_offset; |
59 int fast_gain_code[AC3_MAX_CHANNELS]; | |
60 int fine_snr_offset[AC3_MAX_CHANNELS]; | |
782 | 61 /* mantissa encoding */ |
62 int mant1_cnt, mant2_cnt, mant4_cnt; | |
63 } AC3EncodeContext; | |
64 | |
4643
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
65 static int16_t costab[64]; |
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
66 static int16_t sintab[64]; |
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
67 static int16_t xcos1[128]; |
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
68 static int16_t xsin1[128]; |
0 | 69 |
70 #define MDCT_NBITS 9 | |
71 #define N (1 << MDCT_NBITS) | |
72 | |
73 /* new exponents are sent if their Norm 1 exceed this number */ | |
74 #define EXP_DIFF_THRESHOLD 1000 | |
75 | |
1064 | 76 static inline int16_t fix15(float a) |
0 | 77 { |
78 int v; | |
79 v = (int)(a * (float)(1 << 15)); | |
80 if (v < -32767) | |
81 v = -32767; | |
2967 | 82 else if (v > 32767) |
0 | 83 v = 32767; |
84 return v; | |
85 } | |
86 | |
87 typedef struct IComplex { | |
88 short re,im; | |
89 } IComplex; | |
90 | |
91 static void fft_init(int ln) | |
92 { | |
6166 | 93 int i, n; |
0 | 94 float alpha; |
95 | |
96 n = 1 << ln; | |
97 | |
98 for(i=0;i<(n/2);i++) { | |
99 alpha = 2 * M_PI * (float)i / (float)n; | |
100 costab[i] = fix15(cos(alpha)); | |
101 sintab[i] = fix15(sin(alpha)); | |
102 } | |
103 } | |
104 | |
105 /* butter fly op */ | |
106 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \ | |
107 {\ | |
108 int ax, ay, bx, by;\ | |
109 bx=pre1;\ | |
110 by=pim1;\ | |
111 ax=qre1;\ | |
112 ay=qim1;\ | |
113 pre = (bx + ax) >> 1;\ | |
114 pim = (by + ay) >> 1;\ | |
115 qre = (bx - ax) >> 1;\ | |
116 qim = (by - ay) >> 1;\ | |
117 } | |
118 | |
119 #define MUL16(a,b) ((a) * (b)) | |
120 | |
121 #define CMUL(pre, pim, are, aim, bre, bim) \ | |
122 {\ | |
123 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\ | |
124 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\ | |
125 } | |
126 | |
127 | |
128 /* do a 2^n point complex fft on 2^ln points. */ | |
129 static void fft(IComplex *z, int ln) | |
130 { | |
2979 | 131 int j, l, np, np2; |
132 int nblocks, nloops; | |
0 | 133 register IComplex *p,*q; |
134 int tmp_re, tmp_im; | |
135 | |
136 np = 1 << ln; | |
137 | |
138 /* reverse */ | |
139 for(j=0;j<np;j++) { | |
6141
932523fb0562
Get rid of fft_rev table, use ff_reverse and a shift.
reimar
parents:
6140
diff
changeset
|
140 int k = ff_reverse[j] >> (8 - ln); |
6140 | 141 if (k < j) |
142 FFSWAP(IComplex, z[k], z[j]); | |
0 | 143 } |
144 | |
145 /* pass 0 */ | |
146 | |
147 p=&z[0]; | |
148 j=(np >> 1); | |
149 do { | |
2967 | 150 BF(p[0].re, p[0].im, p[1].re, p[1].im, |
0 | 151 p[0].re, p[0].im, p[1].re, p[1].im); |
152 p+=2; | |
153 } while (--j != 0); | |
154 | |
155 /* pass 1 */ | |
156 | |
157 p=&z[0]; | |
158 j=np >> 2; | |
159 do { | |
2967 | 160 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
0 | 161 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 162 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
0 | 163 p[1].re, p[1].im, p[3].im, -p[3].re); |
164 p+=4; | |
165 } while (--j != 0); | |
166 | |
167 /* pass 2 .. ln-1 */ | |
168 | |
169 nblocks = np >> 3; | |
170 nloops = 1 << 2; | |
171 np2 = np >> 1; | |
172 do { | |
173 p = z; | |
174 q = z + nloops; | |
175 for (j = 0; j < nblocks; ++j) { | |
176 | |
177 BF(p->re, p->im, q->re, q->im, | |
178 p->re, p->im, q->re, q->im); | |
2967 | 179 |
0 | 180 p++; |
181 q++; | |
182 for(l = nblocks; l < np2; l += nblocks) { | |
183 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im); | |
184 BF(p->re, p->im, q->re, q->im, | |
185 p->re, p->im, tmp_re, tmp_im); | |
186 p++; | |
187 q++; | |
188 } | |
189 p += nloops; | |
190 q += nloops; | |
191 } | |
192 nblocks = nblocks >> 1; | |
193 nloops = nloops << 1; | |
194 } while (nblocks != 0); | |
195 } | |
196 | |
197 /* do a 512 point mdct */ | |
1064 | 198 static void mdct512(int32_t *out, int16_t *in) |
0 | 199 { |
200 int i, re, im, re1, im1; | |
2967 | 201 int16_t rot[N]; |
0 | 202 IComplex x[N/4]; |
203 | |
204 /* shift to simplify computations */ | |
205 for(i=0;i<N/4;i++) | |
206 rot[i] = -in[i + 3*N/4]; | |
207 for(i=N/4;i<N;i++) | |
208 rot[i] = in[i - N/4]; | |
2967 | 209 |
0 | 210 /* pre rotation */ |
211 for(i=0;i<N/4;i++) { | |
212 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1; | |
213 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1; | |
214 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]); | |
215 } | |
216 | |
217 fft(x, MDCT_NBITS - 2); | |
2967 | 218 |
0 | 219 /* post rotation */ |
220 for(i=0;i<N/4;i++) { | |
221 re = x[i].re; | |
222 im = x[i].im; | |
223 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]); | |
224 out[2*i] = im1; | |
225 out[N/2-1-2*i] = re1; | |
226 } | |
227 } | |
228 | |
229 /* XXX: use another norm ? */ | |
1064 | 230 static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n) |
0 | 231 { |
232 int sum, i; | |
233 sum = 0; | |
234 for(i=0;i<n;i++) { | |
235 sum += abs(exp1[i] - exp2[i]); | |
236 } | |
237 return sum; | |
238 } | |
239 | |
1064 | 240 static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
241 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
314 | 242 int ch, int is_lfe) |
0 | 243 { |
244 int i, j; | |
245 int exp_diff; | |
2967 | 246 |
0 | 247 /* estimate if the exponent variation & decide if they should be |
248 reused in the next frame */ | |
249 exp_strategy[0][ch] = EXP_NEW; | |
250 for(i=1;i<NB_BLOCKS;i++) { | |
251 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2); | |
2967 | 252 #ifdef DEBUG |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
253 av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff); |
0 | 254 #endif |
255 if (exp_diff > EXP_DIFF_THRESHOLD) | |
256 exp_strategy[i][ch] = EXP_NEW; | |
257 else | |
258 exp_strategy[i][ch] = EXP_REUSE; | |
259 } | |
314 | 260 if (is_lfe) |
2979 | 261 return; |
314 | 262 |
0 | 263 /* now select the encoding strategy type : if exponents are often |
264 recoded, we use a coarse encoding */ | |
265 i = 0; | |
266 while (i < NB_BLOCKS) { | |
267 j = i + 1; | |
268 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) | |
269 j++; | |
270 switch(j - i) { | |
271 case 1: | |
272 exp_strategy[i][ch] = EXP_D45; | |
273 break; | |
274 case 2: | |
275 case 3: | |
276 exp_strategy[i][ch] = EXP_D25; | |
277 break; | |
278 default: | |
279 exp_strategy[i][ch] = EXP_D15; | |
280 break; | |
281 } | |
2979 | 282 i = j; |
0 | 283 } |
284 } | |
285 | |
286 /* set exp[i] to min(exp[i], exp1[i]) */ | |
1064 | 287 static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n) |
0 | 288 { |
289 int i; | |
290 | |
291 for(i=0;i<n;i++) { | |
292 if (exp1[i] < exp[i]) | |
293 exp[i] = exp1[i]; | |
294 } | |
295 } | |
2967 | 296 |
0 | 297 /* update the exponents so that they are the ones the decoder will |
298 decode. Return the number of bits used to code the exponents */ | |
2967 | 299 static int encode_exp(uint8_t encoded_exp[N/2], |
300 uint8_t exp[N/2], | |
0 | 301 int nb_exps, |
302 int exp_strategy) | |
303 { | |
2157
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
304 int group_size, nb_groups, i, j, k, exp_min; |
1064 | 305 uint8_t exp1[N/2]; |
0 | 306 |
307 switch(exp_strategy) { | |
308 case EXP_D15: | |
309 group_size = 1; | |
310 break; | |
311 case EXP_D25: | |
312 group_size = 2; | |
313 break; | |
314 default: | |
315 case EXP_D45: | |
316 group_size = 4; | |
317 break; | |
318 } | |
319 nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3; | |
320 | |
321 /* for each group, compute the minimum exponent */ | |
322 exp1[0] = exp[0]; /* DC exponent is handled separately */ | |
323 k = 1; | |
324 for(i=1;i<=nb_groups;i++) { | |
325 exp_min = exp[k]; | |
326 assert(exp_min >= 0 && exp_min <= 24); | |
327 for(j=1;j<group_size;j++) { | |
328 if (exp[k+j] < exp_min) | |
329 exp_min = exp[k+j]; | |
330 } | |
331 exp1[i] = exp_min; | |
332 k += group_size; | |
333 } | |
334 | |
335 /* constraint for DC exponent */ | |
336 if (exp1[0] > 15) | |
337 exp1[0] = 15; | |
338 | |
2157
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
339 /* Decrease the delta between each groups to within 2 |
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
340 * so that they can be differentially encoded */ |
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
341 for (i=1;i<=nb_groups;i++) |
2979 | 342 exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2); |
2157
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
343 for (i=nb_groups-1;i>=0;i--) |
2979 | 344 exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2); |
2157
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
345 |
0 | 346 /* now we have the exponent values the decoder will see */ |
347 encoded_exp[0] = exp1[0]; | |
348 k = 1; | |
349 for(i=1;i<=nb_groups;i++) { | |
350 for(j=0;j<group_size;j++) { | |
351 encoded_exp[k+j] = exp1[i]; | |
352 } | |
353 k += group_size; | |
354 } | |
2967 | 355 |
0 | 356 #if defined(DEBUG) |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
357 av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy); |
0 | 358 for(i=0;i<=nb_groups * group_size;i++) { |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
359 av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]); |
0 | 360 } |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
361 av_log(NULL, AV_LOG_DEBUG, "\n"); |
0 | 362 #endif |
363 | |
364 return 4 + (nb_groups / 3) * 7; | |
365 } | |
366 | |
367 /* return the size in bits taken by the mantissa */ | |
1064 | 368 static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
0 | 369 { |
370 int bits, mant, i; | |
371 | |
372 bits = 0; | |
373 for(i=0;i<nb_coefs;i++) { | |
374 mant = m[i]; | |
375 switch(mant) { | |
376 case 0: | |
377 /* nothing */ | |
378 break; | |
379 case 1: | |
380 /* 3 mantissa in 5 bits */ | |
2967 | 381 if (s->mant1_cnt == 0) |
0 | 382 bits += 5; |
383 if (++s->mant1_cnt == 3) | |
384 s->mant1_cnt = 0; | |
385 break; | |
386 case 2: | |
387 /* 3 mantissa in 7 bits */ | |
2967 | 388 if (s->mant2_cnt == 0) |
0 | 389 bits += 7; |
390 if (++s->mant2_cnt == 3) | |
391 s->mant2_cnt = 0; | |
392 break; | |
393 case 3: | |
394 bits += 3; | |
395 break; | |
396 case 4: | |
397 /* 2 mantissa in 7 bits */ | |
398 if (s->mant4_cnt == 0) | |
399 bits += 7; | |
2967 | 400 if (++s->mant4_cnt == 2) |
0 | 401 s->mant4_cnt = 0; |
402 break; | |
403 case 14: | |
404 bits += 14; | |
405 break; | |
406 case 15: | |
407 bits += 16; | |
408 break; | |
409 default: | |
410 bits += mant - 1; | |
411 break; | |
412 } | |
413 } | |
414 return bits; | |
415 } | |
416 | |
417 | |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
418 static void bit_alloc_masking(AC3EncodeContext *s, |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
419 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
420 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
421 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
422 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50]) |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
423 { |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
424 int blk, ch; |
6003 | 425 int16_t band_psd[NB_BLOCKS][AC3_MAX_CHANNELS][50]; |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
426 |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
427 for(blk=0; blk<NB_BLOCKS; blk++) { |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
428 for(ch=0;ch<s->nb_all_channels;ch++) { |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
429 if(exp_strategy[blk][ch] == EXP_REUSE) { |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
430 memcpy(psd[blk][ch], psd[blk-1][ch], (N/2)*sizeof(int16_t)); |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
431 memcpy(mask[blk][ch], mask[blk-1][ch], 50*sizeof(int16_t)); |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
432 } else { |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
433 ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0, |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
434 s->nb_coefs[ch], |
6003 | 435 psd[blk][ch], band_psd[blk][ch]); |
436 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, band_psd[blk][ch], | |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
437 0, s->nb_coefs[ch], |
6003 | 438 ff_ac3_fast_gain_tab[s->fast_gain_code[ch]], |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
439 ch == s->lfe_channel, |
5331 | 440 DBA_NONE, 0, NULL, NULL, NULL, |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
441 mask[blk][ch]); |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
442 } |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
443 } |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
444 } |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
445 } |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
446 |
0 | 447 static int bit_alloc(AC3EncodeContext *s, |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
448 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50], |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
449 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
1064 | 450 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
6003 | 451 int frame_bits, int coarse_snr_offset, int fine_snr_offset) |
0 | 452 { |
453 int i, ch; | |
6003 | 454 int snr_offset; |
4705 | 455 |
6003 | 456 snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2; |
0 | 457 |
458 /* compute size */ | |
459 for(i=0;i<NB_BLOCKS;i++) { | |
460 s->mant1_cnt = 0; | |
461 s->mant2_cnt = 0; | |
462 s->mant4_cnt = 0; | |
314 | 463 for(ch=0;ch<s->nb_all_channels;ch++) { |
4706 | 464 ff_ac3_bit_alloc_calc_bap(mask[i][ch], psd[i][ch], 0, |
6003 | 465 s->nb_coefs[ch], snr_offset, |
7017 | 466 s->bit_alloc.floor, ff_ac3_bap_tab, |
467 bap[i][ch]); | |
2967 | 468 frame_bits += compute_mantissa_size(s, bap[i][ch], |
0 | 469 s->nb_coefs[ch]); |
470 } | |
471 } | |
472 #if 0 | |
2967 | 473 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n", |
6003 | 474 coarse_snr_offset, fine_snr_offset, frame_bits, |
0 | 475 16 * s->frame_size - ((frame_bits + 7) & ~7)); |
476 #endif | |
477 return 16 * s->frame_size - frame_bits; | |
478 } | |
479 | |
480 #define SNR_INC1 4 | |
481 | |
482 static int compute_bit_allocation(AC3EncodeContext *s, | |
1064 | 483 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
484 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
485 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |
0 | 486 int frame_bits) |
487 { | |
488 int i, ch; | |
6003 | 489 int coarse_snr_offset, fine_snr_offset; |
1064 | 490 uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
491 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
492 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50]; |
7129 | 493 static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; |
0 | 494 |
495 /* init default parameters */ | |
6003 | 496 s->slow_decay_code = 2; |
497 s->fast_decay_code = 1; | |
498 s->slow_gain_code = 1; | |
499 s->db_per_bit_code = 2; | |
500 s->floor_code = 4; | |
2967 | 501 for(ch=0;ch<s->nb_all_channels;ch++) |
6003 | 502 s->fast_gain_code[ch] = 4; |
2967 | 503 |
0 | 504 /* compute real values */ |
6003 | 505 s->bit_alloc.sr_code = s->sr_code; |
506 s->bit_alloc.sr_shift = s->sr_shift; | |
507 s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->sr_shift; | |
508 s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->sr_shift; | |
509 s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code]; | |
510 s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code]; | |
511 s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code]; | |
2967 | 512 |
0 | 513 /* header size */ |
514 frame_bits += 65; | |
6005 | 515 // if (s->channel_mode == 2) |
314 | 516 // frame_bits += 2; |
6005 | 517 frame_bits += frame_bits_inc[s->channel_mode]; |
0 | 518 |
519 /* audio blocks */ | |
520 for(i=0;i<NB_BLOCKS;i++) { | |
314 | 521 frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */ |
6005 | 522 if (s->channel_mode == AC3_CHMODE_STEREO) { |
314 | 523 frame_bits++; /* rematstr */ |
2644
6ff5dc0dbaf0
While adding stereo rematrixing, I came across something that needs to
michael
parents:
2398
diff
changeset
|
524 if(i==0) frame_bits += 4; |
6ff5dc0dbaf0
While adding stereo rematrixing, I came across something that needs to
michael
parents:
2398
diff
changeset
|
525 } |
314 | 526 frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */ |
2979 | 527 if (s->lfe) |
528 frame_bits++; /* lfeexpstr */ | |
0 | 529 for(ch=0;ch<s->nb_channels;ch++) { |
530 if (exp_strategy[i][ch] != EXP_REUSE) | |
314 | 531 frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */ |
0 | 532 } |
533 frame_bits++; /* baie */ | |
534 frame_bits++; /* snr */ | |
535 frame_bits += 2; /* delta / skip */ | |
536 } | |
537 frame_bits++; /* cplinu for block 0 */ | |
538 /* bit alloc info */ | |
314 | 539 /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */ |
540 /* csnroffset[6] */ | |
541 /* (fsnoffset[4] + fgaincod[4]) * c */ | |
542 frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3); | |
0 | 543 |
1819
34cdcb221665
auxdatae, crcrs fix by (Jean-Francois Panisset <panisset at comcast dot net>)
michael
parents:
1602
diff
changeset
|
544 /* auxdatae, crcrsv */ |
34cdcb221665
auxdatae, crcrs fix by (Jean-Francois Panisset <panisset at comcast dot net>)
michael
parents:
1602
diff
changeset
|
545 frame_bits += 2; |
34cdcb221665
auxdatae, crcrs fix by (Jean-Francois Panisset <panisset at comcast dot net>)
michael
parents:
1602
diff
changeset
|
546 |
0 | 547 /* CRC */ |
548 frame_bits += 16; | |
549 | |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
550 /* calculate psd and masking curve before doing bit allocation */ |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
551 bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask); |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
552 |
0 | 553 /* now the big work begins : do the bit allocation. Modify the snr |
554 offset until we can pack everything in the requested frame size */ | |
555 | |
6003 | 556 coarse_snr_offset = s->coarse_snr_offset; |
557 while (coarse_snr_offset >= 0 && | |
558 bit_alloc(s, mask, psd, bap, frame_bits, coarse_snr_offset, 0) < 0) | |
559 coarse_snr_offset -= SNR_INC1; | |
560 if (coarse_snr_offset < 0) { | |
6081 | 561 av_log(NULL, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n"); |
2979 | 562 return -1; |
0 | 563 } |
6003 | 564 while ((coarse_snr_offset + SNR_INC1) <= 63 && |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
565 bit_alloc(s, mask, psd, bap1, frame_bits, |
6003 | 566 coarse_snr_offset + SNR_INC1, 0) >= 0) { |
567 coarse_snr_offset += SNR_INC1; | |
0 | 568 memcpy(bap, bap1, sizeof(bap1)); |
569 } | |
6003 | 570 while ((coarse_snr_offset + 1) <= 63 && |
571 bit_alloc(s, mask, psd, bap1, frame_bits, coarse_snr_offset + 1, 0) >= 0) { | |
572 coarse_snr_offset++; | |
0 | 573 memcpy(bap, bap1, sizeof(bap1)); |
574 } | |
575 | |
6003 | 576 fine_snr_offset = 0; |
577 while ((fine_snr_offset + SNR_INC1) <= 15 && | |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
578 bit_alloc(s, mask, psd, bap1, frame_bits, |
6003 | 579 coarse_snr_offset, fine_snr_offset + SNR_INC1) >= 0) { |
580 fine_snr_offset += SNR_INC1; | |
0 | 581 memcpy(bap, bap1, sizeof(bap1)); |
582 } | |
6003 | 583 while ((fine_snr_offset + 1) <= 15 && |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
584 bit_alloc(s, mask, psd, bap1, frame_bits, |
6003 | 585 coarse_snr_offset, fine_snr_offset + 1) >= 0) { |
586 fine_snr_offset++; | |
0 | 587 memcpy(bap, bap1, sizeof(bap1)); |
588 } | |
2967 | 589 |
6003 | 590 s->coarse_snr_offset = coarse_snr_offset; |
314 | 591 for(ch=0;ch<s->nb_all_channels;ch++) |
6003 | 592 s->fine_snr_offset[ch] = fine_snr_offset; |
0 | 593 #if defined(DEBUG_BITALLOC) |
594 { | |
595 int j; | |
596 | |
597 for(i=0;i<6;i++) { | |
314 | 598 for(ch=0;ch<s->nb_all_channels;ch++) { |
0 | 599 printf("Block #%d Ch%d:\n", i, ch); |
600 printf("bap="); | |
601 for(j=0;j<s->nb_coefs[ch];j++) { | |
602 printf("%d ",bap[i][ch][j]); | |
603 } | |
604 printf("\n"); | |
605 } | |
606 } | |
607 } | |
608 #endif | |
609 return 0; | |
610 } | |
611 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6516
diff
changeset
|
612 static av_cold int AC3_encode_init(AVCodecContext *avctx) |
0 | 613 { |
614 int freq = avctx->sample_rate; | |
615 int bitrate = avctx->bit_rate; | |
616 int channels = avctx->channels; | |
617 AC3EncodeContext *s = avctx->priv_data; | |
782 | 618 int i, j, ch; |
0 | 619 float alpha; |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
620 int bw_code; |
6005 | 621 static const uint8_t channel_mode_defs[6] = { |
2979 | 622 0x01, /* C */ |
623 0x02, /* L R */ | |
624 0x03, /* L C R */ | |
625 0x06, /* L R SL SR */ | |
626 0x07, /* L C R SL SR */ | |
627 0x07, /* L C R SL SR (+LFE) */ | |
314 | 628 }; |
0 | 629 |
630 avctx->frame_size = AC3_FRAME_SIZE; | |
2967 | 631 |
4645
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4643
diff
changeset
|
632 ac3_common_init(); |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4643
diff
changeset
|
633 |
0 | 634 /* number of channels */ |
314 | 635 if (channels < 1 || channels > 6) |
2979 | 636 return -1; |
6005 | 637 s->channel_mode = channel_mode_defs[channels - 1]; |
314 | 638 s->lfe = (channels == 6) ? 1 : 0; |
639 s->nb_all_channels = channels; | |
640 s->nb_channels = channels > 5 ? 5 : channels; | |
641 s->lfe_channel = s->lfe ? 5 : -1; | |
0 | 642 |
643 /* frequency */ | |
644 for(i=0;i<3;i++) { | |
2967 | 645 for(j=0;j<3;j++) |
6002 | 646 if ((ff_ac3_sample_rate_tab[j] >> i) == freq) |
0 | 647 goto found; |
648 } | |
649 return -1; | |
2967 | 650 found: |
0 | 651 s->sample_rate = freq; |
6003 | 652 s->sr_shift = i; |
653 s->sr_code = j; | |
6005 | 654 s->bitstream_id = 8 + s->sr_shift; |
655 s->bitstream_mode = 0; /* complete main audio service */ | |
0 | 656 |
657 /* bitrate & frame size */ | |
658 for(i=0;i<19;i++) { | |
6082 | 659 if ((ff_ac3_bitrate_tab[i] >> s->sr_shift)*1000 == bitrate) |
0 | 660 break; |
661 } | |
662 if (i == 19) | |
663 return -1; | |
664 s->bit_rate = bitrate; | |
6005 | 665 s->frame_size_code = i << 1; |
666 s->frame_size_min = ff_ac3_frame_size_tab[s->frame_size_code][s->sr_code]; | |
3246 | 667 s->bits_written = 0; |
668 s->samples_written = 0; | |
0 | 669 s->frame_size = s->frame_size_min; |
2967 | 670 |
0 | 671 /* bit allocation init */ |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
672 if(avctx->cutoff) { |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
673 /* calculate bandwidth based on user-specified cutoff frequency */ |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
674 int cutoff = av_clip(avctx->cutoff, 1, s->sample_rate >> 1); |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
675 int fbw_coeffs = cutoff * 512 / s->sample_rate; |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
676 bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60); |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
677 } else { |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
678 /* use default bandwidth setting */ |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
679 /* XXX: should compute the bandwidth according to the frame |
6516 | 680 size, so that we avoid annoying high frequency artifacts */ |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
681 bw_code = 50; |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
682 } |
0 | 683 for(ch=0;ch<s->nb_channels;ch++) { |
684 /* bandwidth for each channel */ | |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
685 s->chbwcod[ch] = bw_code; |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
686 s->nb_coefs[ch] = bw_code * 3 + 73; |
0 | 687 } |
314 | 688 if (s->lfe) { |
2979 | 689 s->nb_coefs[s->lfe_channel] = 7; /* fixed */ |
314 | 690 } |
0 | 691 /* initial snr offset */ |
6003 | 692 s->coarse_snr_offset = 40; |
0 | 693 |
694 /* mdct init */ | |
695 fft_init(MDCT_NBITS - 2); | |
696 for(i=0;i<N/4;i++) { | |
697 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N; | |
698 xcos1[i] = fix15(-cos(alpha)); | |
699 xsin1[i] = fix15(-sin(alpha)); | |
700 } | |
701 | |
925 | 702 avctx->coded_frame= avcodec_alloc_frame(); |
703 avctx->coded_frame->key_frame= 1; | |
0 | 704 |
705 return 0; | |
706 } | |
707 | |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
708 /* output the AC-3 frame header */ |
0 | 709 static void output_frame_header(AC3EncodeContext *s, unsigned char *frame) |
710 { | |
1522
79dddc5cd990
removed the obsolete and unused parameters of init_put_bits
alex
parents:
1408
diff
changeset
|
711 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE); |
0 | 712 |
713 put_bits(&s->pb, 16, 0x0b77); /* frame header */ | |
714 put_bits(&s->pb, 16, 0); /* crc1: will be filled later */ | |
6003 | 715 put_bits(&s->pb, 2, s->sr_code); |
6005 | 716 put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min)); |
717 put_bits(&s->pb, 5, s->bitstream_id); | |
718 put_bits(&s->pb, 3, s->bitstream_mode); | |
719 put_bits(&s->pb, 3, s->channel_mode); | |
720 if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO) | |
2979 | 721 put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */ |
6005 | 722 if (s->channel_mode & 0x04) |
2979 | 723 put_bits(&s->pb, 2, 1); /* XXX -6 dB */ |
6005 | 724 if (s->channel_mode == AC3_CHMODE_STEREO) |
0 | 725 put_bits(&s->pb, 2, 0); /* surround not indicated */ |
314 | 726 put_bits(&s->pb, 1, s->lfe); /* LFE */ |
0 | 727 put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */ |
728 put_bits(&s->pb, 1, 0); /* no compression control word */ | |
729 put_bits(&s->pb, 1, 0); /* no lang code */ | |
730 put_bits(&s->pb, 1, 0); /* no audio production info */ | |
731 put_bits(&s->pb, 1, 0); /* no copyright */ | |
732 put_bits(&s->pb, 1, 1); /* original bitstream */ | |
733 put_bits(&s->pb, 1, 0); /* no time code 1 */ | |
734 put_bits(&s->pb, 1, 0); /* no time code 2 */ | |
6080 | 735 put_bits(&s->pb, 1, 0); /* no additional bit stream info */ |
0 | 736 } |
737 | |
738 /* symetric quantization on 'levels' levels */ | |
739 static inline int sym_quant(int c, int e, int levels) | |
740 { | |
741 int v; | |
742 | |
743 if (c >= 0) { | |
87 | 744 v = (levels * (c << e)) >> 24; |
745 v = (v + 1) >> 1; | |
0 | 746 v = (levels >> 1) + v; |
747 } else { | |
87 | 748 v = (levels * ((-c) << e)) >> 24; |
749 v = (v + 1) >> 1; | |
0 | 750 v = (levels >> 1) - v; |
751 } | |
752 assert (v >= 0 && v < levels); | |
753 return v; | |
754 } | |
755 | |
756 /* asymetric quantization on 2^qbits levels */ | |
757 static inline int asym_quant(int c, int e, int qbits) | |
758 { | |
759 int lshift, m, v; | |
760 | |
761 lshift = e + qbits - 24; | |
762 if (lshift >= 0) | |
763 v = c << lshift; | |
764 else | |
765 v = c >> (-lshift); | |
766 /* rounding */ | |
767 v = (v + 1) >> 1; | |
768 m = (1 << (qbits-1)); | |
769 if (v >= m) | |
770 v = m - 1; | |
771 assert(v >= -m); | |
772 return v & ((1 << qbits)-1); | |
773 } | |
774 | |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
775 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC-3 |
0 | 776 frame */ |
777 static void output_audio_block(AC3EncodeContext *s, | |
1064 | 778 uint8_t exp_strategy[AC3_MAX_CHANNELS], |
779 uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2], | |
780 uint8_t bap[AC3_MAX_CHANNELS][N/2], | |
781 int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2], | |
782 int8_t global_exp[AC3_MAX_CHANNELS], | |
0 | 783 int block_num) |
784 { | |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
785 int ch, nb_groups, group_size, i, baie, rbnd; |
1064 | 786 uint8_t *p; |
787 uint16_t qmant[AC3_MAX_CHANNELS][N/2]; | |
0 | 788 int exp0, exp1; |
789 int mant1_cnt, mant2_cnt, mant4_cnt; | |
1064 | 790 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; |
0 | 791 int delta0, delta1, delta2; |
792 | |
2967 | 793 for(ch=0;ch<s->nb_channels;ch++) |
0 | 794 put_bits(&s->pb, 1, 0); /* 512 point MDCT */ |
2967 | 795 for(ch=0;ch<s->nb_channels;ch++) |
0 | 796 put_bits(&s->pb, 1, 1); /* no dither */ |
797 put_bits(&s->pb, 1, 0); /* no dynamic range */ | |
798 if (block_num == 0) { | |
799 /* for block 0, even if no coupling, we must say it. This is a | |
800 waste of bit :-) */ | |
801 put_bits(&s->pb, 1, 1); /* coupling strategy present */ | |
802 put_bits(&s->pb, 1, 0); /* no coupling strategy */ | |
803 } else { | |
804 put_bits(&s->pb, 1, 0); /* no new coupling strategy */ | |
805 } | |
806 | |
6005 | 807 if (s->channel_mode == AC3_CHMODE_STEREO) |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
808 { |
2979 | 809 if(block_num==0) |
810 { | |
811 /* first block must define rematrixing (rematstr) */ | |
812 put_bits(&s->pb, 1, 1); | |
2967 | 813 |
2979 | 814 /* dummy rematrixing rematflg(1:4)=0 */ |
815 for (rbnd=0;rbnd<4;rbnd++) | |
816 put_bits(&s->pb, 1, 0); | |
817 } | |
818 else | |
819 { | |
820 /* no matrixing (but should be used in the future) */ | |
821 put_bits(&s->pb, 1, 0); | |
822 } | |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
823 } |
0 | 824 |
2967 | 825 #if defined(DEBUG) |
0 | 826 { |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
827 static int count = 0; |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
828 av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++); |
0 | 829 } |
830 #endif | |
831 /* exponent strategy */ | |
832 for(ch=0;ch<s->nb_channels;ch++) { | |
833 put_bits(&s->pb, 2, exp_strategy[ch]); | |
834 } | |
2967 | 835 |
314 | 836 if (s->lfe) { |
2979 | 837 put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]); |
314 | 838 } |
839 | |
0 | 840 for(ch=0;ch<s->nb_channels;ch++) { |
841 if (exp_strategy[ch] != EXP_REUSE) | |
842 put_bits(&s->pb, 6, s->chbwcod[ch]); | |
843 } | |
2967 | 844 |
0 | 845 /* exponents */ |
314 | 846 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 847 switch(exp_strategy[ch]) { |
848 case EXP_REUSE: | |
849 continue; | |
850 case EXP_D15: | |
851 group_size = 1; | |
852 break; | |
853 case EXP_D25: | |
854 group_size = 2; | |
855 break; | |
856 default: | |
857 case EXP_D45: | |
858 group_size = 4; | |
859 break; | |
860 } | |
2979 | 861 nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size); |
0 | 862 p = encoded_exp[ch]; |
863 | |
864 /* first exponent */ | |
865 exp1 = *p++; | |
866 put_bits(&s->pb, 4, exp1); | |
867 | |
868 /* next ones are delta encoded */ | |
869 for(i=0;i<nb_groups;i++) { | |
870 /* merge three delta in one code */ | |
871 exp0 = exp1; | |
872 exp1 = p[0]; | |
873 p += group_size; | |
874 delta0 = exp1 - exp0 + 2; | |
875 | |
876 exp0 = exp1; | |
877 exp1 = p[0]; | |
878 p += group_size; | |
879 delta1 = exp1 - exp0 + 2; | |
880 | |
881 exp0 = exp1; | |
882 exp1 = p[0]; | |
883 p += group_size; | |
884 delta2 = exp1 - exp0 + 2; | |
885 | |
886 put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2); | |
887 } | |
888 | |
2979 | 889 if (ch != s->lfe_channel) |
890 put_bits(&s->pb, 2, 0); /* no gain range info */ | |
0 | 891 } |
892 | |
893 /* bit allocation info */ | |
894 baie = (block_num == 0); | |
895 put_bits(&s->pb, 1, baie); | |
896 if (baie) { | |
6003 | 897 put_bits(&s->pb, 2, s->slow_decay_code); |
898 put_bits(&s->pb, 2, s->fast_decay_code); | |
899 put_bits(&s->pb, 2, s->slow_gain_code); | |
900 put_bits(&s->pb, 2, s->db_per_bit_code); | |
901 put_bits(&s->pb, 3, s->floor_code); | |
0 | 902 } |
903 | |
904 /* snr offset */ | |
905 put_bits(&s->pb, 1, baie); /* always present with bai */ | |
906 if (baie) { | |
6003 | 907 put_bits(&s->pb, 6, s->coarse_snr_offset); |
314 | 908 for(ch=0;ch<s->nb_all_channels;ch++) { |
6003 | 909 put_bits(&s->pb, 4, s->fine_snr_offset[ch]); |
910 put_bits(&s->pb, 3, s->fast_gain_code[ch]); | |
0 | 911 } |
912 } | |
2967 | 913 |
0 | 914 put_bits(&s->pb, 1, 0); /* no delta bit allocation */ |
915 put_bits(&s->pb, 1, 0); /* no data to skip */ | |
916 | |
917 /* mantissa encoding : we use two passes to handle the grouping. A | |
918 one pass method may be faster, but it would necessitate to | |
919 modify the output stream. */ | |
920 | |
921 /* first pass: quantize */ | |
922 mant1_cnt = mant2_cnt = mant4_cnt = 0; | |
923 qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL; | |
924 | |
314 | 925 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 926 int b, c, e, v; |
927 | |
928 for(i=0;i<s->nb_coefs[ch];i++) { | |
929 c = mdct_coefs[ch][i]; | |
930 e = encoded_exp[ch][i] - global_exp[ch]; | |
931 b = bap[ch][i]; | |
932 switch(b) { | |
933 case 0: | |
934 v = 0; | |
935 break; | |
936 case 1: | |
937 v = sym_quant(c, e, 3); | |
938 switch(mant1_cnt) { | |
939 case 0: | |
940 qmant1_ptr = &qmant[ch][i]; | |
941 v = 9 * v; | |
942 mant1_cnt = 1; | |
943 break; | |
944 case 1: | |
945 *qmant1_ptr += 3 * v; | |
946 mant1_cnt = 2; | |
947 v = 128; | |
948 break; | |
949 default: | |
950 *qmant1_ptr += v; | |
951 mant1_cnt = 0; | |
952 v = 128; | |
953 break; | |
954 } | |
955 break; | |
956 case 2: | |
957 v = sym_quant(c, e, 5); | |
958 switch(mant2_cnt) { | |
959 case 0: | |
960 qmant2_ptr = &qmant[ch][i]; | |
961 v = 25 * v; | |
962 mant2_cnt = 1; | |
963 break; | |
964 case 1: | |
965 *qmant2_ptr += 5 * v; | |
966 mant2_cnt = 2; | |
967 v = 128; | |
968 break; | |
969 default: | |
970 *qmant2_ptr += v; | |
971 mant2_cnt = 0; | |
972 v = 128; | |
973 break; | |
974 } | |
975 break; | |
976 case 3: | |
977 v = sym_quant(c, e, 7); | |
978 break; | |
979 case 4: | |
980 v = sym_quant(c, e, 11); | |
981 switch(mant4_cnt) { | |
982 case 0: | |
983 qmant4_ptr = &qmant[ch][i]; | |
984 v = 11 * v; | |
985 mant4_cnt = 1; | |
986 break; | |
987 default: | |
988 *qmant4_ptr += v; | |
989 mant4_cnt = 0; | |
990 v = 128; | |
991 break; | |
992 } | |
993 break; | |
994 case 5: | |
995 v = sym_quant(c, e, 15); | |
996 break; | |
997 case 14: | |
998 v = asym_quant(c, e, 14); | |
999 break; | |
1000 case 15: | |
1001 v = asym_quant(c, e, 16); | |
1002 break; | |
1003 default: | |
1004 v = asym_quant(c, e, b - 1); | |
1005 break; | |
1006 } | |
1007 qmant[ch][i] = v; | |
1008 } | |
1009 } | |
1010 | |
1011 /* second pass : output the values */ | |
314 | 1012 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 1013 int b, q; |
2967 | 1014 |
0 | 1015 for(i=0;i<s->nb_coefs[ch];i++) { |
1016 q = qmant[ch][i]; | |
1017 b = bap[ch][i]; | |
1018 switch(b) { | |
1019 case 0: | |
1020 break; | |
1021 case 1: | |
2967 | 1022 if (q != 128) |
0 | 1023 put_bits(&s->pb, 5, q); |
1024 break; | |
1025 case 2: | |
2967 | 1026 if (q != 128) |
0 | 1027 put_bits(&s->pb, 7, q); |
1028 break; | |
1029 case 3: | |
1030 put_bits(&s->pb, 3, q); | |
1031 break; | |
1032 case 4: | |
1033 if (q != 128) | |
1034 put_bits(&s->pb, 7, q); | |
1035 break; | |
1036 case 14: | |
1037 put_bits(&s->pb, 14, q); | |
1038 break; | |
1039 case 15: | |
1040 put_bits(&s->pb, 16, q); | |
1041 break; | |
1042 default: | |
1043 put_bits(&s->pb, b - 1, q); | |
1044 break; | |
1045 } | |
1046 } | |
1047 } | |
1048 } | |
1049 | |
1050 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) | |
1051 | |
1052 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly) | |
1053 { | |
1054 unsigned int c; | |
1055 | |
1056 c = 0; | |
1057 while (a) { | |
1058 if (a & 1) | |
1059 c ^= b; | |
1060 a = a >> 1; | |
1061 b = b << 1; | |
1062 if (b & (1 << 16)) | |
1063 b ^= poly; | |
1064 } | |
1065 return c; | |
1066 } | |
1067 | |
1068 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly) | |
1069 { | |
1070 unsigned int r; | |
1071 r = 1; | |
1072 while (n) { | |
1073 if (n & 1) | |
1074 r = mul_poly(r, a, poly); | |
1075 a = mul_poly(a, a, poly); | |
1076 n >>= 1; | |
1077 } | |
1078 return r; | |
1079 } | |
1080 | |
1081 | |
1082 /* compute log2(max(abs(tab[]))) */ | |
1064 | 1083 static int log2_tab(int16_t *tab, int n) |
0 | 1084 { |
1085 int i, v; | |
1086 | |
1087 v = 0; | |
1088 for(i=0;i<n;i++) { | |
1089 v |= abs(tab[i]); | |
1090 } | |
65 | 1091 return av_log2(v); |
0 | 1092 } |
1093 | |
1064 | 1094 static void lshift_tab(int16_t *tab, int n, int lshift) |
0 | 1095 { |
1096 int i; | |
1097 | |
1098 if (lshift > 0) { | |
1099 for(i=0;i<n;i++) { | |
1100 tab[i] <<= lshift; | |
1101 } | |
1102 } else if (lshift < 0) { | |
1103 lshift = -lshift; | |
1104 for(i=0;i<n;i++) { | |
1105 tab[i] >>= lshift; | |
1106 } | |
1107 } | |
1108 } | |
1109 | |
1110 /* fill the end of the frame and compute the two crcs */ | |
1111 static int output_frame_end(AC3EncodeContext *s) | |
1112 { | |
1113 int frame_size, frame_size_58, n, crc1, crc2, crc_inv; | |
1064 | 1114 uint8_t *frame; |
0 | 1115 |
1116 frame_size = s->frame_size; /* frame size in words */ | |
1117 /* align to 8 bits */ | |
1118 flush_put_bits(&s->pb); | |
1119 /* add zero bytes to reach the frame size */ | |
1120 frame = s->pb.buf; | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
87
diff
changeset
|
1121 n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2; |
0 | 1122 assert(n >= 0); |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
1123 if(n>0) |
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
1124 memset(pbBufPtr(&s->pb), 0, n); |
2967 | 1125 |
0 | 1126 /* Now we must compute both crcs : this is not so easy for crc1 |
1127 because it is at the beginning of the data... */ | |
1128 frame_size_58 = (frame_size >> 1) + (frame_size >> 3); | |
6108 | 1129 crc1 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
1130 frame + 4, 2 * frame_size_58 - 4)); | |
0 | 1131 /* XXX: could precompute crc_inv */ |
1132 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY); | |
1133 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); | |
5089 | 1134 AV_WB16(frame+2,crc1); |
2967 | 1135 |
6108 | 1136 crc2 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
1137 frame + 2 * frame_size_58, | |
1138 (frame_size - frame_size_58) * 2 - 2)); | |
5089 | 1139 AV_WB16(frame+2*frame_size-2,crc2); |
0 | 1140 |
1141 // printf("n=%d frame_size=%d\n", n, frame_size); | |
1142 return frame_size * 2; | |
1143 } | |
1144 | |
782 | 1145 static int AC3_encode_frame(AVCodecContext *avctx, |
1146 unsigned char *frame, int buf_size, void *data) | |
0 | 1147 { |
1148 AC3EncodeContext *s = avctx->priv_data; | |
2367
c353719836af
fix some type mismatches patch by (Jeff Muizelaar <muizelaar rogers com>)
michael
parents:
2157
diff
changeset
|
1149 int16_t *samples = data; |
0 | 1150 int i, j, k, v, ch; |
1064 | 1151 int16_t input_samples[N]; |
1152 int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1153 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1154 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]; | |
1155 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1156 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1157 int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS]; | |
0 | 1158 int frame_bits; |
1159 | |
1160 frame_bits = 0; | |
314 | 1161 for(ch=0;ch<s->nb_all_channels;ch++) { |
0 | 1162 /* fixed mdct to the six sub blocks & exponent computation */ |
1163 for(i=0;i<NB_BLOCKS;i++) { | |
1064 | 1164 int16_t *sptr; |
0 | 1165 int sinc; |
1166 | |
1167 /* compute input samples */ | |
1064 | 1168 memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t)); |
314 | 1169 sinc = s->nb_all_channels; |
0 | 1170 sptr = samples + (sinc * (N/2) * i) + ch; |
1171 for(j=0;j<N/2;j++) { | |
1172 v = *sptr; | |
1173 input_samples[j + N/2] = v; | |
2967 | 1174 s->last_samples[ch][j] = v; |
0 | 1175 sptr += sinc; |
1176 } | |
1177 | |
1178 /* apply the MDCT window */ | |
1179 for(j=0;j<N/2;j++) { | |
2967 | 1180 input_samples[j] = MUL16(input_samples[j], |
4643
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
1181 ff_ac3_window[j]) >> 15; |
2967 | 1182 input_samples[N-j-1] = MUL16(input_samples[N-j-1], |
4643
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
1183 ff_ac3_window[j]) >> 15; |
0 | 1184 } |
2967 | 1185 |
0 | 1186 /* Normalize the samples to use the maximum available |
1187 precision */ | |
1188 v = 14 - log2_tab(input_samples, N); | |
1189 if (v < 0) | |
1190 v = 0; | |
4686
3a891d2379ce
AC-3 encoded volume is too high. Revert revision 7160:
jbr
parents:
4645
diff
changeset
|
1191 exp_samples[i][ch] = v - 9; |
0 | 1192 lshift_tab(input_samples, N, v); |
1193 | |
1194 /* do the MDCT */ | |
1195 mdct512(mdct_coef[i][ch], input_samples); | |
2967 | 1196 |
0 | 1197 /* compute "exponents". We take into account the |
1198 normalization there */ | |
1199 for(j=0;j<N/2;j++) { | |
1200 int e; | |
1201 v = abs(mdct_coef[i][ch][j]); | |
1202 if (v == 0) | |
1203 e = 24; | |
1204 else { | |
65 | 1205 e = 23 - av_log2(v) + exp_samples[i][ch]; |
0 | 1206 if (e >= 24) { |
1207 e = 24; | |
1208 mdct_coef[i][ch][j] = 0; | |
1209 } | |
1210 } | |
1211 exp[i][ch][j] = e; | |
1212 } | |
1213 } | |
2967 | 1214 |
314 | 1215 compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel); |
0 | 1216 |
1217 /* compute the exponents as the decoder will see them. The | |
1218 EXP_REUSE case must be handled carefully : we select the | |
1219 min of the exponents */ | |
1220 i = 0; | |
1221 while (i < NB_BLOCKS) { | |
1222 j = i + 1; | |
1223 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) { | |
1224 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]); | |
1225 j++; | |
1226 } | |
1227 frame_bits += encode_exp(encoded_exp[i][ch], | |
2967 | 1228 exp[i][ch], s->nb_coefs[ch], |
0 | 1229 exp_strategy[i][ch]); |
1230 /* copy encoded exponents for reuse case */ | |
1231 for(k=i+1;k<j;k++) { | |
2967 | 1232 memcpy(encoded_exp[k][ch], encoded_exp[i][ch], |
1064 | 1233 s->nb_coefs[ch] * sizeof(uint8_t)); |
0 | 1234 } |
1235 i = j; | |
1236 } | |
1237 } | |
1238 | |
3246 | 1239 /* adjust for fractional frame sizes */ |
6082 | 1240 while(s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) { |
1241 s->bits_written -= s->bit_rate; | |
3246 | 1242 s->samples_written -= s->sample_rate; |
1243 } | |
6082 | 1244 s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate); |
3246 | 1245 s->bits_written += s->frame_size * 16; |
1246 s->samples_written += AC3_FRAME_SIZE; | |
1247 | |
0 | 1248 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits); |
1249 /* everything is known... let's output the frame */ | |
1250 output_frame_header(s, frame); | |
2967 | 1251 |
0 | 1252 for(i=0;i<NB_BLOCKS;i++) { |
2967 | 1253 output_audio_block(s, exp_strategy[i], encoded_exp[i], |
0 | 1254 bap[i], mdct_coef[i], exp_samples[i], i); |
1255 } | |
1256 return output_frame_end(s); | |
1257 } | |
1258 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6516
diff
changeset
|
1259 static av_cold int AC3_encode_close(AVCodecContext *avctx) |
925 | 1260 { |
1261 av_freep(&avctx->coded_frame); | |
1014
48349e11c9b2
C99 initializers and kill warnings patch by (mru at users dot sourceforge dot net (Mns Rullgrd))
michaelni
parents:
925
diff
changeset
|
1262 return 0; |
925 | 1263 } |
1264 | |
0 | 1265 #if 0 |
1266 /*************************************************************************/ | |
1267 /* TEST */ | |
1268 | |
5380
389366aa3458
Fix the self tests which are contained in some codecs and are using random().
takis
parents:
5331
diff
changeset
|
1269 #undef random |
0 | 1270 #define FN (N/4) |
1271 | |
1272 void fft_test(void) | |
1273 { | |
1274 IComplex in[FN], in1[FN]; | |
1275 int k, n, i; | |
1276 float sum_re, sum_im, a; | |
1277 | |
1278 /* FFT test */ | |
1279 | |
1280 for(i=0;i<FN;i++) { | |
1281 in[i].re = random() % 65535 - 32767; | |
1282 in[i].im = random() % 65535 - 32767; | |
1283 in1[i] = in[i]; | |
1284 } | |
1285 fft(in, 7); | |
1286 | |
1287 /* do it by hand */ | |
1288 for(k=0;k<FN;k++) { | |
1289 sum_re = 0; | |
1290 sum_im = 0; | |
1291 for(n=0;n<FN;n++) { | |
1292 a = -2 * M_PI * (n * k) / FN; | |
1293 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a); | |
1294 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a); | |
1295 } | |
2967 | 1296 printf("%3d: %6d,%6d %6.0f,%6.0f\n", |
1297 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN); | |
0 | 1298 } |
1299 } | |
1300 | |
1301 void mdct_test(void) | |
1302 { | |
1064 | 1303 int16_t input[N]; |
1304 int32_t output[N/2]; | |
0 | 1305 float input1[N]; |
1306 float output1[N/2]; | |
1307 float s, a, err, e, emax; | |
1308 int i, k, n; | |
1309 | |
1310 for(i=0;i<N;i++) { | |
1311 input[i] = (random() % 65535 - 32767) * 9 / 10; | |
1312 input1[i] = input[i]; | |
1313 } | |
1314 | |
1315 mdct512(output, input); | |
2967 | 1316 |
0 | 1317 /* do it by hand */ |
1318 for(k=0;k<N/2;k++) { | |
1319 s = 0; | |
1320 for(n=0;n<N;n++) { | |
1321 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N)); | |
1322 s += input1[n] * cos(a); | |
1323 } | |
1324 output1[k] = -2 * s / N; | |
1325 } | |
2967 | 1326 |
0 | 1327 err = 0; |
1328 emax = 0; | |
1329 for(i=0;i<N/2;i++) { | |
1330 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]); | |
1331 e = output[i] - output1[i]; | |
1332 if (e > emax) | |
1333 emax = e; | |
1334 err += e * e; | |
1335 } | |
1336 printf("err2=%f emax=%f\n", err / (N/2), emax); | |
1337 } | |
1338 | |
1339 void test_ac3(void) | |
1340 { | |
1341 AC3EncodeContext ctx; | |
1342 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE]; | |
1343 short samples[AC3_FRAME_SIZE]; | |
1344 int ret, i; | |
2967 | 1345 |
0 | 1346 AC3_encode_init(&ctx, 44100, 64000, 1); |
1347 | |
1348 fft_test(); | |
1349 mdct_test(); | |
1350 | |
1351 for(i=0;i<AC3_FRAME_SIZE;i++) | |
1352 samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000); | |
1353 ret = AC3_encode_frame(&ctx, frame, samples); | |
1354 printf("ret=%d\n", ret); | |
1355 } | |
1356 #endif | |
1357 | |
1358 AVCodec ac3_encoder = { | |
1359 "ac3", | |
1360 CODEC_TYPE_AUDIO, | |
1361 CODEC_ID_AC3, | |
1362 sizeof(AC3EncodeContext), | |
1363 AC3_encode_init, | |
1364 AC3_encode_frame, | |
925 | 1365 AC3_encode_close, |
0 | 1366 NULL, |
7451
85ab7655ad4d
Modify all codecs to report their supported input and output sample format(s).
pross
parents:
7129
diff
changeset
|
1367 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, |
7769 | 1368 .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"), |
0 | 1369 }; |