ffmpeg / libavcodec / mpegaudio.c @ 935442b5
History  View  Annotate  Download (21.7 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 "avcodec.h" 
20 
#include <math.h> 
21 
#include "mpegaudio.h" 
22  
23 
/* define it to use floats in quantization (I don't like floats !) */

24 
//#define USE_FLOATS

25  
26 
#define MPA_STEREO 0 
27 
#define MPA_JSTEREO 1 
28 
#define MPA_DUAL 2 
29 
#define MPA_MONO 3 
30  
31 
#include "mpegaudiotab.h" 
32  
33 
int MPA_encode_init(AVCodecContext *avctx)

34 
{ 
35 
MpegAudioContext *s = avctx>priv_data; 
36 
int freq = avctx>sample_rate;

37 
int bitrate = avctx>bit_rate;

38 
int channels = avctx>channels;

39 
int i, v, table, ch_bitrate;

40 
float a;

41  
42 
if (channels > 2) 
43 
return 1; 
44 
bitrate = bitrate / 1000;

45 
s>nb_channels = channels; 
46 
s>freq = freq; 
47 
s>bit_rate = bitrate * 1000;

48 
avctx>frame_size = MPA_FRAME_SIZE; 
49 
avctx>key_frame = 1; /* always key frame */ 
50  
51 
/* encoding freq */

52 
s>lsf = 0;

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

55 
break;

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

58 
break;

59 
} 
60 
} 
61 
if (i == 3) 
62 
return 1; 
63 
s>freq_index = i; 
64  
65 
/* encoding bitrate & frequency */

66 
for(i=0;i<15;i++) { 
67 
if (bitrate_tab[1s>lsf][i] == bitrate) 
68 
break;

69 
} 
70 
if (i == 15) 
71 
return 1; 
72 
s>bitrate_index = i; 
73  
74 
/* compute total header size & pad bit */

75 

76 
a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0); 
77 
s>frame_size = ((int)a) * 8; 
78  
79 
/* frame fractional size to compute padding */

80 
s>frame_frac = 0;

81 
s>frame_frac_incr = (int)((a  floor(a)) * 65536.0); 
82 

83 
/* select the right allocation table */

84 
ch_bitrate = bitrate / s>nb_channels; 
85 
if (!s>lsf) {

86 
if ((freq == 48000 && ch_bitrate >= 56)  
87 
(ch_bitrate >= 56 && ch_bitrate <= 80)) 
88 
table = 0;

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

91 
else if (freq != 32000 && ch_bitrate <= 48) 
92 
table = 2;

93 
else

94 
table = 3;

95 
} else {

96 
table = 4;

97 
} 
98 
/* number of used subbands */

99 
s>sblimit = sblimit_table[table]; 
100 
s>alloc_table = alloc_tables[table]; 
101  
102 
#ifdef DEBUG

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

104 
bitrate, freq, s>frame_size, table, s>frame_frac_incr); 
105 
#endif

106  
107 
for(i=0;i<s>nb_channels;i++) 
108 
s>samples_offset[i] = 0;

109  
110 
for(i=0;i<512;i++) { 
111 
float a = enwindow[i] * 32768.0 * 16.0; 
112 
filter_bank[i] = (int)(a);

113 
} 
114 
for(i=0;i<64;i++) { 
115 
v = (int)(pow(2.0, (3  i) / 3.0) * (1 << 20)); 
116 
if (v <= 0) 
117 
v = 1;

118 
scale_factor_table[i] = v; 
119 
#ifdef USE_FLOATS

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

122 
#define P 15 
123 
scale_factor_shift[i] = 21  P  (i / 3); 
124 
scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0); 
125 
#endif

