libavcodec文档对于何时释放分配的数据以及如何释放这些数据不是很具体。在阅读了文档和示例之后,我将下面的示例程序放在一起。源代码中有一些具体的问题,但我的一般问题是,下面的代码是否正确地释放了所有内存?我意识到下面的程序不会在错误发生后进行任何清理--重点是最后的清理。
testfile()函数就是问题所在。
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
}
#include <cstdio>
using namespace std;
void AVFAIL (int code, const char *what) {
char msg[500];
av_strerror(code, msg, sizeof(msg));
fprintf(stderr, "failed: %s\nerror: %s\n", what, msg);
exit(2);
}
#define AVCHECK(f) do { int e = (f); if (e < 0) AVFAIL(e, #f); } while (0)
#define AVCHECKPTR(p,f) do { p = (f); if (!p) AVFAIL(AVERROR_UNKNOWN, #f); } while (0)
void testfile (const char *filename) {
AVFormatContext *format;
unsigned streamIndex;
AVStream *stream = NULL;
AVCodec *codec;
SwsContext *sws;
AVPacket packet;
AVFrame *rawframe;
AVFrame *rgbframe;
unsigned char *rgbdata;
av_register_all();
// load file header
AVCHECK(av_open_input_file(&format, filename, NULL, 0, NULL));
AVCHECK(av_find_stream_info(format));
// find video stream
for (streamIndex = 0; streamIndex < format->nb_streams && !stream; ++ streamIndex)
if (format->streams[streamIndex]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
stream = format->streams[streamIndex];
if (!stream) {
fprintf(stderr, "no video stream\n");
exit(2);
}
// initialize codec
AVCHECKPTR(codec, avcodec_find_decoder(stream->codec->codec_id));
AVCHECK(avcodec_open(stream->codec, codec));
int width = stream->codec->width;
int height = stream->codec->height;
// initialize frame buffers
int rgbbytes = avpicture_get_size(PIX_FMT_RGB24, width, height);
AVCHECKPTR(rawframe, avcodec_alloc_frame());
AVCHECKPTR(rgbframe, avcodec_alloc_frame());
AVCHECKPTR(rgbdata, (unsigned char *)av_mallocz(rgbbytes));
AVCHECK(avpicture_fill((AVPicture *)rgbframe, rgbdata, PIX_FMT_RGB24, width, height));
// initialize sws (for conversion to rgb24)
AVCHECKPTR(sws, sws_getContext(width, height, stream->codec->pix_fmt, width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL));
// read all frames fromfile
while (av_read_frame(format, &packet) >= 0) {
int frameok = 0;
if (packet.stream_index == (int)streamIndex)
AVCHECK(avcodec_decode_video2(stream->codec, rawframe, &frameok, &packet));
av_free_packet(&packet); // Q: is this necessary or will next av_read_frame take care of it?
if (frameok) {
sws_scale(sws, rawframe->data, rawframe->linesize, 0, height, rgbframe->data, rgbframe->linesize);
// would process rgbframe here
}
// Q: is there anything i need to free here?
}
// CLEANUP: Q: am i missing anything / doing anything unnecessary?
av_free(sws); // Q: is av_free all i need here?
av_free_packet(&packet); // Q: is this necessary (av_read_frame has returned < 0)?
av_free(rgbframe);
av_free(rgbdata);
av_free(rawframe); // Q: i can just do this once at end, instead of in loop above, right?
avcodec_close(stream->codec); // Q: do i need av_free(codec)?
av_close_input_file(format); // Q: do i need av_free(format)?
}
int main (int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s filename\n", argv[0]);
return 1;
}
testfile(argv[1]);
}具体问题:
av_free是释放SwsContext的正确方法吗?av_read_frame返回< 0时,框架循环退出。在这种情况下,当它完成时,我还需要av_free_packet吗?av_free_packet,还是av_read_frame会自动释放/重用旧的AVPacket?av_free,而不是每次重新分配AVFrame,对吗?它似乎运转得很好,但我想确认一下,它的工作是应该的,而不是靠运气。av_free(codec) AVCodec还是在avcodec_close之后在AVCodecContext上做其他事情?av_free(format) AVFormatContext还是在av_close_input_file之后做其他事情?我还意识到,在当前版本的libav中,有些函数被废弃了。由于这里不相关的原因,我不得不使用它们。
发布于 2013-08-04 20:34:24
这些函数不仅不受欢迎,它们在一段时间前就被删除了。所以你真的应该考虑升级。
不管怎样,至于你的问题:
1)不,没有比这更自由的了
2)不,使用sws_freeContext()
3)否,如果av_read_frame()返回错误,则数据包不包含任何有效数据
4)是的,你必须在处理完包后,在下一次av_read_frame()呼叫之前释放它
5)是的,它是完全有效的
6)不,编解码器上下文本身是由libavformat分配的,因此av_close_input_file()负责释放它。所以你没什么可做的了。
7)不,av_close_input_file()释放格式上下文,所以您应该没有什么可做的了。
https://stackoverflow.com/questions/18024835
复制相似问题