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