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