有人之前解决过这个问题吗?我需要简单快速的方法将 QImage::bits() 缓冲区从 RGB32 转换为 YUV420P 像素格式。你能帮我吗?
最佳答案
libswscale , ffmpeg 的一部分项目具有优化的例程来执行色彩空间转换、缩放和过滤。如果您确实想要速度,我建议使用它,除非您无法添加额外的依赖项。我还没有实际测试过这段代码,但总体思路如下:
QImage img = ... //your image in RGB32
//allocate output buffer. use av_malloc to align memory. YUV420P
//needs 1.5 times the number of pixels (Cb and Cr only use 0.25
//bytes per pixel on average)
char* out_buffer = (char*)av_malloc((int)ceil(img.height() * img.width() * 1.5));
//allocate ffmpeg frame structures
AVFrame* inpic = avcodec_alloc_frame();
AVFrame* outpic = avcodec_alloc_frame();
//avpicture_fill sets all of the data pointers in the AVFrame structures
//to the right places in the data buffers. It does not copy the data so
//the QImage and out_buffer still need to live after calling these.
avpicture_fill((AVPicture*)inpic,
img.bits(),
AV_PIX_FMT_ARGB,
img.width(),
img.height());
avpicture_fill((AVPicture*)outpic,
out_buffer,
AV_PIX_FMT_YUV420P,
img.width(),
img.height());
//create the conversion context. you only need to do this once if
//you are going to do the same conversion multiple times.
SwsContext* ctx = sws_getContext(img.width(),
img.height(),
AV_PIX_FMT_ARGB,
img.width(),
img.height(),
AV_PIX_FMT_YUV420P,
SWS_BICUBIC,
NULL, NULL, NULL);
//perform the conversion
sws_scale(ctx,
inpic->data,
inpic->linesize,
0,
img.height(),
outpic->data,
outpic->linesize);
//free memory
av_free(inpic);
av_free(outpic);
//...
//free output buffer when done with it
av_free(out_buffer);
就像我说的,我还没有测试过这段代码,因此可能需要一些调整才能使其正常工作。
关于c++ - 将 QImage 转换为 YUV420P 像素格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584699/