Be more verbose in describing VQF demuxer
[ffmpeg-lucabe.git] / libavcodec / mpegaudioenc.c
blob5caa83aaca2121c07e3a5d35dea5842dcf49700d
1 /*
2 * The simplest mpeg audio layer 2 encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file libavcodec/mpegaudio.c
24 * The simplest mpeg audio layer 2 encoder.
27 #include "avcodec.h"
28 #include "bitstream.h"
30 #undef CONFIG_MPEGAUDIO_HP
31 #define CONFIG_MPEGAUDIO_HP 0
32 #include "mpegaudio.h"
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)
38 #define SAMPLES_BUF_SIZE 4096
40 typedef struct MpegAudioContext {
41 PutBitContext pb;
42 int nb_channels;
43 int freq, bit_rate;
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 int64_t nb_samples; /* total number of samples encoded */
49 /* padding computation */
50 int frame_frac, frame_frac_incr, do_padding;
51 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
52 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
53 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
54 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
55 /* code to group 3 scale factors */
56 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
57 int sblimit; /* number of used subbands */
58 const unsigned char *alloc_table;
59 } MpegAudioContext;
61 /* define it to use floats in quantization (I don't like floats !) */
62 //#define USE_FLOATS
64 #include "mpegaudiodata.h"
65 #include "mpegaudiotab.h"
67 static av_cold int MPA_encode_init(AVCodecContext *avctx)
69 MpegAudioContext *s = avctx->priv_data;
70 int freq = avctx->sample_rate;
71 int bitrate = avctx->bit_rate;
72 int channels = avctx->channels;
73 int i, v, table;
74 float a;
76 if (channels <= 0 || channels > 2){
77 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
78 return -1;
80 bitrate = bitrate / 1000;
81 s->nb_channels = channels;
82 s->freq = freq;
83 s->bit_rate = bitrate * 1000;
84 avctx->frame_size = MPA_FRAME_SIZE;
86 /* encoding freq */
87 s->lsf = 0;
88 for(i=0;i<3;i++) {
89 if (ff_mpa_freq_tab[i] == freq)
90 break;
91 if ((ff_mpa_freq_tab[i] / 2) == freq) {
92 s->lsf = 1;
93 break;
96 if (i == 3){
97 av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
98 return -1;
100 s->freq_index = i;
102 /* encoding bitrate & frequency */
103 for(i=0;i<15;i++) {
104 if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
105 break;
107 if (i == 15){
108 av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
109 return -1;
111 s->bitrate_index = i;
113 /* compute total header size & pad bit */
115 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
116 s->frame_size = ((int)a) * 8;
118 /* frame fractional size to compute padding */
119 s->frame_frac = 0;
120 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
122 /* select the right allocation table */
123 table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
125 /* number of used subbands */
126 s->sblimit = ff_mpa_sblimit_table[table];
127 s->alloc_table = ff_mpa_alloc_tables[table];
129 #ifdef DEBUG
130 av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
131 bitrate, freq, s->frame_size, table, s->frame_frac_incr);
132 #endif
134 for(i=0;i<s->nb_channels;i++)
135 s->samples_offset[i] = 0;
137 for(i=0;i<257;i++) {
138 int v;
139 v = ff_mpa_enwindow[i];
140 #if WFRAC_BITS != 16
141 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
142 #endif
143 filter_bank[i] = v;
144 if ((i & 63) != 0)
145 v = -v;
146 if (i != 0)
147 filter_bank[512 - i] = v;
150 for(i=0;i<64;i++) {
151 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
152 if (v <= 0)
153 v = 1;
154 scale_factor_table[i] = v;
155 #ifdef USE_FLOATS
156 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
157 #else
158 #define P 15
159 scale_factor_shift[i] = 21 - P - (i / 3);
160 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
161 #endif
163 for(i=0;i<128;i++) {
164 v = i - 64;
165 if (v <= -3)
166 v = 0;
167 else if (v < 0)
168 v = 1;
169 else if (v == 0)
170 v = 2;
171 else if (v < 3)
172 v = 3;
173 else
174 v = 4;
175 scale_diff_table[i] = v;
178 for(i=0;i<17;i++) {
179 v = ff_mpa_quant_bits[i];
180 if (v < 0)
181 v = -v;
182 else
183 v = v * 3;
184 total_quant_bits[i] = 12 * v;
187 avctx->coded_frame= avcodec_alloc_frame();
188 avctx->coded_frame->key_frame= 1;
190 return 0;
193 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
194 static void idct32(int *out, int *tab)
196 int i, j;
197 int *t, *t1, xr;
198 const int *xp = costab32;
200 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
202 t = tab + 30;
203 t1 = tab + 2;
204 do {
205 t[0] += t[-4];
206 t[1] += t[1 - 4];
207 t -= 4;
208 } while (t != t1);
210 t = tab + 28;
211 t1 = tab + 4;
212 do {
213 t[0] += t[-8];
214 t[1] += t[1-8];
215 t[2] += t[2-8];
216 t[3] += t[3-8];
217 t -= 8;
218 } while (t != t1);
220 t = tab;
221 t1 = tab + 32;
222 do {
223 t[ 3] = -t[ 3];
224 t[ 6] = -t[ 6];
226 t[11] = -t[11];
227 t[12] = -t[12];
228 t[13] = -t[13];
229 t[15] = -t[15];
230 t += 16;
231 } while (t != t1);
234 t = tab;
235 t1 = tab + 8;
236 do {
237 int x1, x2, x3, x4;
239 x3 = MUL(t[16], FIX(SQRT2*0.5));
240 x4 = t[0] - x3;
241 x3 = t[0] + x3;
243 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
244 x1 = MUL((t[8] - x2), xp[0]);
245 x2 = MUL((t[8] + x2), xp[1]);
247 t[ 0] = x3 + x1;
248 t[ 8] = x4 - x2;
249 t[16] = x4 + x2;
250 t[24] = x3 - x1;
251 t++;
252 } while (t != t1);
254 xp += 2;
255 t = tab;
256 t1 = tab + 4;
257 do {
258 xr = MUL(t[28],xp[0]);
259 t[28] = (t[0] - xr);
260 t[0] = (t[0] + xr);
262 xr = MUL(t[4],xp[1]);
263 t[ 4] = (t[24] - xr);
264 t[24] = (t[24] + xr);
266 xr = MUL(t[20],xp[2]);
267 t[20] = (t[8] - xr);
268 t[ 8] = (t[8] + xr);
270 xr = MUL(t[12],xp[3]);
271 t[12] = (t[16] - xr);
272 t[16] = (t[16] + xr);
273 t++;
274 } while (t != t1);
275 xp += 4;
277 for (i = 0; i < 4; i++) {
278 xr = MUL(tab[30-i*4],xp[0]);
279 tab[30-i*4] = (tab[i*4] - xr);
280 tab[ i*4] = (tab[i*4] + xr);
282 xr = MUL(tab[ 2+i*4],xp[1]);
283 tab[ 2+i*4] = (tab[28-i*4] - xr);
284 tab[28-i*4] = (tab[28-i*4] + xr);
286 xr = MUL(tab[31-i*4],xp[0]);
287 tab[31-i*4] = (tab[1+i*4] - xr);
288 tab[ 1+i*4] = (tab[1+i*4] + xr);
290 xr = MUL(tab[ 3+i*4],xp[1]);
291 tab[ 3+i*4] = (tab[29-i*4] - xr);
292 tab[29-i*4] = (tab[29-i*4] + xr);
294 xp += 2;
297 t = tab + 30;
298 t1 = tab + 1;
299 do {
300 xr = MUL(t1[0], *xp);
301 t1[0] = (t[0] - xr);
302 t[0] = (t[0] + xr);
303 t -= 2;
304 t1 += 2;
305 xp++;
306 } while (t >= tab);
308 for(i=0;i<32;i++) {
309 out[i] = tab[bitinv32[i]];
313 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
315 static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
317 short *p, *q;
318 int sum, offset, i, j;
319 int tmp[64];
320 int tmp1[32];
321 int *out;
323 // print_pow1(samples, 1152);
325 offset = s->samples_offset[ch];
326 out = &s->sb_samples[ch][0][0][0];
327 for(j=0;j<36;j++) {
328 /* 32 samples at once */
329 for(i=0;i<32;i++) {
330 s->samples_buf[ch][offset + (31 - i)] = samples[0];
331 samples += incr;
334 /* filter */
335 p = s->samples_buf[ch] + offset;
336 q = filter_bank;
337 /* maxsum = 23169 */
338 for(i=0;i<64;i++) {
339 sum = p[0*64] * q[0*64];
340 sum += p[1*64] * q[1*64];
341 sum += p[2*64] * q[2*64];
342 sum += p[3*64] * q[3*64];
343 sum += p[4*64] * q[4*64];
344 sum += p[5*64] * q[5*64];
345 sum += p[6*64] * q[6*64];
346 sum += p[7*64] * q[7*64];
347 tmp[i] = sum;
348 p++;
349 q++;
351 tmp1[0] = tmp[16] >> WSHIFT;
352 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
353 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
355 idct32(out, tmp1);
357 /* advance of 32 samples */
358 offset -= 32;
359 out += 32;
360 /* handle the wrap around */
361 if (offset < 0) {
362 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
363 s->samples_buf[ch], (512 - 32) * 2);
364 offset = SAMPLES_BUF_SIZE - 512;
367 s->samples_offset[ch] = offset;
369 // print_pow(s->sb_samples, 1152);
372 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
373 unsigned char scale_factors[SBLIMIT][3],
374 int sb_samples[3][12][SBLIMIT],
375 int sblimit)
377 int *p, vmax, v, n, i, j, k, code;
378 int index, d1, d2;
379 unsigned char *sf = &scale_factors[0][0];
381 for(j=0;j<sblimit;j++) {
382 for(i=0;i<3;i++) {
383 /* find the max absolute value */
384 p = &sb_samples[i][0][j];
385 vmax = abs(*p);
386 for(k=1;k<12;k++) {
387 p += SBLIMIT;
388 v = abs(*p);
389 if (v > vmax)
390 vmax = v;
392 /* compute the scale factor index using log 2 computations */
393 if (vmax > 1) {
394 n = av_log2(vmax);
395 /* n is the position of the MSB of vmax. now
396 use at most 2 compares to find the index */
397 index = (21 - n) * 3 - 3;
398 if (index >= 0) {
399 while (vmax <= scale_factor_table[index+1])
400 index++;
401 } else {
402 index = 0; /* very unlikely case of overflow */
404 } else {
405 index = 62; /* value 63 is not allowed */
408 #if 0
409 printf("%2d:%d in=%x %x %d\n",
410 j, i, vmax, scale_factor_table[index], index);
411 #endif
412 /* store the scale factor */
413 assert(index >=0 && index <= 63);
414 sf[i] = index;
417 /* compute the transmission factor : look if the scale factors
418 are close enough to each other */
419 d1 = scale_diff_table[sf[0] - sf[1] + 64];
420 d2 = scale_diff_table[sf[1] - sf[2] + 64];
422 /* handle the 25 cases */
423 switch(d1 * 5 + d2) {
424 case 0*5+0:
425 case 0*5+4:
426 case 3*5+4:
427 case 4*5+0:
428 case 4*5+4:
429 code = 0;
430 break;
431 case 0*5+1:
432 case 0*5+2:
433 case 4*5+1:
434 case 4*5+2:
435 code = 3;
436 sf[2] = sf[1];
437 break;
438 case 0*5+3:
439 case 4*5+3:
440 code = 3;
441 sf[1] = sf[2];
442 break;
443 case 1*5+0:
444 case 1*5+4:
445 case 2*5+4:
446 code = 1;
447 sf[1] = sf[0];
448 break;
449 case 1*5+1:
450 case 1*5+2:
451 case 2*5+0:
452 case 2*5+1:
453 case 2*5+2:
454 code = 2;
455 sf[1] = sf[2] = sf[0];
456 break;
457 case 2*5+3:
458 case 3*5+3:
459 code = 2;
460 sf[0] = sf[1] = sf[2];
461 break;
462 case 3*5+0:
463 case 3*5+1:
464 case 3*5+2:
465 code = 2;
466 sf[0] = sf[2] = sf[1];
467 break;
468 case 1*5+3:
469 code = 2;
470 if (sf[0] > sf[2])
471 sf[0] = sf[2];
472 sf[1] = sf[2] = sf[0];
473 break;
474 default:
475 assert(0); //cannot happen
476 code = 0; /* kill warning */
479 #if 0
480 printf("%d: %2d %2d %2d %d %d -> %d\n", j,
481 sf[0], sf[1], sf[2], d1, d2, code);
482 #endif
483 scale_code[j] = code;
484 sf += 3;
488 /* The most important function : psycho acoustic module. In this
489 encoder there is basically none, so this is the worst you can do,
490 but also this is the simpler. */
491 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
493 int i;
495 for(i=0;i<s->sblimit;i++) {
496 smr[i] = (int)(fixed_smr[i] * 10);
501 #define SB_NOTALLOCATED 0
502 #define SB_ALLOCATED 1
503 #define SB_NOMORE 2
505 /* Try to maximize the smr while using a number of bits inferior to
506 the frame size. I tried to make the code simpler, faster and
507 smaller than other encoders :-) */
508 static void compute_bit_allocation(MpegAudioContext *s,
509 short smr1[MPA_MAX_CHANNELS][SBLIMIT],
510 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
511 int *padding)
513 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
514 int incr;
515 short smr[MPA_MAX_CHANNELS][SBLIMIT];
516 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
517 const unsigned char *alloc;
519 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
520 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
521 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
523 /* compute frame size and padding */
524 max_frame_size = s->frame_size;
525 s->frame_frac += s->frame_frac_incr;
526 if (s->frame_frac >= 65536) {
527 s->frame_frac -= 65536;
528 s->do_padding = 1;
529 max_frame_size += 8;
530 } else {
531 s->do_padding = 0;
534 /* compute the header + bit alloc size */
535 current_frame_size = 32;
536 alloc = s->alloc_table;
537 for(i=0;i<s->sblimit;i++) {
538 incr = alloc[0];
539 current_frame_size += incr * s->nb_channels;
540 alloc += 1 << incr;
542 for(;;) {
543 /* look for the subband with the largest signal to mask ratio */
544 max_sb = -1;
545 max_ch = -1;
546 max_smr = INT_MIN;
547 for(ch=0;ch<s->nb_channels;ch++) {
548 for(i=0;i<s->sblimit;i++) {
549 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
550 max_smr = smr[ch][i];
551 max_sb = i;
552 max_ch = ch;
556 #if 0
557 printf("current=%d max=%d max_sb=%d alloc=%d\n",
558 current_frame_size, max_frame_size, max_sb,
559 bit_alloc[max_sb]);
560 #endif
561 if (max_sb < 0)
562 break;
564 /* find alloc table entry (XXX: not optimal, should use
565 pointer table) */
566 alloc = s->alloc_table;
567 for(i=0;i<max_sb;i++) {
568 alloc += 1 << alloc[0];
571 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
572 /* nothing was coded for this band: add the necessary bits */
573 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
574 incr += total_quant_bits[alloc[1]];
575 } else {
576 /* increments bit allocation */
577 b = bit_alloc[max_ch][max_sb];
578 incr = total_quant_bits[alloc[b + 1]] -
579 total_quant_bits[alloc[b]];
582 if (current_frame_size + incr <= max_frame_size) {
583 /* can increase size */
584 b = ++bit_alloc[max_ch][max_sb];
585 current_frame_size += incr;
586 /* decrease smr by the resolution we added */
587 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
588 /* max allocation size reached ? */
589 if (b == ((1 << alloc[0]) - 1))
590 subband_status[max_ch][max_sb] = SB_NOMORE;
591 else
592 subband_status[max_ch][max_sb] = SB_ALLOCATED;
593 } else {
594 /* cannot increase the size of this subband */
595 subband_status[max_ch][max_sb] = SB_NOMORE;
598 *padding = max_frame_size - current_frame_size;
599 assert(*padding >= 0);
601 #if 0
602 for(i=0;i<s->sblimit;i++) {
603 printf("%d ", bit_alloc[i]);
605 printf("\n");
606 #endif
610 * Output the mpeg audio layer 2 frame. Note how the code is small
611 * compared to other encoders :-)
613 static void encode_frame(MpegAudioContext *s,
614 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
615 int padding)
617 int i, j, k, l, bit_alloc_bits, b, ch;
618 unsigned char *sf;
619 int q[3];
620 PutBitContext *p = &s->pb;
622 /* header */
624 put_bits(p, 12, 0xfff);
625 put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
626 put_bits(p, 2, 4-2); /* layer 2 */
627 put_bits(p, 1, 1); /* no error protection */
628 put_bits(p, 4, s->bitrate_index);
629 put_bits(p, 2, s->freq_index);
630 put_bits(p, 1, s->do_padding); /* use padding */
631 put_bits(p, 1, 0); /* private_bit */
632 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
633 put_bits(p, 2, 0); /* mode_ext */
634 put_bits(p, 1, 0); /* no copyright */
635 put_bits(p, 1, 1); /* original */
636 put_bits(p, 2, 0); /* no emphasis */
638 /* bit allocation */
639 j = 0;
640 for(i=0;i<s->sblimit;i++) {
641 bit_alloc_bits = s->alloc_table[j];
642 for(ch=0;ch<s->nb_channels;ch++) {
643 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
645 j += 1 << bit_alloc_bits;
648 /* scale codes */
649 for(i=0;i<s->sblimit;i++) {
650 for(ch=0;ch<s->nb_channels;ch++) {
651 if (bit_alloc[ch][i])
652 put_bits(p, 2, s->scale_code[ch][i]);
656 /* scale factors */
657 for(i=0;i<s->sblimit;i++) {
658 for(ch=0;ch<s->nb_channels;ch++) {
659 if (bit_alloc[ch][i]) {
660 sf = &s->scale_factors[ch][i][0];
661 switch(s->scale_code[ch][i]) {
662 case 0:
663 put_bits(p, 6, sf[0]);
664 put_bits(p, 6, sf[1]);
665 put_bits(p, 6, sf[2]);
666 break;
667 case 3:
668 case 1:
669 put_bits(p, 6, sf[0]);
670 put_bits(p, 6, sf[2]);
671 break;
672 case 2:
673 put_bits(p, 6, sf[0]);
674 break;
680 /* quantization & write sub band samples */
682 for(k=0;k<3;k++) {
683 for(l=0;l<12;l+=3) {
684 j = 0;
685 for(i=0;i<s->sblimit;i++) {
686 bit_alloc_bits = s->alloc_table[j];
687 for(ch=0;ch<s->nb_channels;ch++) {
688 b = bit_alloc[ch][i];
689 if (b) {
690 int qindex, steps, m, sample, bits;
691 /* we encode 3 sub band samples of the same sub band at a time */
692 qindex = s->alloc_table[j+b];
693 steps = ff_mpa_quant_steps[qindex];
694 for(m=0;m<3;m++) {
695 sample = s->sb_samples[ch][k][l + m][i];
696 /* divide by scale factor */
697 #ifdef USE_FLOATS
699 float a;
700 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
701 q[m] = (int)((a + 1.0) * steps * 0.5);
703 #else
705 int q1, e, shift, mult;
706 e = s->scale_factors[ch][i][k];
707 shift = scale_factor_shift[e];
708 mult = scale_factor_mult[e];
710 /* normalize to P bits */
711 if (shift < 0)
712 q1 = sample << (-shift);
713 else
714 q1 = sample >> shift;
715 q1 = (q1 * mult) >> P;
716 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
718 #endif
719 if (q[m] >= steps)
720 q[m] = steps - 1;
721 assert(q[m] >= 0 && q[m] < steps);
723 bits = ff_mpa_quant_bits[qindex];
724 if (bits < 0) {
725 /* group the 3 values to save bits */
726 put_bits(p, -bits,
727 q[0] + steps * (q[1] + steps * q[2]));
728 #if 0
729 printf("%d: gr1 %d\n",
730 i, q[0] + steps * (q[1] + steps * q[2]));
731 #endif
732 } else {
733 #if 0
734 printf("%d: gr3 %d %d %d\n",
735 i, q[0], q[1], q[2]);
736 #endif
737 put_bits(p, bits, q[0]);
738 put_bits(p, bits, q[1]);
739 put_bits(p, bits, q[2]);
743 /* next subband in alloc table */
744 j += 1 << bit_alloc_bits;
749 /* padding */
750 for(i=0;i<padding;i++)
751 put_bits(p, 1, 0);
753 /* flush */
754 flush_put_bits(p);
757 static int MPA_encode_frame(AVCodecContext *avctx,
758 unsigned char *frame, int buf_size, void *data)
760 MpegAudioContext *s = avctx->priv_data;
761 short *samples = data;
762 short smr[MPA_MAX_CHANNELS][SBLIMIT];
763 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
764 int padding, i;
766 for(i=0;i<s->nb_channels;i++) {
767 filter(s, i, samples + i, s->nb_channels);
770 for(i=0;i<s->nb_channels;i++) {
771 compute_scale_factors(s->scale_code[i], s->scale_factors[i],
772 s->sb_samples[i], s->sblimit);
774 for(i=0;i<s->nb_channels;i++) {
775 psycho_acoustic_model(s, smr[i]);
777 compute_bit_allocation(s, smr, bit_alloc, &padding);
779 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
781 encode_frame(s, bit_alloc, padding);
783 s->nb_samples += MPA_FRAME_SIZE;
784 return pbBufPtr(&s->pb) - s->pb.buf;
787 static av_cold int MPA_encode_close(AVCodecContext *avctx)
789 av_freep(&avctx->coded_frame);
790 return 0;
793 AVCodec mp2_encoder = {
794 "mp2",
795 CODEC_TYPE_AUDIO,
796 CODEC_ID_MP2,
797 sizeof(MpegAudioContext),
798 MPA_encode_init,
799 MPA_encode_frame,
800 MPA_encode_close,
801 NULL,
802 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
803 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
806 #undef FIX