From patchwork Sat May 30 16:05:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 20008 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 B179744A43F for ; Sat, 30 May 2020 19:09:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4DB8A68B0C4; Sat, 30 May 2020 19:06:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 721FD68AFCC for ; Sat, 30 May 2020 19:06:36 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id r9so6704857wmh.2 for ; Sat, 30 May 2020 09:06:36 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=03LV6CdqXn3cycMllnn5XWSLbWS8cJQ/I9wSuE7Q1uY=; b=s1FVI044dnskMRP0JBThDe7wT563gpfdEOCSrxRBNPPQFyINVDQtqGEnNH188jFO2t vfMNwt0V7mILe3hFSxn82UQUHBv6+8B0Ukf3tirIOwFkZ8OuasQKR0GvYSpMlgGLoAx+ PneIkAabLSDP+/p4kkIOc6Kbq59E2/YMKeKh7PigEGqXAQ+ls2eSx0BjGlst6lcZcLAe AoI4z//xr/tcrXQnacfcKbcPQHc+VBKFaGZ3zqMnq6P4cX38YCl9QFFpHShq3hVCd1b9 egJeZ5HfrxqEa2fLqYk4pZcd1ZV8fRpc7erPzhCyS+RvTXoef6dzsFkVMYWESY5GOxby cJlg== 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:mime-version:content-transfer-encoding; bh=03LV6CdqXn3cycMllnn5XWSLbWS8cJQ/I9wSuE7Q1uY=; b=TBgevvs6iU41Qzsa86gtx9UQ5KcK1r+/tc28Sc6FP7l+A9u7OSCjzgvhoRkYpgnJrF aqXc1BTqe6RAK3KNdfeay+U8paFuwLT75+4NO9GFcERfTEKwJypFb0203t6hC+Sr2Uea RMsgfLC0rWF0/NayqJbsTYBR3/EbjdP7Ub//JwevF1kRsePENAmJlWrhtBGe1txvS8fg SBdyU22bjaMB95ShEfJf99dl4B1dwTnlYsYtiHNOOfrsnwZURdpHrT5J6W8VR5BuTzBd mLB1YSVtyyGHvp07a+ldX0+UU08mhz6kXwauz5wpB9fSzw9LeBci8bD23j4yYgorpfsQ awIQ== X-Gm-Message-State: AOAM531UaXdWjT1tr6/76mot7qzhzONrQqQkG4fYCIJWd8Usy/5YFA/T wHOyK3N8DUDqzkjT0TdME6W9yigF X-Google-Smtp-Source: ABdhPJzRpWbTU4t5ePSYv3TlukI81v58I9mK5DsouuZnU74M1zkb7vcX5uhaU01xlwkWJQqqrJ8+8w== X-Received: by 2002:a1c:b7d5:: with SMTP id h204mr14374015wmf.39.1590854795619; Sat, 30 May 2020 09:06:35 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id v27sm15186517wrv.81.2020.05.30.09.06.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 May 2020 09:06:34 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 30 May 2020 18:05:25 +0200 Message-Id: <20200530160541.29517-20-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200530160541.29517-1-andreas.rheinhardt@gmail.com> References: <20200530160541.29517-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 20/36] avcodec/hevc_mp4toannexb_bsf: Stop reallocating output buffer 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" Instead parse the input packet twice: Once to get the size of the output packet to be allocated (and to check the input packet for consistency) and once to actually copy the data. Not reallocating the output buffer also means that one can now use a PutByteContext for writing; it improves readability. Signed-off-by: Andreas Rheinhardt --- libavcodec/hevc_mp4toannexb_bsf.c | 43 +++++++++++++++++++------------ 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/libavcodec/hevc_mp4toannexb_bsf.c b/libavcodec/hevc_mp4toannexb_bsf.c index 634bfe9c46..3776d5b279 100644 --- a/libavcodec/hevc_mp4toannexb_bsf.c +++ b/libavcodec/hevc_mp4toannexb_bsf.c @@ -119,10 +119,9 @@ static int hevc_mp4toannexb_init(AVBSFContext *ctx) static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) { HEVCBSFContext *s = ctx->priv_data; + PutByteContext pb; AVPacket *in; - GetByteContext gb; - int got_irap = 0; int i, ret = 0; ret = ff_bsf_get_packet(ctx, &in); @@ -135,12 +134,17 @@ static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) return 0; } + for (int pass = 0; pass < 2; pass++) { + GetByteContext gb; + uint64_t out_size = 0; + int got_irap = 0; + bytestream2_init(&gb, in->data, in->size); while (bytestream2_get_bytes_left(&gb)) { uint32_t nalu_size = 0; int nalu_type; - int is_irap, add_extradata, extra_size, prev_size; + int is_irap, add_extradata, extra_size; if (bytestream2_get_bytes_left(&gb) < s->length_size) { ret = AVERROR_INVALIDDATA; @@ -162,21 +166,28 @@ static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) extra_size = add_extradata * ctx->par_out->extradata_size; got_irap |= is_irap; - if (FFMIN(INT_MAX, SIZE_MAX) < 4ULL + nalu_size + extra_size) { - ret = AVERROR_INVALIDDATA; - goto fail; - } - - prev_size = out->size; + if (!pass) { + out_size += extra_size + 4ULL + nalu_size; + bytestream2_skipu(&gb, nalu_size); + } else { + if (extra_size) + bytestream2_put_bufferu(&pb, ctx->par_out->extradata, extra_size); + bytestream2_put_be32u(&pb, 1); + bytestream2_copy_bufferu(&pb, &gb, nalu_size); + } + } - ret = av_grow_packet(out, 4 + nalu_size + extra_size); - if (ret < 0) - goto fail; + if (!pass) { + if (out_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) { + ret = AVERROR(ERANGE); + goto fail; + } + ret = av_new_packet(out, out_size); + if (ret < 0) + goto fail; - if (extra_size) - memcpy(out->data + prev_size, ctx->par_out->extradata, extra_size); - AV_WB32(out->data + prev_size + extra_size, 1); - bytestream2_get_bufferu(&gb, out->data + prev_size + 4 + extra_size, nalu_size); + bytestream2_init_writer(&pb, out->data, out->size); + } } ret = av_packet_copy_props(out, in);