[Libav-user] avcodec_encode_audio() usage

Matthew Einhorn moiein2000 at gmail.com
Wed Oct 12 17:16:49 CEST 2011


On Wed, Oct 12, 2011 at 2:05 AM, Marlon Reid <Marlon at scansoft.co.za> wrote:

> **
> Hi,
>
> I have a small application that takes a raw pcm stream, encodes it to mp3
> and streams it to something like VLC. I am however havinging issues with
> encoding my raw pcm stream. The problem is that my outbuf from
> avcodec_encode_audio() is always  uninitialized.  It is as if
> avcodec_encode_audio() never modifies outbuf.
>
> In any case, here is my function
>
>       *Code:*
>
> unsigned char* CEncoder::encode_audio_frame(unsigned char* in_pcm_buffer,
> int in_pcm_buffer_size, unsigned char * mp3_buffer, int *out_size,
> std::string encoding)
> {
>
> AVCodec *codec;
> AVCodecContext *c= NULL;
> int outbuf_size;
> uint8_t *outbuf;
>
> avcodec_init();
> avcodec_register_all();
>
> codec = avcodec_find_encoder(CODEC_ID_MP3);
> c = avcodec_alloc_context3(codec);
>
> /* put sample parameters */
> c->bit_rate = 64000;
> c->sample_rate = SAMPLE_RATE;
> c->channels = NUM_CHANNELS;
> c->sample_fmt = AV_SAMPLE_FMT_S16;
>
> avcodec_open(c, codec)
> frame_size = c->frame_size;
> outbuf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
>  uint8_t * outbuf = new uint8_t [ outbuf_size ] ;
>
> /* encode the samples */
> *out_size = avcodec_encode_audio(c, outbuf, outbuf_size,
> (short*)in_pcm_buffer);
>
> return outbuf;
> }
>
> I suspect that I am using the avcodec_encode_audio() incorrectly.  Any
> assistance will be great.
>
> Thanks
>
I don't know what the issue is, but the obvious question is since you don't
check if any of the functions (avcodec_find_encoder...) failed, are you sure
they didn't fail?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20111012/cc14dbd4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 807 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20111012/cc14dbd4/attachment.gif>


More information about the Libav-user mailing list