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