126 
} 
127 
for(i=0;i<128;i++) { 
128 
v = i  64;

129 
if (v <= 3) 
130 
v = 0;

131 
else if (v < 0) 
132 
v = 1;

133 
else if (v == 0) 
134 
v = 2;

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

137 
else

138 
v = 4;

139 
scale_diff_table[i] = v; 
140 
} 
141  
142 
for(i=0;i<17;i++) { 
143 
v = quant_bits[i]; 
144 
if (v < 0) 
145 
v = v; 
146 
else

147 
v = v * 3;

148 
total_quant_bits[i] = 12 * v;

149 
} 
150  
151 
return 0; 
152 
} 
153  
154 
/* 32 point floating point IDCT */

155 
static void idct32(int *out, int *tab, int sblimit, int left_shift) 
156 
{ 
157 
int i, j;

158 
int *t, *t1, xr;

159 
const int *xp = costab32; 
160  
161 
for(j=31;j>=3;j=2) tab[j] += tab[j  2]; 
162 

163 
t = tab + 30;

164 
t1 = tab + 2;

165 
do {

166 
t[0] += t[4]; 
167 
t[1] += t[1  4]; 
168 
t = 4;

169 
} while (t != t1);

170  
171 
t = tab + 28;

172 
t1 = tab + 4;

173 
do {

174 
t[0] += t[8]; 
175 
t[1] += t[18]; 
176 
t[2] += t[28]; 
177 
t[3] += t[38]; 
178 
t = 8;

179 
} while (t != t1);

180 

181 
t = tab; 
182 
t1 = tab + 32;

183 
do {

184 
t[ 3] = t[ 3]; 
185 
t[ 6] = t[ 6]; 
186 

187 
t[11] = t[11]; 
188 
t[12] = t[12]; 
189 
t[13] = t[13]; 
190 
t[15] = t[15]; 
191 
t += 16;

192 
} while (t != t1);

193  
194 

195 
t = tab; 
196 
t1 = tab + 8;

197 
do {

198 
int x1, x2, x3, x4;

199 

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

202 
x3 = t[0] + x3;

203 

204 
x2 = MUL((t[24] + t[8]), FIX(SQRT2*0.5)); 
205 
x1 = MUL((t[8]  x2), xp[0]); 
206 
x2 = MUL((t[8] + x2), xp[1]); 
207  
208 
t[ 0] = x3 + x1;

209 
t[ 8] = x4  x2;

210 
t[16] = x4 + x2;

211 
t[24] = x3  x1;

212 
t++; 
213 
} while (t != t1);

214  
215 
xp += 2;

216 
t = tab; 
217 
t1 = tab + 4;

218 
do {

219 
xr = MUL(t[28],xp[0]); 
220 
t[28] = (t[0]  xr); 
221 
t[0] = (t[0] + xr); 
222  
223 
xr = MUL(t[4],xp[1]); 
224 
t[ 4] = (t[24]  xr); 
225 
t[24] = (t[24] + xr); 
226 

227 
xr = MUL(t[20],xp[2]); 
228 
t[20] = (t[8]  xr); 
229 
t[ 8] = (t[8] + xr); 
230 

231 
xr = MUL(t[12],xp[3]); 
232 
t[12] = (t[16]  xr); 
233 
t[16] = (t[16] + xr); 
234 
t++; 
235 
} while (t != t1);

236 
xp += 4;

237  
238 
for (i = 0; i < 4; i++) { 
239 
xr = MUL(tab[30i*4],xp[0]); 
240 
tab[30i*4] = (tab[i*4]  xr); 
241 
tab[ i*4] = (tab[i*4] + xr); 
242 

243 
xr = MUL(tab[ 2+i*4],xp[1]); 
244 
tab[ 2+i*4] = (tab[28i*4]  xr); 
245 
tab[28i*4] = (tab[28i*4] + xr); 
246 

247 
xr = MUL(tab[31i*4],xp[0]); 
248 
tab[31i*4] = (tab[1+i*4]  xr); 
249 
tab[ 1+i*4] = (tab[1+i*4] + xr); 
250 

251 
xr = MUL(tab[ 3+i*4],xp[1]); 
252 
tab[ 3+i*4] = (tab[29i*4]  xr); 
253 
tab[29i*4] = (tab[29i*4] + xr); 
254 

255 
xp += 2;

256 
} 
257  
258 
t = tab + 30;

259 
t1 = tab + 1;

260 
do {

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

262 
t1[0] = (t[0]  xr); 
263 
t[0] = (t[0] + xr); 
264 
t = 2;

265 
t1 += 2;

266 
xp++; 
267 
} while (t >= tab);

268  
269 
for(i=0;i<32;i++) { 
270 
out[i] = tab[bitinv32[i]] << left_shift; 
271 
} 
272 
} 
273  
274 
static void filter(MpegAudioContext *s, int ch, short *samples, int incr) 
275 
{ 
276 
short *p, *q;

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

278 
short tmp[64]; 
279 
int tmp1[32]; 
280 
int *out;

281  
282 
// print_pow1(samples, 1152);

283  
284 
offset = s>samples_offset[ch]; 
285 
out = &s>sb_samples[ch][0][0][0]; 
286 
for(j=0;j<36;j++) { 
287 
/* 32 samples at once */

288 
for(i=0;i<32;i++) { 
289 
s>samples_buf[ch][offset + (31  i)] = samples[0]; 
290 
samples += incr; 
291 
} 
292  
293 
/* filter */

294 
p = s>samples_buf[ch] + offset; 
295 
q = filter_bank; 
296 
/* maxsum = 23169 */

297 
for(i=0;i<64;i++) { 
298 
sum = p[0*64] * q[0*64]; 
299 
sum += p[1*64] * q[1*64]; 
300 
sum += p[2*64] * q[2*64]; 
301 
sum += p[3*64] * q[3*64]; 
302 
sum += p[4*64] * q[4*64]; 
303 
sum += p[5*64] * q[5*64]; 
304 
sum += p[6*64] * q[6*64]; 
305 
sum += p[7*64] * q[7*64]; 
306 
tmp[i] = sum >> 14;

307 
p++; 
308 
q++; 
309 
} 
310 
tmp1[0] = tmp[16]; 
311 
for( i=1; i<=16; i++ ) tmp1[i] = tmp[i+16]+tmp[16i]; 
312 
for( i=17; i<=31; i++ ) tmp1[i] = tmp[i+16]tmp[80i]; 
313  
314 
/* integer IDCT 32 with normalization. XXX: There may be some

315 
overflow left */

316 
norm = 0;

317 
for(i=0;i<32;i++) { 
318 
norm = abs(tmp1[i]); 
319 
} 
320 
n = av_log2(norm)  12;

321 
if (n > 0) { 
322 
for(i=0;i<32;i++) 
323 
tmp1[i] >>= n; 
324 
} else {

325 
n = 0;

326 
} 
327  
328 
idct32(out, tmp1, s>sblimit, n); 
329  
330 
/* advance of 32 samples */

331 
offset = 32;

332 
out += 32;

333 
/* handle the wrap around */

334 
if (offset < 0) { 
335 
memmove(s>samples_buf[ch] + SAMPLES_BUF_SIZE  (512  32), 
336 
s>samples_buf[ch], (512  32) * 2); 
337 
offset = SAMPLES_BUF_SIZE  512;

338 
} 
339 
} 
340 
s>samples_offset[ch] = offset; 
341  
342 
// print_pow(s>sb_samples, 1152);

343 
} 
344  
345 
static void compute_scale_factors(unsigned char scale_code[SBLIMIT], 
346 
unsigned char scale_factors[SBLIMIT][3], 
347 
int sb_samples[3][12][SBLIMIT], 
348 
int sblimit)

