[Libav-user] Picture conversion help

env01 Hoang.Nguyen at envisioninc.com
Wed Aug 24 23:01:49 CEST 2011

I have is a series of streamed PNG images that I must convert over to YUV so
that I can later make an mp4 movie out of.  The original PNG images are fine
as I can view them with an image viewer.  My converted YUV images however
are all garbage.  I have spent days googling and pouring over forums but
could not figure out what I am missing.  Could someone please help?


    int width = 2560;
    int height = 34;
    int totalStrips = 374;
    static struct SwsContext *swsCtx = NULL;

    for(int i=0; i<=totalStrips; ++i) {
        VideoFrame &frame = vid->Next();

        // load the image into an avpicture
        AVPicture inPic;
        PixelFormat inPixFormat = PIX_FMT_BGR24;
        int res = avpicture_alloc(&inPic, inPixFormat, width, height);
        if(res < 0) {
            cout << "ERROR: Unable to alloc picture" << endl;

        res = avpicture_fill(&inPic,
                             (uint8_t*) frame.GetImageData(),
        if(res < 0) {
            cout << "ERROR: Unable to fill picture" << endl;

        // convert the image
        AVPicture outPic;
        PixelFormat outPixFormat = PIX_FMT_YUV420P;
        res = avpicture_alloc(&outPic, outPixFormat, width, height);
        if(res < 0) {
            cout << "ERROR: Unable to alloc picture" << endl;


        if(swsCtx == NULL) {
            swsCtx = sws_getContext(width, height, inPixFormat,
                                    width, height, outPixFormat,
                                    SWS_BICUBIC, NULL, NULL, NULL);

        res = sws_scale(swsCtx, inPic.data, inPic.linesize,
                        0, height, outPic.data, outPic.linesize);
        if(res <= 0) {
            cout << "ERROR: sws_scale() failed at index " << i << endl;

        if(i < 5) {
            // save a couple of images for debugging
            // prposes to see if works or not
            unsigned char buffer[500000];
            res = avpicture_layout(&outPic, outPixFormat, width, height,
                                   buffer, 500000);

            char filename [100];
            sprintf(filename, "ConvertedImage%d.yuv", i);

            ofstream outfile(filename, ios::out | ios::binary);
            outfile.write( (const char*)buffer, res);



View this message in context: http://libav-users.943685.n4.nabble.com/Picture-conversion-help-tp3766636p3766636.html
Sent from the libav-users mailing list archive at Nabble.com.

More information about the Libav-user mailing list