ffmpeg / libavcodec / mpegaudio.c @ 13b54752
History  View  Annotate  Download (21.8 KB)
1 
/*


2 
* The simplest mpeg audio layer 2 encoder

3 
* Copyright (c) 2000 Gerard Lantau.

4 
*

5 
* This program is free software; you can redistribute it and/or modify

6 
* it under the terms of the GNU General Public License as published by

7 
* the Free Software Foundation; either version 2 of the License, or

8 
* (at your option) any later version.

9 
*

10 
* This program is distributed in the hope that it will be useful,

11 
* but WITHOUT ANY WARRANTY; without even the implied warranty of

12 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

13 
* GNU General Public License for more details.

14 
*

15 
* You should have received a copy of the GNU General Public License

16 
* along with this program; if not, write to the Free Software

17 
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

18 
*/

19 
#include <stdlib.h> 
20 
#include <stdio.h> 
21 
#include <string.h> 
22 
#include <math.h> 
23 
#include "avcodec.h" 
24 
#include "mpegaudio.h" 
25  
26 
#define NDEBUG

27 
#include <assert.h> 
28  
29 
/* define it to use floats in quantization (I don't like floats !) */

30 
//#define USE_FLOATS

31  
32 
#define MPA_STEREO 0 
33 
#define MPA_JSTEREO 1 
34 
#define MPA_DUAL 2 
35 
#define MPA_MONO 3 
36  
37 
#include "mpegaudiotab.h" 
38  
39 
int MPA_encode_init(AVCodecContext *avctx)

40 
{ 
41 
MpegAudioContext *s = avctx>priv_data; 
42 
int freq = avctx>sample_rate;

43 
int bitrate = avctx>bit_rate;

44 
int channels = avctx>channels;

45 
int i, v, table, ch_bitrate;

46 
float a;

47  
48 
if (channels > 2) 
49 
return 1; 
50 
bitrate = bitrate / 1000;

51 
s>nb_channels = channels; 
52 
s>freq = freq; 
53 
s>bit_rate = bitrate * 1000;

54 
avctx>frame_size = MPA_FRAME_SIZE; 
55 
avctx>key_frame = 1; /* always key frame */ 
56  
57 
/* encoding freq */

58 
s>lsf = 0;

59 
for(i=0;i<3;i++) { 
60 
if (freq_tab[i] == freq)

61 
break;

62 
if ((freq_tab[i] / 2) == freq) { 
63 
s>lsf = 1;

64 
break;

65 
} 
66 
} 
67 
if (i == 3) 
68 
return 1; 
69 
s>freq_index = i; 
70  
71 
/* encoding bitrate & frequency */

72 
for(i=0;i<15;i++) { 
73 
if (bitrate_tab[1s>lsf][i] == bitrate) 
74 
break;

75 
} 
76 
if (i == 15) 
77 
return 1; 
78 
s>bitrate_index = i; 
79  
80 
/* compute total header size & pad bit */

81 

82 
a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0); 
83 
s>frame_size = ((int)a) * 8; 
84  
85 
/* frame fractional size to compute padding */

86 
s>frame_frac = 0;

87 
s>frame_frac_incr = (int)((a  floor(a)) * 65536.0); 
88 

89 
/* select the right allocation table */

90 
ch_bitrate = bitrate / s>nb_channels; 
91 
if (!s>lsf) {

92 
if ((freq == 48000 && ch_bitrate >= 56)  
93 
(ch_bitrate >= 56 && ch_bitrate <= 80)) 
94 
table = 0;

95 
else if (freq != 48000 && ch_bitrate >= 96) 
96 
table = 1;

97 
else if (freq != 32000 && ch_bitrate <= 48) 
98 
table = 2;

99 
else

100 
table = 3;

101 
} else {

102 
table = 4;

103 
} 
104 
/* number of used subbands */

105 
s>sblimit = sblimit_table[table]; 
106 
s>alloc_table = alloc_tables[table]; 
107  
108 
#ifdef DEBUG

109 
printf("%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",

110 
bitrate, freq, s>frame_size, table, s>frame_frac_incr); 
111 
#endif

112  
113 
for(i=0;i<s>nb_channels;i++) 
114 
s>samples_offset[i] = 0;

