title: OpenCV CV::Mat image to AVFormat video writer
body: <pre><code>AVFrame *cvmatToAvframe(cv::Mat *cv_image, AVFrame *av_frame, StreamingContext *decoder) {
    int width = cv_image-&gt;cols;
    int height = cv_image-&gt;rows;
    int typeOFImage = cv_image-&gt;type();
    int cvLinesizes[1];
    cvLinesizes[0] = cv_image-&gt;step1();
  
   
  
    if (av_frame == NULL) {
      av_frame = av_frame_alloc();
    }
    av_image_alloc(av_frame-&gt;data, av_frame-&gt;linesize, width, height,
                     AVPixelFormat::AV_PIX_FMT_YUV420P, 1);
  
    SwsContext *conversion = sws_getContext(width, height, 
                                           AVPixelFormat::AV_PIX_FMT_BGR24, 
                                           width, height,
                                           AVPixelFormat::AV_PIX_FMT_YUV420P,  
                                           SWS_FAST_BILINEAR, 
                                           NULL, NULL, NULL);
    if (!conversion) {
          std::cerr &lt;&lt; &quot;fail to sws_getCachedContext&quot;;
          return NULL;
      }
  
    sws_scale(conversion, &amp;cv_image-&gt;data, cvLinesizes, 0, height, av_frame-&gt;data,
              av_frame-&gt;linesize);
  
    sws_freeContext(conversion);
    return av_frame;
  }
  </code></pre>
  <p>Using the above code to convert a video that is read using OpenCV.  Then saving it using FFMPEG.  The reason for using FFMPEG to write is OpenCV VideoWriter is very slow.  Need to make this work real time by reading frams from a camera and create a video file out of it.</p>
  <pre><code>int encode_video(StreamingContext *decoder, StreamingContext *encoder, AVFrame *input_frame) {
  
    if (input_frame) input_frame-&gt;pict_type = AV_PICTURE_TYPE_NONE;
  
    AVPacket *output_packet = av_packet_alloc();
    if (!output_packet) {std::cout &lt;&lt; &quot;could not allocate memory for output packet&quot; &lt;&lt; std::endl; return -1;}
  
      // need to confirm is the input frame is of type video
      //decoder-&gt;avfc-&gt;-&gt;codecpar-&gt;codec_type == AVMEDIA_TYPE_VIDEO
  
      int response = avcodec_send_frame(encoder-&gt;video_avCodecContext, input_frame);
      if( (response == AVERROR(EAGAIN) || response==AVERROR_EOF))
      {
          std::cout &lt;&lt; &quot;do nothing &quot; &lt;&lt; std::endl;
          av_packet_unref(output_packet);
          return -1;
      }
      else if (response &lt; 0) 
      {
          const char* error_string = av_err2str(response);
          std::cout &lt;&lt; &quot;Error while sending a packet to the decoder&quot; &lt;&lt; error_string &lt;&lt; std::endl;
          return response;
      }
      else
      {
          while (response &gt;= 0) {
          response = avcodec_receive_packet(encoder-&gt;video_avCodecContext, output_packet);
          if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
          break;
          } else if (response &lt; 0) {
          std::cout &lt;&lt; &quot;Error while receiving packet from encoder&quot; &lt;&lt; av_err2str(response) &lt;&lt; std::endl;
          return -1;
          }
  
          output_packet-&gt;stream_index = decoder-&gt;video_index;
          output_packet-&gt;duration = encoder-&gt;video_avstream-&gt;time_base.den / encoder-&gt;video_avstream-&gt;time_base.num / decoder-&gt;video_avstream-&gt;avg_frame_rate.num * decoder-&gt;video_avstream-&gt;avg_frame_rate.den;
  
          av_packet_rescale_ts(output_packet, decoder-&gt;video_avstream-&gt;time_base, encoder-&gt;video_avstream-&gt;time_base);
          response = av_interleaved_write_frame(encoder-&gt;avFormatContext, output_packet);
          if (response != 0) { std::cout &lt;&lt; &quot;Error while receiving packet from decoder&quot; &lt;&lt; response &lt;&lt; av_err2str(response) &lt;&lt; std::endl; return -1;}
      }
    
    }
    av_packet_unref(output_packet);
    av_packet_free(&amp;output_packet);
    return 0;
  }
  </code></pre>
  <p>The issue is @ avcodec_receive_packet() I get a an error response (return value -11)
  Cannot figure out the issue.</p>
  <p>I am reading the file stored locally on my drive.
  Reading the file using Opencv VideoReader.</p>
  <p>I extract the frames with cap.isOpened() loop.
  Next stpes are conversion to AVFrame fromat and
  Last step to write after encoding it.</p>
  <pre><code>  while(cvcap.isOpened())
    {
      cv::Mat frame;
      cvcap &gt;&gt; frame;
      input_frame = cvmatToAvframe(&amp;frame, input_frame , decoder);
  
      encode_video(decoder, encoder, input_frame );
    }
  </code></pre>