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