Mercurial > libavcodec.hg
annotate ac3enc.c @ 1164:0e3c0c4a7b3d libavcodec
msmpeg4 2pass support & some related cleanup
author | michaelni |
---|---|
date | Wed, 02 Apr 2003 09:57:34 +0000 |
parents | 1e39f273ecd6 |
children | 4d67eb341a0c |
rev | line source |
---|---|
0 | 1 /* |
2 * The simplest AC3 encoder | |
429 | 3 * Copyright (c) 2000 Fabrice Bellard. |
0 | 4 * |
429 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
0 | 18 */ |
1106 | 19 |
20 /** | |
21 * @file ac3enc.c | |
22 * The simplest AC3 encoder. | |
23 */ | |
64 | 24 //#define DEBUG |
25 //#define DEBUG_BITALLOC | |
26 #include "avcodec.h" | |
0 | 27 |
782 | 28 #include "ac3.h" |
29 | |
30 typedef struct AC3EncodeContext { | |
31 PutBitContext pb; | |
32 int nb_channels; | |
33 int nb_all_channels; | |
34 int lfe_channel; | |
35 int bit_rate; | |
1057 | 36 unsigned int sample_rate; |
37 unsigned int bsid; | |
38 unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */ | |
39 unsigned int frame_size; /* current frame size in words */ | |
782 | 40 int halfratecod; |
1057 | 41 unsigned int frmsizecod; |
42 unsigned int fscod; /* frequency */ | |
43 unsigned int acmod; | |
782 | 44 int lfe; |
1057 | 45 unsigned int bsmod; |
782 | 46 short last_samples[AC3_MAX_CHANNELS][256]; |
1057 | 47 unsigned int chbwcod[AC3_MAX_CHANNELS]; |
782 | 48 int nb_coefs[AC3_MAX_CHANNELS]; |
49 | |
50 /* bitrate allocation control */ | |
51 int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod; | |
52 AC3BitAllocParameters bit_alloc; | |
53 int csnroffst; | |
54 int fgaincod[AC3_MAX_CHANNELS]; | |
55 int fsnroffst[AC3_MAX_CHANNELS]; | |
56 /* mantissa encoding */ | |
57 int mant1_cnt, mant2_cnt, mant4_cnt; | |
58 } AC3EncodeContext; | |
59 | |
0 | 60 #include "ac3tab.h" |
61 | |
62 #define MDCT_NBITS 9 | |
63 #define N (1 << MDCT_NBITS) | |
64 | |
65 /* new exponents are sent if their Norm 1 exceed this number */ | |
66 #define EXP_DIFF_THRESHOLD 1000 | |
67 | |
68 static void fft_init(int ln); | |
69 static void ac3_crc_init(void); | |
70 | |
1064 | 71 static inline int16_t fix15(float a) |
0 | 72 { |
73 int v; | |
74 v = (int)(a * (float)(1 << 15)); | |
75 if (v < -32767) | |
76 v = -32767; | |
77 else if (v > 32767) | |
78 v = 32767; | |
79 return v; | |
80 } | |
81 | |
82 static inline int calc_lowcomp1(int a, int b0, int b1) | |
83 { | |
84 if ((b0 + 256) == b1) { | |
85 a = 384 ; | |
86 } else if (b0 > b1) { | |
87 a = a - 64; | |
88 if (a < 0) a=0; | |
89 } | |
90 return a; | |
91 } | |
92 | |
93 static inline int calc_lowcomp(int a, int b0, int b1, int bin) | |
94 { | |
95 if (bin < 7) { | |
96 if ((b0 + 256) == b1) { | |
97 a = 384 ; | |
98 } else if (b0 > b1) { | |
99 a = a - 64; | |
100 if (a < 0) a=0; | |
101 } | |
102 } else if (bin < 20) { | |
103 if ((b0 + 256) == b1) { | |
104 a = 320 ; | |
105 } else if (b0 > b1) { | |
106 a= a - 64; | |
107 if (a < 0) a=0; | |
108 } | |
109 } else { | |
110 a = a - 128; | |
111 if (a < 0) a=0; | |
112 } | |
113 return a; | |
114 } | |
115 | |
116 /* AC3 bit allocation. The algorithm is the one described in the AC3 | |
782 | 117 spec. */ |
1064 | 118 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap, |
119 int8_t *exp, int start, int end, | |
782 | 120 int snroffset, int fgain, int is_lfe, |
121 int deltbae,int deltnseg, | |
1064 | 122 uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba) |
0 | 123 { |
124 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin; | |
125 int fastleak,slowleak,address,tmp; | |
1064 | 126 int16_t psd[256]; /* scaled exponents */ |
127 int16_t bndpsd[50]; /* interpolated exponents */ | |
128 int16_t excite[50]; /* excitation */ | |
129 int16_t mask[50]; /* masking value */ | |
0 | 130 |
131 /* exponent mapping to PSD */ | |
132 for(bin=start;bin<end;bin++) { | |
133 psd[bin]=(3072 - (exp[bin] << 7)); | |
134 } | |
135 | |
136 /* PSD integration */ | |
137 j=start; | |
138 k=masktab[start]; | |
139 do { | |
140 v=psd[j]; | |
141 j++; | |
142 end1=bndtab[k+1]; | |
143 if (end1 > end) end1=end; | |
144 for(i=j;i<end1;i++) { | |
145 int c,adr; | |
146 /* logadd */ | |
147 v1=psd[j]; | |
148 c=v-v1; | |
149 if (c >= 0) { | |
150 adr=c >> 1; | |
151 if (adr > 255) adr=255; | |
152 v=v + latab[adr]; | |
153 } else { | |
154 adr=(-c) >> 1; | |
155 if (adr > 255) adr=255; | |
156 v=v1 + latab[adr]; | |
157 } | |
158 j++; | |
159 } | |
160 bndpsd[k]=v; | |
161 k++; | |
162 } while (end > bndtab[k]); | |
163 | |
164 /* excitation function */ | |
165 bndstrt = masktab[start]; | |
166 bndend = masktab[end-1] + 1; | |
167 | |
782 | 168 if (bndstrt == 0) { |
169 lowcomp = 0; | |
170 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ; | |
171 excite[0] = bndpsd[0] - fgain - lowcomp ; | |
172 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ; | |
173 excite[1] = bndpsd[1] - fgain - lowcomp ; | |
174 begin = 7 ; | |
175 for (bin = 2; bin < 7; bin++) { | |
176 if (!(is_lfe && bin == 6)) | |
177 lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ; | |
178 fastleak = bndpsd[bin] - fgain ; | |
179 slowleak = bndpsd[bin] - s->sgain ; | |
180 excite[bin] = fastleak - lowcomp ; | |
181 if (!(is_lfe && bin == 6)) { | |
182 if (bndpsd[bin] <= bndpsd[bin+1]) { | |
183 begin = bin + 1 ; | |
184 break ; | |
185 } | |
186 } | |
187 } | |
188 | |
189 end1=bndend; | |
190 if (end1 > 22) end1=22; | |
0 | 191 |
782 | 192 for (bin = begin; bin < end1; bin++) { |
193 if (!(is_lfe && bin == 6)) | |
194 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ; | |
195 | |
196 fastleak -= s->fdecay ; | |
197 v = bndpsd[bin] - fgain; | |
198 if (fastleak < v) fastleak = v; | |
199 | |
200 slowleak -= s->sdecay ; | |
201 v = bndpsd[bin] - s->sgain; | |
202 if (slowleak < v) slowleak = v; | |
0 | 203 |
782 | 204 v=fastleak - lowcomp; |
205 if (slowleak > v) v=slowleak; | |
0 | 206 |
782 | 207 excite[bin] = v; |
208 } | |
209 begin = 22; | |
210 } else { | |
211 /* coupling channel */ | |
212 begin = bndstrt; | |
0 | 213 |
782 | 214 fastleak = (s->cplfleak << 8) + 768; |
215 slowleak = (s->cplsleak << 8) + 768; | |
0 | 216 } |
217 | |
782 | 218 for (bin = begin; bin < bndend; bin++) { |
0 | 219 fastleak -= s->fdecay ; |
220 v = bndpsd[bin] - fgain; | |
221 if (fastleak < v) fastleak = v; | |
222 slowleak -= s->sdecay ; | |
223 v = bndpsd[bin] - s->sgain; | |
224 if (slowleak < v) slowleak = v; | |
225 | |
226 v=fastleak; | |
227 if (slowleak > v) v = slowleak; | |
228 excite[bin] = v; | |
229 } | |
230 | |
231 /* compute masking curve */ | |
232 | |
233 for (bin = bndstrt; bin < bndend; bin++) { | |
234 v1 = excite[bin]; | |
235 tmp = s->dbknee - bndpsd[bin]; | |
236 if (tmp > 0) { | |
237 v1 += tmp >> 2; | |
238 } | |
239 v=hth[bin >> s->halfratecod][s->fscod]; | |
240 if (v1 > v) v=v1; | |
241 mask[bin] = v; | |
242 } | |
243 | |
782 | 244 /* delta bit allocation */ |
245 | |
246 if (deltbae == 0 || deltbae == 1) { | |
247 int band, seg, delta; | |
248 band = 0 ; | |
249 for (seg = 0; seg < deltnseg; seg++) { | |
250 band += deltoffst[seg] ; | |
251 if (deltba[seg] >= 4) { | |
252 delta = (deltba[seg] - 3) << 7; | |
253 } else { | |
254 delta = (deltba[seg] - 4) << 7; | |
255 } | |
256 for (k = 0; k < deltlen[seg]; k++) { | |
257 mask[band] += delta ; | |
258 band++ ; | |
259 } | |
260 } | |
261 } | |
262 | |
0 | 263 /* compute bit allocation */ |
264 | |
265 i = start ; | |
266 j = masktab[start] ; | |
267 do { | |
268 v=mask[j]; | |
269 v -= snroffset ; | |
270 v -= s->floor ; | |
271 if (v < 0) v = 0; | |
272 v &= 0x1fe0 ; | |
273 v += s->floor ; | |
274 | |
275 end1=bndtab[j] + bndsz[j]; | |
276 if (end1 > end) end1=end; | |
277 | |
278 for (k = i; k < end1; k++) { | |
279 address = (psd[i] - v) >> 5 ; | |
280 if (address < 0) address=0; | |
281 else if (address > 63) address=63; | |
282 bap[i] = baptab[address]; | |
283 i++; | |
284 } | |
285 } while (end > bndtab[j++]) ; | |
286 } | |
287 | |
288 typedef struct IComplex { | |
289 short re,im; | |
290 } IComplex; | |
291 | |
292 static void fft_init(int ln) | |
293 { | |
294 int i, j, m, n; | |
295 float alpha; | |
296 | |
297 n = 1 << ln; | |
298 | |
299 for(i=0;i<(n/2);i++) { | |
300 alpha = 2 * M_PI * (float)i / (float)n; | |
301 costab[i] = fix15(cos(alpha)); | |
302 sintab[i] = fix15(sin(alpha)); | |
303 } | |
304 | |
305 for(i=0;i<n;i++) { | |
306 m=0; | |
307 for(j=0;j<ln;j++) { | |
308 m |= ((i >> j) & 1) << (ln-j-1); | |
309 } | |
310 fft_rev[i]=m; | |
311 } | |
312 } | |
313 | |
314 /* butter fly op */ | |
315 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \ | |
316 {\ | |
317 int ax, ay, bx, by;\ | |
318 bx=pre1;\ | |
319 by=pim1;\ | |
320 ax=qre1;\ | |
321 ay=qim1;\ | |
322 pre = (bx + ax) >> 1;\ | |
323 pim = (by + ay) >> 1;\ | |
324 qre = (bx - ax) >> 1;\ | |
325 qim = (by - ay) >> 1;\ | |
326 } | |
327 | |
328 #define MUL16(a,b) ((a) * (b)) | |
329 | |
330 #define CMUL(pre, pim, are, aim, bre, bim) \ | |
331 {\ | |
332 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\ | |
333 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\ | |
334 } | |
335 | |
336 | |
337 /* do a 2^n point complex fft on 2^ln points. */ | |
338 static void fft(IComplex *z, int ln) | |
339 { | |
340 int j, l, np, np2; | |
341 int nblocks, nloops; | |
342 register IComplex *p,*q; | |
343 int tmp_re, tmp_im; | |
344 | |
345 np = 1 << ln; | |
346 | |
347 /* reverse */ | |
348 for(j=0;j<np;j++) { | |
349 int k; | |
350 IComplex tmp; | |
351 k = fft_rev[j]; | |
352 if (k < j) { | |
353 tmp = z[k]; | |
354 z[k] = z[j]; | |
355 z[j] = tmp; | |
356 } | |
357 } | |
358 | |
359 /* pass 0 */ | |
360 | |
361 p=&z[0]; | |
362 j=(np >> 1); | |
363 do { | |
364 BF(p[0].re, p[0].im, p[1].re, p[1].im, | |
365 p[0].re, p[0].im, p[1].re, p[1].im); | |
366 p+=2; | |
367 } while (--j != 0); | |
368 | |
369 /* pass 1 */ | |
370 | |
371 p=&z[0]; | |
372 j=np >> 2; | |
373 do { | |
374 BF(p[0].re, p[0].im, p[2].re, p[2].im, | |
375 p[0].re, p[0].im, p[2].re, p[2].im); | |
376 BF(p[1].re, p[1].im, p[3].re, p[3].im, | |
377 p[1].re, p[1].im, p[3].im, -p[3].re); | |
378 p+=4; | |
379 } while (--j != 0); | |
380 | |
381 /* pass 2 .. ln-1 */ | |
382 | |
383 nblocks = np >> 3; | |
384 nloops = 1 << 2; | |
385 np2 = np >> 1; | |
386 do { | |
387 p = z; | |
388 q = z + nloops; | |
389 for (j = 0; j < nblocks; ++j) { | |
390 | |
391 BF(p->re, p->im, q->re, q->im, | |
392 p->re, p->im, q->re, q->im); | |
393 | |
394 p++; | |
395 q++; | |
396 for(l = nblocks; l < np2; l += nblocks) { | |
397 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im); | |
398 BF(p->re, p->im, q->re, q->im, | |
399 p->re, p->im, tmp_re, tmp_im); | |
400 p++; | |
401 q++; | |
402 } | |
403 p += nloops; | |
404 q += nloops; | |
405 } | |
406 nblocks = nblocks >> 1; | |
407 nloops = nloops << 1; | |
408 } while (nblocks != 0); | |
409 } | |
410 | |
411 /* do a 512 point mdct */ | |
1064 | 412 static void mdct512(int32_t *out, int16_t *in) |
0 | 413 { |
414 int i, re, im, re1, im1; | |
1064 | 415 int16_t rot[N]; |
0 | 416 IComplex x[N/4]; |
417 | |
418 /* shift to simplify computations */ | |
419 for(i=0;i<N/4;i++) | |
420 rot[i] = -in[i + 3*N/4]; | |
421 for(i=N/4;i<N;i++) | |
422 rot[i] = in[i - N/4]; | |
423 | |
424 /* pre rotation */ | |
425 for(i=0;i<N/4;i++) { | |
426 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1; | |
427 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1; | |
428 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]); | |
429 } | |
430 | |
431 fft(x, MDCT_NBITS - 2); | |
432 | |
433 /* post rotation */ | |
434 for(i=0;i<N/4;i++) { | |
435 re = x[i].re; | |
436 im = x[i].im; | |
437 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]); | |
438 out[2*i] = im1; | |
439 out[N/2-1-2*i] = re1; | |
440 } | |
441 } | |
442 | |
443 /* XXX: use another norm ? */ | |
1064 | 444 static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n) |
0 | 445 { |
446 int sum, i; | |
447 sum = 0; | |
448 for(i=0;i<n;i++) { | |
449 sum += abs(exp1[i] - exp2[i]); | |
450 } | |
451 return sum; | |
452 } | |
453 | |
1064 | 454 static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
455 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
314 | 456 int ch, int is_lfe) |
0 | 457 { |
458 int i, j; | |
459 int exp_diff; | |
460 | |
461 /* estimate if the exponent variation & decide if they should be | |
462 reused in the next frame */ | |
463 exp_strategy[0][ch] = EXP_NEW; | |
464 for(i=1;i<NB_BLOCKS;i++) { | |
465 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2); | |
466 #ifdef DEBUG | |
467 printf("exp_diff=%d\n", exp_diff); | |
468 #endif | |
469 if (exp_diff > EXP_DIFF_THRESHOLD) | |
470 exp_strategy[i][ch] = EXP_NEW; | |
471 else | |
472 exp_strategy[i][ch] = EXP_REUSE; | |
473 } | |
314 | 474 if (is_lfe) |
475 return; | |
476 | |
0 | 477 /* now select the encoding strategy type : if exponents are often |
478 recoded, we use a coarse encoding */ | |
479 i = 0; | |
480 while (i < NB_BLOCKS) { | |
481 j = i + 1; | |
482 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) | |
483 j++; | |
484 switch(j - i) { | |
485 case 1: | |
486 exp_strategy[i][ch] = EXP_D45; | |
487 break; | |
488 case 2: | |
489 case 3: | |
490 exp_strategy[i][ch] = EXP_D25; | |
491 break; | |
492 default: | |
493 exp_strategy[i][ch] = EXP_D15; | |
494 break; | |
495 } | |
314 | 496 i = j; |
0 | 497 } |
498 } | |
499 | |
500 /* set exp[i] to min(exp[i], exp1[i]) */ | |
1064 | 501 static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n) |
0 | 502 { |
503 int i; | |
504 | |
505 for(i=0;i<n;i++) { | |
506 if (exp1[i] < exp[i]) | |
507 exp[i] = exp1[i]; | |
508 } | |
509 } | |
510 | |
511 /* update the exponents so that they are the ones the decoder will | |
512 decode. Return the number of bits used to code the exponents */ | |
1064 | 513 static int encode_exp(uint8_t encoded_exp[N/2], |
514 uint8_t exp[N/2], | |
0 | 515 int nb_exps, |
516 int exp_strategy) | |
517 { | |
518 int group_size, nb_groups, i, j, k, recurse, exp_min, delta; | |
1064 | 519 uint8_t exp1[N/2]; |
0 | 520 |
521 switch(exp_strategy) { | |
522 case EXP_D15: | |
523 group_size = 1; | |
524 break; | |
525 case EXP_D25: | |
526 group_size = 2; | |
527 break; | |
528 default: | |
529 case EXP_D45: | |
530 group_size = 4; | |
531 break; | |
532 } | |
533 nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3; | |
534 | |
535 /* for each group, compute the minimum exponent */ | |
536 exp1[0] = exp[0]; /* DC exponent is handled separately */ | |
537 k = 1; | |
538 for(i=1;i<=nb_groups;i++) { | |
539 exp_min = exp[k]; | |
540 assert(exp_min >= 0 && exp_min <= 24); | |
541 for(j=1;j<group_size;j++) { | |
542 if (exp[k+j] < exp_min) | |
543 exp_min = exp[k+j]; | |
544 } | |
545 exp1[i] = exp_min; | |
546 k += group_size; | |
547 } | |
548 | |
549 /* constraint for DC exponent */ | |
550 if (exp1[0] > 15) | |
551 exp1[0] = 15; | |
552 | |
553 /* Iterate until the delta constraints between each groups are | |
554 satisfyed. I'm sure it is possible to find a better algorithm, | |
555 but I am lazy */ | |
556 do { | |
557 recurse = 0; | |
558 for(i=1;i<=nb_groups;i++) { | |
559 delta = exp1[i] - exp1[i-1]; | |
560 if (delta > 2) { | |
561 /* if delta too big, we encode a smaller exponent */ | |
562 exp1[i] = exp1[i-1] + 2; | |
563 } else if (delta < -2) { | |
564 /* if delta is too small, we must decrease the previous | |
565 exponent, which means we must recurse */ | |
566 recurse = 1; | |
567 exp1[i-1] = exp1[i] + 2; | |
568 } | |
569 } | |
570 } while (recurse); | |
571 | |
572 /* now we have the exponent values the decoder will see */ | |
573 encoded_exp[0] = exp1[0]; | |
574 k = 1; | |
575 for(i=1;i<=nb_groups;i++) { | |
576 for(j=0;j<group_size;j++) { | |
577 encoded_exp[k+j] = exp1[i]; | |
578 } | |
579 k += group_size; | |
580 } | |
581 | |
582 #if defined(DEBUG) | |
583 printf("exponents: strategy=%d\n", exp_strategy); | |
584 for(i=0;i<=nb_groups * group_size;i++) { | |
585 printf("%d ", encoded_exp[i]); | |
586 } | |
587 printf("\n"); | |
588 #endif | |
589 | |
590 return 4 + (nb_groups / 3) * 7; | |
591 } | |
592 | |
593 /* return the size in bits taken by the mantissa */ | |
1064 | 594 static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
0 | 595 { |
596 int bits, mant, i; | |
597 | |
598 bits = 0; | |
599 for(i=0;i<nb_coefs;i++) { | |
600 mant = m[i]; | |
601 switch(mant) { | |
602 case 0: | |
603 /* nothing */ | |
604 break; | |
605 case 1: | |
606 /* 3 mantissa in 5 bits */ | |
607 if (s->mant1_cnt == 0) | |
608 bits += 5; | |
609 if (++s->mant1_cnt == 3) | |
610 s->mant1_cnt = 0; | |
611 break; | |
612 case 2: | |
613 /* 3 mantissa in 7 bits */ | |
614 if (s->mant2_cnt == 0) | |
615 bits += 7; | |
616 if (++s->mant2_cnt == 3) | |
617 s->mant2_cnt = 0; | |
618 break; | |
619 case 3: | |
620 bits += 3; | |
621 break; | |
622 case 4: | |
623 /* 2 mantissa in 7 bits */ | |
624 if (s->mant4_cnt == 0) | |
625 bits += 7; | |
626 if (++s->mant4_cnt == 2) | |
627 s->mant4_cnt = 0; | |
628 break; | |
629 case 14: | |
630 bits += 14; | |
631 break; | |
632 case 15: | |
633 bits += 16; | |
634 break; | |
635 default: | |
636 bits += mant - 1; | |
637 break; | |
638 } | |
639 } | |
640 return bits; | |
641 } | |
642 | |
643 | |
644 static int bit_alloc(AC3EncodeContext *s, | |
1064 | 645 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
646 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
647 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |
0 | 648 int frame_bits, int csnroffst, int fsnroffst) |
649 { | |
650 int i, ch; | |
651 | |
652 /* compute size */ | |
653 for(i=0;i<NB_BLOCKS;i++) { | |
654 s->mant1_cnt = 0; | |
655 s->mant2_cnt = 0; | |
656 s->mant4_cnt = 0; | |
314 | 657 for(ch=0;ch<s->nb_all_channels;ch++) { |
782 | 658 ac3_parametric_bit_allocation(&s->bit_alloc, |
1064 | 659 bap[i][ch], (int8_t *)encoded_exp[i][ch], |
782 | 660 0, s->nb_coefs[ch], |
661 (((csnroffst-15) << 4) + | |
662 fsnroffst) << 2, | |
663 fgaintab[s->fgaincod[ch]], | |
664 ch == s->lfe_channel, | |
665 2, 0, NULL, NULL, NULL); | |
0 | 666 frame_bits += compute_mantissa_size(s, bap[i][ch], |
667 s->nb_coefs[ch]); | |
668 } | |
669 } | |
670 #if 0 | |
671 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n", | |
672 csnroffst, fsnroffst, frame_bits, | |
673 16 * s->frame_size - ((frame_bits + 7) & ~7)); | |
674 #endif | |
675 return 16 * s->frame_size - frame_bits; | |
676 } | |
677 | |
678 #define SNR_INC1 4 | |
679 | |
680 static int compute_bit_allocation(AC3EncodeContext *s, | |
1064 | 681 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
682 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
683 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |
0 | 684 int frame_bits) |
685 { | |
686 int i, ch; | |
687 int csnroffst, fsnroffst; | |
1064 | 688 uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
314 | 689 static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; |
0 | 690 |
691 /* init default parameters */ | |
692 s->sdecaycod = 2; | |
693 s->fdecaycod = 1; | |
694 s->sgaincod = 1; | |
695 s->dbkneecod = 2; | |
696 s->floorcod = 4; | |
314 | 697 for(ch=0;ch<s->nb_all_channels;ch++) |
0 | 698 s->fgaincod[ch] = 4; |
699 | |
700 /* compute real values */ | |
782 | 701 s->bit_alloc.fscod = s->fscod; |
702 s->bit_alloc.halfratecod = s->halfratecod; | |
703 s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod; | |
704 s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod; | |
705 s->bit_alloc.sgain = sgaintab[s->sgaincod]; | |
706 s->bit_alloc.dbknee = dbkneetab[s->dbkneecod]; | |
707 s->bit_alloc.floor = floortab[s->floorcod]; | |
708 | |
0 | 709 /* header size */ |
710 frame_bits += 65; | |
314 | 711 // if (s->acmod == 2) |
712 // frame_bits += 2; | |
713 frame_bits += frame_bits_inc[s->acmod]; | |
0 | 714 |
715 /* audio blocks */ | |
716 for(i=0;i<NB_BLOCKS;i++) { | |
314 | 717 frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */ |
0 | 718 if (s->acmod == 2) |
314 | 719 frame_bits++; /* rematstr */ |
720 frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */ | |
721 if (s->lfe) | |
722 frame_bits++; /* lfeexpstr */ | |
0 | 723 for(ch=0;ch<s->nb_channels;ch++) { |
724 if (exp_strategy[i][ch] != EXP_REUSE) | |
314 | 725 frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */ |
0 | 726 } |
727 frame_bits++; /* baie */ | |
728 frame_bits++; /* snr */ | |
729 frame_bits += 2; /* delta / skip */ | |
730 } | |
731 frame_bits++; /* cplinu for block 0 */ | |
732 /* bit alloc info */ | |
314 | 733 /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */ |
734 /* csnroffset[6] */ | |
735 /* (fsnoffset[4] + fgaincod[4]) * c */ | |
736 frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3); | |
0 | 737 |
738 /* CRC */ | |
739 frame_bits += 16; | |
740 | |
741 /* now the big work begins : do the bit allocation. Modify the snr | |
742 offset until we can pack everything in the requested frame size */ | |
743 | |
744 csnroffst = s->csnroffst; | |
745 while (csnroffst >= 0 && | |
314 | 746 bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0) |
747 csnroffst -= SNR_INC1; | |
0 | 748 if (csnroffst < 0) { |
314 | 749 fprintf(stderr, "Yack, Error !!!\n"); |
750 return -1; | |
0 | 751 } |
752 while ((csnroffst + SNR_INC1) <= 63 && | |
753 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, | |
754 csnroffst + SNR_INC1, 0) >= 0) { | |
755 csnroffst += SNR_INC1; | |
756 memcpy(bap, bap1, sizeof(bap1)); | |
757 } | |
758 while ((csnroffst + 1) <= 63 && | |
759 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) { | |
760 csnroffst++; | |
761 memcpy(bap, bap1, sizeof(bap1)); | |
762 } | |
763 | |
764 fsnroffst = 0; | |
765 while ((fsnroffst + SNR_INC1) <= 15 && | |
766 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, | |
767 csnroffst, fsnroffst + SNR_INC1) >= 0) { | |
768 fsnroffst += SNR_INC1; | |
769 memcpy(bap, bap1, sizeof(bap1)); | |
770 } | |
771 while ((fsnroffst + 1) <= 15 && | |
772 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, | |
773 csnroffst, fsnroffst + 1) >= 0) { | |
774 fsnroffst++; | |
775 memcpy(bap, bap1, sizeof(bap1)); | |
776 } | |
777 | |
778 s->csnroffst = csnroffst; | |
314 | 779 for(ch=0;ch<s->nb_all_channels;ch++) |
0 | 780 s->fsnroffst[ch] = fsnroffst; |
781 #if defined(DEBUG_BITALLOC) | |
782 { | |
783 int j; | |
784 | |
785 for(i=0;i<6;i++) { | |
314 | 786 for(ch=0;ch<s->nb_all_channels;ch++) { |
0 | 787 printf("Block #%d Ch%d:\n", i, ch); |
788 printf("bap="); | |
789 for(j=0;j<s->nb_coefs[ch];j++) { | |
790 printf("%d ",bap[i][ch][j]); | |
791 } | |
792 printf("\n"); | |
793 } | |
794 } | |
795 } | |
796 #endif | |
797 return 0; | |
798 } | |
799 | |
782 | 800 void ac3_common_init(void) |
801 { | |
802 int i, j, k, l, v; | |
803 /* compute bndtab and masktab from bandsz */ | |
804 k = 0; | |
805 l = 0; | |
806 for(i=0;i<50;i++) { | |
807 bndtab[i] = l; | |
808 v = bndsz[i]; | |
809 for(j=0;j<v;j++) masktab[k++]=i; | |
810 l += v; | |
811 } | |
812 bndtab[50] = 0; | |
813 } | |
814 | |
815 | |
0 | 816 static int AC3_encode_init(AVCodecContext *avctx) |
817 { | |
818 int freq = avctx->sample_rate; | |
819 int bitrate = avctx->bit_rate; | |
820 int channels = avctx->channels; | |
821 AC3EncodeContext *s = avctx->priv_data; | |
782 | 822 int i, j, ch; |
0 | 823 float alpha; |
1064 | 824 static const uint8_t acmod_defs[6] = { |
314 | 825 0x01, /* C */ |
826 0x02, /* L R */ | |
827 0x03, /* L C R */ | |
828 0x06, /* L R SL SR */ | |
829 0x07, /* L C R SL SR */ | |
830 0x07, /* L C R SL SR (+LFE) */ | |
831 }; | |
0 | 832 |
833 avctx->frame_size = AC3_FRAME_SIZE; | |
834 | |
835 /* number of channels */ | |
314 | 836 if (channels < 1 || channels > 6) |
837 return -1; | |
838 s->acmod = acmod_defs[channels - 1]; | |
839 s->lfe = (channels == 6) ? 1 : 0; | |
840 s->nb_all_channels = channels; | |
841 s->nb_channels = channels > 5 ? 5 : channels; | |
842 s->lfe_channel = s->lfe ? 5 : -1; | |
0 | 843 |
844 /* frequency */ | |
845 for(i=0;i<3;i++) { | |
846 for(j=0;j<3;j++) | |
782 | 847 if ((ac3_freqs[j] >> i) == freq) |
0 | 848 goto found; |
849 } | |
850 return -1; | |
851 found: | |
852 s->sample_rate = freq; | |
853 s->halfratecod = i; | |
854 s->fscod = j; | |
855 s->bsid = 8 + s->halfratecod; | |
856 s->bsmod = 0; /* complete main audio service */ | |
857 | |
858 /* bitrate & frame size */ | |
859 bitrate /= 1000; | |
860 for(i=0;i<19;i++) { | |
782 | 861 if ((ac3_bitratetab[i] >> s->halfratecod) == bitrate) |
0 | 862 break; |
863 } | |
864 if (i == 19) | |
865 return -1; | |
866 s->bit_rate = bitrate; | |
867 s->frmsizecod = i << 1; | |
868 s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16); | |
869 /* for now we do not handle fractional sizes */ | |
870 s->frame_size = s->frame_size_min; | |
871 | |
872 /* bit allocation init */ | |
873 for(ch=0;ch<s->nb_channels;ch++) { | |
874 /* bandwidth for each channel */ | |
875 /* XXX: should compute the bandwidth according to the frame | |
876 size, so that we avoid anoying high freq artefacts */ | |
877 s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */ | |
878 s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37; | |
879 } | |
314 | 880 if (s->lfe) { |
881 s->nb_coefs[s->lfe_channel] = 7; /* fixed */ | |
882 } | |
0 | 883 /* initial snr offset */ |
884 s->csnroffst = 40; | |
885 | |
782 | 886 ac3_common_init(); |
0 | 887 |
888 /* mdct init */ | |
889 fft_init(MDCT_NBITS - 2); | |
890 for(i=0;i<N/4;i++) { | |
891 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N; | |
892 xcos1[i] = fix15(-cos(alpha)); | |
893 xsin1[i] = fix15(-sin(alpha)); | |
894 } | |
895 | |
896 ac3_crc_init(); | |
925 | 897 |
898 avctx->coded_frame= avcodec_alloc_frame(); | |
899 avctx->coded_frame->key_frame= 1; | |
0 | 900 |
901 return 0; | |
902 } | |
903 | |
904 /* output the AC3 frame header */ | |
905 static void output_frame_header(AC3EncodeContext *s, unsigned char *frame) | |
906 { | |
907 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE, NULL, NULL); | |
908 | |
909 put_bits(&s->pb, 16, 0x0b77); /* frame header */ | |
910 put_bits(&s->pb, 16, 0); /* crc1: will be filled later */ | |
911 put_bits(&s->pb, 2, s->fscod); | |
912 put_bits(&s->pb, 6, s->frmsizecod + (s->frame_size - s->frame_size_min)); | |
913 put_bits(&s->pb, 5, s->bsid); | |
914 put_bits(&s->pb, 3, s->bsmod); | |
915 put_bits(&s->pb, 3, s->acmod); | |
314 | 916 if ((s->acmod & 0x01) && s->acmod != 0x01) |
917 put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */ | |
918 if (s->acmod & 0x04) | |
919 put_bits(&s->pb, 2, 1); /* XXX -6 dB */ | |
920 if (s->acmod == 0x02) | |
0 | 921 put_bits(&s->pb, 2, 0); /* surround not indicated */ |
314 | 922 put_bits(&s->pb, 1, s->lfe); /* LFE */ |
0 | 923 put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */ |
924 put_bits(&s->pb, 1, 0); /* no compression control word */ | |
925 put_bits(&s->pb, 1, 0); /* no lang code */ | |
926 put_bits(&s->pb, 1, 0); /* no audio production info */ | |
927 put_bits(&s->pb, 1, 0); /* no copyright */ | |
928 put_bits(&s->pb, 1, 1); /* original bitstream */ | |
929 put_bits(&s->pb, 1, 0); /* no time code 1 */ | |
930 put_bits(&s->pb, 1, 0); /* no time code 2 */ | |
931 put_bits(&s->pb, 1, 0); /* no addtional bit stream info */ | |
932 } | |
933 | |
934 /* symetric quantization on 'levels' levels */ | |
935 static inline int sym_quant(int c, int e, int levels) | |
936 { | |
937 int v; | |
938 | |
939 if (c >= 0) { | |
87 | 940 v = (levels * (c << e)) >> 24; |
941 v = (v + 1) >> 1; | |
0 | 942 v = (levels >> 1) + v; |
943 } else { | |
87 | 944 v = (levels * ((-c) << e)) >> 24; |
945 v = (v + 1) >> 1; | |
0 | 946 v = (levels >> 1) - v; |
947 } | |
948 assert (v >= 0 && v < levels); | |
949 return v; | |
950 } | |
951 | |
952 /* asymetric quantization on 2^qbits levels */ | |
953 static inline int asym_quant(int c, int e, int qbits) | |
954 { | |
955 int lshift, m, v; | |
956 | |
957 lshift = e + qbits - 24; | |
958 if (lshift >= 0) | |
959 v = c << lshift; | |
960 else | |
961 v = c >> (-lshift); | |
962 /* rounding */ | |
963 v = (v + 1) >> 1; | |
964 m = (1 << (qbits-1)); | |
965 if (v >= m) | |
966 v = m - 1; | |
967 assert(v >= -m); | |
968 return v & ((1 << qbits)-1); | |
969 } | |
970 | |
971 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3 | |
972 frame */ | |
973 static void output_audio_block(AC3EncodeContext *s, | |
1064 | 974 uint8_t exp_strategy[AC3_MAX_CHANNELS], |
975 uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2], | |
976 uint8_t bap[AC3_MAX_CHANNELS][N/2], | |
977 int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2], | |
978 int8_t global_exp[AC3_MAX_CHANNELS], | |
0 | 979 int block_num) |
980 { | |
981 int ch, nb_groups, group_size, i, baie; | |
1064 | 982 uint8_t *p; |
983 uint16_t qmant[AC3_MAX_CHANNELS][N/2]; | |
0 | 984 int exp0, exp1; |
985 int mant1_cnt, mant2_cnt, mant4_cnt; | |
1064 | 986 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; |
0 | 987 int delta0, delta1, delta2; |
988 | |
989 for(ch=0;ch<s->nb_channels;ch++) | |
990 put_bits(&s->pb, 1, 0); /* 512 point MDCT */ | |
991 for(ch=0;ch<s->nb_channels;ch++) | |
992 put_bits(&s->pb, 1, 1); /* no dither */ | |
993 put_bits(&s->pb, 1, 0); /* no dynamic range */ | |
994 if (block_num == 0) { | |
995 /* for block 0, even if no coupling, we must say it. This is a | |
996 waste of bit :-) */ | |
997 put_bits(&s->pb, 1, 1); /* coupling strategy present */ | |
998 put_bits(&s->pb, 1, 0); /* no coupling strategy */ | |
999 } else { | |
1000 put_bits(&s->pb, 1, 0); /* no new coupling strategy */ | |
1001 } | |
1002 | |
1003 if (s->acmod == 2) { | |
1004 put_bits(&s->pb, 1, 0); /* no matrixing (but should be used in the future) */ | |
1005 } | |
1006 | |
1007 #if defined(DEBUG) | |
1008 { | |
1009 static int count = 0; | |
1010 printf("Block #%d (%d)\n", block_num, count++); | |
1011 } | |
1012 #endif | |
1013 /* exponent strategy */ | |
1014 for(ch=0;ch<s->nb_channels;ch++) { | |
1015 put_bits(&s->pb, 2, exp_strategy[ch]); | |
1016 } | |
1017 | |
314 | 1018 if (s->lfe) { |
1019 put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]); | |
1020 } | |
1021 | |
0 | 1022 for(ch=0;ch<s->nb_channels;ch++) { |
1023 if (exp_strategy[ch] != EXP_REUSE) | |
1024 put_bits(&s->pb, 6, s->chbwcod[ch]); | |
1025 } | |
1026 | |
1027 /* exponents */ | |
314 | 1028 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 1029 switch(exp_strategy[ch]) { |
1030 case EXP_REUSE: | |
1031 continue; | |
1032 case EXP_D15: | |
1033 group_size = 1; | |
1034 break; | |
1035 case EXP_D25: | |
1036 group_size = 2; | |
1037 break; | |
1038 default: | |
1039 case EXP_D45: | |
1040 group_size = 4; | |
1041 break; | |
1042 } | |
314 | 1043 nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size); |
0 | 1044 p = encoded_exp[ch]; |
1045 | |
1046 /* first exponent */ | |
1047 exp1 = *p++; | |
1048 put_bits(&s->pb, 4, exp1); | |
1049 | |
1050 /* next ones are delta encoded */ | |
1051 for(i=0;i<nb_groups;i++) { | |
1052 /* merge three delta in one code */ | |
1053 exp0 = exp1; | |
1054 exp1 = p[0]; | |
1055 p += group_size; | |
1056 delta0 = exp1 - exp0 + 2; | |
1057 | |
1058 exp0 = exp1; | |
1059 exp1 = p[0]; | |
1060 p += group_size; | |
1061 delta1 = exp1 - exp0 + 2; | |
1062 | |
1063 exp0 = exp1; | |
1064 exp1 = p[0]; | |
1065 p += group_size; | |
1066 delta2 = exp1 - exp0 + 2; | |
1067 | |
1068 put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2); | |
1069 } | |
1070 | |
314 | 1071 if (ch != s->lfe_channel) |
1072 put_bits(&s->pb, 2, 0); /* no gain range info */ | |
0 | 1073 } |
1074 | |
1075 /* bit allocation info */ | |
1076 baie = (block_num == 0); | |
1077 put_bits(&s->pb, 1, baie); | |
1078 if (baie) { | |
1079 put_bits(&s->pb, 2, s->sdecaycod); | |
1080 put_bits(&s->pb, 2, s->fdecaycod); | |
1081 put_bits(&s->pb, 2, s->sgaincod); | |
1082 put_bits(&s->pb, 2, s->dbkneecod); | |
1083 put_bits(&s->pb, 3, s->floorcod); | |
1084 } | |
1085 | |
1086 /* snr offset */ | |
1087 put_bits(&s->pb, 1, baie); /* always present with bai */ | |
1088 if (baie) { | |
1089 put_bits(&s->pb, 6, s->csnroffst); | |
314 | 1090 for(ch=0;ch<s->nb_all_channels;ch++) { |
0 | 1091 put_bits(&s->pb, 4, s->fsnroffst[ch]); |
1092 put_bits(&s->pb, 3, s->fgaincod[ch]); | |
1093 } | |
1094 } | |
1095 | |
1096 put_bits(&s->pb, 1, 0); /* no delta bit allocation */ | |
1097 put_bits(&s->pb, 1, 0); /* no data to skip */ | |
1098 | |
1099 /* mantissa encoding : we use two passes to handle the grouping. A | |
1100 one pass method may be faster, but it would necessitate to | |
1101 modify the output stream. */ | |
1102 | |
1103 /* first pass: quantize */ | |
1104 mant1_cnt = mant2_cnt = mant4_cnt = 0; | |
1105 qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL; | |
1106 | |
314 | 1107 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 1108 int b, c, e, v; |
1109 | |
1110 for(i=0;i<s->nb_coefs[ch];i++) { | |
1111 c = mdct_coefs[ch][i]; | |
1112 e = encoded_exp[ch][i] - global_exp[ch]; | |
1113 b = bap[ch][i]; | |
1114 switch(b) { | |
1115 case 0: | |
1116 v = 0; | |
1117 break; | |
1118 case 1: | |
1119 v = sym_quant(c, e, 3); | |
1120 switch(mant1_cnt) { | |
1121 case 0: | |
1122 qmant1_ptr = &qmant[ch][i]; | |
1123 v = 9 * v; | |
1124 mant1_cnt = 1; | |
1125 break; | |
1126 case 1: | |
1127 *qmant1_ptr += 3 * v; | |
1128 mant1_cnt = 2; | |
1129 v = 128; | |
1130 break; | |
1131 default: | |
1132 *qmant1_ptr += v; | |
1133 mant1_cnt = 0; | |
1134 v = 128; | |
1135 break; | |
1136 } | |
1137 break; | |
1138 case 2: | |
1139 v = sym_quant(c, e, 5); | |
1140 switch(mant2_cnt) { | |
1141 case 0: | |
1142 qmant2_ptr = &qmant[ch][i]; | |
1143 v = 25 * v; | |
1144 mant2_cnt = 1; | |
1145 break; | |
1146 case 1: | |
1147 *qmant2_ptr += 5 * v; | |
1148 mant2_cnt = 2; | |
1149 v = 128; | |
1150 break; | |
1151 default: | |
1152 *qmant2_ptr += v; | |
1153 mant2_cnt = 0; | |
1154 v = 128; | |
1155 break; | |
1156 } | |
1157 break; | |
1158 case 3: | |
1159 v = sym_quant(c, e, 7); | |
1160 break; | |
1161 case 4: | |
1162 v = sym_quant(c, e, 11); | |
1163 switch(mant4_cnt) { | |
1164 case 0: | |
1165 qmant4_ptr = &qmant[ch][i]; | |
1166 v = 11 * v; | |
1167 mant4_cnt = 1; | |
1168 break; | |
1169 default: | |
1170 *qmant4_ptr += v; | |
1171 mant4_cnt = 0; | |
1172 v = 128; | |
1173 break; | |
1174 } | |
1175 break; | |
1176 case 5: | |
1177 v = sym_quant(c, e, 15); | |
1178 break; | |
1179 case 14: | |
1180 v = asym_quant(c, e, 14); | |
1181 break; | |
1182 case 15: | |
1183 v = asym_quant(c, e, 16); | |
1184 break; | |
1185 default: | |
1186 v = asym_quant(c, e, b - 1); | |
1187 break; | |
1188 } | |
1189 qmant[ch][i] = v; | |
1190 } | |
1191 } | |
1192 | |
1193 /* second pass : output the values */ | |
314 | 1194 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 1195 int b, q; |
1196 | |
1197 for(i=0;i<s->nb_coefs[ch];i++) { | |
1198 q = qmant[ch][i]; | |
1199 b = bap[ch][i]; | |
1200 switch(b) { | |
1201 case 0: | |
1202 break; | |
1203 case 1: | |
1204 if (q != 128) | |
1205 put_bits(&s->pb, 5, q); | |
1206 break; | |
1207 case 2: | |
1208 if (q != 128) | |
1209 put_bits(&s->pb, 7, q); | |
1210 break; | |
1211 case 3: | |
1212 put_bits(&s->pb, 3, q); | |
1213 break; | |
1214 case 4: | |
1215 if (q != 128) | |
1216 put_bits(&s->pb, 7, q); | |
1217 break; | |
1218 case 14: | |
1219 put_bits(&s->pb, 14, q); | |
1220 break; | |
1221 case 15: | |
1222 put_bits(&s->pb, 16, q); | |
1223 break; | |
1224 default: | |
1225 put_bits(&s->pb, b - 1, q); | |
1226 break; | |
1227 } | |
1228 } | |
1229 } | |
1230 } | |
1231 | |
1232 /* compute the ac3 crc */ | |
1233 | |
1234 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) | |
1235 | |
1236 static void ac3_crc_init(void) | |
1237 { | |
1238 unsigned int c, n, k; | |
1239 | |
1240 for(n=0;n<256;n++) { | |
1241 c = n << 8; | |
1242 for (k = 0; k < 8; k++) { | |
1243 if (c & (1 << 15)) | |
1244 c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff); | |
1245 else | |
1246 c = c << 1; | |
1247 } | |
1248 crc_table[n] = c; | |
1249 } | |
1250 } | |
1251 | |
1064 | 1252 static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc) |
0 | 1253 { |
1254 int i; | |
1255 for(i=0;i<n;i++) { | |
1256 crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff; | |
1257 } | |
1258 return crc; | |
1259 } | |
1260 | |
1261 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly) | |
1262 { | |
1263 unsigned int c; | |
1264 | |
1265 c = 0; | |
1266 while (a) { | |
1267 if (a & 1) | |
1268 c ^= b; | |
1269 a = a >> 1; | |
1270 b = b << 1; | |
1271 if (b & (1 << 16)) | |
1272 b ^= poly; | |
1273 } | |
1274 return c; | |
1275 } | |
1276 | |
1277 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly) | |
1278 { | |
1279 unsigned int r; | |
1280 r = 1; | |
1281 while (n) { | |
1282 if (n & 1) | |
1283 r = mul_poly(r, a, poly); | |
1284 a = mul_poly(a, a, poly); | |
1285 n >>= 1; | |
1286 } | |
1287 return r; | |
1288 } | |
1289 | |
1290 | |
1291 /* compute log2(max(abs(tab[]))) */ | |
1064 | 1292 static int log2_tab(int16_t *tab, int n) |
0 | 1293 { |
1294 int i, v; | |
1295 | |
1296 v = 0; | |
1297 for(i=0;i<n;i++) { | |
1298 v |= abs(tab[i]); | |
1299 } | |
65 | 1300 return av_log2(v); |
0 | 1301 } |
1302 | |
1064 | 1303 static void lshift_tab(int16_t *tab, int n, int lshift) |
0 | 1304 { |
1305 int i; | |
1306 | |
1307 if (lshift > 0) { | |
1308 for(i=0;i<n;i++) { | |
1309 tab[i] <<= lshift; | |
1310 } | |
1311 } else if (lshift < 0) { | |
1312 lshift = -lshift; | |
1313 for(i=0;i<n;i++) { | |
1314 tab[i] >>= lshift; | |
1315 } | |
1316 } | |
1317 } | |
1318 | |
1319 /* fill the end of the frame and compute the two crcs */ | |
1320 static int output_frame_end(AC3EncodeContext *s) | |
1321 { | |
1322 int frame_size, frame_size_58, n, crc1, crc2, crc_inv; | |
1064 | 1323 uint8_t *frame; |
0 | 1324 |
1325 frame_size = s->frame_size; /* frame size in words */ | |
1326 /* align to 8 bits */ | |
1327 flush_put_bits(&s->pb); | |
1328 /* add zero bytes to reach the frame size */ | |
1329 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
|
1330 n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2; |
0 | 1331 assert(n >= 0); |
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
|
1332 memset(pbBufPtr(&s->pb), 0, n); |
0 | 1333 |
1334 /* Now we must compute both crcs : this is not so easy for crc1 | |
1335 because it is at the beginning of the data... */ | |
1336 frame_size_58 = (frame_size >> 1) + (frame_size >> 3); | |
1337 crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0); | |
1338 /* XXX: could precompute crc_inv */ | |
1339 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY); | |
1340 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); | |
1341 frame[2] = crc1 >> 8; | |
1342 frame[3] = crc1; | |
1343 | |
1344 crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0); | |
1345 frame[2*frame_size - 2] = crc2 >> 8; | |
1346 frame[2*frame_size - 1] = crc2; | |
1347 | |
1348 // printf("n=%d frame_size=%d\n", n, frame_size); | |
1349 return frame_size * 2; | |
1350 } | |
1351 | |
782 | 1352 static int AC3_encode_frame(AVCodecContext *avctx, |
1353 unsigned char *frame, int buf_size, void *data) | |
0 | 1354 { |
1355 AC3EncodeContext *s = avctx->priv_data; | |
1356 short *samples = data; | |
1357 int i, j, k, v, ch; | |
1064 | 1358 int16_t input_samples[N]; |
1359 int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1360 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1361 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]; | |
1362 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1363 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1364 int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS]; | |
0 | 1365 int frame_bits; |
1366 | |
1367 frame_bits = 0; | |
314 | 1368 for(ch=0;ch<s->nb_all_channels;ch++) { |
0 | 1369 /* fixed mdct to the six sub blocks & exponent computation */ |
1370 for(i=0;i<NB_BLOCKS;i++) { | |
1064 | 1371 int16_t *sptr; |
0 | 1372 int sinc; |
1373 | |
1374 /* compute input samples */ | |
1064 | 1375 memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t)); |
314 | 1376 sinc = s->nb_all_channels; |
0 | 1377 sptr = samples + (sinc * (N/2) * i) + ch; |
1378 for(j=0;j<N/2;j++) { | |
1379 v = *sptr; | |
1380 input_samples[j + N/2] = v; | |
1381 s->last_samples[ch][j] = v; | |
1382 sptr += sinc; | |
1383 } | |
1384 | |
1385 /* apply the MDCT window */ | |
1386 for(j=0;j<N/2;j++) { | |
1387 input_samples[j] = MUL16(input_samples[j], | |
1388 ac3_window[j]) >> 15; | |
1389 input_samples[N-j-1] = MUL16(input_samples[N-j-1], | |
1390 ac3_window[j]) >> 15; | |
1391 } | |
1392 | |
1393 /* Normalize the samples to use the maximum available | |
1394 precision */ | |
1395 v = 14 - log2_tab(input_samples, N); | |
1396 if (v < 0) | |
1397 v = 0; | |
1398 exp_samples[i][ch] = v - 8; | |
1399 lshift_tab(input_samples, N, v); | |
1400 | |
1401 /* do the MDCT */ | |
1402 mdct512(mdct_coef[i][ch], input_samples); | |
1403 | |
1404 /* compute "exponents". We take into account the | |
1405 normalization there */ | |
1406 for(j=0;j<N/2;j++) { | |
1407 int e; | |
1408 v = abs(mdct_coef[i][ch][j]); | |
1409 if (v == 0) | |
1410 e = 24; | |
1411 else { | |
65 | 1412 e = 23 - av_log2(v) + exp_samples[i][ch]; |
0 | 1413 if (e >= 24) { |
1414 e = 24; | |
1415 mdct_coef[i][ch][j] = 0; | |
1416 } | |
1417 } | |
1418 exp[i][ch][j] = e; | |
1419 } | |
1420 } | |
1421 | |
314 | 1422 compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel); |
0 | 1423 |
1424 /* compute the exponents as the decoder will see them. The | |
1425 EXP_REUSE case must be handled carefully : we select the | |
1426 min of the exponents */ | |
1427 i = 0; | |
1428 while (i < NB_BLOCKS) { | |
1429 j = i + 1; | |
1430 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) { | |
1431 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]); | |
1432 j++; | |
1433 } | |
1434 frame_bits += encode_exp(encoded_exp[i][ch], | |
1435 exp[i][ch], s->nb_coefs[ch], | |
1436 exp_strategy[i][ch]); | |
1437 /* copy encoded exponents for reuse case */ | |
1438 for(k=i+1;k<j;k++) { | |
1439 memcpy(encoded_exp[k][ch], encoded_exp[i][ch], | |
1064 | 1440 s->nb_coefs[ch] * sizeof(uint8_t)); |
0 | 1441 } |
1442 i = j; | |
1443 } | |
1444 } | |
1445 | |
1446 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits); | |
1447 /* everything is known... let's output the frame */ | |
1448 output_frame_header(s, frame); | |
1449 | |
1450 for(i=0;i<NB_BLOCKS;i++) { | |
1451 output_audio_block(s, exp_strategy[i], encoded_exp[i], | |
1452 bap[i], mdct_coef[i], exp_samples[i], i); | |
1453 } | |
1454 return output_frame_end(s); | |
1455 } | |
1456 | |
925 | 1457 static int AC3_encode_close(AVCodecContext *avctx) |
1458 { | |
1459 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
|
1460 return 0; |
925 | 1461 } |
1462 | |
0 | 1463 #if 0 |
1464 /*************************************************************************/ | |
1465 /* TEST */ | |
1466 | |
1467 #define FN (N/4) | |
1468 | |
1469 void fft_test(void) | |
1470 { | |
1471 IComplex in[FN], in1[FN]; | |
1472 int k, n, i; | |
1473 float sum_re, sum_im, a; | |
1474 | |
1475 /* FFT test */ | |
1476 | |
1477 for(i=0;i<FN;i++) { | |
1478 in[i].re = random() % 65535 - 32767; | |
1479 in[i].im = random() % 65535 - 32767; | |
1480 in1[i] = in[i]; | |
1481 } | |
1482 fft(in, 7); | |
1483 | |
1484 /* do it by hand */ | |
1485 for(k=0;k<FN;k++) { | |
1486 sum_re = 0; | |
1487 sum_im = 0; | |
1488 for(n=0;n<FN;n++) { | |
1489 a = -2 * M_PI * (n * k) / FN; | |
1490 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a); | |
1491 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a); | |
1492 } | |
1493 printf("%3d: %6d,%6d %6.0f,%6.0f\n", | |
1494 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN); | |
1495 } | |
1496 } | |
1497 | |
1498 void mdct_test(void) | |
1499 { | |
1064 | 1500 int16_t input[N]; |
1501 int32_t output[N/2]; | |
0 | 1502 float input1[N]; |
1503 float output1[N/2]; | |
1504 float s, a, err, e, emax; | |
1505 int i, k, n; | |
1506 | |
1507 for(i=0;i<N;i++) { | |
1508 input[i] = (random() % 65535 - 32767) * 9 / 10; | |
1509 input1[i] = input[i]; | |
1510 } | |
1511 | |
1512 mdct512(output, input); | |
1513 | |
1514 /* do it by hand */ | |
1515 for(k=0;k<N/2;k++) { | |
1516 s = 0; | |
1517 for(n=0;n<N;n++) { | |
1518 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N)); | |
1519 s += input1[n] * cos(a); | |
1520 } | |
1521 output1[k] = -2 * s / N; | |
1522 } | |
1523 | |
1524 err = 0; | |
1525 emax = 0; | |
1526 for(i=0;i<N/2;i++) { | |
1527 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]); | |
1528 e = output[i] - output1[i]; | |
1529 if (e > emax) | |
1530 emax = e; | |
1531 err += e * e; | |
1532 } | |
1533 printf("err2=%f emax=%f\n", err / (N/2), emax); | |
1534 } | |
1535 | |
1536 void test_ac3(void) | |
1537 { | |
1538 AC3EncodeContext ctx; | |
1539 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE]; | |
1540 short samples[AC3_FRAME_SIZE]; | |
1541 int ret, i; | |
1542 | |
1543 AC3_encode_init(&ctx, 44100, 64000, 1); | |
1544 | |
1545 fft_test(); | |
1546 mdct_test(); | |
1547 | |
1548 for(i=0;i<AC3_FRAME_SIZE;i++) | |
1549 samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000); | |
1550 ret = AC3_encode_frame(&ctx, frame, samples); | |
1551 printf("ret=%d\n", ret); | |
1552 } | |
1553 #endif | |
1554 | |
1555 AVCodec ac3_encoder = { | |
1556 "ac3", | |
1557 CODEC_TYPE_AUDIO, | |
1558 CODEC_ID_AC3, | |
1559 sizeof(AC3EncodeContext), | |
1560 AC3_encode_init, | |
1561 AC3_encode_frame, | |
925 | 1562 AC3_encode_close, |
0 | 1563 NULL, |
1564 }; |