From patchwork Thu Feb 3 18:44:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34103 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp2741235iov; Thu, 3 Feb 2022 10:46:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4XoBdPMQtLvDEDM2toseWxuSKgCXP5GF///JJw/w+ZbO+WlC6UBjMmlS6METyopw8ZB7I X-Received: by 2002:a17:907:1c94:: with SMTP id nb20mr30669684ejc.144.1643913975041; Thu, 03 Feb 2022 10:46:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643913975; cv=none; d=google.com; s=arc-20160816; b=xnUz943pFkg5t26JvX953YMEtNkVVp8sOXiM6BMVZUdYp5A7afW0XjZRXKypunRWyC TXf1a/Xy6I8cd3mDboPFPaRKQ9MU5t5VReNoWCcJzWT+ttMEJYJAsSXhX4irCFNuMDkg wEr44C5V2LJq5AzrNk0u6hCm6Da/cCC0v7eeUyy2y2ZRThEhwn+16XTsx3CIst10CfCs L1c2COqSlEEEbbO0Dh5qLneQbuH1xLRAyTTbSGWL3yXVVZG+56u7EP21vGESJFIHuF+I vu/OYsR8c3cXVfz6IAQ8HJ4VMOWiSENxwri7MLO0eC0w4bTZQ8fOjY/zPUwPJg6VaIKL GhJA== 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=JNJlWV4ddWSffqpp00W67EU+gtTsrz29DSviJxasJlc=; b=SQT3iAjO7xpdTaqV71CAq/93BDUXOBoHJfVeT6mUPJPQrTbOaPNZ3zkTeIfDvGVrSc 8rKLtvONLWhDRTYpkAD7mUJYXChqNQj6Ew3lcMDU513DYD3yHCM8n0w1WsvajH/Us3ML EcR/MPHJyWMHkKPCvFea8vrb9JpWRTqFxF0D1M1qLKJfoSH91qyJl3sUX36iM6CwNoWL NFEaZ7j7AGAhYRA6pCnkwcvJ7eTDKIL3Gm7/dnTB8NbzQv8yolz6T91XDihTvalBE/ZZ EYiUkWKzWvI1ltryGJKpjg06i1rxo1EtzovibZ3fwUg6bEbq57OVOQkL9/TUHVVTcrBC zADA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=JFFjdW6D; 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 en5si12493571edb.335.2022.02.03.10.46.14; Thu, 03 Feb 2022 10:46:15 -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=JFFjdW6D; 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 DD34268B143; Thu, 3 Feb 2022 20:45:40 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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-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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 6QmnC4mr2IPz 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; }