[FFmpeg-trac] #5838(undetermined:closed): avcodec_open2 doesn't open raw video codec unles parameters in context are set

FFmpeg trac at avcodec.org
Sun Sep 25 12:01:41 EEST 2016

#5838: avcodec_open2 doesn't open raw video codec unles parameters in context are
             Reporter:  veroorzaker  |                    Owner:
                 Type:  enhancement  |                   Status:  closed
             Priority:  normal       |                Component:
              Version:  git-master   |  undetermined
             Keywords:               |               Resolution:
             Blocking:               |  needs_more_info
Analyzed by developer:  0            |               Blocked By:
                                     |  Reproduced by developer:  0

Comment (by veroorzaker):

 Apologies for the late reply, was away for a while.
 Code (basically taken from avcodec_open2 documentation, but with

 #include <libavformat/avformat.h>

 int main(int argc, char **argv)
   AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_RAWVIDEO);
   if (!codec) {
       return 1;
   AVCodecContext *c = avcodec_alloc_context3(codec);
   if (!c) {
       return 1;
   if (avcodec_open2(c, codec, NULL) < 0) {
       fprintf(stderr, "Could not open codec\n");
       return 1;
   return 0;

 This prints "Could not open codec" because the raw video codec requires
 width/height/... information to be set. Which makes sense, but it took me
 a while to figure that out because it is in my opinion not documented

 The only mention which comes close I found so far is in the
 decoding_encoding.c example where it says "For some codecs, such as
 msmpeg4 and mpeg4, width and height MUST be initialized there because this
 information is not available in the bitstream.." but there's no indication
 of how to do that exactly. The proper way to init everything for decoding
 seems to be to first open a file and find a stream, then use
 avcodec_find_decoder/avcodec_alloc_context3 and then use
 avcodec_parameters_to_context to copy the stream's codecpar info into the
 context before calling avcodec_open2. But that's just what I deduced from
 trying out different things to get raw video to decode, and I'm not even
 sure if my conclusion is correct.

 So my point is: calling avcodec_alloc_context3 followed by avcodec_open2
 immediately (as shown in multiple places in documentation and code
 samples) fails for some codecs unless certain parameters are already set
 in the context, but I could not find anything documenting that clearly.
 Yet it seems to be crucial in writing code which works for opening
 arbitrary video files.

 Just mentioning this in the avcodec_open2 documentation would and updating
 samples accordingly would be great. I wouldn't mind contributing this, but
 then I'd need to now for sure if my assumption about
 avcodec_parameters_to_context being required before calling avcodec_open2
 is correct.

Ticket URL: <https://trac.ffmpeg.org/ticket/5838#comment:3>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker

More information about the FFmpeg-trac mailing list