title: ffmpeg RTSP error while decoding MB
body: <p>I'm using ffmpeg to read an h264 RTSP stream from a Cisco 3050 IP camera and reencode it to disk as h264 (there are reasons why I'm not just using <code>-codec:copy</code>).</p>
  
  <p>The ffmpeg version is as follows:</p>
  
  <pre><code>ffmpeg version 3.2.6 Copyright (c) 2000-2017 the FFmpeg developers
    built with gcc 6.3.0 (Alpine 6.3.0)
  </code></pre>
  
  <p>I've also tried with ffmpeg 2.8.14-0ubuntu0.16.04.1 and the latest ffmpeg built from source (I used <a href="https://github.com/FFmpeg/FFmpeg/commit/d06b01fc2d4f5e031d45f9460d1eea610d23d6c5" rel="noreferrer">this</a> commit) and see the same behaviour as below.</p>
  
  <p>The command I'm running is:</p>
  
  <pre><code>ffmpeg -rtsp_transport udp -i 'rtsp://&lt;user&gt;:&lt;pw&gt;@&lt;ip&gt;:554/StreamingSetting?version=1.0&amp;action=getRTSPStream&amp;ChannelID=1&amp;ChannelName=Channel1' -r 10 -c:v h264 -crf 23 -x264-params keyint=60:min-keyint=60 -an -f ssegment -segment_time 60 -strftime 1 /output/%Y%m%d_%H%M%S.ts -abort_on empty_output
  </code></pre>
  
  <p>I get a variety of errors at a fairly steady rate of at least one per second. Here's a sample:</p>
  
  <pre><code>[rtsp @ 0x7f268c5e9220] max delay reached. need to consume packet
  [rtsp @ 0x7f268c5e9220] RTP: missed 40 packets
  [h264 @ 0x55b1e115d400] left block unavailable for requested intra mode
  [h264 @ 0x55b1e115d400] error while decoding MB 0 12, bytestream 114567
  [h264 @ 0x55b1e115d400] concealing 3889 DC, 3889 AC, 3889 MV errors in I frame
  </code></pre>
  
  <p>The most common one is 'error while decoding MB x x, bytestream x'. This corresponds to severe corruption in the video file when played back.</p>
  
  <p>I see many references to that error message on stackoverflow and elsewhere, but I've yet to find a satisfying explanation or workaround. It comes from <a href="https://github.com/FFmpeg/FFmpeg/blob/release/3.2/libavcodec/h264_slice.c#L2475" rel="noreferrer">this line</a> which appears to correspond to missing data at the end of the stream. 'left block unavailable' comes from <a href="https://github.com/FFmpeg/FFmpeg/blob/release/3.2/libavcodec/h264_parse.c#L159" rel="noreferrer">here</a> and also looks like missing data.</p>
  
  <p>Others have suggested using <code>-rtsp_transport tcp</code> instead (<a href="https://stackoverflow.com/a/39619783/1688738">1</a>, <a href="http://answers.opencv.org/question/34012/ip-camera-h264-error-while-decoding" rel="noreferrer">2</a>, <a href="http://answers.opencv.org/question/120402/decoding-a-h264-high-stream-with-opencvs-ffmpeg-on-ubuntu" rel="noreferrer">3</a>) which in my case just gives a slightly different mix of errors, and still video corruption:</p>
  
  <pre><code>[h264 @ 0x557923191b00] left block unavailable for requested intra4x4 mode -1
  [h264 @ 0x557923191b00] error while decoding MB 0 28, bytestream 31068
  [h264 @ 0x557923191b00] concealing 2609 DC, 2609 AC, 2609 MV errors in I frame
  [rtsp @ 0x7f88e817b220] CSeq 5 expected, 0 received.
  </code></pre>
  
  <p>Using Wireshark I confirmed that in both UDP and TCP mode, all of the packets are making it from the camera to the PC (sequential RTP sequence numbers without any missing) which makes me think the data is being lost after it arrives at ffmpeg.</p>
  
  <p>I also see similar behaviour when running the same command against a Panasonic WV-SFV110 camera, but with less frequent errors overall. Switching from UDP to TCP on the Panasonic camera reduces but does not completely eliminate the errors/corruption.</p>
  
  <p>I also tried a similar command with VLC and got similar errors (<code>cvlc rtsp://&lt;user&gt;:&lt;pw&gt;@&lt;ip&gt;/MediaInput/h264 :sout='#transcode{vcodec=h264}:std{access=file, mux=ts, dst="output.ts"}</code>) -- presumably the code hasn't diverged much since libav forked from ffmpeg.</p>
  
  <p>The camera is plugged directly into a PoE port on the PC so network congestion can't be a problem. Given that the PC has enough CPU to keep up encoding the live stream, it seems to me a problem with ffmpeg that it still drops data from the TCP stream.</p>
  
  <p>Qualitatively, there are several factors which seem to make the problem worse:</p>
  
  <ul>
  <li>Higher video resolution</li>
  <li>Higher system load on the machine running ffmpeg (e.g. transcoding to a low res .avi file produces fewer errors than transcoding to h264 VBR; using <code>-codec:copy</code> eliminates all errors except a couple while ffmpeg is starting up)</li>
  <li>Greater motion within the camera view</li>
  </ul>
  
  <p>What the does the error mean? And what can I do about it?</p>