115  
116 
for(i=0;i<512;i++) { 
117 
float a = enwindow[i] * 32768.0 * 16.0; 
118 
filter_bank[i] = (int)(a);

119 
} 
120 
for(i=0;i<64;i++) { 
121 
v = (int)(pow(2.0, (3  i) / 3.0) * (1 << 20)); 
122 
if (v <= 0) 
123 
v = 1;

124 
scale_factor_table[i] = v; 
125 
#ifdef USE_FLOATS

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

128 
#define P 15 
129 
scale_factor_shift[i] = 21  P  (i / 3); 
130 
scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0); 
131 
#endif

132 
} 
133 
for(i=0;i<128;i++) { 
134 
v = i  64;

135 
if (v <= 3) 
136 
v = 0;

137 
else if (v < 0) 
138 
v = 1;

139 
else if (v == 0) 
140 
v = 2;

141 
else if (v < 3) 
142 
v = 3;

143 
else

144 
v = 4;

145 
scale_diff_table[i] = v; 
146 
} 
147  
148 
for(i=0;i<17;i++) { 
149 
v = quant_bits[i]; 
150 
if (v < 0) 
151 
v = v; 
152 
else

153 
v = v * 3;

154 
total_quant_bits[i] = 12 * v;

155 
} 
156  
157 
return 0; 
158 
} 
159  
160 
/* 32 point floating point IDCT */

