参考链接
- FFmpeg源代码简单分析:avformat_close_input()_雷霄骅的博客-CSDN博客_avformat_close_input
avformat_close_input()
- 本文简单分析FFmpeg的avformat_close_input()函数。
- 该函数用于关闭一个AVFormatContext,一般情况下是和avformat_open_input()成对使用的。
- avformat_close_input()的声明位于libavformat\avformat.h,如下所示。
/*** Close an opened input AVFormatContext. Free it and all its contents* and set *s to NULL.*/
void avformat_close_input(AVFormatContext **s);
- 下面看一下avformat_close_input()的源代码,位于demux.c文件中。
void avformat_close_input(AVFormatContext **ps)
{AVFormatContext *s;AVIOContext *pb;if (!ps || !*ps)return;s = *ps;pb = s->pb;if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||(s->flags & AVFMT_FLAG_CUSTOM_IO))pb = NULL;if (s->iformat)if (s->iformat->read_close)s->iformat->read_close(s);avformat_free_context(s);*ps = NULL;avio_close(pb);
}
函数调用关系图
- 从源代码中可以看出,avformat_close_input()主要做了以下几步工作:
- (1)调用AVInputFormat的read_close()方法关闭输入流
- (2)调用avformat_free_context()释放AVFormatContext
- (3)调用avio_close()关闭并且释放AVIOContext
AVInputFormat-> read_close()
- AVInputFormat的read_close()是一个函数指针,指向关闭输入流的函数。
- 不同的AVInputFormat包含有不同的read_close()方法。
- 例如,FLV格式对应的AVInputFormat的定义如下。
const AVInputFormat ff_flv_demuxer = {.name = "flv",.long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),.priv_data_size = sizeof(FLVContext),.read_probe = flv_probe,.read_header = flv_read_header,.read_packet = flv_read_packet,.read_seek = flv_read_seek,.read_close = flv_read_close,.extensions = "flv",.priv_class = &flv_kux_class,
};
- 从ff_flv_demuxer的定义中可以看出,read_close()指向的函数是flv_read_close()
- 我们可以看一下flv_read_close()的定义,如下所示。
- 从flv_read_close()的定义可以看出,该函数释放了FLVContext中的new_extradata数组中每个元素指向的内存。
static int flv_read_close(AVFormatContext *s)
{int i;FLVContext *flv = s->priv_data;for (i=0; i<FLV_STREAM_TYPE_NB; i++)av_freep(&flv->new_extradata[i]);av_freep(&flv->keyframe_times);av_freep(&flv->keyframe_filepositions);return 0;
}
avformat_free_context()
- avformat_free_context()是一个FFmpeg的API函数,用于释放一个AVFormatContext。
- 在这里要注意搞清楚avformat_free_context()和avformat_close_input()之间的区别与联系。
- 有关avformat_free_context()可以参考文章:FFmpeg源代码简单分析-通用-常见结构体的初始化和销毁(AVFormatContext,AVFrame等)_MY CUP OF TEA的博客-CSDN博客
avio_close()
- avio_close()是一个FFmpeg的API函数,用于关闭和释放AVIOContext。
- 它的声明位于libavformat\avio.h,如下所示
/*** Close the resource accessed by the AVIOContext s and free it.* This function can only be used if s was opened by avio_open().** The internal buffer is automatically flushed before closing the* resource.** @return 0 on success, an AVERROR < 0 on error.* @see avio_closep*/
int avio_close(AVIOContext *s);
int avio_close(AVIOContext *s)
{FFIOContext *const ctx = ffiocontext(s);URLContext *h;int ret, error;if (!s)return 0;avio_flush(s);h = s->opaque;s->opaque = NULL;av_freep(&s->buffer);if (s->write_flag)av_log(s, AV_LOG_VERBOSE,"Statistics: %"PRId64" bytes written, %d seeks, %d writeouts\n",ctx->bytes_written, ctx->seek_count, ctx->writeout_count);elseav_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n",ctx->bytes_read, ctx->seek_count);av_opt_free(s);error = s->error;avio_context_free(&s);ret = ffurl_close(h);if (ret < 0)return ret;return error;
}
- 从源代码可以看出,avio_close()按照顺序做了以下几个步骤:
- (1)调用avio_flush()强制清除缓存中的数据
- (2)调用av_freep()释放掉AVIOContext种的buffer
- (3)调用av_free()释放掉AVIOContext结构体 av_free函数弃用,av_opt_free和avio_context_free
- (4)调用ffurl_close()关闭并且释放掉URLContext
- 下面按照顺序分别看看avio_flush()和ffurl_close()这两个函数
avio_flush()
- avio_flush()是一个FFmpeg的API函数,声明位于libavformat\avio.h,如下所示。
void avio_flush(AVIOContext *s)
{int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;flush_buffer(s);if (seekback)avio_seek(s, seekback, SEEK_CUR);
}
- 可以看出avio_flush()简单调用了flush_buffer()函数。我们看一下flush_buffer()的定义。
static void flush_buffer(AVIOContext *s)
{s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);if (s->write_flag && s->buf_ptr_max > s->buffer) {writeout(s, s->buffer, s->buf_ptr_max - s->buffer);if (s->update_checksum) {s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,s->buf_ptr_max - s->checksum_ptr);s->checksum_ptr = s->buffer;}}s->buf_ptr = s->buf_ptr_max = s->buffer;if (!s->write_flag)s->buf_end = s->buffer;
}
- 从flush_buffer()定义我们可以看出,该函数将当前缓存指针buf_ptr的位置重新设置到缓存buffer的首部,然后根据AVIOContext对应的流是否可写分别做不同的处理。
- 如果AVIOContext对应的流是只读的(write_flag取值为0),就将缓存的尾部buf_end设定到缓存首部位置;
- 如果AVIOContext对应的流如果是可写的(write_flag取值非0),则会调用writeout()函数输出缓存中剩余的数据。
- 在这里我们看一下writeout()函数的定义,如下所示。
static void writeout(AVIOContext *s, const uint8_t *data, int len)
{FFIOContext *const ctx = ffiocontext(s);if (!s->error) {int ret = 0;if (s->write_data_type)ret = s->write_data_type(s->opaque, (uint8_t *)data,len,ctx->current_type,ctx->last_time);else if (s->write_packet)ret = s->write_packet(s->opaque, (uint8_t *)data, len);if (ret < 0) {s->error = ret;} else {ctx->bytes_written += len;s->bytes_written = ctx->bytes_written;if (s->pos + len > ctx->written_output_size) {ctx->written_output_size = s->pos + len;
#if FF_API_AVIOCONTEXT_WRITTEN
FF_DISABLE_DEPRECATION_WARNINGSs->written = ctx->written_output_size;
FF_ENABLE_DEPRECATION_WARNINGS
#endif}}}if (ctx->current_type == AVIO_DATA_MARKER_SYNC_POINT ||ctx->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {ctx->current_type = AVIO_DATA_MARKER_UNKNOWN;}ctx->last_time = AV_NOPTS_VALUE;ctx->writeout_count++;s->pos += len;
}
- 从定义可以看出,writeout()调用了AVIOContext的write_packet()方法。根据此前文章《FFmpeg源代码简单分析:avio_open2()》中的分析我们可以了解到,AVIOContext的write_packet()实际指向了ffurl_write()函数,而ffurl_write()经过retry_transfer_wrapper()函数最终调用了URLProtocol的url_write()函数。url_write()是一个函数指针,不同的URLProtocol的url_write()指向不同的函数。
- 例如,file(文件)对应的URLProtocol的定义位于libavformat\file.c,如下所示。
const URLProtocol ff_file_protocol = {.name = "file",.url_open = file_open,.url_read = file_read,.url_write = file_write,.url_seek = file_seek,.url_close = file_close,.url_get_file_handle = file_get_handle,.url_check = file_check,.url_delete = file_delete,.url_move = file_move,.priv_data_size = sizeof(FileContext),.priv_data_class = &file_class,.url_open_dir = file_open_dir,.url_read_dir = file_read_dir,.url_close_dir = file_close_dir,.default_whitelist = "file,crypto,data"
};
- 可以看出ff_file_protocol中的url_write()指向的是file_write()函数。
- 我们继续看一下file_write()的源代码,如下所示。
- 从源代码中可以看出file_write()调用了系统的write()方法向文件中写数据(很多人可能对write()函数很陌生,可以简单理解为它等同于fwrite())。
static int file_write(URLContext *h, const unsigned char *buf, int size)
{FileContext *c = h->priv_data;int ret;size = FFMIN(size, c->blocksize);ret = write(c->fd, buf, size);return (ret == -1) ? AVERROR(errno) : ret;
}
ffurl_close()和ffurl_closep()
- ffurl_close()和ffurl_closep()是FFmpeg内部的两个函数,它们的声明位于libavformat\url.h,如下所示。
- 其实这两个函数是等同的
/*** Close the resource accessed by the URLContext h, and free the* memory used by it. Also set the URLContext pointer to NULL.** @return a negative value if an error condition occurred, 0* otherwise*/
int ffurl_closep(URLContext **h);
int ffurl_close(URLContext *h);
- 可见ffurl_close()调用了ffurl_closep()。
int ffurl_close(URLContext *h)
{return ffurl_closep(&h);
}
int ffurl_closep(URLContext **hh)
{URLContext *h= *hh;int ret = 0;if (!h)return 0; /* can happen when ffurl_open fails */if (h->is_connected && h->prot->url_close)ret = h->prot->url_close(h);
#if CONFIG_NETWORKif (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)ff_network_close();
#endifif (h->prot->priv_data_size) {if (h->prot->priv_data_class)av_opt_free(h->priv_data);av_freep(&h->priv_data);}av_opt_free(h);av_freep(hh);return ret;
}
- 从ffurl_closep()的定义可以看出,它主要做了两步工作:
- (1)调用URLProtocol的url_close()
- (2)调用av_freep()释放URLContext结构体
- 其中URLProtocol的url_close()是一个函数指针,其指向的函数与具体的URLProtocol有关,这里我们还是看一下file(文件)对应的URLProtocol,如下所示。
const URLProtocol ff_file_protocol = {.name = "file",.url_open = file_open,.url_read = file_read,.url_write = file_write,.url_seek = file_seek,.url_close = file_close,.url_get_file_handle = file_get_handle,.url_check = file_check,.url_delete = file_delete,.url_move = file_move,.priv_data_size = sizeof(FileContext),.priv_data_class = &file_class,.url_open_dir = file_open_dir,.url_read_dir = file_read_dir,.url_close_dir = file_close_dir,.default_whitelist = "file,crypto,data"
};
- 从ff_file_protocol中可以看出,url_close()指向file_close()函数。我们再看一下file_close()的定义,如下所示。、
static int file_close(URLContext *h)
{FileContext *c = h->priv_data;int ret = close(c->fd);return (ret == -1) ? AVERROR(errno) : 0;
}
- 可见file_close()最终调用了系统函数close()关闭了文件指针(不熟悉close()的可以简单把它理解为fclose())。
- 至此avio_close()函数分析完毕。