Mercurial > libavcodec.hg
annotate ac3enc.c @ 9677:592097778fa0 libavcodec
Use IF1 frame sizes in AMR-WB.
Patch by Martin Storsj: martin (remove that) at martin (oh and this too) st
author | benoit |
---|---|
date | Wed, 20 May 2009 08:57:32 +0000 |
parents | f15a920ce40e |
children | c78fd9154378 |
rev | line source |
---|---|
0 | 1 /* |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
2 * The simplest AC-3 encoder |
8629
04423b2f6e0b
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
7769
diff
changeset
|
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 /** | |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8629
diff
changeset
|
23 * @file libavcodec/ac3enc.c |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
24 * The simplest AC-3 encoder. |
1106 | 25 */ |
64 | 26 //#define DEBUG |
27 //#define DEBUG_BITALLOC | |
6763 | 28 #include "libavutil/crc.h" |
64 | 29 #include "avcodec.h" |
9428 | 30 #include "get_bits.h" // for ff_reverse |
9411
4cb7c65fc775
Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents:
9142
diff
changeset
|
31 #include "put_bits.h" |
782 | 32 #include "ac3.h" |
9513 | 33 #include "audioconvert.h" |
782 | 34 |
35 typedef struct AC3EncodeContext { | |
36 PutBitContext pb; | |
37 int nb_channels; | |
38 int nb_all_channels; | |
39 int lfe_channel; | |
9452 | 40 const uint8_t *channel_map; |
782 | 41 int bit_rate; |
1057 | 42 unsigned int sample_rate; |
6005 | 43 unsigned int bitstream_id; |
1057 | 44 unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */ |
45 unsigned int frame_size; /* current frame size in words */ | |
3246 | 46 unsigned int bits_written; |
47 unsigned int samples_written; | |
6003 | 48 int sr_shift; |
6005 | 49 unsigned int frame_size_code; |
6003 | 50 unsigned int sr_code; /* frequency */ |
6005 | 51 unsigned int channel_mode; |
782 | 52 int lfe; |
6005 | 53 unsigned int bitstream_mode; |
782 | 54 short last_samples[AC3_MAX_CHANNELS][256]; |
1057 | 55 unsigned int chbwcod[AC3_MAX_CHANNELS]; |
782 | 56 int nb_coefs[AC3_MAX_CHANNELS]; |
2967 | 57 |
782 | 58 /* bitrate allocation control */ |
6003 | 59 int slow_gain_code, slow_decay_code, fast_decay_code, db_per_bit_code, floor_code; |
782 | 60 AC3BitAllocParameters bit_alloc; |
6003 | 61 int coarse_snr_offset; |
62 int fast_gain_code[AC3_MAX_CHANNELS]; | |
63 int fine_snr_offset[AC3_MAX_CHANNELS]; | |
782 | 64 /* mantissa encoding */ |
65 int mant1_cnt, mant2_cnt, mant4_cnt; | |
66 } AC3EncodeContext; | |
67 | |
4643
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
68 static int16_t costab[64]; |
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
69 static int16_t sintab[64]; |
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
70 static int16_t xcos1[128]; |
1e175640dad3
Remove common code from AC-3 encoder and utilize ac3.c.
jbr
parents:
4641
diff
changeset
|
71 static int16_t xsin1[128]; |
0 | 72 |
73 #define MDCT_NBITS 9 | |
74 #define N (1 << MDCT_NBITS) | |
75 | |
76 /* new exponents are sent if their Norm 1 exceed this number */ | |
77 #define EXP_DIFF_THRESHOLD 1000 | |
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 | |
9007
043574c5c153
Add missing av_cold in static init/close functions.
stefano
parents:
8718
diff
changeset
|
94 static av_cold void fft_init(int ln) |
0 | 95 { |
6166 | 96 int i, n; |
0 | 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 | |
108 /* butter fly op */ | |
109 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \ | |
110 {\ | |
111 int ax, ay, bx, by;\ | |
112 bx=pre1;\ | |
113 by=pim1;\ | |
114 ax=qre1;\ | |
115 ay=qim1;\ | |
116 pre = (bx + ax) >> 1;\ | |
117 pim = (by + ay) >> 1;\ | |
118 qre = (bx - ax) >> 1;\ | |
119 qim = (by - ay) >> 1;\ | |
120 } | |
121 | |
122 #define CMUL(pre, pim, are, aim, bre, bim) \ | |
123 {\ | |
124 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\ | |
125 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\ | |
126 } | |
127 | |
128 | |
129 /* do a 2^n point complex fft on 2^ln points. */ | |
130 static void fft(IComplex *z, int ln) | |
131 { | |
2979 | 132 int j, l, np, np2; |
133 int nblocks, nloops; | |
0 | 134 register IComplex *p,*q; |
135 int tmp_re, tmp_im; | |
136 | |
137 np = 1 << ln; | |
138 | |
139 /* reverse */ | |
140 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
|
141 int k = ff_reverse[j] >> (8 - ln); |
6140 | 142 if (k < j) |
143 FFSWAP(IComplex, z[k], z[j]); | |
0 | 144 } |
145 | |
146 /* pass 0 */ | |
147 | |
148 p=&z[0]; | |
149 j=(np >> 1); | |
150 do { | |
2967 | 151 BF(p[0].re, p[0].im, p[1].re, p[1].im, |
0 | 152 p[0].re, p[0].im, p[1].re, p[1].im); |
153 p+=2; | |
154 } while (--j != 0); | |
155 | |
156 /* pass 1 */ | |
157 | |
158 p=&z[0]; | |
159 j=np >> 2; | |
160 do { | |
2967 | 161 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
0 | 162 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 163 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
0 | 164 p[1].re, p[1].im, p[3].im, -p[3].re); |
165 p+=4; | |
166 } while (--j != 0); | |
167 | |
168 /* pass 2 .. ln-1 */ | |
169 | |
170 nblocks = np >> 3; | |
171 nloops = 1 << 2; | |
172 np2 = np >> 1; | |
173 do { | |
174 p = z; | |
175 q = z + nloops; | |
176 for (j = 0; j < nblocks; ++j) { | |
177 | |
178 BF(p->re, p->im, q->re, q->im, | |
179 p->re, p->im, q->re, q->im); | |
2967 | 180 |
0 | 181 p++; |
182 q++; | |
183 for(l = nblocks; l < np2; l += nblocks) { | |
184 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im); | |
185 BF(p->re, p->im, q->re, q->im, | |
186 p->re, p->im, tmp_re, tmp_im); | |
187 p++; | |
188 q++; | |
189 } | |
190 p += nloops; | |
191 q += nloops; | |
192 } | |
193 nblocks = nblocks >> 1; | |
194 nloops = nloops << 1; | |
195 } while (nblocks != 0); | |
196 } | |
197 | |
198 /* do a 512 point mdct */ | |
1064 | 199 static void mdct512(int32_t *out, int16_t *in) |
0 | 200 { |
201 int i, re, im, re1, im1; | |
2967 | 202 int16_t rot[N]; |
0 | 203 IComplex x[N/4]; |
204 | |
205 /* shift to simplify computations */ | |
206 for(i=0;i<N/4;i++) | |
207 rot[i] = -in[i + 3*N/4]; | |
208 for(i=N/4;i<N;i++) | |
209 rot[i] = in[i - N/4]; | |
2967 | 210 |
0 | 211 /* pre rotation */ |
212 for(i=0;i<N/4;i++) { | |
213 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1; | |
214 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1; | |
215 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]); | |
216 } | |
217 | |
218 fft(x, MDCT_NBITS - 2); | |
2967 | 219 |
0 | 220 /* post rotation */ |
221 for(i=0;i<N/4;i++) { | |
222 re = x[i].re; | |
223 im = x[i].im; | |
224 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]); | |
225 out[2*i] = im1; | |
226 out[N/2-1-2*i] = re1; | |
227 } | |
228 } | |
229 | |
230 /* XXX: use another norm ? */ | |
1064 | 231 static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n) |
0 | 232 { |
233 int sum, i; | |
234 sum = 0; | |
235 for(i=0;i<n;i++) { | |
236 sum += abs(exp1[i] - exp2[i]); | |
237 } | |
238 return sum; | |
239 } | |
240 | |
1064 | 241 static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
242 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
314 | 243 int ch, int is_lfe) |
0 | 244 { |
245 int i, j; | |
246 int exp_diff; | |
2967 | 247 |
0 | 248 /* estimate if the exponent variation & decide if they should be |
249 reused in the next frame */ | |
250 exp_strategy[0][ch] = EXP_NEW; | |
251 for(i=1;i<NB_BLOCKS;i++) { | |
252 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2); | |
2967 | 253 #ifdef DEBUG |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
254 av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff); |
0 | 255 #endif |
256 if (exp_diff > EXP_DIFF_THRESHOLD) | |
257 exp_strategy[i][ch] = EXP_NEW; | |
258 else | |
259 exp_strategy[i][ch] = EXP_REUSE; | |
260 } | |
314 | 261 if (is_lfe) |
2979 | 262 return; |
314 | 263 |
0 | 264 /* now select the encoding strategy type : if exponents are often |
265 recoded, we use a coarse encoding */ | |
266 i = 0; | |
267 while (i < NB_BLOCKS) { | |
268 j = i + 1; | |
269 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) | |
270 j++; | |
271 switch(j - i) { | |
272 case 1: | |
273 exp_strategy[i][ch] = EXP_D45; | |
274 break; | |
275 case 2: | |
276 case 3: | |
277 exp_strategy[i][ch] = EXP_D25; | |
278 break; | |
279 default: | |
280 exp_strategy[i][ch] = EXP_D15; | |
281 break; | |
282 } | |
2979 | 283 i = j; |
0 | 284 } |
285 } | |
286 | |
287 /* set exp[i] to min(exp[i], exp1[i]) */ | |
1064 | 288 static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n) |
0 | 289 { |
290 int i; | |
291 | |
292 for(i=0;i<n;i++) { | |
293 if (exp1[i] < exp[i]) | |
294 exp[i] = exp1[i]; | |
295 } | |
296 } | |
2967 | 297 |
0 | 298 /* update the exponents so that they are the ones the decoder will |
299 decode. Return the number of bits used to code the exponents */ | |
2967 | 300 static int encode_exp(uint8_t encoded_exp[N/2], |
301 uint8_t exp[N/2], | |
0 | 302 int nb_exps, |
303 int exp_strategy) | |
304 { | |
2157
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
305 int group_size, nb_groups, i, j, k, exp_min; |
1064 | 306 uint8_t exp1[N/2]; |
0 | 307 |
308 switch(exp_strategy) { | |
309 case EXP_D15: | |
310 group_size = 1; | |
311 break; | |
312 case EXP_D25: | |
313 group_size = 2; | |
314 break; | |
315 default: | |
316 case EXP_D45: | |
317 group_size = 4; | |
318 break; | |
319 } | |
320 nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3; | |
321 | |
322 /* for each group, compute the minimum exponent */ | |
323 exp1[0] = exp[0]; /* DC exponent is handled separately */ | |
324 k = 1; | |
325 for(i=1;i<=nb_groups;i++) { | |
326 exp_min = exp[k]; | |
327 assert(exp_min >= 0 && exp_min <= 24); | |
328 for(j=1;j<group_size;j++) { | |
329 if (exp[k+j] < exp_min) | |
330 exp_min = exp[k+j]; | |
331 } | |
332 exp1[i] = exp_min; | |
333 k += group_size; | |
334 } | |
335 | |
336 /* constraint for DC exponent */ | |
337 if (exp1[0] > 15) | |
338 exp1[0] = 15; | |
339 | |
2157
4478e603a8e3
simpler delta decreasing algorithm patch by (Jeff Muizelaar <jrmuizel at student dot cs dot uwaterloo dot ca>)
michael
parents:
1819
diff
changeset
|
340 /* 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
|
341 * 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
|
342 for (i=1;i<=nb_groups;i++) |
2979 | 343 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
|
344 for (i=nb_groups-1;i>=0;i--) |
2979 | 345 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
|
346 |
0 | 347 /* now we have the exponent values the decoder will see */ |
348 encoded_exp[0] = exp1[0]; | |
349 k = 1; | |
350 for(i=1;i<=nb_groups;i++) { | |
351 for(j=0;j<group_size;j++) { | |
352 encoded_exp[k+j] = exp1[i]; | |
353 } | |
354 k += group_size; | |
355 } | |
2967 | 356 |
0 | 357 #if defined(DEBUG) |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
358 av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy); |
0 | 359 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
|
360 av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]); |
0 | 361 } |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
362 av_log(NULL, AV_LOG_DEBUG, "\n"); |
0 | 363 #endif |
364 | |
365 return 4 + (nb_groups / 3) * 7; | |
366 } | |
367 | |
368 /* return the size in bits taken by the mantissa */ | |
1064 | 369 static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |
0 | 370 { |
371 int bits, mant, i; | |
372 | |
373 bits = 0; | |
374 for(i=0;i<nb_coefs;i++) { | |
375 mant = m[i]; | |
376 switch(mant) { | |
377 case 0: | |
378 /* nothing */ | |
379 break; | |
380 case 1: | |
381 /* 3 mantissa in 5 bits */ | |
2967 | 382 if (s->mant1_cnt == 0) |
0 | 383 bits += 5; |
384 if (++s->mant1_cnt == 3) | |
385 s->mant1_cnt = 0; | |
386 break; | |
387 case 2: | |
388 /* 3 mantissa in 7 bits */ | |
2967 | 389 if (s->mant2_cnt == 0) |
0 | 390 bits += 7; |
391 if (++s->mant2_cnt == 3) | |
392 s->mant2_cnt = 0; | |
393 break; | |
394 case 3: | |
395 bits += 3; | |
396 break; | |
397 case 4: | |
398 /* 2 mantissa in 7 bits */ | |
399 if (s->mant4_cnt == 0) | |
400 bits += 7; | |
2967 | 401 if (++s->mant4_cnt == 2) |
0 | 402 s->mant4_cnt = 0; |
403 break; | |
404 case 14: | |
405 bits += 14; | |
406 break; | |
407 case 15: | |
408 bits += 16; | |
409 break; | |
410 default: | |
411 bits += mant - 1; | |
412 break; | |
413 } | |
414 } | |
415 return bits; | |
416 } | |
417 | |
418 | |
4704
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
419 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
|
420 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
|
421 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
|
422 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
|
423 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
|
424 { |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
425 int blk, ch; |
6003 | 426 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
|
427 |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
428 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
|
429 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
|
430 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
|
431 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
|
432 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
|
433 } else { |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
434 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
|
435 s->nb_coefs[ch], |
6003 | 436 psd[blk][ch], band_psd[blk][ch]); |
437 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
|
438 0, s->nb_coefs[ch], |
6003 | 439 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
|
440 ch == s->lfe_channel, |
5331 | 441 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
|
442 mask[blk][ch]); |
0cb02a723a62
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
jbr
parents:
4686
diff
changeset
|
443 } |
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 |
0 | 448 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
|
449 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
|
450 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
1064 | 451 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
6003 | 452 int frame_bits, int coarse_snr_offset, int fine_snr_offset) |
0 | 453 { |
454 int i, ch; | |
6003 | 455 int snr_offset; |
4705 | 456 |
6003 | 457 snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2; |
0 | 458 |
459 /* compute size */ | |
460 for(i=0;i<NB_BLOCKS;i++) { | |
461 s->mant1_cnt = 0; | |
462 s->mant2_cnt = 0; | |
463 s->mant4_cnt = 0; | |
314 | 464 for(ch=0;ch<s->nb_all_channels;ch++) { |
4706 | 465 ff_ac3_bit_alloc_calc_bap(mask[i][ch], psd[i][ch], 0, |
6003 | 466 s->nb_coefs[ch], snr_offset, |
7017 | 467 s->bit_alloc.floor, ff_ac3_bap_tab, |
468 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]; |
7129 | 494 static const 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 | |
9513 | 613 static av_cold int set_channel_info(AC3EncodeContext *s, int channels, |
614 int64_t *channel_layout) | |
615 { | |
616 int ch_layout; | |
617 | |
618 if (channels < 1 || channels > AC3_MAX_CHANNELS) | |
619 return -1; | |
620 if ((uint64_t)*channel_layout > 0x7FF) | |
621 return -1; | |
622 ch_layout = *channel_layout; | |
623 if (!ch_layout) | |
624 ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL); | |
625 if (avcodec_channel_layout_num_channels(ch_layout) != channels) | |
626 return -1; | |
627 | |
628 s->lfe = !!(ch_layout & CH_LOW_FREQUENCY); | |
629 s->nb_all_channels = channels; | |
630 s->nb_channels = channels - s->lfe; | |
631 s->lfe_channel = s->lfe ? s->nb_channels : -1; | |
632 if (s->lfe) | |
633 ch_layout -= CH_LOW_FREQUENCY; | |
634 | |
635 switch (ch_layout) { | |
636 case CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break; | |
637 case CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break; | |
638 case CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break; | |
639 case CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break; | |
640 case CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break; | |
641 case CH_LAYOUT_QUAD: | |
642 case CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break; | |
643 case CH_LAYOUT_5POINT0: | |
644 case CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break; | |
645 default: | |
646 return -1; | |
647 } | |
648 | |
649 s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe]; | |
650 *channel_layout = ch_layout; | |
651 if (s->lfe) | |
652 *channel_layout |= CH_LOW_FREQUENCY; | |
653 | |
654 return 0; | |
655 } | |
656 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6516
diff
changeset
|
657 static av_cold int AC3_encode_init(AVCodecContext *avctx) |
0 | 658 { |
659 int freq = avctx->sample_rate; | |
660 int bitrate = avctx->bit_rate; | |
661 AC3EncodeContext *s = avctx->priv_data; | |
782 | 662 int i, j, ch; |
0 | 663 float alpha; |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
664 int bw_code; |
0 | 665 |
666 avctx->frame_size = AC3_FRAME_SIZE; | |
2967 | 667 |
4645
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4643
diff
changeset
|
668 ac3_common_init(); |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4643
diff
changeset
|
669 |
9516
f15a920ce40e
ac3enc: log a warning message if the channel layout is not specified at
jbr
parents:
9513
diff
changeset
|
670 if (!avctx->channel_layout) { |
f15a920ce40e
ac3enc: log a warning message if the channel layout is not specified at
jbr
parents:
9513
diff
changeset
|
671 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The " |
f15a920ce40e
ac3enc: log a warning message if the channel layout is not specified at
jbr
parents:
9513
diff
changeset
|
672 "encoder will guess the layout, but it " |
f15a920ce40e
ac3enc: log a warning message if the channel layout is not specified at
jbr
parents:
9513
diff
changeset
|
673 "might be incorrect.\n"); |
f15a920ce40e
ac3enc: log a warning message if the channel layout is not specified at
jbr
parents:
9513
diff
changeset
|
674 } |
9513 | 675 if (set_channel_info(s, avctx->channels, &avctx->channel_layout)) { |
676 av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n"); | |
2979 | 677 return -1; |
9513 | 678 } |
0 | 679 |
680 /* frequency */ | |
681 for(i=0;i<3;i++) { | |
2967 | 682 for(j=0;j<3;j++) |
6002 | 683 if ((ff_ac3_sample_rate_tab[j] >> i) == freq) |
0 | 684 goto found; |
685 } | |
686 return -1; | |
2967 | 687 found: |
0 | 688 s->sample_rate = freq; |
6003 | 689 s->sr_shift = i; |
690 s->sr_code = j; | |
6005 | 691 s->bitstream_id = 8 + s->sr_shift; |
692 s->bitstream_mode = 0; /* complete main audio service */ | |
0 | 693 |
694 /* bitrate & frame size */ | |
695 for(i=0;i<19;i++) { | |
6082 | 696 if ((ff_ac3_bitrate_tab[i] >> s->sr_shift)*1000 == bitrate) |
0 | 697 break; |
698 } | |
699 if (i == 19) | |
700 return -1; | |
701 s->bit_rate = bitrate; | |
6005 | 702 s->frame_size_code = i << 1; |
703 s->frame_size_min = ff_ac3_frame_size_tab[s->frame_size_code][s->sr_code]; | |
3246 | 704 s->bits_written = 0; |
705 s->samples_written = 0; | |
0 | 706 s->frame_size = s->frame_size_min; |
2967 | 707 |
0 | 708 /* bit allocation init */ |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
709 if(avctx->cutoff) { |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
710 /* calculate bandwidth based on user-specified cutoff frequency */ |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
711 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
|
712 int fbw_coeffs = cutoff * 512 / s->sample_rate; |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
713 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
|
714 } else { |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
715 /* use default bandwidth setting */ |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
716 /* XXX: should compute the bandwidth according to the frame |
6516 | 717 size, so that we avoid annoying high frequency artifacts */ |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
718 bw_code = 50; |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
719 } |
0 | 720 for(ch=0;ch<s->nb_channels;ch++) { |
721 /* bandwidth for each channel */ | |
6083
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
722 s->chbwcod[ch] = bw_code; |
77d27412c35d
use cutoff frequency to adjust bandwidth in ac3 encoder
jbr
parents:
6082
diff
changeset
|
723 s->nb_coefs[ch] = bw_code * 3 + 73; |
0 | 724 } |
314 | 725 if (s->lfe) { |
2979 | 726 s->nb_coefs[s->lfe_channel] = 7; /* fixed */ |
314 | 727 } |
0 | 728 /* initial snr offset */ |
6003 | 729 s->coarse_snr_offset = 40; |
0 | 730 |
731 /* mdct init */ | |
732 fft_init(MDCT_NBITS - 2); | |
733 for(i=0;i<N/4;i++) { | |
734 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N; | |
735 xcos1[i] = fix15(-cos(alpha)); | |
736 xsin1[i] = fix15(-sin(alpha)); | |
737 } | |
738 | |
925 | 739 avctx->coded_frame= avcodec_alloc_frame(); |
740 avctx->coded_frame->key_frame= 1; | |
0 | 741 |
742 return 0; | |
743 } | |
744 | |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
745 /* output the AC-3 frame header */ |
0 | 746 static void output_frame_header(AC3EncodeContext *s, unsigned char *frame) |
747 { | |
1522
79dddc5cd990
removed the obsolete and unused parameters of init_put_bits
alex
parents:
1408
diff
changeset
|
748 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE); |
0 | 749 |
750 put_bits(&s->pb, 16, 0x0b77); /* frame header */ | |
751 put_bits(&s->pb, 16, 0); /* crc1: will be filled later */ | |
6003 | 752 put_bits(&s->pb, 2, s->sr_code); |
6005 | 753 put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min)); |
754 put_bits(&s->pb, 5, s->bitstream_id); | |
755 put_bits(&s->pb, 3, s->bitstream_mode); | |
756 put_bits(&s->pb, 3, s->channel_mode); | |
757 if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO) | |
2979 | 758 put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */ |
6005 | 759 if (s->channel_mode & 0x04) |
2979 | 760 put_bits(&s->pb, 2, 1); /* XXX -6 dB */ |
6005 | 761 if (s->channel_mode == AC3_CHMODE_STEREO) |
0 | 762 put_bits(&s->pb, 2, 0); /* surround not indicated */ |
314 | 763 put_bits(&s->pb, 1, s->lfe); /* LFE */ |
0 | 764 put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */ |
765 put_bits(&s->pb, 1, 0); /* no compression control word */ | |
766 put_bits(&s->pb, 1, 0); /* no lang code */ | |
767 put_bits(&s->pb, 1, 0); /* no audio production info */ | |
768 put_bits(&s->pb, 1, 0); /* no copyright */ | |
769 put_bits(&s->pb, 1, 1); /* original bitstream */ | |
770 put_bits(&s->pb, 1, 0); /* no time code 1 */ | |
771 put_bits(&s->pb, 1, 0); /* no time code 2 */ | |
6080 | 772 put_bits(&s->pb, 1, 0); /* no additional bit stream info */ |
0 | 773 } |
774 | |
775 /* symetric quantization on 'levels' levels */ | |
776 static inline int sym_quant(int c, int e, int levels) | |
777 { | |
778 int v; | |
779 | |
780 if (c >= 0) { | |
87 | 781 v = (levels * (c << e)) >> 24; |
782 v = (v + 1) >> 1; | |
0 | 783 v = (levels >> 1) + v; |
784 } else { | |
87 | 785 v = (levels * ((-c) << e)) >> 24; |
786 v = (v + 1) >> 1; | |
0 | 787 v = (levels >> 1) - v; |
788 } | |
789 assert (v >= 0 && v < levels); | |
790 return v; | |
791 } | |
792 | |
793 /* asymetric quantization on 2^qbits levels */ | |
794 static inline int asym_quant(int c, int e, int qbits) | |
795 { | |
796 int lshift, m, v; | |
797 | |
798 lshift = e + qbits - 24; | |
799 if (lshift >= 0) | |
800 v = c << lshift; | |
801 else | |
802 v = c >> (-lshift); | |
803 /* rounding */ | |
804 v = (v + 1) >> 1; | |
805 m = (1 << (qbits-1)); | |
806 if (v >= m) | |
807 v = m - 1; | |
808 assert(v >= -m); | |
809 return v & ((1 << qbits)-1); | |
810 } | |
811 | |
7470
1a93d3bbe3ee
cosmetics: make all references to AC-3 capitalized and hyphenated
jbr
parents:
7451
diff
changeset
|
812 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC-3 |
0 | 813 frame */ |
814 static void output_audio_block(AC3EncodeContext *s, | |
1064 | 815 uint8_t exp_strategy[AC3_MAX_CHANNELS], |
816 uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2], | |
817 uint8_t bap[AC3_MAX_CHANNELS][N/2], | |
818 int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2], | |
819 int8_t global_exp[AC3_MAX_CHANNELS], | |
0 | 820 int block_num) |
821 { | |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
822 int ch, nb_groups, group_size, i, baie, rbnd; |
1064 | 823 uint8_t *p; |
824 uint16_t qmant[AC3_MAX_CHANNELS][N/2]; | |
0 | 825 int exp0, exp1; |
826 int mant1_cnt, mant2_cnt, mant4_cnt; | |
1064 | 827 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; |
0 | 828 int delta0, delta1, delta2; |
829 | |
2967 | 830 for(ch=0;ch<s->nb_channels;ch++) |
0 | 831 put_bits(&s->pb, 1, 0); /* 512 point MDCT */ |
2967 | 832 for(ch=0;ch<s->nb_channels;ch++) |
0 | 833 put_bits(&s->pb, 1, 1); /* no dither */ |
834 put_bits(&s->pb, 1, 0); /* no dynamic range */ | |
835 if (block_num == 0) { | |
836 /* for block 0, even if no coupling, we must say it. This is a | |
837 waste of bit :-) */ | |
838 put_bits(&s->pb, 1, 1); /* coupling strategy present */ | |
839 put_bits(&s->pb, 1, 0); /* no coupling strategy */ | |
840 } else { | |
841 put_bits(&s->pb, 1, 0); /* no new coupling strategy */ | |
842 } | |
843 | |
6005 | 844 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
|
845 { |
2979 | 846 if(block_num==0) |
847 { | |
848 /* first block must define rematrixing (rematstr) */ | |
849 put_bits(&s->pb, 1, 1); | |
2967 | 850 |
2979 | 851 /* dummy rematrixing rematflg(1:4)=0 */ |
852 for (rbnd=0;rbnd<4;rbnd++) | |
853 put_bits(&s->pb, 1, 0); | |
854 } | |
855 else | |
856 { | |
857 /* no matrixing (but should be used in the future) */ | |
858 put_bits(&s->pb, 1, 0); | |
859 } | |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
860 } |
0 | 861 |
2967 | 862 #if defined(DEBUG) |
0 | 863 { |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
864 static int count = 0; |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
865 av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++); |
0 | 866 } |
867 #endif | |
868 /* exponent strategy */ | |
869 for(ch=0;ch<s->nb_channels;ch++) { | |
870 put_bits(&s->pb, 2, exp_strategy[ch]); | |
871 } | |
2967 | 872 |
314 | 873 if (s->lfe) { |
2979 | 874 put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]); |
314 | 875 } |
876 | |
0 | 877 for(ch=0;ch<s->nb_channels;ch++) { |
878 if (exp_strategy[ch] != EXP_REUSE) | |
879 put_bits(&s->pb, 6, s->chbwcod[ch]); | |
880 } | |
2967 | 881 |
0 | 882 /* exponents */ |
314 | 883 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 884 switch(exp_strategy[ch]) { |
885 case EXP_REUSE: | |
886 continue; | |
887 case EXP_D15: | |
888 group_size = 1; | |
889 break; | |
890 case EXP_D25: | |
891 group_size = 2; | |
892 break; | |
893 default: | |
894 case EXP_D45: | |
895 group_size = 4; | |
896 break; | |
897 } | |
2979 | 898 nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size); |
0 | 899 p = encoded_exp[ch]; |
900 | |
901 /* first exponent */ | |
902 exp1 = *p++; | |
903 put_bits(&s->pb, 4, exp1); | |
904 | |
905 /* next ones are delta encoded */ | |
906 for(i=0;i<nb_groups;i++) { | |
907 /* merge three delta in one code */ | |
908 exp0 = exp1; | |
909 exp1 = p[0]; | |
910 p += group_size; | |
911 delta0 = exp1 - exp0 + 2; | |
912 | |
913 exp0 = exp1; | |
914 exp1 = p[0]; | |
915 p += group_size; | |
916 delta1 = exp1 - exp0 + 2; | |
917 | |
918 exp0 = exp1; | |
919 exp1 = p[0]; | |
920 p += group_size; | |
921 delta2 = exp1 - exp0 + 2; | |
922 | |
923 put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2); | |
924 } | |
925 | |
2979 | 926 if (ch != s->lfe_channel) |
927 put_bits(&s->pb, 2, 0); /* no gain range info */ | |
0 | 928 } |
929 | |
930 /* bit allocation info */ | |
931 baie = (block_num == 0); | |
932 put_bits(&s->pb, 1, baie); | |
933 if (baie) { | |
6003 | 934 put_bits(&s->pb, 2, s->slow_decay_code); |
935 put_bits(&s->pb, 2, s->fast_decay_code); | |
936 put_bits(&s->pb, 2, s->slow_gain_code); | |
937 put_bits(&s->pb, 2, s->db_per_bit_code); | |
938 put_bits(&s->pb, 3, s->floor_code); | |
0 | 939 } |
940 | |
941 /* snr offset */ | |
942 put_bits(&s->pb, 1, baie); /* always present with bai */ | |
943 if (baie) { | |
6003 | 944 put_bits(&s->pb, 6, s->coarse_snr_offset); |
314 | 945 for(ch=0;ch<s->nb_all_channels;ch++) { |
6003 | 946 put_bits(&s->pb, 4, s->fine_snr_offset[ch]); |
947 put_bits(&s->pb, 3, s->fast_gain_code[ch]); | |
0 | 948 } |
949 } | |
2967 | 950 |
0 | 951 put_bits(&s->pb, 1, 0); /* no delta bit allocation */ |
952 put_bits(&s->pb, 1, 0); /* no data to skip */ | |
953 | |
954 /* mantissa encoding : we use two passes to handle the grouping. A | |
955 one pass method may be faster, but it would necessitate to | |
956 modify the output stream. */ | |
957 | |
958 /* first pass: quantize */ | |
959 mant1_cnt = mant2_cnt = mant4_cnt = 0; | |
960 qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL; | |
961 | |
314 | 962 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 963 int b, c, e, v; |
964 | |
965 for(i=0;i<s->nb_coefs[ch];i++) { | |
966 c = mdct_coefs[ch][i]; | |
967 e = encoded_exp[ch][i] - global_exp[ch]; | |
968 b = bap[ch][i]; | |
969 switch(b) { | |
970 case 0: | |
971 v = 0; | |
972 break; | |
973 case 1: | |
974 v = sym_quant(c, e, 3); | |
975 switch(mant1_cnt) { | |
976 case 0: | |
977 qmant1_ptr = &qmant[ch][i]; | |
978 v = 9 * v; | |
979 mant1_cnt = 1; | |
980 break; | |
981 case 1: | |
982 *qmant1_ptr += 3 * v; | |
983 mant1_cnt = 2; | |
984 v = 128; | |
985 break; | |
986 default: | |
987 *qmant1_ptr += v; | |
988 mant1_cnt = 0; | |
989 v = 128; | |
990 break; | |
991 } | |
992 break; | |
993 case 2: | |
994 v = sym_quant(c, e, 5); | |
995 switch(mant2_cnt) { | |
996 case 0: | |
997 qmant2_ptr = &qmant[ch][i]; | |
998 v = 25 * v; | |
999 mant2_cnt = 1; | |
1000 break; | |
1001 case 1: | |
1002 *qmant2_ptr += 5 * v; | |
1003 mant2_cnt = 2; | |
1004 v = 128; | |
1005 break; | |
1006 default: | |
1007 *qmant2_ptr += v; | |
1008 mant2_cnt = 0; | |
1009 v = 128; | |
1010 break; | |
1011 } | |
1012 break; | |
1013 case 3: | |
1014 v = sym_quant(c, e, 7); | |
1015 break; | |
1016 case 4: | |
1017 v = sym_quant(c, e, 11); | |
1018 switch(mant4_cnt) { | |
1019 case 0: | |
1020 qmant4_ptr = &qmant[ch][i]; | |
1021 v = 11 * v; | |
1022 mant4_cnt = 1; | |
1023 break; | |
1024 default: | |
1025 *qmant4_ptr += v; | |
1026 mant4_cnt = 0; | |
1027 v = 128; | |
1028 break; | |
1029 } | |
1030 break; | |
1031 case 5: | |
1032 v = sym_quant(c, e, 15); | |
1033 break; | |
1034 case 14: | |
1035 v = asym_quant(c, e, 14); | |
1036 break; | |
1037 case 15: | |
1038 v = asym_quant(c, e, 16); | |
1039 break; | |
1040 default: | |
1041 v = asym_quant(c, e, b - 1); | |
1042 break; | |
1043 } | |
1044 qmant[ch][i] = v; | |
1045 } | |
1046 } | |
1047 | |
1048 /* second pass : output the values */ | |
314 | 1049 for (ch = 0; ch < s->nb_all_channels; ch++) { |
0 | 1050 int b, q; |
2967 | 1051 |
0 | 1052 for(i=0;i<s->nb_coefs[ch];i++) { |
1053 q = qmant[ch][i]; | |
1054 b = bap[ch][i]; | |
1055 switch(b) { | |
1056 case 0: | |
1057 break; | |
1058 case 1: | |
2967 | 1059 if (q != 128) |
0 | 1060 put_bits(&s->pb, 5, q); |
1061 break; | |
1062 case 2: | |
2967 | 1063 if (q != 128) |
0 | 1064 put_bits(&s->pb, 7, q); |
1065 break; | |
1066 case 3: | |
1067 put_bits(&s->pb, 3, q); | |
1068 break; | |
1069 case 4: | |
1070 if (q != 128) | |
1071 put_bits(&s->pb, 7, q); | |
1072 break; | |
1073 case 14: | |
1074 put_bits(&s->pb, 14, q); | |
1075 break; | |
1076 case 15: | |
1077 put_bits(&s->pb, 16, q); | |
1078 break; | |
1079 default: | |
1080 put_bits(&s->pb, b - 1, q); | |
1081 break; | |
1082 } | |
1083 } | |
1084 } | |
1085 } | |
1086 | |
1087 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) | |
1088 | |
1089 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly) | |
1090 { | |
1091 unsigned int c; | |
1092 | |
1093 c = 0; | |
1094 while (a) { | |
1095 if (a & 1) | |
1096 c ^= b; | |
1097 a = a >> 1; | |
1098 b = b << 1; | |
1099 if (b & (1 << 16)) | |
1100 b ^= poly; | |
1101 } | |
1102 return c; | |
1103 } | |
1104 | |
1105 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly) | |
1106 { | |
1107 unsigned int r; | |
1108 r = 1; | |
1109 while (n) { | |
1110 if (n & 1) | |
1111 r = mul_poly(r, a, poly); | |
1112 a = mul_poly(a, a, poly); | |
1113 n >>= 1; | |
1114 } | |
1115 return r; | |
1116 } | |
1117 | |
1118 | |
1119 /* compute log2(max(abs(tab[]))) */ | |
1064 | 1120 static int log2_tab(int16_t *tab, int n) |
0 | 1121 { |
1122 int i, v; | |
1123 | |
1124 v = 0; | |
1125 for(i=0;i<n;i++) { | |
1126 v |= abs(tab[i]); | |
1127 } | |
65 | 1128 return av_log2(v); |
0 | 1129 } |
1130 | |
1064 | 1131 static void lshift_tab(int16_t *tab, int n, int lshift) |
0 | 1132 { |
1133 int i; | |
1134 | |
1135 if (lshift > 0) { | |
1136 for(i=0;i<n;i++) { | |
1137 tab[i] <<= lshift; | |
1138 } | |
1139 } else if (lshift < 0) { | |
1140 lshift = -lshift; | |
1141 for(i=0;i<n;i++) { | |
1142 tab[i] >>= lshift; | |
1143 } | |
1144 } | |
1145 } | |
1146 | |
1147 /* fill the end of the frame and compute the two crcs */ | |
1148 static int output_frame_end(AC3EncodeContext *s) | |
1149 { | |
1150 int frame_size, frame_size_58, n, crc1, crc2, crc_inv; | |
1064 | 1151 uint8_t *frame; |
0 | 1152 |
1153 frame_size = s->frame_size; /* frame size in words */ | |
1154 /* align to 8 bits */ | |
1155 flush_put_bits(&s->pb); | |
1156 /* add zero bytes to reach the frame size */ | |
1157 frame = s->pb.buf; | |
9431 | 1158 n = 2 * s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2; |
0 | 1159 assert(n >= 0); |
1408
4d67eb341a0c
AC3 encoding patch ba (Ross Martin <ffmpeg at ross dot interwrx dot com>)
michaelni
parents:
1106
diff
changeset
|
1160 if(n>0) |
9431 | 1161 memset(put_bits_ptr(&s->pb), 0, n); |
2967 | 1162 |
0 | 1163 /* Now we must compute both crcs : this is not so easy for crc1 |
1164 because it is at the beginning of the data... */ | |
1165 frame_size_58 = (frame_size >> 1) + (frame_size >> 3); | |
6108 | 1166 crc1 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
1167 frame + 4, 2 * frame_size_58 - 4)); | |
0 | 1168 /* XXX: could precompute crc_inv */ |
1169 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY); | |
1170 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); | |
5089 | 1171 AV_WB16(frame+2,crc1); |
2967 | 1172 |
6108 | 1173 crc2 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, |
1174 frame + 2 * frame_size_58, | |
1175 (frame_size - frame_size_58) * 2 - 2)); | |
5089 | 1176 AV_WB16(frame+2*frame_size-2,crc2); |
0 | 1177 |
1178 // printf("n=%d frame_size=%d\n", n, frame_size); | |
1179 return frame_size * 2; | |
1180 } | |
1181 | |
782 | 1182 static int AC3_encode_frame(AVCodecContext *avctx, |
1183 unsigned char *frame, int buf_size, void *data) | |
0 | 1184 { |
1185 AC3EncodeContext *s = avctx->priv_data; | |
2367
c353719836af
fix some type mismatches patch by (Jeff Muizelaar <muizelaar rogers com>)
michael
parents:
2157
diff
changeset
|
1186 int16_t *samples = data; |
0 | 1187 int i, j, k, v, ch; |
1064 | 1188 int16_t input_samples[N]; |
1189 int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1190 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1191 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]; | |
1192 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1193 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
1194 int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS]; | |
0 | 1195 int frame_bits; |
1196 | |
1197 frame_bits = 0; | |
314 | 1198 for(ch=0;ch<s->nb_all_channels;ch++) { |
9452 | 1199 int ich = s->channel_map[ch]; |
0 | 1200 /* fixed mdct to the six sub blocks & exponent computation */ |
1201 for(i=0;i<NB_BLOCKS;i++) { | |
1064 | 1202 int16_t *sptr; |
0 | 1203 int sinc; |
1204 | |
1205 /* compute input samples */ | |
9452 | 1206 memcpy(input_samples, s->last_samples[ich], N/2 * sizeof(int16_t)); |
314 | 1207 sinc = s->nb_all_channels; |
9452 | 1208 sptr = samples + (sinc * (N/2) * i) + ich; |
0 | 1209 for(j=0;j<N/2;j++) { |
1210 v = *sptr; | |
1211 input_samples[j + N/2] = v; | |
9452 | 1212 s->last_samples[ich][j] = v; |
0 | 1213 sptr += sinc; |
1214 } | |
1215 | |
1216 /* apply the MDCT window */ | |
1217 for(j=0;j<N/2;j++) { | |
2967 | 1218 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
|
1219 ff_ac3_window[j]) >> 15; |
2967 | 1220 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
|
1221 ff_ac3_window[j]) >> 15; |
0 | 1222 } |
2967 | 1223 |
0 | 1224 /* Normalize the samples to use the maximum available |
1225 precision */ | |
1226 v = 14 - log2_tab(input_samples, N); | |
1227 if (v < 0) | |
1228 v = 0; | |
4686
3a891d2379ce
AC-3 encoded volume is too high. Revert revision 7160:
jbr
parents:
4645
diff
changeset
|
1229 exp_samples[i][ch] = v - 9; |
0 | 1230 lshift_tab(input_samples, N, v); |
1231 | |
1232 /* do the MDCT */ | |
1233 mdct512(mdct_coef[i][ch], input_samples); | |
2967 | 1234 |
0 | 1235 /* compute "exponents". We take into account the |
1236 normalization there */ | |
1237 for(j=0;j<N/2;j++) { | |
1238 int e; | |
1239 v = abs(mdct_coef[i][ch][j]); | |
1240 if (v == 0) | |
1241 e = 24; | |
1242 else { | |
65 | 1243 e = 23 - av_log2(v) + exp_samples[i][ch]; |
0 | 1244 if (e >= 24) { |
1245 e = 24; | |
1246 mdct_coef[i][ch][j] = 0; | |
1247 } | |
1248 } | |
1249 exp[i][ch][j] = e; | |
1250 } | |
1251 } | |
2967 | 1252 |
314 | 1253 compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel); |
0 | 1254 |
1255 /* compute the exponents as the decoder will see them. The | |
1256 EXP_REUSE case must be handled carefully : we select the | |
1257 min of the exponents */ | |
1258 i = 0; | |
1259 while (i < NB_BLOCKS) { | |
1260 j = i + 1; | |
1261 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) { | |
1262 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]); | |
1263 j++; | |
1264 } | |
1265 frame_bits += encode_exp(encoded_exp[i][ch], | |
2967 | 1266 exp[i][ch], s->nb_coefs[ch], |
0 | 1267 exp_strategy[i][ch]); |
1268 /* copy encoded exponents for reuse case */ | |
1269 for(k=i+1;k<j;k++) { | |
2967 | 1270 memcpy(encoded_exp[k][ch], encoded_exp[i][ch], |
1064 | 1271 s->nb_coefs[ch] * sizeof(uint8_t)); |
0 | 1272 } |
1273 i = j; | |
1274 } | |
1275 } | |
1276 | |
3246 | 1277 /* adjust for fractional frame sizes */ |
6082 | 1278 while(s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) { |
1279 s->bits_written -= s->bit_rate; | |
3246 | 1280 s->samples_written -= s->sample_rate; |
1281 } | |
6082 | 1282 s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate); |
3246 | 1283 s->bits_written += s->frame_size * 16; |
1284 s->samples_written += AC3_FRAME_SIZE; | |
1285 | |
0 | 1286 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits); |
1287 /* everything is known... let's output the frame */ | |
1288 output_frame_header(s, frame); | |
2967 | 1289 |
0 | 1290 for(i=0;i<NB_BLOCKS;i++) { |
2967 | 1291 output_audio_block(s, exp_strategy[i], encoded_exp[i], |
0 | 1292 bap[i], mdct_coef[i], exp_samples[i], i); |
1293 } | |
1294 return output_frame_end(s); | |
1295 } | |
1296 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6516
diff
changeset
|
1297 static av_cold int AC3_encode_close(AVCodecContext *avctx) |
925 | 1298 { |
1299 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
|
1300 return 0; |
925 | 1301 } |
1302 | |
0 | 1303 #if 0 |
1304 /*************************************************************************/ | |
1305 /* TEST */ | |
1306 | |
5380
389366aa3458
Fix the self tests which are contained in some codecs and are using random().
takis
parents:
5331
diff
changeset
|
1307 #undef random |
0 | 1308 #define FN (N/4) |
1309 | |
1310 void fft_test(void) | |
1311 { | |
1312 IComplex in[FN], in1[FN]; | |
1313 int k, n, i; | |
1314 float sum_re, sum_im, a; | |
1315 | |
1316 /* FFT test */ | |
1317 | |
1318 for(i=0;i<FN;i++) { | |
1319 in[i].re = random() % 65535 - 32767; | |
1320 in[i].im = random() % 65535 - 32767; | |
1321 in1[i] = in[i]; | |
1322 } | |
1323 fft(in, 7); | |
1324 | |
1325 /* do it by hand */ | |
1326 for(k=0;k<FN;k++) { | |
1327 sum_re = 0; | |
1328 sum_im = 0; | |
1329 for(n=0;n<FN;n++) { | |
1330 a = -2 * M_PI * (n * k) / FN; | |
1331 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a); | |
1332 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a); | |
1333 } | |
2967 | 1334 printf("%3d: %6d,%6d %6.0f,%6.0f\n", |
1335 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN); | |
0 | 1336 } |
1337 } | |
1338 | |
1339 void mdct_test(void) | |
1340 { | |
1064 | 1341 int16_t input[N]; |
1342 int32_t output[N/2]; | |
0 | 1343 float input1[N]; |
1344 float output1[N/2]; | |
1345 float s, a, err, e, emax; | |
1346 int i, k, n; | |
1347 | |
1348 for(i=0;i<N;i++) { | |
1349 input[i] = (random() % 65535 - 32767) * 9 / 10; | |
1350 input1[i] = input[i]; | |
1351 } | |
1352 | |
1353 mdct512(output, input); | |
2967 | 1354 |
0 | 1355 /* do it by hand */ |
1356 for(k=0;k<N/2;k++) { | |
1357 s = 0; | |
1358 for(n=0;n<N;n++) { | |
1359 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N)); | |
1360 s += input1[n] * cos(a); | |
1361 } | |
1362 output1[k] = -2 * s / N; | |
1363 } | |
2967 | 1364 |
0 | 1365 err = 0; |
1366 emax = 0; | |
1367 for(i=0;i<N/2;i++) { | |
1368 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]); | |
1369 e = output[i] - output1[i]; | |
1370 if (e > emax) | |
1371 emax = e; | |
1372 err += e * e; | |
1373 } | |
1374 printf("err2=%f emax=%f\n", err / (N/2), emax); | |
1375 } | |
1376 | |
1377 void test_ac3(void) | |
1378 { | |
1379 AC3EncodeContext ctx; | |
1380 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE]; | |
1381 short samples[AC3_FRAME_SIZE]; | |
1382 int ret, i; | |
2967 | 1383 |
0 | 1384 AC3_encode_init(&ctx, 44100, 64000, 1); |
1385 | |
1386 fft_test(); | |
1387 mdct_test(); | |
1388 | |
1389 for(i=0;i<AC3_FRAME_SIZE;i++) | |
1390 samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000); | |
1391 ret = AC3_encode_frame(&ctx, frame, samples); | |
1392 printf("ret=%d\n", ret); | |
1393 } | |
1394 #endif | |
1395 | |
1396 AVCodec ac3_encoder = { | |
1397 "ac3", | |
1398 CODEC_TYPE_AUDIO, | |
1399 CODEC_ID_AC3, | |
1400 sizeof(AC3EncodeContext), | |
1401 AC3_encode_init, | |
1402 AC3_encode_frame, | |
925 | 1403 AC3_encode_close, |
0 | 1404 NULL, |
7451
85ab7655ad4d
Modify all codecs to report their supported input and output sample format(s).
pross
parents:
7129
diff
changeset
|
1405 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, |
7769 | 1406 .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"), |
9513 | 1407 .channel_layouts = (int64_t[]){ |
1408 CH_LAYOUT_MONO, | |
1409 CH_LAYOUT_STEREO, | |
1410 CH_LAYOUT_2_1, | |
1411 CH_LAYOUT_SURROUND, | |
1412 CH_LAYOUT_2_2, | |
1413 CH_LAYOUT_QUAD, | |
1414 CH_LAYOUT_4POINT0, | |
1415 CH_LAYOUT_5POINT0, | |
1416 CH_LAYOUT_5POINT0_BACK, | |
1417 (CH_LAYOUT_MONO | CH_LOW_FREQUENCY), | |
1418 (CH_LAYOUT_STEREO | CH_LOW_FREQUENCY), | |
1419 (CH_LAYOUT_2_1 | CH_LOW_FREQUENCY), | |
1420 (CH_LAYOUT_SURROUND | CH_LOW_FREQUENCY), | |
1421 (CH_LAYOUT_2_2 | CH_LOW_FREQUENCY), | |
1422 (CH_LAYOUT_QUAD | CH_LOW_FREQUENCY), | |
1423 (CH_LAYOUT_4POINT0 | CH_LOW_FREQUENCY), | |
1424 CH_LAYOUT_5POINT1, | |
1425 CH_LAYOUT_5POINT1_BACK, | |
1426 0 }, | |
0 | 1427 }; |