avformat: add parent AVFormatContext to URLContext

Add a URLContext.fmt_ctx back-reference, set by io_open_default before
url_open() runs. Will be used to access shared resources per avfc.

Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
This commit is contained in:
Kacper Michajłow
2026-06-15 02:13:16 +02:00
parent 336b175c9f
commit c6e2ae5107
5 changed files with 45 additions and 9 deletions

View File

@@ -365,16 +365,17 @@ int ffurl_alloc(URLContext **puc, const char *filename, int flags,
return AVERROR_PROTOCOL_NOT_FOUND;
}
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char* blacklist,
URLContext *parent)
static int url_open_whitelist(URLContext **puc, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char* blacklist,
URLContext *parent, AVFormatContext *avfc)
{
AVDictionary *tmp_opts = NULL;
AVDictionaryEntry *e;
int ret = ffurl_alloc(puc, filename, flags, int_cb);
if (ret < 0)
return ret;
(*puc)->avfc = avfc;
if (parent) {
ret = av_opt_copy(*puc, parent);
if (ret < 0)
@@ -415,6 +416,15 @@ fail:
return ret;
}
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char* blacklist,
URLContext *parent)
{
return url_open_whitelist(puc, filename, flags, int_cb, options,
whitelist, blacklist, parent, NULL);
}
int ffio_fdopen(AVIOContext **sp, URLContext *h)
{
AVIOContext *s;
@@ -474,16 +484,18 @@ int ffio_fdopen(AVIOContext **sp, URLContext *h)
return 0;
}
int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char *blacklist)
int ffio_open_whitelist2(AVIOContext **s, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char *blacklist,
AVFormatContext *avfc)
{
URLContext *h;
int err;
*s = NULL;
err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
err = url_open_whitelist(&h, filename, flags, int_cb, options, whitelist,
blacklist, NULL, avfc);
if (err < 0)
return err;
err = ffio_fdopen(s, h);
@@ -494,6 +506,14 @@ int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
return 0;
}
int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char *blacklist)
{
return ffio_open_whitelist2(s, filename, flags, int_cb, options,
whitelist, blacklist, NULL);
}
int avio_open2(AVIOContext **s, const char *filename, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options)
{

View File

@@ -23,6 +23,8 @@
#include "libavutil/log.h"
typedef struct AVFormatContext AVFormatContext;
extern const AVClass ff_avio_class;
typedef struct FFIOContext {
@@ -255,6 +257,16 @@ int ffio_open_whitelist(AVIOContext **s, const char *url, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char *blacklist);
/**
* Like ffio_open_whitelist(), but additionally records @p avfc on the
* underlying URLContext before it is connected, so protocols can use shared
* per-format resource. Pass NULL for standalone use.
*/
int ffio_open_whitelist2(AVIOContext **s, const char *url, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options,
const char *whitelist, const char *blacklist,
AVFormatContext *avfc);
/**
* Close a null buffer.
*

View File

@@ -23,6 +23,8 @@
#include <stdint.h>
#include "config_components.h"
#include "packet_internal.h"
#include "avformat.h"

View File

@@ -153,7 +153,8 @@ static int io_open_default(AVFormatContext *s, AVIOContext **pb,
av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
return ffio_open_whitelist2(pb, url, flags, &s->interrupt_callback, options,
s->protocol_whitelist, s->protocol_blacklist, s);
}
static int io_close2_default(AVFormatContext *s, AVIOContext *pb)

View File

@@ -46,6 +46,7 @@ typedef struct URLContext {
const char *protocol_whitelist;
const char *protocol_blacklist;
int min_packet_size; /**< if non zero, the stream is packetized with this min packet size */
struct AVFormatContext *avfc; /**< the AVFormatContext that opened this URLContext, or NULL for standalone use */
} URLContext;
typedef struct URLProtocol {