Some (de)muxers open additional files beyond the main IO context.
Currently, they call avio_open() directly, which prevents the caller
from using custom IO for such streams.
This commit adds callbacks to AVFormatContext that default to
avio_open2()/avio_close(), but can be overridden by the caller. All
muxers and demuxers using AVIO are switched to using those callbacks
instead of calling avio_open()/avio_close() directly.
(de)muxers that use the URLProtocol layer directly instead of AVIO
remain unconverted for now. This should be fixed in later commits.
API changes, most recent first:
+2016-xx-xx - xxxxxxx - lavf 57.3.0 - avformat.h
+ Add AVFormatContext.opaque, io_open and io_close, allowing custom IO
+ for muxers and demuxers that open additional files.
+
2015-xx-xx - xxxxxxx - lavc 57.12.0 - avcodec.h
Add AVCodecDescriptor.profiles and avcodec_profile_name().
* Must not be accessed in any way by callers.
*/
AVFormatInternal *internal;
+
+ /**
+ * Arbitrary user data set by the caller.
+ */
+ void *opaque;
+
+ /**
+ * A callback for opening new IO streams.
+ *
+ * Certain muxers or demuxers (e.g. for various playlist-based formats) need
+ * to open additional files during muxing or demuxing. This callback allows
+ * the caller to provide custom IO in such cases.
+ *
+ * @param s the format context
+ * @param pb on success, the newly opened IO context should be returned here
+ * @param url the url to open
+ * @param flags a combination of AVIO_FLAG_*
+ * @param options a dictionary of additional options, with the same
+ * semantics as in avio_open2()
+ * @return 0 on success, a negative AVERROR code on failure
+ *
+ * @note Certain muxers and demuxers do nesting, i.e. they open one or more
+ * additional internal format contexts. Thus the AVFormatContext pointer
+ * passed to this callback may be different from the one facing the caller.
+ * It will, however, have the same 'opaque' field.
+ */
+ int (*io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url,
+ int flags, AVDictionary **options);
+
+ /**
+ * A callback for closing the streams opened with AVFormatContext.io_open().
+ */
+ void (*io_close)(struct AVFormatContext *s, AVIOContext *pb);
} AVFormatContext;
typedef struct AVPacketList {
AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
- ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
+ ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
return ret;
avio_printf(out, "\t</Period>\n");
avio_printf(out, "</MPD>\n");
avio_flush(out);
- avio_close(out);
+ ff_format_io_close(s, &out);
return ff_rename(temp_filename, s->filename);
}
os->ctx = ctx;
ctx->oformat = oformat;
ctx->interrupt_callback = s->interrupt_callback;
+ ctx->opaque = s->opaque;
+ ctx->io_close = s->io_close;
+ ctx->io_open = s->io_open;
if (!(st = avformat_new_stream(ctx, NULL))) {
ret = AVERROR(ENOMEM);
for (i = 0; i < s->nb_streams; i++) {
OutputStream *os = &c->streams[i];
if (os->out)
- avio_close(os->out);
- os->out = NULL;
+ ff_format_io_close(s, &os->out);
if (os->ctx && os->ctx_inited)
av_write_trailer(os->ctx);
if (os->ctx && os->ctx->pb)
snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->filename);
- ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL);
+ ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
return ret;
int b64_size = AV_BASE64_SIZE(os->metadata_size);
char *base64 = av_malloc(b64_size);
if (!base64) {
- avio_close(out);
+ ff_format_io_close(s, &out);
return AVERROR(ENOMEM);
}
av_base64_encode(base64, b64_size, os->metadata, os->metadata_size);
}
avio_printf(out, "</manifest>\n");
avio_flush(out);
- avio_close(out);
+ ff_format_io_close(s, &out);
return ff_rename(temp_filename, filename);
}
"%s/stream%d.abst", s->filename, index);
snprintf(temp_filename, sizeof(temp_filename),
"%s/stream%d.abst.tmp", s->filename, index);
- ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL);
+ ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
return ret;
}
update_size(out, afrt_pos);
update_size(out, 0);
- avio_close(out);
+ ff_format_io_close(s, &out);
return ff_rename(temp_filename, filename);
}
static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
{
int ret, i;
- ret = avio_open2(&os->out, os->temp_filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL);
+ ret = s->io_open(s, &os->out, os->temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0)
return ret;
avio_wb32(os->out, 0);
return 0;
}
-static void close_file(OutputStream *os)
+static void close_file(AVFormatContext *s, OutputStream *os)
{
int64_t pos = avio_tell(os->out);
avio_seek(os->out, 0, SEEK_SET);
avio_wb32(os->out, pos);
avio_flush(os->out);
- avio_close(os->out);
- os->out = NULL;
+ ff_format_io_close(s, &os->out);
}
static int hds_write_header(AVFormatContext *s)
avio_flush(os->ctx->pb);
os->packets_written = 0;
- close_file(os);
+ close_file(s, os);
snprintf(target_filename, sizeof(target_filename),
"%s/stream%dSeg1-Frag%d", s->filename, index, os->fragment_index);
};
typedef struct HLSContext {
+ AVFormatContext *ctx;
int n_variants;
struct variant **variants;
int cur_seq_no;
av_dict_copy(&tmp, c->avio_opts, 0);
- ret = avio_open2(in, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
+ ret = c->ctx->io_open(c->ctx, in, url, AVIO_FLAG_READ, &tmp);
av_dict_free(&tmp);
return ret;
fail:
av_free(new_url);
if (close_in)
- avio_close(in);
+ ff_format_io_close(c->ctx, &in);
return ret;
}
HLSContext *c = s->priv_data;
int ret = 0, i, j, stream_offset = 0;
+ c->ctx = s;
c->interrupt_callback = &s->interrupt_callback;
if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
oc->oformat = hls->oformat;
oc->interrupt_callback = s->interrupt_callback;
+ oc->opaque = s->opaque;
+ oc->io_open = s->io_open;
+ oc->io_close = s->io_close;
for (i = 0; i < s->nb_streams; i++) {
AVStream *st;
int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->size);
snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
- if ((ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ if ((ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL)) < 0)
goto fail;
for (en = hls->list; en; en = en->next) {
avio_printf(out, "#EXT-X-ENDLIST\n");
fail:
- avio_closep(&out);
+ ff_format_io_close(s, &out);
if (ret >= 0)
ff_rename(temp_filename, s->filename);
return ret;
return AVERROR(EINVAL);
c->number++;
- if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
return err;
if (oc->oformat->priv_class && oc->priv_data)
hls->duration = 0;
av_write_frame(oc, NULL); /* Flush any buffered data */
- avio_close(oc->pb);
+ ff_format_io_close(s, &oc->pb);
ret = hls_start(s);
AVFormatContext *oc = hls->avf;
av_write_trailer(oc);
- avio_closep(&oc->pb);
+ ff_format_io_close(s, &oc->pb);
avformat_free_context(oc);
av_free(hls->basename);
append_entry(hls, hls->duration);
s->img_number) < 0 && s->img_number > 1)
return AVERROR(EIO);
for (i = 0; i < 3; i++) {
- if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
- &s1->interrupt_callback, NULL) < 0) {
+ if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
if (i >= 1)
break;
av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
if (f[i]) {
ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
if (!s->is_pipe)
- avio_close(f[i]);
+ ff_format_io_close(s1, &f[i]);
if (ret[i] > 0)
pkt->size += ret[i];
}
return AVERROR(EIO);
}
for (i = 0; i < 3; i++) {
- if (avio_open2(&pb[i], img->tmp, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL) < 0) {
+ if (s->io_open(s, &pb[i], img->tmp, AVIO_FLAG_WRITE, NULL) < 0) {
av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->tmp);
return AVERROR(EIO);
}
avio_write(pb[0], pkt->data, ysize);
avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize) / 2);
avio_write(pb[2], pkt->data + ysize + (pkt->size - ysize) / 2, (pkt->size - ysize) / 2);
- avio_close(pb[1]);
- avio_close(pb[2]);
+ ff_format_io_close(s, &pb[1]);
+ ff_format_io_close(s, &pb[2]);
} else {
if (ff_guess_image2_codec(s->filename) == AV_CODEC_ID_JPEG2000) {
AVStream *st = s->streams[0];
}
avio_flush(pb[0]);
if (!img->is_pipe) {
- avio_close(pb[0]);
+ ff_format_io_close(s, &pb[0]);
ff_rename(img->tmp, filename);
}
return 0;
}
+/**
+ * A wrapper around AVFormatContext.io_close that should be used
+ * intead of calling the pointer directly.
+ */
+void ff_format_io_close(AVFormatContext *s, AVIOContext **pb);
+
#endif /* AVFORMAT_INTERNAL_H */
}
}
-static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref,
- AVIOInterruptCB *int_cb)
+static int mov_open_dref(AVFormatContext *s, AVIOContext **pb, char *src,
+ MOVDref *ref)
{
/* try relative path, we do not try the absolute because it can leak information about our
system to an attacker */
av_strlcat(filename, ref->path + l + 1, 1024);
- if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
+ if (!s->io_open(s, pb, filename, AVIO_FLAG_READ, NULL))
return 0;
}
}
if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
MOVDref *dref = &sc->drefs[sc->dref_id - 1];
if (c->enable_drefs) {
- if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback) < 0)
+ if (mov_open_dref(c->fc, &sc->pb, c->fc->filename, dref) < 0)
av_log(c->fc, AV_LOG_ERROR,
"stream %d, error opening alias: path='%s', dir='%s', "
"filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
}
av_freep(&sc->drefs);
if (sc->pb && sc->pb != s->pb)
- avio_close(sc->pb);
+ ff_format_io_close(s, &sc->pb);
av_freep(&sc->chunk_offsets);
av_freep(&sc->stsc_data);
* writing, so we re-open the same output, but for reading. It also avoids
* a read/seek/write/seek back and forth. */
avio_flush(s->pb);
- ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
+ ret = s->io_open(s, &read_pb, s->filename, AVIO_FLAG_READ, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
"the second pass (faststart)\n", s->filename);
avio_write(s->pb, read_buf[read_buf_id], n);
pos += n;
} while (pos < pos_end);
- avio_close(read_pb);
+ ff_format_io_close(s, &read_pb);
end:
av_free(buf);
.child_class_next = format_child_class_next,
};
+static int io_open_default(AVFormatContext *s, AVIOContext **pb,
+ const char *url, int flags, AVDictionary **options)
+{
+ return avio_open2(pb, url, flags, &s->interrupt_callback, options);
+}
+
+static void io_close_default(AVFormatContext *s, AVIOContext *pb)
+{
+ avio_close(pb);
+}
+
static void avformat_get_context_defaults(AVFormatContext *s)
{
memset(s, 0, sizeof(AVFormatContext));
s->av_class = &av_format_context_class;
+ s->io_open = io_open_default;
+ s->io_close = io_close_default;
+
av_opt_set_defaults(s);
}
oc->oformat = seg->oformat;
oc->interrupt_callback = s->interrupt_callback;
+ oc->opaque = s->opaque;
+ oc->io_close = s->io_close;
+ oc->io_open = s->io_open;
for (i = 0; i < s->nb_streams; i++) {
AVStream *st;
int i, ret = 0;
char buf[1024];
- if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ if ((ret = s->io_open(s, &seg->pb, seg->list, AVIO_FLAG_WRITE, NULL)) < 0)
goto fail;
avio_printf(seg->pb, "#EXTM3U\n");
if (last)
avio_printf(seg->pb, "#EXT-X-ENDLIST\n");
fail:
- avio_closep(&seg->pb);
+ ff_format_io_close(s, &seg->pb);
+
return ret;
}
s->filename, c->number++) < 0)
return AVERROR(EINVAL);
- if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
return err;
if (oc->oformat->priv_class && oc->priv_data)
av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
if (write_trailer)
av_write_trailer(oc);
- avio_close(oc->pb);
+ ff_format_io_close(oc, &oc->pb);
return ret;
}
static void seg_free_context(SegmentContext *seg)
{
- avio_closep(&seg->pb);
+ ff_format_io_close(seg->avf, &seg->pb);
avformat_free_context(seg->avf);
seg->avf = NULL;
}
seg->individual_header_trailer = 0;
if (seg->list && seg->list_type != LIST_HLS)
- if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ if ((ret = s->io_open(s, &seg->pb, seg->list, AVIO_FLAG_WRITE, NULL)) < 0)
goto fail;
for (i = 0; i < s->nb_streams; i++)
}
if (seg->write_header_trailer) {
- if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
goto fail;
} else {
if ((ret = open_null_ctx(&oc->pb)) < 0)
}
if ((ret = avformat_write_header(oc, NULL)) < 0) {
- avio_close(oc->pb);
+ ff_format_io_close(oc, &oc->pb);
goto fail;
}
if (!seg->write_header_trailer) {
close_null_ctx(oc->pb);
- if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ if ((ret = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
goto fail;
}
avio_printf(seg->pb, "%s\n", oc->filename);
avio_flush(seg->pb);
if (seg->size && !(seg->number % seg->size)) {
- avio_closep(&seg->pb);
- if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
+ ff_format_io_close(s, &seg->pb);
+ if ((ret = s->io_open(s, &seg->pb, seg->list,
+ AVIO_FLAG_WRITE, NULL)) < 0)
goto fail;
}
}
}
fail:
- avio_close(seg->pb);
+ ff_format_io_close(s, &seg->pb);
avformat_free_context(oc);
return ret;
}
snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
snprintf(temp_filename, sizeof(temp_filename), "%s/Manifest.tmp", s->filename);
- ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
+ ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
return ret;
}
avio_printf(out, "</SmoothStreamingMedia>\n");
avio_flush(out);
- avio_close(out);
+ ff_format_io_close(s, &out);
return ff_rename(temp_filename, filename);
}
AVIOContext *in;
int ret;
uint32_t len;
- if ((ret = avio_open2(&in, filename, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
+ if ((ret = s->io_open(s, &in, filename, AVIO_FLAG_READ, NULL)) < 0)
return ret;
ret = AVERROR(EIO);
*moof_size = avio_rb32(in);
avio_seek(in, end, SEEK_SET);
}
fail:
- avio_close(in);
+ ff_format_io_close(s, &in);
return ret;
}
{
AVIOContext *in, *out;
int ret = 0;
- if ((ret = avio_open2(&in, infile, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
+ if ((ret = s->io_open(s, &in, infile, AVIO_FLAG_READ, NULL)) < 0)
return ret;
- if ((ret = avio_open2(&out, outfile, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL)) < 0) {
- avio_close(in);
+ if ((ret = s->io_open(s, &out, outfile, AVIO_FLAG_WRITE, NULL)) < 0) {
+ ff_format_io_close(s, &in);
return ret;
}
while (size > 0) {
size -= n;
}
avio_flush(out);
- avio_close(out);
- avio_close(in);
+ ff_format_io_close(s, &out);
+ ff_format_io_close(s, &in);
return ret;
}
sd->size = size;
return data;
}
+
+void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
+{
+ if (*pb)
+ s->io_close(s, *pb);
+ *pb = NULL;
+}