161 
static void idct32(int *out, int *tab, int sblimit, int left_shift) 
162 
{ 
163 
int i, j;

164 
int *t, *t1, xr;

165 
const int *xp = costab32; 
166  
167 
for(j=31;j>=3;j=2) tab[j] += tab[j  2]; 
168 

169 
t = tab + 30;

170 
t1 = tab + 2;

171 
do {

172 
t[0] += t[4]; 
173 
t[1] += t[1  4]; 
174 
t = 4;

175 
} while (t != t1);

176  
177 
t = tab + 28;

178 
t1 = tab + 4;

179 
do {

180 
t[0] += t[8]; 
181 
t[1] += t[18]; 
182 
t[2] += t[28]; 
183 
t[3] += t[38]; 
184 
t = 8;

185 
} while (t != t1);

186 

187 
t = tab; 
188 
t1 = tab + 32;

189 
do {

190 
t[ 3] = t[ 3]; 
191 
t[ 6] = t[ 6]; 
192 

193 
t[11] = t[11]; 
194 
t[12] = t[12]; 
195 
t[13] = t[13]; 
196 
t[15] = t[15]; 
197 
t += 16;

198 
} while (t != t1);

199  
200 

201 
t = tab; 
202 
t1 = tab + 8;

203 
do {

204 
int x1, x2, x3, x4;

205 

206 
x3 = MUL(t[16], FIX(SQRT2*0.5)); 
207 
x4 = t[0]  x3;

208 
x3 = t[0] + x3;

209 

210 
x2 = MUL((t[24] + t[8]), FIX(SQRT2*0.5)); 
211 
x1 = MUL((t[8]  x2), xp[0]); 
212 
x2 = MUL((t[8] + x2), xp[1]); 
213  
214 
t[ 0] = x3 + x1;

215 
t[ 8] = x4  x2;

216 
t[16] = x4 + x2;

217 
t[24] = x3  x1;

218 
t++; 
219 
} while (t != t1);

220  
221 
xp += 2;

222 
t = tab; 
223 
t1 = tab + 4;

224 
do {

225 
xr = MUL(t[28],xp[0]); 
226 
t[28] = (t[0]  xr); 
227 
t[0] = (t[0] + xr); 
228  
229 
xr = MUL(t[4],xp[1]); 
230 
t[ 4] = (t[24]  xr); 
231 
t[24] = (t[24] + xr); 
232 

233 
xr = MUL(t[20],xp[2]); 
234 
t[20] = (t[8]  xr); 
235 
t[ 8] = (t[8] + xr); 
236 

237 
xr = MUL(t[12],xp[3]); 
238 
t[12] = (t[16]  xr); 
239 
t[16] = (t[16] + xr); 
240 
t++; 
241 
} while (t != t1);

242 
xp += 4;

243  
244 
for (i = 0; i < 4; i++) { 
245 
xr = MUL(tab[30i*4],xp[0]); 
246 
tab[30i*4] = (tab[i*4]  xr); 
247 
tab[ i*4] = (tab[i*4] + xr); 
248 

249 
xr = MUL(tab[ 2+i*4],xp[1]); 
250 
tab[ 2+i*4] = (tab[28i*4]  xr); 
251 
tab[28i*4] = (tab[28i*4] + xr); 
252 

253 
xr = MUL(tab[31i*4],xp[0]); 
254 
tab[31i*4] = (tab[1+i*4]  xr); 
255 
tab[ 1+i*4] = (tab[1+i*4] + xr); 
256 

257 
xr = MUL(tab[ 3+i*4],xp[1]); 
258 
tab[ 3+i*4] = (tab[29i*4]  xr); 
259 
tab[29i*4] = (tab[29i*4] + xr); 
260 

261 
xp += 2;

262 
} 
263  
264 
t = tab + 30;

265 
t1 = tab + 1;

266 
do {

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

268 
t1[0] = (t[0]  xr); 
269 
t[0] = (t[0] + xr); 
270 
t = 2;

271 
t1 += 2;

272 
xp++; 
273 
} while (t >= tab);

274  
275 
for(i=0;i<32;i++) { 
276 
out[i] = tab[bitinv32[i]] << left_shift; 
277 
} 
278 
} 
279  
280 
static void filter(MpegAudioContext *s, int ch, short *samples, int incr) 
281 
{ 
282 
short *p, *q;

283 
int sum, offset, i, j, norm, n;

284 
short tmp[64]; 
285 
int tmp1[32]; 
286 
int *out;

287  
288 
// print_pow1(samples, 1152);

289  
290 
offset = s>samples_offset[ch]; 
291 
out = &s>sb_samples[ch][0][0][0]; 
292 
for(j=0;j<36;j++) { 
293 
/* 32 samples at once */

294 
for(i=0;i<32;i++) { 
295 
s>samples_buf[ch][offset + (31  i)] = samples[0]; 
296 
samples += incr; 
297 
} 
298  
299 
/* filter */

300 
p = s>samples_buf[ch] + offset; 
301 
q = filter_bank; 
302 
/* maxsum = 23169 */

303 
for(i=0;i<64;i++) { 
304 
sum = p[0*64] * q[0*64]; 
305 
sum += p[1*64] * q[1*64]; 
306 
sum += p[2*64] * q[2*64]; 
307 
sum += p[3*64] * q[3*64]; 
308 
sum += p[4*64] * q[4*64]; 
309 
sum += p[5*64] * q[5*64]; 
310 
sum += p[6*64] * q[6*64]; 
311 
sum += p[7*64] * q[7*64]; 
312 
tmp[i] = sum >> 14;

313 
p++; 
314 
q++; 
315 
} 
316 
tmp1[0] = tmp[16]; 
317 
for( i=1; i<=16; i++ ) tmp1[i] = tmp[i+16]+tmp[16i]; 
318 
for( i=17; i<=31; i++ ) tmp1[i] = tmp[i+16]tmp[80i]; 
319  
320 
/* integer IDCT 32 with normalization. XXX: There may be some

321 
overflow left */

322 
norm = 0;

323 
for(i=0;i<32;i++) { 
324 
norm = abs(tmp1[i]); 
325 
} 
326 
n = log2(norm)  12;

327 
if (n > 0) { 
328 
for(i=0;i<32;i++) 
329 
tmp1[i] >>= n; 
330 
} else {

331 
n = 0;

332 
} 
333  
334 
idct32(out, tmp1, s>sblimit, n); 
335  
336 
/* advance of 32 samples */

337 
offset = 32;

338 
out += 32;

339 
/* handle the wrap around */

340 
if (offset < 0) { 
341 
memmove(s>samples_buf[ch] + SAMPLES_BUF_SIZE  (512  32), 
342 
s>samples_buf[ch], (512  32) * 2); 
343 
offset = SAMPLES_BUF_SIZE  512;

344 
} 
345 
} 
346 
s>samples_offset[ch] = offset; 
347  
348 
// print_pow(s>sb_samples, 1152);

349 
} 
350  
351 
static void compute_scale_factors(unsigned char scale_code[SBLIMIT], 
352 
unsigned char scale_factors[SBLIMIT][3], 
353 
int sb_samples[3][12][SBLIMIT], 
354 
int sblimit)

