title: sws_scale YUV --> RGB distorted image
body: <p>I want to convert <code>YUV420P</code> image (received from <code>H.264</code> stream) to <code>RGB</code>, while also resizing it, using <code>sws_scale</code>.<br>
  The size of the original image is <code>480 × 800</code>. Just converting with same dimensions works fine.<br>
  But when I try to change the dimensions, I get a distorted image, with the following pattern:</p>
  
  <ul>
  <li>changing to <code>481 × 800</code> will yield a distorted B&amp;W image which looks like it's cut in the middle</li>
  <li><code>482 × 800</code> will be even more distorted</li>
  <li><code>483 × 800</code> is distorted but in color</li>
  <li><code>484 × 800</code> is ok (scaled correctly).</li>
  </ul>
  
  <p>Now this pattern follows - scaling will only work fine if the difference between divides by 4.</p>
  
  <p>Here's a sample code of the way that I decode and convert the image. All methods show "success". </p>
  
  <pre><code>int srcX = 480;
  int srcY = 800;
  int dstX = 481; // or 482, 483 etc
  int dstY = 800;
  
  AVFrame* avFrameYUV = avcodec_alloc_frame();
  avpicture_fill((AVPicture *)avFrameYUV, decoded_yuv_frame, PIX_FMT_YUV420P, srcX , srcY);
  
  AVFrame *avFrameRGB = avcodec_alloc_frame();
  
  AVPacket avPacket;
  av_init_packet(&amp;avPacket);
  avPacket.size = read; // size of raw data
  avPacket.data = raw_data; // raw data before decoding to YUV
  
  int frame_decoded = 0;
  int decoded_length = avcodec_decode_video2(g_avCodecContext, avFrameYUV, &amp;frame_decoded, &amp;avPacket);
  int size = dstX * dstY * 3;
  
  struct SwsContext *img_convert_ctx = sws_getContext(srcX, srcY, SOURCE_FORMAT, dstX, dstY, PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
  
  avpicture_fill((AVPicture *)avFrameRGB, rgb_frame, PIX_FMT_RGB24, dstX, dstY);
  sws_scale(img_convert_ctx, avFrameYUV-&gt;data, avFrameYUV-&gt;linesize, 0, srcY, avFrameRGB-&gt;data, avFrameRGB-&gt;linesize);
  
  // draws the resulting frame with windows BitBlt
  DrawBitmap(hdc, dstX, dstY, rgb_frame, size);
  
  sws_freeContext(img_convert_ctx); 
  </code></pre>