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