ffmpeg / libavcodec / mpegaudioenc.c @ 6bb6fb05
History  View  Annotate  Download (22.6 KB)
1 
/*


2 
* The simplest mpeg audio layer 2 encoder

3 
* Copyright (c) 2000, 2001 Fabrice Bellard

4 
*

5 
* This file is part of Libav.

6 
*

7 
* Libav 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 
* Libav 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 Libav; if not, write to the Free Software

19 
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 021101301 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 
#define FRAC_BITS 15 /* fractional bits for sb_samples and dct */ 
31 
#define WFRAC_BITS 14 /* fractional bits for window */ 
32  
33 
#include "mpegaudio.h" 
34  
35 
/* currently, cannot change these constants (need to modify

36 
quantization stage) */

37 
#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)

38  
39 
#define SAMPLES_BUF_SIZE 4096 
40  
41 
typedef struct MpegAudioContext { 
42 
PutBitContext pb; 
43 
int nb_channels;

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 */ 
48 
/* padding computation */

49 
int frame_frac, frame_frac_incr, do_padding;

50 
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */ 
51 
int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */ 
52 
int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]; 
53 
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */ 
54 
/* code to group 3 scale factors */

55 
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; 
56 
int sblimit; /* number of used subbands */ 
57 
const unsigned char *alloc_table; 
58 
} MpegAudioContext; 
59  
60 
/* define it to use floats in quantization (I don't like floats !) */

61 
#define USE_FLOATS

62  
63 
#include "mpegaudiodata.h" 
64 
#include "mpegaudiotab.h" 
65  
66 
static av_cold int MPA_encode_init(AVCodecContext *avctx) 
67 
{ 
68 
MpegAudioContext *s = avctx>priv_data; 
69 
int freq = avctx>sample_rate;

70 
int bitrate = avctx>bit_rate;

71 
int channels = avctx>channels;

72 
int i, v, table;

73 
float a;

74  
75 
if (channels <= 0  channels > 2){ 
76 
av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);

77 
return 1; 
78 
} 
79 
bitrate = bitrate / 1000;

80 
s>nb_channels = channels; 
81 
avctx>frame_size = MPA_FRAME_SIZE; 
82  
83 
/* encoding freq */

84 
s>lsf = 0;

85 
for(i=0;i<3;i++) { 
86 
if (ff_mpa_freq_tab[i] == freq)

87 
break;

88 
if ((ff_mpa_freq_tab[i] / 2) == freq) { 
89 
s>lsf = 1;

90 
break;

91 
} 
92 
} 
93 
if (i == 3){ 
94 
av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);

95 
return 1; 
96 
} 
97 
s>freq_index = i; 
98  
99 
/* encoding bitrate & frequency */

100 
for(i=0;i<15;i++) { 
101 
if (ff_mpa_bitrate_tab[s>lsf][1][i] == bitrate) 
102 
break;

103 
} 
104 
if (i == 15){ 
105 
av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);

106 
return 1; 
107 
} 
108 
s>bitrate_index = i; 
109  
110 
/* compute total header size & pad bit */

111  
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); 
118  
119 
/* select the right allocation table */

120 
table = ff_mpa_l2_select_table(bitrate, s>nb_channels, freq, s>lsf); 
121  
122 
/* number of used subbands */

123 
s>sblimit = ff_mpa_sblimit_table[table]; 
124 
s>alloc_table = ff_mpa_alloc_tables[table]; 
125  
126 
av_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",

127 
bitrate, freq, s>frame_size, table, s>frame_frac_incr); 
128  
129 
for(i=0;i<s>nb_channels;i++) 
130 
s>samples_offset[i] = 0;

