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