From patchwork Thu Oct 17 08:29:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 15796 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 CBA3B449FA2 for ; Thu, 17 Oct 2019 11:30:46 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BC1CD68A67C; Thu, 17 Oct 2019 11:30:46 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 518D568A660 for ; Thu, 17 Oct 2019 11:30:40 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id z9so1259652wrl.11 for ; Thu, 17 Oct 2019 01:30:40 -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=gwF6LozavfvAx6l7CqYlgErjzyvEcoWE2+B04uQup28=; b=eW/q7zHM3jMeKd7Y4sZSckMdBrn0S8H1+QQAEWGP5oqv+rPyrYvYDRJ5WfRyhRNg+0 UHjfslXSuBzP7UiIDDYDaWZ15E0Ky4yJO5x+TqmKtXbBk1Pt4y40hreIMd0+7KSvQNWS 7vuWmLh4A8AiiYwqLS9E9z5krV5Z0vrihoexhCLvAKSgLIJhtdG0nNrePturAugCciCe /wNff3S43NzS8MW8l6eEOOtkHQJ5YFCza3WCK1svnr9agAaxETcYIJSBKV/yCrSZjH6B TqZYIPHplqNwQ+sxgHCLdkFjPZ8poAJvDd7q+MT3n963rJS9J5mPTt4RYRt4ug0fgWO0 sjig== 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=gwF6LozavfvAx6l7CqYlgErjzyvEcoWE2+B04uQup28=; b=LxVhWiCcDYqqO1CHVeLRQaf6agdFIH93eC/EfZ4bQWYy7NK3+IEvbolPsYVuhN7udr 1iTXPeG4Lx+RowAFF+ViO1E2Y6zQ00Uz0hJbT8RUZvASeJ2O9D9T9sk/PHiFHZSWgCXI S36O4otNadDtphxJI2hZ6pA4+quHwVwZTp2FIjHiu5BI1oNkN4qlojcVtx1dNw+p7tUP zcsTuokRxDf65JKxHRm9Sx0eZPJhIsLVSGccTHuRLYj5NJAu/ofnlMmbwF/eetTTtYG4 HiYDBS/XVdDs/nuDBJ4/z/KXRjJta4P3VrMcfnEc7oVGa8PwD9t7T8FhkrbS6DZ3E4Ww LJLg== X-Gm-Message-State: APjAAAXzjzFBEr6B5apDniDFaEc+uWS1WdhVm/C3fsCgZ8Ihip2SWPNR tpxNjEiwRJ25c9V58Um1zhcJdnco X-Google-Smtp-Source: APXvYqwTuPZRNgdQmdfpsU96W7QTACJKwIAdcpOF/kIbYrOzRPIqd1KqxQ1V3DDTqMHw3qRUgZjCaw== X-Received: by 2002:adf:ecd0:: with SMTP id s16mr1860913wro.65.1571301038916; Thu, 17 Oct 2019 01:30:38 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id l7sm1369273wrv.77.2019.10.17.01.30.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 01:30:38 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Oct 2019 10:29:33 +0200 Message-Id: <20191017082945.13534-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191017082945.13534-1-andreas.rheinhardt@gmail.com> References: <20191017082945.13534-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/15] h264_mp4toannexb: Switch to GetByteContext to read extradata 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" This is done in order to improve legibility. No functional change is intended with this commit at all; in particular, the unsafe read functions are used throughout as h264_extradata_to_annexb already performs its own checks. (These checks will nevertheless be improved in further commits.) Signed-off-by: Andreas Rheinhardt --- libavcodec/h264_mp4toannexb_bsf.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index bbf124ad04..f31902b506 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -27,6 +27,7 @@ #include "avcodec.h" #include "bsf.h" +#include "bytestream.h" #include "h264.h" typedef struct H264BSFContext { @@ -68,18 +69,25 @@ static int alloc_and_copy(AVPacket *out, static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) { H264BSFContext *s = ctx->priv_data; + GetByteContext ogb, *gb = &ogb; uint16_t unit_size; uint32_t total_size = 0; uint8_t *out = NULL, unit_nb, sps_done = 0, sps_seen = 0, pps_seen = 0; - const uint8_t *extradata = ctx->par_in->extradata + 4; static const uint8_t nalu_header[4] = { 0, 0, 0, 1 }; - int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size + int length_size; s->sps_offset = s->pps_offset = -1; + bytestream2_init(gb, ctx->par_in->extradata, ctx->par_in->extradata_size); + + bytestream2_skipu(gb, 4); + + /* retrieve length coded size */ + length_size = (bytestream2_get_byteu(gb) & 0x3) + 1; + /* retrieve sps and pps unit(s) */ - unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */ + unit_nb = bytestream2_get_byteu(gb) & 0x1f; /* number of sps unit(s) */ if (!unit_nb) { goto pps; } else { @@ -90,10 +98,10 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) while (unit_nb--) { int err; - unit_size = AV_RB16(extradata); + unit_size = bytestream2_get_be16u(gb); total_size += unit_size + 4; av_assert1(total_size <= INT_MAX - padding); - if (extradata + 2 + unit_size > ctx->par_in->extradata + ctx->par_in->extradata_size) { + if (gb->buffer + unit_size > gb->buffer_end) { av_log(ctx, AV_LOG_ERROR, "Packet header is not contained in global extradata, " "corrupted stream or invalid MP4/AVCC bitstream\n"); av_free(out); @@ -102,11 +110,10 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, const int padding) if ((err = av_reallocp(&out, total_size + padding)) < 0) return err; memcpy(out + total_size - unit_size - 4, nalu_header, 4); - memcpy(out + total_size - unit_size, extradata + 2, unit_size); - extradata += 2 + unit_size; + bytestream2_get_bufferu(gb, out + total_size - unit_size, unit_size); pps: if (!unit_nb && !sps_done++) { - unit_nb = *extradata++; /* number of pps unit(s) */ + unit_nb = bytestream2_get_byteu(gb); /* number of pps unit(s) */ if (unit_nb) { s->pps_offset = total_size; pps_seen = 1;