131  
132 
for(i=0;i<257;i++) { 
133 
int v;

134 
v = ff_mpa_enwindow[i]; 
135 
#if WFRAC_BITS != 16 
136 
v = (v + (1 << (16  WFRAC_BITS  1))) >> (16  WFRAC_BITS); 
137 
#endif

138 
filter_bank[i] = v; 
139 
if ((i & 63) != 0) 
140 
v = v; 
141 
if (i != 0) 
142 
filter_bank[512  i] = v;

143 
} 
144  
145 
for(i=0;i<64;i++) { 
146 
v = (int)(pow(2.0, (3  i) / 3.0) * (1 << 20)); 
147 
if (v <= 0) 
148 
v = 1;

149 
scale_factor_table[i] = v; 
150 
#ifdef USE_FLOATS

151 
scale_factor_inv_table[i] = pow(2.0, (3  i) / 3.0) / (float)(1 << 20); 
152 
#else

153 
#define P 15 
154 
scale_factor_shift[i] = 21  P  (i / 3); 
155 
scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0); 
156 
#endif

157 
} 
158 
for(i=0;i<128;i++) { 
159 
v = i  64;

160 
if (v <= 3) 
161 
v = 0;

162 
else if (v < 0) 
163 
v = 1;

164 
else if (v == 0) 
165 
v = 2;

166 
else if (v < 3) 
167 
v = 3;

168 
else

169 
v = 4;

170 
scale_diff_table[i] = v; 
171 
} 
172  
173 
for(i=0;i<17;i++) { 
174 
v = ff_mpa_quant_bits[i]; 
175 
if (v < 0) 
176 
v = v; 
177 
else

178 
v = v * 3;

179 
total_quant_bits[i] = 12 * v;

180 
} 
181  
182 
avctx>coded_frame= avcodec_alloc_frame(); 
183 
avctx>coded_frame>key_frame= 1;

184  
185 
return 0; 
186 
} 
187  
188 
/* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */

