简体   繁体   English

ffmpeg sws_scale YUV420p到RGBA没有给出正确的缩放结果(C ++)

[英]ffmpeg sws_scale YUV420p to RGBA not giving correct scaling result (c++)

I am trying to scale a decoded YUV420p frame(1018x700) via sws_scale to RGBA, I am saving data to a raw video file and then playing the raw video using ffplay to see the result. 我正在尝试通过sws_scale将已解码的YUV420p帧(1018x700)缩放为RGBA,我正在将数据保存到原始视频文件中,然后使用ffplay播放原始视频以查看结果。

Here is my code: 这是我的代码:

sws_ctx = sws_getContext(video_dec_ctx->width, video_dec_ctx->height,AV_PIX_FMT_YUV420P, video_dec_ctx->width, video_dec_ctx->height, AV_PIX_FMT_BGR32, SWS_LANCZOS | SWS_ACCURATE_RND, 0, 0, 0);
ret = avcodec_decode_video2(video_dec_ctx, yuvframe, got_frame, &pkt);
    if (ret < 0) {
        std::cout<<"Error in decoding"<<std::endl;
        return ret;
    }else{
        //the source and destination heights and widths are the same
        int sourceX = video_dec_ctx->width;
        int sourceY = video_dec_ctx->height;
        int destX = video_dec_ctx->width;
        int destY = video_dec_ctx->height;

        //declare destination frame
        AVFrame avFrameRGB;
        avFrameRGB.linesize[0] = destX * 4;
        avFrameRGB.data[0] = (uint8_t*)malloc(avFrameRGB.linesize[0] * destY);

        //scale the frame to avFrameRGB
        sws_scale(sws_ctx, yuvframe->data, yuvframe->linesize, 0, yuvframe->height, avFrameRGB.data, avFrameRGB.linesize);

        //write to file
        fwrite(avFrameRGB.data[0], 1, video_dst_bufsize, video_dst_file);
 }

Here is the result without scaling (ie in YUV420p Format) 这是不缩放的结果(即YUV420p格式) 这是不按比例缩放的结果(即YUV420p格式)

Here is the after scaling while playing using ffplay (ie in RGBA format) 这是使用ffplay(即RGBA格式)播放时的缩放后 在此处输入图片说明

I run the ffplay using the following command ('video' is the raw video file) 我使用以下命令运行ffplay(“视频”是原始视频文件)

ffplay -f rawvideo -pix_fmt bgr32 -video_size 1018x700 video

What should I fix to make the correct scaling happen to RGB32? 我该如何解决才能使RGB32正确缩放?

I found the solution, the problem here was that I was not using the correct buffer size to write to the file. 我找到了解决方案,这里的问题是我没有使用正确的缓冲区大小来写入文件。

fwrite(avFrameRGB.data[0], 1, video_dst_bufsize, video_dst_file);

The variable video_dst_file was being taken from the return value of 变量video_dst_file是从的返回值中获取的

video_dst_bufsize = av_image_alloc(yuvframe.data, yuvframe.linesize, destX, destY, AV_PIX_FMT_YUV420P, 1);

The solution is to get the return value from and use this in the fwrite statement: 解决方案是从fget语句中获取返回值并在其中使用它:

video_dst_bufsize_RGB = av_image_alloc(avFrameRGB.data, avFrameRGB.linesize, destX, destY, AV_PIX_FMT_BGR32, 1);

fwrite(avFrameRGB.data[0], 1, video_dst_bufsize_RGB, video_dst_file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM