title: fffmpeg gets the frame from the rtsp
body: <p>I'm using IP CAMERA and
  I'm trying to use ffmpeg <a href="https://github.com/chelyaev/ffmpeg-tutorial" rel="nofollow">tutorial</a> in Android</p>
  
  <p>and I get the frame and send to bitmap
  and Works well</p>
  
  <p>but when it gets frame from the rtsp
  somethings wrong
  frame is corrupted</p>
  
  <p>I found <a href="https://github.com/chelyaev/ffmpeg-tutorial" rel="nofollow">this</a> and <a href="https://github.com/chelyaev/ffmpeg-tutorial" rel="nofollow">this</a></p>
  
  <p>but it does not work</p>
  
  <p>does anyone has solution?</p>
  
  <pre><code>[h264 @ 0xaacfe20] concealing 977 DC, 977 AC, 977 MV errors
  [h264 @ 0xaacfe20] negative number of zero coeffs at 11 9
  [h264 @ 0xaacfe20] error while decoding MB 11 9
  </code></pre>
  
  <p>when I run tutorial in the pc I found that</p>
  
  <p>this is my code</p>
  
  <pre><code>AVFormatContext *gFormatCtx = NULL;
  
  AVCodecContext *gVideoCodecCtx = NULL;
  AVCodec *gVideoCodec = NULL;
  int gVideoStreamIdx = -1;
  
  AVFrame *gFrame = NULL;
  AVFrame *gFrameRGB = NULL;
  
  struct SwsContext *gImgConvertCtx = NULL;
  
  int gPictureSize = 0;
  uint8_t *gVideoBuffer = NULL;
  
  
  int openMovie(const char filePath[])
  {
  int i;
  
  if (gFormatCtx != NULL)
      return -1;
  /*
  if (av_open_input_file(&amp;gFormatCtx, filePath, NULL, 0, NULL) != 0)
      return -2;
  */
  gFormatCtx = avformat_alloc_context();
  
  if (avformat_open_input(&amp;gFormatCtx, filePath,NULL,NULL) != 0)
      return -2;
  
  if (av_find_stream_info(gFormatCtx) &lt; 0)
      return -3;
  
  for (i = 0; i &lt; gFormatCtx-&gt;nb_streams; i++) {
      if (gFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type == AVMEDIA_TYPE_VIDEO) {
          gVideoStreamIdx = i;
  
          break;
      }
  }
  if (gVideoStreamIdx == -1)
      return -4;
  
  gVideoCodecCtx = gFormatCtx-&gt;streams[gVideoStreamIdx]-&gt;codec;
  
  gVideoCodec = avcodec_find_decoder(gVideoCodecCtx-&gt;codec_id);
  if (gVideoCodec == NULL)
      return -5;
  
  if (avcodec_open(gVideoCodecCtx, gVideoCodec) &lt; 0)
      return -6;
  
  gFrame = avcodec_alloc_frame();
  if (gFrame == NULL)
      return -7;
  
  gFrameRGB = avcodec_alloc_frame();
  if (gFrameRGB == NULL)
      return -8;
  
  gPictureSize = avpicture_get_size(PIX_FMT_RGB565LE, gVideoCodecCtx-&gt;width, gVideoCodecCtx-&gt;height);
  gVideoBuffer = (uint8_t*)(malloc(sizeof(uint8_t) * gPictureSize));
  
  avpicture_fill((AVPicture*)gFrameRGB, gVideoBuffer, PIX_FMT_RGB565LE, gVideoCodecCtx-&gt;width, gVideoCodecCtx-&gt;height);
  
  return 0;
  }
  
  int decodeFrame()
  {
  int frameFinished = 0;
  AVPacket packet;
  
  gVideoCodecCtx-&gt;time_base= (AVRational){1, 10};
  
  while (av_read_frame(gFormatCtx, &amp;packet) &gt;= 0) {
      if (packet.stream_index == gVideoStreamIdx) {
          avcodec_decode_video2(gVideoCodecCtx, gFrame, &amp;frameFinished, &amp;packet);
  
          if (frameFinished) {
              gImgConvertCtx = sws_getCachedContext(gImgConvertCtx,
                  gVideoCodecCtx-&gt;width, gVideoCodecCtx-&gt;height, gVideoCodecCtx-&gt;pix_fmt,
                  gVideoCodecCtx-&gt;width, gVideoCodecCtx-&gt;height, PIX_FMT_RGB565LE, SWS_BICUBIC, NULL, NULL, NULL);
  
              sws_scale(gImgConvertCtx, gFrame-&gt;data, gFrame-&gt;linesize, 0, gVideoCodecCtx-&gt;height, gFrameRGB-&gt;data, gFrameRGB-&gt;linesize);
  
              av_free_packet(&amp;packet);
  
              return 0;
          }
      }
  
      av_free_packet(&amp;packet);
  }
  
  return -1;
  }
  
  void copyPixels(uint8_t *pixels)
  {
  memcpy(pixels, gFrameRGB-&gt;data[0], gPictureSize);
  }
  
  int getWidth()
  {
  return gVideoCodecCtx-&gt;width;
  }
  
  int getHeight()
  {
  return gVideoCodecCtx-&gt;height;
  }
  
  void closeMovie()
  {
  if (gVideoBuffer != NULL) {
      free(gVideoBuffer);
      gVideoBuffer = NULL;
  }
  
  if (gFrame != NULL)
      av_freep(gFrame);
  if (gFrameRGB != NULL)
      av_freep(gFrameRGB);
  
  if (gVideoCodecCtx != NULL) {
      avcodec_close(gVideoCodecCtx);
      gVideoCodecCtx = NULL;
  }
  
  if (gFormatCtx != NULL) {
      av_close_input_file(gFormatCtx);
      gFormatCtx = NULL;
  }
  
  }
  </code></pre>