d521acc588c99df03a1bac48861e1183f86a06b3
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 "bitstream.h"
27 #include "mpegaudio.h"
29 /* currently, cannot change these constants (need to modify
30 quantization stage) */
33 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
34 #define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
36 #define SAMPLES_BUF_SIZE 4096
38 typedef struct MpegAudioContext
{
42 int lsf
; /* 1 if mpeg2 low bitrate selected */
43 int bitrate_index
; /* bit rate */
45 int frame_size
; /* frame size, in bits, without padding */
46 int64_t nb_samples
; /* total number of samples encoded */
47 /* padding computation */
48 int frame_frac
, frame_frac_incr
, do_padding
;
49 short samples_buf
[MPA_MAX_CHANNELS
][SAMPLES_BUF_SIZE
]; /* buffer for filter */
50 int samples_offset
[MPA_MAX_CHANNELS
]; /* offset in samples_buf */
51 int sb_samples
[MPA_MAX_CHANNELS
][3][12][SBLIMIT
];
52 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3]; /* scale factors */
53 /* code to group 3 scale factors */
54 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
55 int sblimit
; /* number of used subbands */
56 const unsigned char *alloc_table
;
59 /* define it to use floats in quantization (I don't like floats !) */
62 #include "mpegaudiotab.h"
64 static 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
;
75 bitrate
= bitrate
/ 1000;
76 s
->nb_channels
= channels
;
78 s
->bit_rate
= bitrate
* 1000;
79 avctx
->frame_size
= MPA_FRAME_SIZE
;
84 if (mpa_freq_tab
[i
] == freq
)
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
);
97 /* encoding bitrate & frequency */
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
);
106 s
->bitrate_index
= i
;
108 /* compute total header size & pad bit */
110 a
= (float)(bitrate
* 1000 * MPA_FRAME_SIZE
) / (freq
* 8.0);
111 s
->frame_size
= ((int)a
) * 8;
113 /* frame fractional size to compute padding */
115 s
->frame_frac_incr
= (int)((a
- floor(a
)) * 65536.0);
117 /* select the right allocation table */
118 table
= l2_select_table(bitrate
, s
->nb_channels
, freq
, s
->lsf
);
120 /* number of used subbands */
121 s
->sblimit
= sblimit_table
[table
];
122 s
->alloc_table
= alloc_tables
[table
];
125 av_log(avctx
, AV_LOG_DEBUG
, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
126 bitrate
, freq
, s
->frame_size
, table
, s
->frame_frac_incr
);
129 for(i
=0;i
<s
->nb_channels
;i
++)
130 s
->samples_offset
[i
] = 0;
136 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
142 filter_bank
[512 - i
] = v
;
146 v
= (int)(pow(2.0, (3 - i
) / 3.0) * (1 << 20));
149 scale_factor_table
[i
] = v
;
151 scale_factor_inv_table
[i
] = pow(2.0, -(3 - i
) / 3.0) / (float)(1 << 20);
154 scale_factor_shift
[i
] = 21 - P
- (i
/ 3);
155 scale_factor_mult
[i
] = (1 << P
) * pow(2.0, (i
% 3) / 3.0);
170 scale_diff_table
[i
] = v
;
179 total_quant_bits
[i
] = 12 * v
;
182 avctx
->coded_frame
= avcodec_alloc_frame();
183 avctx
->coded_frame
->key_frame
= 1;
188 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
189 static void idct32(int *out
, int *tab
)
193 const int *xp
= costab32
;
195 for(j
=31;j
>=3;j
-=2) tab
[j
] += tab
[j
- 2];
234 x3
= MUL(t
[16], FIX(SQRT2
*0.5));
238 x2
= MUL(-(t
[24] + t
[8]), FIX(SQRT2
*0.5));
239 x1
= MUL((t
[8] - x2
), xp
[0]);
240 x2
= MUL((t
[8] + x2
), xp
[1]);
253 xr
= MUL(t
[28],xp
[0]);
257 xr
= MUL(t
[4],xp
[1]);
258 t
[ 4] = (t
[24] - xr
);
259 t
[24] = (t
[24] + xr
);
261 xr
= MUL(t
[20],xp
[2]);
265 xr
= MUL(t
[12],xp
[3]);
266 t
[12] = (t
[16] - xr
);
267 t
[16] = (t
[16] + xr
);
272 for (i
= 0; i
< 4; i
++) {
273 xr
= MUL(tab
[30-i
*4],xp
[0]);
274 tab
[30-i
*4] = (tab
[i
*4] - xr
);
275 tab
[ i
*4] = (tab
[i
*4] + xr
);
277 xr
= MUL(tab
[ 2+i
*4],xp
[1]);
278 tab
[ 2+i
*4] = (tab
[28-i
*4] - xr
);
279 tab
[28-i
*4] = (tab
[28-i
*4] + xr
);
281 xr
= MUL(tab
[31-i
*4],xp
[0]);
282 tab
[31-i
*4] = (tab
[1+i
*4] - xr
);
283 tab
[ 1+i
*4] = (tab
[1+i
*4] + xr
);
285 xr
= MUL(tab
[ 3+i
*4],xp
[1]);
286 tab
[ 3+i
*4] = (tab
[29-i
*4] - xr
);
287 tab
[29-i
*4] = (tab
[29-i
*4] + xr
);
295 xr
= MUL(t1
[0], *xp
);
304 out
[i
] = tab
[bitinv32
[i
]];
308 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
310 static void filter(MpegAudioContext
*s
, int ch
, short *samples
, int incr
)
313 int sum
, offset
, i
, j
;
318 // print_pow1(samples, 1152);
320 offset
= s
->samples_offset
[ch
];
321 out
= &s
->sb_samples
[ch
][0][0][0];
323 /* 32 samples at once */
325 s
->samples_buf
[ch
][offset
+ (31 - i
)] = samples
[0];
330 p
= s
->samples_buf
[ch
] + offset
;
334 sum
= p
[0*64] * q
[0*64];
335 sum
+= p
[1*64] * q
[1*64];
336 sum
+= p
[2*64] * q
[2*64];
337 sum
+= p
[3*64] * q
[3*64];
338 sum
+= p
[4*64] * q
[4*64];
339 sum
+= p
[5*64] * q
[5*64];
340 sum
+= p
[6*64] * q
[6*64];
341 sum
+= p
[7*64] * q
[7*64];
346 tmp1
[0] = tmp
[16] >> WSHIFT
;
347 for( i
=1; i
<=16; i
++ ) tmp1
[i
] = (tmp
[i
+16]+tmp
[16-i
]) >> WSHIFT
;
348 for( i
=17; i
<=31; i
++ ) tmp1
[i
] = (tmp
[i
+16]-tmp
[80-i
]) >> WSHIFT
;
352 /* advance of 32 samples */
355 /* handle the wrap around */
357 memmove(s
->samples_buf
[ch
] + SAMPLES_BUF_SIZE
- (512 - 32),
358 s
->samples_buf
[ch
], (512 - 32) * 2);
359 offset
= SAMPLES_BUF_SIZE
- 512;
362 s
->samples_offset
[ch
] = offset
;
364 // print_pow(s->sb_samples, 1152);
367 static void compute_scale_factors(unsigned char scale_code
[SBLIMIT
],
368 unsigned char scale_factors
[SBLIMIT
][3],
369 int sb_samples
[3][12][SBLIMIT
],
372 int *p
, vmax
, v
, n
, i
, j
, k
, code
;
374 unsigned char *sf
= &scale_factors
[0][0];
376 for(j
=0;j
<sblimit
;j
++) {
378 /* find the max absolute value */
379 p
= &sb_samples
[i
][0][j
];
387 /* compute the scale factor index using log 2 computations */
390 /* n is the position of the MSB of vmax. now
391 use at most 2 compares to find the index */
392 index
= (21 - n
) * 3 - 3;
394 while (vmax
<= scale_factor_table
[index
+1])
397 index
= 0; /* very unlikely case of overflow */
400 index
= 62; /* value 63 is not allowed */
404 printf("%2d:%d in=%x %x %d\n",
405 j
, i
, vmax
, scale_factor_table
[index
], index
);
407 /* store the scale factor */
408 assert(index
>=0 && index
<= 63);
412 /* compute the transmission factor : look if the scale factors
413 are close enough to each other */
414 d1
= scale_diff_table
[sf
[0] - sf
[1] + 64];
415 d2
= scale_diff_table
[sf
[1] - sf
[2] + 64];
417 /* handle the 25 cases */
418 switch(d1
* 5 + d2
) {
450 sf
[1] = sf
[2] = sf
[0];
455 sf
[0] = sf
[1] = sf
[2];
461 sf
[0] = sf
[2] = sf
[1];
467 sf
[1] = sf
[2] = sf
[0];
470 assert(0); //cant happen
471 code
= 0; /* kill warning */
475 printf("%d: %2d %2d %2d %d %d -> %d\n", j
,
476 sf
[0], sf
[1], sf
[2], d1
, d2
, code
);
478 scale_code
[j
] = code
;
483 /* The most important function : psycho acoustic module. In this
484 encoder there is basically none, so this is the worst you can do,
485 but also this is the simpler. */
486 static void psycho_acoustic_model(MpegAudioContext
*s
, short smr
[SBLIMIT
])
490 for(i
=0;i
<s
->sblimit
;i
++) {
491 smr
[i
] = (int)(fixed_smr
[i
] * 10);
496 #define SB_NOTALLOCATED 0
497 #define SB_ALLOCATED 1
500 /* Try to maximize the smr while using a number of bits inferior to
501 the frame size. I tried to make the code simpler, faster and
502 smaller than other encoders :-) */
503 static void compute_bit_allocation(MpegAudioContext
*s
,
504 short smr1
[MPA_MAX_CHANNELS
][SBLIMIT
],
505 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
508 int i
, ch
, b
, max_smr
, max_ch
, max_sb
, current_frame_size
, max_frame_size
;
510 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
511 unsigned char subband_status
[MPA_MAX_CHANNELS
][SBLIMIT
];
512 const unsigned char *alloc
;
514 memcpy(smr
, smr1
, s
->nb_channels
* sizeof(short) * SBLIMIT
);
515 memset(subband_status
, SB_NOTALLOCATED
, s
->nb_channels
* SBLIMIT
);
516 memset(bit_alloc
, 0, s
->nb_channels
* SBLIMIT
);
518 /* compute frame size and padding */
519 max_frame_size
= s
->frame_size
;
520 s
->frame_frac
+= s
->frame_frac_incr
;
521 if (s
->frame_frac
>= 65536) {
522 s
->frame_frac
-= 65536;
529 /* compute the header + bit alloc size */
530 current_frame_size
= 32;
531 alloc
= s
->alloc_table
;
532 for(i
=0;i
<s
->sblimit
;i
++) {
534 current_frame_size
+= incr
* s
->nb_channels
;
538 /* look for the subband with the largest signal to mask ratio */
541 max_smr
= 0x80000000;
542 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
543 for(i
=0;i
<s
->sblimit
;i
++) {
544 if (smr
[ch
][i
] > max_smr
&& subband_status
[ch
][i
] != SB_NOMORE
) {
545 max_smr
= smr
[ch
][i
];
552 printf("current=%d max=%d max_sb=%d alloc=%d\n",
553 current_frame_size
, max_frame_size
, max_sb
,
559 /* find alloc table entry (XXX: not optimal, should use
561 alloc
= s
->alloc_table
;
562 for(i
=0;i
<max_sb
;i
++) {
563 alloc
+= 1 << alloc
[0];
566 if (subband_status
[max_ch
][max_sb
] == SB_NOTALLOCATED
) {
567 /* nothing was coded for this band: add the necessary bits */
568 incr
= 2 + nb_scale_factors
[s
->scale_code
[max_ch
][max_sb
]] * 6;
569 incr
+= total_quant_bits
[alloc
[1]];
571 /* increments bit allocation */
572 b
= bit_alloc
[max_ch
][max_sb
];
573 incr
= total_quant_bits
[alloc
[b
+ 1]] -
574 total_quant_bits
[alloc
[b
]];
577 if (current_frame_size
+ incr
<= max_frame_size
) {
578 /* can increase size */
579 b
= ++bit_alloc
[max_ch
][max_sb
];
580 current_frame_size
+= incr
;
581 /* decrease smr by the resolution we added */
582 smr
[max_ch
][max_sb
] = smr1
[max_ch
][max_sb
] - quant_snr
[alloc
[b
]];
583 /* max allocation size reached ? */
584 if (b
== ((1 << alloc
[0]) - 1))
585 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
587 subband_status
[max_ch
][max_sb
] = SB_ALLOCATED
;
589 /* cannot increase the size of this subband */
590 subband_status
[max_ch
][max_sb
] = SB_NOMORE
;
593 *padding
= max_frame_size
- current_frame_size
;
594 assert(*padding
>= 0);
597 for(i
=0;i
<s
->sblimit
;i
++) {
598 printf("%d ", bit_alloc
[i
]);
605 * Output the mpeg audio layer 2 frame. Note how the code is small
606 * compared to other encoders :-)
608 static void encode_frame(MpegAudioContext
*s
,
609 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
],
612 int i
, j
, k
, l
, bit_alloc_bits
, b
, ch
;
615 PutBitContext
*p
= &s
->pb
;
619 put_bits(p
, 12, 0xfff);
620 put_bits(p
, 1, 1 - s
->lsf
); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
621 put_bits(p
, 2, 4-2); /* layer 2 */
622 put_bits(p
, 1, 1); /* no error protection */
623 put_bits(p
, 4, s
->bitrate_index
);
624 put_bits(p
, 2, s
->freq_index
);
625 put_bits(p
, 1, s
->do_padding
); /* use padding */
626 put_bits(p
, 1, 0); /* private_bit */
627 put_bits(p
, 2, s
->nb_channels
== 2 ? MPA_STEREO
: MPA_MONO
);
628 put_bits(p
, 2, 0); /* mode_ext */
629 put_bits(p
, 1, 0); /* no copyright */
630 put_bits(p
, 1, 1); /* original */
631 put_bits(p
, 2, 0); /* no emphasis */
635 for(i
=0;i
<s
->sblimit
;i
++) {
636 bit_alloc_bits
= s
->alloc_table
[j
];
637 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
638 put_bits(p
, bit_alloc_bits
, bit_alloc
[ch
][i
]);
640 j
+= 1 << bit_alloc_bits
;
644 for(i
=0;i
<s
->sblimit
;i
++) {
645 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
646 if (bit_alloc
[ch
][i
])
647 put_bits(p
, 2, s
->scale_code
[ch
][i
]);
652 for(i
=0;i
<s
->sblimit
;i
++) {
653 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
654 if (bit_alloc
[ch
][i
]) {
655 sf
= &s
->scale_factors
[ch
][i
][0];
656 switch(s
->scale_code
[ch
][i
]) {
658 put_bits(p
, 6, sf
[0]);
659 put_bits(p
, 6, sf
[1]);
660 put_bits(p
, 6, sf
[2]);
664 put_bits(p
, 6, sf
[0]);
665 put_bits(p
, 6, sf
[2]);
668 put_bits(p
, 6, sf
[0]);
675 /* quantization & write sub band samples */
680 for(i
=0;i
<s
->sblimit
;i
++) {
681 bit_alloc_bits
= s
->alloc_table
[j
];
682 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
683 b
= bit_alloc
[ch
][i
];
685 int qindex
, steps
, m
, sample
, bits
;
686 /* we encode 3 sub band samples of the same sub band at a time */
687 qindex
= s
->alloc_table
[j
+b
];
688 steps
= quant_steps
[qindex
];
690 sample
= s
->sb_samples
[ch
][k
][l
+ m
][i
];
691 /* divide by scale factor */
695 a
= (float)sample
* scale_factor_inv_table
[s
->scale_factors
[ch
][i
][k
]];
696 q
[m
] = (int)((a
+ 1.0) * steps
* 0.5);
700 int q1
, e
, shift
, mult
;
701 e
= s
->scale_factors
[ch
][i
][k
];
702 shift
= scale_factor_shift
[e
];
703 mult
= scale_factor_mult
[e
];
705 /* normalize to P bits */
707 q1
= sample
<< (-shift
);
709 q1
= sample
>> shift
;
710 q1
= (q1
* mult
) >> P
;
711 q
[m
] = ((q1
+ (1 << P
)) * steps
) >> (P
+ 1);
716 assert(q
[m
] >= 0 && q
[m
] < steps
);
718 bits
= quant_bits
[qindex
];
720 /* group the 3 values to save bits */
722 q
[0] + steps
* (q
[1] + steps
* q
[2]));
724 printf("%d: gr1 %d\n",
725 i
, q
[0] + steps
* (q
[1] + steps
* q
[2]));
729 printf("%d: gr3 %d %d %d\n",
730 i
, q
[0], q
[1], q
[2]);
732 put_bits(p
, bits
, q
[0]);
733 put_bits(p
, bits
, q
[1]);
734 put_bits(p
, bits
, q
[2]);
738 /* next subband in alloc table */
739 j
+= 1 << bit_alloc_bits
;
745 for(i
=0;i
<padding
;i
++)
752 static int MPA_encode_frame(AVCodecContext
*avctx
,
753 unsigned char *frame
, int buf_size
, void *data
)
755 MpegAudioContext
*s
= avctx
->priv_data
;
756 short *samples
= data
;
757 short smr
[MPA_MAX_CHANNELS
][SBLIMIT
];
758 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
761 for(i
=0;i
<s
->nb_channels
;i
++) {
762 filter(s
, i
, samples
+ i
, s
->nb_channels
);
765 for(i
=0;i
<s
->nb_channels
;i
++) {
766 compute_scale_factors(s
->scale_code
[i
], s
->scale_factors
[i
],
767 s
->sb_samples
[i
], s
->sblimit
);
769 for(i
=0;i
<s
->nb_channels
;i
++) {
770 psycho_acoustic_model(s
, smr
[i
]);
772 compute_bit_allocation(s
, smr
, bit_alloc
, &padding
);
774 init_put_bits(&s
->pb
, frame
, MPA_MAX_CODED_FRAME_SIZE
);
776 encode_frame(s
, bit_alloc
, padding
);
778 s
->nb_samples
+= MPA_FRAME_SIZE
;
779 return pbBufPtr(&s
->pb
) - s
->pb
.buf
;
782 static int MPA_encode_close(AVCodecContext
*avctx
)
784 av_freep(&avctx
->coded_frame
);
788 AVCodec mp2_encoder
= {
792 sizeof(MpegAudioContext
),