title: Decoding a h264 (High) stream with OpenCV's ffmpeg on Ubuntu
body: <p>I am working with a video stream from an ip camera on Ubuntu 14.04. Everything was going great with a camera that has these parameters (from FFMPEG):</p>
  
  <pre><code>    Stream #0:0: Video: h264 (Main), yuv420p(progressive), 352x192, 29.97 tbr, 90k tbn, 180k tbc
  </code></pre>
  
  <p>But then i changed to a newer camera, which has these parameters:</p>
  
  <pre><code>    Stream #0:0: Video: h264 (High), yuvj420p(pc, bt709, progressive), 1280x720, 25 fps, 25 tbr, 90k tbn, 50 tbc
  </code></pre>
  
  <p>My C++ program uses OpenCV3 to process the stream. By default OpenCV uses ffmpeg to decode and display the stream with function VideoCapture. </p>
  
  <pre><code>VideoCapture vc;
  vc.open(input_stream);
  while ((vc &gt;&gt; frame), !frame.empty()) {
     *do work*
  }
  </code></pre>
  
  <p>With the new camera stream i get errors like these (from ffmpeg):</p>
  
  <pre><code>[h264 @ 0x7c6980] cabac decode of qscale diff failed at 41 38
  [h264 @ 0x7c6980] error while decoding MB 41 38, bytestream (3572)
  [h264 @ 0x7c6980] left block unavailable for requested intra mode at 0 44
  [h264 @ 0x7bc2c0] SEI type 25 truncated at 208
  </code></pre>
  
  <p>The image sometimes is glitched, sometimes completely frozen. However <strong>on vlc it plays perfectly</strong>. I installed the newest version (3.2.2) of ffmpeg player with</p>
  
  <pre><code>./configure --enable-gpl --enable-libx264
  </code></pre>
  
  <p>Now playing directly with ffplay (instead of launching from source code with OpenCV function VideoCapture), the stream plays better, but sometimes still displays warnings:</p>
  
  <pre><code>[NULL @ 0x7f834c008c00] SEI type 25 size 896 truncated at 320=1/1   
  [h264 @ 0x7f834c0d5d20] SEI type 25 size 896 truncated at 319=1/1   
  [rtsp @ 0x7f834c0008c0] max delay reached. need to consume packet   
  [rtsp @ 0x7f834c0008c0] RTP: missed 1 packets
  [h264 @ 0x7f834c094740] concealing 675 DC, 675 AC, 675 MV errors in P frame
  </code></pre>
  
  <p>Changing the camera hardware is not an option. The camera can be set to encode to h265 or mjpeg. When encoding to mjpeg it can output 5 fps, which is not enough. Decoding to a static video is not an option either, because i need to display real time results about the stream. <a href="http://docs.opencv.org/trunk/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d" rel="nofollow noreferrer">Here</a> is a list of API backends that can be used in function VideoCapture. Maybe i should switch to some other decoder and player?
  From my research i conclude that i have these options:</p>
  
  <ul>
  <li>Somehow get OpenCV to use libVlc instead of ffmpeg</li>
  </ul>
  
  <p>One example of switching to vlc is <a href="http://study.marearts.com/2015/09/opencv-rtsp-connection-using-vlc-library.html" rel="nofollow noreferrer">here</a>, but i don't understand it well enough to say if that is what i need. Or maybe i should be <a href="https://stackoverflow.com/questions/30345495/ffmpeg-h264-parsing">parsing</a> the stream in code?</p>
  
  <ul>
  <li>Use vlc to preprocess the stream, as suggested <a href="https://stackoverflow.com/questions/23529620/opencv-and-network-cameras-or-how-to-spy-on-the-neighbors">here</a>.</li>
  </ul>
  
  <p>This is probably slow, which again is bad for real time results.<br>
  Any suggestions and coments will be appreciated.</p>