2 * The simplest mpeg audio layer 2 encoder
3 * Copyright (c) 2000, 2001 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 /* currently, cannot change these constants (need to modify
24 quantization stage) */
27 #define MUL(a,b) (((INT64)(a) * (INT64)(b)) >> FRAC_BITS)
28 #define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
30 #define SAMPLES_BUF_SIZE 4096
32 typedef struct MpegAudioContext
{
36 int lsf
; /* 1 if mpeg2 low bitrate selected */
37 int bitrate_index
; /* bit rate */
39 int frame_size
; /* frame size, in bits, without padding */
40 INT64 nb_samples
; /* total number of samples encoded */
41 /* padding computation */
42 int frame_frac
, frame_frac_incr
, do_padding
;
43 short samples_buf
[MPA_MAX_CHANNELS
][SAMPLES_BUF_SIZE
]; /* buffer for filter */
44 int samples_offset
[MPA_MAX_CHANNELS
]; /* offset in samples_buf */
45 int sb_samples
[MPA_MAX_CHANNELS
][3][12][SBLIMIT
];
46 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3]; /* scale factors */
47 /* code to group 3 scale factors */
48 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
49 int sblimit
; /* number of used subbands */
50 const unsigned char *alloc_table
;
53 /* define it to use floats in quantization (I don't like floats !) */
56 #include "mpegaudiotab.h"
58 int MPA_encode_init(AVCodecContext
*avctx
)
60 MpegAudioContext
*s
= avctx
->priv_data
;
61 int freq
= avctx
->sample_rate
;
62 int bitrate
= avctx
->bit_rate
;
63 int channels
= avctx
->channels
;
69 bitrate
= bitrate
/ 1000;
70 s
->nb_channels
= channels
;
72 s
->bit_rate
= bitrate
* 1000;
73 avctx
->frame_size
= MPA_FRAME_SIZE
;
74 avctx
->key_frame
= 1; /* always key frame */
79 if (mpa_freq_tab
[i
] == freq
)
81 if ((mpa_freq_tab
[i
] / 2) == freq
) {
90 /* encoding bitrate & frequency */
92 if (mpa_bitrate_tab
[s
->lsf
][1][i
] == bitrate
)
99 /* compute total header size & pad bit */
101 a
= (float)(bitrate
* 1000 * MPA_FRAME_SIZE
) / (freq
* 8.0);
102 s
->frame_size
= ((int)a
) * 8;
104 /* frame fractional size to compute padding */
106 s
->frame_frac_incr
= (int)((a
- floor(a
)) * 65536.0);
108 /* select the right allocation table */
109 table
= l2_select_table(bitrate
, s
->nb_channels
, freq
, s
->lsf
);
111 /* number of used subbands */
112 s
->sblimit
= sblimit_table
[table
];
113 s
->alloc_table
= alloc_tables
[table
];
116 printf("%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
117 bitrate
, freq
, s
->frame_size
, table
, s
->frame_frac_incr
);
120 for(i
=0;i
<s
->nb_channels
;i
++)
121 s
->samples_offset
[i
] = 0;
127 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
133 filter_bank
[512 - i
] = v
;
137 v
= (int)(pow(2.0, (3 - i
) / 3.0) * (1 << 20));
140 scale_factor_table
[i
] = v
;
142 scale_factor_inv_table
[i
] = pow(2.0, -(3 - i
) / 3.0) / (float)(1 << 20);
145 scale_factor_shift
[i
] = 21 - P
- (i
/ 3);
146 scale_factor_mult
[i
] = (1 << P
) * pow(2.0, (i
% 3) / 3.0);
161 scale_diff_table
[i
] = v
;
170 total_quant_bits
[i
] = 12 * v
;
176 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
177 static void idct32(int *out
, int *tab
)
181 const int *xp
= costab32
;
183 for(j
=31;j
>=3;j
-=2) tab
[j
] += tab
[j
- 2];
222 x3
= MUL(t
[16], FIX(SQRT2
*0.5));
226 x2
= MUL(-(t
[24] + t
[8]), FIX(SQRT2
*0.5));
227 x1
= MUL((t
[8] - x2
), xp
[0]);
228 x2
= MUL((t
[8] + x2
), xp
[1]);
241 xr
= MUL(t
[28],xp
[0]);
245 xr
= MUL(t
[4],xp
[1]);
246 t
[ 4] = (t
[24] - xr
);
247 t
[24] = (t
[24] + xr
);
249 xr
= MUL(t
[20],xp
[2]);
253 xr
= MUL(t
[12],xp
[3]);
254 t
[12] = (t
[16] - xr
);
255 t
[16] = (t
[16] + xr
);
260 for (i
= 0; i
< 4; i
++) {
261 xr
= MUL(tab
[30-i
*4],xp
[0]);
262 tab
[30-i
*4] = (tab
[i
*4] - xr
);
263 tab
[ i
*4] = (tab
[i
*4] + xr
);
265 xr
= MUL(tab
[ 2+i
*4],xp
[1]);
266 tab
[ 2+i
*4] = (tab
[28-i
*4] - xr
);
267 tab
[28-i
*4] = (tab
[28-i
*4] + xr
);
269 xr
= MUL(tab
[31-i
*4],xp
[0]);
270 tab
[31-i
*4] = (tab
[1+i
*4] - xr
);
271 tab
[ 1+i
*4] = (tab
[1+i
*4] + xr
);
273 xr
= MUL(tab
[ 3+i
*4],xp
[1]);
274 tab
[ 3+i
*4] = (tab
[29-i
*4] - xr
);
275 tab
[29-i
*4] = (tab
[29-i
*4] + xr
);
283 xr
= MUL(t1
[0], *xp
);
292 out
[i
] = tab
[bitinv32
[i
]];
296 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
298 static void filter(MpegAudioContext
*s
, int ch
, short *samples
, int incr
)
301 int sum
, offset
, i
, j
;
306 // print_pow1(samples, 1152);
308 offset
= s
->samples_offset
[ch
];
309 out
= &s
->sb_samples
[ch
][0][0][0];
311 /* 32 samples at once */
313 s
->samples_buf
[ch
][offset
+ (31 - i
)] = samples
[0];
318 p
= s
->samples_buf
[ch
] + offset
;
322 sum
= p
[0*64] * q
[0*64];
323 sum
+= p
[1*64] * q
[1*64];
324 sum
+= p
[2*64] * q
[2*64];
325 sum
+= p
[3*64] * q
[3*64];
326 sum
+= p
[4*64] * q
[4*64];
327 sum
+= p
[5*64] * q
[5*64];
328 sum
+= p
[6*64] * q
[6*64];
329 sum
+= p
[7*64] * q
[7*64];
334 tmp1
[0] = tmp
[16] >> WSHIFT
;
335 for( i
=1; i
<=16; i
++ ) tmp1
[i
] = (tmp
[i
+16]+tmp
[16-i
]) >> WSHIFT
;
336 for( i
=17; i
<=31; i
++ ) tmp1
[i
] = (tmp
[i
+16]-tmp
[80-i
]) >> WSHIFT
;
340 /* advance of 32 samples */
343 /* handle the wrap around */
345 memmove(s
->samples_buf
[ch
] + SAMPLES_BUF_SIZE
- (512 - 32),
346 s
->samples_buf
[ch
], (512 - 32) * 2);
347 offset
= SAMPLES_BUF_SIZE
- 512;
350 s
->samples_offset
[ch
] = offset
;
352 // print_pow(s->sb_samples, 1152);
355 static void compute_scale_factors(unsigned char scale_code
[SBLIMIT
],
356 unsigned char scale_factors
[SBLIMIT
][3],
357 int sb_samples
[3][12][SBLIMIT
],
360 int *p
, vmax
, v
, n
, i
, j
, k
, code
;
362 unsigned char *sf
= &scale_factors
[0][0];
364 for(j
=0;j
<sblimit
;j
++) {
366 /* find the max absolute value */
367 p
= &sb_samples
[i
][0][j
];
375 /* compute the scale factor index using log 2 computations */
378 /* n is the position of the MSB of vmax. now
379 use at most 2 compares to find the index */
380 index
= (21 - n
) * 3 - 3;
382 while (vmax
<= scale_factor_table
[index
+1])
385 index
= 0; /* very unlikely case of overflow */
388 index
= 62; /* value 63 is not allowed */
392 printf("%2d:%d in=%x %x %d\n",
393 j
, i
, vmax
, scale_factor_table
[index
], index
);
395 /* store the scale factor */
396 assert(index
>=0 && index
<= 63);
400 /* compute the transmission factor : look if the scale factors
401 are close enough to each other */
402 d1
= scale_diff_table
[sf
[0] - sf
[1] + 64];
403 d2
= scale_diff_table
[sf
[1] - sf
[2] + 64];
405 /* handle the 25 cases */
406 switch(d1
* 5 + d2
) {
438 sf
[1] = sf
[2] = sf
[0];
443 sf
[0] = sf
[1] = sf
[2];
449 sf
[0] = sf
[2] = sf
[1];
455 sf
[1] = sf
[2] = sf
[0];
462 printf("%d: %2d %2d %2d %d %d -> %d\n", j
,
463 sf
[0], sf
[1], sf
[2], d1
, d2
, code
);
465 scale_code
[j
] = code
;
470 /* The most important function : psycho acoustic module. In this
471 encoder there is basically none, so this is the worst you can do,
472 but also this is the simpler. */
473 static void psycho_acoustic_model(MpegAudioContext
*s
, short smr
[SBLIMIT
])
477 for(i
=0;i
<s
->sblimit
;i
++) {
478 smr
[i
] = (int)(fixed_smr
[i
] * 10);
483 #define SB_NOTALLOCATED 0
484 #define SB_ALLOCATED 1
487 /* Try to maximize the smr while using a number of bits inferior to
488 the frame size. I tried to make the code simpler, faster and
489 smaller than other encoders :-) */
490 static void compute_bit_allocation(MpegAudioContext
*s
,
491 short smr1
[MPA_MAX_CHANNELS
][SBLIMIT
],
492 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
495 int i
, ch
, b
, max_smr
, max_ch
, max_sb
, current_frame_size
, max_frame_size
;
497 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
498 unsigned char subband_status
[MPA_MAX_CHANNELS
][SBLIMIT
];
499 const unsigned char *alloc
;
501 memcpy(smr
, smr1
, s
->nb_channels
* sizeof(short) * SBLIMIT
);
502 memset(subband_status
, SB_NOTALLOCATED
, s
->nb_channels
* SBLIMIT
);
503 memset(bit_alloc
, 0, s
->nb_channels
* SBLIMIT
);
505 /* compute frame size and padding */
506 max_frame_size
= s
->frame_size
;
507 s
->frame_frac
+= s
->frame_frac_incr
;
508 if (s
->frame_frac
>= 65536) {
509 s
->frame_frac
-= 65536;
516 /* compute the header + bit alloc size */
517 current_frame_size
= 32;
518 alloc
= s
->alloc_table
;
519 for(i
=0;i
<s
->sblimit
;i
++) {
521 current_frame_size
+= incr
* s
->nb_channels
;
525 /* look for the subband with the largest signal to mask ratio */
528 max_smr
= 0x80000000;
529 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
530 for(i
=0;i
<s
->sblimit
;i
++) {
531 if (smr
[ch
][i
] > max_smr
&& subband_status
[ch
][i
] != SB_NOMORE
) {
532 max_smr
= smr
[ch
][i
];
539 printf("current=%d max=%d max_sb=%d alloc=%d\n",
540 current_frame_size
, max_frame_size
, max_sb
,
546 /* find alloc table entry (XXX: not optimal, should use
548 alloc
= s
->alloc_table
;
549 for(i
=0;i
<max_sb
;i
++) {
550 alloc
+= 1 << alloc
[0];
553 if (subband_status
[max_ch
][max_sb
] == SB_NOTALLOCATED
) {
554 /* nothing was coded for this band: add the necessary bits */
555 incr
= 2 + nb_scale_factors
[s
->scale_code
[max_ch
][max_sb
]] * 6;
556 incr
+= total_quant_bits
[alloc
[1]];
558 /* increments bit allocation */
559 b
= bit_alloc
[max_ch
][max_sb
];
560 incr
= total_quant_bits
[alloc
[b
+ 1]] -
561 total_quant_bits
[alloc
[b
]];
564 if (current_frame_size
+ incr
<= max_frame_size
) {
565 /* can increase size */
566 b
= ++bit_alloc
[max_ch
][max_sb
];
567 current_frame_size
+= incr
;
568 /* decrease smr by the resolution we added */
569 smr
[max_ch
][max_sb
] = smr1
[max_ch
][max_sb
] - quant_snr
[alloc
[b
]];
570 /* max allocation size reached ? */
571 if (b
== ((1 << alloc
[0]) - 1))
572 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
574 subband_status
[max_ch
][max_sb
] = SB_ALLOCATED
;
576 /* cannot increase the size of this subband */
577 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
580 *padding
= max_frame_size
- current_frame_size
;
581 assert(*padding
>= 0);
584 for(i
=0;i
<s
->sblimit
;i
++) {
585 printf("%d ", bit_alloc
[i
]);
592 * Output the mpeg audio layer 2 frame. Note how the code is small
593 * compared to other encoders :-)
595 static void encode_frame(MpegAudioContext
*s
,
596 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
599 int i
, j
, k
, l
, bit_alloc_bits
, b
, ch
;
602 PutBitContext
*p
= &s
->pb
;
606 put_bits(p
, 12, 0xfff);
607 put_bits(p
, 1, 1 - s
->lsf
); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
608 put_bits(p
, 2, 4-2); /* layer 2 */
609 put_bits(p
, 1, 1); /* no error protection */
610 put_bits(p
, 4, s
->bitrate_index
);
611 put_bits(p
, 2, s
->freq_index
);
612 put_bits(p
, 1, s
->do_padding
); /* use padding */
613 put_bits(p
, 1, 0); /* private_bit */
614 put_bits(p
, 2, s
->nb_channels
== 2 ? MPA_STEREO
: MPA_MONO
);
615 put_bits(p
, 2, 0); /* mode_ext */
616 put_bits(p
, 1, 0); /* no copyright */
617 put_bits(p
, 1, 1); /* original */
618 put_bits(p
, 2, 0); /* no emphasis */
622 for(i
=0;i
<s
->sblimit
;i
++) {
623 bit_alloc_bits
= s
->alloc_table
[j
];
624 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
625 put_bits(p
, bit_alloc_bits
, bit_alloc
[ch
][i
]);
627 j
+= 1 << bit_alloc_bits
;
631 for(i
=0;i
<s
->sblimit
;i
++) {
632 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
633 if (bit_alloc
[ch
][i
])
634 put_bits(p
, 2, s
->scale_code
[ch
][i
]);
639 for(i
=0;i
<s
->sblimit
;i
++) {
640 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
641 if (bit_alloc
[ch
][i
]) {
642 sf
= &s
->scale_factors
[ch
][i
][0];
643 switch(s
->scale_code
[ch
][i
]) {
645 put_bits(p
, 6, sf
[0]);
646 put_bits(p
, 6, sf
[1]);
647 put_bits(p
, 6, sf
[2]);
651 put_bits(p
, 6, sf
[0]);
652 put_bits(p
, 6, sf
[2]);
655 put_bits(p
, 6, sf
[0]);
662 /* quantization & write sub band samples */
667 for(i
=0;i
<s
->sblimit
;i
++) {
668 bit_alloc_bits
= s
->alloc_table
[j
];
669 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
670 b
= bit_alloc
[ch
][i
];
672 int qindex
, steps
, m
, sample
, bits
;
673 /* we encode 3 sub band samples of the same sub band at a time */
674 qindex
= s
->alloc_table
[j
+b
];
675 steps
= quant_steps
[qindex
];
677 sample
= s
->sb_samples
[ch
][k
][l
+ m
][i
];
678 /* divide by scale factor */
682 a
= (float)sample
* scale_factor_inv_table
[s
->scale_factors
[ch
][i
][k
]];
683 q
[m
] = (int)((a
+ 1.0) * steps
* 0.5);
687 int q1
, e
, shift
, mult
;
688 e
= s
->scale_factors
[ch
][i
][k
];
689 shift
= scale_factor_shift
[e
];
690 mult
= scale_factor_mult
[e
];
692 /* normalize to P bits */
694 q1
= sample
<< (-shift
);
696 q1
= sample
>> shift
;
697 q1
= (q1
* mult
) >> P
;
698 q
[m
] = ((q1
+ (1 << P
)) * steps
) >> (P
+ 1);
703 assert(q
[m
] >= 0 && q
[m
] < steps
);
705 bits
= quant_bits
[qindex
];
707 /* group the 3 values to save bits */
709 q
[0] + steps
* (q
[1] + steps
* q
[2]));
711 printf("%d: gr1 %d\n",
712 i
, q
[0] + steps
* (q
[1] + steps
* q
[2]));
716 printf("%d: gr3 %d %d %d\n",
717 i
, q
[0], q
[1], q
[2]);
719 put_bits(p
, bits
, q
[0]);
720 put_bits(p
, bits
, q
[1]);
721 put_bits(p
, bits
, q
[2]);
725 /* next subband in alloc table */
726 j
+= 1 << bit_alloc_bits
;
732 for(i
=0;i
<padding
;i
++)
739 int MPA_encode_frame(AVCodecContext
*avctx
,
740 unsigned char *frame
, int buf_size
, void *data
)
742 MpegAudioContext
*s
= avctx
->priv_data
;
743 short *samples
= data
;
744 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
745 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
748 for(i
=0;i
<s
->nb_channels
;i
++) {
749 filter(s
, i
, samples
+ i
, s
->nb_channels
);
752 for(i
=0;i
<s
->nb_channels
;i
++) {
753 compute_scale_factors(s
->scale_code
[i
], s
->scale_factors
[i
],
754 s
->sb_samples
[i
], s
->sblimit
);
756 for(i
=0;i
<s
->nb_channels
;i
++) {
757 psycho_acoustic_model(s
, smr
[i
]);
759 compute_bit_allocation(s
, smr
, bit_alloc
, &padding
);
761 init_put_bits(&s
->pb
, frame
, MPA_MAX_CODED_FRAME_SIZE
, NULL
, NULL
);
763 encode_frame(s
, bit_alloc
, padding
);
765 s
->nb_samples
+= MPA_FRAME_SIZE
;
766 return pbBufPtr(&s
->pb
) - s
->pb
.buf
;
770 AVCodec mp2_encoder
= {
774 sizeof(MpegAudioContext
),