summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libavformat/avformat.h22
-rw-r--r--libavformat/options.c28
-rw-r--r--libavformat/utils.c104
3 files changed, 153 insertions, 1 deletions
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 2b5b50e381..56a9d57a66 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -730,6 +730,7 @@ typedef struct AVFormatContext {
#if FF_API_FLAG_RTP_HINT
#define AVFMT_FLAG_RTP_HINT 0x0040 ///< Deprecated, use the -movflags rtphint muxer specific AVOption instead
#endif
+#define AVFMT_FLAG_CUSTOM_IO 0x0080 ///< The caller has supplied a custom AVIOContext, don't avio_close() it.
int loop_input;
@@ -1041,6 +1042,27 @@ int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
AVFormatParameters *ap);
/**
+ * Open an input stream and read the header. The codecs are not opened.
+ * The stream must be closed with av_close_input_file().
+ *
+ * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
+ * May be a pointer to NULL, in which case an AVFormatContext is allocated by this
+ * function and written into ps.
+ * Note that a user-supplied AVFormatContext will be freed on failure.
+ * @param filename Name of the stream to open.
+ * @param fmt If non-NULL, this parameter forces a specific input format.
+ * Otherwise the format is autodetected.
+ * @param options A dictionary filled with AVFormatContext and demuxer-private options.
+ * On return this parameter will be destroyed and replaced with a dict containing
+ * options that were not found. May be NULL.
+ *
+ * @return 0 on success, a negative AVERROR on failure.
+ *
+ * @note If you want to use custom IO, preallocate the format context and set its pb field.
+ */
+int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);
+
+/**
* Allocate an AVFormatContext.
* avformat_free_context() can be used to free the context and everything
* allocated by the framework within it.
diff --git a/libavformat/options.c b/libavformat/options.c
index c11f19e687..c2729b75d9 100644
--- a/libavformat/options.c
+++ b/libavformat/options.c
@@ -33,6 +33,33 @@ static const char* format_to_name(void* ptr)
else return "NULL";
}
+static const AVOption *opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
+{
+ AVFormatContext *s = obj;
+ AVInputFormat *ifmt = NULL;
+ AVOutputFormat *ofmt = NULL;
+ if (s->priv_data) {
+ if ((s->iformat && !s->iformat->priv_class) ||
+ (s->oformat && !s->oformat->priv_class))
+ return NULL;
+ return av_opt_find(s->priv_data, name, unit, opt_flags, search_flags);
+ }
+
+ while ((ifmt = av_iformat_next(ifmt))) {
+ const AVOption *o;
+
+ if (ifmt->priv_class && (o = av_opt_find(&ifmt->priv_class, name, unit, opt_flags, search_flags)))
+ return o;
+ }
+ while ((ofmt = av_oformat_next(ofmt))) {
+ const AVOption *o;
+
+ if (ofmt->priv_class && (o = av_opt_find(&ofmt->priv_class, name, unit, opt_flags, search_flags)))
+ return o;
+ }
+ return NULL;
+}
+
#define OFFSET(x) offsetof(AVFormatContext,x)
#define DEFAULT 0 //should be NAN but it does not work as it is not a constant in glibc as required by ANSI/ISO C
//these names are too long to be readable
@@ -72,6 +99,7 @@ static const AVClass av_format_context_class = {
.item_name = format_to_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
+ .opt_find = opt_find,
};
static void avformat_get_context_defaults(AVFormatContext *s)
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 60f4d03e4b..31cdde3edb 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -605,6 +605,107 @@ int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
}
+/* open input file and probe the format if necessary */
+static int init_input(AVFormatContext *s, const char *filename)
+{
+ int ret;
+ AVProbeData pd = {filename, NULL, 0};
+
+ if (s->pb) {
+ s->flags |= AVFMT_FLAG_CUSTOM_IO;
+ if (!s->iformat)
+ return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, 0);
+ else if (s->iformat->flags & AVFMT_NOFILE)
+ return AVERROR(EINVAL);
+ return 0;
+ }
+
+ if ( (s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
+ (!s->iformat && (s->iformat = av_probe_input_format(&pd, 0))))
+ return 0;
+
+ if ((ret = avio_open(&s->pb, filename, AVIO_FLAG_READ)) < 0)
+ return ret;
+ if (s->iformat)
+ return 0;
+ return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, 0);
+}
+
+int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
+{
+ AVFormatContext *s = *ps;
+ int ret = 0;
+ AVFormatParameters ap = { 0 };
+ AVDictionary *tmp = NULL;
+
+ if (!s && !(s = avformat_alloc_context()))
+ return AVERROR(ENOMEM);
+ if (fmt)
+ s->iformat = fmt;
+
+ if (options)
+ av_dict_copy(&tmp, *options, 0);
+
+ if ((ret = av_opt_set_dict(s, &tmp)) < 0)
+ goto fail;
+
+ if ((ret = init_input(s, filename)) < 0)
+ goto fail;
+
+ /* check filename in case an image number is expected */
+ if (s->iformat->flags & AVFMT_NEEDNUMBER) {
+ if (!av_filename_number_test(filename)) {
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ }
+
+ s->duration = s->start_time = AV_NOPTS_VALUE;
+ av_strlcpy(s->filename, filename, sizeof(s->filename));
+
+ /* allocate private data */
+ if (s->iformat->priv_data_size > 0) {
+ if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ if (s->iformat->priv_class) {
+ *(const AVClass**)s->priv_data = s->iformat->priv_class;
+ av_opt_set_defaults(s->priv_data);
+ if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
+ goto fail;
+ }
+ }
+
+ /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
+ if (s->pb)
+ ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC);
+
+ if (s->iformat->read_header)
+ if ((ret = s->iformat->read_header(s, &ap)) < 0)
+ goto fail;
+
+ if (s->pb && !s->data_offset)
+ s->data_offset = avio_tell(s->pb);
+
+ s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
+
+ if (options) {
+ av_dict_free(options);
+ *options = tmp;
+ }
+ *ps = s;
+ return 0;
+
+fail:
+ av_dict_free(&tmp);
+ if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
+ avio_close(s->pb);
+ avformat_free_context(s);
+ *ps = NULL;
+ return ret;
+}
+
/*******************************************************/
static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
@@ -2573,7 +2674,8 @@ void avformat_free_context(AVFormatContext *s)
void av_close_input_file(AVFormatContext *s)
{
- AVIOContext *pb = s->iformat->flags & AVFMT_NOFILE ? NULL : s->pb;
+ AVIOContext *pb = (s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO) ?
+ NULL : s->pb;
av_close_input_stream(s);
if (pb)
avio_close(pb);