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
23 * @file libavcodec/mpegaudio.c
24 * The simplest mpeg audio layer 2 encoder.
28 #include "bitstream.h"
30 #undef CONFIG_MPEGAUDIO_HP
31 #define CONFIG_MPEGAUDIO_HP 0
32 #include "mpegaudio.h"
34 /* currently, cannot change these constants (need to modify
35 quantization stage) */
36 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
38 #define SAMPLES_BUF_SIZE 4096
40 typedef struct MpegAudioContext
{
44 int lsf
; /* 1 if mpeg2 low bitrate selected */
45 int bitrate_index
; /* bit rate */
47 int frame_size
; /* frame size, in bits, without padding */
48 int64_t nb_samples
; /* total number of samples encoded */
49 /* padding computation */
50 int frame_frac
, frame_frac_incr
, do_padding
;
51 short samples_buf
[MPA_MAX_CHANNELS
][SAMPLES_BUF_SIZE
]; /* buffer for filter */
52 int samples_offset
[MPA_MAX_CHANNELS
]; /* offset in samples_buf */
53 int sb_samples
[MPA_MAX_CHANNELS
][3][12][SBLIMIT
];
54 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3]; /* scale factors */
55 /* code to group 3 scale factors */
56 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
57 int sblimit
; /* number of used subbands */
58 const unsigned char *alloc_table
;
61 /* define it to use floats in quantization (I don't like floats !) */
64 #include "mpegaudiodata.h"
65 #include "mpegaudiotab.h"
67 static av_cold
int MPA_encode_init(AVCodecContext
*avctx
)
69 MpegAudioContext
*s
= avctx
->priv_data
;
70 int freq
= avctx
->sample_rate
;
71 int bitrate
= avctx
->bit_rate
;
72 int channels
= avctx
->channels
;
76 if (channels
<= 0 || channels
> 2){
77 av_log(avctx
, AV_LOG_ERROR
, "encoding %d channel(s) is not allowed in mp2\n", channels
);
80 bitrate
= bitrate
/ 1000;
81 s
->nb_channels
= channels
;
83 s
->bit_rate
= bitrate
* 1000;
84 avctx
->frame_size
= MPA_FRAME_SIZE
;
89 if (ff_mpa_freq_tab
[i
] == freq
)
91 if ((ff_mpa_freq_tab
[i
] / 2) == freq
) {
97 av_log(avctx
, AV_LOG_ERROR
, "Sampling rate %d is not allowed in mp2\n", freq
);
102 /* encoding bitrate & frequency */
104 if (ff_mpa_bitrate_tab
[s
->lsf
][1][i
] == bitrate
)
108 av_log(avctx
, AV_LOG_ERROR
, "bitrate %d is not allowed in mp2\n", bitrate
);
111 s
->bitrate_index
= i
;
113 /* compute total header size & pad bit */
115 a
= (float)(bitrate
* 1000 * MPA_FRAME_SIZE
) / (freq
* 8.0);
116 s
->frame_size
= ((int)a
) * 8;
118 /* frame fractional size to compute padding */
120 s
->frame_frac_incr
= (int)((a
- floor(a
)) * 65536.0);
122 /* select the right allocation table */
123 table
= ff_mpa_l2_select_table(bitrate
, s
->nb_channels
, freq
, s
->lsf
);
125 /* number of used subbands */
126 s
->sblimit
= ff_mpa_sblimit_table
[table
];
127 s
->alloc_table
= ff_mpa_alloc_tables
[table
];
130 av_log(avctx
, AV_LOG_DEBUG
, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
131 bitrate
, freq
, s
->frame_size
, table
, s
->frame_frac_incr
);
134 for(i
=0;i
<s
->nb_channels
;i
++)
135 s
->samples_offset
[i
] = 0;
139 v
= ff_mpa_enwindow
[i
];
141 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
147 filter_bank
[512 - i
] = v
;
151 v
= (int)(pow(2.0, (3 - i
) / 3.0) * (1 << 20));
154 scale_factor_table
[i
] = v
;
156 scale_factor_inv_table
[i
] = pow(2.0, -(3 - i
) / 3.0) / (float)(1 << 20);
159 scale_factor_shift
[i
] = 21 - P
- (i
/ 3);
160 scale_factor_mult
[i
] = (1 << P
) * pow(2.0, (i
% 3) / 3.0);
175 scale_diff_table
[i
] = v
;
179 v
= ff_mpa_quant_bits
[i
];
184 total_quant_bits
[i
] = 12 * v
;
187 avctx
->coded_frame
= avcodec_alloc_frame();
188 avctx
->coded_frame
->key_frame
= 1;
193 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
194 static void idct32(int *out
, int *tab
)
198 const int *xp
= costab32
;
200 for(j
=31;j
>=3;j
-=2) tab
[j
] += tab
[j
- 2];
239 x3
= MUL(t
[16], FIX(SQRT2
*0.5));
243 x2
= MUL(-(t
[24] + t
[8]), FIX(SQRT2
*0.5));
244 x1
= MUL((t
[8] - x2
), xp
[0]);
245 x2
= MUL((t
[8] + x2
), xp
[1]);
258 xr
= MUL(t
[28],xp
[0]);
262 xr
= MUL(t
[4],xp
[1]);
263 t
[ 4] = (t
[24] - xr
);
264 t
[24] = (t
[24] + xr
);
266 xr
= MUL(t
[20],xp
[2]);
270 xr
= MUL(t
[12],xp
[3]);
271 t
[12] = (t
[16] - xr
);
272 t
[16] = (t
[16] + xr
);
277 for (i
= 0; i
< 4; i
++) {
278 xr
= MUL(tab
[30-i
*4],xp
[0]);
279 tab
[30-i
*4] = (tab
[i
*4] - xr
);
280 tab
[ i
*4] = (tab
[i
*4] + xr
);
282 xr
= MUL(tab
[ 2+i
*4],xp
[1]);
283 tab
[ 2+i
*4] = (tab
[28-i
*4] - xr
);
284 tab
[28-i
*4] = (tab
[28-i
*4] + xr
);
286 xr
= MUL(tab
[31-i
*4],xp
[0]);
287 tab
[31-i
*4] = (tab
[1+i
*4] - xr
);
288 tab
[ 1+i
*4] = (tab
[1+i
*4] + xr
);
290 xr
= MUL(tab
[ 3+i
*4],xp
[1]);
291 tab
[ 3+i
*4] = (tab
[29-i
*4] - xr
);
292 tab
[29-i
*4] = (tab
[29-i
*4] + xr
);
300 xr
= MUL(t1
[0], *xp
);
309 out
[i
] = tab
[bitinv32
[i
]];
313 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
315 static void filter(MpegAudioContext
*s
, int ch
, short *samples
, int incr
)
318 int sum
, offset
, i
, j
;
323 // print_pow1(samples, 1152);
325 offset
= s
->samples_offset
[ch
];
326 out
= &s
->sb_samples
[ch
][0][0][0];
328 /* 32 samples at once */
330 s
->samples_buf
[ch
][offset
+ (31 - i
)] = samples
[0];
335 p
= s
->samples_buf
[ch
] + offset
;
339 sum
= p
[0*64] * q
[0*64];
340 sum
+= p
[1*64] * q
[1*64];
341 sum
+= p
[2*64] * q
[2*64];
342 sum
+= p
[3*64] * q
[3*64];
343 sum
+= p
[4*64] * q
[4*64];
344 sum
+= p
[5*64] * q
[5*64];
345 sum
+= p
[6*64] * q
[6*64];
346 sum
+= p
[7*64] * q
[7*64];
351 tmp1
[0] = tmp
[16] >> WSHIFT
;
352 for( i
=1; i
<=16; i
++ ) tmp1
[i
] = (tmp
[i
+16]+tmp
[16-i
]) >> WSHIFT
;
353 for( i
=17; i
<=31; i
++ ) tmp1
[i
] = (tmp
[i
+16]-tmp
[80-i
]) >> WSHIFT
;
357 /* advance of 32 samples */
360 /* handle the wrap around */
362 memmove(s
->samples_buf
[ch
] + SAMPLES_BUF_SIZE
- (512 - 32),
363 s
->samples_buf
[ch
], (512 - 32) * 2);
364 offset
= SAMPLES_BUF_SIZE
- 512;
367 s
->samples_offset
[ch
] = offset
;
369 // print_pow(s->sb_samples, 1152);
372 static void compute_scale_factors(unsigned char scale_code
[SBLIMIT
],
373 unsigned char scale_factors
[SBLIMIT
][3],
374 int sb_samples
[3][12][SBLIMIT
],
377 int *p
, vmax
, v
, n
, i
, j
, k
, code
;
379 unsigned char *sf
= &scale_factors
[0][0];
381 for(j
=0;j
<sblimit
;j
++) {
383 /* find the max absolute value */
384 p
= &sb_samples
[i
][0][j
];
392 /* compute the scale factor index using log 2 computations */
395 /* n is the position of the MSB of vmax. now
396 use at most 2 compares to find the index */
397 index
= (21 - n
) * 3 - 3;
399 while (vmax
<= scale_factor_table
[index
+1])
402 index
= 0; /* very unlikely case of overflow */
405 index
= 62; /* value 63 is not allowed */
409 printf("%2d:%d in=%x %x %d\n",
410 j
, i
, vmax
, scale_factor_table
[index
], index
);
412 /* store the scale factor */
413 assert(index
>=0 && index
<= 63);
417 /* compute the transmission factor : look if the scale factors
418 are close enough to each other */
419 d1
= scale_diff_table
[sf
[0] - sf
[1] + 64];
420 d2
= scale_diff_table
[sf
[1] - sf
[2] + 64];
422 /* handle the 25 cases */
423 switch(d1
* 5 + d2
) {
455 sf
[1] = sf
[2] = sf
[0];
460 sf
[0] = sf
[1] = sf
[2];
466 sf
[0] = sf
[2] = sf
[1];
472 sf
[1] = sf
[2] = sf
[0];
475 assert(0); //cannot happen
476 code
= 0; /* kill warning */
480 printf("%d: %2d %2d %2d %d %d -> %d\n", j
,
481 sf
[0], sf
[1], sf
[2], d1
, d2
, code
);
483 scale_code
[j
] = code
;
488 /* The most important function : psycho acoustic module. In this
489 encoder there is basically none, so this is the worst you can do,
490 but also this is the simpler. */
491 static void psycho_acoustic_model(MpegAudioContext
*s
, short smr
[SBLIMIT
])
495 for(i
=0;i
<s
->sblimit
;i
++) {
496 smr
[i
] = (int)(fixed_smr
[i
] * 10);
501 #define SB_NOTALLOCATED 0
502 #define SB_ALLOCATED 1
505 /* Try to maximize the smr while using a number of bits inferior to
506 the frame size. I tried to make the code simpler, faster and
507 smaller than other encoders :-) */
508 static void compute_bit_allocation(MpegAudioContext
*s
,
509 short smr1
[MPA_MAX_CHANNELS
][SBLIMIT
],
510 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
513 int i
, ch
, b
, max_smr
, max_ch
, max_sb
, current_frame_size
, max_frame_size
;
515 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
516 unsigned char subband_status
[MPA_MAX_CHANNELS
][SBLIMIT
];
517 const unsigned char *alloc
;
519 memcpy(smr
, smr1
, s
->nb_channels
* sizeof(short) * SBLIMIT
);
520 memset(subband_status
, SB_NOTALLOCATED
, s
->nb_channels
* SBLIMIT
);
521 memset(bit_alloc
, 0, s
->nb_channels
* SBLIMIT
);
523 /* compute frame size and padding */
524 max_frame_size
= s
->frame_size
;
525 s
->frame_frac
+= s
->frame_frac_incr
;
526 if (s
->frame_frac
>= 65536) {
527 s
->frame_frac
-= 65536;
534 /* compute the header + bit alloc size */
535 current_frame_size
= 32;
536 alloc
= s
->alloc_table
;
537 for(i
=0;i
<s
->sblimit
;i
++) {
539 current_frame_size
+= incr
* s
->nb_channels
;
543 /* look for the subband with the largest signal to mask ratio */
547 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
548 for(i
=0;i
<s
->sblimit
;i
++) {
549 if (smr
[ch
][i
] > max_smr
&& subband_status
[ch
][i
] != SB_NOMORE
) {
550 max_smr
= smr
[ch
][i
];
557 printf("current=%d max=%d max_sb=%d alloc=%d\n",
558 current_frame_size
, max_frame_size
, max_sb
,
564 /* find alloc table entry (XXX: not optimal, should use
566 alloc
= s
->alloc_table
;
567 for(i
=0;i
<max_sb
;i
++) {
568 alloc
+= 1 << alloc
[0];
571 if (subband_status
[max_ch
][max_sb
] == SB_NOTALLOCATED
) {
572 /* nothing was coded for this band: add the necessary bits */
573 incr
= 2 + nb_scale_factors
[s
->scale_code
[max_ch
][max_sb
]] * 6;
574 incr
+= total_quant_bits
[alloc
[1]];
576 /* increments bit allocation */
577 b
= bit_alloc
[max_ch
][max_sb
];
578 incr
= total_quant_bits
[alloc
[b
+ 1]] -
579 total_quant_bits
[alloc
[b
]];
582 if (current_frame_size
+ incr
<= max_frame_size
) {
583 /* can increase size */
584 b
= ++bit_alloc
[max_ch
][max_sb
];
585 current_frame_size
+= incr
;
586 /* decrease smr by the resolution we added */
587 smr
[max_ch
][max_sb
] = smr1
[max_ch
][max_sb
] - quant_snr
[alloc
[b
]];
588 /* max allocation size reached ? */
589 if (b
== ((1 << alloc
[0]) - 1))
590 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
592 subband_status
[max_ch
][max_sb
] = SB_ALLOCATED
;
594 /* cannot increase the size of this subband */
595 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
598 *padding
= max_frame_size
- current_frame_size
;
599 assert(*padding
>= 0);
602 for(i
=0;i
<s
->sblimit
;i
++) {
603 printf("%d ", bit_alloc
[i
]);
610 * Output the mpeg audio layer 2 frame. Note how the code is small
611 * compared to other encoders :-)
613 static void encode_frame(MpegAudioContext
*s
,
614 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
617 int i
, j
, k
, l
, bit_alloc_bits
, b
, ch
;
620 PutBitContext
*p
= &s
->pb
;
624 put_bits(p
, 12, 0xfff);
625 put_bits(p
, 1, 1 - s
->lsf
); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
626 put_bits(p
, 2, 4-2); /* layer 2 */
627 put_bits(p
, 1, 1); /* no error protection */
628 put_bits(p
, 4, s
->bitrate_index
);
629 put_bits(p
, 2, s
->freq_index
);
630 put_bits(p
, 1, s
->do_padding
); /* use padding */
631 put_bits(p
, 1, 0); /* private_bit */
632 put_bits(p
, 2, s
->nb_channels
== 2 ? MPA_STEREO
: MPA_MONO
);
633 put_bits(p
, 2, 0); /* mode_ext */
634 put_bits(p
, 1, 0); /* no copyright */
635 put_bits(p
, 1, 1); /* original */
636 put_bits(p
, 2, 0); /* no emphasis */
640 for(i
=0;i
<s
->sblimit
;i
++) {
641 bit_alloc_bits
= s
->alloc_table
[j
];
642 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
643 put_bits(p
, bit_alloc_bits
, bit_alloc
[ch
][i
]);
645 j
+= 1 << bit_alloc_bits
;
649 for(i
=0;i
<s
->sblimit
;i
++) {
650 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
651 if (bit_alloc
[ch
][i
])
652 put_bits(p
, 2, s
->scale_code
[ch
][i
]);
657 for(i
=0;i
<s
->sblimit
;i
++) {
658 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
659 if (bit_alloc
[ch
][i
]) {
660 sf
= &s
->scale_factors
[ch
][i
][0];
661 switch(s
->scale_code
[ch
][i
]) {
663 put_bits(p
, 6, sf
[0]);
664 put_bits(p
, 6, sf
[1]);
665 put_bits(p
, 6, sf
[2]);
669 put_bits(p
, 6, sf
[0]);
670 put_bits(p
, 6, sf
[2]);
673 put_bits(p
, 6, sf
[0]);
680 /* quantization & write sub band samples */
685 for(i
=0;i
<s
->sblimit
;i
++) {
686 bit_alloc_bits
= s
->alloc_table
[j
];
687 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
688 b
= bit_alloc
[ch
][i
];
690 int qindex
, steps
, m
, sample
, bits
;
691 /* we encode 3 sub band samples of the same sub band at a time */
692 qindex
= s
->alloc_table
[j
+b
];
693 steps
= ff_mpa_quant_steps
[qindex
];
695 sample
= s
->sb_samples
[ch
][k
][l
+ m
][i
];
696 /* divide by scale factor */
700 a
= (float)sample
* scale_factor_inv_table
[s
->scale_factors
[ch
][i
][k
]];
701 q
[m
] = (int)((a
+ 1.0) * steps
* 0.5);
705 int q1
, e
, shift
, mult
;
706 e
= s
->scale_factors
[ch
][i
][k
];
707 shift
= scale_factor_shift
[e
];
708 mult
= scale_factor_mult
[e
];
710 /* normalize to P bits */
712 q1
= sample
<< (-shift
);
714 q1
= sample
>> shift
;
715 q1
= (q1
* mult
) >> P
;
716 q
[m
] = ((q1
+ (1 << P
)) * steps
) >> (P
+ 1);
721 assert(q
[m
] >= 0 && q
[m
] < steps
);
723 bits
= ff_mpa_quant_bits
[qindex
];
725 /* group the 3 values to save bits */
727 q
[0] + steps
* (q
[1] + steps
* q
[2]));
729 printf("%d: gr1 %d\n",
730 i
, q
[0] + steps
* (q
[1] + steps
* q
[2]));
734 printf("%d: gr3 %d %d %d\n",
735 i
, q
[0], q
[1], q
[2]);
737 put_bits(p
, bits
, q
[0]);
738 put_bits(p
, bits
, q
[1]);
739 put_bits(p
, bits
, q
[2]);
743 /* next subband in alloc table */
744 j
+= 1 << bit_alloc_bits
;
750 for(i
=0;i
<padding
;i
++)
757 static int MPA_encode_frame(AVCodecContext
*avctx
,
758 unsigned char *frame
, int buf_size
, void *data
)
760 MpegAudioContext
*s
= avctx
->priv_data
;
761 short *samples
= data
;
762 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
763 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
766 for(i
=0;i
<s
->nb_channels
;i
++) {
767 filter(s
, i
, samples
+ i
, s
->nb_channels
);
770 for(i
=0;i
<s
->nb_channels
;i
++) {
771 compute_scale_factors(s
->scale_code
[i
], s
->scale_factors
[i
],
772 s
->sb_samples
[i
], s
->sblimit
);
774 for(i
=0;i
<s
->nb_channels
;i
++) {
775 psycho_acoustic_model(s
, smr
[i
]);
777 compute_bit_allocation(s
, smr
, bit_alloc
, &padding
);
779 init_put_bits(&s
->pb
, frame
, MPA_MAX_CODED_FRAME_SIZE
);
781 encode_frame(s
, bit_alloc
, padding
);
783 s
->nb_samples
+= MPA_FRAME_SIZE
;
784 return pbBufPtr(&s
->pb
) - s
->pb
.buf
;
787 static av_cold
int MPA_encode_close(AVCodecContext
*avctx
)
789 av_freep(&avctx
->coded_frame
);
793 AVCodec mp2_encoder
= {
797 sizeof(MpegAudioContext
),
802 .sample_fmts
= (enum SampleFormat
[]){SAMPLE_FMT_S16
,SAMPLE_FMT_NONE
},
803 .long_name
= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),