[Ffmpeg-devel] Audio Decoding and Encoding Using FFmpeg: program crashing at avcodec_encode_audio

Måns Rullgård mru
Tue Mar 7 14:40:59 CET 2006

Renjith Nair said:
> Hi All,
> I have a problem while trying to encode audio using ffmpeg. Whenever the
>  function avcodec_encode_audio is called teh system crashes. When that line
> is commented out then the program works without any problem. Can anybody
> have a clue, what is wrong with this code. Thanks in Advance.
> P.S: I am developing an audio encoding/decoding api using ffmpeg and
> planning to publish the code and the documentation on the web for free. Very
> few ffmpeg documentations (for programmers) are currently available on the
> web. So i plan to create a programmer's documentation with sample programs
> to assist the further development of ffmpeg. All are welcome to co-operate.
> The code with the problem is the following:
> outBuf = (int16_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
> output = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

Don't cast the return value of malloc() (unless you are using C++ (which you

> while(av_read_frame(pFormatCtx, &packet)>=0)
> {
>      // Is this a packet from the audio stream?
>      if(packet.stream_index==audioStream)
>      {
>     // Decoding the frame
>     len = avcodec_decode_audio(pCodecCtx,outBuf, &outBufSize,packet.data,
> packet.size);
>     // Encoding the frame
>     outPacket.size = avcodec_encode_audio(pCodecCtx,output, outputSize,
> (short*)outBuf);//<-- This line causes the crash

You can't use the same AVCodecContext for both encoding and decoding.

M?ns Rullg?rd
mru at inprovide.com

More information about the ffmpeg-devel mailing list