title: Reading RTSP stream with FFMpeg library - how to use avcodec_open2?
body: <p>While trying to read rtsp stream I get some problems, with code and documentation alike. Short description: whatever I do, <code>avcodec_open2</code> either fails (saying "codec type or id mismatches") or <code>width</code> and <code>height</code> of codec context after the call are 0 (thus making further code useless). Stream itself can be opened normally by VLC player and <code>av_dump_format()</code> displays correct info. My code is based on <strong>technique</strong> answer to <a href="https://stackoverflow.com/questions/10715170/receiving-rtsp-stream-using-ffmpeg-library">this question</a>.</p>
  
  <p>Long description: my code is in C#, but here is C++-equivalent of FFMpeg calls (I actually reduced my code to this minimum and problem persists):</p>
  
  <pre><code>av_register_all();
  avformat_network_init(); //return code ignored
  
  AVFormatContext* formatContext = avformat_alloc_context();
  if (avformat_open_input(&amp;formatContext, stream_path, null, null) != 0) {
      return;
  }
  
  if (avformat_find_stream_info(formatContext, null) &lt; 0) {
      return;
  }
  
  int videoStreamIndex = 0;
  for (int i = 0; i &lt; formatContext-&gt;nb_streams; ++i) {
      AVStream* s = formatContext-&gt;streams[i];
      if (s-&gt;codec == null) continue;
      AVCodecContext c = *(s-&gt;codec);
      if (c.codec_type == AVMEDIA_TYPE_VIDEO) videoStreamIndex = i;
  }
  
  //start reading packets from stream and write them to file
  //av_read_play(formatContext); //return code ignored
  //this call would print "method PLAY failed: 455 Method Not Valid in This State"
  //seems to be the case that for rtsp stream it isn't needed
  
  AVCodec* codec = null;
  codec = avcodec_find_decoder(AV_CODEC_ID_H264);
  if (codec == null) {
      return;
  }
  
  AVCodecContext* codecContext = avcodec_alloc_context3(null);
  avcodec_get_context_defaults3(codecContext, codec);//return code ignored
  avcodec_copy_context(codecContext, formatContext-&gt;streams[videoStreamIndex]-&gt;codec); //return code ignored
  
  av_dump_format(formatContext, videoStreamIndex, stream_path, 0);
  
  if (avcodec_open2(codecContext, codec, null) &lt; 0) {
      return;
  }
  </code></pre>
  
  <p>The code actually uses DLL version of FFMpeg library; avcodec-55.dll and avformat-55.dll are used.</p>
  
  <p>Documentation <a href="https://www.ffmpeg.org/doxygen/trunk/group__lavc__core.html#ga3933b21b9dcb9173df3e56673b7a7d76" rel="nofollow noreferrer">says</a> something weird about which calls can be made in which succession (that <code>copy_context</code> should be called before <code>get_context_defaults</code>), current code is left close as possible to <strong>technique</strong> version. As written, it results in non-zero return from <code>avcodec_open2</code> with "codec type or id mismatches" message. Changing the order does little good: now <code>avcodec_open2</code> executes successfully, but both <code>codecContext-&gt;width</code> and <code>codecContext-&gt;height</code> are 0 afterwards.</p>
  
  <p>Also documentation doesn't mention which is default value for the third argument of <code>avcodec_open2</code> should be, but source code seems to taking into account that <code>options</code> can be NULL.</p>
  
  <p>Output of <code>av_dump_format</code> is as follows:</p>
  
  <pre><code>Input #0, rtsp, from 'rtsp://xx.xx.xx.xx:xx/video.pro1':
    Metadata:
      title           : QStream
      comment         : QStreaming Media
    Duration: N/A, start: 0.000000, bitrate: 64 kb/s
      Stream #0:0: Video: h264 (Baseline), yuvj420p(pc), 1920x1080, 30 fps, 25 tbr, 90k tbn, 60 tbc
      Stream #0:1: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s
  </code></pre>