From patchwork Sun Mar 14 02:27:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 26389 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 68A4C44B4EA for ; Sun, 14 Mar 2021 04:27:58 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3FA8C68ACD7; Sun, 14 Mar 2021 04:27:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B4AEA68AC97 for ; Sun, 14 Mar 2021 04:27:51 +0200 (EET) Received: by mail-qv1-f49.google.com with SMTP id a14so6195832qvj.7 for ; Sat, 13 Mar 2021 18:27:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=MheRuKimQpdiFTBtVR9rJLjaGUux93xMGxMubWSjrvc=; b=djI+uqVGhZZzEOUkrnBcIhEoykVmCcrwUx2MD2QiemMT4uEn0H+xxyXvnlUAFf7GtH lty/x78LslEmanPIXqI/MeJzdAKofhz+7/ULtVx+uEb3kD5663eZ0SaEwoDNEdgNSHFs UHBTFMlxdbq6/1sSU4/35/43dhH0ntmtxxvOqVW1BEFrWFtHf95Vxky8CsG4KYeBjzrN ae/YDOdiummTIJst/1zOMohe9yewEhLYUYBLElTwqIBWuE7Bu7VkWssohZ9rZO/ENXyR o9qkn0VBpPhx7ocDCYHpB0GAn4HVpwUs7v7fpCf+8nXGTUzc0ZSGDwqu2Dmp9UsMLtbB +HYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=MheRuKimQpdiFTBtVR9rJLjaGUux93xMGxMubWSjrvc=; b=G+XbqscpgUISNlz4mJ85sby8TcTzdyTrsgaxhIfR9d/4wvEBNatmHIx3tNz6ZGMnPw H+PoMsARAOXUvw8Ym/m4zR6OBwU3Wz9sf7arWQM2Zgi+MEt0luhHXdJFpfMNsO888+vq BcgIj0buy/cZjFvqeUOjbrwPVw3OMpRallWwg3eUro9xlRGDP/nJgN0jxtangFVNdfRS wRaUGS/m/yDWv/mAgM1wL+fDJWzuKOrm4t3vDCZaQlDRLZenA+RT0p8HEEkVZ7JIJPwM RhnL+mYLQeFN2JVWpUeLACu4j8QcnY2JFBPzCFjg2mOfAHFsXUM1a+vME2vfwVR8ocIa mk9A== X-Gm-Message-State: AOAM530WYGZ2W/tJ0PnuemMaPvs+m2HfGRdIua9uwSFiS4UYhdTzcVLw 5yj53XNxA2yXGKhfv7C+zwV/Bw2HdH0= X-Google-Smtp-Source: ABdhPJykOif2sdxmgBzMTvEyyBlOyR1UWRh+sOx0QlveXDg4ykswkDPR4KrCumKo7U5U3UPQzaopvQ== X-Received: by 2002:a0c:ea29:: with SMTP id t9mr18831229qvp.52.1615688869797; Sat, 13 Mar 2021 18:27:49 -0800 (PST) Received: from localhost.localdomain ([181.23.75.214]) by smtp.gmail.com with ESMTPSA id i8sm6941743qtj.16.2021.03.13.18.27.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Mar 2021 18:27:49 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 13 Mar 2021 23:27:14 -0300 Message-Id: <20210314022714.43661-1-jamrial@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/wrapped_avframe: stop using sizeof(AVFrame) X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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" Signed-off-by: James Almer --- Setting pkt->size to 0 but leaving pkt->data pointing to the frame ensures that the packet isn't mistaken for an empty one, and prevents wrong use of the data pointer in case av_packet_make_writable() is called on it (The resulting packet will be the same as if you call it on an empty one). I decided to set the opaque field of the AVBufferRef to the frame pointer so we can do something like ensure pkt->size is 0 and pkt->data is equal to it before trying to treat pkt->data as an AVFrame, but i'm not sure if that could be done now, or only after a major bump (e.g. 4.3 lavf/lavd and 4.4 lavc at runtime, where demuxers like the vapoursynth one propagate packets to the wrapped_avframe decoder, and if the latter does the above check, it would fail). libavcodec/wrapped_avframe.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/libavcodec/wrapped_avframe.c b/libavcodec/wrapped_avframe.c index 85ff32d13a..c921990024 100644 --- a/libavcodec/wrapped_avframe.c +++ b/libavcodec/wrapped_avframe.c @@ -44,32 +44,20 @@ static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { AVFrame *wrapped = av_frame_clone(frame); - uint8_t *data; - int size = sizeof(*wrapped) + AV_INPUT_BUFFER_PADDING_SIZE; if (!wrapped) return AVERROR(ENOMEM); - data = av_mallocz(size); - if (!data) { - av_frame_free(&wrapped); - return AVERROR(ENOMEM); - } - - pkt->buf = av_buffer_create(data, size, - wrapped_avframe_release_buffer, NULL, + pkt->buf = av_buffer_create((uint8_t *)wrapped, 0, + wrapped_avframe_release_buffer, wrapped, AV_BUFFER_FLAG_READONLY); if (!pkt->buf) { av_frame_free(&wrapped); - av_freep(&data); return AVERROR(ENOMEM); } - av_frame_move_ref((AVFrame*)data, wrapped); - av_frame_free(&wrapped); - - pkt->data = data; - pkt->size = sizeof(*wrapped); + pkt->data = (uint8_t *)wrapped; + pkt->size = 0; pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; @@ -87,9 +75,6 @@ static int wrapped_avframe_decode(AVCodecContext *avctx, void *data, return AVERROR(EPERM); } - if (pkt->size < sizeof(AVFrame)) - return AVERROR(EINVAL); - in = (AVFrame*)pkt->data; out = data;