title: ffmpeg: RGB to YUV conversion loses color and scale
body: <p>I am trying to convert RGB frames to YUV420P format in ffmpeg/libav. Following is the code for conversion and also the images before and after conversion. The converted image loses all color information and also the scale changes significantly. Does anybody have idea how to handle this? I am completely new to ffmpeg/libav!</p>
  
  <pre><code>// Did we get a video frame?
     if(frameFinished)
     {
         i++;
         sws_scale(img_convert_ctx, (const uint8_t * const *)pFrame-&gt;data,
                   pFrame-&gt;linesize, 0, pCodecCtx-&gt;height,
                   pFrameRGB-&gt;data, pFrameRGB-&gt;linesize);                   
  
         //==============================================================
         AVFrame *pFrameYUV = avcodec_alloc_frame();
         // Determine required buffer size and allocate buffer
         int numBytes2 = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx-&gt;width,                                 
                                            pCodecCtx-&gt;height);
         uint8_t *buffer = (uint8_t *)av_malloc(numBytes2*sizeof(uint8_t));
  
         avpicture_fill((AVPicture *)pFrameYUV, buffer, PIX_FMT_RGB24,
                         pCodecCtx-&gt;width, pCodecCtx-&gt;height);
  
  
         rgb_to_yuv_ctx = sws_getContext(pCodecCtx-&gt;width, pCodecCtx-&gt;height,  
                                         PIX_FMT_RGB24,
                                         pCodecCtx-&gt;width,pCodecCtx-&gt;height, 
                                         PIX_FMT_RGB24,
                                         SWS_BICUBIC, NULL,NULL,NULL);
  
         sws_scale(rgb_to_yuv_ctx, pFrameRGB-&gt;data, pFrameRGB-&gt;linesize, 0, 
                   pCodecCtx-&gt;height, pFrameYUV-&gt;data, pFrameYUV-&gt;linesize);
  
         sws_freeContext(rgb_to_yuv_ctx);
  
         SaveFrame(pFrameYUV, pCodecCtx-&gt;width, pCodecCtx-&gt;height, i);
  
         av_free(buffer);
         av_free(pFrameYUV);
     }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/eoDTe.jpg" alt="original RGB24 frame"></p>
  
  <p><img src="https://i.stack.imgur.com/3DOfE.jpg" alt="frame after RGB24-&gt;YUV420P conversion"></p>