2 * The simplest mpeg audio layer 2 encoder
3 * Copyright (c) 2000 Gerard Lantau.
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.
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.
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.
22 #include <netinet/in.h>
25 #include "mpegaudio.h"
30 /* define it to use floats in quantization (I don't like floats !) */
38 #include "mpegaudiotab.h"
40 int MPA_encode_init(AVEncodeContext *avctx)
42 MpegAudioContext *s = avctx->priv_data;
43 int freq = avctx->rate;
44 int bitrate = avctx->bit_rate;
45 int channels = avctx->channels;
52 bitrate = bitrate / 1000;
54 s->bit_rate = bitrate * 1000;
55 avctx->frame_size = MPA_FRAME_SIZE;
56 avctx->key_frame = 1; /* always key frame */
61 if (freq_tab[i] == freq)
63 if ((freq_tab[i] / 2) == freq) {
72 /* encoding bitrate & frequency */
74 if (bitrate_tab[1-s->lsf][i] == bitrate)
81 /* compute total header size & pad bit */
83 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
84 s->frame_size = ((int)a) * 8;
86 /* frame fractional size to compute padding */
88 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
90 /* select the right allocation table */
92 if ((freq == 48000 && bitrate >= 56) ||
93 (bitrate >= 56 && bitrate <= 80))
95 else if (freq != 48000 && bitrate >= 96)
97 else if (freq != 32000 && bitrate <= 48)
104 /* number of used subbands */
105 s->sblimit = sblimit_table[table];
106 s->alloc_table = alloc_tables[table];
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);
113 s->samples_offset = 0;
116 float a = enwindow[i] * 32768.0 * 16.0;
117 filter_bank[i] = (int)(a);
120 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
123 scale_factor_table[i] = v;
125 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
128 scale_factor_shift[i] = 21 - P - (i / 3);
129 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
144 scale_diff_table[i] = v;
153 total_quant_bits[i] = 12 * v;
159 /* 32 point floating point IDCT */
160 static void idct32(int *out, int *tab, int sblimit, int left_shift)
164 const int *xp = costab32;
166 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
205 x3 = MUL(t[16], FIX(SQRT2*0.5));
209 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
210 x1 = MUL((t[8] - x2), xp[0]);
211 x2 = MUL((t[8] + x2), xp[1]);
224 xr = MUL(t[28],xp[0]);
228 xr = MUL(t[4],xp[1]);
229 t[ 4] = (t[24] - xr);
230 t[24] = (t[24] + xr);
232 xr = MUL(t[20],xp[2]);
236 xr = MUL(t[12],xp[3]);
237 t[12] = (t[16] - xr);
238 t[16] = (t[16] + xr);
243 for (i = 0; i < 4; i++) {
244 xr = MUL(tab[30-i*4],xp[0]);
245 tab[30-i*4] = (tab[i*4] - xr);
246 tab[ i*4] = (tab[i*4] + xr);
248 xr = MUL(tab[ 2+i*4],xp[1]);
249 tab[ 2+i*4] = (tab[28-i*4] - xr);
250 tab[28-i*4] = (tab[28-i*4] + xr);
252 xr = MUL(tab[31-i*4],xp[0]);
253 tab[31-i*4] = (tab[1+i*4] - xr);
254 tab[ 1+i*4] = (tab[1+i*4] + xr);
256 xr = MUL(tab[ 3+i*4],xp[1]);
257 tab[ 3+i*4] = (tab[29-i*4] - xr);
258 tab[29-i*4] = (tab[29-i*4] + xr);
266 xr = MUL(t1[0], *xp);
275 out[i] = tab[bitinv32[i]] << left_shift;
279 static void filter(MpegAudioContext *s, short *samples)
282 int sum, offset, i, j, norm, n;
287 // print_pow1(samples, 1152);
289 offset = s->samples_offset;
290 out = &s->sb_samples[0][0][0];
292 /* 32 samples at once */
294 s->samples_buf[offset + (31 - i)] = samples[i];
297 p = s->samples_buf + offset;
301 sum = p[0*64] * q[0*64];
302 sum += p[1*64] * q[1*64];
303 sum += p[2*64] * q[2*64];
304 sum += p[3*64] * q[3*64];
305 sum += p[4*64] * q[4*64];
306 sum += p[5*64] * q[5*64];
307 sum += p[6*64] * q[6*64];
308 sum += p[7*64] * q[7*64];
314 for( i=1; i<=16; i++ ) tmp1[i] = tmp[i+16]+tmp[16-i];
315 for( i=17; i<=31; i++ ) tmp1[i] = tmp[i+16]-tmp[80-i];
317 /* integer IDCT 32 with normalization. XXX: There may be some
321 norm |= abs(tmp1[i]);
331 idct32(out, tmp1, s->sblimit, n);
333 /* advance of 32 samples */
337 /* handle the wrap around */
339 memmove(s->samples_buf + SAMPLES_BUF_SIZE - (512 - 32),
340 s->samples_buf, (512 - 32) * 2);
341 offset = SAMPLES_BUF_SIZE - 512;
344 s->samples_offset = offset;
346 // print_pow(s->sb_samples, 1152);
349 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
350 unsigned char scale_factors[SBLIMIT][3],
351 int sb_samples[3][12][SBLIMIT],
354 int *p, vmax, v, n, i, j, k, code;
356 unsigned char *sf = &scale_factors[0][0];
358 for(j=0;j<sblimit;j++) {
360 /* find the max absolute value */
361 p = &sb_samples[i][0][j];
369 /* compute the scale factor index using log 2 computations */
372 /* n is the position of the MSB of vmax. now
373 use at most 2 compares to find the index */
374 index = (21 - n) * 3 - 3;
376 while (vmax <= scale_factor_table[index+1])
379 index = 0; /* very unlikely case of overflow */
386 printf("%2d:%d in=%x %x %d\n",
387 j, i, vmax, scale_factor_table[index], index);
389 /* store the scale factor */
390 assert(index >=0 && index <= 63);
394 /* compute the transmission factor : look if the scale factors
395 are close enough to each other */
396 d1 = scale_diff_table[sf[0] - sf[1] + 64];
397 d2 = scale_diff_table[sf[1] - sf[2] + 64];
399 /* handle the 25 cases */
400 switch(d1 * 5 + d2) {
432 sf[1] = sf[2] = sf[0];
437 sf[0] = sf[1] = sf[2];
443 sf[0] = sf[2] = sf[1];
449 sf[1] = sf[2] = sf[0];
456 printf("%d: %2d %2d %2d %d %d -> %d\n", j,
457 sf[0], sf[1], sf[2], d1, d2, code);
459 scale_code[j] = code;
464 /* The most important function : psycho acoustic module. In this
465 encoder there is basically none, so this is the worst you can do,
466 but also this is the simpler. */
467 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
471 for(i=0;i<s->sblimit;i++) {
472 smr[i] = (int)(fixed_smr[i] * 10);
477 #define SB_NOTALLOCATED 0
478 #define SB_ALLOCATED 1
481 /* Try to maximize the smr while using a number of bits inferior to
482 the frame size. I tried to make the code simpler, faster and
483 smaller than other encoders :-) */
484 static void compute_bit_allocation(MpegAudioContext *s,
486 unsigned char bit_alloc[SBLIMIT],
489 int i, b, max_smr, max_sb, current_frame_size, max_frame_size;
492 unsigned char subband_status[SBLIMIT];
493 const unsigned char *alloc;
495 memcpy(smr, smr1, sizeof(short) * s->sblimit);
496 memset(subband_status, SB_NOTALLOCATED, s->sblimit);
497 memset(bit_alloc, 0, s->sblimit);
499 /* compute frame size and padding */
500 max_frame_size = s->frame_size;
501 s->frame_frac += s->frame_frac_incr;
502 if (s->frame_frac >= 65536) {
503 s->frame_frac -= 65536;
510 /* compute the header + bit alloc size */
511 current_frame_size = 32;
512 alloc = s->alloc_table;
513 for(i=0;i<s->sblimit;i++) {
515 current_frame_size += incr;
519 /* look for the subband with the largest signal to mask ratio */
521 max_smr = 0x80000000;
522 for(i=0;i<s->sblimit;i++) {
523 if (smr[i] > max_smr && subband_status[i] != SB_NOMORE) {
529 printf("current=%d max=%d max_sb=%d alloc=%d\n",
530 current_frame_size, max_frame_size, max_sb,
536 /* find alloc table entry (XXX: not optimal, should use
538 alloc = s->alloc_table;
539 for(i=0;i<max_sb;i++) {
540 alloc += 1 << alloc[0];
543 if (subband_status[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_sb]] * 6;
546 incr += total_quant_bits[alloc[1]];
548 /* increments bit allocation */
549 b = bit_alloc[max_sb];
550 incr = total_quant_bits[alloc[b + 1]] -
551 total_quant_bits[alloc[b]];
554 if (current_frame_size + incr <= max_frame_size) {
555 /* can increase size */
556 b = ++bit_alloc[max_sb];
557 current_frame_size += incr;
558 /* decrease smr by the resolution we added */
559 smr[max_sb] = smr1[max_sb] - quant_snr[alloc[b]];
560 /* max allocation size reached ? */
561 if (b == ((1 << alloc[0]) - 1))
562 subband_status[max_sb] = SB_NOMORE;
564 subband_status[max_sb] = SB_ALLOCATED;
566 /* cannot increase the size of this subband */
567 subband_status[max_sb] = SB_NOMORE;
570 *padding = max_frame_size - current_frame_size;
571 assert(*padding >= 0);
574 for(i=0;i<s->sblimit;i++) {
575 printf("%d ", bit_alloc[i]);
582 * Output the mpeg audio layer 2 frame. Note how the code is small
583 * compared to other encoders :-)
585 static void encode_frame(MpegAudioContext *s,
586 unsigned char bit_alloc[SBLIMIT],
589 int i, j, k, l, bit_alloc_bits, b;
592 PutBitContext *p = &s->pb;
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, 4-2); /* 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, 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 */
612 for(i=0;i<s->sblimit;i++) {
613 bit_alloc_bits = s->alloc_table[j];
614 put_bits(p, bit_alloc_bits, bit_alloc[i]);
615 j += 1 << bit_alloc_bits;
619 for(i=0;i<s->sblimit;i++) {
621 put_bits(p, 2, s->scale_code[i]);
625 sf = &s->scale_factors[0][0];
626 for(i=0;i<s->sblimit;i++) {
628 switch(s->scale_code[i]) {
630 put_bits(p, 6, sf[0]);
631 put_bits(p, 6, sf[1]);
632 put_bits(p, 6, sf[2]);
636 put_bits(p, 6, sf[0]);
637 put_bits(p, 6, sf[2]);
640 put_bits(p, 6, sf[0]);
647 /* quantization & write sub band samples */
652 for(i=0;i<s->sblimit;i++) {
653 bit_alloc_bits = s->alloc_table[j];
656 int qindex, steps, m, sample, bits;
657 /* we encode 3 sub band samples of the same sub band at a time */
658 qindex = s->alloc_table[j+b];
659 steps = quant_steps[qindex];
661 sample = s->sb_samples[k][l + m][i];
662 /* divide by scale factor */
666 a = (float)sample * scale_factor_inv_table[s->scale_factors[i][k]];
667 q[m] = (int)((a + 1.0) * steps * 0.5);
671 int q1, e, shift, mult;
672 e = s->scale_factors[i][k];
673 shift = scale_factor_shift[e];
674 mult = scale_factor_mult[e];
676 /* normalize to P bits */
678 q1 = sample << (-shift);
680 q1 = sample >> shift;
681 q1 = (q1 * mult) >> P;
682 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
687 assert(q[m] >= 0 && q[m] < steps);
689 bits = quant_bits[qindex];
691 /* group the 3 values to save bits */
693 q[0] + steps * (q[1] + steps * q[2]));
695 printf("%d: gr1 %d\n",
696 i, q[0] + steps * (q[1] + steps * q[2]));
700 printf("%d: gr3 %d %d %d\n",
701 i, q[0], q[1], q[2]);
703 put_bits(p, bits, q[0]);
704 put_bits(p, bits, q[1]);
705 put_bits(p, bits, q[2]);
708 /* next subband in alloc table */
709 j += 1 << bit_alloc_bits;
715 for(i=0;i<padding;i++)
722 int MPA_encode_frame(AVEncodeContext *avctx,
723 unsigned char *frame, int buf_size, void *data)
725 MpegAudioContext *s = avctx->priv_data;
726 short *samples = data;
728 unsigned char bit_alloc[SBLIMIT];
732 compute_scale_factors(s->scale_code, s->scale_factors,
733 s->sb_samples, s->sblimit);
734 psycho_acoustic_model(s, smr);
735 compute_bit_allocation(s, smr, bit_alloc, &padding);
737 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE, NULL, NULL);
739 encode_frame(s, bit_alloc, padding);
741 s->nb_samples += MPA_FRAME_SIZE;
742 return s->pb.buf_ptr - s->pb.buf;
746 AVEncoder mp2_encoder = {
750 sizeof(MpegAudioContext),