From patchwork Mon Jun 7 23:04:10 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: 28132 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp3887378iof; Mon, 7 Jun 2021 16:07:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRR1nGSE6T8FRBOX/NGYRs2x2I+fliXDgidv82rvUyRxyva0BcEoXe9tm4MdhKv6VDhFKw X-Received: by 2002:a17:907:2074:: with SMTP id qp20mr20032943ejb.35.1623107226184; Mon, 07 Jun 2021 16:07:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623107226; cv=none; d=google.com; s=arc-20160816; b=A89qOpVOBbDAf9rv3tbZ6wzmKFsD9aGTvmVByuoZpqVJOBBce66uWGPWZutW1e10Jk zqgwyz8rsDBum9QC90EAl07JcmDjlf0UtekLra/dYYfBQQ+R4JkWJ1WRORmZC0oaNh7w COJEuVtp64c6q0IaXP2dlPGWPVlotNdzpXIua99lFTchXpCjIs9+ZQHt8/GcHWBuiGWA y650tTZGhddBW2OHyTTgG79F2HpcXatmQdJyPf7TKW4gtW3qpA6GaWGDajgxsK2tmFJz hC0BLkF442ennv8ZHe2lGDqWb7fbjLj54vx3kW0G7wMTDbtq2MR6BfvBxI8iMo7NcRZO 74iw== 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=jUGv146QYOxgRb7l7LOx3T1qwsYcswKrT+ceWtB1aEk=; b=KPNSet2JmBWF8IAqCzJnGnlvVp984LvOl9EoH54ir+JeTVp+WNyAp58SQUB6j48zri v3qpNK5D/xhApXbC01ij3OiPtqWIlknXjqvhhAXhxoByOiON/vs88V76K9WGVU1MezMu HpzYoHK12vHcwV8LC8UeVPcSnhYTSm2hwKpPpYQp+UK9UWkbb9HIzIGreOVJ0OXGsVzM D7SmyaS5JoYZdjF/RBY8xYL4Fs+OQvLn4CHDYLj8x4wmKlMgkg7MNulYpOtQqXj+M/m9 HFfAWKgDn5ZoYzdi/7iqCgNR83n+8Ehr8+v7KI1QGgg2WUHi6Zffbpnmm3i1AI9l3egn XjqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b="uTG3/G32"; 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 m11si13263296edq.265.2021.06.07.16.07.05; Mon, 07 Jun 2021 16:07:06 -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="uTG3/G32"; 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 D4ECE687F3D; Tue, 8 Jun 2021 02:05:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9A0BA680C55 for ; Tue, 8 Jun 2021 02:04:56 +0300 (EEST) Received: by mail-lf1-f50.google.com with SMTP id t7so21898137lff.0 for ; Mon, 07 Jun 2021 16:04:56 -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=Yi+tpy1lCcTejLc8cCCtnq9dSO48mpsu9CqVm3/+hHo=; b=uTG3/G32uyCLPTwkiSf7xNyVpmUoPnyc0swn1RUNIl3GpfJvr7cJo/BrKaV4nT6rBm Or851heJWBAa6i79Mmq2NjC6iTax0jK2LAsyfKXa8sIWxtlGz6EN5NNlXcIiQGcQWGSi 9M7tPsUg50p73bGoAIoKU9+lSSLVWpRfrecpyow+oB4W2AWEMdVP22yCn0Y8PXatS5QF yhYCpL/ypMSKLFFGrOqiPXS0eWoO2Hm2BHVVCumJpHRM88SmRJlVX8NZdivDqwILGEdB U2M7jac4y4oDmF55nUDRPQ1NDzBxcc82IUUy5YpKLtiSVNVWqjBN3AOtDly2mp2U192V H7BQ== 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=Yi+tpy1lCcTejLc8cCCtnq9dSO48mpsu9CqVm3/+hHo=; b=RyNAYOMW12h04P86j7b4NgjTILMGonKCj7iBiVmP4V2N2pvX2mAJsiBBcRcVE5KRrj IZBu+q+dM+AosQFHNfwb+vnqJa6By1jOK2yz0Py5fFKBkG99ada5mmJd36VMdOkmCb+s 9/hhF2Kp11EL/VMDq535i3kQ0/FUge8sXh2eOAH6o5mTtewyWGR00cY+ppxjFHyrrooX GPRzTTR2+UO2ZF+6siiIVCVODP7gBZAP2+/yymPqPgzvGflKpPsJaIE/xMWRl7WBJ3BZ iFFMQA6HnhoK2fH/gT/mpuJoZz5alHN2+gJDpz9YDrgPbIJWby9vSK74rdRD4njHzvW4 37BQ== X-Gm-Message-State: AOAM530x4257WHjKXSfeYkZ/5zVyJD4kOqjzAS8ivgqfX5ZA4lMIVWD8 4oE4l654QLek70yJqJyoY0hEtWyFTnEeiA== X-Received: by 2002:ac2:55b7:: with SMTP id y23mr11944540lfg.40.1623107095537; Mon, 07 Jun 2021 16:04:55 -0700 (PDT) Received: from localhost.localdomain (84-217-56-54.customers.ownit.se. [84.217.56.54]) by smtp.gmail.com with ESMTPSA id v9sm1999563ljv.131.2021.06.07.16.04.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 16:04:55 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Jun 2021 01:04:10 +0200 Message-Id: <20210607230414.612-32-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20210607230414.612-1-dcnieho@gmail.com> References: <20210607230414.612-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 31/35] 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: fG300/WUobsE 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 that 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. Signed-off-by: Diederick Niehorster --- libavformat/avformat.h | 20 +++++++++++++ libavformat/utils.c | 66 +++++++++++++++++++++++++++++++++++++++--- libavformat/version.h | 2 +- 3 files changed, 83 insertions(+), 5 deletions(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 5e1e82a315..b444602b86 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1896,6 +1896,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. diff --git a/libavformat/utils.c b/libavformat/utils.c index fe8eaa6cb3..2e8084a41b 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,8 +605,8 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, s->duration = s->start_time = AV_NOPTS_VALUE; - /* Allocate private data. */ - if (s->iformat->priv_data_size > 0) { + /* Allocate private data if not already allocated. */ + if (s->iformat->priv_data_size > 0 && !s->priv_data) { if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) { ret = AVERROR(ENOMEM); goto fail; diff --git a/libavformat/version.h b/libavformat/version.h index 13df244d97..d5dd22059b 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 59 -#define LIBAVFORMAT_VERSION_MINOR 5 +#define LIBAVFORMAT_VERSION_MINOR 6 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \