ffmpeg7.0 AVFrame的分配与释放

一、分配函数

/**
 * Allocate an AVFrame and set its fields to default values.  The resulting
 * struct must be freed using av_frame_free().
 *
 * @return An AVFrame filled with default values or NULL on failure.
 *
 * @note this only allocates the AVFrame itself, not the data buffers. Those
 * must be allocated through other means, e.g. with av_frame_get_buffer() or
 * manually.
 */
AVFrame *av_frame_alloc(void);

根据描述,AVFrame的分配函数是av_frame_alloc,但是仅仅只是分配AVFrame对象,AVFrame的成员变量data是不分配的。

1.1 分配data、buf、extended_data、extended_buf等

/**
 * Allocate new buffer(s) for audio or video data.
 *
 * The following fields must be set on frame before calling this function:
 * - format (pixel format for video, sample format for audio)
 * - width and height for video
 * - nb_samples and ch_layout for audio
 *
 * This function will fill AVFrame.data and AVFrame.buf arrays and, if
 * necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf.
 * For planar formats, one buffer will be allocated for each plane.
 *
 * @warning: if frame already has been allocated, calling this function will
 *           leak memory. In addition, undefined behavior can occur in certain
 *           cases.
 *
 * @param frame frame in which to store the new buffers.
 * @param align Required buffer size alignment. If equal to 0, alignment will be
 *              chosen automatically for the current CPU. It is highly
 *              recommended to pass 0 here unless you know what you are doing.
 *
 * @return 0 on success, a negative AVERROR on error.
 */
int av_frame_get_buffer(AVFrame *frame, int align);

示例:

分配一个解码器的pix_fmt格式的内存:

AVFrame* picture = av_frame_alloc();
if (!picture) {
	fprintf(stderr, "Could not allocate video frame\n");
	return;
}
picture->format = codec_ctx->pix_fmt;
picture->width = codec_ctx->width;
picture->height = codec_ctx->height;
ret = av_frame_get_buffer(picture, 0);

分配一个BGR格式的内存:

AVFrame* pRGB = av_frame_alloc();
if (!pRGB) {
	return;
}
pRGB->format = AV_PIX_FMT_BGR24;
pRGB->width = codec_ctx->width;
pRGB->height = codec_ctx->height;
ret = av_frame_get_buffer(pRGB, 0);

二、内存释放

/**
 * Free the frame and any dynamically allocated objects in it,
 * e.g. extended_data. If the frame is reference counted, it will be
 * unreferenced first.
 *
 * @param frame frame to be freed. The pointer will be set to NULL.
 */
void av_frame_free(AVFrame **frame);

根据描述,使用下面代码可以释放分配的内存

av_frame_free(&pRGB);
av_frame_free(&picture);
相关推荐
PlumCarefree15 小时前
基于鸿蒙API10的RTSP播放器(五:拖动底部视频滑轨实现跳转)
华为·ffmpeg·音视频
LuckyInn16 小时前
从安装ffmpeg开始,把一个视频按照每秒30帧fps剪切为图片
ffmpeg·音视频
cuijiecheng201817 小时前
FFmpeg源码:skip_bits、skip_bits1、show_bits函数分析
ffmpeg
__Destiny__21 小时前
视频格式转为mp4(使用ffmpeg)
ffmpeg·视频编解码
<Sunny>21 小时前
SDL 2.0视频数据渲染到窗口上播放流程
ffmpeg·音视频
PlumCarefree1 天前
USB摄像头视频流转RTSP流
图像处理·ffmpeg·音视频·媒体·视频编解码
PlumCarefree1 天前
基于鸿蒙API10的RTSP播放器(八:音量和亮度调节功能的整合)
华为·ffmpeg·音视频·harmonyos
源之缘-OFD先行者1 天前
ffmpeg实现视频的合成与分割
ffmpeg·音视频
一尺丈量2 天前
ffmpeg硬件解码一般流程
c++·人工智能·ffmpeg·cuda·硬件解码
cuijiecheng20182 天前
音视频入门基础:AAC专题(5)——FFmpeg源码中,判断某文件是否为AAC裸流文件的实现
ffmpeg·音视频·aac