189 
static void idct32(int *out, int *tab) 
190 
{ 
191 
int i, j;

192 
int *t, *t1, xr;

193 
const int *xp = costab32; 
194  
195 
for(j=31;j>=3;j=2) tab[j] += tab[j  2]; 
196  
197 
t = tab + 30;

198 
t1 = tab + 2;

199 
do {

200 
t[0] += t[4]; 
201 
t[1] += t[1  4]; 
202 
t = 4;

203 
} while (t != t1);

204  
205 
t = tab + 28;

206 
t1 = tab + 4;

207 
do {

208 
t[0] += t[8]; 
209 
t[1] += t[18]; 
210 
t[2] += t[28]; 
211 
t[3] += t[38]; 
212 
t = 8;

213 
} while (t != t1);

214  
215 
t = tab; 
216 
t1 = tab + 32;

217 
do {

218 
t[ 3] = t[ 3]; 
219 
t[ 6] = t[ 6]; 
220  
221 
t[11] = t[11]; 
222 
t[12] = t[12]; 
223 
t[13] = t[13]; 
224 
t[15] = t[15]; 
225 
t += 16;

226 
} while (t != t1);

227  
228  
229 
t = tab; 
230 
t1 = tab + 8;

231 
do {

232 
int x1, x2, x3, x4;

233  
234 
x3 = MUL(t[16], FIX(SQRT2*0.5)); 
235 
x4 = t[0]  x3;

236 
x3 = t[0] + x3;

237  
238 
x2 = MUL((t[24] + t[8]), FIX(SQRT2*0.5)); 
239 
x1 = MUL((t[8]  x2), xp[0]); 
240 
x2 = MUL((t[8] + x2), xp[1]); 
241  
242 
t[ 0] = x3 + x1;

243 
t[ 8] = x4  x2;

244 
t[16] = x4 + x2;

245 
t[24] = x3  x1;

246 
t++; 
247 
} while (t != t1);

248  
249 
xp += 2;

250 
t = tab; 
251 
t1 = tab + 4;

252 
do {

253 
xr = MUL(t[28],xp[0]); 
254 
t[28] = (t[0]  xr); 
255 
t[0] = (t[0] + xr); 
256  
257 
xr = MUL(t[4],xp[1]); 
258 
t[ 4] = (t[24]  xr); 
259 
t[24] = (t[24] + xr); 
260  
261 
xr = MUL(t[20],xp[2]); 
262 
t[20] = (t[8]  xr); 
263 
t[ 8] = (t[8] + xr); 
264  
265 
xr = MUL(t[12],xp[3]); 
266 
t[12] = (t[16]  xr); 
267 
t[16] = (t[16] + xr); 
268 
t++; 
269 
} while (t != t1);

270 
xp += 4;

271  
272 
for (i = 0; i < 4; i++) { 
273 
xr = MUL(tab[30i*4],xp[0]); 
274 
tab[30i*4] = (tab[i*4]  xr); 
275 
tab[ i*4] = (tab[i*4] + xr); 
276  
277 
xr = MUL(tab[ 2+i*4],xp[1]); 
278 
tab[ 2+i*4] = (tab[28i*4]  xr); 
279 
tab[28i*4] = (tab[28i*4] + xr); 
280  
281 
xr = MUL(tab[31i*4],xp[0]); 
282 
tab[31i*4] = (tab[1+i*4]  xr); 
283 
tab[ 1+i*4] = (tab[1+i*4] + xr); 
284  
285 
xr = MUL(tab[ 3+i*4],xp[1]); 
286 
tab[ 3+i*4] = (tab[29i*4]  xr); 
287 
tab[29i*4] = (tab[29i*4] + xr); 
288  
289 
xp += 2;

290 
} 
291  
292 
t = tab + 30;

293 
t1 = tab + 1;

294 
do {

295 
xr = MUL(t1[0], *xp);

296 
t1[0] = (t[0]  xr); 
297 
t[0] = (t[0] + xr); 
298 
t = 2;

299 
t1 += 2;

300 
xp++; 
301 
} while (t >= tab);

302  
303 
for(i=0;i<32;i++) { 
304 
out[i] = tab[bitinv32[i]]; 
305 
} 
306 
} 
307  
308 
#define WSHIFT (WFRAC_BITS + 15  FRAC_BITS) 
309  
310 
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr) 
311 
{ 
312 
short *p, *q;

313 
int sum, offset, i, j;

314 
int tmp[64]; 
315 
int tmp1[32]; 
316 
int *out;

317  
318 
// print_pow1(samples, 1152);

319  
320 
offset = s>samples_offset[ch]; 
321 
out = &s>sb_samples[ch][0][0][0]; 
322 
for(j=0;j<36;j++) { 
323 
/* 32 samples at once */

324 
for(i=0;i<32;i++) { 
325 
s>samples_buf[ch][offset + (31  i)] = samples[0]; 
326 
samples += incr; 
327 
} 
328  
329 
/* filter */

330 
p = s>samples_buf[ch] + offset; 
331 
q = filter_bank; 
332 
/* maxsum = 23169 */

333 
for(i=0;i<64;i++) { 
334 
sum = p[0*64] * q[0*64]; 
335 
sum += p[1*64] * q[1*64]; 
336 
sum += p[2*64] * q[2*64]; 
337 
sum += p[3*64] * q[3*64]; 
338 
sum += p[4*64] * q[4*64]; 
339 
sum += p[5*64] * q[5*64]; 
340 
sum += p[6*64] * q[6*64]; 
341 
sum += p[7*64] * q[7*64]; 
342 
tmp[i] = sum; 
343 
p++; 
344 
q++; 
345 
} 
346 
tmp1[0] = tmp[16] >> WSHIFT; 
347 
for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16i]) >> WSHIFT; 
348 
for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]tmp[80i]) >> WSHIFT; 
349  
350 
idct32(out, tmp1); 
351  
352 
/* advance of 32 samples */

353 
offset = 32;

354 
out += 32;

355 
/* handle the wrap around */

356 
if (offset < 0) { 
357 
memmove(s>samples_buf[ch] + SAMPLES_BUF_SIZE  (512  32), 
358 
s>samples_buf[ch], (512  32) * 2); 
359 
offset = SAMPLES_BUF_SIZE  512;

360 
} 
361 
} 
362 
s>samples_offset[ch] = offset; 
363  
364 
// print_pow(s>sb_samples, 1152);

