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);
相关推荐
程序员老陆2 小时前
FFmpeg的AVFrame全部数据操作函数详解
ffmpeg·音视频·avframe
glumes3 小时前
从《牛来》到 DCP:一部电影是怎么走进电影院的?
ffmpeg·音视频开发
程序员老陆1 天前
FFmpeg RTSP 解复用器(rtsp demuxer)完全解析:从「能播」到「稳播」的参数圣经
ffmpeg·音视频·拉流·rtsp
诸葛大钢铁2 天前
视频转音频怎么转换?在线工具、VLC、FFmpeg 三种方法详解
ffmpeg·音视频·mp4转mp3·视频转音频·视频转mp3·视频提取音频
qingmiaozhuan2 天前
电脑录屏软件追剧高光截取:FFmpeg帧裁剪与可视化方案对比
ffmpeg·电脑·音视频开发·录屏技术·电脑录屏
程序员老陆3 天前
FFmpeg6操作 RTMP参数详解
ffmpeg·音视频·rtmp
戒了,最后一次3 天前
Windows 安装 FFmpeg 完整教程(附环境变量配置 + 常用命令示例)
windows·ffmpeg
≮傷£≯√3 天前
FFmpeg + qt 音频播放
qt·ffmpeg·音视频
程序员老陆4 天前
Qt的QThread::usleep和FFmpeg的libavutil模块的av_usleep哪个精度高一些?
开发语言·qt·ffmpeg·音视频
程序员老陆4 天前
如何使用FFmpeg6的API进行HLS开发呢?
ffmpeg·音视频·hls