From patchwork Tue Feb 1 21:20:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34046 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp983142iov; Tue, 1 Feb 2022 13:21:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwAZkf1FUwVqnyQadxEni9IZtDOOeRKu6NXntvewbW4ZZLjEW76zUWyNjqmdjy8ji+ZZzwy X-Received: by 2002:aa7:df16:: with SMTP id c22mr27270945edy.177.1643750496390; Tue, 01 Feb 2022 13:21:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643750496; cv=none; d=google.com; s=arc-20160816; b=VBnZvv9KrNL+X2m7qGagFz1YXu5fBvVZlEm3xk+YRCO+sTIyPYVMC8qqxbJuw1avZm GLM5lVyjevPSUk2yztQ5jXlGY6r4I6MR9q2Kv0qHZZwa3B67AFRfVh0GJUJEG7zTIq71 0mfybvNpRwux4WnKfiGMQu2a7mixGPptPJKacv2cXyTZpacCog2mID+i4QVi7ag54Fyb AaqetImNVR9Oq4p4q6ncPdw8uG3rspokAldAkzCPNr8uC3/DYJjNW6BIIqRMYd6rCSW4 2vNhX286C/pCWmndcSvfYF4p/sOxb7J3tS9D1g0tfIQoz2fGhYkTcOSekG/0Vl2RkJC3 11KQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=iK3gCIkjua0MWVkRZaC4hUjAO8yNrJD5ZAIFxMPpCok=; b=YzpWF79DSbEgh0Bh6ICJOy5DbZteH3KVqmeu1iJA6m6TAduX0eQ7h6QENbdJmY3F5n j3j7je8vlCghq38SaUYOK3qdMVMvjdPQb0PZcyb9YH4Oj9zg3c/72Z0rFbEjR1TvS2Va 3DwtYhb5KfFt/ko8tPIjpFtyS5I+QGdtU58gNtQFxnGNN4e5atheGJyd96PFgP5u9F/d /MV5iKx9tbJlLcm6njXWB4VMb6H5PW8lqS4SLkJ+7GMhVMLQzTPjbewOoInndsrsNcxt 7Y8kRgVEIdhCMFK/St+VOozi0lfveMJP57soUIM7CyckTugzWUq/V6DTbOkJIC/ljnRF 33gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Fec50Poo; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hd40si10418086ejc.568.2022.02.01.13.21.35; Tue, 01 Feb 2022 13:21:36 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Fec50Poo; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 16B6168AFF0; Tue, 1 Feb 2022 23:21:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2377868AF51 for ; Tue, 1 Feb 2022 23:21:09 +0200 (EET) Received: by mail-qt1-f179.google.com with SMTP id r14so15641757qtt.5 for ; Tue, 01 Feb 2022 13:21:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MYRFh3Do6ITTaR9nSjPNKOI/Rvooyr5jCtxMJSTvGl4=; b=Fec50Poo5cJ2hOQ/gcHnPhd0gvhgnT0VIv2J42Kg6OGHB4cZd2IQ7HJmlbwe5/y/ac EsBI9E2CatfAiTPQaiuXhofxobxXP2LmQWaInN1lfd1bupf8456Cq82rizmJbGGtip8o CYR5A6A5qe0YJ7twyZS9u9wMNwaECafzXs4SMoVVu9oZAxPisTdr+VBYsQGMhzPUCgwW GKgG/+5aUKidJdLX0B4ZJEespZwKm+VZjXAq7v0dLYNHhtMXUux8tgCgoaRE9XWONy1v GiEeTpOxx3E2fuLTSJ11+6IEjRVpOxpjV69Loi9ERnx1hEWmOOO1mX2Tnu/D5s5EhemS N0jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MYRFh3Do6ITTaR9nSjPNKOI/Rvooyr5jCtxMJSTvGl4=; b=hEITZjN5Y8tP1HNzKS17aCuH3s/NwmOe8sTBFrJocISc3Ch7gvjkFLO83mYm0qnK+r uXg6n7JLGTKHaiybQZDtUOSipOm+quiybhBkw3kibsw/mRuLTx4ShNebCdaI8Lk/2KB4 9TaqKqqVd9iwVxcmiXZ2ma2WU8EnY893hOEpUyI3uRGjXg7MRdrPw3G4jgMU23MydJxr e8ZEPe2dIEbcMxWjVUNAYzKl12dnz37h2VuDGHHuyVdqL4yMi1DYMdMIK6vM03M433ua e/qt8a7L4zF8QXidZlo/B9uj7THWTj2yQtmCmAfedYZnomoeuQLEuDIyTJuzi0hssBfe 4M5w== X-Gm-Message-State: AOAM532iJamKI5xnm+gksyd2rHZwntcZ0Ybcc/yqNfZpNvuBrB/vRrI+ 4K68gWEhhNcfAq/F2bqOKuMBgsetaZ/ZrQ== X-Received: by 2002:a05:622a:199e:: with SMTP id u30mr15386582qtc.564.1643750467576; Tue, 01 Feb 2022 13:21:07 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id ay18sm2928225qkb.8.2022.02.01.13.21.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 13:21:07 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 1 Feb 2022 16:20:50 -0500 Message-Id: <20220201212056.29712-3-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220201212056.29712-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/8] avpriv_find_start_code(): rewrite while loop and add comments for clarity X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Scott Theisen Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: scgM6xVf7MZa The expected number of iterations may increase by one for an input of alternating 0 and 1 bytes. Instead of incrementing by 2 everytime, it now alternates between incrementing by 1 and by 3. No functional change, but now much clearer. --- libavcodec/utils.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/libavcodec/utils.c b/libavcodec/utils.c index cb4437edc2..882f90be79 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -957,12 +957,26 @@ const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, return p; } + /* with memory address increasing left to right, we are looking for (in hexadecimal): + * 00 00 01 XX + * p points at the address which should have the value of XX + */ while (p < end) { - if (p[-1] > 1 ) p += 3; - else if (p[-2] ) p += 2; - else if (p[-3]|(p[-1]-1)) p++; + // UU UU UU + if (p[-1] > 1) p += 3; // start check over with 3 new bytes + else if (p[-1] == 0) p++; // could be in a start code, so check next byte + // this should be one comparison against 1 since p is unsigned, + // i.e. p[-1] == 0 is equivalent to p[-1] < 1 + + // UU UU 01 + else if (p[-2] != 0) p += 2; // we have UU YY 01, so increment by 2 + // to start check over with 3 new bytes + // UU 00 01 + else if (p[-3] != 0) p += 3; // we have YY 00 01, so increment by 3 + // to start check over with 3 new bytes + // 00 00 01 else { - p++; + p++; // p now points at the address following the start code value XX break; } } @@ -972,7 +986,8 @@ const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, // this will cause the last 4 bytes before end to be read, // i.e. no out of bounds memory access occurs - *state = AV_RB32(p - 4); // read the previous 4 bytes + *state = AV_RB32(p - 4); + // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1} return p; }