27 #include <lame/lame.h>
41 #define BUFFER_SIZE (7200 + 2 * MPA_FRAME_SIZE + MPA_FRAME_SIZE / 4+1000)
46 lame_global_flags *
gfp;
62 if (!
s->buffer ||
s->buffer_size -
s->buffer_index <
BUFFER_SIZE) {
65 ff_dlog(
s->avctx,
"resizing output buffer: %d -> %d\n",
s->buffer_size,
68 s->buffer_size =
s->buffer_index = 0;
71 s->buffer_size = new_size;
99 if (!(
s->gfp = lame_init()))
103 lame_set_num_channels(
s->gfp, avctx->
channels);
116 lame_set_VBR(
s->gfp, vbr_default);
121 lame_set_VBR(
s->gfp, vbr_abr);
122 lame_set_VBR_mean_bitrate_kbps(
s->gfp, avctx->
bit_rate / 1000);
124 lame_set_brate(
s->gfp, avctx->
bit_rate / 1000);
130 lame_set_lowpassfreq(
s->gfp, avctx->
cutoff);
133 lame_set_bWriteVbrTag(
s->gfp,0);
136 lame_set_disable_reservoir(
s->gfp, !
s->reservoir);
139 if (lame_init_params(
s->gfp) < 0) {
153 for (ch = 0; ch < avctx->
channels; ch++) {
155 sizeof(*
s->samples_flt[ch]));
156 if (!
s->samples_flt[ch]) {
180 #define ENCODE_BUFFER(func, buf_type, buf_name) do { \
181 lame_result = func(s->gfp, \
182 (const buf_type *)buf_name[0], \
183 (const buf_type *)buf_name[1], frame->nb_samples, \
184 s->buffer + s->buffer_index, \
185 s->buffer_size - s->buffer_index); \
193 int len, ret, ch, discard_padding;
210 for (ch = 0; ch < avctx->
channels; ch++) {
211 s->fdsp->vector_fmul_scalar(
s->samples_flt[ch],
221 }
else if (!
s->afq.frame_alloc) {
224 lame_result = lame_encode_flush(
s->gfp,
s->buffer +
s->buffer_index,
225 s->buffer_size -
s->buffer_index);
227 if (lame_result < 0) {
228 if (lame_result == -1) {
230 "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
231 s->buffer_index,
s->buffer_size -
s->buffer_index);
235 s->buffer_index += lame_result;
251 if (
s->buffer_index < 4)
263 len = hdr.frame_size;
266 if (len <= s->buffer_index) {
269 memcpy(avpkt->
data,
s->buffer,
len);
270 s->buffer_index -=
len;
271 memmove(
s->buffer,
s->buffer +
len,
s->buffer_index);
284 if ((!
s->delay_sent && avctx->
initial_padding > 0) || discard_padding > 0) {
292 if (!
s->delay_sent) {
296 AV_WL32(side_data + 4, discard_padding);
305 #define OFFSET(x) offsetof(LAMEContext, x)
306 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
327 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
331 .
name =
"libmp3lame",
350 .wrapper_name =
"libmp3lame",
static enum AVSampleFormat sample_fmts[]
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Libavcodec external API header.
#define FF_COMPRESSION_DEFAULT
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
static av_cold int init(AVCodecContext *avctx)
audio channel layout utility functions
common internal and external API header
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_FLTP
float, planar
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
#define LIBAVUTIL_VERSION_INT
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static av_cold int mp3lame_encode_init(AVCodecContext *avctx)
#define ENCODE_BUFFER(func, buf_type, buf_name)
static const int libmp3lame_sample_rates[]
static const AVOption options[]
static const AVCodecDefault libmp3lame_defaults[]
static av_cold int mp3lame_encode_close(AVCodecContext *avctx)
static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static int realloc_buffer(LAMEContext *s)
static const AVClass libmp3lame_class
AVCodec ff_libmp3lame_encoder
mpeg audio declarations for both encoder and decoder.
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
enum AVSampleFormat sample_fmt
audio sample format
int global_quality
Global quality for codecs which cannot change it per frame.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
int channels
number of audio channels
int cutoff
Audio cutoff bandwidth (0 means "automatic")
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
This structure stores compressed data.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define av_malloc_array(a, b)
static void error(const char *err)