Mercurial > libavcodec.hg
annotate mpegaudio.c @ 2710:b71d3566fc6d libavcodec
fix mpeg2 chroma_420_type
author | michael |
---|---|
date | Wed, 18 May 2005 19:42:42 +0000 |
parents | b2846918585c |
children | cc55bc1f8d92 |
rev | line source |
---|---|
0 | 1 /* |
2 * The simplest mpeg audio layer 2 encoder | |
429 | 3 * Copyright (c) 2000, 2001 Fabrice Bellard. |
0 | 4 * |
429 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
0 | 18 */ |
1106 | 19 |
20 /** | |
21 * @file mpegaudio.c | |
22 * The simplest mpeg audio layer 2 encoder. | |
23 */ | |
24 | |
64 | 25 #include "avcodec.h" |
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2281
diff
changeset
|
26 #include "bitstream.h" |
0 | 27 #include "mpegaudio.h" |
28 | |
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
29 /* 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
|
30 quantization stage) */ |
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
31 #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
|
32 #define WFRAC_BITS 14 |
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 */ | |
54 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; | |
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++) { | |
84 | 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++) { | |
84 | 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 */ | |
109 | |
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); | |
116 | |
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 | |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
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; | |
168 else | |
169 v = 4; | |
170 scale_diff_table[i] = v; | |
171 } | |
172 | |
173 for(i=0;i<17;i++) { | |
174 v = quant_bits[i]; | |
175 if (v < 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]; | |
196 | |
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); | |
214 | |
215 t = tab; | |
216 t1 = tab + 32; | |
217 do { | |
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]; | |
225 t += 16; | |
226 } while (t != t1); | |
227 | |
228 | |
229 t = tab; | |
230 t1 = tab + 8; | |
231 do { | |
232 int x1, x2, x3, x4; | |
233 | |
234 x3 = MUL(t[16], FIX(SQRT2*0.5)); | |
235 x4 = t[0] - x3; | |
236 x3 = t[0] + x3; | |
237 | |
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); | |
260 | |
261 xr = MUL(t[20],xp[2]); | |
262 t[20] = (t[8] - xr); | |
263 t[ 8] = (t[8] + xr); | |
264 | |
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); | |
276 | |
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); | |
280 | |
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); | |
284 | |
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); | |
288 | |
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) { | |
357 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32), | |
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], | |
368 unsigned char scale_factors[SBLIMIT][3], | |
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]; | |
375 | |
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); |
0 | 390 /* n is the position of the MSB of vmax. now |
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 |
404 printf("%2d:%d in=%x %x %d\n", | |
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]; | |
416 | |
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 } |
473 | |
474 #if 0 | |
475 printf("%d: %2d %2d %2d %d %d -> %d\n", j, | |
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 :-) */ | |
503 static void compute_bit_allocation(MpegAudioContext *s, | |
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); | |
517 | |
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 | |
552 printf("current=%d max=%d max_sb=%d alloc=%d\n", | |
553 current_frame_size, max_frame_size, max_sb, | |
554 bit_alloc[max_sb]); | |
555 #endif | |
556 if (max_sb < 0) | |
557 break; | |
558 | |
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]; | |
573 incr = total_quant_bits[alloc[b + 1]] - | |
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 } | |
642 | |
643 /* scale codes */ | |
644 for(i=0;i<s->sblimit;i++) { | |
645 for(ch=0;ch<s->nb_channels;ch++) { | |
646 if (bit_alloc[ch][i]) | |
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 } | |
674 | |
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]; | |
704 | |
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 */ | |
721 put_bits(p, -bits, | |
722 q[0] + steps * (q[1] + steps * q[2])); | |
723 #if 0 | |
724 printf("%d: gr1 %d\n", | |
725 i, q[0] + steps * (q[1] + steps * q[2])); | |
726 #endif | |
727 } else { | |
728 #if 0 | |
729 printf("%d: gr3 %d %d %d\n", | |
730 i, q[0], q[1], q[2]); | |
731 #endif | |
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 */ | |
739 j += 1 << bit_alloc_bits; | |
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, |
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++) { | |
766 compute_scale_factors(s->scale_code[i], s->scale_factors[i], | |
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); | |
777 | |
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 |
2661
b2846918585c
a few #ifdef CONFIG_X_ENCODER, patch by (Roine Gustafsson <roine users.sourceforge net]
michael
parents:
2522
diff
changeset
|
788 #ifdef CONFIG_MP2_ENCODER |
0 | 789 AVCodec mp2_encoder = { |
790 "mp2", | |
791 CODEC_TYPE_AUDIO, | |
792 CODEC_ID_MP2, | |
793 sizeof(MpegAudioContext), | |
794 MPA_encode_init, | |
795 MPA_encode_frame, | |
925 | 796 MPA_encode_close, |
0 | 797 NULL, |
798 }; | |
2661
b2846918585c
a few #ifdef CONFIG_X_ENCODER, patch by (Roine Gustafsson <roine users.sourceforge net]
michael
parents:
2522
diff
changeset
|
799 #endif // CONFIG_MP2_ENCODER |
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
800 |
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
801 #undef FIX |