How to convert cv::Mat to AVFrame in ffmpeg
<ul>
<li>I have one cv::Mat image.Now i want to convert this mat to AVFrame in ffmpeg.</li>
<li><p>I have trie below code,</p>

<pre><code>void matToAVFrame(cv::Mat frame)
{
    AVFrame *dst, *yuv422_final_frame;
      cv::Size frameSize = frame.size();
      AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
      AVFormatContext* outContainer = avformat_alloc_context();
      AVStream *outStream = avformat_new_stream(outContainer, encoder);
      avcodec_get_context_defaults3(outStream-&gt;codec, encoder);
      dst = av_frame_alloc();
      outStream-&gt;codec-&gt;pix_fmt = AV_PIX_FMT_YUV420P;
      outStream-&gt;codec-&gt;width = frame.cols;
      outStream-&gt;codec-&gt;height = frame.rows;
      avpicture_fill((AVPicture*)dst, frame.data, AV_PIX_FMT_BGR24, outStream-&gt;codec-&gt;width, outStream-&gt;codec-&gt;height);
      dst-&gt;width = frameSize.width;
      dst-&gt;height = frameSize.height;

//      av_image_alloc((uint8_t**)frame.data, dst-&gt;linesize,dst-&gt;width, dst-&gt;height,
//            AV_PIX_FMT_BGR24, 32);

      yuv422_final_frame = av_frame_alloc();
      int yuv422_num_bytes = avpicture_get_size( AV_PIX_FMT_YUV420P,  dst-&gt;width, dst-&gt;height );
      uint8_t* final_frame2_buffer = (uint8_t *)av_malloc(yuv422_num_bytes*sizeof(uint8_t));


      yuv422_final_frame-&gt;width = frameSize.width;
      yuv422_final_frame-&gt;height = frameSize.height;
      avpicture_fill((AVPicture*)yuv422_final_frame, (uchar*)dst-&gt;data, AV_PIX_FMT_YUV420P,  dst-&gt;width, dst-&gt;height);

     SwsContext *final_sws_ctx = sws_getContext(dst-&gt;width , dst-&gt;height,
         AV_PIX_FMT_BGR24, dst-&gt;width, dst-&gt;height,
         AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, 0, 0, 0);

//     avpicture_fill((AVPicture*)dst, frame.data, AV_PIX_FMT_RGB24, dst-&gt;width, dst-&gt;height);
     sws_scale(final_sws_ctx, dst-&gt;data,
             dst-&gt;linesize,
               0, dst-&gt;height,
               yuv422_final_frame-&gt;data,
               yuv422_final_frame-&gt;linesize);

     Mat cvmImage = cv::Mat( yuv422_final_frame-&gt;height, yuv422_final_frame-&gt;width, CV_8UC3, yuv422_final_frame-&gt;data[2],
             yuv422_final_frame-&gt;linesize[2]);

     imwrite("/home/radix/Desktop/OpenCV/cvmImage.png", cvmImage);
}
int main()
{
    av_register_all();
    Mat frame = imread("path to image");
    matToAVFrame(frame);
}
</code></pre></li>
</ul>

<p>but it is giving me some bad image, and in some cases it is not giving any image.
* So please anybody tell me where i am doing wrong.Thanks!!</p>

<ul>
<li><strong>EDIT:</strong></li>
<li><p>Sometimes it gives me error like <code>[swscaler @ 0x1c9a6e0] bad src image pointers</code></p></li>
<li><p>So i am getting this type of output from above code.</p></li>
<li><p><strong>Input Image:</strong></p></li>
</ul>

<p><a href="https://i.stack.imgur.com/j4cpq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j4cpq.png" alt="InputImage"></a></p>

<ul>
<li><strong>output image:</strong></li>
</ul>

<p><a href="https://i.stack.imgur.com/vO5Oz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vO5Oz.png" alt="outputImage"></a></p>