c++ - ffmpeg sws_scale YUV420p 到 RGBA 未给出正确的缩放结果(c++)

标签 c++ video ffmpeg yuv rgba

我正在尝试通过 sws_scale 将解码的 YUV420p 帧(1018x700)缩放为 RGBA,我将数据保存到原始视频文件,然后使用 ffplay 播放原始视频以查看结果。

这是我的代码:

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);
 }

这是没有缩放的结果(即 YUV420p 格式) Here is the result without scaling (i.e. in YUV420p Format)

这是使用 ffplay 播放时缩放后的效果(即 RGBA 格式) enter image description here

我使用以下命令运行 ffplay(“video”是原始视频文件)

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

我应该修复什么才能使 RGB32 进行正确的缩放?

最佳答案

我找到了解决方案,这里的问题是我没有使用正确的缓冲区大小来写入文件。

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

变量 video_dst_file 是从返回值中获取的

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

解决方案是从 fwrite 语句中获取返回值并使用它:

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);

关于c++ - ffmpeg sws_scale YUV420p 到 RGBA 未给出正确的缩放结果(c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35147015/

相关文章:

C++开始和结束不工作

actionscript-3 - 将YouTube channel 嵌入Flash网站

ffmpeg - 如何在 sdl 版本 1.2.5 中隐藏 sdl 屏幕?

ffmpeg - 在背景上分层黑白视频,使黑色像素在 FFMPEG 中透明

video - 如何使用 Accord.FFMPEG.Video 将 H.264 流保存到 mp4 容器

c++ - 了解 bash 中程序使用的内存(在 ubuntu linux 中)

c++ - 使用 fstream 从文件中读取多个值?

c++ - 找出我的项目在 Visual Studio 中使用的所有 Boost .lib 文件

video - 使用ffmpeg从视频中提取最终帧

audio - FFmpeg:连接多个视频,一些有音频,一些没有