From patchwork Thu Mar 4 15:42:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26092 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 9C7D1449EC7 for ; Thu, 4 Mar 2021 17:43:16 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 868F568AB95; Thu, 4 Mar 2021 17:43:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3F72568AB3A for ; Thu, 4 Mar 2021 17:43:08 +0200 (EET) Received: by mail-ej1-f41.google.com with SMTP id r17so50205964ejy.13 for ; Thu, 04 Mar 2021 07:43:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=50w3qM06uMvk63JA16dzbYb0KGfzaMooQ/t2xDiPj/c=; b=m6StDpGnhUZ2HopfE3r3FiixS3R5ztE7TtbMuVI5Cm6gGAIEPYL8Va6nZJJ8mQVqum EVhUHzEvVcgbkpJa90CI4Rq2ZOKcgiqCZbJrp82TSg/1O/5/1e3u25HX73syZn3nDV/s 8GWgxCDd+jW9xxLtx3CCBaE8pbu8j587+L0J2RoxKeEiSdpJwmsWJWBR/rCzJ5mo41Us KMWi4eZ33JMQLqOa8oycUuqQrJgGsBJwBu8+J8x+11sYLpnYr2N61KZ5RMkZcfWvFHbr AaeRV9BNjjIi6AZ7fk0xD4N5OBaVyqfMQtRDdfcqHpDkrwYE4pEa0I7aee+3qDY1F66d ZZEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=50w3qM06uMvk63JA16dzbYb0KGfzaMooQ/t2xDiPj/c=; b=Z+V0hKViJHLCqwDwX6uDFGAo4f7sMtS5JBL07hvxzUKsZm8vvprir2nic1ycsFIf0j UwlOdKwK2gYVXlF77Lb/3wMlak56UdcXseHeNYZ0qOYVDf6nQRXS2wgbZzjFU0cewu92 Tpg9w5SJHSVqqsgSfxw1WbjiOF0rNVyInTSgd6Tw7Yn4mJAXgUDOSlsFkg/20dCjR4p9 hNRAKjrUAxtwKrLGwxYhnmmGYBLeOivpE4FTaz54RRzsfHfrji83bJUWoLX0vZuEakGw ArBI8O7W7BcrkXb7tbaVFkWz3rrX/eo0JqLdpQmnZWzKeMTu5H1Of+MjVUhp3s+rb+Kr clFw== X-Gm-Message-State: AOAM53385JHCD/NAVU3QGzUFllgaPn7yQxNJgq2/E2MGsnbHrLm3BC4L 0Neh8rCQy9qb69kpzbi3IJ3u66JBoRI= X-Google-Smtp-Source: ABdhPJyxkjEj1Eut6OmIT9G3lanS+vwYGNH79ou5ZvsrP74+99fe84BWXyOwXQUcKRDT2/pIfW4itQ== X-Received: by 2002:a17:906:260a:: with SMTP id h10mr4992141ejc.392.1614872587464; Thu, 04 Mar 2021 07:43:07 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id y24sm22673391eds.23.2021.03.04.07.43.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 07:43:07 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 4 Mar 2021 16:42:30 +0100 Message-Id: <20210304154233.934640-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210304154233.934640-1-andreas.rheinhardt@gmail.com> References: <20210304154233.934640-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/8] avcodec/decode: Avoid stack packets when decoding subtitles 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Use AVCodecInternal.buffer_pkt (previously only used in avcodec_send_packet) instead of stack packets when decoding subtitles. Also stop sharing side-data between packets and use the user-supplied packet directly for decoding when possible (no subtitle decoder ever modifies the packet it is given). Signed-off-by: Andreas Rheinhardt --- Is it actually intentional that even in case of invalid UTF-8 got_sub_ptr is not reset and frame_number incremented? libavcodec/decode.c | 57 +++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index c976795311..84c4039836 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -869,19 +869,20 @@ static void get_subtitle_defaults(AVSubtitle *sub) } #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */ -static int recode_subtitle(AVCodecContext *avctx, - AVPacket *outpkt, const AVPacket *inpkt) +static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt, + const AVPacket *inpkt, AVPacket *buf_pkt) { #if CONFIG_ICONV iconv_t cd = (iconv_t)-1; 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) + if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) { + *outpkt = inpkt; return 0; + } #if CONFIG_ICONV inb = inpkt->data; @@ -895,28 +896,31 @@ static int recode_subtitle(AVCodecContext *avctx, cd = iconv_open("UTF-8", avctx->sub_charenc); av_assert0(cd != (iconv_t)-1); - ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES); + ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES); + if (ret < 0) + goto end; + ret = av_packet_copy_props(buf_pkt, inpkt); if (ret < 0) goto end; - outpkt->buf = tmp.buf; - outpkt->data = tmp.data; - outpkt->size = tmp.size; - outb = outpkt->data; - outl = outpkt->size; + outb = buf_pkt->data; + outl = buf_pkt->size; if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 || iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 || - outl >= outpkt->size || inl != 0) { + outl >= buf_pkt->size || inl != 0) { 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; - memset(outpkt->data + outpkt->size, 0, outl); + buf_pkt->size -= outl; + memset(buf_pkt->data + buf_pkt->size, 0, outl); + *outpkt = buf_pkt; + ret = 0; end: + if (ret < 0) + av_packet_unref(buf_pkt); if (cd != (iconv_t)-1) iconv_close(cd); return ret; @@ -1039,20 +1043,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; + const AVPacket *pkt; - ret = recode_subtitle(avctx, &pkt_recoded, avpkt); + ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt); if (ret < 0) return ret; - ret = extract_packet_props(avctx->internal, &pkt_recoded); - if (ret < 0) - return ret; + ret = extract_packet_props(avctx->internal, pkt); + if (ret < 0) + goto cleanup; 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, (AVPacket*)pkt); av_assert1((ret >= 0) >= !!*got_sub_ptr && !!*got_sub_ptr >= !!sub->num_rects); @@ -1091,16 +1096,12 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, } } - 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) avctx->frame_number++; + + cleanup: + if (pkt == avci->buffer_pkt) // did we recode? + av_packet_unref(avci->buffer_pkt); } return ret;