X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fmpegaudioenc.c;h=51a6f5be5a1d0b350e74a6bc2dfe4ef5718cc13d;hb=b0de1c766329dd8c9960ad1722e2f653160abc1b;hp=5caa83aaca2121c07e3a5d35dea5842dcf49700d;hpb=bad5537e2c2caeb5deb1ff9d771ea01058b8010c;p=ffmpeg diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c index 5caa83aaca2..51a6f5be5a1 100644 --- a/libavcodec/mpegaudioenc.c +++ b/libavcodec/mpegaudioenc.c @@ -2,34 +2,41 @@ * The simplest mpeg audio layer 2 encoder * Copyright (c) 2000, 2001 Fabrice Bellard * - * This file is part of FFmpeg. + * This file is part of Libav. * - * FFmpeg is free software; you can redistribute it and/or + * Libav is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * FFmpeg is distributed in the hope that it will be useful, + * Libav is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software + * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** - * @file libavcodec/mpegaudio.c + * @file * The simplest mpeg audio layer 2 encoder. */ +#include "libavutil/channel_layout.h" + #include "avcodec.h" -#include "bitstream.h" +#include "internal.h" +#include "put_bits.h" + +#define FRAC_BITS 15 /* fractional bits for sb_samples and dct */ +#define WFRAC_BITS 14 /* fractional bits for window */ -#undef CONFIG_MPEGAUDIO_HP -#define CONFIG_MPEGAUDIO_HP 0 #include "mpegaudio.h" +#include "mpegaudiodsp.h" +#include "mpegaudiodata.h" +#include "mpegaudiotab.h" /* currently, cannot change these constants (need to modify quantization stage) */ @@ -40,12 +47,10 @@ typedef struct MpegAudioContext { PutBitContext pb; int nb_channels; - int freq, bit_rate; int lsf; /* 1 if mpeg2 low bitrate selected */ int bitrate_index; /* bit rate */ int freq_index; int frame_size; /* frame size, in bits, without padding */ - int64_t nb_samples; /* total number of samples encoded */ /* padding computation */ int frame_frac, frame_frac_incr, do_padding; short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */ @@ -56,14 +61,13 @@ typedef struct MpegAudioContext { 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; @@ -75,38 +79,37 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) if (channels <= 0 || channels > 2){ av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels); - return -1; + return AVERROR(EINVAL); } bitrate = bitrate / 1000; s->nb_channels = channels; - s->freq = freq; - s->bit_rate = bitrate * 1000; avctx->frame_size = MPA_FRAME_SIZE; + avctx->delay = 512 - 32 + 1; /* encoding freq */ s->lsf = 0; for(i=0;i<3;i++) { - if (ff_mpa_freq_tab[i] == freq) + if (avpriv_mpa_freq_tab[i] == freq) break; - if ((ff_mpa_freq_tab[i] / 2) == freq) { + if ((avpriv_mpa_freq_tab[i] / 2) == freq) { s->lsf = 1; break; } } if (i == 3){ av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq); - return -1; + return AVERROR(EINVAL); } s->freq_index = i; /* encoding bitrate & frequency */ for(i=0;i<15;i++) { - if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate) + if (avpriv_mpa_bitrate_tab[s->lsf][1][i] == bitrate) break; } if (i == 15){ av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate); - return -1; + return AVERROR(EINVAL); } s->bitrate_index = i; @@ -126,10 +129,8 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) s->sblimit = ff_mpa_sblimit_table[table]; s->alloc_table = ff_mpa_alloc_tables[table]; -#ifdef DEBUG - av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", - bitrate, freq, s->frame_size, table, s->frame_frac_incr); -#endif + av_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", + bitrate, freq, s->frame_size, table, s->frame_frac_incr); for(i=0;inb_channels;i++) s->samples_offset[i] = 0; @@ -140,25 +141,19 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) #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; @@ -172,7 +167,7 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) v = 3; else v = 4; - scale_diff_table[i] = v; + s->scale_diff_table[i] = v; } for(i=0;i<17;i++) { @@ -181,12 +176,9 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) v = -v; else v = v * 3; - total_quant_bits[i] = 12 * v; + s->total_quant_bits[i] = 12 * v; } - avctx->coded_frame= avcodec_alloc_frame(); - avctx->coded_frame->key_frame= 1; - return 0; } @@ -312,7 +304,7 @@ static void idct32(int *out, int *tab) #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS) -static void filter(MpegAudioContext *s, int ch, short *samples, int incr) +static void filter(MpegAudioContext *s, int ch, const short *samples, int incr) { short *p, *q; int sum, offset, i, j; @@ -320,8 +312,6 @@ static void filter(MpegAudioContext *s, int ch, short *samples, int incr) int tmp1[32]; int *out; - // print_pow1(samples, 1152); - offset = s->samples_offset[ch]; out = &s->sb_samples[ch][0][0][0]; for(j=0;j<36;j++) { @@ -333,7 +323,7 @@ static void filter(MpegAudioContext *s, int ch, short *samples, int incr) /* 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]; @@ -365,11 +355,10 @@ static void filter(MpegAudioContext *s, int ch, short *samples, int incr) } } s->samples_offset[ch] = offset; - - // print_pow(s->sb_samples, 1152); } -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) @@ -396,7 +385,7 @@ static void compute_scale_factors(unsigned char scale_code[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 */ @@ -405,10 +394,8 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT], index = 62; /* value 63 is not allowed */ } -#if 0 - printf("%2d:%d in=%x %x %d\n", - j, i, vmax, scale_factor_table[index], index); -#endif + av_dlog(NULL, "%2d:%d in=%x %x %d\n", + j, i, vmax, s->scale_factor_table[index], index); /* store the scale factor */ assert(index >=0 && index <= 63); sf[i] = index; @@ -416,8 +403,8 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT], /* 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) { @@ -476,10 +463,8 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT], code = 0; /* kill warning */ } -#if 0 - printf("%d: %2d %2d %2d %d %d -> %d\n", j, - sf[0], sf[1], sf[2], d1, d2, code); -#endif + av_dlog(NULL, "%d: %2d %2d %2d %d %d -> %d\n", j, + sf[0], sf[1], sf[2], d1, d2, code); scale_code[j] = code; sf += 3; } @@ -553,13 +538,11 @@ static void compute_bit_allocation(MpegAudioContext *s, } } } -#if 0 - printf("current=%d max=%d max_sb=%d alloc=%d\n", - current_frame_size, max_frame_size, max_sb, - bit_alloc[max_sb]); -#endif if (max_sb < 0) break; + av_dlog(NULL, "current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n", + current_frame_size, max_frame_size, max_sb, max_ch, + bit_alloc[max_ch][max_sb]); /* find alloc table entry (XXX: not optimal, should use pointer table) */ @@ -571,12 +554,12 @@ static void compute_bit_allocation(MpegAudioContext *s, 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) { @@ -597,13 +580,6 @@ static void compute_bit_allocation(MpegAudioContext *s, } *padding = max_frame_size - current_frame_size; assert(*padding >= 0); - -#if 0 - for(i=0;isblimit;i++) { - printf("%d ", bit_alloc[i]); - } - printf("\n"); -#endif } /* @@ -692,30 +668,11 @@ static void encode_frame(MpegAudioContext *s, 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); @@ -725,15 +682,7 @@ static void encode_frame(MpegAudioContext *s, /* group the 3 values to save bits */ put_bits(p, -bits, q[0] + steps * (q[1] + steps * q[2])); -#if 0 - printf("%d: gr1 %d\n", - i, q[0] + steps * (q[1] + steps * q[2])); -#endif } else { -#if 0 - printf("%d: gr3 %d %d %d\n", - i, q[0], q[1], q[2]); -#endif put_bits(p, bits, q[0]); put_bits(p, bits, q[1]); put_bits(p, bits, q[2]); @@ -754,21 +703,21 @@ static void encode_frame(MpegAudioContext *s, flush_put_bits(p); } -static int MPA_encode_frame(AVCodecContext *avctx, - unsigned char *frame, int buf_size, void *data) +static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, + const AVFrame *frame, int *got_packet_ptr) { MpegAudioContext *s = avctx->priv_data; - short *samples = data; + const int16_t *samples = (const int16_t *)frame->data[0]; short smr[MPA_MAX_CHANNELS][SBLIMIT]; unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; - int padding, i; + int padding, i, ret; for(i=0;inb_channels;i++) { filter(s, i, samples + i, s->nb_channels); } for(i=0;inb_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;inb_channels;i++) { @@ -776,31 +725,43 @@ static int MPA_encode_frame(AVCodecContext *avctx, } compute_bit_allocation(s, smr, bit_alloc, &padding); - init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE); + if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE))) { + av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n"); + return ret; + } + + init_put_bits(&s->pb, avpkt->data, avpkt->size); encode_frame(s, bit_alloc, padding); - s->nb_samples += MPA_FRAME_SIZE; - return pbBufPtr(&s->pb) - s->pb.buf; -} + if (frame->pts != AV_NOPTS_VALUE) + avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay); -static av_cold int MPA_encode_close(AVCodecContext *avctx) -{ - av_freep(&avctx->coded_frame); + avpkt->size = put_bits_count(&s->pb) / 8; + *got_packet_ptr = 1; return 0; } -AVCodec mp2_encoder = { - "mp2", - CODEC_TYPE_AUDIO, - CODEC_ID_MP2, - sizeof(MpegAudioContext), - MPA_encode_init, - MPA_encode_frame, - MPA_encode_close, - NULL, - .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, - .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"), +static const AVCodecDefault mp2_defaults[] = { + { "b", "384000" }, + { NULL }, }; -#undef FIX +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, + .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, + AV_SAMPLE_FMT_NONE }, + .supported_samplerates = (const int[]){ + 44100, 48000, 32000, 22050, 24000, 16000, 0 + }, + .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO, + AV_CH_LAYOUT_STEREO, + 0 }, + .defaults = mp2_defaults, +};