From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 191C440AA5 for ; Thu, 3 Feb 2022 18:46:12 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A7E2768B169; Thu, 3 Feb 2022 20:45:40 +0200 (EET) Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E38C968A9FC for ; Thu, 3 Feb 2022 20:45:32 +0200 (EET) Received: by mail-qv1-f52.google.com with SMTP id e20so3354409qvu.7 for ; Thu, 03 Feb 2022 10:45:32 -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=XIVZ8HK7u2tn2hE58ekTiYEL7VIfeF314J8YecgtM0s=; b=JFFjdW6DVOL4uo8IYsGMBvmNQFr57Sjbjec5UxV9NgJL7AGfSSveEMlct9yA93WmwS 9vL8KnLH8GW25+wfs8/fDZ6hVio6WXhxhAKAfS0Wjanqq+FirwrFo+uzZFYyXX0PRsk2 OJNAOSaX/wE3rfBYKAwnSq3zaeqmHxxUeuFKSY7DrJ8qcVvdHJvHsVyhbuOmclI1YQeq /b7BcBpL/Dknx1hqgk7YtG9mARTPsvoM9Dknno1OyJKjLoF5SXeGmx7KlJrKePBR4zbq 4QenRi0SpmBgIOdVlJxorf0Chnbl57u+6/sC97tQdwCvLZYwSemfr9EvjpKav6rDpOBV PNww== 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=XIVZ8HK7u2tn2hE58ekTiYEL7VIfeF314J8YecgtM0s=; b=Gvmj+bnQUAnBGj+O7LiD8SCbKe8xRjOHB/RFUSLtulx3gIFMLBr3Cmtr+8rG9i960Q YH2Jdt4V6WuMSlCQFoeuBkNJJctC/cnAjRMW338Hqtvn0gWbYBnlcpO40aZf26R24PyY +0Io1ZH7upqM6vjWEYIcPGKOLrejBqWEUbOFEEGBoTnG/9Nfp8OApnHbtf3qSkE41Oo9 jyTo+AX22AUcLobxBILPPSoydsJky/y2csqwaGOvOptGZHzLcUYqmG//q5wJF//zNujL SG/8tdazHvuuhZAwTEJx+o3b8ETsG/zp9zHx0Nh+IGXBNr4q3H5WaMu8g8WvR96TMwJD 49WA== X-Gm-Message-State: AOAM532gQwL3F+C6GzBrSAoLS9HNmLywRupGSm6kIBWbqB5bMg0faR+C FapPSDtpMviQdbW9XPdL4UYWXdbEiuTjKA== X-Google-Smtp-Source: ABdhPJyW6BdGVydgx2/VX9AgTxIPP4bsdRJMYRe2FxJIGtvPzTvNqhcZGLZdQocnTZcC1uJCqn62Ew== X-Received: by 2002:a05:6214:d0c:: with SMTP id 12mr31954388qvh.66.1643913931287; Thu, 03 Feb 2022 10:45:31 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id f16sm5947887qtk.8.2022.02.03.10.45.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 10:45:30 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Thu, 3 Feb 2022 13:44:40 -0500 Message-Id: <20220203184450.5491-4-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220203184450.5491-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220203184450.5491-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 03/13] 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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. For the check p[-2] != 0: Also reduce the number of iterations by correctly starting with three new bytes on the next iteration, instead of keeping byte p[-3] which is invalid, i.e. known to be 01 when it must be 00. --- libavcodec/utils.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/libavcodec/utils.c b/libavcodec/utils.c index cb4437edc2..8f8cc820bd 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 += 3; // we have UU YY 01, so increment by 3 + // 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; } -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".