349 
{ 
350 
int *p, vmax, v, n, i, j, k, code;

351 
int index, d1, d2;

352 
unsigned char *sf = &scale_factors[0][0]; 
353 

354 
for(j=0;j<sblimit;j++) { 
355 
for(i=0;i<3;i++) { 
356 
/* find the max absolute value */

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

358 
vmax = abs(*p); 
359 
for(k=1;k<12;k++) { 
360 
p += SBLIMIT; 
361 
v = abs(*p); 
362 
if (v > vmax)

363 
vmax = v; 
364 
} 
365 
/* compute the scale factor index using log 2 computations */

366 
if (vmax > 0) { 
367 
n = av_log2(vmax); 
368 
/* n is the position of the MSB of vmax. now

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

370 
index = (21  n) * 3  3; 
371 
if (index >= 0) { 
372 
while (vmax <= scale_factor_table[index+1]) 
373 
index++; 
374 
} else {

375 
index = 0; /* very unlikely case of overflow */ 
376 
} 
377 
} else {

378 
index = 63;

379 
} 
380 

381 
#if 0

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

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

384 
#endif

385 
/* store the scale factor */

386 
assert(index >=0 && index <= 63); 
387 
sf[i] = index; 
388 
} 
389  
390 
/* compute the transmission factor : look if the scale factors

391 
are close enough to each other */

392 
d1 = scale_diff_table[sf[0]  sf[1] + 64]; 
393 
d2 = scale_diff_table[sf[1]  sf[2] + 64]; 
394 

395 
/* handle the 25 cases */

396 
switch(d1 * 5 + d2) { 
397 
case 0*5+0: 
398 
case 0*5+4: 
399 
case 3*5+4: 
400 
case 4*5+0: 
401 
case 4*5+4: 
402 
code = 0;

403 
break;

404 
case 0*5+1: 
405 
case 0*5+2: 
406 
case 4*5+1: 
407 
case 4*5+2: 
408 
code = 3;

409 
sf[2] = sf[1]; 
410 
break;

411 
case 0*5+3: 
412 
case 4*5+3: 
413 
code = 3;

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

416 
case 1*5+0: 
417 
case 1*5+4: 
418 
case 2*5+4: 
419 
code = 1;

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

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

428 
sf[1] = sf[2] = sf[0]; 
429 
break;

430 
case 2*5+3: 
431 
case 3*5+3: 
432 
code = 2;

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

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

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

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

443 
if (sf[0] > sf[2]) 
444 
sf[0] = sf[2]; 
445 
sf[1] = sf[2] = sf[0]; 
446 
break;

447 
default:

448 
abort(); 
449 
} 
450 

451 
#if 0

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

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

454 
#endif

455 
scale_code[j] = code; 
456 
sf += 3;

457 
} 
458 
} 
459  
460 
/* The most important function : psycho acoustic module. In this

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

462 
but also this is the simpler. */