365 
} 
366  
367 
static void compute_scale_factors(unsigned char scale_code[SBLIMIT], 
368 
unsigned char scale_factors[SBLIMIT][3], 
369 
int sb_samples[3][12][SBLIMIT], 
370 
int sblimit)

371 
{ 
372 
int *p, vmax, v, n, i, j, k, code;

373 
int index, d1, d2;

374 
unsigned char *sf = &scale_factors[0][0]; 
375  
376 
for(j=0;j<sblimit;j++) { 
377 
for(i=0;i<3;i++) { 
378 
/* find the max absolute value */

379 
p = &sb_samples[i][0][j];

380 
vmax = abs(*p); 
381 
for(k=1;k<12;k++) { 
382 
p += SBLIMIT; 
383 
v = abs(*p); 
384 
if (v > vmax)

385 
vmax = v; 
386 
} 
387 
/* compute the scale factor index using log 2 computations */

388 
if (vmax > 1) { 
389 
n = av_log2(vmax); 
390 
/* n is the position of the MSB of vmax. now

391 
use at most 2 compares to find the index */

392 
index = (21  n) * 3  3; 
393 
if (index >= 0) { 
394 
while (vmax <= scale_factor_table[index+1]) 
395 
index++; 
396 
} else {

397 
index = 0; /* very unlikely case of overflow */ 
398 
} 
399 
} else {

400 
index = 62; /* value 63 is not allowed */ 
401 
} 
402  
403 
av_dlog(NULL, "%2d:%d in=%x %x %d\n", 
404 
j, i, vmax, scale_factor_table[index], index); 
405 
/* store the scale factor */

406 
assert(index >=0 && index <= 63); 
407 
sf[i] = index; 
408 
} 
409  
410 
/* compute the transmission factor : look if the scale factors

411 
are close enough to each other */

412 
d1 = scale_diff_table[sf[0]  sf[1] + 64]; 
413 
d2 = scale_diff_table[sf[1]  sf[2] + 64]; 
414  
415 
/* handle the 25 cases */

416 
switch(d1 * 5 + d2) { 
417 
case 0*5+0: 
418 
case 0*5+4: 
419 
case 3*5+4: 
420 
case 4*5+0: 
421 
case 4*5+4: 
422 
code = 0;

423 
break;

424 
case 0*5+1: 
425 
case 0*5+2: 
426 
case 4*5+1: 
427 
case 4*5+2: 
428 
code = 3;

429 
sf[2] = sf[1]; 
430 
break;

431 
case 0*5+3: 
432 
case 4*5+3: 
433 
code = 3;

434 
sf[1] = sf[2]; 
435 
break;

436 
case 1*5+0: 
437 
case 1*5+4: 
438 
case 2*5+4: 
439 
code = 1;

440 
sf[1] = sf[0]; 
441 
break;

442 
case 1*5+1: 
443 
case 1*5+2: 
444 
case 2*5+0: 
445 
case 2*5+1: 
446 
case 2*5+2: 
447 
code = 2;

448 
sf[1] = sf[2] = sf[0]; 
449 
break;

450 
case 2*5+3: 
451 
case 3*5+3: 
452 
code = 2;

453 
sf[0] = sf[1] = sf[2]; 
454 
break;

455 
case 3*5+0: 
456 
case 3*5+1: 
457 
case 3*5+2: 
458 
code = 2;

459 
sf[0] = sf[2] = sf[1]; 
460 
break;

461 
case 1*5+3: 
462 
code = 2;

463 
if (sf[0] > sf[2]) 
464 
sf[0] = sf[2]; 
465 
sf[1] = sf[2] = sf[0]; 
466 
break;

467 
default:

468 
assert(0); //cannot happen 
469 
code = 0; /* kill warning */ 
470 
} 
471  
472 
av_dlog(NULL, "%d: %2d %2d %2d %d %d > %d\n", j, 
473 
sf[0], sf[1], sf[2], d1, d2, code); 
474 
scale_code[j] = code; 
475 
sf += 3;

476 
} 
477 
} 
478  
479 
/* The most important function : psycho acoustic module. In this

480 
encoder there is basically none, so this is the worst you can do,

481 
but also this is the simpler. */

