Mercurial > libavcodec.hg
annotate mpegaudio.c @ 2091:02a4fd7c606c libavcodec
flush audio encoder buffers at the end
fix vorbis in nut again
author | michael |
---|---|
date | Tue, 22 Jun 2004 21:14:01 +0000 |
parents | fdb8244da1e5 |
children | 6f44ab5ad03c |
rev | line source |
---|---|
0 | 1 /* |
2 * The simplest mpeg audio layer 2 encoder | |
429 | 3 * Copyright (c) 2000, 2001 Fabrice Bellard. |
0 | 4 * |
429 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
0 | 18 */ |
1106 | 19 |
20 /** | |
21 * @file mpegaudio.c | |
22 * The simplest mpeg audio layer 2 encoder. | |
23 */ | |
24 | |
64 | 25 #include "avcodec.h" |
0 | 26 #include "mpegaudio.h" |
27 | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
28 /* currently, cannot change these constants (need to modify |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
29 quantization stage) */ |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
30 #define FRAC_BITS 15 |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
31 #define WFRAC_BITS 14 |
1064 | 32 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS) |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
33 #define FIX(a) ((int)((a) * (1 << FRAC_BITS))) |
84 | 34 |
35 #define SAMPLES_BUF_SIZE 4096 | |
36 | |
37 typedef struct MpegAudioContext { | |
38 PutBitContext pb; | |
39 int nb_channels; | |
40 int freq, bit_rate; | |
41 int lsf; /* 1 if mpeg2 low bitrate selected */ | |
42 int bitrate_index; /* bit rate */ | |
43 int freq_index; | |
44 int frame_size; /* frame size, in bits, without padding */ | |
1064 | 45 int64_t nb_samples; /* total number of samples encoded */ |
84 | 46 /* padding computation */ |
47 int frame_frac, frame_frac_incr, do_padding; | |
48 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */ | |
49 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */ | |
50 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]; | |
51 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */ | |
52 /* code to group 3 scale factors */ | |
53 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; | |
54 int sblimit; /* number of used subbands */ | |
55 const unsigned char *alloc_table; | |
56 } MpegAudioContext; | |
57 | |
0 | 58 /* define it to use floats in quantization (I don't like floats !) */ |
59 //#define USE_FLOATS | |
60 | |
61 #include "mpegaudiotab.h" | |
62 | |
1057 | 63 static int MPA_encode_init(AVCodecContext *avctx) |
0 | 64 { |
65 MpegAudioContext *s = avctx->priv_data; | |
66 int freq = avctx->sample_rate; | |
67 int bitrate = avctx->bit_rate; | |
68 int channels = avctx->channels; | |
84 | 69 int i, v, table; |
0 | 70 float a; |
71 | |
72 if (channels > 2) | |
73 return -1; | |
74 bitrate = bitrate / 1000; | |
75 s->nb_channels = channels; | |
76 s->freq = freq; | |
77 s->bit_rate = bitrate * 1000; | |
78 avctx->frame_size = MPA_FRAME_SIZE; | |
79 | |
80 /* encoding freq */ | |
81 s->lsf = 0; | |
82 for(i=0;i<3;i++) { | |
84 | 83 if (mpa_freq_tab[i] == freq) |
0 | 84 break; |
84 | 85 if ((mpa_freq_tab[i] / 2) == freq) { |
0 | 86 s->lsf = 1; |
87 break; | |
88 } | |
89 } | |
90 if (i == 3) | |
91 return -1; | |
92 s->freq_index = i; | |
93 | |
94 /* encoding bitrate & frequency */ | |
95 for(i=0;i<15;i++) { | |
84 | 96 if (mpa_bitrate_tab[s->lsf][1][i] == bitrate) |
0 | 97 break; |
98 } | |
99 if (i == 15) | |
100 return -1; | |
101 s->bitrate_index = i; | |
102 | |
103 /* compute total header size & pad bit */ | |
104 | |
105 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0); | |
106 s->frame_size = ((int)a) * 8; | |
107 | |
108 /* frame fractional size to compute padding */ | |
109 s->frame_frac = 0; | |
110 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0); | |
111 | |
112 /* select the right allocation table */ | |
84 | 113 table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf); |
114 | |
0 | 115 /* number of used subbands */ |
116 s->sblimit = sblimit_table[table]; | |
117 s->alloc_table = alloc_tables[table]; | |
118 | |
119 #ifdef DEBUG | |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
120 av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", |
0 | 121 bitrate, freq, s->frame_size, table, s->frame_frac_incr); |
122 #endif | |
123 | |
124 for(i=0;i<s->nb_channels;i++) | |
125 s->samples_offset[i] = 0; | |
126 | |
84 | 127 for(i=0;i<257;i++) { |
128 int v; | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
129 v = mpa_enwindow[i]; |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
130 #if WFRAC_BITS != 16 |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
131 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
132 #endif |
84 | 133 filter_bank[i] = v; |
134 if ((i & 63) != 0) | |
135 v = -v; | |
136 if (i != 0) | |
137 filter_bank[512 - i] = v; | |
0 | 138 } |
84 | 139 |
0 | 140 for(i=0;i<64;i++) { |
141 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20)); | |
142 if (v <= 0) | |
143 v = 1; | |
144 scale_factor_table[i] = v; | |
145 #ifdef USE_FLOATS | |
146 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20); | |
147 #else | |
148 #define P 15 | |
149 scale_factor_shift[i] = 21 - P - (i / 3); | |
150 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0); | |
151 #endif | |
152 } | |
153 for(i=0;i<128;i++) { | |
154 v = i - 64; | |
155 if (v <= -3) | |
156 v = 0; | |
157 else if (v < 0) | |
158 v = 1; | |
159 else if (v == 0) | |
160 v = 2; | |
161 else if (v < 3) | |
162 v = 3; | |
163 else | |
164 v = 4; | |
165 scale_diff_table[i] = v; | |
166 } | |
167 | |
168 for(i=0;i<17;i++) { | |
169 v = quant_bits[i]; | |
170 if (v < 0) | |
171 v = -v; | |
172 else | |
173 v = v * 3; | |
174 total_quant_bits[i] = 12 * v; | |
175 } | |
176 | |
925 | 177 avctx->coded_frame= avcodec_alloc_frame(); |
178 avctx->coded_frame->key_frame= 1; | |
179 | |
0 | 180 return 0; |
181 } | |
182 | |
84 | 183 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */ |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
184 static void idct32(int *out, int *tab) |
0 | 185 { |
186 int i, j; | |
187 int *t, *t1, xr; | |
188 const int *xp = costab32; | |
189 | |
190 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2]; | |
191 | |
192 t = tab + 30; | |
193 t1 = tab + 2; | |
194 do { | |
195 t[0] += t[-4]; | |
196 t[1] += t[1 - 4]; | |
197 t -= 4; | |
198 } while (t != t1); | |
199 | |
200 t = tab + 28; | |
201 t1 = tab + 4; | |
202 do { | |
203 t[0] += t[-8]; | |
204 t[1] += t[1-8]; | |
205 t[2] += t[2-8]; | |
206 t[3] += t[3-8]; | |
207 t -= 8; | |
208 } while (t != t1); | |
209 | |
210 t = tab; | |
211 t1 = tab + 32; | |
212 do { | |
213 t[ 3] = -t[ 3]; | |
214 t[ 6] = -t[ 6]; | |
215 | |
216 t[11] = -t[11]; | |
217 t[12] = -t[12]; | |
218 t[13] = -t[13]; | |
219 t[15] = -t[15]; | |
220 t += 16; | |
221 } while (t != t1); | |
222 | |
223 | |
224 t = tab; | |
225 t1 = tab + 8; | |
226 do { | |
227 int x1, x2, x3, x4; | |
228 | |
229 x3 = MUL(t[16], FIX(SQRT2*0.5)); | |
230 x4 = t[0] - x3; | |
231 x3 = t[0] + x3; | |
232 | |
233 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5)); | |
234 x1 = MUL((t[8] - x2), xp[0]); | |
235 x2 = MUL((t[8] + x2), xp[1]); | |
236 | |
237 t[ 0] = x3 + x1; | |
238 t[ 8] = x4 - x2; | |
239 t[16] = x4 + x2; | |
240 t[24] = x3 - x1; | |
241 t++; | |
242 } while (t != t1); | |
243 | |
244 xp += 2; | |
245 t = tab; | |
246 t1 = tab + 4; | |
247 do { | |
248 xr = MUL(t[28],xp[0]); | |
249 t[28] = (t[0] - xr); | |
250 t[0] = (t[0] + xr); | |
251 | |
252 xr = MUL(t[4],xp[1]); | |
253 t[ 4] = (t[24] - xr); | |
254 t[24] = (t[24] + xr); | |
255 | |
256 xr = MUL(t[20],xp[2]); | |
257 t[20] = (t[8] - xr); | |
258 t[ 8] = (t[8] + xr); | |
259 | |
260 xr = MUL(t[12],xp[3]); | |
261 t[12] = (t[16] - xr); | |
262 t[16] = (t[16] + xr); | |
263 t++; | |
264 } while (t != t1); | |
265 xp += 4; | |
266 | |
267 for (i = 0; i < 4; i++) { | |
268 xr = MUL(tab[30-i*4],xp[0]); | |
269 tab[30-i*4] = (tab[i*4] - xr); | |
270 tab[ i*4] = (tab[i*4] + xr); | |
271 | |
272 xr = MUL(tab[ 2+i*4],xp[1]); | |
273 tab[ 2+i*4] = (tab[28-i*4] - xr); | |
274 tab[28-i*4] = (tab[28-i*4] + xr); | |
275 | |
276 xr = MUL(tab[31-i*4],xp[0]); | |
277 tab[31-i*4] = (tab[1+i*4] - xr); | |
278 tab[ 1+i*4] = (tab[1+i*4] + xr); | |
279 | |
280 xr = MUL(tab[ 3+i*4],xp[1]); | |
281 tab[ 3+i*4] = (tab[29-i*4] - xr); | |
282 tab[29-i*4] = (tab[29-i*4] + xr); | |
283 | |
284 xp += 2; | |
285 } | |
286 | |
287 t = tab + 30; | |
288 t1 = tab + 1; | |
289 do { | |
290 xr = MUL(t1[0], *xp); | |
291 t1[0] = (t[0] - xr); | |
292 t[0] = (t[0] + xr); | |
293 t -= 2; | |
294 t1 += 2; | |
295 xp++; | |
296 } while (t >= tab); | |
297 | |
298 for(i=0;i<32;i++) { | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
299 out[i] = tab[bitinv32[i]]; |
0 | 300 } |
301 } | |
302 | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
303 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS) |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
304 |
0 | 305 static void filter(MpegAudioContext *s, int ch, short *samples, int incr) |
306 { | |
307 short *p, *q; | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
308 int sum, offset, i, j; |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
309 int tmp[64]; |
0 | 310 int tmp1[32]; |
311 int *out; | |
312 | |
313 // print_pow1(samples, 1152); | |
314 | |
315 offset = s->samples_offset[ch]; | |
316 out = &s->sb_samples[ch][0][0][0]; | |
317 for(j=0;j<36;j++) { | |
318 /* 32 samples at once */ | |
319 for(i=0;i<32;i++) { | |
320 s->samples_buf[ch][offset + (31 - i)] = samples[0]; | |
321 samples += incr; | |
322 } | |
323 | |
324 /* filter */ | |
325 p = s->samples_buf[ch] + offset; | |
326 q = filter_bank; | |
327 /* maxsum = 23169 */ | |
328 for(i=0;i<64;i++) { | |
329 sum = p[0*64] * q[0*64]; | |
330 sum += p[1*64] * q[1*64]; | |
331 sum += p[2*64] * q[2*64]; | |
332 sum += p[3*64] * q[3*64]; | |
333 sum += p[4*64] * q[4*64]; | |
334 sum += p[5*64] * q[5*64]; | |
335 sum += p[6*64] * q[6*64]; | |
336 sum += p[7*64] * q[7*64]; | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
337 tmp[i] = sum; |
0 | 338 p++; |
339 q++; | |
340 } | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
341 tmp1[0] = tmp[16] >> WSHIFT; |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
342 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT; |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
343 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT; |
0 | 344 |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
345 idct32(out, tmp1); |
0 | 346 |
347 /* advance of 32 samples */ | |
348 offset -= 32; | |
349 out += 32; | |
350 /* handle the wrap around */ | |
351 if (offset < 0) { | |
352 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32), | |
353 s->samples_buf[ch], (512 - 32) * 2); | |
354 offset = SAMPLES_BUF_SIZE - 512; | |
355 } | |
356 } | |
357 s->samples_offset[ch] = offset; | |
358 | |
359 // print_pow(s->sb_samples, 1152); | |
360 } | |
361 | |
362 static void compute_scale_factors(unsigned char scale_code[SBLIMIT], | |
363 unsigned char scale_factors[SBLIMIT][3], | |
364 int sb_samples[3][12][SBLIMIT], | |
365 int sblimit) | |
366 { | |
367 int *p, vmax, v, n, i, j, k, code; | |
368 int index, d1, d2; | |
369 unsigned char *sf = &scale_factors[0][0]; | |
370 | |
371 for(j=0;j<sblimit;j++) { | |
372 for(i=0;i<3;i++) { | |
373 /* find the max absolute value */ | |
374 p = &sb_samples[i][0][j]; | |
375 vmax = abs(*p); | |
376 for(k=1;k<12;k++) { | |
377 p += SBLIMIT; | |
378 v = abs(*p); | |
379 if (v > vmax) | |
380 vmax = v; | |
381 } | |
382 /* compute the scale factor index using log 2 computations */ | |
383 if (vmax > 0) { | |
70 | 384 n = av_log2(vmax); |
0 | 385 /* n is the position of the MSB of vmax. now |
386 use at most 2 compares to find the index */ | |
387 index = (21 - n) * 3 - 3; | |
388 if (index >= 0) { | |
389 while (vmax <= scale_factor_table[index+1]) | |
390 index++; | |
391 } else { | |
392 index = 0; /* very unlikely case of overflow */ | |
393 } | |
394 } else { | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
395 index = 62; /* value 63 is not allowed */ |
0 | 396 } |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
397 |
0 | 398 #if 0 |
399 printf("%2d:%d in=%x %x %d\n", | |
400 j, i, vmax, scale_factor_table[index], index); | |
401 #endif | |
402 /* store the scale factor */ | |
403 assert(index >=0 && index <= 63); | |
404 sf[i] = index; | |
405 } | |
406 | |
407 /* compute the transmission factor : look if the scale factors | |
408 are close enough to each other */ | |
409 d1 = scale_diff_table[sf[0] - sf[1] + 64]; | |
410 d2 = scale_diff_table[sf[1] - sf[2] + 64]; | |
411 | |
412 /* handle the 25 cases */ | |
413 switch(d1 * 5 + d2) { | |
414 case 0*5+0: | |
415 case 0*5+4: | |
416 case 3*5+4: | |
417 case 4*5+0: | |
418 case 4*5+4: | |
419 code = 0; | |
420 break; | |
421 case 0*5+1: | |
422 case 0*5+2: | |
423 case 4*5+1: | |
424 case 4*5+2: | |
425 code = 3; | |
426 sf[2] = sf[1]; | |
427 break; | |
428 case 0*5+3: | |
429 case 4*5+3: | |
430 code = 3; | |
431 sf[1] = sf[2]; | |
432 break; | |
433 case 1*5+0: | |
434 case 1*5+4: | |
435 case 2*5+4: | |
436 code = 1; | |
437 sf[1] = sf[0]; | |
438 break; | |
439 case 1*5+1: | |
440 case 1*5+2: | |
441 case 2*5+0: | |
442 case 2*5+1: | |
443 case 2*5+2: | |
444 code = 2; | |
445 sf[1] = sf[2] = sf[0]; | |
446 break; | |
447 case 2*5+3: | |
448 case 3*5+3: | |
449 code = 2; | |
450 sf[0] = sf[1] = sf[2]; | |
451 break; | |
452 case 3*5+0: | |
453 case 3*5+1: | |
454 case 3*5+2: | |
455 code = 2; | |
456 sf[0] = sf[2] = sf[1]; | |
457 break; | |
458 case 1*5+3: | |
459 code = 2; | |
460 if (sf[0] > sf[2]) | |
461 sf[0] = sf[2]; | |
462 sf[1] = sf[2] = sf[0]; | |
463 break; | |
464 default: | |
653 | 465 av_abort(); |
0 | 466 } |
467 | |
468 #if 0 | |
469 printf("%d: %2d %2d %2d %d %d -> %d\n", j, | |
470 sf[0], sf[1], sf[2], d1, d2, code); | |
471 #endif | |
472 scale_code[j] = code; | |
473 sf += 3; | |
474 } | |
475 } | |
476 | |
477 /* The most important function : psycho acoustic module. In this | |
478 encoder there is basically none, so this is the worst you can do, | |
479 but also this is the simpler. */ | |
480 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) | |
481 { | |
482 int i; | |
483 | |
484 for(i=0;i<s->sblimit;i++) { | |
485 smr[i] = (int)(fixed_smr[i] * 10); | |
486 } | |
487 } | |
488 | |
489 | |
490 #define SB_NOTALLOCATED 0 | |
491 #define SB_ALLOCATED 1 | |
492 #define SB_NOMORE 2 | |
493 | |
494 /* Try to maximize the smr while using a number of bits inferior to | |
495 the frame size. I tried to make the code simpler, faster and | |
496 smaller than other encoders :-) */ | |
497 static void compute_bit_allocation(MpegAudioContext *s, | |
498 short smr1[MPA_MAX_CHANNELS][SBLIMIT], | |
499 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], | |
500 int *padding) | |
501 { | |
502 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size; | |
503 int incr; | |
504 short smr[MPA_MAX_CHANNELS][SBLIMIT]; | |
505 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT]; | |
506 const unsigned char *alloc; | |
507 | |
508 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT); | |
509 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT); | |
510 memset(bit_alloc, 0, s->nb_channels * SBLIMIT); | |
511 | |
512 /* compute frame size and padding */ | |
513 max_frame_size = s->frame_size; | |
514 s->frame_frac += s->frame_frac_incr; | |
515 if (s->frame_frac >= 65536) { | |
516 s->frame_frac -= 65536; | |
517 s->do_padding = 1; | |
518 max_frame_size += 8; | |
519 } else { | |
520 s->do_padding = 0; | |
521 } | |
522 | |
523 /* compute the header + bit alloc size */ | |
524 current_frame_size = 32; | |
525 alloc = s->alloc_table; | |
526 for(i=0;i<s->sblimit;i++) { | |
527 incr = alloc[0]; | |
528 current_frame_size += incr * s->nb_channels; | |
529 alloc += 1 << incr; | |
530 } | |
531 for(;;) { | |
532 /* look for the subband with the largest signal to mask ratio */ | |
533 max_sb = -1; | |
534 max_ch = -1; | |
535 max_smr = 0x80000000; | |
536 for(ch=0;ch<s->nb_channels;ch++) { | |
537 for(i=0;i<s->sblimit;i++) { | |
538 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) { | |
539 max_smr = smr[ch][i]; | |
540 max_sb = i; | |
541 max_ch = ch; | |
542 } | |
543 } | |
544 } | |
545 #if 0 | |
546 printf("current=%d max=%d max_sb=%d alloc=%d\n", | |
547 current_frame_size, max_frame_size, max_sb, | |
548 bit_alloc[max_sb]); | |
549 #endif | |
550 if (max_sb < 0) | |
551 break; | |
552 | |
553 /* find alloc table entry (XXX: not optimal, should use | |
554 pointer table) */ | |
555 alloc = s->alloc_table; | |
556 for(i=0;i<max_sb;i++) { | |
557 alloc += 1 << alloc[0]; | |
558 } | |
559 | |
560 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) { | |
561 /* nothing was coded for this band: add the necessary bits */ | |
562 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6; | |
563 incr += total_quant_bits[alloc[1]]; | |
564 } else { | |
565 /* increments bit allocation */ | |
566 b = bit_alloc[max_ch][max_sb]; | |
567 incr = total_quant_bits[alloc[b + 1]] - | |
568 total_quant_bits[alloc[b]]; | |
569 } | |
570 | |
571 if (current_frame_size + incr <= max_frame_size) { | |
572 /* can increase size */ | |
573 b = ++bit_alloc[max_ch][max_sb]; | |
574 current_frame_size += incr; | |
575 /* decrease smr by the resolution we added */ | |
576 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]]; | |
577 /* max allocation size reached ? */ | |
578 if (b == ((1 << alloc[0]) - 1)) | |
579 subband_status[max_ch][max_sb] = SB_NOMORE; | |
580 else | |
581 subband_status[max_ch][max_sb] = SB_ALLOCATED; | |
582 } else { | |
583 /* cannot increase the size of this subband */ | |
584 subband_status[max_ch][max_sb] = SB_NOMORE; | |
585 } | |
586 } | |
587 *padding = max_frame_size - current_frame_size; | |
588 assert(*padding >= 0); | |
589 | |
590 #if 0 | |
591 for(i=0;i<s->sblimit;i++) { | |
592 printf("%d ", bit_alloc[i]); | |
593 } | |
594 printf("\n"); | |
595 #endif | |
596 } | |
597 | |
598 /* | |
599 * Output the mpeg audio layer 2 frame. Note how the code is small | |
600 * compared to other encoders :-) | |
601 */ | |
602 static void encode_frame(MpegAudioContext *s, | |
603 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], | |
604 int padding) | |
605 { | |
606 int i, j, k, l, bit_alloc_bits, b, ch; | |
607 unsigned char *sf; | |
608 int q[3]; | |
609 PutBitContext *p = &s->pb; | |
610 | |
611 /* header */ | |
612 | |
613 put_bits(p, 12, 0xfff); | |
614 put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */ | |
615 put_bits(p, 2, 4-2); /* layer 2 */ | |
616 put_bits(p, 1, 1); /* no error protection */ | |
617 put_bits(p, 4, s->bitrate_index); | |
618 put_bits(p, 2, s->freq_index); | |
619 put_bits(p, 1, s->do_padding); /* use padding */ | |
620 put_bits(p, 1, 0); /* private_bit */ | |
621 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO); | |
622 put_bits(p, 2, 0); /* mode_ext */ | |
623 put_bits(p, 1, 0); /* no copyright */ | |
624 put_bits(p, 1, 1); /* original */ | |
625 put_bits(p, 2, 0); /* no emphasis */ | |
626 | |
627 /* bit allocation */ | |
628 j = 0; | |
629 for(i=0;i<s->sblimit;i++) { | |
630 bit_alloc_bits = s->alloc_table[j]; | |
631 for(ch=0;ch<s->nb_channels;ch++) { | |
632 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); | |
633 } | |
634 j += 1 << bit_alloc_bits; | |
635 } | |
636 | |
637 /* scale codes */ | |
638 for(i=0;i<s->sblimit;i++) { | |
639 for(ch=0;ch<s->nb_channels;ch++) { | |
640 if (bit_alloc[ch][i]) | |
641 put_bits(p, 2, s->scale_code[ch][i]); | |
642 } | |
643 } | |
644 | |
645 /* scale factors */ | |
646 for(i=0;i<s->sblimit;i++) { | |
647 for(ch=0;ch<s->nb_channels;ch++) { | |
648 if (bit_alloc[ch][i]) { | |
649 sf = &s->scale_factors[ch][i][0]; | |
650 switch(s->scale_code[ch][i]) { | |
651 case 0: | |
652 put_bits(p, 6, sf[0]); | |
653 put_bits(p, 6, sf[1]); | |
654 put_bits(p, 6, sf[2]); | |
655 break; | |
656 case 3: | |
657 case 1: | |
658 put_bits(p, 6, sf[0]); | |
659 put_bits(p, 6, sf[2]); | |
660 break; | |
661 case 2: | |
662 put_bits(p, 6, sf[0]); | |
663 break; | |
664 } | |
665 } | |
666 } | |
667 } | |
668 | |
669 /* quantization & write sub band samples */ | |
670 | |
671 for(k=0;k<3;k++) { | |
672 for(l=0;l<12;l+=3) { | |
673 j = 0; | |
674 for(i=0;i<s->sblimit;i++) { | |
675 bit_alloc_bits = s->alloc_table[j]; | |
676 for(ch=0;ch<s->nb_channels;ch++) { | |
677 b = bit_alloc[ch][i]; | |
678 if (b) { | |
679 int qindex, steps, m, sample, bits; | |
680 /* we encode 3 sub band samples of the same sub band at a time */ | |
681 qindex = s->alloc_table[j+b]; | |
682 steps = quant_steps[qindex]; | |
683 for(m=0;m<3;m++) { | |
684 sample = s->sb_samples[ch][k][l + m][i]; | |
685 /* divide by scale factor */ | |
686 #ifdef USE_FLOATS | |
687 { | |
688 float a; | |
689 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]]; | |
690 q[m] = (int)((a + 1.0) * steps * 0.5); | |
691 } | |
692 #else | |
693 { | |
694 int q1, e, shift, mult; | |
695 e = s->scale_factors[ch][i][k]; | |
696 shift = scale_factor_shift[e]; | |
697 mult = scale_factor_mult[e]; | |
698 | |
699 /* normalize to P bits */ | |
700 if (shift < 0) | |
701 q1 = sample << (-shift); | |
702 else | |
703 q1 = sample >> shift; | |
704 q1 = (q1 * mult) >> P; | |
705 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1); | |
706 } | |
707 #endif | |
708 if (q[m] >= steps) | |
709 q[m] = steps - 1; | |
710 assert(q[m] >= 0 && q[m] < steps); | |
711 } | |
712 bits = quant_bits[qindex]; | |
713 if (bits < 0) { | |
714 /* group the 3 values to save bits */ | |
715 put_bits(p, -bits, | |
716 q[0] + steps * (q[1] + steps * q[2])); | |
717 #if 0 | |
718 printf("%d: gr1 %d\n", | |
719 i, q[0] + steps * (q[1] + steps * q[2])); | |
720 #endif | |
721 } else { | |
722 #if 0 | |
723 printf("%d: gr3 %d %d %d\n", | |
724 i, q[0], q[1], q[2]); | |
725 #endif | |
726 put_bits(p, bits, q[0]); | |
727 put_bits(p, bits, q[1]); | |
728 put_bits(p, bits, q[2]); | |
729 } | |
730 } | |
731 } | |
732 /* next subband in alloc table */ | |
733 j += 1 << bit_alloc_bits; | |
734 } | |
735 } | |
736 } | |
737 | |
738 /* padding */ | |
739 for(i=0;i<padding;i++) | |
740 put_bits(p, 1, 0); | |
741 | |
742 /* flush */ | |
743 flush_put_bits(p); | |
744 } | |
745 | |
1057 | 746 static int MPA_encode_frame(AVCodecContext *avctx, |
747 unsigned char *frame, int buf_size, void *data) | |
0 | 748 { |
749 MpegAudioContext *s = avctx->priv_data; | |
750 short *samples = data; | |
751 short smr[MPA_MAX_CHANNELS][SBLIMIT]; | |
752 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; | |
753 int padding, i; | |
754 | |
755 for(i=0;i<s->nb_channels;i++) { | |
756 filter(s, i, samples + i, s->nb_channels); | |
757 } | |
758 | |
759 for(i=0;i<s->nb_channels;i++) { | |
760 compute_scale_factors(s->scale_code[i], s->scale_factors[i], | |
761 s->sb_samples[i], s->sblimit); | |
762 } | |
763 for(i=0;i<s->nb_channels;i++) { | |
764 psycho_acoustic_model(s, smr[i]); | |
765 } | |
766 compute_bit_allocation(s, smr, bit_alloc, &padding); | |
767 | |
1522
79dddc5cd990
removed the obsolete and unused parameters of init_put_bits
alex
parents:
1106
diff
changeset
|
768 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE); |
0 | 769 |
770 encode_frame(s, bit_alloc, padding); | |
771 | |
772 s->nb_samples += MPA_FRAME_SIZE; | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
89
diff
changeset
|
773 return pbBufPtr(&s->pb) - s->pb.buf; |
0 | 774 } |
775 | |
925 | 776 static int MPA_encode_close(AVCodecContext *avctx) |
777 { | |
778 av_freep(&avctx->coded_frame); | |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
925
diff
changeset
|
779 return 0; |
925 | 780 } |
0 | 781 |
782 AVCodec mp2_encoder = { | |
783 "mp2", | |
784 CODEC_TYPE_AUDIO, | |
785 CODEC_ID_MP2, | |
786 sizeof(MpegAudioContext), | |
787 MPA_encode_init, | |
788 MPA_encode_frame, | |
925 | 789 MPA_encode_close, |
0 | 790 NULL, |
791 }; | |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
792 |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
793 #undef FIX |