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