463 
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) 
464 
{ 
465 
int i;

466  
467 
for(i=0;i<s>sblimit;i++) { 
468 
smr[i] = (int)(fixed_smr[i] * 10); 
469 
} 
470 
} 
471  
472  
473 
#define SB_NOTALLOCATED 0 
474 
#define SB_ALLOCATED 1 
475 
#define SB_NOMORE 2 
476  
477 
/* Try to maximize the smr while using a number of bits inferior to

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

479 
smaller than other encoders :) */

480 
static void compute_bit_allocation(MpegAudioContext *s, 
481 
short smr1[MPA_MAX_CHANNELS][SBLIMIT],

482 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], 
483 
int *padding)

484 
{ 
485 
int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;

486 
int incr;

487 
short smr[MPA_MAX_CHANNELS][SBLIMIT];

488 
unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT]; 
489 
const unsigned char *alloc; 
490  
491 
memcpy(smr, smr1, s>nb_channels * sizeof(short) * SBLIMIT); 
492 
memset(subband_status, SB_NOTALLOCATED, s>nb_channels * SBLIMIT); 
493 
memset(bit_alloc, 0, s>nb_channels * SBLIMIT);

494 

495 
/* compute frame size and padding */

496 
max_frame_size = s>frame_size; 
497 
s>frame_frac += s>frame_frac_incr; 
498 
if (s>frame_frac >= 65536) { 
499 
s>frame_frac = 65536;

500 
s>do_padding = 1;

501 
max_frame_size += 8;

502 
} else {

503 
s>do_padding = 0;

504 
} 
505  
506 
/* compute the header + bit alloc size */