355 
{ 
356 
int *p, vmax, v, n, i, j, k, code;

357 
int index, d1, d2;

358 
unsigned char *sf = &scale_factors[0][0]; 
359 

360 
for(j=0;j<sblimit;j++) { 
361 
for(i=0;i<3;i++) { 
362 
/* find the max absolute value */

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

364 
vmax = abs(*p); 
365 
for(k=1;k<12;k++) { 
366 
p += SBLIMIT; 
367 
v = abs(*p); 
368 
if (v > vmax)

369 
vmax = v; 
370 
} 
371 
/* compute the scale factor index using log 2 computations */

372 
if (vmax > 0) { 
373 
n = log2(vmax); 
374 
/* n is the position of the MSB of vmax. now

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

376 
index = (21  n) * 3  3; 
377 
if (index >= 0) { 
378 
while (vmax <= scale_factor_table[index+1]) 
379 
index++; 
380 
} else {

381 
index = 0; /* very unlikely case of overflow */ 
382 
} 
383 
} else {

384 
index = 63;

385 
} 
386 

387 
#if 0

388 
printf("%2d:%d in=%x %x %d\n",

389 
j, i, vmax, scale_factor_table[index], index);

390 
#endif

391 
/* store the scale factor */

392 
assert(index >=0 && index <= 63); 
393 
sf[i] = index; 
394 
} 
395  
396 
/* compute the transmission factor : look if the scale factors

397 
are close enough to each other */

398 
d1 = scale_diff_table[sf[0]  sf[1] + 64]; 
399 
d2 = scale_diff_table[sf[1]  sf[2] + 64]; 
400 

401 
/* handle the 25 cases */

402 
switch(d1 * 5 + d2) { 
403 
case 0*5+0: 
404 
case 0*5+4: 
405 
case 3*5+4: 
406 
case 4*5+0: 
407 
case 4*5+4: 
408 
code = 0;

409 
break;

410 
case 0*5+1: 
411 
case 0*5+2: 
412 
case 4*5+1: 
413 
case 4*5+2: 
414 
code = 3;

415 
sf[2] = sf[1]; 
416 
break;

417 
case 0*5+3: 
418 
case 4*5+3: 
419 
code = 3;

420 
sf[1] = sf[2]; 
421 
break;

422 
case 1*5+0: 
423 
case 1*5+4: 
424 
case 2*5+4: 
425 
code = 1;

426 
sf[1] = sf[0]; 
427 
break;

428 
case 1*5+1: 
429 
case 1*5+2: 
430 
case 2*5+0: 
431 
case 2*5+1: 
432 
case 2*5+2: 
433 
code = 2;

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

436 
case 2*5+3: 
437 
case 3*5+3: 
438 
code = 2;

439 
sf[0] = sf[1] = sf[2]; 
440 
break;

441 
case 3*5+0: 
442 
case 3*5+1: 
443 
case 3*5+2: 
444 
code = 2;

445 
sf[0] = sf[2] = sf[1]; 
446 
break;

447 
case 1*5+3: 
448 
code = 2;

449 
if (sf[0] > sf[2]) 
450 
sf[0] = sf[2]; 
451 
sf[1] = sf[2] = sf[0]; 
452 
break;

453 
default:

454 
abort(); 
455 
} 
456 

457 
#if 0

458 
printf("%d: %2d %2d %2d %d %d > %d\n", j,

459 
sf[0], sf[1], sf[2], d1, d2, code);

460 
#endif

461 
scale_code[j] = code; 
462 
sf += 3;

463 
} 
464 
} 
465  
466 
/* The most important function : psycho acoustic module. In this

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

468 
but also this is the simpler. */

469 
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) 
470 
{ 
471 
int i;

472  
473 
for(i=0;i<s>sblimit;i++) { 
474 
smr[i] = (int)(fixed_smr[i] * 10); 
475 
} 
476 
} 
477  
478  
479 
#define SB_NOTALLOCATED 0 
480 
#define SB_ALLOCATED 1 
481 
#define SB_NOMORE 2 
482  
483 
/* Try to maximize the smr while using a number of bits inferior to

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

485 
smaller than other encoders :) */

