From patchwork Mon Oct 23 14:08:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 44333 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1b28:b0:15d:8365:d4b8 with SMTP id ch40csp1433667pzb; Mon, 23 Oct 2023 07:09:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHl6rXsIc0+87iP/6pcRkkkSMDtWUMzZ4fXtaJpZ4TdD/U/DE8HgqhYwPndmDv7cfz+UtJe X-Received: by 2002:a17:907:7b89:b0:9a5:d972:af43 with SMTP id ne9-20020a1709077b8900b009a5d972af43mr9253665ejc.65.1698070165806; Mon, 23 Oct 2023 07:09:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698070165; cv=none; d=google.com; s=arc-20160816; b=PIYw/PJ9flrL1jRH2PJV7ODyeuMunyGe4G7qzLaDd88xgLHj2MxC4/uh7jhuOmTig7 Lq6wA2U4M4cAydH81r6A+IO+rGJwLeULwO0NvejGDr1mimqUmgyAVvfNoueDpELjj8CQ ygj8JH+HkOLylrW8qOBCNt2c43Z9EclF/jHVQ1ZqULybcOkm6i4q6Qq5eJzBmRYtgXAB b5bMuq3SvSSajdIF6HAojfJVZhfw6hufcmywpgaDEUkK1yE/0/dx6maRZZ5kVp0e18CA 4uU0qUC3NFVD2deRjxIWw+ufXM7sdmuMRGLp7h+oLww3Td7TSsiVqbg0H27YENufWHl7 l/0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=HmwWexO84aIfNsNXgQcPHigWZ20VKKmYaqFYKty3b78=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=WuItyKQ256fKKhwHXA0Lftr+uekTcm+Zg+ieZAN2BWh0ZwnICs/mReyYWtA5rcZj20 TUuQryaIJY+c/qAJdZOXUpKwfd0Y32Ph1ekobsCtxKY//DdWHBcIV3jZENnyDcePRRlx mCMPp0/385pn2QPhFJGiZP72OUnDuSDZN2X0iNaGWgGIZEDKnrY5fE53IgqTswgmroKw XTGJMqdxspq0Z1vAtCzLJPfJy62UBYLolEameOkQWKOZ8OTBzW9euTMcB/MQALuvqKK5 TGb7EQwR77q8WVdVqGlX8vWfS/yO4a1m2Fe+GOC0kL5Bc8H6CeX7EObgSYM8Ew2u2lEa +cPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=Q2vjb+vu; 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 do16-20020a170906c11000b0099cd34c0f5esi3401627ejc.466.2023.10.23.07.09.18; Mon, 23 Oct 2023 07:09:25 -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=20230601 header.b=Q2vjb+vu; 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 B254968CA28; Mon, 23 Oct 2023 17:09:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5E1A568B9BF for ; Mon, 23 Oct 2023 17:09:09 +0300 (EEST) Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6bd73395bceso2400242b3a.0 for ; Mon, 23 Oct 2023 07:09:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698070147; x=1698674947; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=vDIGInD9ZpL4VjfmlK0LpoPeYfSxyHXwkDE94tJ+SO8=; b=Q2vjb+vuizB8yYGbfGfu1GzgWJTSxreVMKzwJh1dr8L76ojjuL8EJB5Nk5EXwnSPzN Wnf8Wyr/0Z+xWDXCB6BaZaCWbXcjYZWDvbP4nCFCaErl7RDM49gutX2ApSStgPwICQe+ Z65xwtEOIGI4dTHssNmWBK56rFXiHOEOWcxVkE03OOf+X4T00lijsmSPJPO5PoGxbZnA Yglgc0j4qLhNIRvttlGo78zlG4dvkMURAjoaUPdZc5Q5uNfOzk0oWltXH7JQMY1CZrjm AMirv6XidcW2A/RJUgUhccNvy1dE0woMNLjmqFaOR3ECNRcr3WAjXwTLPTlAZ3PziKXb QUqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698070147; x=1698674947; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vDIGInD9ZpL4VjfmlK0LpoPeYfSxyHXwkDE94tJ+SO8=; b=JNRxvg8N6Q+pCmhN1qEKlPXDHb3cLTtY7ruLKEy+V+lcCxEtbJy/b8rpOpMlahi2zR Y7q4sBTWN50HnOtrFonj7Zdw4RAMlK0Fz3BDlkmBWy14PQriK+w5Hs6vGEwsQexOZ/ag Q9gtapBnujhNhxQ27f3U4FxG6rNvJygKgSkNhgz5uUWnlExYs6oHhaepqRXs9cQzuj2U IWYRbdgFnE+uVFDq3V/x/b9XzPC55TVVOp3XzwrLtlz/3IPB+cYJ2ghYi2OGxgsZg54U qsddhbolctFg0Q0puuyUVO/0qUZzMZeFki6Wn4c2+PG3K96eo5/R7rpgbrytSVGPY2Ea xkDg== X-Gm-Message-State: AOJu0YzQr9vp/EqTvqOpkCzAa8l85CI7f3mzO+XIIVWy4RAD4YI/dh0F hWZGfHVLz+0USZA8wNm62mCk3ZJ6PwQ= X-Received: by 2002:a05:6a00:986:b0:68f:d44c:22f8 with SMTP id u6-20020a056a00098600b0068fd44c22f8mr13281836pfg.1.1698070146522; Mon, 23 Oct 2023 07:09:06 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id c15-20020a63da0f000000b005b1bf3a200fsm5934651pgh.1.2023.10.23.07.09.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 07:09:06 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 23 Oct 2023 11:08:51 -0300 Message-ID: <20231023140851.2087-1-jamrial@gmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat: introduce AVStreamGroup 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ++JqIRy3rj4m Signed-off-by: James Almer --- Changes since the first version: - The union will now hold pointers to Group type specific structs - The array of elements in the group is now of pointers to the streams rather than index values. - Simplified the helpers - Changed field type for idx and id - Added an AVDictionary to hold metadata libavformat/avformat.c | 30 +++++++++ libavformat/avformat.h | 138 +++++++++++++++++++++++++++++++++++++++++ libavformat/internal.h | 33 ++++++++++ libavformat/options.c | 62 ++++++++++++++++++ 4 files changed, 263 insertions(+) diff --git a/libavformat/avformat.c b/libavformat/avformat.c index 5b8bb7879e..99cda56c2f 100644 --- a/libavformat/avformat.c +++ b/libavformat/avformat.c @@ -80,6 +80,24 @@ FF_ENABLE_DEPRECATION_WARNINGS av_freep(pst); } +void ff_free_stream_group(AVStreamGroup **pstg) +{ + AVStreamGroup *stg = *pstg; + + if (!stg) + return; + + av_freep(&stg->streams); + av_freep(&stg->priv_data); + switch (stg->type) { + // Structs in the union are freed here + default: + break; + } + + av_freep(pstg); +} + void ff_remove_stream(AVFormatContext *s, AVStream *st) { av_assert0(s->nb_streams>0); @@ -88,6 +106,14 @@ void ff_remove_stream(AVFormatContext *s, AVStream *st) ff_free_stream(&s->streams[ --s->nb_streams ]); } +void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg) +{ + av_assert0(s->nb_stream_groups > 0); + av_assert0(s->stream_groups[ s->nb_stream_groups - 1 ] == stg); + + ff_free_stream_group(&s->stream_groups[ --s->nb_stream_groups ]); +} + /* XXX: suppress the packet queue */ void ff_flush_packet_queue(AVFormatContext *s) { @@ -118,6 +144,9 @@ void avformat_free_context(AVFormatContext *s) for (unsigned i = 0; i < s->nb_streams; i++) ff_free_stream(&s->streams[i]); + for (unsigned i = 0; i < s->nb_stream_groups; i++) + ff_free_stream_group(&s->stream_groups[i]); + s->nb_stream_groups = 0; s->nb_streams = 0; for (unsigned i = 0; i < s->nb_programs; i++) { @@ -138,6 +167,7 @@ void avformat_free_context(AVFormatContext *s) av_dict_free(&si->id3v2_meta); av_packet_free(&si->pkt); av_packet_free(&si->parse_pkt); + av_freep(&s->stream_groups); av_freep(&s->streams); ff_flush_packet_queue(s); av_freep(&s->url); diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 9e7eca007e..f045084c8d 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1018,6 +1018,77 @@ typedef struct AVStream { int pts_wrap_bits; } AVStream; +enum AVStreamGroupParamsType { + AV_STREAM_GROUP_PARAMS_NONE, +}; + +typedef struct AVStreamGroup { + /** + * A class for @ref avoptions. Set on group creation. + */ + const AVClass *av_class; + + void *priv_data; + + /** + * Group index in AVFormatContext. + */ + unsigned int index; + + /** + * Group-specific group ID. + * + * decoding: set by libavformat + * encoding: set by the user, replaced by libavformat if left unset + */ + int64_t id; + + /** + * Group-specific type + * + * decoding: set by libavformat on group creation + * encoding: set by the user on group creation + */ + enum AVStreamGroupParamsType type; + + /** + * Group-specific type parameters + */ + union { + uintptr_t dummy; // Placeholder + } params; + + /** + * Metadata that applies to the whole file. + * + * - demuxing: set by libavformat in avformat_open_input() + * - muxing: may be set by the caller before avformat_write_header() + * + * Freed by libavformat in avformat_free_context(). + */ + AVDictionary *metadata; + + /** + * Number of elements in AVStreamGroup.streams. + * + * Set by avformat_stream_group_add_stream() must not be modified by any other code. + */ + unsigned int nb_streams; + + /** + * A list of streams in the group. New entries are created with + * avformat_stream_group_add_stream(). + * + * - demuxing: entries are created by libavformat in avformat_open_input(). + * If AVFMTCTX_NOHEADER is set in ctx_flags, then new entries may also + * appear in av_read_frame(). + * - muxing: entries are created by the user before avformat_write_header(). + * + * Freed by libavformat in avformat_free_context(). + */ + const AVStream **streams; +} AVStreamGroup; + struct AVCodecParserContext *av_stream_get_parser(const AVStream *s); #if FF_API_GET_END_PTS @@ -1726,6 +1797,26 @@ typedef struct AVFormatContext { * @return 0 on success, a negative AVERROR code on failure */ int (*io_close2)(struct AVFormatContext *s, AVIOContext *pb); + + /** + * Number of elements in AVFormatContext.stream_groups. + * + * Set by avformat_stream_group_create(), must not be modified by any other code. + */ + unsigned int nb_stream_groups; + + /** + * A list of all stream groups in the file. New groups are created with + * avformat_stream_group_create(), and filled with avformat_stream_group_add_stream(). + * + * - demuxing: groups may be created by libavformat in avformat_open_input(). + * If AVFMTCTX_NOHEADER is set in ctx_flags, then new groups may also + * appear in av_read_frame(). + * - muxing: groups may be created by the user before avformat_write_header(). + * + * Freed by libavformat in avformat_free_context(). + */ + AVStreamGroup **stream_groups; } AVFormatContext; /** @@ -1844,6 +1935,28 @@ const AVClass *avformat_get_class(void); */ const AVClass *av_stream_get_class(void); +/** + * Add a new empty stream group to a media file. + * + * When demuxing, it may be called by the demuxer in read_header(). If the + * flag AVFMTCTX_NOHEADER is set in s.ctx_flags, then it may also + * be called in read_packet(). + * + * When muxing, may be called by the user before avformat_write_header(). + * + * User is required to call avformat_free_context() to clean up the allocation + * by avformat_stream_group_create(). + * + * New streams can be added to the group with avformat_stream_group_add_stream(). + * + * @param s media file handle + * + * @return newly created group or NULL on error. + * @see avformat_new_stream, avformat_stream_group_add_stream. + */ +AVStreamGroup *avformat_stream_group_create(AVFormatContext *s, + enum AVStreamGroupParamsType type); + /** * Add a new stream to a media file. * @@ -1863,6 +1976,31 @@ const AVClass *av_stream_get_class(void); */ AVStream *avformat_new_stream(AVFormatContext *s, const struct AVCodec *c); +/** + * Add an already allocated stream to a stream group. + * + * When demuxing, it may be called by the demuxer in read_header(). If the + * flag AVFMTCTX_NOHEADER is set in s.ctx_flags, then it may also + * be called in read_packet(). + * + * When muxing, may be called by the user before avformat_write_header() after + * having allocated a new group with avformat_stream_group_create() and stream with + * avformat_new_stream(). + * + * User is required to call avformat_free_context() to clean up the allocation + * by avformat_stream_group_add_stream(). + * + * @param stg stream group belonging to a media file. + * @param st stream in the media file to add to the group. + * + * @retval 0 success + * @retval AVERROR(EEXIST) the stream was already in the group + * @retval "another negative error code" legitimate errors + * + * @see avformat_new_stream, avformat_stream_group_create. + */ +int avformat_stream_group_add_stream(AVStreamGroup *stg, const AVStream *st); + #if FF_API_AVSTREAM_SIDE_DATA /** * Wrap an existing array as stream side data. diff --git a/libavformat/internal.h b/libavformat/internal.h index 7702986c9c..c6181683ef 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -202,6 +202,7 @@ typedef struct FFStream { */ AVStream pub; + AVFormatContext *fmtctx; /** * Set to 1 if the codec allows reordering, so pts can be different * from dts. @@ -427,6 +428,26 @@ static av_always_inline const FFStream *cffstream(const AVStream *st) return (const FFStream*)st; } +typedef struct FFStreamGroup { + /** + * The public context. + */ + AVStreamGroup pub; + + AVFormatContext *fmtctx; +} FFStreamGroup; + + +static av_always_inline FFStreamGroup *ffstreamgroup(AVStreamGroup *stg) +{ + return (FFStreamGroup*)stg; +} + +static av_always_inline const FFStreamGroup *cffstreamgroup(const AVStreamGroup *stg) +{ + return (const FFStreamGroup*)stg; +} + #ifdef __GNUC__ #define dynarray_add(tab, nb_ptr, elem)\ do {\ @@ -608,6 +629,18 @@ void ff_free_stream(AVStream **st); */ void ff_remove_stream(AVFormatContext *s, AVStream *st); +/** + * Frees a stream group without modifying the corresponding AVFormatContext. + * Must only be called if the latter doesn't matter or if the stream + * is not yet attached to an AVFormatContext. + */ +void ff_free_stream_group(AVStreamGroup **pstg); +/** + * Remove a stream group from its AVFormatContext and free it. + * The group must be the last stream of the AVFormatContext. + */ +void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg); + unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id); enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag); diff --git a/libavformat/options.c b/libavformat/options.c index 0f79fe0fa4..09eb13e97a 100644 --- a/libavformat/options.c +++ b/libavformat/options.c @@ -270,6 +270,7 @@ AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c) if (!st->codecpar) goto fail; + sti->fmtctx = s; sti->avctx = avcodec_alloc_context3(NULL); if (!sti->avctx) goto fail; @@ -324,6 +325,67 @@ fail: return NULL; } +AVStreamGroup *avformat_stream_group_create(AVFormatContext *s, + enum AVStreamGroupParamsType type) +{ + AVStreamGroup **stream_groups; + AVStreamGroup *stg; + FFStreamGroup *stgi; + + stream_groups = av_realloc_array(s->stream_groups, s->nb_stream_groups + 1, + sizeof(*stream_groups)); + if (!stream_groups) + return NULL; + s->stream_groups = stream_groups; + + stgi = av_mallocz(sizeof(*stgi)); + if (!stgi) + return NULL; + stg = &stgi->pub; + + stg->type = type; + switch (type) { + // Structs in the union are allocated here + default: + break; + } + + stgi->fmtctx = s; + stg->index = s->nb_stream_groups; + + s->stream_groups[s->nb_stream_groups++] = stg; + + return stg; +} + +static int stream_group_add_stream(AVStreamGroup *stg, const AVStream *st) +{ + const AVStream **streams = av_realloc_array(stg->streams, stg->nb_streams + 1, + sizeof(*stg->streams)); + if (!streams) + return AVERROR(ENOMEM); + + stg->streams = streams; + stg->streams[stg->nb_streams++] = st; + + return 0; +} + +int avformat_stream_group_add_stream(AVStreamGroup *stg, const AVStream *st) +{ + const FFStreamGroup *stgi = cffstreamgroup(stg); + const FFStream *sti = cffstream(st); + + if (stgi->fmtctx != sti->fmtctx) + return AVERROR(EINVAL); + + for (int i = 0; i < stg->nb_streams; i++) + if (stg->streams[i]->index == st->index) + return AVERROR(EEXIST); + + return stream_group_add_stream(stg, st); +} + static int option_is_disposition(const AVOption *opt) { return opt->type == AV_OPT_TYPE_CONST &&