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