[libav-api] Segfault using avcodec_decode_audio3 in multithreaded app

Ibrahim Sha'ath ibrahimshaath at gmail.com
Sun Feb 12 18:19:26 CET 2012


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];
  while(avpkt->size > 0){
    int16_t *samples = (int16_t*)outputBuffer;
    int outputBufferSize = sizeof(outputBuffer);
    int bytesConsumed = avcodec_decode_audio3(cCtx, samples,
&outputBufferSize, avpkt);
    if(bytesConsumed <= 0){
      avpkt->size = 0;
      return 1;
    }
    int newSamplesDecoded = outputBufferSize / sizeof(int16_t);
    int oldSampleCount = ab->getSampleCount();
    try{
      ab->addToSampleCount(newSamplesDecoded);
    }catch(Exception& e){
      throw e;
    }
    for(int i = 0; i < newSamplesDecoded; i++)
      ab->setSample(oldSampleCount+i, (float)samples[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;
  }
  return 0;
}

I don't know if it's directly relevant, but when the WMA is decoded, the
app produces many errors of the form:
[wmav2 @ 0x106829e20] prev_block_len_bits N out of range
(where N is in the range 4..6). These appear regardless of platform or
whether the segfault occurs.

My libav build (of version 0.7.4) was configured as follows:
./configure \
--enable-shared --disable-static \
--enable-gpl --enable-version3 \
--disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver \
--disable-swscale --disable-postproc --disable-avfilter --disable-doc \
--disable-network --disable-protocols --enable-protocol=file \
--enable-memalign-hack --enable-runtime-cpudetect

and on Windows I also add the following arguments:
--disable-pthreads --enable-w32threads

A stack trace from the segfault is pasted below.

Level Function File Line
0 get_bits get_bits.h 285 0x6624c092
1 wma_decode_block wmadec.c 453 0x6624c092
2 wma_decode_frame wmadec.c 776 0x6624df89
3 wma_decode_superframe wmadec.c 874 0x6624df89
4 avcodec_decode_audio3 utils.c 748 0x661ec720
5 ?? 0 0x401446
6 ?? 0 0x401a16
7 ?? 0 0x450b5a
8 ?? 0 0x4619be
9 ?? 0 0x461a9f
10 ?? 0 0x46045d
11 ZN12QtConcurrent16ThreadEngineBase3runEv
C:\QtSDK\Desktop\Qt\4.7.4\mingw\lib\QtCore4.dll 0 0x6a1ccab8
12 ZN11QThreadPoolC2EP7QObject
C:\QtSDK\Desktop\Qt\4.7.4\mingw\lib\QtCore4.dll 0 0x6a1ce6eb
13 ZN7QThread11setPriorityENS_8PriorityE
C:\QtSDK\Desktop\Qt\4.7.4\mingw\lib\QtCore4.dll 0 0x6a1d81ce
14 msvcrt!_itow_s C:\Windows\system32\msvcrt.dll 0 0x76411287
15 msvcrt!_endthreadex C:\Windows\system32\msvcrt.dll 0 0x76411328
16 KERNEL32!AcquireSRWLockExclusive C:\Windows\system32\kernel32.dll 0
0x7683ed6c
17 ntdll!RtlInsertElementGenericTableAvl C:\Windows\system32\ntdll.dll 0
0x770b377b
18 ntdll!RtlInsertElementGenericTableAvl C:\Windows\system32\ntdll.dll 0
0x770b374e
19 ?? 0
... <More>

I would appreciate any help anyone can offer with this; I've spent a long
time trawling docs and posts online and tried countless iterations of this
code, but none have worked without flaws of one kind or another.

Regards
Ibrahim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libav.org/pipermail/libav-api/attachments/20120212/a5232ac8/attachment.html>


More information about the libav-api mailing list