76
79
if (channels <= 0 || channels > 2){
77
80
av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
81
return AVERROR(EINVAL);
80
83
bitrate = bitrate / 1000;
81
84
s->nb_channels = channels;
82
85
avctx->frame_size = MPA_FRAME_SIZE;
86
avctx->delay = 512 - 32 + 1;
84
88
/* encoding freq */
725
732
flush_put_bits(p);
728
static int MPA_encode_frame(AVCodecContext *avctx,
729
unsigned char *frame, int buf_size, void *data)
735
static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
736
const AVFrame *frame, int *got_packet_ptr)
731
738
MpegAudioContext *s = avctx->priv_data;
732
const short *samples = data;
739
const int16_t *samples = (const int16_t *)frame->data[0];
733
740
short smr[MPA_MAX_CHANNELS][SBLIMIT];
734
741
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
737
744
for(i=0;i<s->nb_channels;i++) {
738
745
filter(s, i, samples + i, s->nb_channels);
748
755
compute_bit_allocation(s, smr, bit_alloc, &padding);
750
init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
757
if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE))) {
758
av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
762
init_put_bits(&s->pb, avpkt->data, avpkt->size);
752
764
encode_frame(s, bit_alloc, padding);
754
return put_bits_ptr(&s->pb) - s->pb.buf;
766
if (frame->pts != AV_NOPTS_VALUE)
767
avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
769
avpkt->size = put_bits_count(&s->pb) / 8;
757
774
static av_cold int MPA_encode_close(AVCodecContext *avctx)
776
#if FF_API_OLD_ENCODE_AUDIO
759
777
av_freep(&avctx->coded_frame);
768
787
AVCodec ff_mp2_encoder = {
770
.type = AVMEDIA_TYPE_AUDIO,
772
.priv_data_size = sizeof(MpegAudioContext),
773
.init = MPA_encode_init,
774
.encode = MPA_encode_frame,
775
.close = MPA_encode_close,
776
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
777
.supported_samplerates= (const int[]){44100, 48000, 32000, 22050, 24000, 16000, 0},
778
.long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
779
.defaults = mp2_defaults,
789
.type = AVMEDIA_TYPE_AUDIO,
790
.id = AV_CODEC_ID_MP2,
791
.priv_data_size = sizeof(MpegAudioContext),
792
.init = MPA_encode_init,
793
.encode2 = MPA_encode_frame,
794
.close = MPA_encode_close,
795
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
796
AV_SAMPLE_FMT_NONE },
797
.supported_samplerates = (const int[]){
798
44100, 48000, 32000, 22050, 24000, 16000, 0
800
.channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
803
.long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
804
.defaults = mp2_defaults,