将 QImage 转换为YUV420P像素格式
Converting QImage to YUV420P pixel format
有人更早地解决这个问题吗?我需要简单快速的方法来将 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中的像素
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 为什么更改视频格式会更改其像素值?
- 使用C++解码为ffmpeg中的特定像素格式
- 图像的像素格式始终返回 32 位
- glew和glut您如何激活像素格式
- 未知像素格式错误SDL2
- 如何选择DirectX像素格式
- 如何使用 Windows API 获取像素格式布局
- OpenGL从4*浮点格式(C++)上传像素数据
- 通过 C/C++ 使用 JPEG 像素格式在 v4l2 中设置/获取相机 JPEG 压缩质量
- 将用于绘制为位图的像素格式更改
- 以在编译时和运行时都可用的方式描述C++像素格式信息
- 奇怪的SDL_Surface->格式->字节每像素值
- 如何在libavcodec中设置解码像素格式
- SDL2 纹理的像素格式和表面颜色遮罩
- 将 QImage 转换为YUV420P像素格式
- OpenGL上下文创建从不选择像素格式