Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg sws_scale got distorted image from YUV420P to RGB24

Tags:

c++

ffmpeg

I got distorted image when try to convert YUV420p to RGB24 using sws_scale.

Code:

ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
if (ret < 0) {
    fprintf(stderr, "Error decoding video frame\n");
    return ret;
}
if (*got_frame) 
{
    printf("video_frame%s n:%d coded_n:%d pts:%s\n",
               cached ? "(cached)" : "",
               video_frame_count++, frame->coded_picture_number,
               "#"/*av_ts2timestr(frame->pts, &video_dec_ctx->time_base)*/);
    /* copy decoded frame to destination buffer:
     * this is required since rawvideo expects non aligned data */
    av_image_copy(video_dst_data, video_dst_linesize,
                  (const uint8_t **)(frame->data), frame->linesize,
                  video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height);
    /* write to rawvideo file */
    fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);

    AVPicture pic;
    avpicture_alloc( &pic, AV_PIX_FMT_RGB24, frame->width, frame->height);
    SwsContext *ctxt = sws_getContext(frame->width, frame->height, static_cast<AVPixelFormat>(frame->format),
        frame->width, frame->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
    if ( NULL == ctxt )
    {
       //Log("failed to get sws context");
    }

    if ( 0 < sws_scale(ctxt, frame->data, frame->linesize, 0, frame->height, pic.data, pic.linesize))
    {
        char szPic[256] = { 0 };
        sprintf( szPic, "decoded/%d.bmp", video_frame_count );
        FILE *pf = fopen(szPic,"w");
        if ( NULL != pf )
        {
            BITMAPFILEHEADER bmpFileHeader = {0};
            bmpFileHeader.bfReserved1 = 0;
            bmpFileHeader.bfReserved2 = 0;
            bmpFileHeader.bfType = 0x4D42;
            bmpFileHeader.bfSize = sizeof(bmpFileHeader) + sizeof(BITMAPINFOHEADER) + pic.linesize[0] * frame->height;
            bmpFileHeader.bfOffBits = sizeof(bmpFileHeader) + sizeof(BITMAPINFOHEADER);
            BITMAPINFOHEADER bmiHeader = { 0 };
            bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            bmiHeader.biWidth = frame->width;
            bmiHeader.biHeight = 0 - frame->height;
            bmiHeader.biPlanes = 1;
            bmiHeader.biBitCount = 24;
            bmiHeader.biCompression = BI_RGB;
            bmiHeader.biSizeImage = pic.linesize[0] * frame->height;
            bmiHeader.biXPelsPerMeter = 0;
            bmiHeader.biYPelsPerMeter = 0;
            bmiHeader.biClrUsed = 0;
            bmiHeader.biClrImportant = 0;
            fwrite( &bmpFileHeader, 1, sizeof(bmpFileHeader), pf );
            fwrite( &bmiHeader, 1, sizeof(bmiHeader), pf );
            fwrite( pic.data[0], 1, pic.linesize[0] * frame->height, pf );
            fclose( pf );
        }
    }
    // pic.data[0] now contains the image data in RGB format (3 bytes)
    // and pic.linesize[0] is the pitch of the data (ie. size of a row in memory, which can be larger than width*sizeof(pixel))


    avpicture_free(&pic);
    sws_freeContext(ctxt);
}

above only decode frame then convert this from to RGB24, then write a bitmap. original video frame like this, original video but converted image, converted image is there missing some code or some code is wrong?

thanks in advance.

like image 568
Leo Hwang Avatar asked Nov 12 '22 17:11

Leo Hwang


1 Answers

fwrite( pic.data[0], 1, pic.linesize[0] * frame->height, pf );

For an image of e.g. 1280x720, linesize is typically larger, e.g. 1312, so you'll be writing more data than image size if you write linesize*height. You want to write (in a loop) width pixels offset by linesize bytes:

uint8_t *ptr = pic.data[0];
for (int y = 0; y < frame->height; y++) {
    fwrite(ptr, 1, frame->width, pf);
    ptr += pic.linesize[0];
}

And then it should work correctly.

like image 118
Ronald S. Bultje Avatar answered Nov 15 '22 12:11

Ronald S. Bultje