2 * The simplest mpeg audio layer 2 encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * The simplest mpeg audio layer 2 encoder.
26 #include "mpegaudio.h"
28 /* currently, cannot change these constants (need to modify
29 quantization stage) */
32 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
33 #define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
35 #define SAMPLES_BUF_SIZE 4096
37 typedef struct MpegAudioContext
{
41 int lsf
; /* 1 if mpeg2 low bitrate selected */
42 int bitrate_index
; /* bit rate */
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
;
58 /* define it to use floats in quantization (I don't like floats !) */
61 #include "mpegaudiotab.h"
63 static int MPA_encode_init(AVCodecContext
*avctx
)
65 MpegAudioContext
*s
= avctx
->priv_data
;
66 int freq
= avctx
->sample_rate
;
67 int bitrate
= avctx
->bit_rate
;
68 int channels
= avctx
->channels
;
74 bitrate
= bitrate
/ 1000;
75 s
->nb_channels
= channels
;
77 s
->bit_rate
= bitrate
* 1000;
78 avctx
->frame_size
= MPA_FRAME_SIZE
;
83 if (mpa_freq_tab
[i
] == freq
)
85 if ((mpa_freq_tab
[i
] / 2) == freq
) {
94 /* encoding bitrate & frequency */
96 if (mpa_bitrate_tab
[s
->lsf
][1][i
] == bitrate
)
101 s
->bitrate_index
= i
;
103 /* compute total header size & pad bit */
105 a
= (float)(bitrate
* 1000 * MPA_FRAME_SIZE
) / (freq
* 8.0);
106 s
->frame_size
= ((int)a
) * 8;
108 /* frame fractional size to compute padding */
110 s
->frame_frac_incr
= (int)((a
- floor(a
)) * 65536.0);
112 /* select the right allocation table */
113 table
= l2_select_table(bitrate
, s
->nb_channels
, freq
, s
->lsf
);
115 /* number of used subbands */
116 s
->sblimit
= sblimit_table
[table
];
117 s
->alloc_table
= alloc_tables
[table
];
120 av_log(AV_LOG_DEBUG
, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
121 bitrate
, freq
, s
->frame_size
, table
, s
->frame_frac_incr
);
124 for(i
=0;i
<s
->nb_channels
;i
++)
125 s
->samples_offset
[i
] = 0;
131 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
137 filter_bank
[512 - i
] = v
;
141 v
= (int)(pow(2.0, (3 - i
) / 3.0) * (1 << 20));
144 scale_factor_table
[i
] = v
;
146 scale_factor_inv_table
[i
] = pow(2.0, -(3 - i
) / 3.0) / (float)(1 << 20);
149 scale_factor_shift
[i
] = 21 - P
- (i
/ 3);
150 scale_factor_mult
[i
] = (1 << P
) * pow(2.0, (i
% 3) / 3.0);
165 scale_diff_table
[i
] = v
;
174 total_quant_bits
[i
] = 12 * v
;
177 avctx
->coded_frame
= avcodec_alloc_frame();
178 avctx
->coded_frame
->key_frame
= 1;
183 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
184 static void idct32(int *out
, int *tab
)
188 const int *xp
= costab32
;
190 for(j
=31;j
>=3;j
-=2) tab
[j
] += tab
[j
- 2];
229 x3
= MUL(t
[16], FIX(SQRT2
*0.5));
233 x2
= MUL(-(t
[24] + t
[8]), FIX(SQRT2
*0.5));
234 x1
= MUL((t
[8] - x2
), xp
[0]);
235 x2
= MUL((t
[8] + x2
), xp
[1]);
248 xr
= MUL(t
[28],xp
[0]);
252 xr
= MUL(t
[4],xp
[1]);
253 t
[ 4] = (t
[24] - xr
);
254 t
[24] = (t
[24] + xr
);
256 xr
= MUL(t
[20],xp
[2]);
260 xr
= MUL(t
[12],xp
[3]);
261 t
[12] = (t
[16] - xr
);
262 t
[16] = (t
[16] + xr
);
267 for (i
= 0; i
< 4; i
++) {
268 xr
= MUL(tab
[30-i
*4],xp
[0]);
269 tab
[30-i
*4] = (tab
[i
*4] - xr
);
270 tab
[ i
*4] = (tab
[i
*4] + xr
);
272 xr
= MUL(tab
[ 2+i
*4],xp
[1]);
273 tab
[ 2+i
*4] = (tab
[28-i
*4] - xr
);
274 tab
[28-i
*4] = (tab
[28-i
*4] + xr
);
276 xr
= MUL(tab
[31-i
*4],xp
[0]);
277 tab
[31-i
*4] = (tab
[1+i
*4] - xr
);
278 tab
[ 1+i
*4] = (tab
[1+i
*4] + xr
);
280 xr
= MUL(tab
[ 3+i
*4],xp
[1]);
281 tab
[ 3+i
*4] = (tab
[29-i
*4] - xr
);
282 tab
[29-i
*4] = (tab
[29-i
*4] + xr
);
290 xr
= MUL(t1
[0], *xp
);
299 out
[i
] = tab
[bitinv32
[i
]];
303 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
305 static void filter(MpegAudioContext
*s
, int ch
, short *samples
, int incr
)
308 int sum
, offset
, i
, j
;
313 // print_pow1(samples, 1152);
315 offset
= s
->samples_offset
[ch
];
316 out
= &s
->sb_samples
[ch
][0][0][0];
318 /* 32 samples at once */
320 s
->samples_buf
[ch
][offset
+ (31 - i
)] = samples
[0];
325 p
= s
->samples_buf
[ch
] + offset
;
329 sum
= p
[0*64] * q
[0*64];
330 sum
+= p
[1*64] * q
[1*64];
331 sum
+= p
[2*64] * q
[2*64];
332 sum
+= p
[3*64] * q
[3*64];
333 sum
+= p
[4*64] * q
[4*64];
334 sum
+= p
[5*64] * q
[5*64];
335 sum
+= p
[6*64] * q
[6*64];
336 sum
+= p
[7*64] * q
[7*64];
341 tmp1
[0] = tmp
[16] >> WSHIFT
;
342 for( i
=1; i
<=16; i
++ ) tmp1
[i
] = (tmp
[i
+16]+tmp
[16-i
]) >> WSHIFT
;
343 for( i
=17; i
<=31; i
++ ) tmp1
[i
] = (tmp
[i
+16]-tmp
[80-i
]) >> WSHIFT
;
347 /* advance of 32 samples */
350 /* handle the wrap around */
352 memmove(s
->samples_buf
[ch
] + SAMPLES_BUF_SIZE
- (512 - 32),
353 s
->samples_buf
[ch
], (512 - 32) * 2);
354 offset
= SAMPLES_BUF_SIZE
- 512;
357 s
->samples_offset
[ch
] = offset
;
359 // print_pow(s->sb_samples, 1152);
362 static void compute_scale_factors(unsigned char scale_code
[SBLIMIT
],
363 unsigned char scale_factors
[SBLIMIT
][3],
364 int sb_samples
[3][12][SBLIMIT
],
367 int *p
, vmax
, v
, n
, i
, j
, k
, code
;
369 unsigned char *sf
= &scale_factors
[0][0];
371 for(j
=0;j
<sblimit
;j
++) {
373 /* find the max absolute value */
374 p
= &sb_samples
[i
][0][j
];
382 /* compute the scale factor index using log 2 computations */
385 /* n is the position of the MSB of vmax. now
386 use at most 2 compares to find the index */
387 index
= (21 - n
) * 3 - 3;
389 while (vmax
<= scale_factor_table
[index
+1])
392 index
= 0; /* very unlikely case of overflow */
395 index
= 62; /* value 63 is not allowed */
399 printf("%2d:%d in=%x %x %d\n",
400 j
, i
, vmax
, scale_factor_table
[index
], index
);
402 /* store the scale factor */
403 assert(index
>=0 && index
<= 63);
407 /* compute the transmission factor : look if the scale factors
408 are close enough to each other */
409 d1
= scale_diff_table
[sf
[0] - sf
[1] + 64];
410 d2
= scale_diff_table
[sf
[1] - sf
[2] + 64];
412 /* handle the 25 cases */
413 switch(d1
* 5 + d2
) {
445 sf
[1] = sf
[2] = sf
[0];
450 sf
[0] = sf
[1] = sf
[2];
456 sf
[0] = sf
[2] = sf
[1];
462 sf
[1] = sf
[2] = sf
[0];
469 printf("%d: %2d %2d %2d %d %d -> %d\n", j
,
470 sf
[0], sf
[1], sf
[2], d1
, d2
, code
);
472 scale_code
[j
] = code
;
477 /* The most important function : psycho acoustic module. In this
478 encoder there is basically none, so this is the worst you can do,
479 but also this is the simpler. */
480 static void psycho_acoustic_model(MpegAudioContext
*s
, short smr
[SBLIMIT
])
484 for(i
=0;i
<s
->sblimit
;i
++) {
485 smr
[i
] = (int)(fixed_smr
[i
] * 10);
490 #define SB_NOTALLOCATED 0
491 #define SB_ALLOCATED 1
494 /* Try to maximize the smr while using a number of bits inferior to
495 the frame size. I tried to make the code simpler, faster and
496 smaller than other encoders :-) */
497 static void compute_bit_allocation(MpegAudioContext
*s
,
498 short smr1
[MPA_MAX_CHANNELS
][SBLIMIT
],
499 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
502 int i
, ch
, b
, max_smr
, max_ch
, max_sb
, current_frame_size
, max_frame_size
;
504 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
505 unsigned char subband_status
[MPA_MAX_CHANNELS
][SBLIMIT
];
506 const unsigned char *alloc
;
508 memcpy(smr
, smr1
, s
->nb_channels
* sizeof(short) * SBLIMIT
);
509 memset(subband_status
, SB_NOTALLOCATED
, s
->nb_channels
* SBLIMIT
);
510 memset(bit_alloc
, 0, s
->nb_channels
* SBLIMIT
);
512 /* compute frame size and padding */
513 max_frame_size
= s
->frame_size
;
514 s
->frame_frac
+= s
->frame_frac_incr
;
515 if (s
->frame_frac
>= 65536) {
516 s
->frame_frac
-= 65536;
523 /* compute the header + bit alloc size */
524 current_frame_size
= 32;
525 alloc
= s
->alloc_table
;
526 for(i
=0;i
<s
->sblimit
;i
++) {
528 current_frame_size
+= incr
* s
->nb_channels
;
532 /* look for the subband with the largest signal to mask ratio */
535 max_smr
= 0x80000000;
536 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
537 for(i
=0;i
<s
->sblimit
;i
++) {
538 if (smr
[ch
][i
] > max_smr
&& subband_status
[ch
][i
] != SB_NOMORE
) {
539 max_smr
= smr
[ch
][i
];
546 printf("current=%d max=%d max_sb=%d alloc=%d\n",
547 current_frame_size
, max_frame_size
, max_sb
,
553 /* find alloc table entry (XXX: not optimal, should use
555 alloc
= s
->alloc_table
;
556 for(i
=0;i
<max_sb
;i
++) {
557 alloc
+= 1 << alloc
[0];
560 if (subband_status
[max_ch
][max_sb
] == SB_NOTALLOCATED
) {
561 /* nothing was coded for this band: add the necessary bits */
562 incr
= 2 + nb_scale_factors
[s
->scale_code
[max_ch
][max_sb
]] * 6;
563 incr
+= total_quant_bits
[alloc
[1]];
565 /* increments bit allocation */
566 b
= bit_alloc
[max_ch
][max_sb
];
567 incr
= total_quant_bits
[alloc
[b
+ 1]] -
568 total_quant_bits
[alloc
[b
]];
571 if (current_frame_size
+ incr
<= max_frame_size
) {
572 /* can increase size */
573 b
= ++bit_alloc
[max_ch
][max_sb
];
574 current_frame_size
+= incr
;
575 /* decrease smr by the resolution we added */
576 smr
[max_ch
][max_sb
] = smr1
[max_ch
][max_sb
] - quant_snr
[alloc
[b
]];
577 /* max allocation size reached ? */
578 if (b
== ((1 << alloc
[0]) - 1))
579 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
581 subband_status
[max_ch
][max_sb
] = SB_ALLOCATED
;
583 /* cannot increase the size of this subband */
584 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
587 *padding
= max_frame_size
- current_frame_size
;
588 assert(*padding
>= 0);
591 for(i
=0;i
<s
->sblimit
;i
++) {
592 printf("%d ", bit_alloc
[i
]);
599 * Output the mpeg audio layer 2 frame. Note how the code is small
600 * compared to other encoders :-)
602 static void encode_frame(MpegAudioContext
*s
,
603 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
606 int i
, j
, k
, l
, bit_alloc_bits
, b
, ch
;
609 PutBitContext
*p
= &s
->pb
;
613 put_bits(p
, 12, 0xfff);
614 put_bits(p
, 1, 1 - s
->lsf
); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
615 put_bits(p
, 2, 4-2); /* layer 2 */
616 put_bits(p
, 1, 1); /* no error protection */
617 put_bits(p
, 4, s
->bitrate_index
);
618 put_bits(p
, 2, s
->freq_index
);
619 put_bits(p
, 1, s
->do_padding
); /* use padding */
620 put_bits(p
, 1, 0); /* private_bit */
621 put_bits(p
, 2, s
->nb_channels
== 2 ? MPA_STEREO
: MPA_MONO
);
622 put_bits(p
, 2, 0); /* mode_ext */
623 put_bits(p
, 1, 0); /* no copyright */
624 put_bits(p
, 1, 1); /* original */
625 put_bits(p
, 2, 0); /* no emphasis */
629 for(i
=0;i
<s
->sblimit
;i
++) {
630 bit_alloc_bits
= s
->alloc_table
[j
];
631 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
632 put_bits(p
, bit_alloc_bits
, bit_alloc
[ch
][i
]);
634 j
+= 1 << bit_alloc_bits
;
638 for(i
=0;i
<s
->sblimit
;i
++) {
639 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
640 if (bit_alloc
[ch
][i
])
641 put_bits(p
, 2, s
->scale_code
[ch
][i
]);
646 for(i
=0;i
<s
->sblimit
;i
++) {
647 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
648 if (bit_alloc
[ch
][i
]) {
649 sf
= &s
->scale_factors
[ch
][i
][0];
650 switch(s
->scale_code
[ch
][i
]) {
652 put_bits(p
, 6, sf
[0]);
653 put_bits(p
, 6, sf
[1]);
654 put_bits(p
, 6, sf
[2]);
658 put_bits(p
, 6, sf
[0]);
659 put_bits(p
, 6, sf
[2]);
662 put_bits(p
, 6, sf
[0]);
669 /* quantization & write sub band samples */
674 for(i
=0;i
<s
->sblimit
;i
++) {
675 bit_alloc_bits
= s
->alloc_table
[j
];
676 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
677 b
= bit_alloc
[ch
][i
];
679 int qindex
, steps
, m
, sample
, bits
;
680 /* we encode 3 sub band samples of the same sub band at a time */
681 qindex
= s
->alloc_table
[j
+b
];
682 steps
= quant_steps
[qindex
];
684 sample
= s
->sb_samples
[ch
][k
][l
+ m
][i
];
685 /* divide by scale factor */
689 a
= (float)sample
* scale_factor_inv_table
[s
->scale_factors
[ch
][i
][k
]];
690 q
[m
] = (int)((a
+ 1.0) * steps
* 0.5);
694 int q1
, e
, shift
, mult
;
695 e
= s
->scale_factors
[ch
][i
][k
];
696 shift
= scale_factor_shift
[e
];
697 mult
= scale_factor_mult
[e
];
699 /* normalize to P bits */
701 q1
= sample
<< (-shift
);
703 q1
= sample
>> shift
;
704 q1
= (q1
* mult
) >> P
;
705 q
[m
] = ((q1
+ (1 << P
)) * steps
) >> (P
+ 1);
710 assert(q
[m
] >= 0 && q
[m
] < steps
);
712 bits
= quant_bits
[qindex
];
714 /* group the 3 values to save bits */
716 q
[0] + steps
* (q
[1] + steps
* q
[2]));
718 printf("%d: gr1 %d\n",
719 i
, q
[0] + steps
* (q
[1] + steps
* q
[2]));
723 printf("%d: gr3 %d %d %d\n",
724 i
, q
[0], q
[1], q
[2]);
726 put_bits(p
, bits
, q
[0]);
727 put_bits(p
, bits
, q
[1]);
728 put_bits(p
, bits
, q
[2]);
732 /* next subband in alloc table */
733 j
+= 1 << bit_alloc_bits
;
739 for(i
=0;i
<padding
;i
++)
746 static int MPA_encode_frame(AVCodecContext
*avctx
,
747 unsigned char *frame
, int buf_size
, void *data
)
749 MpegAudioContext
*s
= avctx
->priv_data
;
750 short *samples
= data
;
751 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
752 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
755 for(i
=0;i
<s
->nb_channels
;i
++) {
756 filter(s
, i
, samples
+ i
, s
->nb_channels
);
759 for(i
=0;i
<s
->nb_channels
;i
++) {
760 compute_scale_factors(s
->scale_code
[i
], s
->scale_factors
[i
],
761 s
->sb_samples
[i
], s
->sblimit
);
763 for(i
=0;i
<s
->nb_channels
;i
++) {
764 psycho_acoustic_model(s
, smr
[i
]);
766 compute_bit_allocation(s
, smr
, bit_alloc
, &padding
);
768 init_put_bits(&s
->pb
, frame
, MPA_MAX_CODED_FRAME_SIZE
);
770 encode_frame(s
, bit_alloc
, padding
);
772 s
->nb_samples
+= MPA_FRAME_SIZE
;
773 return pbBufPtr(&s
->pb
) - s
->pb
.buf
;
776 static int MPA_encode_close(AVCodecContext
*avctx
)
778 av_freep(&avctx
->coded_frame
);
782 AVCodec mp2_encoder
= {
786 sizeof(MpegAudioContext
),