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