title: Write FFMPEG frame to png/jpeg file
body: <p>I have AV_PIX_FMT_YUV420P AVFrame, and I want to write it as PNG or JPEG file, it doesn't matter what format is inside (BGR, BGRA, RGB or RGBA). I tried to save it as AV_PIX_FMT_YUV420P, but I got 3 pictures in one, so I need to convert it to RGB24 first. I do it via:</p>
  
  <pre><code>        int destW = decCtx-&gt;width;
          int destH = decCtx-&gt;height;
  
          AVFrame * resFrame = av_frame_alloc();
          resFrame-&gt;linesize[0] = destW * 3;
          resFrame-&gt;data[0] = (uint8_t*)malloc(resFrame-&gt;linesize[0] * destH);
  
          SwsContext * ctx = sws_getContext(decCtx-&gt;width,
                                            decCtx-&gt;height,
                                            AV_PIX_FMT_YUV420P,
                                            decCtx-&gt;width,
                                            decCtx-&gt;height,
                                            AV_PIX_FMT_RGB24,
                                            SWS_FAST_BILINEAR, 0, 0, 0);
          sws_scale(ctx, frame-&gt;data, frame-&gt;linesize, 0,
                    decCtx-&gt;height, resFrame-&gt;data, resFrame-&gt;linesize);
          sws_freeContext(ctx);
          int64_t pts = frame-&gt;pts;
  
          resFrame-&gt;format = AV_PIX_FMT_RGB24;
          resFrame-&gt;pts = pts;
  </code></pre>
  
  <p>It looks like I have correct converted frame, but now encoder return me a packet with size 86, so my image is actually blank or near it. I do encoding via:</p>
  
  <pre><code>    encCodec = avcodec_find_encoder(AV_CODEC_ID_PNG);
  
      if (!encCodec) {
          return false;
      }
  
      encCtx = avcodec_alloc_context3(encCodec);
      if (!encCtx) {
          return false;
      }
  
      encCtx-&gt;bit_rate = decCtx-&gt;bit_rate;
      encCtx-&gt;codec_type = AVMEDIA_TYPE_VIDEO;
      encCtx-&gt;thread_count = 1;
      encCtx-&gt;width = decCtx-&gt;width;
      encCtx-&gt;height = decCtx-&gt;height;
  
      int fps = fmtc-&gt;streams[iVideoStream]-&gt;r_frame_rate.num / fmtc-&gt;streams[iVideoStream]-&gt;r_frame_rate.den;
      encCtx-&gt;time_base = (AVRational){1, fps};
      encCtx-&gt;framerate = (AVRational){fps, 1};
  
      encCtx-&gt;gop_size = decCtx-&gt;gop_size;
      encCtx-&gt;max_b_frames = decCtx-&gt;max_b_frames;
      encCtx-&gt;pix_fmt = AV_PIX_FMT_RGB24;
  
      int ret = avcodec_parameters_from_context(fmtc-&gt;streams[iVideoStream]-&gt;codecpar, encCtx);
      if (ret &lt; 0) {
          return false;
      }
  
      ret = avcodec_open2(encCtx, encCodec,  nullptr);
      if (ret &lt; 0) {
          return false;
      }
  
      ret = avcodec_send_frame(encCtx, source);
      if (ret &lt; 0) {
          return false;
      }
  
      av_packet_unref(pkt);
      av_init_packet(pkt);
  
      ret = avcodec_receive_packet(encCtx, pkt);
      if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
          return false;
      else if (ret &lt; 0) {
          return false;
      }
  
      if (ret &gt;= 0)
      {
          FILE * outPng = fopen("./sample.png", "wb");
          fwrite(pkt-&gt;data, pkt-&gt;size, 1, outPng);
          fclose(outPng);
      }
  </code></pre>
  
  <p>What I do wrong with converting image to RGB24 or with encoding it?</p>