ffmpeg中av_image_alloc()函数的用法 以及 另一种同样功能的函数用法
一、ffmpeg中av_image_alloc()是这样定义的。此函数的功能是按照指定的宽、高、像素格式来分析图像内存。
参数说明:
pointers[4]:保存图像通道的地址。如果是RGB,则前三个指针分别指向R,G,B的内存地址。第四个指针保留不用linesizes[4]:保存图像每个通道的内存对齐的步长,即一行的对齐内存的宽度,此值大小等于图像宽度。w: 要申请内存的图像宽度。h: 要申请内存的图像高度。pix_fmt: 要申请内存的图像的像素格式。align: 用于内存对齐的值。返回值:所申请的内存空间的总大小。如果是负值,表示申请失败。/*** Allocate an image with size w and h and pixel format pix_fmt, and* fill pointers and linesizes accordingly.* The allocated image buffer has to be freed by using* av_freep(&pointers[0]).** @param align the value to use for buffer size alignment* @return the size in bytes required for the image buffer, a negative* error code in case of failure*/
int av_image_alloc(uint8_t *pointers[4], int linesizes[4],int w, int h, enum AVPixelFormat pix_fmt, int align);
(1)调用实例
参见本地工程 \vs2010cProjects\ffmpeg_yuv_file_show_sdl2\ffmpeg_yuv_file_show_sdl2。
具体代码:
ret= av_image_alloc(src_data, src_linesize,src_w, src_h, src_pixfmt, 1); //按照长、宽、像素格式分配各个通道的内存大小以及步长(linesize),内存地址保存到src_data的指针数组中if (ret< 0) {printf( "Could not allocate source image\n");return -1;
}
二、另一种同样功能的函数调用方法
int image_buf_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1); //计算指定像素格式、图像宽、高所需要的对齐的内存大小
out_buffer = (unsigned char *)av_malloc(image_buf_size); //分配指定大小的内存空间
av_image_get_buffer_size()函数的作用是通过指定像素格式、图像宽、图像高来计算所需的内存大小。
ffmpeg中关于av_image_get_buffer_size()的定义如下:
/*** Return the size in bytes of the amount of data required to store an* image with the given parameters.** @param pix_fmt the pixel format of the image* @param width the width of the image in pixels* @param height the height of the image in pixels* @param align the assumed linesize alignment* @return the buffer size in bytes, a negative error code in case of failure*/
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align);
重点说明一个参数align:此参数是设定内存对齐的对齐数,也就是按多大的字节进行内存对齐。比如设置为1,表示按1字节对齐,那么得到的结果就是与实际的内存大小一样。再比如设置为4,表示按4字节对齐。也就是内存的起始地址必须是4的整倍数。
ffmpeg中关于av_image_fill_array()函数的定义:
/*** Setup the data pointers and linesizes based on the specified image* parameters and the provided array.** The fields of the given image are filled in by using the src* address which points to the image data buffer. Depending on the* specified pixel format, one or multiple image data pointers and* line sizes will be set. If a planar format is specified, several* pointers will be set pointing to the different picture planes and* the line sizes of the different planes will be stored in the* lines_sizes array. Call with src == NULL to get the required* size for the src buffer.** To allocate the buffer and fill in the dst_data and dst_linesize in* one call, use av_image_alloc().** @param dst_data data pointers to be filled in* @param dst_linesize linesizes for the image in dst_data to be filled in* @param src buffer which will contain or contains the actual image data, can be NULL* @param pix_fmt the pixel format of the image* @param width the width of the image in pixels* @param height the height of the image in pixels* @param align the value used in src for linesize alignment* @return the size in bytes required for src, a negative error code* in case of failure*/
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],const uint8_t *src,enum AVPixelFormat pix_fmt, int width, int height, int align);
av_image_fill_arrays()函数自身不具备内存申请的功能,此函数类似于格式化已经申请的内存,即通过av_malloc()函数申请的内存空间。
再者,av_image_fill_arrays()中参数具体说明:
dst_data[4]: [out]对申请的内存格式化为三个通道后,分别保存其地址
dst_linesize[4]: [out]格式化的内存的步长(即内存对齐后的宽度)
*src: [in]av_alloc()函数申请的内存地址。
pix_fmt: [in] 申请 src内存时的像素格式
width: [in]申请src内存时指定的宽度
height: [in]申请scr内存时指定的高度
align: [in]申请src内存时指定的对齐字节数。
(1)调用实例1
image_buf_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 11, 5, 1); //按1字节进行内存对齐,得到的内存大小最接近实际大小printf("image_buf_size:%d\n",image_buf_size);image_buf_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 11, 5, 0); //按0字节进行内存对齐,得到的内存大小是0printf("image_buf_size:%d\n",image_buf_size);image_buf_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 11, 5, 4); //按4字节进行内存对齐,得到的内存大小稍微大一些printf("image_buf_size:%d\n",image_buf_size);
(2)调用实例2
img_convert_out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(dst_pixfmt, dst_w, dst_h, 1));
av_image_fill_arrays(pFrameYUV->data,pFrameYUV->linesize, img_convert_out_buffer,dst_pixfmt, dst_w, dst_h, 1);
img_convert_ctx = sws_getContext(src_w, src_h, src_pixfmt,dst_w, dst_h, dst_pixfmt,SWS_BICUBIC, NULL, NULL, NULL);
(3)调用方法总结
通过以上实例可以看到,(a)计算所需内存大小av_image_get_bufferz_size() --> (b) 按计算的内存大小申请所需内存 av_malloc() --> (c) 对申请的内存进行格式化 av_image_fill_arrays();
ffmpeg中av_image_alloc()函数的用法 以及 另一种同样功能的函数用法相关推荐
- FFmpeg中的日志以及avio实现对文件的读写功能
ffmpeg日志 ffmpeg日志,接口都定义在log.h,使用的库是libavutil 使用时一定要加上对头文件extern "C"外部,否则会报对应的函数没有定义 // // ...
- 目标检测,FFmpeg中第一个基于深度学习模型的视频分析功能
2021年4月,终于把目标检测(object detection)加到FFmpeg upstream了,有maintainer身份加持,还是交互了将近100封邮件,花了两个多月才完成upstream, ...
- FFMpeg中apiexample.c例子分析——解码分析
FFMpeg中apiexample.c例子分析--解码分析 收藏 我们直接从 video_decode_example() 函数开始讲,该函数实现了如何去解码一个视频文件,以 .mpeg 文 件为例. ...
- ffmpeg中的sws_scale算法性能测试
经常用到ffmpeg中的sws_scale来进行图像缩放和格式转换,该函数可以使用各种不同算法来对图像进行处理.以前一直很懒,懒得测试和甄别应该使用哪种算法,最近的工作时间,很多时候需要等待别人.忙里 ...
- php函数 99乘法表,[菜鸟学php] php版自定义函数实现99乘法表
54im.com php for循环例子系列文章: 使用自定义函数方式来实现99乘法表,函数是一种可以在任何被需要的时候执行的代码块. 创建 PHP 函数: 所有的函数都使用关键词 "fun ...
- python中的lambda函数用法--无需定义函数名的函数或子程序,避免代码充斥着大量单行函数
匿名函数lambda:是指一类无需定义标识符(函数名)的函数或子程序. lambda 函数可以接收任意多个参数 (包括可选参数) 并且返回单个表达式的值. 要点: lambda 函数不能包含命令 包含 ...
- 二级计算机中应用的公式,计算机二级公式总结(计算机二级excel主要函数的用法)...
计算机二级考试office高级应用中excel主要考那些函数? 计算机考试office高级中excel主要考的函数有: 求和函数SUM.条件求和函数SUMIF.件求和函数SUMIFS.平均数AVERA ...
- Python编程语言学习:python语言中快速查询python自带模块函数的用法及其属性方法、如何查询某个函数关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法.如何查询某个函数&关键词的用法.输出一个类或者实例化对象的所有属性和方法名之详细攻略 ...
- FFmpeg中可执行文件ffplay用法汇总
从https://ffbinaries.com/downloads 下载最新的4.1版本的windows 64位FFplay.目前linux下的只有3.2版本的.FFplay是一个由FFmpeg和SD ...
最新文章
- 观看自由!B站上线斯坦福最新「机器学习系统(MLSys)」全集
- leetcode算法题--一周中的第几天
- Linux 新增一个用户命令 adduser
- wxWidgets:wxCmdLineParser类用法
- 试题 G: 外卖店优先级 第十届蓝桥杯
- Spring批处理CSV处理
- 阿里巴巴Java开发手册——速读记录
- contentProvider 内容提供者
- ml302硬件手册_Cat.1模组ML302使用MQTT协议接入OneNet平台
- oracle学习-数据迁移
- 如何直观的长时间统计Android应用的动态内存消耗
- 安装pycuda的正确办法
- Postgresql的基本操作
- 计算机桌面工作提醒,有什么好用的电脑桌面日程安排提醒软件?
- Golang 解析XML
- 图像的几何变换maketform imtransform imresize imcrop
- 航空公司客户流失分析
- 史上最全 | 室外大规模3D检测数据集汇总
- JavaScript 销毁对象
- Java实现JWS生成与验签