如何从YUV420p转换ffmpeg编码器的RGB?

编程入门 行业动态 更新时间:2024-10-21 13:24:29
本文介绍了如何从YUV420p转换ffmpeg编码器的RGB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过使用c ++代码从位图图像制作.avi视频文件。 我写了以下代码:

I want to make .avi video file from bitmap images by using c++ code. I wrote the following code:

//Get RGB array data from bmp file uint8_t* rgb24Data = new uint8_t[3*imgWidth*imgHeight]; hBitmap = (HBITMAP) LoadImage( NULL, _T("myfile.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); GetDIBits(hdc, hBitmap, 0, imgHeight, rgb24Data , (BITMAPINFO*)&bmi, DIB_RGB_COLORS); /* Allocate the encoded raw picture. */ AVPicture dst_picture; avpicture_alloc(&dst_picture, AV_PIX_FMT_YUV420P, imgWidth, imgHeight); /* Convert rgb24Data to YUV420p and stored into array dst_picture.data */ RGB24toYUV420P(imgWidth, imgHeight, rgb24Data, dst_picture.data); //How to implement this function? //code for encode frame dst_picture here

我的问题是如何实现 RGB24toYUV420P()函数,此函数将将 RGB24 数据从数组rgb24Data转换为 YUV420p 并存储到ffmpeg编码器的数组 dst_picture.data 中

My problem is how to implement RGB24toYUV420P() function, this function will convert RGB24 data from array rgb24Data to YUV420p and store into array dst_picture.data for ffmpeg encoder?

推荐答案

您可以使用SwScale

You can use the SwScale

如下所示:

#include <libswscale/swscale.h> SwsContext * ctx = sws_getContext(imgWidth, imgHeight, AV_PIX_FMT_RGB24, imgWidth, imgHeight, AV_PIX_FMT_YUV420P, 0, 0, 0, 0); uint8_t * inData[1] = { rgb24Data }; // RGB24 have one plane int inLinesize[1] = { 3*imgWidth }; // RGB stride sws_scale(ctx, inData, inLinesize, 0, imgHeight, dst_picture.data, dst_picture.linesize)

请注意,您应该只创建一个 SwsContext 对象的实例,而不是每个框架。

Note that you should create an instance of the SwsContext object only once, not for each frame.

更多推荐

如何从YUV420p转换ffmpeg编码器的RGB?

本文发布于:2023-11-30 20:43:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编码器   YUV420p   ffmpeg   RGB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!