title: ffmpeg AVFrame to opencv Mat conversion
body: <p>I am currently work on a project which decode the received frame using ffmepg, after decode, I want to convert the AVFrame to opencv Mat frame so that I can play it on the imShow function. </p>
  
  <p>What I have is the byte stream, I read it into buffer, decoded to AVFrame:</p>
  
  <pre><code>f = fopen(filename, "rb");
  if (!f) {
      fprintf(stderr, "Could not open %s\n", filename);
      exit(1);
  }
  
  frame = avcodec_alloc_frame();
  if (!frame) {
      fprintf(stderr, "Could not allocate video frame\n");
      exit(1);
  }
  
  framergb = avcodec_alloc_frame();
  if (!framergb) {
      fprintf(stderr, "Could not allocate video frame\n");
      exit(1);
  }
  
  bytes=avpicture_get_size(PIX_FMT_RGB24, CAMER_WIDTH, CAMER_HEIGHT);
  buffer=(uint8_t *)av_malloc(bytes*sizeof(uint8_t));
  avpicture_fill((AVPicture *)framergb, buffer, PIX_FMT_RGB24,
                  CAMER_WIDTH, CAMER_HEIGHT);
  
  frame_count = 0;
  for(;;) {
      avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
      if (avpkt.size == 0)
          break;
  
      avpkt.data = inbuf;
      while (avpkt.size &gt; 0)
          if (decode_write_frame(outfilename, c, frame, &amp;frame_count, &amp;avpkt, 0) &lt; 0)
              exit(1);
  }
  
  avpkt.data = NULL;
  avpkt.size = 0;
  decode_write_frame(outfilename, c, frame, &amp;frame_count, &amp;avpkt, 1);
  </code></pre>
  
  <p>and the <strong>decode_write_frame</strong> defined like this:</p>
  
  <pre><code>static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
  {
  int len, got_frame;
  char buf[1024];
  struct SwsContext *convert_ctx;
  
  len = avcodec_decode_video2(avctx, frame, &amp;got_frame, pkt);
  if (len &lt; 0) {
      fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
      return len;
  }
  if (got_frame) {
      printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
      fflush(stdout);
  
  int w = avctx-&gt;width;
  int h = avctx-&gt;height;
  convert_ctx = sws_getContext(w, h, avctx-&gt;pix_fmt,
                      w, h, PIX_FMT_RGB24, SWS_BICUBIC,
                      NULL, NULL, NULL);
  
  if(convert_ctx == NULL) {
      fprintf(stderr, "Cannot initialize the conversion context!\n");
      exit(1);
  }
  
  sws_scale(convert_ctx, frame-&gt;data,
              frame-&gt;linesize, 0,
              h,
              framergb-&gt;data, framergb-&gt;linesize);
  
      /* the picture is allocated by the decoder, no need to free it */
      snprintf(buf, sizeof(buf), outfilename, *frame_count);
  
      bmp_save(framergb-&gt;data[0], framergb-&gt;linesize[0],
               avctx-&gt;width, avctx-&gt;height, buf);
      (*frame_count)++;
  }
  if (pkt-&gt;data) {
      pkt-&gt;size -= len;
      pkt-&gt;data += len;
  }
  return 0;
  }
  </code></pre>
  
  <p>here the <strong>bmp_save()</strong> is defined by the original code author to realise AVFrame to bmp picture conversion. I want to modify here so that let the AVFrame convert to opencv Mat frame. How should I do this conversion?</p>
  
  <p>Thanks in advance.</p>