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