From patchwork Sun Sep 22 18:00:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 51701 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d154:0:b0:48e:c0f8:d0de with SMTP id bt20csp2073924vqb; Sun, 22 Sep 2024 11:09:20 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXCZJ7UdDDayy7pnB1RHspZhsm6MAc4ZaDf2fghU9h+VhkCS0jmi8DGXwqp/PeUDoZkp20eFPgYhFPk2SGV4Pte@gmail.com X-Google-Smtp-Source: AGHT+IE4Pq5Gm6dE5Eoqg0bxmemNhw5GRp6rNO5Va8h65loELrlvnLM+6BIAM7V1pE3OqUIIJCqm X-Received: by 2002:a05:6512:1105:b0:530:daeb:c1d4 with SMTP id 2adb3069b0e04-536acf6ad3fmr4077957e87.12.1727028559839; Sun, 22 Sep 2024 11:09:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727028559; cv=none; d=google.com; s=arc-20240605; b=Vibe8wEhA/9HNbbwUx3pexF+hos+/aU3zYXoFrdtDUSD8gipHsaaCnJeMg7UhJpn3c EBcg9XEY55WJ+4HFNYHByxBIOG1NxoFbYnuiWmVe2OW7nVAqK7tFfrQmI2Lmk/6q56a0 UbdacH/UyszTeCJZme6c2t0LLABm1V8wkozvJOTiJhuI/779DMrCawD6+3V5oUB31U2g /9E9uH8uqh5yteyC4kQmb7DEousd2wg1GM2A0+XQ9cdDJ4tEAJ/HnXoyWcpHfr45XMyh qLQ5SkmniDlr6ygyntjfwAU1R6uPKjdAqaCPi2pJRkv5qFzMFvZU8Uc2pmpN/Rpp20p9 Fu0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=LGPvGYuYFqpmEXGPZrDwmnH6eqdc1vU3wqMdJ1XZMUI=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=Pe/B2kcqD1JyfKA6EgiAYGZTy+235fKDSqJpw87OWqXqsEPPKx8VQoXZN1bJ61p3fk WCRcDGf+9WGNjeB8q85MM49dCFoPGQRBcnDSKPWbZLFnbD1TbBcShgXBMFf7BCtCiT7F U95PicOLe5fOOLxmcL/KEReYiIonX7aO8lGE8TylOLAVsq8Lfik7OvsNMp08NP56EERr /EcP7Pj5LNKJ+Gx1dLEDulTvUkI9tjnfGc4PsUzoAail6iAaLLwHP+halF8wXztlGTDm fteb7f/xsQRcNuaIrQURKrLd4X9LO5+jc0W/IoRudgpXciSEMgd36ygn5etXv/JfsFmN cAGA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b=nXrrNHkm; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 2adb3069b0e04-53687042c51si6127085e87.54.2024.09.22.11.09.19; Sun, 22 Sep 2024 11:09:19 -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=@khirnov.net header.s=mail header.b=nXrrNHkm; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CAE0568DAB0; Sun, 22 Sep 2024 21:00:31 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6FEC268DA01 for ; Sun, 22 Sep 2024 21:00:24 +0300 (EEST) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=nXrrNHkm; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id E65F94DE2 for ; Sun, 22 Sep 2024 20:00:23 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id e1-ZrYkU-6O4 for ; Sun, 22 Sep 2024 20:00:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1727028021; bh=Xo/87TMYLrFrL6UMzkSvhooCMTx16ugv4eS3Ed6ymPo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=nXrrNHkmxkm0QbyJtbNK+EiGxcab0rtdVhxqRKRPzzuoH+zYbU4lM/1eu7XU0EOR0 Z79NcoVARYMdsEUkvNtf60XjLkhVAK487ybgdIE2Ust9sNnrHLFsPPOVRVmDyCDvve Pz4q1y/o5H1uotPAaQUevyKclYpbXkv9GfG9Hoe/uJla6PG80PWOQkUWsPRLcY/BEh oO+mC0bMOuBj3HOKzlc0OGJC3gp1Fm5V42LEy+Y1kr82h8GsjNMN4G/um1426zhdLO fovXOBktxtl4LrjyiKOYmR/aGL5mLiUuyJnewOUxFwME2DueLtBgoUp3ZwF+mxXtTc Ku7tFD/kzt3jw== Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 921994D9D for ; Sun, 22 Sep 2024 20:00:21 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id A31423A086F for ; Sun, 22 Sep 2024 20:00:14 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sun, 22 Sep 2024 20:00:04 +0200 Message-ID: <20240922180005.6549-2-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240922180005.6549-1-anton@khirnov.net> References: <20240922180005.6549-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] lavc/decode: make sure side data mapping does not produce duplicates 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: PYnStHcHe06e Also, deduplicate the code performing the mapping. --- libavcodec/decode.c | 98 +++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 57cca44d05..9303254ef3 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -1400,14 +1400,50 @@ int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt) return ret; } +static const AVPacketSideData* +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; +} + const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx, enum AVPacketSideDataType type) { - for (int i = 0; i < avctx->nb_coded_side_data; i++) - if (avctx->coded_side_data[i].type == type) - return &avctx->coded_side_data[i]; + return packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data, type); +} - return NULL; +static int side_data_map(AVFrame *dst, + const AVPacketSideData *sd_src, int nb_sd_src, + const SideDataMap *map) + +{ + for (int i = 0; map[i].packet < AV_PKT_DATA_NB; i++) { + const enum AVFrameSideDataType type_pkt = map[i].packet; + const enum AVFrameSideDataType type_frame = map[i].frame; + const AVPacketSideData *sd_pkt; + AVFrameSideData *sd_frame; + + sd_pkt = packet_side_data_get(sd_src, nb_sd_src, type_pkt); + if (!sd_pkt) + continue; + + sd_frame = av_frame_get_side_data(dst, type_frame); + if (sd_frame) + continue; + + sd_frame = av_frame_new_side_data(dst, type_frame, sd_pkt->size); + if (!sd_frame) + return AVERROR(ENOMEM); + + memcpy(sd_frame->data, sd_pkt->data, sd_pkt->size); + } + + return 0; } static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame) @@ -1425,17 +1461,17 @@ static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame) int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt) { - static const struct { - enum AVPacketSideDataType packet; - enum AVFrameSideDataType frame; - } sd[] = { + static const SideDataMap sd[] = { { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC }, { AV_PKT_DATA_AFD, AV_FRAME_DATA_AFD }, { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS }, { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE }, { AV_PKT_DATA_SKIP_SAMPLES, AV_FRAME_DATA_SKIP_SAMPLES }, + { AV_PKT_DATA_NB } }; + int ret = 0; + frame->pts = pkt->pts; frame->duration = pkt->duration; #if FF_API_FRAME_PKT @@ -1445,31 +1481,14 @@ FF_DISABLE_DEPRECATION_WARNINGS FF_ENABLE_DEPRECATION_WARNINGS #endif - for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) { - size_t size; - const uint8_t *packet_sd = av_packet_get_side_data(pkt, ff_sd_global_map[i].packet, &size); - if (packet_sd) { - AVFrameSideData *frame_sd; + ret = side_data_map(frame, pkt->side_data, pkt->side_data_elems, ff_sd_global_map); + if (ret < 0) + return ret; - frame_sd = av_frame_new_side_data(frame, ff_sd_global_map[i].frame, size); - if (!frame_sd) - return AVERROR(ENOMEM); - memcpy(frame_sd->data, packet_sd, size); - } - } - for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) { - size_t size; - uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size); - if (packet_sd) { - AVFrameSideData *frame_sd = av_frame_new_side_data(frame, - sd[i].frame, - size); - if (!frame_sd) - return AVERROR(ENOMEM); + ret = side_data_map(frame, pkt->side_data, pkt->side_data_elems, sd); + if (ret < 0) + return ret; - memcpy(frame_sd->data, packet_sd, size); - } - } add_metadata_from_side_data(pkt, frame); if (pkt->flags & AV_PKT_FLAG_DISCARD) { @@ -1490,19 +1509,10 @@ int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame) { int ret; - for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) { - const AVPacketSideData *packet_sd = ff_get_coded_side_data(avctx, - ff_sd_global_map[i].packet); - if (packet_sd) { - AVFrameSideData *frame_sd = av_frame_new_side_data(frame, - ff_sd_global_map[i].frame, - packet_sd->size); - if (!frame_sd) - return AVERROR(ENOMEM); - - memcpy(frame_sd->data, packet_sd->data, packet_sd->size); - } - } + ret = side_data_map(frame, avctx->coded_side_data, avctx->nb_coded_side_data, + ff_sd_global_map); + if (ret < 0) + return ret; if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) { const AVPacket *pkt = avctx->internal->last_pkt_props;