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
24 * The simplest mpeg audio layer 2 encoder.
28 #include "bitstream.h"
29 #include "mpegaudio.h"
31 /* currently, cannot change these constants (need to modify
32 quantization stage) */
33 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> 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 "mpegaudiodata.h"
62 #include "mpegaudiotab.h"
64 static av_cold
int MPA_encode_init(AVCodecContext
*avctx
)
66 MpegAudioContext
*s
= avctx
->priv_data
;
67 int freq
= avctx
->sample_rate
;
68 int bitrate
= avctx
->bit_rate
;
69 int channels
= avctx
->channels
;
73 if (channels
<= 0 || channels
> 2){
74 av_log(avctx
, AV_LOG_ERROR
, "encoding %d channel(s) is not allowed in mp2\n", channels
);
77 bitrate
= bitrate
/ 1000;
78 s
->nb_channels
= channels
;
80 s
->bit_rate
= bitrate
* 1000;
81 avctx
->frame_size
= MPA_FRAME_SIZE
;
86 if (ff_mpa_freq_tab
[i
] == freq
)
88 if ((ff_mpa_freq_tab
[i
] / 2) == freq
) {
94 av_log(avctx
, AV_LOG_ERROR
, "Sampling rate %d is not allowed in mp2\n", freq
);
99 /* encoding bitrate & frequency */
101 if (ff_mpa_bitrate_tab
[s
->lsf
][1][i
] == bitrate
)
105 av_log(avctx
, AV_LOG_ERROR
, "bitrate %d is not allowed in mp2\n", bitrate
);
108 s
->bitrate_index
= i
;
110 /* compute total header size & pad bit */
112 a
= (float)(bitrate
* 1000 * MPA_FRAME_SIZE
) / (freq
* 8.0);
113 s
->frame_size
= ((int)a
) * 8;
115 /* frame fractional size to compute padding */
117 s
->frame_frac_incr
= (int)((a
- floor(a
)) * 65536.0);
119 /* select the right allocation table */
120 table
= ff_mpa_l2_select_table(bitrate
, s
->nb_channels
, freq
, s
->lsf
);
122 /* number of used subbands */
123 s
->sblimit
= ff_mpa_sblimit_table
[table
];
124 s
->alloc_table
= ff_mpa_alloc_tables
[table
];
127 av_log(avctx
, AV_LOG_DEBUG
, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
128 bitrate
, freq
, s
->frame_size
, table
, s
->frame_frac_incr
);
131 for(i
=0;i
<s
->nb_channels
;i
++)
132 s
->samples_offset
[i
] = 0;
136 v
= ff_mpa_enwindow
[i
];
138 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
144 filter_bank
[512 - i
] = v
;
148 v
= (int)(pow(2.0, (3 - i
) / 3.0) * (1 << 20));
151 scale_factor_table
[i
] = v
;
153 scale_factor_inv_table
[i
] = pow(2.0, -(3 - i
) / 3.0) / (float)(1 << 20);
156 scale_factor_shift
[i
] = 21 - P
- (i
/ 3);
157 scale_factor_mult
[i
] = (1 << P
) * pow(2.0, (i
% 3) / 3.0);
172 scale_diff_table
[i
] = v
;
176 v
= ff_mpa_quant_bits
[i
];
181 total_quant_bits
[i
] = 12 * v
;
184 avctx
->coded_frame
= avcodec_alloc_frame();
185 avctx
->coded_frame
->key_frame
= 1;
190 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
191 static void idct32(int *out
, int *tab
)
195 const int *xp
= costab32
;
197 for(j
=31;j
>=3;j
-=2) tab
[j
] += tab
[j
- 2];
236 x3
= MUL(t
[16], FIX(SQRT2
*0.5));
240 x2
= MUL(-(t
[24] + t
[8]), FIX(SQRT2
*0.5));
241 x1
= MUL((t
[8] - x2
), xp
[0]);
242 x2
= MUL((t
[8] + x2
), xp
[1]);
255 xr
= MUL(t
[28],xp
[0]);
259 xr
= MUL(t
[4],xp
[1]);
260 t
[ 4] = (t
[24] - xr
);
261 t
[24] = (t
[24] + xr
);
263 xr
= MUL(t
[20],xp
[2]);
267 xr
= MUL(t
[12],xp
[3]);
268 t
[12] = (t
[16] - xr
);
269 t
[16] = (t
[16] + xr
);
274 for (i
= 0; i
< 4; i
++) {
275 xr
= MUL(tab
[30-i
*4],xp
[0]);
276 tab
[30-i
*4] = (tab
[i
*4] - xr
);
277 tab
[ i
*4] = (tab
[i
*4] + xr
);
279 xr
= MUL(tab
[ 2+i
*4],xp
[1]);
280 tab
[ 2+i
*4] = (tab
[28-i
*4] - xr
);
281 tab
[28-i
*4] = (tab
[28-i
*4] + xr
);
283 xr
= MUL(tab
[31-i
*4],xp
[0]);
284 tab
[31-i
*4] = (tab
[1+i
*4] - xr
);
285 tab
[ 1+i
*4] = (tab
[1+i
*4] + xr
);
287 xr
= MUL(tab
[ 3+i
*4],xp
[1]);
288 tab
[ 3+i
*4] = (tab
[29-i
*4] - xr
);
289 tab
[29-i
*4] = (tab
[29-i
*4] + xr
);
297 xr
= MUL(t1
[0], *xp
);
306 out
[i
] = tab
[bitinv32
[i
]];
310 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
312 static void filter(MpegAudioContext
*s
, int ch
, short *samples
, int incr
)
315 int sum
, offset
, i
, j
;
320 // print_pow1(samples, 1152);
322 offset
= s
->samples_offset
[ch
];
323 out
= &s
->sb_samples
[ch
][0][0][0];
325 /* 32 samples at once */
327 s
->samples_buf
[ch
][offset
+ (31 - i
)] = samples
[0];
332 p
= s
->samples_buf
[ch
] + offset
;
336 sum
= p
[0*64] * q
[0*64];
337 sum
+= p
[1*64] * q
[1*64];
338 sum
+= p
[2*64] * q
[2*64];
339 sum
+= p
[3*64] * q
[3*64];
340 sum
+= p
[4*64] * q
[4*64];
341 sum
+= p
[5*64] * q
[5*64];
342 sum
+= p
[6*64] * q
[6*64];
343 sum
+= p
[7*64] * q
[7*64];
348 tmp1
[0] = tmp
[16] >> WSHIFT
;
349 for( i
=1; i
<=16; i
++ ) tmp1
[i
] = (tmp
[i
+16]+tmp
[16-i
]) >> WSHIFT
;
350 for( i
=17; i
<=31; i
++ ) tmp1
[i
] = (tmp
[i
+16]-tmp
[80-i
]) >> WSHIFT
;
354 /* advance of 32 samples */
357 /* handle the wrap around */
359 memmove(s
->samples_buf
[ch
] + SAMPLES_BUF_SIZE
- (512 - 32),
360 s
->samples_buf
[ch
], (512 - 32) * 2);
361 offset
= SAMPLES_BUF_SIZE
- 512;
364 s
->samples_offset
[ch
] = offset
;
366 // print_pow(s->sb_samples, 1152);
369 static void compute_scale_factors(unsigned char scale_code
[SBLIMIT
],
370 unsigned char scale_factors
[SBLIMIT
][3],
371 int sb_samples
[3][12][SBLIMIT
],
374 int *p
, vmax
, v
, n
, i
, j
, k
, code
;
376 unsigned char *sf
= &scale_factors
[0][0];
378 for(j
=0;j
<sblimit
;j
++) {
380 /* find the max absolute value */
381 p
= &sb_samples
[i
][0][j
];
389 /* compute the scale factor index using log 2 computations */
392 /* n is the position of the MSB of vmax. now
393 use at most 2 compares to find the index */
394 index
= (21 - n
) * 3 - 3;
396 while (vmax
<= scale_factor_table
[index
+1])
399 index
= 0; /* very unlikely case of overflow */
402 index
= 62; /* value 63 is not allowed */
406 printf("%2d:%d in=%x %x %d\n",
407 j
, i
, vmax
, scale_factor_table
[index
], index
);
409 /* store the scale factor */
410 assert(index
>=0 && index
<= 63);
414 /* compute the transmission factor : look if the scale factors
415 are close enough to each other */
416 d1
= scale_diff_table
[sf
[0] - sf
[1] + 64];
417 d2
= scale_diff_table
[sf
[1] - sf
[2] + 64];
419 /* handle the 25 cases */
420 switch(d1
* 5 + d2
) {
452 sf
[1] = sf
[2] = sf
[0];
457 sf
[0] = sf
[1] = sf
[2];
463 sf
[0] = sf
[2] = sf
[1];
469 sf
[1] = sf
[2] = sf
[0];
472 assert(0); //cannot happen
473 code
= 0; /* kill warning */
477 printf("%d: %2d %2d %2d %d %d -> %d\n", j
,
478 sf
[0], sf
[1], sf
[2], d1
, d2
, code
);
480 scale_code
[j
] = code
;
485 /* The most important function : psycho acoustic module. In this
486 encoder there is basically none, so this is the worst you can do,
487 but also this is the simpler. */
488 static void psycho_acoustic_model(MpegAudioContext
*s
, short smr
[SBLIMIT
])
492 for(i
=0;i
<s
->sblimit
;i
++) {
493 smr
[i
] = (int)(fixed_smr
[i
] * 10);
498 #define SB_NOTALLOCATED 0
499 #define SB_ALLOCATED 1
502 /* Try to maximize the smr while using a number of bits inferior to
503 the frame size. I tried to make the code simpler, faster and
504 smaller than other encoders :-) */
505 static void compute_bit_allocation(MpegAudioContext
*s
,
506 short smr1
[MPA_MAX_CHANNELS
][SBLIMIT
],
507 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
510 int i
, ch
, b
, max_smr
, max_ch
, max_sb
, current_frame_size
, max_frame_size
;
512 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
513 unsigned char subband_status
[MPA_MAX_CHANNELS
][SBLIMIT
];
514 const unsigned char *alloc
;
516 memcpy(smr
, smr1
, s
->nb_channels
* sizeof(short) * SBLIMIT
);
517 memset(subband_status
, SB_NOTALLOCATED
, s
->nb_channels
* SBLIMIT
);
518 memset(bit_alloc
, 0, s
->nb_channels
* SBLIMIT
);
520 /* compute frame size and padding */
521 max_frame_size
= s
->frame_size
;
522 s
->frame_frac
+= s
->frame_frac_incr
;
523 if (s
->frame_frac
>= 65536) {
524 s
->frame_frac
-= 65536;
531 /* compute the header + bit alloc size */
532 current_frame_size
= 32;
533 alloc
= s
->alloc_table
;
534 for(i
=0;i
<s
->sblimit
;i
++) {
536 current_frame_size
+= incr
* s
->nb_channels
;
540 /* look for the subband with the largest signal to mask ratio */
544 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
545 for(i
=0;i
<s
->sblimit
;i
++) {
546 if (smr
[ch
][i
] > max_smr
&& subband_status
[ch
][i
] != SB_NOMORE
) {
547 max_smr
= smr
[ch
][i
];
554 printf("current=%d max=%d max_sb=%d alloc=%d\n",
555 current_frame_size
, max_frame_size
, max_sb
,
561 /* find alloc table entry (XXX: not optimal, should use
563 alloc
= s
->alloc_table
;
564 for(i
=0;i
<max_sb
;i
++) {
565 alloc
+= 1 << alloc
[0];
568 if (subband_status
[max_ch
][max_sb
] == SB_NOTALLOCATED
) {
569 /* nothing was coded for this band: add the necessary bits */
570 incr
= 2 + nb_scale_factors
[s
->scale_code
[max_ch
][max_sb
]] * 6;
571 incr
+= total_quant_bits
[alloc
[1]];
573 /* increments bit allocation */
574 b
= bit_alloc
[max_ch
][max_sb
];
575 incr
= total_quant_bits
[alloc
[b
+ 1]] -
576 total_quant_bits
[alloc
[b
]];
579 if (current_frame_size
+ incr
<= max_frame_size
) {
580 /* can increase size */
581 b
= ++bit_alloc
[max_ch
][max_sb
];
582 current_frame_size
+= incr
;
583 /* decrease smr by the resolution we added */
584 smr
[max_ch
][max_sb
] = smr1
[max_ch
][max_sb
] - quant_snr
[alloc
[b
]];
585 /* max allocation size reached ? */
586 if (b
== ((1 << alloc
[0]) - 1))
587 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
589 subband_status
[max_ch
][max_sb
] = SB_ALLOCATED
;
591 /* cannot increase the size of this subband */
592 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
595 *padding
= max_frame_size
- current_frame_size
;
596 assert(*padding
>= 0);
599 for(i
=0;i
<s
->sblimit
;i
++) {
600 printf("%d ", bit_alloc
[i
]);
607 * Output the mpeg audio layer 2 frame. Note how the code is small
608 * compared to other encoders :-)
610 static void encode_frame(MpegAudioContext
*s
,
611 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
614 int i
, j
, k
, l
, bit_alloc_bits
, b
, ch
;
617 PutBitContext
*p
= &s
->pb
;
621 put_bits(p
, 12, 0xfff);
622 put_bits(p
, 1, 1 - s
->lsf
); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
623 put_bits(p
, 2, 4-2); /* layer 2 */
624 put_bits(p
, 1, 1); /* no error protection */
625 put_bits(p
, 4, s
->bitrate_index
);
626 put_bits(p
, 2, s
->freq_index
);
627 put_bits(p
, 1, s
->do_padding
); /* use padding */
628 put_bits(p
, 1, 0); /* private_bit */
629 put_bits(p
, 2, s
->nb_channels
== 2 ? MPA_STEREO
: MPA_MONO
);
630 put_bits(p
, 2, 0); /* mode_ext */
631 put_bits(p
, 1, 0); /* no copyright */
632 put_bits(p
, 1, 1); /* original */
633 put_bits(p
, 2, 0); /* no emphasis */
637 for(i
=0;i
<s
->sblimit
;i
++) {
638 bit_alloc_bits
= s
->alloc_table
[j
];
639 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
640 put_bits(p
, bit_alloc_bits
, bit_alloc
[ch
][i
]);
642 j
+= 1 << bit_alloc_bits
;
646 for(i
=0;i
<s
->sblimit
;i
++) {
647 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
648 if (bit_alloc
[ch
][i
])
649 put_bits(p
, 2, s
->scale_code
[ch
][i
]);
654 for(i
=0;i
<s
->sblimit
;i
++) {
655 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
656 if (bit_alloc
[ch
][i
]) {
657 sf
= &s
->scale_factors
[ch
][i
][0];
658 switch(s
->scale_code
[ch
][i
]) {
660 put_bits(p
, 6, sf
[0]);
661 put_bits(p
, 6, sf
[1]);
662 put_bits(p
, 6, sf
[2]);
666 put_bits(p
, 6, sf
[0]);
667 put_bits(p
, 6, sf
[2]);
670 put_bits(p
, 6, sf
[0]);
677 /* quantization & write sub band samples */
682 for(i
=0;i
<s
->sblimit
;i
++) {
683 bit_alloc_bits
= s
->alloc_table
[j
];
684 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
685 b
= bit_alloc
[ch
][i
];
687 int qindex
, steps
, m
, sample
, bits
;
688 /* we encode 3 sub band samples of the same sub band at a time */
689 qindex
= s
->alloc_table
[j
+b
];
690 steps
= ff_mpa_quant_steps
[qindex
];
692 sample
= s
->sb_samples
[ch
][k
][l
+ m
][i
];
693 /* divide by scale factor */
697 a
= (float)sample
* scale_factor_inv_table
[s
->scale_factors
[ch
][i
][k
]];
698 q
[m
] = (int)((a
+ 1.0) * steps
* 0.5);
702 int q1
, e
, shift
, mult
;
703 e
= s
->scale_factors
[ch
][i
][k
];
704 shift
= scale_factor_shift
[e
];
705 mult
= scale_factor_mult
[e
];
707 /* normalize to P bits */
709 q1
= sample
<< (-shift
);
711 q1
= sample
>> shift
;
712 q1
= (q1
* mult
) >> P
;
713 q
[m
] = ((q1
+ (1 << P
)) * steps
) >> (P
+ 1);
718 assert(q
[m
] >= 0 && q
[m
] < steps
);
720 bits
= ff_mpa_quant_bits
[qindex
];
722 /* group the 3 values to save bits */
724 q
[0] + steps
* (q
[1] + steps
* q
[2]));
726 printf("%d: gr1 %d\n",
727 i
, q
[0] + steps
* (q
[1] + steps
* q
[2]));
731 printf("%d: gr3 %d %d %d\n",
732 i
, q
[0], q
[1], q
[2]);
734 put_bits(p
, bits
, q
[0]);
735 put_bits(p
, bits
, q
[1]);
736 put_bits(p
, bits
, q
[2]);
740 /* next subband in alloc table */
741 j
+= 1 << bit_alloc_bits
;
747 for(i
=0;i
<padding
;i
++)
754 static int MPA_encode_frame(AVCodecContext
*avctx
,
755 unsigned char *frame
, int buf_size
, void *data
)
757 MpegAudioContext
*s
= avctx
->priv_data
;
758 short *samples
= data
;
759 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
760 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
763 for(i
=0;i
<s
->nb_channels
;i
++) {
764 filter(s
, i
, samples
+ i
, s
->nb_channels
);
767 for(i
=0;i
<s
->nb_channels
;i
++) {
768 compute_scale_factors(s
->scale_code
[i
], s
->scale_factors
[i
],
769 s
->sb_samples
[i
], s
->sblimit
);
771 for(i
=0;i
<s
->nb_channels
;i
++) {
772 psycho_acoustic_model(s
, smr
[i
]);
774 compute_bit_allocation(s
, smr
, bit_alloc
, &padding
);
776 init_put_bits(&s
->pb
, frame
, MPA_MAX_CODED_FRAME_SIZE
);
778 encode_frame(s
, bit_alloc
, padding
);
780 s
->nb_samples
+= MPA_FRAME_SIZE
;
781 return pbBufPtr(&s
->pb
) - s
->pb
.buf
;
784 static av_cold
int MPA_encode_close(AVCodecContext
*avctx
)
786 av_freep(&avctx
->coded_frame
);
790 AVCodec mp2_encoder
= {
794 sizeof(MpegAudioContext
),
799 .long_name
= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),