From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 16A4942784 for ; Wed, 30 Mar 2022 12:21:35 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E532768B2A6; Wed, 30 Mar 2022 15:20:20 +0300 (EEST) Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BB32068B240 for ; Wed, 30 Mar 2022 15:20:19 +0300 (EEST) Received: by mail-lf1-f54.google.com with SMTP id z12so21687292lfu.10 for ; Wed, 30 Mar 2022 05:20:19 -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=CkpEGxcejScS9D4Hm7xVGgijLix5CJGbSatbgtgFHBc=; b=d6WHtgStD2t6oMCWCVK7raJuulLtU2vzL3Lrz42lsgH8rgkXSOp84x4qXhVbtjw5BS WsI6Bz1RKcnfL4KGAIxymxZmJPlYWNzhGWcCd4BoY+qfEoKLZ5dHLRh8yYqcht6Ka/WO text8Fs5rjgImXWWg87DBiWPsxKLpRnqyX4OApnXp6u29QWCTkXyo1kz0pcQjMb0E8f7 J6NuVFrxTR9+7N8BRrdZpZAPzurD3oQg9V4ViSr7VF5wEJV4CszgRun8PcePmmFADQHy F7jvQzvNakgizPPclOtjA2V2vEYl9XQVFBVdj43RvEH7txpt4opaw+E/9uij4po10UVo vzRw== 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=CkpEGxcejScS9D4Hm7xVGgijLix5CJGbSatbgtgFHBc=; b=DAW+MtfzPDMCH3V7MLzlaPDth5MV1njbftHDXwbn7lZXhLIP2KDcXzEubLuiYna8Te alp3Te4lddZGu/EMEMUxiWAGom152HH2rfjMw1e1fKO74nMcVFRJDc/j/tqY6TcMI33N XwX4IlmXcap8/1G0USFnTIZlSiZzZ7gcNw1+cMYN8yuYKZ/Dcb1FOUUSihYJIeyGybBY sTOrQZW5q5IvfIWbDSeCR+Ye4p12yqAg3thWv4GpRSpNO1F6UuQh26a1MykulL+aR1Uy UfObh4k6Snr9NgLrypelwURoGWYzSPQ6sIW+1ilvDkHPlO8Wn4KY0laeO5NuHYCvFe+V pzmg== X-Gm-Message-State: AOAM531f2F10zEWDMFdHoPGURvdwNjlOD6KWnUVftOuu5DFHn+8Wyl4L kDN6enqKlLVni6D8AFFb9BoI/IyUtlc= X-Google-Smtp-Source: ABdhPJztsYOqi1MsRrDOn1sTz5yjDuPVN+Zg1aHWR/L5l9el06SI7GMf4IkAfUofuiJ+PbjM+3sVaA== X-Received: by 2002:a05:6512:128e:b0:44a:4067:9ffe with SMTP id u14-20020a056512128e00b0044a40679ffemr6612210lfs.64.1648642818438; Wed, 30 Mar 2022 05:20:18 -0700 (PDT) Received: from localhost.localdomain (deedock.humlab.lu.se. [130.235.135.183]) by smtp.gmail.com with ESMTPSA id o19-20020a2e9b53000000b00249b8cbdd70sm1843569ljj.103.2022.03.30.05.20.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 05:20:17 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Wed, 30 Mar 2022 14:18:03 +0200 Message-Id: <20220330121806.822-19-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20220330121806.822-1-dcnieho@gmail.com> References: <20220330121806.822-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 18/21] 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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 ef189d9d8e..52442b2e55 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 88d6f750a2..4a7d2f441c 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, \ -- 2.28.0.windows.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".