507 
current_frame_size = 32;

508 
alloc = s>alloc_table; 
509 
for(i=0;i<s>sblimit;i++) { 
510 
incr = alloc[0];

511 
current_frame_size += incr * s>nb_channels; 
512 
alloc += 1 << incr;

513 
} 
514 
for(;;) {

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

516 
max_sb = 1;

517 
max_ch = 1;

518 
max_smr = 0x80000000;

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

522 
max_smr = smr[ch][i]; 
523 
max_sb = i; 
524 
max_ch = ch; 
525 
} 
526 
} 
527 
} 
528 
#if 0

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

530 
current_frame_size, max_frame_size, max_sb,

531 
bit_alloc[max_sb]);

532 
#endif

533 
if (max_sb < 0) 
534 
break;

535 

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

537 
pointer table) */

538 
alloc = s>alloc_table; 
539 
for(i=0;i<max_sb;i++) { 
540 
alloc += 1 << alloc[0]; 
541 
} 
542  
543 
if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {

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

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

547 
} else {

548 
/* increments bit allocation */

549 
b = bit_alloc[max_ch][max_sb]; 
550 
incr = total_quant_bits[alloc[b + 1]] 

551 
total_quant_bits[alloc[b]]; 
552 
} 
553  
554 
if (current_frame_size + incr <= max_frame_size) {

555 
/* can increase size */

556 
b = ++bit_alloc[max_ch][max_sb]; 
557 
current_frame_size += incr; 
558 
/* decrease smr by the resolution we added */

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

561 
if (b == ((1 << alloc[0])  1)) 
562 
subband_status[max_ch][max_sb] = SB_NOMORE; 
563 
else

564 
subband_status[max_ch][max_sb] = SB_ALLOCATED; 
565 
} else {

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

567 
subband_status[max_ch][max_sb] = SB_NOMORE; 
568 
} 
569 
} 
570 
*padding = max_frame_size  current_frame_size; 
571 
assert(*padding >= 0);

572  
573 
#if 0

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

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

576 
}

577 
printf("\n");

578 
#endif

579 
} 
580  
581 
/*

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

583 
* compared to other encoders :)

584 
*/

585 
static void encode_frame(MpegAudioContext *s, 
586 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], 
587 
int padding)

588 
{ 
589 
int i, j, k, l, bit_alloc_bits, b, ch;

590 
unsigned char *sf; 
591 
int q[3]; 
592 
PutBitContext *p = &s>pb; 
593  
594 
/* header */

595  
596 
put_bits(p, 12, 0xfff); 
597 
put_bits(p, 1, 1  s>lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */ 
598 
put_bits(p, 2, 42); /* layer 2 */ 
599 
put_bits(p, 1, 1); /* no error protection */ 
600 
put_bits(p, 4, s>bitrate_index);

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

602 
put_bits(p, 1, s>do_padding); /* use padding */ 
603 
put_bits(p, 1, 0); /* private_bit */ 
604 
put_bits(p, 2, s>nb_channels == 2 ? MPA_STEREO : MPA_MONO); 
605 
put_bits(p, 2, 0); /* mode_ext */ 
606 
put_bits(p, 1, 0); /* no copyright */ 
607 
put_bits(p, 1, 1); /* original */ 
608 
put_bits(p, 2, 0); /* no emphasis */ 
609  
610 
/* bit allocation */

611 
j = 0;

612 
for(i=0;i<s>sblimit;i++) { 
613 
bit_alloc_bits = s>alloc_table[j]; 
614 
for(ch=0;ch<s>nb_channels;ch++) { 
615 
put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); 
616 
} 
617 
j += 1 << bit_alloc_bits;

618 
} 
619 

620 
/* scale codes */

621 
for(i=0;i<s>sblimit;i++) { 
622 
for(ch=0;ch<s>nb_channels;ch++) { 
623 
if (bit_alloc[ch][i])

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

625 
} 
626 
} 
627  
628 
/* scale factors */

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

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

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

634 
case 0: 
635 
put_bits(p, 6, sf[0]); 
636 
put_bits(p, 6, sf[1]); 
637 
put_bits(p, 6, sf[2]); 
638 
break;

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

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

647 
} 
648 
} 
649 
} 
650 
} 
651 

