From patchwork Wed Mar 3 23:17:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 26081 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 0276544A663 for ; Thu, 4 Mar 2021 01:17:58 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C8F16688050; Thu, 4 Mar 2021 01:17:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9383D68A3C3 for ; Thu, 4 Mar 2021 01:17:51 +0200 (EET) Received: by mail-qk1-f182.google.com with SMTP id 130so11311688qkh.11 for ; Wed, 03 Mar 2021 15:17: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=3bedo1ZF5BuLFbsI9zOqRMWZdToCU/UQ+S1Nj9xJRiA=; b=I5c066pkWQKQ9DOzYAHlmMZ7oQ3UC9P5Kddn/PL3JomgouxBsEbrnRjz3IwpOZ4+4O GuhK+nDgWrD//CcCfmDFizhyAoYEQp0L0DcoqNFZM9SgWJrjSiGPO70REPmpW59KIcjU Th+THyYHNjF/DiTLYCpYpMdoBZNft0YCsCiXZK/FhA5A3+3U3r/tavB1omMdjpwvpMxx kSp9olVjUHrKs3cHncVj8CBs7r1u6TeY/5NujINftqoxh+oRVP/FFcBV9gTBEsMPHDlQ FtZjthBY1ksjNwzid+36M+C7yuO68VLqKgydWoPwjhI+dz3HMkw8oyygz1NnFTYbp0Bh CB2g== 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=3bedo1ZF5BuLFbsI9zOqRMWZdToCU/UQ+S1Nj9xJRiA=; b=ujsvZwmXF5HDpctV+baQsquNMYhcrIVuKHae4SyiczwWvrVXkh8l9kZVuYrL2WDx8X QIyl42BV+Ei9hWqP82lg40ZK4XIz74K2TYdF5/HUlda00buJ2CAF5EprO0LwI1stTAkM d8+aswU+CEoDPGtdm07RBGyq+LkRi4A7RcrIhDOZulLcIqYAqFyeXyCbgqJmF1jGj8UB J7EMXDbnqoZ38LfgRekV7iLUH3A0zmO+Jnpp+CUVRlXq880np9QoHlV2QywlHG0KjPt7 2X/SDXnEJGaP7R9F/XN6ecQxzkEfi75tX+CED7bIe2huOldRJFa/mfDprz5KeH8eRe6y KgJg== X-Gm-Message-State: AOAM532NS7js1g0Wges5V3S580K7soIoptmPZqL2kzsKGFHCs0pNh1ML 3wyq0K2ivyVod7UmFZDbAQv8aaoYkAY1uQ== X-Google-Smtp-Source: ABdhPJzc6wCzZIrrGyjwcDkGlko7aRMWTu95lAkxdHrCTySzbt3VCSqNzTSi2haHWrwTyFiWRcg/Zw== X-Received: by 2002:a37:a8a:: with SMTP id 132mr1452298qkk.463.1614813469453; Wed, 03 Mar 2021 15:17:49 -0800 (PST) Received: from localhost.localdomain ([181.23.89.132]) by smtp.gmail.com with ESMTPSA id n67sm18465690qkb.35.2021.03.03.15.17.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Mar 2021 15:17:48 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Mar 2021 20:17:36 -0300 Message-Id: <20210303231736.13225-1-jamrial@gmail.com> X-Mailer: git-send-email 2.30.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/decode: use the context's internal buffer_pkt in avcodec_decode_subtitle2() 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" This removes stack usage of AVPacket, and ensures proper handling of packet references. Signed-off-by: James Almer --- libavcodec/decode.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 5e5d0c7066..ae1043b985 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -877,11 +877,10 @@ static int recode_subtitle(AVCodecContext *avctx, int ret = 0; char *inb, *outb; size_t inl, outl; - AVPacket tmp; #endif if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) - return 0; + return av_packet_ref(outpkt, inpkt); #if CONFIG_ICONV cd = iconv_open("UTF-8", avctx->sub_charenc); @@ -896,12 +895,12 @@ static int recode_subtitle(AVCodecContext *avctx, goto end; } - ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES); + ret = av_new_packet(outpkt, inl * UTF8_MAX_BYTES); + if (ret < 0) + goto end; + ret = av_packet_copy_props(outpkt, inpkt); if (ret < 0) goto end; - outpkt->buf = tmp.buf; - outpkt->data = tmp.data; - outpkt->size = tmp.size; outb = outpkt->data; outl = outpkt->size; @@ -911,7 +910,6 @@ static int recode_subtitle(AVCodecContext *avctx, ret = FFMIN(AVERROR(errno), -1); av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" " "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc); - av_packet_unref(&tmp); goto end; } outpkt->size -= outl; @@ -1040,20 +1038,21 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, get_subtitle_defaults(sub); if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) { - AVPacket pkt_recoded = *avpkt; + AVCodecInternal *avci = avctx->internal; - ret = recode_subtitle(avctx, &pkt_recoded, avpkt); + av_packet_unref(avci->buffer_pkt); + ret = recode_subtitle(avctx, avci->buffer_pkt, avpkt); if (ret < 0) { *got_sub_ptr = 0; } else { - ret = extract_packet_props(avctx->internal, &pkt_recoded); + ret = extract_packet_props(avctx->internal, avci->buffer_pkt); if (ret < 0) return ret; if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE) sub->pts = av_rescale_q(avpkt->pts, avctx->pkt_timebase, AV_TIME_BASE_Q); - ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded); + ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avci->buffer_pkt); av_assert1((ret >= 0) >= !!*got_sub_ptr && !!*got_sub_ptr >= !!sub->num_rects); @@ -1091,14 +1090,6 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, break; } } - - if (avpkt->data != pkt_recoded.data) { // did we recode? - /* prevent from destroying side data from original packet */ - pkt_recoded.side_data = NULL; - pkt_recoded.side_data_elems = 0; - - av_packet_unref(&pkt_recoded); - } } if (*got_sub_ptr)