From patchwork Mon Aug 28 12:34:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 43368 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3822:b0:149:dfde:5c0a with SMTP id p34csp113920pzf; Mon, 28 Aug 2023 05:37:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEtIvfyk9m52cZ5ue0ztBHbL51FhIY+BPz/BbwCDBfE3iaMwr7hBxVXMsqupYaGNK9lAH19 X-Received: by 2002:a05:6402:14c3:b0:523:1e18:3a78 with SMTP id f3-20020a05640214c300b005231e183a78mr19547319edx.12.1693226246042; Mon, 28 Aug 2023 05:37:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693226246; cv=none; d=google.com; s=arc-20160816; b=bdRp/TiYARQdS17gaghYB28eNOzjVf2wTpLrKGcZb9Gl7NrWp/sKX6pxqiAVfHlrtq Cf6wkKL3zw0EFed8991Gi8tqsBbOWLkZ+abjDjHmk2pV/0gQmX/ix98Qs7DMdf8JFqkR vItckLd59r6eE6Drtej+iWle1S0EF0t2U6sSFnhHDUdaPlIcTFKl8Jmzd0E6cieC/ZVo ySX6ANKj4JEf4381N/dHaqdyh/yES2RHLI9EzQzOTTq1LVFINeoAecSEMbFdb4ot9eQ0 9inH7nfT899mU5ppcq16WCEKzntUdDDQoIf4NeAnQOxlqdx2oEzNmA8QY/wfUmYA59TW QOTA== 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:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=AMZEW36AZJ+7cWQJyOMbGyy1TyGmmhXPUzhuF+iDAZs=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=uwinfM7T00Qa1np+Mo/215IhfNNltgn6U1jPBo8vb20nkSgNIHMU0Z/N7HT3tgens9 WZY9D8BW76CYpt59tfrnixGlst1mwrrJoFWanm04PttkfIXGnkcrD90ujLLlmwFWLLAP hAKwk6iz39xCw6jV289zCxWGL6a4fex94sRNRWKf3AN4Ms0zfzVwNWWE7Su84RN/Vbvi VJ3Fm7BprzwUBAoy0E1OzTBESSeOnqpKdKwTgcyqODnHMyRfpmvQS58egZakj+ZKQr5y CL/TnTbYcYSrc6SnIIbFLP77KHQiyRgi4+aG1b7mdTcRtokJnkeVMi32oAovzpqYVrx8 iQsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=CryVLocO; 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 y23-20020aa7ccd7000000b0052a49441c0esi4677827edt.554.2023.08.28.05.37.06; Mon, 28 Aug 2023 05:37:26 -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=20221208 header.b=CryVLocO; 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 E69BB68C602; Mon, 28 Aug 2023 15:36:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4D77168A360 for ; Mon, 28 Aug 2023 15:36:44 +0300 (EEST) Received: by mail-ot1-f54.google.com with SMTP id 46e09a7af769-6bf0decd032so968552a34.0 for ; Mon, 28 Aug 2023 05:36:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693226202; x=1693831002; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=DOBQAv0ALr5uGXugeWGjRi1QbFdyAGVLRYQSve7Kl5o=; b=CryVLocOZju/fuZYe2Z83hpsLNRZ3ukWnmNllaAOMO8zeQzoXIZL3khfwlfePtjNCa GQPSUsWxbFdla+ampRFV/rOHv/shdHrk1zKug3vEizZI2lZbKmXerjsM3aGH0lr0cZIY j6z6i+KiiqSLa3kckUpQjL0BN4zSNp33B58UfatAjXx83yf3g2JvBitnWJ3YNnOeDUko FGgsxN7k1jfh731BJx7p7m74JiXpbGJ1ROL0kTAYVeKtRCkWaoSrJDQ4FSmoAJW0DpTX oivo4PnWJo1+jB1Da8EKwp/X0XGZRTnajtnfx+yjTJ+8/wkf4FsPIki/EJFJXCdW61xq tsEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693226202; x=1693831002; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DOBQAv0ALr5uGXugeWGjRi1QbFdyAGVLRYQSve7Kl5o=; b=Ri/jPLPK75XP1wXva419Iv6vJK+fJf+IvNwHzA29Jq/L0WYUC5yH7BrKBymMovJavZ mTn06fB1kwQ5XJwuhnK12uOzF+x4r018wvzpViphavzRqIz49aC3w5utVQKMNMlDc6vz PUCpd1MvEsXRWnQNDIDpXTch8WR42OI9DZHElY/4d83wAqhQfkirwmHnmbqsUiM4nDDM qJa15RdwKwrn7lZE7ZPgkGClKTfjyzBJnMZqOMm6P6hgtUFVWHkiPRbKevTG60QuhYm0 3hw834wu85I8I1Q+7iPfv/4q5IX36r85RW7voDuKbVCt8ccBjHh1HQH+UFLVcPd23a4o GMfg== X-Gm-Message-State: AOJu0YyEo5M6OiZ5i39a8dCETg1j72kRbu6dzmVLWkz0bNB1tNzvqmpD r3uAsTUC/Nw/fwIrCmawnbeuKedIiOE= X-Received: by 2002:a05:6830:1e8d:b0:6b9:9129:dddf with SMTP id n13-20020a0568301e8d00b006b99129dddfmr12579676otr.16.1693226202543; Mon, 28 Aug 2023 05:36:42 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id b15-20020a056830104f00b006b9a98b9659sm3404615otp.19.2023.08.28.05.36.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Aug 2023 05:36:42 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 28 Aug 2023 09:34:53 -0300 Message-ID: <20230828123617.57535-2-jamrial@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230828123617.57535-1-jamrial@gmail.com> References: <20230828123617.57535-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/12] avcodec/avcodec: add side data to AVCodecContext 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: +lsGxCERx2up Signed-off-by: James Almer --- libavcodec/avcodec.c | 2 + libavcodec/avcodec.h | 8 ++++ libavcodec/avpacket.c | 103 ++++++++++++++++++++++++++++++++++++++++++ libavcodec/packet.h | 64 ++++++++++++++++++++++++++ 4 files changed, 177 insertions(+) diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 0700a53b5c..c811d1f7c4 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -474,6 +474,8 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_freep(&avctx->internal); } + av_packet_side_data_set_free(&avctx->packet_side_data); + for (i = 0; i < avctx->nb_coded_side_data; i++) av_freep(&avctx->coded_side_data[i].data); av_freep(&avctx->coded_side_data); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 649411ac79..dda8a2412b 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -2100,6 +2100,14 @@ typedef struct AVCodecContext { * an error. */ int64_t frame_num; + + /** + * Additional data associated with the entire stream. + * + * - decoding: set by user + * - encoding: unused + */ + AVPacketSideDataSet packet_side_data; } AVCodecContext; /** diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 5fef65e97a..7e646d82f5 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -645,3 +645,106 @@ int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp) return 0; } + +AVPacketSideData *av_packet_side_data_set_get(const AVPacketSideDataSet *set, + enum AVPacketSideDataType type) +{ + for (int i = 0; i < set->nb_sd; i++) + if (set->sd[i]->type == type) + return set->sd[i]; + + return NULL; +} + +static int add_side_data_to_set(AVPacketSideDataSet *set, + AVPacketSideData **psd, + enum AVPacketSideDataType type, + uint8_t *data, size_t size) +{ + AVPacketSideData *sd, **tmp; + + for (int i = 0; i < set->nb_sd; i++) { + sd = set->sd[i]; + + if (sd->type == type) { + av_freep(&sd->data); + sd->data = data; + sd->size = size; + *psd = sd; + return 0; + } + } + + if (set->nb_sd + 1U > FFMIN(INT_MAX, SIZE_MAX / sizeof(*tmp))) + return AVERROR(ERANGE); + + tmp = av_realloc_array(set->sd, set->nb_sd + 1, sizeof(*tmp)); + if (!tmp) + return AVERROR(ENOMEM); + + set->sd = tmp; + + sd = av_mallocz(sizeof(*sd)); + if (!sd) + return AVERROR(ENOMEM); + + sd->type = type; + sd->data = data; + sd->size = size; + + set->sd[set->nb_sd++] = sd; + *psd = sd; + + return 0; +} + +int av_packet_side_data_set_add(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + uint8_t *data, size_t size) +{ + AVPacketSideData *sd; + + return add_side_data_to_set(set, &sd, type, data, size); +} + +AVPacketSideData *av_packet_side_data_set_new(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + size_t size) +{ + AVPacketSideData *sd = NULL; + uint8_t *data = av_malloc(size); + int ret; + + if (!data) + return NULL; + + ret = add_side_data_to_set(set, &sd, type, data, size); + if (ret < 0) + av_freep(&data); + + return sd; +} + +void av_packet_side_data_set_remove(AVPacketSideDataSet *set, + enum AVPacketSideDataType type) +{ + for (int i = set->nb_sd - 1; i >= 0; i--) { + AVPacketSideData *sd = set->sd[i]; + if (sd->type != type) + continue; + av_free(set->sd[i]->data); + av_free(set->sd[i]); + set->sd[i] = set->sd[--set->nb_sd]; + } +} + +void av_packet_side_data_set_free(AVPacketSideDataSet *set) +{ + for (int i = 0; i < set->nb_sd; i++) { + av_free(set->sd[i]->data); + av_free(set->sd[i]); + } + set->nb_sd = 0; + + av_freep(&set->sd); +} diff --git a/libavcodec/packet.h b/libavcodec/packet.h index f28e7e7011..a0220c64b3 100644 --- a/libavcodec/packet.h +++ b/libavcodec/packet.h @@ -318,6 +318,14 @@ typedef struct AVPacketSideData { enum AVPacketSideDataType type; } AVPacketSideData; +/** + * Structure to hold a set of AVPacketSideDataSet + */ +typedef struct AVPacketSideDataSet { + AVPacketSideData **sd; + int nb_sd; +} AVPacketSideDataSet; + /** * This structure stores compressed data. It is typically exported by demuxers * and then passed as input to decoders, or received as output from encoders and @@ -724,6 +732,62 @@ int av_packet_make_writable(AVPacket *pkt); */ void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst); +/** + * Allocate a new side data entry into to a set. + * + * @param set a set to which the side data should be added + * @param type side data type + * @param size side data size + * @return pointer to freshly allocated side data entry or NULL otherwise. + */ +AVPacketSideData *av_packet_side_data_set_new(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + size_t size); + +/** + * Wrap an existing array as a packet side data into a set. + * + * @param set a set to which the side data should be added + * @param type side information type + * @param data the side data array. It must be allocated with the av_malloc() + * family of functions. The ownership of the data is transferred to + * pkt. + * @param size side information size + * @return a non-negative number on success, a negative AVERROR code on + * failure. On failure, the set is unchanged and the data remains + * owned by the caller. + */ +int av_packet_side_data_set_add(AVPacketSideDataSet *set, + enum AVPacketSideDataType type, + uint8_t *data, size_t size); + +/** + * Remove side data of the given type from a set. + * + * @param set a set from which the side data should be removed + * @param type side information type + */ +void av_packet_side_data_set_remove(AVPacketSideDataSet *set, + enum AVPacketSideDataType type); + +/** + * Get side information from set. + * + * @param set a set from which the side data should be fetched + * @param type desired side information type + * + * @return pointer to side data if present or NULL otherwise + */ +AVPacketSideData *av_packet_side_data_set_get(const AVPacketSideDataSet *set, + enum AVPacketSideDataType type); + +/** + * Convenience function to free all the side data stored in a set. + * + * @param set the set to free + */ +void av_packet_side_data_set_free(AVPacketSideDataSet *set); + /** * @} */