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 789B547900 for ; Wed, 27 Sep 2023 13:13:21 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D516168CAF7; Wed, 27 Sep 2023 16:13:12 +0300 (EEST) Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 10C5C68CAD5 for ; Wed, 27 Sep 2023 16:13:06 +0300 (EEST) Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1c46b30a1ceso99130085ad.3 for ; Wed, 27 Sep 2023 06:13:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695820384; x=1696425184; darn=ffmpeg.org; 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=b7MvLH/0e7ChTn+mobmn8ffE4xgtw8uKGetdxWmCeCA=; b=XdCMlqvD/54MGdrBpU5tvlp1wJpFAA1Jhixw9maJusl5Ooa1dF+CYexB1VvJRYOXxO UOjlm2h2OQop8hsYuGjwvHm4yeY140mXeTbbj1BBdRGlxlQec2Qb0BfDr/qz4L+M0tUw 2oVuZCGOJWd1M77pyCslHFP+yUwTx7inrwwQaKTpccy0mPhvCUyeZltGb3p+zW0kZs9z 1sBHBeBI9zobvs1ZSkT4/vIMzdG+CiB5InYqLmqCobuLmbPQ8/X1Rx02kTKbjn1u8SOf olKUrUct+ZshvRm3dTeAv4CRlShrH83bHubm4Npa2XyaYD05dtiJVAYt/XP6JSqJLsI6 V2ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695820384; x=1696425184; 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=b7MvLH/0e7ChTn+mobmn8ffE4xgtw8uKGetdxWmCeCA=; b=sE0zbojhMMMeCnctXuTi1D186WfQR2N+rlmu57lNDddA63wXbFeAw1XsEDnnmm5ALj b68NwUjBFW/SRaamlea98kIv/Q5zNpu8Xm5qdzUBTBoR4Cm7TVWLDx0zV5LToxHG/Kt9 JCNCOxG/SvXBPyonjc5KXe2jKdg+OpYfL2l3kfhvj5VBpvtaFA7X7/PgWSGVLx3d/Dn5 qysD0wH6CaN1ddLiaXLzBlRHMmueaJ25BE0tyYunYohIt8YQUjprMG0cBSZUvIETaqeC 3trbJOGQu6BbxUXaRItqXVlJZMZHbuRcCk02XY7zv5cs+Z0fhIdZpbESfhbhe/D7tFDo kvRA== X-Gm-Message-State: AOJu0YyFnVNPEzcjSA7Jg4GGXmsfNeAiE9ZRwMdAJUsuncu6ojbYtXh3 tCa8/f59izbFvYAt2P7kYHkCEGODooc= X-Google-Smtp-Source: AGHT+IGeSzWHXsxLxDPCp4GmuiAWNgx4Jgp/z7j0idnS+tV2yXmE8bLVVB59aXMrc217YSUvjVSwtg== X-Received: by 2002:a17:903:244d:b0:1c2:1068:1f4f with SMTP id l13-20020a170903244d00b001c210681f4fmr2065606pls.17.1695820383776; Wed, 27 Sep 2023 06:13:03 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id v10-20020a1709029a0a00b001b53953f306sm13050600plp.178.2023.09.27.06.13.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 06:13:03 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Sep 2023 10:12:32 -0300 Message-ID: <20230927131242.1950-2-jamrial@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230927131242.1950-1-jamrial@gmail.com> References: <20230927131242.1950-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/11] avcodec/packet: add generic side data helpers 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 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: Handling AVPacketSideData directly, which can used on structs other than AVPacket. This will be useful in the following commits. Signed-off-by: James Almer --- libavcodec/avpacket.c | 99 +++++++++++++++++++++++++++++++++++++++++++ libavcodec/packet.h | 96 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 191 insertions(+), 4 deletions(-) diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 9ec1feb068..750a17de48 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -646,3 +646,102 @@ int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp) return 0; } + +const AVPacketSideData *av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, + enum AVPacketSideDataType type) +{ + for (int i = 0; i < nb_sd; i++) + if (sd[i].type == type) + return &sd[i]; + + return NULL; +} + +static AVPacketSideData *packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, + enum AVPacketSideDataType type, + void *data, size_t size) +{ + AVPacketSideData *sd = *psd, *tmp; + int nb_sd = *pnb_sd; + + for (int i = 0; i < nb_sd; i++) { + if (sd[i].type != type) + continue; + + av_free(sd[i].data); + sd[i].data = data; + sd[i].size = size; + return &sd[i]; + } + + if (nb_sd + 1U > INT_MAX) + return NULL; + + tmp = av_realloc_array(sd, nb_sd + 1, sizeof(*tmp)); + if (!tmp) + return NULL; + + *psd = sd = tmp; + sd[nb_sd].type = type; + sd[nb_sd].data = data; + sd[nb_sd].size = size; + *pnb_sd = nb_sd + 1; + + return &sd[nb_sd]; +} + +AVPacketSideData *av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, + enum AVPacketSideDataType type, + void *data, size_t size, int flags) +{ + return packet_side_data_add(psd, pnb_sd, type, data, size); +} + +AVPacketSideData *av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, + enum AVPacketSideDataType type, + size_t size, int flags) +{ + AVPacketSideData *sd = NULL; + void *data; + + if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) + return NULL; + + data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!data) + return NULL; + + sd = packet_side_data_add(psd, pnb_sd, type, data, size); + if (!sd) + av_freep(&data); + + return sd; +} + +void av_packet_side_data_remove(AVPacketSideData *sd, int *pnb_sd, + enum AVPacketSideDataType type) +{ + int nb_sd = *pnb_sd; + + for (int i = nb_sd - 1; i >= 0; i--) { + if (sd[i].type != type) + continue; + av_free(sd[i].data); + sd[i] = sd[--nb_sd]; + break; + } + + *pnb_sd = nb_sd; +} + +void av_packet_side_data_free(AVPacketSideData **psd, int *pnb_sd) +{ + AVPacketSideData *sd = *psd; + int nb_sd = *pnb_sd; + + for (int i = 0; i < nb_sd; i++) + av_free(sd[i].data); + + av_freep(psd); + *pnb_sd = 0; +} diff --git a/libavcodec/packet.h b/libavcodec/packet.h index f28e7e7011..96fc0084d6 100644 --- a/libavcodec/packet.h +++ b/libavcodec/packet.h @@ -33,9 +33,9 @@ #include "libavcodec/version_major.h" /** - * @defgroup lavc_packet AVPacket + * @defgroup lavc_packet_side_data AVPacketSideData * - * Types and functions for working with AVPacket. + * Types and functions for working with AVPacketSideData. * @{ */ enum AVPacketSideDataType { @@ -318,6 +318,96 @@ typedef struct AVPacketSideData { enum AVPacketSideDataType type; } AVPacketSideData; +/** + * Allocate a new packet side data. + * + * @param sd pointer to an array of side data to which the side data should + * be added. *sd may be NULL, in which case the array will be + * initialized. + * @param nb_sd pointer to an integer containing the number of entries in + * the array. The integer value will be increased by 1 on success. + * @param type side data type + * @param size desired side data size + * @param flags currently unused. Must be zero + * + * @return pointer to freshly allocated side data on success, or NULL otherwise. + */ +AVPacketSideData *av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, + enum AVPacketSideDataType type, + size_t size, int flags); + +/** + * Wrap existing data as packet side data. + * + * @param sd pointer to an array of side data to which the side data should + * be added. *sd may be NULL, in which case the array will be + * initialized + * @param nb_sd pointer to an integer containing the number of entries in + * the array. The integer value will be increased by 1 on success. + * @param type side data type + * @param data a data array. It must be allocated with the av_malloc() family + * of functions. The ownership of the data is transferred to the + * side data array on success + * @param size size of the data array + * @param flags currently unused. Must be zero + * + * @return pointer to freshly allocated side data on success, or NULL otherwise + * On failure, the side data array is unchanged and the data remains + * owned by the caller. + */ +AVPacketSideData *av_packet_side_data_add(AVPacketSideData **sd, int *nb_sd, + enum AVPacketSideDataType type, + void *data, size_t size, int flags); + +/** + * Get side information from a side data array. + * + * @param sd the array from which the side data should be fetched + * @param nb_sd value containing the number of entries in the array. + * @param type desired side information type + * + * @return pointer to side data if present or NULL otherwise + */ +const AVPacketSideData *av_packet_side_data_get(const AVPacketSideData *sd, + int nb_sd, + enum AVPacketSideDataType type); + +/** + * Remove side data of the given type from a side data array. + * + * @param sd the array from which the side data should be removed + * @param nb_sd pointer to an integer containing the number of entries in + * the array. Will be reduced by the amount of entries removed + * upon return + * @param type side information type + */ +void av_packet_side_data_remove(AVPacketSideData *sd, int *nb_sd, + enum AVPacketSideDataType type); + +/** + * Convenience function to free all the side data stored in an array, and + * the array itself. + * + * @param sd pointer to array of side data to free. Will be set to NULL + * upon return. + * @param nb_sd pointer to an integer containing the number of entries in + * the array. Will be set to 0 upon return. + */ +void av_packet_side_data_free(AVPacketSideData **sd, int *nb_sd); + +const char *av_packet_side_data_name(enum AVPacketSideDataType type); + +/** + * @} + */ + +/** + * @defgroup lavc_packet AVPacket + * + * Types and functions for working with AVPacket. + * @{ + */ + /** * 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 @@ -603,8 +693,6 @@ int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t* av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size); -const char *av_packet_side_data_name(enum AVPacketSideDataType type); - /** * Pack a dictionary for use in side_data. * -- 2.42.0 _______________________________________________ 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".