[Libav-user] How to Convert AV_PIX_FMT_RGB24 to AV_PIX_FMT_YUV420P

Hector Alonso halonso at vpod.tv
Thu Feb 14 10:38:56 CET 2013

Hi Chris,

You can implement some kind of function like:

// dependencies into C++

extern "C" {

#include <libavformat/avformat.h>

#include <libswscale/swscale.h>

#include <libavutil/pixdesc.h>

#include <libavutil/samplefmt.h>

#include <libavutil/intreadwrite.h>



  * This function converts an image (uncompressed buffer) from an input

  * specified pixel format to an output specified pixel format and buffer.

  * dependencies LibAV.

  * @param [in] eInFormat input image Pixel format @see LibaAV
PixelFormat enum type.

  * @param [in] iInWidth input image width

  * @param [in] iInHeight input image height

  * @param [in] ptInData input image buffer

  * @param [in] eOutFormat output image Pixel format @see LibaAV
PixelFormat enum type.

  * @param [in] iOutWidth output image width

  * @param [in] iOutHeight output image height

  * @param [out] src output image buffer

  * @return true if everything was correct, false otherwise)


bool ConvertImage1(PixelFormat eInFormat, int iInWidth, int iInHeight,
void* ptInData, PixelFormat eOutFormat, int iOutWidth, int iOutHeight,
AVPicture* src)


    SwsContext* ptImgConvertCtx;    // Frame conversion context

    AVPicture ptPictureIn;

    uint8_t * ptBufferIn;

    //Initialize convert context


    ptImgConvertCtx = sws_getContext(iInWidth, iInHeight, eInFormat,
  // (source format)

                                     iOutWidth, iOutHeight,
eOutFormat,  // (dest format)

                                     SWS_BICUBIC, NULL, NULL, NULL);

    // Init input frame:


    // Allocate an AVFrame structure

    // Determine required buffer size and allocate buffer

    // int iNumBytesIn=avpicture_get_size(eInFormat, iInWidth,iInHeight);


    // Assign appropriate parts of buffer to image planes in pFrameOut

    avpicture_fill(&ptPictureIn, ptBufferIn, eInFormat, iInWidth, iInHeight);

    // Do conversion:


    int iRes = sws_scale(ptImgConvertCtx,

                        ptPictureIn.data, //src






    //Free memory


    //Check result:

    if (iRes == iOutHeight)

        return true;

    return false;


And use it like this (in this example is used to downscale, but you
can use it for rescaling and or pixel format conversions)

// Downscale

        AVPicture src;

        int iWidth = m_ptCurrentVideoMode->getWidth() / DL_LOW_DEFINITION_DEN;

        int iHeight = (m_ptCurrentVideoMode->getHeight() *  iWidth) /

        iWidth = (floor(iWidth / 2)) * 2;

        iHeight = (floor(iHeight / 2)) * 2;

        avpicture_alloc(&src, AV_PIX_FMT_UYVY422, iWidth, iHeight);




                     (void *)ptBuffer,


                     iWidth, iHeight,






// ... whatever ...

// Free aux frame


I hope it helps!

On Thu, Feb 14, 2013 at 10:00 AM, Chris Share <cpsmusic at yahoo.com> wrote:

> Hi,
> I'm currently trying to implement file export for the open source
> animation program Pencil2D. This involves converting RGB (0 - 255) image
> data to a suitable movie format.
> The examples in the source tree have been very helpful however I still
> have some questions:
> The scaling_video.c is close to what I need however the conversion is the
> opposite of what I want. What I'm not clear about is how to change the
> "fill_yuv_image" function to something like "fill_rgb_image". How does the
> RGB data get written into the "uint8_t *data[4]"? Is it written
> consecutively (all R values get written to data[0], all G values to
> data[1], etc.)?
> Cheers,
> Chris
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130214/8f5279c6/attachment.html>

More information about the Libav-user mailing list