482 
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) 
483 
{ 
484 
int i;

485  
486 
for(i=0;i<s>sblimit;i++) { 
487 
smr[i] = (int)(fixed_smr[i] * 10); 
488 
} 
489 
} 
490  
491  
492 
#define SB_NOTALLOCATED 0 
493 
#define SB_ALLOCATED 1 
494 
#define SB_NOMORE 2 
495  
496 
/* Try to maximize the smr while using a number of bits inferior to

497 
the frame size. I tried to make the code simpler, faster and

498 
smaller than other encoders :) */

499 
static void compute_bit_allocation(MpegAudioContext *s, 
500 
short smr1[MPA_MAX_CHANNELS][SBLIMIT],

501 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], 
502 
int *padding)

503 
{ 
504 
int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;

505 
int incr;

506 
short smr[MPA_MAX_CHANNELS][SBLIMIT];

507 
unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT]; 
508 
const unsigned char *alloc; 
509  
510 
memcpy(smr, smr1, s>nb_channels * sizeof(short) * SBLIMIT); 
511 
memset(subband_status, SB_NOTALLOCATED, s>nb_channels * SBLIMIT); 
512 
memset(bit_alloc, 0, s>nb_channels * SBLIMIT);

513  
514 
/* compute frame size and padding */

515 
max_frame_size = s>frame_size; 
516 
s>frame_frac += s>frame_frac_incr; 
517 
if (s>frame_frac >= 65536) { 
518 
s>frame_frac = 65536;

519 
s>do_padding = 1;

520 
max_frame_size += 8;

521 
} else {

522 
s>do_padding = 0;

523 
} 
524  
525 
/* compute the header + bit alloc size */

526 
current_frame_size = 32;

527 
alloc = s>alloc_table; 
528 
for(i=0;i<s>sblimit;i++) { 
529 
incr = alloc[0];

530 
current_frame_size += incr * s>nb_channels; 
531 
alloc += 1 << incr;

532 
} 
533 
for(;;) {

534 
/* look for the subband with the largest signal to mask ratio */

535 
max_sb = 1;

536 
max_ch = 1;

537 
max_smr = INT_MIN; 
538 
for(ch=0;ch<s>nb_channels;ch++) { 
539 
for(i=0;i<s>sblimit;i++) { 
540 
if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {

541 
max_smr = smr[ch][i]; 
542 
max_sb = i; 
543 
max_ch = ch; 
544 
} 
545 
} 
546 
} 
547 
av_dlog(NULL, "current=%d max=%d max_sb=%d alloc=%d\n", 
548 
current_frame_size, max_frame_size, max_sb, 
549 
bit_alloc[max_sb]); 
550 
if (max_sb < 0) 
551 
break;

552  
553 
/* find alloc table entry (XXX: not optimal, should use

554 
pointer table) */

555 
alloc = s>alloc_table; 
556 
for(i=0;i<max_sb;i++) { 
557 
alloc += 1 << alloc[0]; 
558 
} 
559  
560 
if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {

561 
/* nothing was coded for this band: add the necessary bits */

562 
incr = 2 + nb_scale_factors[s>scale_code[max_ch][max_sb]] * 6; 
563 
incr += total_quant_bits[alloc[1]];

564 
} else {

565 
/* increments bit allocation */

566 
b = bit_alloc[max_ch][max_sb]; 
567 
incr = total_quant_bits[alloc[b + 1]] 

568 
total_quant_bits[alloc[b]]; 
569 
} 
570  
571 
if (current_frame_size + incr <= max_frame_size) {

572 
/* can increase size */

573 
b = ++bit_alloc[max_ch][max_sb]; 
574 
current_frame_size += incr; 
575 
/* decrease smr by the resolution we added */

576 
smr[max_ch][max_sb] = smr1[max_ch][max_sb]  quant_snr[alloc[b]]; 
577 
/* max allocation size reached ? */

578 
if (b == ((1 << alloc[0])  1)) 
579 
subband_status[max_ch][max_sb] = SB_NOMORE; 
580 
else

581 
subband_status[max_ch][max_sb] = SB_ALLOCATED; 
582 
} else {

583 
/* cannot increase the size of this subband */

584 
subband_status[max_ch][max_sb] = SB_NOMORE; 
585 
} 
586 
} 
587 
*padding = max_frame_size  current_frame_size; 
588 
assert(*padding >= 0);