652 
/* quantization & write sub band samples */

653  
654 
for(k=0;k<3;k++) { 
655 
for(l=0;l<12;l+=3) { 
656 
j = 0;

657 
for(i=0;i<s>sblimit;i++) { 
658 
bit_alloc_bits = s>alloc_table[j]; 
659 
for(ch=0;ch<s>nb_channels;ch++) { 
660 
b = bit_alloc[ch][i]; 
661 
if (b) {

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

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

664 
qindex = s>alloc_table[j+b]; 
665 
steps = quant_steps[qindex]; 
666 
for(m=0;m<3;m++) { 
667 
sample = s>sb_samples[ch][k][l + m][i]; 
668 
/* divide by scale factor */

669 
#ifdef USE_FLOATS

670 
{ 
671 
float a;

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

673 
q[m] = (int)((a + 1.0) * steps * 0.5); 
674 
} 
675 
#else

676 
{ 
677 
int q1, e, shift, mult;

678 
e = s>scale_factors[ch][i][k]; 
679 
shift = scale_factor_shift[e]; 
680 
mult = scale_factor_mult[e]; 
681 

682 
/* normalize to P bits */

683 
if (shift < 0) 
684 
q1 = sample << (shift); 
685 
else

686 
q1 = sample >> shift; 
687 
q1 = (q1 * mult) >> P; 
688 
q[m] = ((q1 + (1 << P)) * steps) >> (P + 1); 
689 
} 
690 
#endif

691 
if (q[m] >= steps)

692 
q[m] = steps  1;

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

694 
} 
695 
bits = quant_bits[qindex]; 
696 
if (bits < 0) { 
697 
/* group the 3 values to save bits */

698 
put_bits(p, bits, 
699 
q[0] + steps * (q[1] + steps * q[2])); 
700 
#if 0

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

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

703 
#endif

704 
} else {

705 
#if 0

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

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

708 
#endif

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

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

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

712 
} 
713 
} 
714 
} 
715 
/* next subband in alloc table */

716 
j += 1 << bit_alloc_bits;

717 
} 
718 
} 
719 
} 
720  
721 
/* padding */

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

726 
flush_put_bits(p); 
727 
} 
728  
729 
int MPA_encode_frame(AVCodecContext *avctx,

730 
unsigned char *frame, int buf_size, void *data) 
731 
{ 
732 
MpegAudioContext *s = avctx>priv_data; 
733 
short *samples = data;

734 
short smr[MPA_MAX_CHANNELS][SBLIMIT];

735 
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; 
736 
int padding, i;

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

755 
s>nb_samples += MPA_FRAME_SIZE; 
756 
return s>pb.buf_ptr  s>pb.buf;

757 
} 
758  
759  
760 
AVCodec mp2_encoder = { 
761 
"mp2",

762 
CODEC_TYPE_AUDIO, 
763 
CODEC_ID_MP2, 
764 
sizeof(MpegAudioContext),

765 
MPA_encode_init, 
766 
MPA_encode_frame, 
767 
NULL,

768 
}; 