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.
21 #include "mpegaudio.h"
23 /* define it to use floats in quantization (I don't like floats !) */
31 #include "mpegaudiotab.h"
33 int MPA_encode_init(AVCodecContext
*avctx
)
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
;
44 bitrate
= bitrate
/ 1000;
45 s
->nb_channels
= channels
;
47 s
->bit_rate
= bitrate
* 1000;
48 avctx
->frame_size
= MPA_FRAME_SIZE
;
49 avctx
->key_frame
= 1; /* always key frame */
54 if (freq_tab
[i
] == freq
)
56 if ((freq_tab
[i
] / 2) == freq
) {
65 /* encoding bitrate & frequency */
67 if (bitrate_tab
[1-s
->lsf
][i
] == bitrate
)
74 /* compute total header size & pad bit */
76 a
= (float)(bitrate
* 1000 * MPA_FRAME_SIZE
) / (freq
* 8.0);
77 s
->frame_size
= ((int)a
) * 8;
79 /* frame fractional size to compute padding */
81 s
->frame_frac_incr
= (int)((a
- floor(a
)) * 65536.0);
83 /* select the right allocation table */
84 ch_bitrate
= bitrate
/ s
->nb_channels
;
86 if ((freq
== 48000 && ch_bitrate
>= 56) ||
87 (ch_bitrate
>= 56 && ch_bitrate
<= 80))
89 else if (freq
!= 48000 && ch_bitrate
>= 96)
91 else if (freq
!= 32000 && ch_bitrate
<= 48)
98 /* number of used subbands */
99 s
->sblimit
= sblimit_table
[table
];
100 s
->alloc_table
= alloc_tables
[table
];
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
);
107 for(i
=0;i
<s
->nb_channels
;i
++)
108 s
->samples_offset
[i
] = 0;
111 float a
= enwindow
[i
] * 32768.0 * 16.0;
112 filter_bank
[i
] = (int)(a
);
115 v
= (int)(pow(2.0, (3 - i
) / 3.0) * (1 << 20));
118 scale_factor_table
[i
] = v
;
120 scale_factor_inv_table
[i
] = pow(2.0, -(3 - i
) / 3.0) / (float)(1 << 20);
123 scale_factor_shift
[i
] = 21 - P
- (i
/ 3);
124 scale_factor_mult
[i
] = (1 << P
) * pow(2.0, (i
% 3) / 3.0);
139 scale_diff_table
[i
] = v
;
148 total_quant_bits
[i
] = 12 * v
;
154 /* 32 point floating point IDCT */
155 static void idct32(int *out
, int *tab
, int sblimit
, int left_shift
)
159 const int *xp
= costab32
;
161 for(j
=31;j
>=3;j
-=2) tab
[j
] += tab
[j
- 2];
200 x3
= MUL(t
[16], FIX(SQRT2
*0.5));
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]);
219 xr
= MUL(t
[28],xp
[0]);
223 xr
= MUL(t
[4],xp
[1]);
224 t
[ 4] = (t
[24] - xr
);
225 t
[24] = (t
[24] + xr
);
227 xr
= MUL(t
[20],xp
[2]);
231 xr
= MUL(t
[12],xp
[3]);
232 t
[12] = (t
[16] - xr
);
233 t
[16] = (t
[16] + xr
);
238 for (i
= 0; i
< 4; i
++) {
239 xr
= MUL(tab
[30-i
*4],xp
[0]);
240 tab
[30-i
*4] = (tab
[i
*4] - xr
);
241 tab
[ i
*4] = (tab
[i
*4] + xr
);
243 xr
= MUL(tab
[ 2+i
*4],xp
[1]);
244 tab
[ 2+i
*4] = (tab
[28-i
*4] - xr
);
245 tab
[28-i
*4] = (tab
[28-i
*4] + xr
);
247 xr
= MUL(tab
[31-i
*4],xp
[0]);
248 tab
[31-i
*4] = (tab
[1+i
*4] - xr
);
249 tab
[ 1+i
*4] = (tab
[1+i
*4] + xr
);
251 xr
= MUL(tab
[ 3+i
*4],xp
[1]);
252 tab
[ 3+i
*4] = (tab
[29-i
*4] - xr
);
253 tab
[29-i
*4] = (tab
[29-i
*4] + xr
);
261 xr
= MUL(t1
[0], *xp
);
270 out
[i
] = tab
[bitinv32
[i
]] << left_shift
;
274 static void filter(MpegAudioContext
*s
, int ch
, short *samples
, int incr
)
277 int sum
, offset
, i
, j
, norm
, n
;
282 // print_pow1(samples, 1152);
284 offset
= s
->samples_offset
[ch
];
285 out
= &s
->sb_samples
[ch
][0][0][0];
287 /* 32 samples at once */
289 s
->samples_buf
[ch
][offset
+ (31 - i
)] = samples
[0];
294 p
= s
->samples_buf
[ch
] + offset
;
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];
311 for( i
=1; i
<=16; i
++ ) tmp1
[i
] = tmp
[i
+16]+tmp
[16-i
];
312 for( i
=17; i
<=31; i
++ ) tmp1
[i
] = tmp
[i
+16]-tmp
[80-i
];
314 /* integer IDCT 32 with normalization. XXX: There may be some
318 norm
|= abs(tmp1
[i
]);
320 n
= av_log2(norm
) - 12;
328 idct32(out
, tmp1
, s
->sblimit
, n
);
330 /* advance of 32 samples */
333 /* handle the wrap around */
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;
340 s
->samples_offset
[ch
] = offset
;
342 // print_pow(s->sb_samples, 1152);
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
],
350 int *p
, vmax
, v
, n
, i
, j
, k
, code
;
352 unsigned char *sf
= &scale_factors
[0][0];
354 for(j
=0;j
<sblimit
;j
++) {
356 /* find the max absolute value */
357 p
= &sb_samples
[i
][0][j
];
365 /* compute the scale factor index using log 2 computations */
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;
372 while (vmax
<= scale_factor_table
[index
+1])
375 index
= 0; /* very unlikely case of overflow */
382 printf("%2d:%d in=%x %x %d\n",
383 j
, i
, vmax
, scale_factor_table
[index
], index
);
385 /* store the scale factor */
386 assert(index
>=0 && index
<= 63);
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];
395 /* handle the 25 cases */
396 switch(d1
* 5 + d2
) {
428 sf
[1] = sf
[2] = sf
[0];
433 sf
[0] = sf
[1] = sf
[2];
439 sf
[0] = sf
[2] = sf
[1];
445 sf
[1] = sf
[2] = sf
[0];
452 printf("%d: %2d %2d %2d %d %d -> %d\n", j
,
453 sf
[0], sf
[1], sf
[2], d1
, d2
, code
);
455 scale_code
[j
] = code
;
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
])
467 for(i
=0;i
<s
->sblimit
;i
++) {
468 smr
[i
] = (int)(fixed_smr
[i
] * 10);
473 #define SB_NOTALLOCATED 0
474 #define SB_ALLOCATED 1
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
],
485 int i
, ch
, b
, max_smr
, max_ch
, max_sb
, current_frame_size
, max_frame_size
;
487 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
488 unsigned char subband_status
[MPA_MAX_CHANNELS
][SBLIMIT
];
489 const unsigned char *alloc
;
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
);
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;
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
++) {
511 current_frame_size
+= incr
* s
->nb_channels
;
515 /* look for the subband with the largest signal to mask ratio */
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
];
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_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]];
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
]];
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
;
564 subband_status
[max_ch
][max_sb
] = SB_ALLOCATED
;
566 /* cannot increase the size of this subband */
567 subband_status
[max_ch
][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
[MPA_MAX_CHANNELS
][SBLIMIT
],
589 int i
, j
, k
, l
, bit_alloc_bits
, b
, ch
;
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, 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 */
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
]);
617 j
+= 1 << bit_alloc_bits
;
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
]);
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
]) {
635 put_bits(p
, 6, sf
[0]);
636 put_bits(p
, 6, sf
[1]);
637 put_bits(p
, 6, sf
[2]);
641 put_bits(p
, 6, sf
[0]);
642 put_bits(p
, 6, sf
[2]);
645 put_bits(p
, 6, sf
[0]);
652 /* quantization & write sub band samples */
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
];
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
];
667 sample
= s
->sb_samples
[ch
][k
][l
+ m
][i
];
668 /* divide by scale factor */
672 a
= (float)sample
* scale_factor_inv_table
[s
->scale_factors
[ch
][i
][k
]];
673 q
[m
] = (int)((a
+ 1.0) * steps
* 0.5);
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
];
682 /* normalize to P bits */
684 q1
= sample
<< (-shift
);
686 q1
= sample
>> shift
;
687 q1
= (q1
* mult
) >> P
;
688 q
[m
] = ((q1
+ (1 << P
)) * steps
) >> (P
+ 1);
693 assert(q
[m
] >= 0 && q
[m
] < steps
);
695 bits
= quant_bits
[qindex
];
697 /* group the 3 values to save bits */
699 q
[0] + steps
* (q
[1] + steps
* q
[2]));
701 printf("%d: gr1 %d\n",
702 i
, q
[0] + steps
* (q
[1] + steps
* q
[2]));
706 printf("%d: gr3 %d %d %d\n",
707 i
, q
[0], q
[1], q
[2]);
709 put_bits(p
, bits
, q
[0]);
710 put_bits(p
, bits
, q
[1]);
711 put_bits(p
, bits
, q
[2]);
715 /* next subband in alloc table */
716 j
+= 1 << bit_alloc_bits
;
722 for(i
=0;i
<padding
;i
++)
729 int MPA_encode_frame(AVCodecContext
*avctx
,
730 unsigned char *frame
, int buf_size
, void *data
)
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
];
738 for(i
=0;i
<s
->nb_channels
;i
++) {
739 filter(s
, i
, samples
+ i
, s
->nb_channels
);
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
);
746 for(i
=0;i
<s
->nb_channels
;i
++) {
747 psycho_acoustic_model(s
, smr
[i
]);
749 compute_bit_allocation(s
, smr
, bit_alloc
, &padding
);
751 init_put_bits(&s
->pb
, frame
, MPA_MAX_CODED_FRAME_SIZE
, NULL
, NULL
);
753 encode_frame(s
, bit_alloc
, padding
);
755 s
->nb_samples
+= MPA_FRAME_SIZE
;
756 return s
->pb
.buf_ptr
- s
->pb
.buf
;
760 AVCodec mp2_encoder
= {
764 sizeof(MpegAudioContext
),