From patchwork Fri Mar 25 14:10:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Diederick C. Niehorster" X-Patchwork-Id: 34979 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:ab0:5fda:0:0:0:0:0 with SMTP id g26csp1377148uaj; Fri, 25 Mar 2022 07:15:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9T45HWexcyAKW2iKK5I1n3TAbAzVCASWIh1t3CAGKaLvC7lQFw6eIGTK5GXHGTNC8IEo4 X-Received: by 2002:a05:6402:454:b0:416:2db7:685b with SMTP id p20-20020a056402045400b004162db7685bmr13439891edw.43.1648217702258; Fri, 25 Mar 2022 07:15:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648217702; cv=none; d=google.com; s=arc-20160816; b=IrN3AC4wISKYmVUX/QzVu0KOVx+oYxKR2D45T8ciHmq/9+DwNR7Zmvly007uGE2o3y 2zNfUO6HBmMxSFYDT287GuYeZpWKS31nUAH8fbvka47mNq+l/O1ylLCYzn1OhKxXR53r 5OzgYMYZp4yk3tEkIaLgijLcuU1h36+lh9c3nHGYjZ5mpbNwr2YrMWQmXCGngH8Ee699 pMJYwNS8EEJ9cFMphWyMwClXnWqR/FwJgQLx03LUHh2L753PZbrZWZjvWld3j6/7s9fa cestT7Kc4Aenaf9Tc68VUWuobOPrhjh383VDybw19d9ulXgI6Y61W2XCJmnitELumfVM ppfw== 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=QNKr3ad6Vr2YPY5WLtKgho+nMm/e+u3T+WtTpcAav0o=; b=qdS+7DIn4usTJ9dPcq/WTRsOE7zZFC3ym4+mY5pP2BloLpnUOrbWjdJe3odU1IIbsz C9G30pc86EeVedCP6yZINuANUndL69a0rYe8GmwGLVEhJkC3ZN/iSboNOnjanrssXBTo AZIr8cFg0IXr+U2PYUZwWiX2SeubeUTyuUhCwF9nxhqStCZWvU4+RuU+VMBIPUKKCsZ6 DJtxs3Mf07c9n9gdn5CQ5UUpxyMgJorHs37O1L64RRBG4/ZeV20EEGGoHwO7W6AaOfIe F04R0MR/m7TqP45ztXXSlB6Me8UI4r+IcgIItKw9WjS7Y0dGWLyChopAgriF4Z2OPXb9 3dVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=h8olXqq3; 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 g16-20020a1709061e1000b006df76385f13si2648099ejj.947.2022.03.25.07.15.01; Fri, 25 Mar 2022 07:15:02 -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=20210112 header.b=h8olXqq3; 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 D71A0680074; Fri, 25 Mar 2022 16:13:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2CAAE680074 for ; Fri, 25 Mar 2022 16:13:15 +0200 (EET) Received: by mail-lf1-f51.google.com with SMTP id p10so7747316lfa.12 for ; Fri, 25 Mar 2022 07:13:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7k/XGJ22OYVL8WSwvqmnNwFWf9jjK02W9jsCwc14GBA=; b=h8olXqq3bG24lZWbWnQal8zstTisxGZAlURIqGaORmDJiyp/YMNlVCHU1KxddAD54F y6wkQ/C8fAOllIc0NB/yOblt0pRyO/1vAeDnbM+LCVGyqjaArM8SxQlEnFo+aKDRZ5u3 mSum7ywK8y2qnQRbPQuR7I22KdCiIaIUsqJO00bD3HQdf+7gKP0i5M+PmnU1WYH9yB0C W6pV1XXwwLiNv/llB5m2Qw63sguP1p3Um59ZhQI7kJ89KxHI5OUkcPIRXcc/B4/msUDb /jp70ifc44ZX6Te/vw/iQWUMK3rRikLNKBjgtEYqqpUxTeTX3K+tMNNBg2Oekn64NAPT KYNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7k/XGJ22OYVL8WSwvqmnNwFWf9jjK02W9jsCwc14GBA=; b=NvbIHB19eQBD2sRm48qG0whF9RBjG67yG8MUuzMWzp2TWPZ3C0cSMJq3DGZOyhJdOP dA2sCrBDND/DnEV+MP2S9Dq8oTY8lgeoHSnT9x1yiyZhUJGA28CSOB1huja0snMSLM3x zoqRiWB4auKHYwXbpW6yfDz7gYsHLPd6+NXjWb64LuTexHJrGJbUE77KyEN2NLBUBlQp H2gux/vkdByzy3FrTJMtT5zObKsuBDB/u2Qvwg3BwEeno06PCwdeRWF050ssi9UbD0fa Okplp/f9l1154/+oym11P9YauWLoGJY1MN5BsTXVUkaC1xLu2GZkZvlXvPihKxsIu5Cv fdBw== X-Gm-Message-State: AOAM531OdOQb14kFmF/Lh9ufoEVtXhyn+mrmjPCTwU2woddPJYSl8aJn YalOVpLv4If53eEyGBS5H2tnKaBGe+8= X-Received: by 2002:a05:6512:3b92:b0:448:1bf4:1cbd with SMTP id g18-20020a0565123b9200b004481bf41cbdmr8059927lfv.494.1648217594189; Fri, 25 Mar 2022 07:13:14 -0700 (PDT) Received: from localhost.localdomain (deedock.humlab.lu.se. [130.235.135.183]) by smtp.gmail.com with ESMTPSA id v25-20020a2e4819000000b00245f3318b8csm696305lja.31.2022.03.25.07.13.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 07:13:13 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Fri, 25 Mar 2022 15:10:38 +0100 Message-Id: <20220325141041.1748-20-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20220325141041.1748-1-dcnieho@gmail.com> References: <20220325141041.1748-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 19/22] 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: 4i8ex8bLEfZK 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 Bump libavformat version. Signed-off-by: Diederick Niehorster --- libavformat/avformat.h | 29 +++++++++++++++-- libavformat/demux.c | 74 ++++++++++++++++++++++++++++++++++++++---- libavformat/version.h | 2 +- 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 027a914e13..004d81640f 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -2024,6 +2024,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. @@ -2119,9 +2139,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. @@ -2129,6 +2149,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/demux.c b/libavformat/demux.c index c1c9422ac0..b314477e47 100644 --- a/libavformat/demux.c +++ b/libavformat/demux.c @@ -217,6 +217,56 @@ FF_ENABLE_DEPRECATION_WARNINGS return 0; } + +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) { @@ -233,8 +283,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); @@ -245,7 +301,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; } @@ -288,12 +346,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); diff --git a/libavformat/version.h b/libavformat/version.h index 1ab39022c2..7cd09f7ef8 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ #include "version_major.h" -#define LIBAVFORMAT_VERSION_MINOR 23 +#define LIBAVFORMAT_VERSION_MINOR 24 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \