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);
相关推荐
weixin_421607551 小时前
SRT字幕驱动视频自动分镜切割:电影解说批量生成的工程实现思路
ffmpeg·srt字幕·ai剪辑·影视解说·ai电影解说·视频自动切割·字幕文件解析
相偎2 小时前
arm平台编译mpp、ffmpeg和xfreerdp
arm开发·ffmpeg
LoyalToOrigin21 小时前
iOS 26 libass字幕渲染问题兼容解决实践
ios·ffmpeg·objective-c
siv771 天前
影视解说视频智能生产全链路方案解析:从脚本生成到多平台分发
ffmpeg·srt字幕·ai剪辑·影视解说·ai电影解说·视频自动切割·字幕文件解析
独占的甜蜜1 天前
从FLAC到WAV:whisper.cpp中的FFmpeg音频预处理全解析过程
ffmpeg·whisper·音视频
独占的甜蜜1 天前
从FLAC到WAV:whisper.cpp中的FFmpeg音频预处理全解析
ffmpeg·whisper·音视频
JMchen1232 天前
集成第三方 C/C++ 库到 Android NDK 项目:OpenCV 与 FFmpeg 实战指南
opencv·ffmpeg·音视频开发·cmake·jni·ndk·abi 兼容性
qcx232 天前
【AI Agent实战】 0 成本视频处理全流程:ffmpeg + whisper 实现去水印、双语字幕、品牌片尾 | 实战SOP
人工智能·ffmpeg·音视频
huxiao_06012 天前
Windosw下VS 2022编译FFmpeg(支持x264、x265、fdk-acc)
ffmpeg·音视频
小驴程序源4 天前
TS 分片合并完整教程
python·ffmpeg