589 
} 
590  
591 
/*

592 
* Output the mpeg audio layer 2 frame. Note how the code is small

593 
* compared to other encoders :)

594 
*/

595 
static void encode_frame(MpegAudioContext *s, 
596 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], 
597 
int padding)

598 
{ 
599 
int i, j, k, l, bit_alloc_bits, b, ch;

600 
unsigned char *sf; 
601 
int q[3]; 
602 
PutBitContext *p = &s>pb; 
603  
604 
/* header */

605  
606 
put_bits(p, 12, 0xfff); 
607 
put_bits(p, 1, 1  s>lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */ 
608 
put_bits(p, 2, 42); /* layer 2 */ 
609 
put_bits(p, 1, 1); /* no error protection */ 
610 
put_bits(p, 4, s>bitrate_index);

611 
put_bits(p, 2, s>freq_index);

612 
put_bits(p, 1, s>do_padding); /* use padding */ 
613 
put_bits(p, 1, 0); /* private_bit */ 
614 
put_bits(p, 2, s>nb_channels == 2 ? MPA_STEREO : MPA_MONO); 
615 
put_bits(p, 2, 0); /* mode_ext */ 
616 
put_bits(p, 1, 0); /* no copyright */ 
617 
put_bits(p, 1, 1); /* original */ 
618 
put_bits(p, 2, 0); /* no emphasis */ 
619  
620 
/* bit allocation */

621 
j = 0;

622 
for(i=0;i<s>sblimit;i++) { 
623 
bit_alloc_bits = s>alloc_table[j]; 
624 
for(ch=0;ch<s>nb_channels;ch++) { 
625 
put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); 
626 
} 
627 
j += 1 << bit_alloc_bits;

628 
} 
629  
630 
/* scale codes */

631 
for(i=0;i<s>sblimit;i++) { 
632 
for(ch=0;ch<s>nb_channels;ch++) { 
633 
if (bit_alloc[ch][i])

634 
put_bits(p, 2, s>scale_code[ch][i]);

635 
} 
636 
} 
637  
638 
/* scale factors */

639 
for(i=0;i<s>sblimit;i++) { 
640 
for(ch=0;ch<s>nb_channels;ch++) { 
641 
if (bit_alloc[ch][i]) {

642 
sf = &s>scale_factors[ch][i][0];

643 
switch(s>scale_code[ch][i]) {

644 
case 0: 
645 
put_bits(p, 6, sf[0]); 
646 
put_bits(p, 6, sf[1]); 
647 
put_bits(p, 6, sf[2]); 
648 
break;

649 
case 3: 
650 
case 1: 
651 
put_bits(p, 6, sf[0]); 
652 
put_bits(p, 6, sf[2]); 
653 
break;

654 
case 2: 
655 
put_bits(p, 6, sf[0]); 
656 
break;

657 
} 
658 
} 
659 
} 
660 
} 
661  
662 
/* quantization & write sub band samples */

663  
664 
for(k=0;k<3;k++) { 
665 
for(l=0;l<12;l+=3) { 
666 
j = 0;

667 
for(i=0;i<s>sblimit;i++) { 
668 
bit_alloc_bits = s>alloc_table[j]; 
669 
for(ch=0;ch<s>nb_channels;ch++) { 
670 
b = bit_alloc[ch][i]; 
671 
if (b) {

672 
int qindex, steps, m, sample, bits;

673 
/* we encode 3 sub band samples of the same sub band at a time */

674 
qindex = s>alloc_table[j+b]; 
675 
steps = ff_mpa_quant_steps[qindex]; 
676 
for(m=0;m<3;m++) { 
677 
sample = s>sb_samples[ch][k][l + m][i]; 
678 
/* divide by scale factor */

679 
#ifdef USE_FLOATS

680 
{ 
681 
float a;

682 
a = (float)sample * scale_factor_inv_table[s>scale_factors[ch][i][k]];

683 
q[m] = (int)((a + 1.0) * steps * 0.5); 
684 
} 
685 
#else

686 
{ 
687 
int q1, e, shift, mult;

688 
e = s>scale_factors[ch][i][k]; 
689 
shift = scale_factor_shift[e]; 
690 
mult = scale_factor_mult[e]; 
691  
692 
/* normalize to P bits */

693 
if (shift < 0) 
694 
q1 = sample << (shift); 
695 
else

696 
q1 = sample >> shift; 
697 
q1 = (q1 * mult) >> P; 
698 
q[m] = ((q1 + (1 << P)) * steps) >> (P + 1); 
699 
} 
700 
#endif

701 
if (q[m] >= steps)

702 
q[m] = steps  1;

703 
assert(q[m] >= 0 && q[m] < steps);

704 
} 
705 
bits = ff_mpa_quant_bits[qindex]; 
706 
if (bits < 0) { 
707 
/* group the 3 values to save bits */

708 
put_bits(p, bits, 
709 
q[0] + steps * (q[1] + steps * q[2])); 
710 
} else {

711 
put_bits(p, bits, q[0]);

712 
put_bits(p, bits, q[1]);

713 
put_bits(p, bits, q[2]);

714 
} 
715 
} 
716 
} 
717 
/* next subband in alloc table */

718 
j += 1 << bit_alloc_bits;

719 
} 
720 
} 
721 
} 
722  
723 
/* padding */

724 
for(i=0;i<padding;i++) 
725 
put_bits(p, 1, 0); 
726  
727 
/* flush */

728 
flush_put_bits(p); 
729 
} 
730  
731 
static int MPA_encode_frame(AVCodecContext *avctx, 
732 
unsigned char *frame, int buf_size, void *data) 
733 
{ 
734 
MpegAudioContext *s = avctx>priv_data; 
735 
const short *samples = data; 
736 
short smr[MPA_MAX_CHANNELS][SBLIMIT];

737 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; 
738 
int padding, i;

739  
740 
for(i=0;i<s>nb_channels;i++) { 
741 
filter(s, i, samples + i, s>nb_channels); 
742 
} 
743  
744 
for(i=0;i<s>nb_channels;i++) { 
745 
compute_scale_factors(s>scale_code[i], s>scale_factors[i], 
746 
s>sb_samples[i], s>sblimit); 
747 
} 
748 
for(i=0;i<s>nb_channels;i++) { 
749 
psycho_acoustic_model(s, smr[i]); 
750 
} 
751 
compute_bit_allocation(s, smr, bit_alloc, &padding); 
752  
753 
init_put_bits(&s>pb, frame, MPA_MAX_CODED_FRAME_SIZE); 
754  
755 
encode_frame(s, bit_alloc, padding); 
756  
757 
return put_bits_ptr(&s>pb)  s>pb.buf;

758 
} 
759  
760 
static av_cold int MPA_encode_close(AVCodecContext *avctx) 
761 
{ 
762 
av_freep(&avctx>coded_frame); 
763 
return 0; 
764 
} 
765  
766 
AVCodec ff_mp2_encoder = { 
767 
"mp2",

768 
AVMEDIA_TYPE_AUDIO, 
769 
CODEC_ID_MP2, 
770 
sizeof(MpegAudioContext),

771 
MPA_encode_init, 
772 
MPA_encode_frame, 
773 
MPA_encode_close, 
774 
NULL,

775 
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, 
776 
.supported_samplerates= (const int[]){44100, 48000, 32000, 22050, 24000, 16000, 0}, 
777 
.long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),

778 
}; 
779  
780 
#undef FIX
