[Libav-user] sws scale superimposing converted images

env01 Hoang.Nguyen at envisioninc.com
Thu Aug 25 02:08:58 CEST 2011


Hi,
I have a series of streamed PNG images that I would like to convert to YUV
so that I can later make an mp4 movie out of.  My code is below.  The
problem I’m running into is that each subsequent conversion is superimposed
into all the previous ones (i.e. 1st image is good, 2nd image is superimpose
with the 1st, 3rd image is superimpose with the 2nd + 3rd, etc.)


void Test2(Video *vid)
{
    static struct SwsContext *swsCtx = NULL;

    /* find the video encoder */
    AVCodec *codec = avcodec_find_decoder(CODEC_ID_PNG);
    if (!codec) {
        cout << "ERROR: codec not found" << endl;
        exit(1);
    }

    AVCodecContext *cdcCtx = avcodec_alloc_context();
    if(!cdcCtx) {
        cout << "ERROR: codec context cannot be allocated" << endl;
        exit(1);
    }

    /* open the codec */
    if (avcodec_open(cdcCtx, codec) < 0) {
        cout << "ERROR: could not open codec" << endl;
        exit(1);
    }

    for(int i=0; i<5; ++i) {
        AVPacket pkt;
        av_init_packet(&pkt);

        VideoFrame &imageStrip = vid->Next();
        pkt.data = (uint8_t*) imageStrip.GetImageData();
        pkt.size = imageStrip.GetTotalImageSize();

        int gotPic = 0;
        AVFrame *inFrame = avcodec_alloc_frame();
        int len = avcodec_decode_video2(cdcCtx, inFrame, &gotPic, &pkt);
        if(len < 0) {
            cout << "ERROR: Unable to decode strip " << i << endl;
            exit(1);
        }

        int width = inFrame->width;
        int height = inFrame->height;
        PixelFormat inPixFormat = (PixelFormat) inFrame->format;

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


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

        res = sws_scale(swsCtx,
                        inFrame->data, inFrame->linesize,
                        0, height,
                        outPic.data, outPic.linesize);

        if(res <= 0) {
            cout << "ERROR: sws_scale() failed at index " << i << endl;
        }
        else {
            // save to file for debugging purpose

            unsigned char buffer[500000];
            memset(buffer, 0, 500000);

            res = avpicture_layout(&outPic, outPixFormat, width, height,
                                   buffer, 500000);

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

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

            outfile.flush();
            outfile.close();
        }

        av_free(inFrame);
        avpicture_free(&outPic);
    }
}


--
View this message in context: http://libav-users.943685.n4.nabble.com/sws-scale-superimposing-converted-images-tp3767011p3767011.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list