486 
static void compute_bit_allocation(MpegAudioContext *s, 
487 
short smr1[MPA_MAX_CHANNELS][SBLIMIT],

488 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], 
489 
int *padding)

490 
{ 
491 
int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;

492 
int incr;

493 
short smr[MPA_MAX_CHANNELS][SBLIMIT];

494 
unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT]; 
495 
const unsigned char *alloc; 
496  
497 
memcpy(smr, smr1, s>nb_channels * sizeof(short) * SBLIMIT); 
498 
memset(subband_status, SB_NOTALLOCATED, s>nb_channels * SBLIMIT); 
499 
memset(bit_alloc, 0, s>nb_channels * SBLIMIT);

500 

501 
/* compute frame size and padding */

502 
max_frame_size = s>frame_size; 
503 
s>frame_frac += s>frame_frac_incr; 
504 
if (s>frame_frac >= 65536) { 
505 
s>frame_frac = 65536;

506 
s>do_padding = 1;

507 
max_frame_size += 8;

508 
} else {

509 
s>do_padding = 0;

510 
} 
511  
512 
/* compute the header + bit alloc size */

513 
current_frame_size = 32;

514 
alloc = s>alloc_table; 
515 
for(i=0;i<s>sblimit;i++) { 
516 
incr = alloc[0];

517 
current_frame_size += incr * s>nb_channels; 
518 
alloc += 1 << incr;

519 
} 
520 
for(;;) {

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

522 
max_sb = 1;

523 
max_ch = 1;

524 
max_smr = 0x80000000;

525 
for(ch=0;ch<s>nb_channels;ch++) { 
526 
for(i=0;i<s>sblimit;i++) { 
527 
if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {

528 
max_smr = smr[ch][i]; 
529 
max_sb = i; 
530 
max_ch = ch; 
531 
} 
532 
} 
533 
} 
534 
#if 0

535 
printf("current=%d max=%d max_sb=%d alloc=%d\n",

536 
current_frame_size, max_frame_size, max_sb,

537 
bit_alloc[max_sb]);

538 
#endif

539 
if (max_sb < 0) 
540 
break;

541 

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

543 
pointer table) */

544 
alloc = s>alloc_table; 
545 
for(i=0;i<max_sb;i++) { 
546 
alloc += 1 << alloc[0]; 
547 
} 
548  
549 
if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {

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

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

553 
} else {

554 
/* increments bit allocation */

555 
b = bit_alloc[max_ch][max_sb]; 
556 
incr = total_quant_bits[alloc[b + 1]] 

557 
total_quant_bits[alloc[b]]; 
558 
} 
559  
560 
if (current_frame_size + incr <= max_frame_size) {

561 
/* can increase size */

562 
b = ++bit_alloc[max_ch][max_sb]; 
563 
current_frame_size += incr; 
564 
/* decrease smr by the resolution we added */

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

567 
if (b == ((1 << alloc[0])  1)) 
568 
subband_status[max_ch][max_sb] = SB_NOMORE; 
569 
else

570 
subband_status[max_ch][max_sb] = SB_ALLOCATED; 
571 
} else {

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

573 
subband_status[max_ch][max_sb] = SB_NOMORE; 
574 
} 
575 
} 
576 
*padding = max_frame_size  current_frame_size; 
577 
assert(*padding >= 0);

578  
579 
#if 0

580 
for(i=0;i<s>sblimit;i++) {

581 
printf("%d ", bit_alloc[i]);

582 
}

583 
printf("\n");

584 
#endif

585 
} 
586  
587 
/*

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

589 
* compared to other encoders :)

590 
*/

591 
static void encode_frame(MpegAudioContext *s, 
592 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], 
593 
int padding)

