From patchwork Tue Jul 6 09:20:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Diederick C. Niehorster" X-Patchwork-Id: 28802 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp4922942ios; Tue, 6 Jul 2021 02:27:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqmML6Vv7/PI/TYmCIgb1POpBVNBALMnd9pKq25NVFXgjkfs1XIW/NJgICcCilxbqkA6l4 X-Received: by 2002:a17:907:3d86:: with SMTP id he6mr18047677ejc.440.1625563624030; Tue, 06 Jul 2021 02:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625563624; cv=none; d=google.com; s=arc-20160816; b=W9DlzAsU8v7Z8T+xR3PiBvhCO7Dn13txm2XiyZUzpYLvVZQ1IGtSRL8Ry//QW4GzJD j9GpUVinmzoO7NSNAu/CfSTiD5qX6B64BkkKT0rJZxQ9b7fj65oe7QSGkhYyO41s3DUw KeVj7zVsArIBoTLuGvHxSuDtrZiJTTUtsbNHwmAdxMIIeds0R7m0/y2wXyeiI5HGpvnz T7X3cO/11xn1+cHYsiucBGH+6ea+Wh7A1o7JFDBDGeWSTWJ3/GLkA3Z3kEe/S59TeZVO GidsmNF8nL8wKXOUQ+KaIC7h3oYRazhRqSBL5YsRc/vLSCMUPUfKTHWaUhRXPJEAfdic V6IA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=mMruHb9GX/04GTWkmNiFEeLpD/wsgKycTHD4nuvDleA=; b=b9yct7/TmkIdpeoeXq3nsiV3ttf7OSAg+JY/uHVQyqoOQO8M+xgAz0ng0+3vB7t6EJ xqDAJWIIXSBjbjwwqoCMcTxfZKJb9S23i5BJWRezvR63QnkNw4v1ovukEGe+mCZNmaq4 OaHkogTk8mjeCJJIzllcBkZqUeqJ85ILJV6AgvkopBQTmkYwOvI2RkAL4q5RHFPSsovs Cur2slBh4y6976u+C0XZQBYU8Drh7AqxEpkufr7Kpc+NNs5nFxOFD7yX5h+6lVlV0nH9 B4GdJVRk1XG5Jk39+OhzPXTAyKDXwvnv8yzQkUtZJugVjlUfGTSe5W8dpN836KFF6SZI cVzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Wil12HL2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id cw19si13322115ejc.372.2021.07.06.02.27.03; Tue, 06 Jul 2021 02:27:04 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Wil12HL2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B746F68A936; Tue, 6 Jul 2021 12:26:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1A34A68A917 for ; Tue, 6 Jul 2021 12:26:41 +0300 (EEST) Received: by mail-lj1-f178.google.com with SMTP id s18so6473823ljg.7 for ; Tue, 06 Jul 2021 02:26:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nWA/F1KE/bFpWAQAcLJza+oAlo2bbpYiWL6I6UHffys=; b=Wil12HL29VlH1QEPWv2pvlZpE/MP1D+H5UzuXn8Laa3CxtKPCpbqOdxv8alyyRSPTp gQoQTH+Se3SyvtDh2wWMoXNRCG5L4QMEaOQ7xB/HBCbqkASQ+8zPb2r9Z9JEYq2pUnQL Y2FYGug26Z8BZUCtmMCe6ObhFPi+ynmKgp3QFxpnxiWd+gQaDR6g+VoH60CYzIL7wNlC tqnkfBL80pkQFELU+i8Jl9tc1SxmNVU5LBdNogMVENQvdjkTV2V5c6b3xF+LlkHiTEvH dEDsFHWj0ATNX4HjLN0wrhj0vA67XNuDXQGBD8cAC6LqZxvTGIgrruNOigusaC9TSDR6 6WwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nWA/F1KE/bFpWAQAcLJza+oAlo2bbpYiWL6I6UHffys=; b=l9iDzXFRZodS/lCDHGzaVm5maKitmJdQ313GxkpYDPQoqBknK4iaYvPlJexKwkohMO 43BBkeR7yt6SaUXyFGPsiikpdKNyavVh09mkcFsPZQ0001QJhFdcMRxHwtqWN5ZiwdN8 UGE7mDwkIqKRSlIo2KwCvPj/b3zo/f+kXbs17yRs+fO5poQrkxwRDOXb6lnXpTNR+etf Qbd3B46CipaA8JH0UMrSMR/Iq6ZyTfQblXr0O5QqcWU4RhEeiaJoicJ5n3ITB2aCONYr 6zbWBlODxdethFM+pkgiOZytDOp5L9vZlmYE7TqvA2rNV0S9dWe0fhcXqPGkPC5m6N7H 964w== X-Gm-Message-State: AOAM530hBCwbhn/Qbg0NLLY8pk9jP51S2Qo1y1Olo/gQQ4ODOC3koF1a gz3q8YTaEshZFWkLlQwZ2mdeKTg09QE= X-Received: by 2002:a2e:7203:: with SMTP id n3mr14510280ljc.372.1625563600173; Tue, 06 Jul 2021 02:26:40 -0700 (PDT) Received: from localhost.localdomain (deedock.humlab.lu.se. [130.235.135.183]) by smtp.gmail.com with ESMTPSA id n24sm1340051lfb.273.2021.07.06.02.26.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jul 2021 02:26:39 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Tue, 6 Jul 2021 11:20:16 +0200 Message-Id: <20210706092020.1057-31-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20210706092020.1057-1-dcnieho@gmail.com> References: <20210706092020.1057-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 30/34] avformat: add avformat_alloc_input_context() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Diederick Niehorster Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: p/JU+zsC2nE9 avformat_alloc_input_context function analogous to avformat_alloc_output_context2, except that it does not take a filename argument as guessing the format by just the filename does not make sense. avformat_alloc_input_context can be used e.g. with the avdevice capabilities API, which needs an allocated input format with priv_data (and default options) set, but device should not be opened. Added some checks to avformat_open_input, for the case that a AVFormatContext* allocated by avformat_alloc_input_context is provided: 1. if avformat_open_input's AVInputFormat *fmt argument is not NULL, clean up any already set s->iformat 2. if s->url is already set and avformat_open_input's filename argument is not NULL, free current url and replace by provided filename 3. if s->url is already set and avformat_open_input's filename argument is NULL, do not set s->url to "", but keep current url 4. if s->priv_data has already been allocated, do not do so again. 4b. do reset options to default and apply provided options (if any) 5. add 4b to docs of avformat_open_input Bumping avformat version. Signed-off-by: Diederick Niehorster --- libavformat/avformat.h | 29 +++++++++++++++-- libavformat/utils.c | 74 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index a276aad64a..a0a20b9045 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1889,6 +1889,26 @@ AVProgram *av_new_program(AVFormatContext *s, int id); * @} */ + /** + * Allocate an AVFormatContext for an input format. + * avformat_free_context() can be used to free the context and + * everything allocated by the framework within it. NB: in general + * the correct format cannot be known (unless the user has extra + * information) until the file is opened. If forcing a format by + * this method, but it turns out not to match the file's format + * upon avformat_open_input(), the latter will throw an error. + * + * @param *ctx is set to the created format context, or to NULL in + * case of failure + * @param iformat format to use for allocating the context, if NULL + * format_name is used instead + * @param format_name the name of input format to use for allocating the + * context + * @return >= 0 in case of success, a negative AVERROR code in case of + * failure + */ +int avformat_alloc_input_context(AVFormatContext **ctx, const AVInputFormat *iformat, + const char *format_name); /** * Allocate an AVFormatContext for an output format. @@ -1984,9 +2004,9 @@ int av_probe_input_buffer(AVIOContext *pb, const AVInputFormat **fmt, * Open an input stream and read the header. The codecs are not opened. * The stream must be closed with avformat_close_input(). * - * @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. + * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context, or + * avformat_alloc_input_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 url URL of the stream to open. * @param fmt If non-NULL, this parameter forces a specific input format. @@ -1994,6 +2014,9 @@ int av_probe_input_buffer(AVIOContext *pb, const AVInputFormat **fmt, * @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. + * Note that if a AVFormatContext allocated by avformat_alloc_input_context + * is provided, any demuxer-private options will be overwritten by their defaults + * before applying this new set of demuxer-private options, if any. * * @return 0 on success, a negative AVERROR on failure. * diff --git a/libavformat/utils.c b/libavformat/utils.c index 0df14682a4..34e21d6450 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -478,6 +478,56 @@ static int update_stream_avctx(AVFormatContext *s) } +int avformat_alloc_input_context(AVFormatContext **avctx, const AVInputFormat *iformat, + const char *format) +{ + AVFormatContext *s = avformat_alloc_context(); + int ret = 0; + + *avctx = NULL; + if (!s) + goto nomem; + + if (!iformat) { + if (format) { + iformat = av_find_input_format(format); + if (!iformat) { + av_log(s, AV_LOG_ERROR, "Requested input format '%s' not found\n", format); + ret = AVERROR(EINVAL); + goto error; + } + } + else { + av_log(s, AV_LOG_ERROR, "You should provide an input format or the name of an input format when calling this function\n"); + ret = AVERROR(EINVAL); + goto error; + } + } + + s->iformat = iformat; + if (s->iformat->priv_data_size > 0) { + s->priv_data = av_mallocz(s->iformat->priv_data_size); + if (!s->priv_data) + goto nomem; + if (s->iformat->priv_class) { + *(const AVClass **) s->priv_data = s->iformat->priv_class; + av_opt_set_defaults(s->priv_data); + } + } + else + s->priv_data = NULL; + + *avctx = s; + return 0; +nomem: + av_log(s, AV_LOG_ERROR, "Out of memory\n"); + ret = AVERROR(ENOMEM); +error: + avformat_free_context(s); + return ret; +} + + int avformat_open_input(AVFormatContext **ps, const char *filename, const AVInputFormat *fmt, AVDictionary **options) { @@ -492,8 +542,14 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n"); return AVERROR(EINVAL); } - if (fmt) + if (fmt) { + if (s->iformat) { + if (s->iformat->priv_class && s->priv_data) + av_opt_free(s->priv_data); + av_freep(&s->priv_data); + } s->iformat = fmt; + } if (options) av_dict_copy(&tmp, *options, 0); @@ -504,7 +560,9 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, if ((ret = av_opt_set_dict(s, &tmp)) < 0) goto fail; - if (!(s->url = av_strdup(filename ? filename : ""))) { + if (filename && s->url) + av_freep(&s->url); + if (!s->url && !(s->url = av_strdup(filename ? filename : ""))) { ret = AVERROR(ENOMEM); goto fail; } @@ -547,12 +605,16 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, s->duration = s->start_time = AV_NOPTS_VALUE; - /* Allocate private data. */ + /* Allocate private data and set options. */ if (s->iformat->priv_data_size > 0) { - if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) { - ret = AVERROR(ENOMEM); - goto fail; + /* allocate if not already allocated */ + if (!s->priv_data) { + if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) { + ret = AVERROR(ENOMEM); + goto fail; + } } + /* Overwrite any options already set (1: back to defaults, 2: apply options, if any) */ if (s->iformat->priv_class) { *(const AVClass **) s->priv_data = s->iformat->priv_class; av_opt_set_defaults(s->priv_data);