[libav-api] Segfault using avcodec_decode_audio3 in multithreaded app

Ibrahim Sha'ath ibrahimshaath at gmail.com
Sun Feb 12 22:28:45 CET 2012


Thanks for the quick response Justin,

I was just going off hints from
here<http://comments.gmane.org/gmane.comp.video.ffmpeg.libav.user/5111>among
other places. But having swiftly swapped in some av_malloc (which I
believe is also guaranteed to be properly aligned?) the segfault is still
there. This code does appeal to me more as I (clearly!) never really
understood the macro, but can you suggest what might be wrong? What else am
I misunderstanding?

int LibAvDecoder::decodePacket(AVCodecContext* cCtx, AVPacket* avpkt,
AudioStream* ab){
  while(avpkt->size > 0){
    int outputBufferSize = ((AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2) *
sizeof(int16_t);
    int16_t* outputBuffer = (int16_t*)av_malloc(outputBufferSize);
    int bytesConsumed = avcodec_decode_audio3(cCtx, outputBuffer,
&outputBufferSize, avpkt);
    if(bytesConsumed <= 0){
      avpkt->size = 0;
      av_free(outputBuffer);
      return 1;
    }
    int newSamplesDecoded = outputBufferSize / sizeof(int16_t);
    int oldSampleCount = ab->getSampleCount();
    try{
      ab->addToSampleCount(newSamplesDecoded);
    }catch(Exception& e){
      av_free(outputBuffer);
      throw e;
    }
    for(int i = 0; i < newSamplesDecoded; i++)
      ab->setSample(oldSampleCount+i, (float)outputBuffer[i]);
    if(bytesConsumed < avpkt->size){
      size_t newLength = avpkt->size - bytesConsumed;
      uint8_t* datacopy = avpkt->data;
      avpkt->data = (uint8_t*)av_malloc(newLength);
      memcpy(avpkt->data, datacopy + bytesConsumed, newLength);
      av_free(datacopy);
    }
    avpkt->size -= bytesConsumed;
    av_free(outputBuffer);
  }
  return 0;
}

Regards
Ibrahim

On 12 February 2012 19:33, Justin Ruggles <justin.ruggles at gmail.com> wrote:

> On 02/12/2012 12:19 PM, Ibrahim Sha'ath wrote:
>
> > Hello all,
> >
> > I've been wrestling with a segfault that occurs during multithreaded
> > batch decoding jobs, and I hope someone can help. This issue seems only
> > to occur with Windows Media files, and I can't always reproduce it (but
> > I can get it to happen fairly consistently by decoding a batch of files
> > that includes some WMAs).
> >
> > My app is built in C++ using the Qt framework, and it targets Mac OS and
> > Windows, though the segfault seems to be limited to Windows.
> >
> > I assume that I must have made some mistake with the memory allocation
> > in my code, or possibly overlooked some implication of running the
> > decoding function across parallel threads. The process that handles
> > decoding is here
> > <https://github.com/ibsh/is_KeyFinder/blob/master/decoderlibav.cpp>; the
> > function of primary interest is pasted below:
> >
> > int LibAvDecoder::decodePacket(AVCodecContext* cCtx, AVPacket* avpkt,
> > AudioStream* ab){
> >   DECLARE_ALIGNED(16, uint8_t,
> > outputBuffer)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
>
>
> DECLARE_ALIGNED() doesn't work on the stack like that.
>
> -Justin
> _______________________________________________
> libav-api mailing list
> libav-api at libav.org
> https://lists.libav.org/mailman/listinfo/libav-api
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libav.org/pipermail/libav-api/attachments/20120212/ef999102/attachment-0001.html>


More information about the libav-api mailing list