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);
相关推荐
阿里巴啦16 小时前
python+yt-dlp开源项目,支持 YouTube, Bilibili, TikTok/抖音,快手 等多个平台的视频/音频/字幕下载/ai摘要等功能
python·ffmpeg·whisper·音视频·视频处理·ai摘要·音视频转录
来鸟 鸣间2 天前
linux下ffmpeg源码编译
linux·运维·ffmpeg
Echo_NGC22372 天前
【FFmpeg使用指南】Part 2:滤镜图架构与信号处理
架构·ffmpeg·音视频·信号处理
Echo_NGC22372 天前
【FFmpeg使用指南】Part 1:核心架构与媒体流处理
ffmpeg·音视频·媒体·视频
ssxueyi2 天前
用 Claude Code 从零开发自己的Direct3D 硬件加速播放器
ffmpeg·ai编程·directx·视频播放器·从零开始·claude code·csdn征文活动
Yan_uuu2 天前
ubuntu18.04 安装 x264、ffmpeg、nv-codec-hearers 支持GPU硬件加速
c++·图像处理·ubuntu·ffmpeg
runner365.git2 天前
做一个基于ffmpeg的AI Agent智能体
人工智能·ffmpeg·大模型
彷徨而立2 天前
【FFmpeg】理解 av_packet_from_data 和 av_packet_unref 接口
ffmpeg
runner365.git3 天前
ffmpeg8.0合入whisper,语音识别模型终于进入ffmpeg
ffmpeg·whisper·语音识别
小徐敲java4 天前
视频推流服务器与FFmpeg 安装配置
服务器·ffmpeg·音视频