2
2
* The simplest mpeg audio layer 2 encoder
3
3
* Copyright (c) 2000, 2001 Fabrice Bellard.
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* 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
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
21
23
* @file mpegaudio.c
22
24
* The simplest mpeg audio layer 2 encoder.
25
27
#include "avcodec.h"
28
#include "bitstream.h"
26
29
#include "mpegaudio.h"
28
31
/* currently, cannot change these constants (need to modify
29
32
quantization stage) */
32
33
#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
33
34
#define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
50
51
int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
51
52
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
52
53
/* code to group 3 scale factors */
53
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
54
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
54
55
int sblimit; /* number of used subbands */
55
56
const unsigned char *alloc_table;
56
57
} MpegAudioContext;
80
81
/* encoding freq */
83
if (mpa_freq_tab[i] == freq)
84
if (mpa_freq_tab[i] == freq)
85
86
if ((mpa_freq_tab[i] / 2) == freq) {
92
av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
94
97
/* encoding bitrate & frequency */
95
98
for(i=0;i<15;i++) {
96
if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)
99
if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)
103
av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
101
106
s->bitrate_index = i;
103
108
/* compute total header size & pad bit */
105
110
a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
106
111
s->frame_size = ((int)a) * 8;
108
113
/* frame fractional size to compute padding */
109
114
s->frame_frac = 0;
110
115
s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
112
117
/* select the right allocation table */
113
118
table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
117
122
s->alloc_table = alloc_tables[table];
120
printf("%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
125
av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
121
126
bitrate, freq, s->frame_size, table, s->frame_frac_incr);
208
213
} while (t != t1);
221
226
} while (t != t1);
227
232
int x1, x2, x3, x4;
229
234
x3 = MUL(t[16], FIX(SQRT2*0.5));
233
238
x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
234
239
x1 = MUL((t[8] - x2), xp[0]);
235
240
x2 = MUL((t[8] + x2), xp[1]);
268
273
xr = MUL(tab[30-i*4],xp[0]);
269
274
tab[30-i*4] = (tab[i*4] - xr);
270
275
tab[ i*4] = (tab[i*4] + xr);
272
277
xr = MUL(tab[ 2+i*4],xp[1]);
273
278
tab[ 2+i*4] = (tab[28-i*4] - xr);
274
279
tab[28-i*4] = (tab[28-i*4] + xr);
276
281
xr = MUL(tab[31-i*4],xp[0]);
277
282
tab[31-i*4] = (tab[1+i*4] - xr);
278
283
tab[ 1+i*4] = (tab[1+i*4] + xr);
280
285
xr = MUL(tab[ 3+i*4],xp[1]);
281
286
tab[ 3+i*4] = (tab[29-i*4] - xr);
282
287
tab[29-i*4] = (tab[29-i*4] + xr);
350
355
/* handle the wrap around */
351
356
if (offset < 0) {
352
memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
357
memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
353
358
s->samples_buf[ch], (512 - 32) * 2);
354
359
offset = SAMPLES_BUF_SIZE - 512;
362
367
static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
363
unsigned char scale_factors[SBLIMIT][3],
368
unsigned char scale_factors[SBLIMIT][3],
364
369
int sb_samples[3][12][SBLIMIT],
367
372
int *p, vmax, v, n, i, j, k, code;
368
373
int index, d1, d2;
369
374
unsigned char *sf = &scale_factors[0][0];
371
376
for(j=0;j<sblimit;j++) {
372
377
for(i=0;i<3;i++) {
373
378
/* find the max absolute value */
382
387
/* compute the scale factor index using log 2 computations */
384
389
n = av_log2(vmax);
385
/* n is the position of the MSB of vmax. now
390
/* n is the position of the MSB of vmax. now
386
391
use at most 2 compares to find the index */
387
392
index = (21 - n) * 3 - 3;
388
393
if (index >= 0) {
399
printf("%2d:%d in=%x %x %d\n",
404
printf("%2d:%d in=%x %x %d\n",
400
405
j, i, vmax, scale_factor_table[index], index);
402
407
/* store the scale factor */
462
467
sf[1] = sf[2] = sf[0];
470
assert(0); //cant happen
471
code = 0; /* kill warning */
469
printf("%d: %2d %2d %2d %d %d -> %d\n", j,
475
printf("%d: %2d %2d %2d %d %d -> %d\n", j,
470
476
sf[0], sf[1], sf[2], d1, d2, code);
472
478
scale_code[j] = code;
494
500
/* Try to maximize the smr while using a number of bits inferior to
495
501
the frame size. I tried to make the code simpler, faster and
496
502
smaller than other encoders :-) */
497
static void compute_bit_allocation(MpegAudioContext *s,
503
static void compute_bit_allocation(MpegAudioContext *s,
498
504
short smr1[MPA_MAX_CHANNELS][SBLIMIT],
499
505
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
508
514
memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
509
515
memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
510
516
memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
512
518
/* compute frame size and padding */
513
519
max_frame_size = s->frame_size;
514
520
s->frame_frac += s->frame_frac_incr;
546
printf("current=%d max=%d max_sb=%d alloc=%d\n",
552
printf("current=%d max=%d max_sb=%d alloc=%d\n",
547
553
current_frame_size, max_frame_size, max_sb,
548
554
bit_alloc[max_sb]);
553
559
/* find alloc table entry (XXX: not optimal, should use
554
560
pointer table) */
555
561
alloc = s->alloc_table;
565
571
/* increments bit allocation */
566
572
b = bit_alloc[max_ch][max_sb];
567
incr = total_quant_bits[alloc[b + 1]] -
573
incr = total_quant_bits[alloc[b + 1]] -
568
574
total_quant_bits[alloc[b]];
634
640
j += 1 << bit_alloc_bits;
637
643
/* scale codes */
638
644
for(i=0;i<s->sblimit;i++) {
639
645
for(ch=0;ch<s->nb_channels;ch++) {
640
if (bit_alloc[ch][i])
646
if (bit_alloc[ch][i])
641
647
put_bits(p, 2, s->scale_code[ch][i]);
712
718
bits = quant_bits[qindex];
714
720
/* group the 3 values to save bits */
716
722
q[0] + steps * (q[1] + steps * q[2]));
718
printf("%d: gr1 %d\n",
724
printf("%d: gr1 %d\n",
719
725
i, q[0] + steps * (q[1] + steps * q[2]));
723
printf("%d: gr3 %d %d %d\n",
729
printf("%d: gr3 %d %d %d\n",
724
730
i, q[0], q[1], q[2]);
726
732
put_bits(p, bits, q[0]);
727
733
put_bits(p, bits, q[1]);
728
734
put_bits(p, bits, q[2]);
746
752
static int MPA_encode_frame(AVCodecContext *avctx,
747
unsigned char *frame, int buf_size, void *data)
753
unsigned char *frame, int buf_size, void *data)
749
755
MpegAudioContext *s = avctx->priv_data;
750
756
short *samples = data;
759
765
for(i=0;i<s->nb_channels;i++) {
760
compute_scale_factors(s->scale_code[i], s->scale_factors[i],
766
compute_scale_factors(s->scale_code[i], s->scale_factors[i],
761
767
s->sb_samples[i], s->sblimit);
763
769
for(i=0;i<s->nb_channels;i++) {
766
772
compute_bit_allocation(s, smr, bit_alloc, &padding);
768
init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE, NULL, NULL);
774
init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
770
776
encode_frame(s, bit_alloc, padding);
772
778
s->nb_samples += MPA_FRAME_SIZE;
773
779
return pbBufPtr(&s->pb) - s->pb.buf;