[Libav-user] Coversion from YUV to RGB loss of image details

Alex Cohn alexcohn at netvision.net.il
Tue Jun 5 10:20:46 CEST 2012


On Tue, Jun 5, 2012 at 5:16 AM, Sampath Subasinghe <susiriss at gmail.com>wrote:

> Hi All,
>          I am using libswscale to do some YUV - RGB conversion back and
> forth. There are 2 steps performed on an image.
>
> 1. RGB - YUV conversion
> 2. YUV - RGB conversion
>
> Issue is that in the image output from step 2, the right edge of the image
> is missing, means the pixels are black. The rest of the image seems ok
> compared to original image. This black edge is roughly 5-10 pixels wide and
> varies depending on the size of the image. This issue happens for images
> with certain dimensions. For some images this issue does not appear. So I
> guess this is due to libswscale issue or the way i'm using it is wrong.
> Also I get a libswscale warning on this conversion.
>
> [swscaler @ 0x1768020] Warning: data is not aligned! This can lead to a
> speedloss
>
> If I analyze the Y values along the right edge, I can see that they have
> valid values, so I deduce this is an issue arising from step 2 (YUV to
> RGB). My code (truncated for clarity) looks like follows for each step.
>
> Step 1. RGB- YUV conversion.-------------------**---------
>
> // Create YUV buffer
>    m_bufferSize = avpicture_get_size(PIX_FMT_**YUV420P, width, height);
>    avPictureInfo_ = avcodec_alloc_frame();
>    m_buffer = (uint8_t *)av_malloc(m_bufferSize);
>
>    avpicture_fill((AVPicture*)**avPictureInfo_, m_buffer, PIX_FMT_YUV420P,
>                   width, height);
>
> // Then fill it with  RGB data
> struct SwsContext *img_convert_ctx = sws_getContext(m_width, m_height,
> PIX_FMT_RGB24,
>                                                        m_width, m_height,
> PIX_FMT_YUV420P, SWS_BICUBIC,
>                                                        NULL, NULL, NULL);
>
>    if (img_convert_ctx == NULL) {
>        return NULL;
>    }
>
>        uint8_t * srcData[4] = {m_buffer, 0, 0, 0};
>    int srcLineSize[4] = {m_width * 3 * sizeof(uint8_t), 0, 0, 0};
>    sws_scale(img_convert_ctx, srcData, srcLineSize, 0,
>              m_height, yuvImage->avPictureInfo_->**data,
> yuvImage->avPictureInfo_->**linesize);
>
>    free(img_convert_ctx);
>
> Step 2: YUV - RGB conversion ------------------------------**
> --------------------
>
>    m_bufferSize = avpicture_get_size(PIX_FMT_**RGB24, width, height);
>    m_buffer = (uint8_t *)av_malloc(m_bufferSize);
>
> struct SwsContext *img_convert_ctx = sws_getContext(m_width, m_height,
> PIX_FMT_YUV420P,
>                                                        m_width, m_height,
> PIX_FMT_RGB24, SWS_BICUBIC,
>                                                        NULL, NULL, NULL);
>
>    if (img_convert_ctx == NULL) {
>        return NULL;
>    }
>
>
>    AVFrame * rgbPictInfo = avcodec_alloc_frame();
>    avpicture_fill((AVPicture*)**rgbPictInfo, rgbImage->m_buffer,
> PIX_FMT_RGB24,
>                   m_width, m_height);
>    sws_scale(img_convert_ctx, avPictureInfo_->data,
> avPictureInfo_->linesize, 0,
>              m_height, rgbPictInfo->data, rgbPictInfo->linesize);
>
> ------------------------------**-----------------
>
> I am using libav functions to allocate memory etc, so they should take
> care of alignment. So why does libswscale complain about not aligned data?
> Why are pixels missing along in the right edge of the image? I'm also
> attaching two images from step 1 and 2.
>
> Please help!
> -Sampath


Your example is 45 pixel wide.

It is a natural limitation of YUV_420P images that the dimensions should
divide 2, or even 4. You also have SWS_BICUBIC that requires more alignment
on the edges.

Sincerely,
Alex Cohn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20120605/7fdf2e42/attachment.html>


More information about the Libav-user mailing list