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