c++ - 将 QImage 转换为 YUV420P 像素格式

标签 c++ qt

有人之前解决过这个问题吗?我需要简单快速的方法将 QImage::bits() 缓冲区从 RGB32 转换为 YUV420P 像素格式。你能帮我吗?

最佳答案

libswscaleffmpeg 的一部分项目具有优化的例程来执行色彩空间转换、缩放和过滤。如果您确实想要速度,我建议使用它,除非您无法添加额外的依赖项。我还没有实际测试过这段代码,但总体思路如下:

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/

相关文章:

c++ - QLineEdit 在 Qt 中调用 SetText() 后无法撤消

c++ - 关于英特尔的露天采矿示例

c++ - 如何在没有复制赋值运算符的情况下交换两个对象?

c++ - 在 CMake 中包含目录

c++ - 将逗号分隔的文本文件读入数组

c++ - qjsonrpc 中的客户端连接/断开事件

python - 使用 Python (PyQT 5.10) 在 QT 5.10 中加载自己的字体

c++ - Qt - 非常简单的切换 Action 绘图

c++ - 所有 .txt 文件的列表

c++ - 无法访问 std::move(...) C++ 11