ffmpeg - 在 avcodec_decode_video2 之后转储 yuv 值的正确方法

标签 ffmpeg

我正在解码 H.265 原始数据:

 avcodec_decode_video2(decoderCtx, pictYUV, &gotPicture, &packet)

调用后,pictYUV->format 的值正如预期的那样,是 AV_PIX_FMT_YUV420P。

我将原始 yuv 值转储到文件中,如下所示:
fwrite(pictYUV->data[0], 1, w*h, f);
fwrite(pictYUV->data[2], 1, w*h/4, f);
fwrite(pictYUV->data[1], 1, w*h/4, f);

对于 640x480 的输入大小,当查看文件时(使用 ImageMagick 显示实用程序),图像是预期的。

但是,对于 864x480 的输入尺寸,图像似乎已损坏。

有趣的是,如果我通过 sws_scale 运行 pictYUV,并转储生成的 yuv 输出,图像看起来很好。
sws_scale(swsCtx, pictYUV->data, pictYUV->linesize, 0, pictYUV->height,
  pictNewYUV->data, pictNewYUV->linesize);

我只需要 yuv 数据。我希望我可以避免对 sws_scale 的额外调用。想知道我错过了什么。问候。

最佳答案

I dump raw yuv values to a file as follows:

fwrite(pictYUV->data[0], 1, w*h, f);
fwrite(pictYUV->data[2], 1, w*h/4, f);
fwrite(pictYUV->data[1], 1, w*h/4, f);


行被填充,写入每一行(大小为 w 用于亮度或 w/2 用于色度),然后将数据指针递增 data->linesize[n] , 其中 n是平面索引:
static void writePlane(FILE *f, uint8_t *data, ptrdiff_t linesize,
                       int w, int h)
{
    for (int y = 0; y < h; y++) {
        fwrite(data, 1, w, f);
        data += linesize;
    }
}

然后使用以下方法编写平面:
writePlane(f, pictYUV->data[0], pictYUV->linesize[0], w, h);
writePlane(f, pictYUV->data[1], pictYUV->linesize[1], w / 2, h / 2);
writePlane(f, pictYUV->data[2], pictYUV->linesize[2], w / 2, h / 2);

关于ffmpeg - 在 avcodec_decode_video2 之后转储 yuv 值的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44785801/

相关文章:

python - ffmpeg-python 错误,无法加载 mp4 编码器

video - ffmpeg中带有png图像的电影

ffmpeg - 混合超过 1000 个 mp3 的声音不好/我可以使用 FFmpeg 合并 1000 个 mp3 吗?

python - python中关于ffmpeg的错误

php - 通过 shell_exec() 创建的视频无法播放,但通过终端创建的视频可以

python - 如何在 python 函数中使用 ffmpeg

video - 使用 FFMPEG 创建视频缩略图时无法使用比例大小

Windows 控制台 - BAT 兼容性

npm - 你如何将 ffmpeg npm 包与 svelte 一起使用?

ffmpeg 无效的流说明符 : si