Rename function
[FFMpeg-mirror/DVCPRO-HD.git] / libavcodec / mpegaudioenc.c
blob07b550940156af5717a2ef1202d8721154447de3
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 mpegaudio.c
24 * The simplest mpeg audio layer 2 encoder.
27 #include "avcodec.h"
28 #include "bitstream.h"
29 #include "mpegaudio.h"
31 /* currently, cannot change these constants (need to modify
32 quantization stage) */
33 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
35 #define SAMPLES_BUF_SIZE 4096
37 typedef struct MpegAudioContext {
38 PutBitContext pb;
39 int nb_channels;
40 int freq, bit_rate;
41 int lsf; /* 1 if mpeg2 low bitrate selected */
42 int bitrate_index; /* bit rate */
43 int freq_index;
44 int frame_size; /* frame size, in bits, without padding */
45 int64_t nb_samples; /* total number of samples encoded */
46 /* padding computation */
47 int frame_frac, frame_frac_incr, do_padding;
48 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
49 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
50 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
51 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
52 /* code to group 3 scale factors */
53 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
54 int sblimit; /* number of used subbands */
55 const unsigned char *alloc_table;
56 } MpegAudioContext;
58 /* define it to use floats in quantization (I don't like floats !) */
59 //#define USE_FLOATS
61 #include "mpegaudiodata.h"
62 #include "mpegaudiotab.h"
64 static av_cold int MPA_encode_init(AVCodecContext *avctx)
66 MpegAudioContext *s = avctx->priv_data;
67 int freq = avctx->sample_rate;
68 int bitrate = avctx->bit_rate;
69 int channels = avctx->channels;
70 int i, v, table;
71 float a;
73 if (channels <= 0 || channels > 2){
74 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
75 return -1;
77 bitrate = bitrate / 1000;
78 s->nb_channels = channels;
79 s->freq = freq;
80 s->bit_rate = bitrate * 1000;
81 avctx->frame_size = MPA_FRAME_SIZE;
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;
93 if (i == 3){
94 av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
95 return -1;
97 s->freq_index = i;
99 /* encoding bitrate & frequency */
100 for(i=0;i<15;i++) {
101 if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
102 break;
104 if (i == 15){
105 av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
106 return -1;
108 s->bitrate_index = i;
110 /* compute total header size & pad bit */
112 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
113 s->frame_size = ((int)a) * 8;
115 /* frame fractional size to compute padding */
116 s->frame_frac = 0;
117 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
119 /* select the right allocation table */
120 table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
122 /* number of used subbands */
123 s->sblimit = ff_mpa_sblimit_table[table];
124 s->alloc_table = ff_mpa_alloc_tables[table];
126 #ifdef DEBUG
127 av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
128 bitrate, freq, s->frame_size, table, s->frame_frac_incr);
129 #endif
131 for(i=0;i<s->nb_channels;i++)
132 s->samples_offset[i] = 0;
134 for(i=0;i<257;i++) {
135 int v;
136 v = ff_mpa_enwindow[i];
137 #if WFRAC_BITS != 16
138 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
139 #endif
140 filter_bank[i] = v;
141 if ((i & 63) != 0)
142 v = -v;
143 if (i != 0)
144 filter_bank[512 - i] = v;
147 for(i=0;i<64;i++) {
148 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
149 if (v <= 0)
150 v = 1;
151 scale_factor_table[i] = v;
152 #ifdef USE_FLOATS
153 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
154 #else
155 #define P 15
156 scale_factor_shift[i] = 21 - P - (i / 3);
157 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
158 #endif
160 for(i=0;i<128;i++) {
161 v = i - 64;
162 if (v <= -3)
163 v = 0;
164 else if (v < 0)
165 v = 1;
166 else if (v == 0)
167 v = 2;
168 else if (v < 3)
169 v = 3;
170 else
171 v = 4;
172 scale_diff_table[i] = v;
175 for(i=0;i<17;i++) {
176 v = ff_mpa_quant_bits[i];
177 if (v < 0)
178 v = -v;
179 else
180 v = v * 3;
181 total_quant_bits[i] = 12 * v;
184 avctx->coded_frame= avcodec_alloc_frame();
185 avctx->coded_frame->key_frame= 1;
187 return 0;
190 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
191 static void idct32(int *out, int *tab)
193 int i, j;
194 int *t, *t1, xr;
195 const int *xp = costab32;
197 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
199 t = tab + 30;
200 t1 = tab + 2;
201 do {
202 t[0] += t[-4];
203 t[1] += t[1 - 4];
204 t -= 4;
205 } while (t != t1);
207 t = tab + 28;
208 t1 = tab + 4;
209 do {
210 t[0] += t[-8];
211 t[1] += t[1-8];
212 t[2] += t[2-8];
213 t[3] += t[3-8];
214 t -= 8;
215 } while (t != t1);
217 t = tab;
218 t1 = tab + 32;
219 do {
220 t[ 3] = -t[ 3];
221 t[ 6] = -t[ 6];
223 t[11] = -t[11];
224 t[12] = -t[12];
225 t[13] = -t[13];
226 t[15] = -t[15];
227 t += 16;
228 } while (t != t1);
231 t = tab;
232 t1 = tab + 8;
233 do {
234 int x1, x2, x3, x4;
236 x3 = MUL(t[16], FIX(SQRT2*0.5));
237 x4 = t[0] - x3;
238 x3 = t[0] + x3;
240 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
241 x1 = MUL((t[8] - x2), xp[0]);
242 x2 = MUL((t[8] + x2), xp[1]);
244 t[ 0] = x3 + x1;
245 t[ 8] = x4 - x2;
246 t[16] = x4 + x2;
247 t[24] = x3 - x1;
248 t++;
249 } while (t != t1);
251 xp += 2;
252 t = tab;
253 t1 = tab + 4;
254 do {
255 xr = MUL(t[28],xp[0]);
256 t[28] = (t[0] - xr);
257 t[0] = (t[0] + xr);
259 xr = MUL(t[4],xp[1]);
260 t[ 4] = (t[24] - xr);
261 t[24] = (t[24] + xr);
263 xr = MUL(t[20],xp[2]);
264 t[20] = (t[8] - xr);
265 t[ 8] = (t[8] + xr);
267 xr = MUL(t[12],xp[3]);
268 t[12] = (t[16] - xr);
269 t[16] = (t[16] + xr);
270 t++;
271 } while (t != t1);
272 xp += 4;
274 for (i = 0; i < 4; i++) {
275 xr = MUL(tab[30-i*4],xp[0]);
276 tab[30-i*4] = (tab[i*4] - xr);
277 tab[ i*4] = (tab[i*4] + xr);
279 xr = MUL(tab[ 2+i*4],xp[1]);
280 tab[ 2+i*4] = (tab[28-i*4] - xr);
281 tab[28-i*4] = (tab[28-i*4] + xr);
283 xr = MUL(tab[31-i*4],xp[0]);
284 tab[31-i*4] = (tab[1+i*4] - xr);
285 tab[ 1+i*4] = (tab[1+i*4] + xr);
287 xr = MUL(tab[ 3+i*4],xp[1]);
288 tab[ 3+i*4] = (tab[29-i*4] - xr);
289 tab[29-i*4] = (tab[29-i*4] + xr);
291 xp += 2;
294 t = tab + 30;
295 t1 = tab + 1;
296 do {
297 xr = MUL(t1[0], *xp);
298 t1[0] = (t[0] - xr);
299 t[0] = (t[0] + xr);
300 t -= 2;
301 t1 += 2;
302 xp++;
303 } while (t >= tab);
305 for(i=0;i<32;i++) {
306 out[i] = tab[bitinv32[i]];
310 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
312 static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
314 short *p, *q;
315 int sum, offset, i, j;
316 int tmp[64];
317 int tmp1[32];
318 int *out;
320 // print_pow1(samples, 1152);
322 offset = s->samples_offset[ch];
323 out = &s->sb_samples[ch][0][0][0];
324 for(j=0;j<36;j++) {
325 /* 32 samples at once */
326 for(i=0;i<32;i++) {
327 s->samples_buf[ch][offset + (31 - i)] = samples[0];
328 samples += incr;
331 /* filter */
332 p = s->samples_buf[ch] + offset;
333 q = filter_bank;
334 /* maxsum = 23169 */
335 for(i=0;i<64;i++) {
336 sum = p[0*64] * q[0*64];
337 sum += p[1*64] * q[1*64];
338 sum += p[2*64] * q[2*64];
339 sum += p[3*64] * q[3*64];
340 sum += p[4*64] * q[4*64];
341 sum += p[5*64] * q[5*64];
342 sum += p[6*64] * q[6*64];
343 sum += p[7*64] * q[7*64];
344 tmp[i] = sum;
345 p++;
346 q++;
348 tmp1[0] = tmp[16] >> WSHIFT;
349 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
350 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
352 idct32(out, tmp1);
354 /* advance of 32 samples */
355 offset -= 32;
356 out += 32;
357 /* handle the wrap around */
358 if (offset < 0) {
359 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
360 s->samples_buf[ch], (512 - 32) * 2);
361 offset = SAMPLES_BUF_SIZE - 512;
364 s->samples_offset[ch] = offset;
366 // print_pow(s->sb_samples, 1152);
369 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
370 unsigned char scale_factors[SBLIMIT][3],
371 int sb_samples[3][12][SBLIMIT],
372 int sblimit)
374 int *p, vmax, v, n, i, j, k, code;
375 int index, d1, d2;
376 unsigned char *sf = &scale_factors[0][0];
378 for(j=0;j<sblimit;j++) {
379 for(i=0;i<3;i++) {
380 /* find the max absolute value */
381 p = &sb_samples[i][0][j];
382 vmax = abs(*p);
383 for(k=1;k<12;k++) {
384 p += SBLIMIT;
385 v = abs(*p);
386 if (v > vmax)
387 vmax = v;
389 /* compute the scale factor index using log 2 computations */
390 if (vmax > 1) {
391 n = av_log2(vmax);
392 /* n is the position of the MSB of vmax. now
393 use at most 2 compares to find the index */
394 index = (21 - n) * 3 - 3;
395 if (index >= 0) {
396 while (vmax <= scale_factor_table[index+1])
397 index++;
398 } else {
399 index = 0; /* very unlikely case of overflow */
401 } else {
402 index = 62; /* value 63 is not allowed */
405 #if 0
406 printf("%2d:%d in=%x %x %d\n",
407 j, i, vmax, scale_factor_table[index], index);
408 #endif
409 /* store the scale factor */
410 assert(index >=0 && index <= 63);
411 sf[i] = index;
414 /* compute the transmission factor : look if the scale factors
415 are close enough to each other */
416 d1 = scale_diff_table[sf[0] - sf[1] + 64];
417 d2 = scale_diff_table[sf[1] - sf[2] + 64];
419 /* handle the 25 cases */
420 switch(d1 * 5 + d2) {
421 case 0*5+0:
422 case 0*5+4:
423 case 3*5+4:
424 case 4*5+0:
425 case 4*5+4:
426 code = 0;
427 break;
428 case 0*5+1:
429 case 0*5+2:
430 case 4*5+1:
431 case 4*5+2:
432 code = 3;
433 sf[2] = sf[1];
434 break;
435 case 0*5+3:
436 case 4*5+3:
437 code = 3;
438 sf[1] = sf[2];
439 break;
440 case 1*5+0:
441 case 1*5+4:
442 case 2*5+4:
443 code = 1;
444 sf[1] = sf[0];
445 break;
446 case 1*5+1:
447 case 1*5+2:
448 case 2*5+0:
449 case 2*5+1:
450 case 2*5+2:
451 code = 2;
452 sf[1] = sf[2] = sf[0];
453 break;
454 case 2*5+3:
455 case 3*5+3:
456 code = 2;
457 sf[0] = sf[1] = sf[2];
458 break;
459 case 3*5+0:
460 case 3*5+1:
461 case 3*5+2:
462 code = 2;
463 sf[0] = sf[2] = sf[1];
464 break;
465 case 1*5+3:
466 code = 2;
467 if (sf[0] > sf[2])
468 sf[0] = sf[2];
469 sf[1] = sf[2] = sf[0];
470 break;
471 default:
472 assert(0); //cannot happen
473 code = 0; /* kill warning */
476 #if 0
477 printf("%d: %2d %2d %2d %d %d -> %d\n", j,
478 sf[0], sf[1], sf[2], d1, d2, code);
479 #endif
480 scale_code[j] = code;
481 sf += 3;
485 /* The most important function : psycho acoustic module. In this
486 encoder there is basically none, so this is the worst you can do,
487 but also this is the simpler. */
488 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
490 int i;
492 for(i=0;i<s->sblimit;i++) {
493 smr[i] = (int)(fixed_smr[i] * 10);
498 #define SB_NOTALLOCATED 0
499 #define SB_ALLOCATED 1
500 #define SB_NOMORE 2
502 /* Try to maximize the smr while using a number of bits inferior to
503 the frame size. I tried to make the code simpler, faster and
504 smaller than other encoders :-) */
505 static void compute_bit_allocation(MpegAudioContext *s,
506 short smr1[MPA_MAX_CHANNELS][SBLIMIT],
507 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
508 int *padding)
510 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
511 int incr;
512 short smr[MPA_MAX_CHANNELS][SBLIMIT];
513 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
514 const unsigned char *alloc;
516 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
517 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
518 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
520 /* compute frame size and padding */
521 max_frame_size = s->frame_size;
522 s->frame_frac += s->frame_frac_incr;
523 if (s->frame_frac >= 65536) {
524 s->frame_frac -= 65536;
525 s->do_padding = 1;
526 max_frame_size += 8;
527 } else {
528 s->do_padding = 0;
531 /* compute the header + bit alloc size */
532 current_frame_size = 32;
533 alloc = s->alloc_table;
534 for(i=0;i<s->sblimit;i++) {
535 incr = alloc[0];
536 current_frame_size += incr * s->nb_channels;
537 alloc += 1 << incr;
539 for(;;) {
540 /* look for the subband with the largest signal to mask ratio */
541 max_sb = -1;
542 max_ch = -1;
543 max_smr = INT_MIN;
544 for(ch=0;ch<s->nb_channels;ch++) {
545 for(i=0;i<s->sblimit;i++) {
546 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
547 max_smr = smr[ch][i];
548 max_sb = i;
549 max_ch = ch;
553 #if 0
554 printf("current=%d max=%d max_sb=%d alloc=%d\n",
555 current_frame_size, max_frame_size, max_sb,
556 bit_alloc[max_sb]);
557 #endif
558 if (max_sb < 0)
559 break;
561 /* find alloc table entry (XXX: not optimal, should use
562 pointer table) */
563 alloc = s->alloc_table;
564 for(i=0;i<max_sb;i++) {
565 alloc += 1 << alloc[0];
568 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
569 /* nothing was coded for this band: add the necessary bits */
570 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
571 incr += total_quant_bits[alloc[1]];
572 } else {
573 /* increments bit allocation */
574 b = bit_alloc[max_ch][max_sb];
575 incr = total_quant_bits[alloc[b + 1]] -
576 total_quant_bits[alloc[b]];
579 if (current_frame_size + incr <= max_frame_size) {
580 /* can increase size */
581 b = ++bit_alloc[max_ch][max_sb];
582 current_frame_size += incr;
583 /* decrease smr by the resolution we added */
584 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
585 /* max allocation size reached ? */
586 if (b == ((1 << alloc[0]) - 1))
587 subband_status[max_ch][max_sb] = SB_NOMORE;
588 else
589 subband_status[max_ch][max_sb] = SB_ALLOCATED;
590 } else {
591 /* cannot increase the size of this subband */
592 subband_status[max_ch][max_sb] = SB_NOMORE;
595 *padding = max_frame_size - current_frame_size;
596 assert(*padding >= 0);
598 #if 0
599 for(i=0;i<s->sblimit;i++) {
600 printf("%d ", bit_alloc[i]);
602 printf("\n");
603 #endif
607 * Output the mpeg audio layer 2 frame. Note how the code is small
608 * compared to other encoders :-)
610 static void encode_frame(MpegAudioContext *s,
611 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
612 int padding)
614 int i, j, k, l, bit_alloc_bits, b, ch;
615 unsigned char *sf;
616 int q[3];
617 PutBitContext *p = &s->pb;
619 /* header */
621 put_bits(p, 12, 0xfff);
622 put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
623 put_bits(p, 2, 4-2); /* layer 2 */
624 put_bits(p, 1, 1); /* no error protection */
625 put_bits(p, 4, s->bitrate_index);
626 put_bits(p, 2, s->freq_index);
627 put_bits(p, 1, s->do_padding); /* use padding */
628 put_bits(p, 1, 0); /* private_bit */
629 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
630 put_bits(p, 2, 0); /* mode_ext */
631 put_bits(p, 1, 0); /* no copyright */
632 put_bits(p, 1, 1); /* original */
633 put_bits(p, 2, 0); /* no emphasis */
635 /* bit allocation */
636 j = 0;
637 for(i=0;i<s->sblimit;i++) {
638 bit_alloc_bits = s->alloc_table[j];
639 for(ch=0;ch<s->nb_channels;ch++) {
640 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
642 j += 1 << bit_alloc_bits;
645 /* scale codes */
646 for(i=0;i<s->sblimit;i++) {
647 for(ch=0;ch<s->nb_channels;ch++) {
648 if (bit_alloc[ch][i])
649 put_bits(p, 2, s->scale_code[ch][i]);
653 /* scale factors */
654 for(i=0;i<s->sblimit;i++) {
655 for(ch=0;ch<s->nb_channels;ch++) {
656 if (bit_alloc[ch][i]) {
657 sf = &s->scale_factors[ch][i][0];
658 switch(s->scale_code[ch][i]) {
659 case 0:
660 put_bits(p, 6, sf[0]);
661 put_bits(p, 6, sf[1]);
662 put_bits(p, 6, sf[2]);
663 break;
664 case 3:
665 case 1:
666 put_bits(p, 6, sf[0]);
667 put_bits(p, 6, sf[2]);
668 break;
669 case 2:
670 put_bits(p, 6, sf[0]);
671 break;
677 /* quantization & write sub band samples */
679 for(k=0;k<3;k++) {
680 for(l=0;l<12;l+=3) {
681 j = 0;
682 for(i=0;i<s->sblimit;i++) {
683 bit_alloc_bits = s->alloc_table[j];
684 for(ch=0;ch<s->nb_channels;ch++) {
685 b = bit_alloc[ch][i];
686 if (b) {
687 int qindex, steps, m, sample, bits;
688 /* we encode 3 sub band samples of the same sub band at a time */
689 qindex = s->alloc_table[j+b];
690 steps = ff_mpa_quant_steps[qindex];
691 for(m=0;m<3;m++) {
692 sample = s->sb_samples[ch][k][l + m][i];
693 /* divide by scale factor */
694 #ifdef USE_FLOATS
696 float a;
697 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
698 q[m] = (int)((a + 1.0) * steps * 0.5);
700 #else
702 int q1, e, shift, mult;
703 e = s->scale_factors[ch][i][k];
704 shift = scale_factor_shift[e];
705 mult = scale_factor_mult[e];
707 /* normalize to P bits */
708 if (shift < 0)
709 q1 = sample << (-shift);
710 else
711 q1 = sample >> shift;
712 q1 = (q1 * mult) >> P;
713 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
715 #endif
716 if (q[m] >= steps)
717 q[m] = steps - 1;
718 assert(q[m] >= 0 && q[m] < steps);
720 bits = ff_mpa_quant_bits[qindex];
721 if (bits < 0) {
722 /* group the 3 values to save bits */
723 put_bits(p, -bits,
724 q[0] + steps * (q[1] + steps * q[2]));
725 #if 0
726 printf("%d: gr1 %d\n",
727 i, q[0] + steps * (q[1] + steps * q[2]));
728 #endif
729 } else {
730 #if 0
731 printf("%d: gr3 %d %d %d\n",
732 i, q[0], q[1], q[2]);
733 #endif
734 put_bits(p, bits, q[0]);
735 put_bits(p, bits, q[1]);
736 put_bits(p, bits, q[2]);
740 /* next subband in alloc table */
741 j += 1 << bit_alloc_bits;
746 /* padding */
747 for(i=0;i<padding;i++)
748 put_bits(p, 1, 0);
750 /* flush */
751 flush_put_bits(p);
754 static int MPA_encode_frame(AVCodecContext *avctx,
755 unsigned char *frame, int buf_size, void *data)
757 MpegAudioContext *s = avctx->priv_data;
758 short *samples = data;
759 short smr[MPA_MAX_CHANNELS][SBLIMIT];
760 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
761 int padding, i;
763 for(i=0;i<s->nb_channels;i++) {
764 filter(s, i, samples + i, s->nb_channels);
767 for(i=0;i<s->nb_channels;i++) {
768 compute_scale_factors(s->scale_code[i], s->scale_factors[i],
769 s->sb_samples[i], s->sblimit);
771 for(i=0;i<s->nb_channels;i++) {
772 psycho_acoustic_model(s, smr[i]);
774 compute_bit_allocation(s, smr, bit_alloc, &padding);
776 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
778 encode_frame(s, bit_alloc, padding);
780 s->nb_samples += MPA_FRAME_SIZE;
781 return pbBufPtr(&s->pb) - s->pb.buf;
784 static av_cold int MPA_encode_close(AVCodecContext *avctx)
786 av_freep(&avctx->coded_frame);
787 return 0;
790 AVCodec mp2_encoder = {
791 "mp2",
792 CODEC_TYPE_AUDIO,
793 CODEC_ID_MP2,
794 sizeof(MpegAudioContext),
795 MPA_encode_init,
796 MPA_encode_frame,
797 MPA_encode_close,
798 NULL,
799 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
802 #undef FIX