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