* The simplest mpeg audio layer 2 encoder.
*/
-#include "libavutil/audioconvert.h"
+#include "libavutil/channel_layout.h"
#include "avcodec.h"
#include "internal.h"
#define WFRAC_BITS 14 /* fractional bits for window */
#include "mpegaudio.h"
+#include "mpegaudiodsp.h"
+#include "mpegaudiodata.h"
+#include "mpegaudiotab.h"
/* currently, cannot change these constants (need to modify
quantization stage) */
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
int sblimit; /* number of used subbands */
const unsigned char *alloc_table;
+ int16_t filter_bank[512];
+ int scale_factor_table[64];
+ unsigned char scale_diff_table[128];
+ float scale_factor_inv_table[64];
+ unsigned short total_quant_bits[17]; /* total number of bits per allocation group */
} MpegAudioContext;
-/* define it to use floats in quantization (I don't like floats !) */
-#define USE_FLOATS
-
-#include "mpegaudiodata.h"
-#include "mpegaudiotab.h"
-
static av_cold int MPA_encode_init(AVCodecContext *avctx)
{
MpegAudioContext *s = avctx->priv_data;
bitrate = bitrate / 1000;
s->nb_channels = channels;
avctx->frame_size = MPA_FRAME_SIZE;
- avctx->delay = 512 - 32 + 1;
+ avctx->initial_padding = 512 - 32 + 1;
/* encoding freq */
s->lsf = 0;
#if WFRAC_BITS != 16
v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
#endif
- filter_bank[i] = v;
+ s->filter_bank[i] = v;
if ((i & 63) != 0)
v = -v;
if (i != 0)
- filter_bank[512 - i] = v;
+ s->filter_bank[512 - i] = v;
}
for(i=0;i<64;i++) {
v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
if (v <= 0)
v = 1;
- scale_factor_table[i] = v;
-#ifdef USE_FLOATS
- scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
-#else
-#define P 15
- scale_factor_shift[i] = 21 - P - (i / 3);
- scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
-#endif
+ s->scale_factor_table[i] = v;
+ s->scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
}
for(i=0;i<128;i++) {
v = i - 64;
v = 3;
else
v = 4;
- scale_diff_table[i] = v;
+ s->scale_diff_table[i] = v;
}
for(i=0;i<17;i++) {
v = -v;
else
v = v * 3;
- total_quant_bits[i] = 12 * v;
+ s->total_quant_bits[i] = 12 * v;
}
-#if FF_API_OLD_ENCODE_AUDIO
- avctx->coded_frame= avcodec_alloc_frame();
- if (!avctx->coded_frame)
- return AVERROR(ENOMEM);
-#endif
-
return 0;
}
/* filter */
p = s->samples_buf[ch] + offset;
- q = filter_bank;
+ q = s->filter_bank;
/* maxsum = 23169 */
for(i=0;i<64;i++) {
sum = p[0*64] * q[0*64];
s->samples_offset[ch] = offset;
}
-static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
+static void compute_scale_factors(MpegAudioContext *s,
+ unsigned char scale_code[SBLIMIT],
unsigned char scale_factors[SBLIMIT][3],
int sb_samples[3][12][SBLIMIT],
int sblimit)
use at most 2 compares to find the index */
index = (21 - n) * 3 - 3;
if (index >= 0) {
- while (vmax <= scale_factor_table[index+1])
+ while (vmax <= s->scale_factor_table[index+1])
index++;
} else {
index = 0; /* very unlikely case of overflow */
}
av_dlog(NULL, "%2d:%d in=%x %x %d\n",
- j, i, vmax, scale_factor_table[index], index);
+ j, i, vmax, s->scale_factor_table[index], index);
/* store the scale factor */
assert(index >=0 && index <= 63);
sf[i] = index;
/* compute the transmission factor : look if the scale factors
are close enough to each other */
- d1 = scale_diff_table[sf[0] - sf[1] + 64];
- d2 = scale_diff_table[sf[1] - sf[2] + 64];
+ d1 = s->scale_diff_table[sf[0] - sf[1] + 64];
+ d2 = s->scale_diff_table[sf[1] - sf[2] + 64];
/* handle the 25 cases */
switch(d1 * 5 + d2) {
if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
/* nothing was coded for this band: add the necessary bits */
incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
- incr += total_quant_bits[alloc[1]];
+ incr += s->total_quant_bits[alloc[1]];
} else {
/* increments bit allocation */
b = bit_alloc[max_ch][max_sb];
- incr = total_quant_bits[alloc[b + 1]] -
- total_quant_bits[alloc[b]];
+ incr = s->total_quant_bits[alloc[b + 1]] -
+ s->total_quant_bits[alloc[b]];
}
if (current_frame_size + incr <= max_frame_size) {
qindex = s->alloc_table[j+b];
steps = ff_mpa_quant_steps[qindex];
for(m=0;m<3;m++) {
+ float a;
sample = s->sb_samples[ch][k][l + m][i];
/* divide by scale factor */
-#ifdef USE_FLOATS
- {
- float a;
- a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
- q[m] = (int)((a + 1.0) * steps * 0.5);
- }
-#else
- {
- int q1, e, shift, mult;
- e = s->scale_factors[ch][i][k];
- shift = scale_factor_shift[e];
- mult = scale_factor_mult[e];
-
- /* normalize to P bits */
- if (shift < 0)
- q1 = sample << (-shift);
- else
- q1 = sample >> shift;
- q1 = (q1 * mult) >> P;
- q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
- }
-#endif
+ a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
+ q[m] = (int)((a + 1.0) * steps * 0.5);
if (q[m] >= steps)
q[m] = steps - 1;
assert(q[m] >= 0 && q[m] < steps);
}
for(i=0;i<s->nb_channels;i++) {
- compute_scale_factors(s->scale_code[i], s->scale_factors[i],
+ compute_scale_factors(s, s->scale_code[i], s->scale_factors[i],
s->sb_samples[i], s->sblimit);
}
for(i=0;i<s->nb_channels;i++) {
encode_frame(s, bit_alloc, padding);
if (frame->pts != AV_NOPTS_VALUE)
- avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
+ avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
avpkt->size = put_bits_count(&s->pb) / 8;
*got_packet_ptr = 1;
return 0;
}
-static av_cold int MPA_encode_close(AVCodecContext *avctx)
-{
-#if FF_API_OLD_ENCODE_AUDIO
- av_freep(&avctx->coded_frame);
-#endif
- return 0;
-}
-
static const AVCodecDefault mp2_defaults[] = {
- { "b", "128k" },
+ { "b", "384000" },
{ NULL },
};
AVCodec ff_mp2_encoder = {
.name = "mp2",
+ .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
.type = AVMEDIA_TYPE_AUDIO,
.id = AV_CODEC_ID_MP2,
.priv_data_size = sizeof(MpegAudioContext),
.init = MPA_encode_init,
.encode2 = MPA_encode_frame,
- .close = MPA_encode_close,
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
AV_SAMPLE_FMT_NONE },
.supported_samplerates = (const int[]){
.channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
AV_CH_LAYOUT_STEREO,
0 },
- .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
.defaults = mp2_defaults,
};