From patchwork Wed Oct 4 12:28:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 44139 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4f21:b0:15d:8365:d4b8 with SMTP id gi33csp477021pzb; Wed, 4 Oct 2023 05:29:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHFO6NRlIZZLYL7OXNkYPzi4IsfHnRT4pOV/Tz1fTLgn76+hHvBk7g0RrRJnByCYlb9H6iX X-Received: by 2002:a05:6402:344a:b0:52e:3262:e6c9 with SMTP id l10-20020a056402344a00b0052e3262e6c9mr1813722edc.33.1696422573037; Wed, 04 Oct 2023 05:29:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696422573; cv=none; d=google.com; s=arc-20160816; b=TVBuzq2iG4naHgiwh+tzBgPhH1zeq3HlVrzr1qPAEQ4SyqK79mhfsdOtMRyCd5zvoN DSqN2f/1bKkQCmryajN0v3G/RIf23HiryVAyPpH1bgy/GjhNY+quvuxeuKh/Gb/IGuA2 +BwWrNmS3MeYs0cOnFjBvn3cL+tdb5wFEtAOdDXBgOtTXrb3qYYK5p2sRwLRZ1W/k/eE 2iRmLUs9H6y2c+/RB8wN2tuVMM9IVAeqMHW0hOzaZCIlSx44xu27wLqTLnJh7OsqF9zI Hg2zA3LV3FtY8mMufeaVNIWOqik67h/HvPlCEvinx8Lf5WNfjNMXzLONcU8fG3EHuAbX vl/Q== 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=ZaK9pkzL0Zd4U8e/NSqEySLeIGCq7g5bRK/vBZiYGpc=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=js6qRBiC0X+Zzzjcp3Br6sJwJ89a/41F2zA1dgBs6VaHWSLQRIl9toDIvM0GLw+ca2 v4p3zFF3tBFW3p6txO8Mpo7OTIkyhljkennrPr337Ih6m1Pq4Z8OHa8cxzmgEIr8pJX7 71004xHcs/krkrWyI8qAmqi8tZuuxRB+hAKxgKr0GAS4wsxBdwgYbK9qTwzZdTII1D3n wkAqzdhtmlpGrAfFiP6HbD7OhvnIolPe4cqsnugRyg0jYn9/kl3jJ6uiCeXLJnab13QA dog8I0nd/KAfCEIVptNjvrPH/M0XFQGo87POEZrECvPiRlvIGLrCGhDQd9frTR8asc88 4JGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=kPoq5X8Y; 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 be6-20020a0564021a2600b00537f49644f4si1564263edb.668.2023.10.04.05.29.16; Wed, 04 Oct 2023 05:29:33 -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=kPoq5X8Y; 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 4BF5768CBE6; Wed, 4 Oct 2023 15:29:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EE28C68CA95 for ; Wed, 4 Oct 2023 15:28:53 +0300 (EEST) Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1c60cec8041so14329205ad.3 for ; Wed, 04 Oct 2023 05:28:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696422532; x=1697027332; 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=R3Zm6r4etpZyCssLTm4cVEu136ubR+ksEzZ1kCN6qYs=; b=kPoq5X8YIS/bQkoSKVZRD9dGZiUZhATvvHop/+a0JzrhjpaxaOVvY6DML+AAiV6ToP ItELfKzyjBjS5E9Zt2s87HZYAWU4MYu40A9tdbxyT07o175739QW6o58hg3NoEC8PIl2 chFA0vSnraRZFt1J8xg6gFHjG3L0wEjuq/2aD7pyHNFn0Y5RaYG3HefAwDJFfXwSheLf CP53h1HgRnW0o7QufoTOYCVF3taKak368QigP2x+p/E2+bbvV7u6S8pXN/HkwzHuQmcn h+hDrSYTOevJ2w3i2Y83lVOuvIHFHh0gEfz738A01yW2TGOFkxdaFG+bZs/ZoZd/oPf2 MMFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696422532; x=1697027332; 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=R3Zm6r4etpZyCssLTm4cVEu136ubR+ksEzZ1kCN6qYs=; b=pKrNgg4aq+Qn+mfZRj3fATEXgndSYgTUOgsAiUE3emsAWc+fw8Q5B0zdhlzjs12+1L gmjXlGQlGakv2h8dKg8tiXKpLXAv7LgPNHbvfvyPf3GG+A78bALi1GUGAbuN0zKUyyD9 aBth5gOYOrJLVHbOskyrh7aKoj2YO82r/9/WHEns/vK7L3hfmwOZChDZ9rYemN6NM+1F eeJPdhVRoU8ABKnKWG3lYqZYAsw8J7tB3uDm9SJjuJIALtvtL6Iwx7JlUVwfA4bGt36H dp5g135rtlhb42z4cxtX1S33CwLSAmCtcu9qlH0eTGK57Z3ccNTnV4UbHi2ZP6vlQDYo y/nA== X-Gm-Message-State: AOJu0YwbjDulSD79V8ZOkYdBv8foxL58SkQzGBqACrl4d6a+5MbsesHO EhZ9tlo/mNbvZlQmw/evmXUKBrRsKbg= X-Received: by 2002:a17:902:e5c8:b0:1c7:6200:bc7d with SMTP id u8-20020a170902e5c800b001c76200bc7dmr2536975plf.52.1696422531784; Wed, 04 Oct 2023 05:28:51 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id h11-20020a170902748b00b001c57aac6e5esm3551831pll.23.2023.10.04.05.28.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 05:28:51 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 4 Oct 2023 09:28:39 -0300 Message-ID: <20231004122849.56604-2-jamrial@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231004122849.56604-1-jamrial@gmail.com> References: <20231004122849.56604-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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: YCLmkXJmkbAb 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 | 101 ++++++++++++++++++++++++++++++++++++++++++ libavcodec/packet.h | 96 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 193 insertions(+), 4 deletions(-) diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 9ec1feb068..f29a205af2 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -646,3 +646,104 @@ 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 == 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; + uint8_t *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; + + memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE); + + 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. *