[FFmpeg-devel] [RFC] missing close/free functions for avformat?
Reimar Döffinger
Reimar.Doeffinger
Mon Dec 17 20:28:12 CET 2007
Hello,
On Mon, Dec 17, 2007 at 01:09:14PM +0100, Michael Niedermayer wrote:
> On Sun, Dec 16, 2007 at 11:09:48AM +0100, Reimar D?ffinger wrote:
> > On Sun, Dec 16, 2007 at 02:44:09AM +0100, Michael Niedermayer wrote:
> > > On Sat, Dec 15, 2007 at 08:32:53PM +0100, Reimar D?ffinger wrote:
> > [...]
> > > > It's untested, but does something like this look good?
> > >
> > > if you test it and are sure its correct and theres no way to make
> > > av_close_input_file() work with _stream as well ...
> >
> > Of course there are ways, e.g. a autoalloced_byteio flag (ok, preferably
> > a better name) in the AVFormatContext that indicates if we should do
> > url_fclose on the byteiocontext or not.
> > That would mean we could get rid of the (very minor) "must_open_file"
> > code duplication between open and close functions.
> > But then IMO the naming is inconsistent, and calling it
> > av_free_format_context would be better (keeping the av_close_input_file
> > as deprecated for compatibility).
> > I find that name appropriate because such a function frees the
> > AVFormatContext and everything that avformat allocated "behind the back"
> > of the user, and the only alternative that would give a consistent
>
> ok
This is what I had in mind, say if you think it makes sense.
Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavformat/utils.c
===================================================================
--- libavformat/utils.c (revision 11254)
+++ libavformat/utils.c (working copy)
@@ -477,6 +474,8 @@
}
}
err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
+ if (pb)
+ (*ic_ptr)->fclose_byteio = 1;
if (err)
goto fail;
return 0;
@@ -2039,7 +2038,7 @@
return AVERROR(ENOSYS);
}
-void av_close_input_file(AVFormatContext *s)
+void av_free_format_context(AVFormatContext *s)
{
int i;
AVStream *st;
@@ -2068,12 +2067,17 @@
av_freep(&s->programs[i]);
}
flush_packet_queue(s);
- if (!(s->iformat->flags & AVFMT_NOFILE))
+ if (s->fclose_byteio)
url_fclose(s->pb);
av_freep(&s->priv_data);
av_free(s);
}
+void av_close_input_file(AVFormatContext *s)
+{
+ av_free_format_context(s);
+}
+
AVStream *av_new_stream(AVFormatContext *s, int id)
{
AVStream *st;
Index: libavformat/avformat.h
===================================================================
--- libavformat/avformat.h (revision 11254)
+++ libavformat/avformat.h (working copy)
@@ -21,8 +21,8 @@
#ifndef FFMPEG_AVFORMAT_H
#define FFMPEG_AVFORMAT_H
-#define LIBAVFORMAT_VERSION_INT ((52<<16)+(2<<8)+0)
-#define LIBAVFORMAT_VERSION 52.2.0
+#define LIBAVFORMAT_VERSION_INT ((52<<16)+(3<<8)+0)
+#define LIBAVFORMAT_VERSION 52.3.0
#define LIBAVFORMAT_BUILD LIBAVFORMAT_VERSION_INT
#define LIBAVFORMAT_IDENT "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
@@ -445,6 +434,14 @@
unsigned int nb_programs;
AVProgram **programs;
+
+ /**
+ * If true, url_fclose will be called on the ByteIOContext
+ * otherwise the user is responsible for freeing it.
+ * This is for internal use by libavformat and should not
+ * be accessed by external code.
+ */
+ int fclose_byteio;
} AVFormatContext;
typedef struct AVPacketList {
@@ -571,12 +568,15 @@
AVFormatParameters *ap);
/**
* Allocate an AVFormatContext.
- * Can be freed with av_free() but do not forget to free everything you
- * explicitly allocated as well!
*/
AVFormatContext *av_alloc_format_context(void);
/**
+ * Free an AVFormatContext and all substructures allocated by libavformat.
+ */
+void av_free_format_context(AVFormatContext *);
+
+/**
* Read packets of a media file to get stream information. This
* is useful for file formats with no headers such as MPEG. This
* function also computes the real frame rate in case of mpeg2 repeat
@@ -653,8 +653,9 @@
* Close a media file (but not its codecs).
*
* @param s media file handle
+ * @deprecated Use av_free_format_context instead.
*/
-void av_close_input_file(AVFormatContext *s);
+attribute_deprecated void av_close_input_file(AVFormatContext *s);
/**
* Add a new stream to a media file.
More information about the ffmpeg-devel
mailing list