libav/ffmpeg: avcodec_decode_video2() returns -1 when separating demultiplexing and decoding
- by unbekannt
I'm using libav (from a C++ program on Linux and Windows) to decode video streams from a file, which works fine (decoding various formats like H264 and MPEG2) using avformat_open_input(), av_read_frame() and avcodec_decode_video2().
Now I have to separate demultiplexing and decoding. One class will call avformat_open_input() and av_read_frame() and then pass the AVPackets into a queue that is read by another class. There I use avcodec_alloc_context3() to get the AVCodecContext needed for avcodec_decode_video2(). I've tested that with a MPEG2 video stream and it works.
Problems arise if I try to decode a H264 stream: avcodec_decode_video2() always returns -1 and outputs "no frame". I understand that additional data (SPS/PPS) is needed to decode this stream, so I've tried to replicate the original AVCodecContext from the demultiplexer in the decoder, but it won't work:
Copying the content of the extradata field and setting all other values that differ from the default ones in the decoder: -1 is returned
Using the same context (i.e. passing along the pointer) results in a crash
I also tried to set CODEC_FLAG2_CHUNKS. avcodec_decode_video2() then always returns packet.size - 3 (??) and frameFinished is never set to 1.
In my opinion I have a general problem here that will arise whenever settings from the original CodecContext are needed to decode the AVPackets. I'd be grateful for any hints on how to solve that problem!