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