594 
{ 
595 
int i, j, k, l, bit_alloc_bits, b, ch;

596 
unsigned char *sf; 
597 
int q[3]; 
598 
PutBitContext *p = &s>pb; 
599  
600 
/* header */

601  
602 
put_bits(p, 12, 0xfff); 
603 
put_bits(p, 1, 1  s>lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */ 
604 
put_bits(p, 2, 42); /* layer 2 */ 
605 
put_bits(p, 1, 1); /* no error protection */ 
606 
put_bits(p, 4, s>bitrate_index);

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

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

617 
j = 0;

618 
for(i=0;i<s>sblimit;i++) { 
619 
bit_alloc_bits = s>alloc_table[j]; 
620 
for(ch=0;ch<s>nb_channels;ch++) { 
621 
put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); 
622 
} 
623 
j += 1 << bit_alloc_bits;

624 
} 
625 

626 
/* scale codes */

627 
for(i=0;i<s>sblimit;i++) { 
628 
for(ch=0;ch<s>nb_channels;ch++) { 
629 
if (bit_alloc[ch][i])

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

631 
} 
632 
} 
633  
634 
/* scale factors */

635 
for(i=0;i<s>sblimit;i++) { 
636 
for(ch=0;ch<s>nb_channels;ch++) { 
637 
if (bit_alloc[ch][i]) {

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

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

640 
case 0: 
641 
put_bits(p, 6, sf[0]); 
642 
put_bits(p, 6, sf[1]); 
643 
put_bits(p, 6, sf[2]); 
644 
break;

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

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

653 
} 
654 
} 
655 
} 
656 
} 
657 

658 
/* quantization & write sub band samples */

659  
660 
for(k=0;k<3;k++) { 
661 
for(l=0;l<12;l+=3) { 
662 
j = 0;

663 
for(i=0;i<s>sblimit;i++) { 
664 
bit_alloc_bits = s>alloc_table[j]; 
665 
for(ch=0;ch<s>nb_channels;ch++) { 
666 
b = bit_alloc[ch][i]; 
667 
if (b) {

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

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

670 
qindex = s>alloc_table[j+b]; 
671 
steps = quant_steps[qindex]; 
672 
for(m=0;m<3;m++) { 
673 
sample = s>sb_samples[ch][k][l + m][i]; 
674 
/* divide by scale factor */

675 
#ifdef USE_FLOATS

676 
{ 
677 
float a;

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

679 
q[m] = (int)((a + 1.0) * steps * 0.5); 
680 
} 
681 
#else

682 
{ 
683 
int q1, e, shift, mult;

684 
e = s>scale_factors[ch][i][k]; 
685 
shift = scale_factor_shift[e]; 
686 
mult = scale_factor_mult[e]; 
687 

688 
/* normalize to P bits */

689 
if (shift < 0) 
690 
q1 = sample << (shift); 
691 
else

692 
q1 = sample >> shift; 
693 
q1 = (q1 * mult) >> P; 
694 
q[m] = ((q1 + (1 << P)) * steps) >> (P + 1); 
695 
} 
696 
#endif

697 
if (q[m] >= steps)

698 
q[m] = steps  1;

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

700 
} 
701 
bits = quant_bits[qindex]; 
702 
if (bits < 0) { 
703 
/* group the 3 values to save bits */

704 
put_bits(p, bits, 
705 
q[0] + steps * (q[1] + steps * q[2])); 
706 
#if 0

707 
printf("%d: gr1 %d\n",

708 
i, q[0] + steps * (q[1] + steps * q[2]));

709 
#endif

710 
} else {

711 
#if 0

712 
printf("%d: gr3 %d %d %d\n",

713 
i, q[0], q[1], q[2]);

714 
#endif

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

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

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

718 
} 
719 
} 
720 
} 
721 
/* next subband in alloc table */

722 
j += 1 << bit_alloc_bits;

723 
} 
724 
} 
725 
} 
726  
727 
/* padding */

728 
for(i=0;i<padding;i++) 
729 
put_bits(p, 1, 0); 
730  
731 
/* flush */

732 
flush_put_bits(p); 
733 
} 
734  
735 
int MPA_encode_frame(AVCodecContext *avctx,

736 
unsigned char *frame, int buf_size, void *data) 
737 
{ 
738 
MpegAudioContext *s = avctx>priv_data; 
739 
short *samples = data;

740 
short smr[MPA_MAX_CHANNELS][SBLIMIT];

741 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; 
742 
int padding, i;

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

761 
s>nb_samples += MPA_FRAME_SIZE; 
762 
return s>pb.buf_ptr  s>pb.buf;

763 
} 
764  
765  
766 
AVCodec mp2_encoder = { 
767 
"mp2",

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

771 
MPA_encode_init, 
772 
MPA_encode_frame, 
773 
NULL,

774 
}; 