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