38 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
40 #define SAMPLES_BUF_SIZE 4096
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;
127 av_dlog(avctx,
"%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
147 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
152 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
196 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
240 x1 =
MUL((t[8] - x2), xp[0]);
241 x2 =
MUL((t[8] + x2), xp[1]);
254 xr =
MUL(t[28],xp[0]);
258 xr =
MUL(t[4],xp[1]);
259 t[ 4] = (t[24] - xr);
260 t[24] = (t[24] + xr);
262 xr =
MUL(t[20],xp[2]);
266 xr =
MUL(t[12],xp[3]);
267 t[12] = (t[16] - xr);
268 t[16] = (t[16] + xr);
273 for (i = 0; i < 4; i++) {
274 xr =
MUL(tab[30-i*4],xp[0]);
275 tab[30-i*4] = (tab[i*4] - xr);
276 tab[ i*4] = (tab[i*4] + xr);
278 xr =
MUL(tab[ 2+i*4],xp[1]);
279 tab[ 2+i*4] = (tab[28-i*4] - xr);
280 tab[28-i*4] = (tab[28-i*4] + xr);
282 xr =
MUL(tab[31-i*4],xp[0]);
283 tab[31-i*4] = (tab[1+i*4] - xr);
284 tab[ 1+i*4] = (tab[1+i*4] + xr);
286 xr =
MUL(tab[ 3+i*4],xp[1]);
287 tab[ 3+i*4] = (tab[29-i*4] - xr);
288 tab[29-i*4] = (tab[29-i*4] + xr);
296 xr =
MUL(t1[0], *xp);
309 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
314 int sum, offset, i, j;
324 s->
samples_buf[ch][offset + (31 - i)] = samples[0];
333 sum = p[0*64] * q[0*64];
334 sum += p[1*64] * q[1*64];
335 sum += p[2*64] * q[2*64];
336 sum += p[3*64] * q[3*64];
337 sum += p[4*64] * q[4*64];
338 sum += p[5*64] * q[5*64];
339 sum += p[6*64] * q[6*64];
340 sum += p[7*64] * q[7*64];
345 tmp1[0] = tmp[16] >>
WSHIFT;
346 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >>
WSHIFT;
347 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >>
WSHIFT;
365 unsigned char scale_factors[SBLIMIT][3],
366 int sb_samples[3][12][SBLIMIT],
369 int *p, vmax,
v, n, i, j, k, code;
371 unsigned char *sf = &scale_factors[0][0];
373 for(j=0;j<sblimit;j++) {
376 p = &sb_samples[i][0][j];
389 index = (21 - n) * 3 - 3;
403 assert(index >=0 && index <= 63);
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];
470 sf[0], sf[1], sf[2], d1, d2, code);
471 scale_code[j] = code;
489 #define SB_NOTALLOCATED 0
490 #define SB_ALLOCATED 1
501 int i, ch,
b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
505 const unsigned char *alloc;
507 memcpy(smr, smr1, s->
nb_channels *
sizeof(
short) * SBLIMIT);
523 current_frame_size = 32;
537 if (smr[ch][i] > max_smr && subband_status[ch][i] !=
SB_NOMORE) {
538 max_smr = smr[ch][i];
546 av_dlog(
NULL,
"current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
547 current_frame_size, max_frame_size, max_sb, max_ch,
553 for(i=0;i<max_sb;i++) {
554 alloc += 1 << alloc[0];
568 if (current_frame_size + incr <= max_frame_size) {
571 current_frame_size += incr;
573 smr[max_ch][max_sb] = smr1[max_ch][max_sb] -
quant_snr[alloc[
b]];
575 if (b == ((1 << alloc[0]) - 1))
576 subband_status[max_ch][max_sb] =
SB_NOMORE;
581 subband_status[max_ch][max_sb] =
SB_NOMORE;
584 *padding = max_frame_size - current_frame_size;
585 assert(*padding >= 0);
596 int i, j, k, l, bit_alloc_bits,
b, ch;
624 j += 1 << bit_alloc_bits;
669 int qindex, steps, m, sample,
bits;
679 a = (float)sample * scale_factor_inv_table[s->
scale_factors[ch][i][k]];
680 q[m] = (
int)((a + 1.0) * steps * 0.5);
684 int q1, e, shift,
mult;
691 q1 = sample << (-shift);
693 q1 = sample >> shift;
694 q1 = (q1 *
mult) >> P;
695 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
700 assert(q[m] >= 0 && q[m] < steps);
706 q[0] + steps * (q[1] + steps * q[2]));
715 j += 1 << bit_alloc_bits;
721 for(i=0;i<padding;i++)
729 unsigned char *frame,
int buf_size,
void *
data)
777 .supported_samplerates= (
const int[]){44100, 48000, 32000, 22050, 24000, 16000, 0},
#define MPA_MAX_CODED_FRAME_SIZE
AVFrame * coded_frame
the picture in the bitstream
static const unsigned char nb_scale_factors[4]
const int ff_mpa_quant_bits[17]
static int MPA_encode_frame(AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data)
static int scale_factor_table[64]
static void compute_scale_factors(unsigned char scale_code[SBLIMIT], unsigned char scale_factors[SBLIMIT][3], int sb_samples[3][12][SBLIMIT], int sblimit)
mpeg audio layer common tables.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int32_t scale_factor_mult[15][3]
static const int costab32[30]
static unsigned char scale_diff_table[128]
const int ff_mpa_quant_steps[17]
static void idct32(int *out, int *tab)
const uint16_t avpriv_mpa_freq_tab[3]
const unsigned char *const ff_mpa_alloc_tables[5]
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]
mpeg audio layer 2 tables.
static av_cold int MPA_encode_init(AVCodecContext *avctx)
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]
static int bit_alloc(AC3EncodeContext *s, int snr_offset)
Run the bit allocation with a given SNR offset.
static const unsigned short quant_snr[17]
static int init(AVCodecParserContext *s)
static int16_t filter_bank[512]
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static const int bitinv32[32]
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
const unsigned char * alloc_table
int bit_rate
the average bitrate
const int32_t ff_mpa_enwindow[257]
static av_cold int MPA_encode_close(AVCodecContext *avctx)
#define av_dlog(pctx,...)
av_dlog macros Useful to print debug messages that shouldn't get compiled in normally.
int frame_size
Samples per packet, initialized when calling 'init'.
static void encode_frame(MpegAudioContext *s, unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int padding)
int samples_offset[MPA_MAX_CHANNELS]
int sample_rate
samples per second
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
static const float fixed_smr[SBLIMIT]
main external API structure.
static void close(AVCodecParserContext *s)
struct MpegAudioContext MpegAudioContext
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int16_t mult(Float11 *f1, Float11 *f2)
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]
static const AVCodecDefault mp2_defaults[]
static int8_t scale_factor_shift[64]
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
AVSampleFormat
all in native-endian format
mpeg audio declarations for both encoder and decoder.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
const int ff_mpa_sblimit_table[5]
int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
int channels
number of audio channels
int key_frame
1 -> keyframe, 0-> not
static const struct @46 tab
TwinVQ codebooks.
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
static void compute_bit_allocation(MpegAudioContext *s, short smr1[MPA_MAX_CHANNELS][SBLIMIT], unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int *padding)
static unsigned short total_quant_bits[17]
int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]