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