From patchwork Wed Feb 9 03:28:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34197 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1371636iov; Tue, 8 Feb 2022 19:29:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJywknKaWFODj90gO14K/bOeFOnjGN690dycfdD1S3j3A9J+HcDQ+xHFZW8ihk75JBWzsXY1 X-Received: by 2002:a05:6402:270c:: with SMTP id y12mr450003edd.30.1644377387636; Tue, 08 Feb 2022 19:29:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377387; cv=none; d=google.com; s=arc-20160816; b=uZg4YltBTlCfaP7FWUpzoSYb1ce5OxVxCp1EIwL0KjvhmC10GdQMZZEe9CLa258Vs3 hZO7kAjbeYrA/ePzrkJbT4uh8O2XxD7rqJttCXNt0CdYZObo78gJXV/AbvqpDs60bXWO LGrCTsHeOKEiDhSFZYeemKWSXSqKjRQWlR1zsSoHIizjFDZwAfOPdbvrLpvUc9pvPiVl XJ2DKcoupugIklnfwZ7MGnNMJTKa0t+watTeBtbi6wYD+W+PFfea6pd5pxAiwwMh3rwA NWbwwX3nht4IXpbvPCl9QDSFs2bN2Yn1j7jkbOQHwWQI3tN68LxNv/fOH9flyekaAjTX q2+Q== 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=aGcygFHcu6uAfFsCfYA7+7mfQeLpbXYLKSiKh6oRRcc=; b=TOnKWomwrlox6xT3pkjXOS8XhuQhOni11UaC0QOraIwYu0mzXZy5CirbKRrF83B1my dLyvSJryEMQ9osxGFJOSb5LyhohikDIbqrfKdCX3nxkKUdBEE53rmJiXs1lxX/gEG+zK 4k3zQjSX8EYKsPz8I2Sn/QacXuHz/1Nujf1MWWKWfwq7v7yOsV7nGUIbmYtHYGpnhNlF /SSmrosW9LzIeeYpWCVWDi065277NBCklHTE3Iklgv2pzyss+0T36pclDJUI5aHsGEeA sybgbjg9V6iGfBu4KgKpQ6sDHHBL6j6Ob5bszscE3RSG2zkKkTYfxULJYlg6Nm8iZUnl iPZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=DT76ro5L; 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 hz1si4722282ejc.474.2022.02.08.19.29.47; Tue, 08 Feb 2022 19:29:47 -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=DT76ro5L; 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 6DA7668AE9D; Wed, 9 Feb 2022 05:29:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8269768AF36 for ; Wed, 9 Feb 2022 05:29:24 +0200 (EET) Received: by mail-qt1-f169.google.com with SMTP id x5so806514qtw.10 for ; Tue, 08 Feb 2022 19:29:24 -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=k/Pv17vBz3bcLCw6u97q3DPVzxMc+46A3HY9W1wCesQ=; b=DT76ro5LntJsH7Z7vqCfYorBhKLHnDyP4FaOu2SlWtYy5RodznhitQrBaNz9Ct/AnS sPC5jeU2bhckd7MQ5iP5uiiKrbqi8HJhMdSeZNsfKDxfshxpfQ1wbyR/nzwmPcsVExnW fjJrijQevJGViJ8sV8//+4JY8i6Ez829sg74hSL1YmXG+u5AymtHdL47vwA6oHCTVi/z mHRHpgljtWMojLJQwyc2QhRgTbSCCJIKhkXud64LtEAhHRVM3kFbh2J+Wb8Nt3CaUEjW c2wPepS7+3ZIJrAoasqbN5+CnddrcZ6K8LpZ02ZqWDwZgIh0e2nUbZjISnYAHejbzXG6 6nlw== 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=k/Pv17vBz3bcLCw6u97q3DPVzxMc+46A3HY9W1wCesQ=; b=lADxqiB32AuYt2xsKAw+ijQasd35ooilJ0YeGPaouuJX2F9+4LQzXU28S48L48KnRT nvtJGOlw2E5lgnFLWjRamSn9pTU5ScC43vjb8jadcAswL2k3qrZRvugVPv8u7Lkp/o+b e622uu28G1oYqBoRtoVeY6ydAI8TvDnYhr+pg7rJahi0sWC6UwDAceJ/Iob7umCRkttg 7yXHmbneXUCqIEbkJxzw7xeB9vqV5hBWVLxMmhP/WMmyCe7s4HIRwB3WTH1IRhK+uuo6 omlYkshg9ZkQAw0jEBmDBsoMULbHLVK5y6VVM63EpXyzFErlc1yvjJ7btGgaFgCdiGSX QYNA== X-Gm-Message-State: AOAM5303vTQje/Mp9qbBi803T8tc4GQYZgPz1vrp2X9NuEhJ2Ztwjs/U CZlvZuaiIXhrf9F0YhHuu8j/jff2uxWWiQ== X-Received: by 2002:a05:622a:1011:: with SMTP id d17mr146827qte.377.1644377362980; Tue, 08 Feb 2022 19:29:22 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:22 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:47 -0500 Message-Id: <20220209032854.565698-2-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 1/8] avcodec/startcode.h: create start_code_is_valid() 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: ToYaJeWBzdQf This slightly changes what is considered valid in the following cases: cavsdec.c: 0x000000XX is now considered invalid. mpeg12dec.c: 0x000000XX is now considered invalid. (where X is any value) IS_MARKER is equivalent since VC1_CODE_RES0 = 0x00000100 --- libavcodec/cavsdec.c | 2 +- libavcodec/cbs_mpeg2.c | 6 +++--- libavcodec/extract_extradata_bsf.c | 2 +- libavcodec/mpeg12.c | 2 +- libavcodec/mpeg12dec.c | 2 +- libavcodec/mpegvideo_parser.c | 2 +- libavcodec/remove_extradata_bsf.c | 8 +++----- libavcodec/startcode.h | 17 +++++++++++++++++ libavcodec/vaapi_vc1.c | 2 +- libavcodec/vc1_common.h | 4 +--- libavcodec/vc1dec.c | 2 +- libavformat/avs2dec.c | 4 ++-- libavformat/avs3dec.c | 4 ++-- libavformat/cavsvideodec.c | 2 +- libavformat/mpegvideodec.c | 2 +- libavformat/rtpenc_mpv.c | 2 +- 16 files changed, 38 insertions(+), 25 deletions(-) diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c index 894aa1b54a..75cf17e271 100644 --- a/libavcodec/cavsdec.c +++ b/libavcodec/cavsdec.c @@ -1250,7 +1250,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, buf_end = buf + buf_size; for(;;) { buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc); - if ((stc & 0xFFFFFE00) || buf_ptr == buf_end) { + if (!start_code_is_valid(stc) || buf_ptr == buf_end) { if (!h->stc) av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n"); return FFMAX(0, buf_ptr - buf); diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c index 33bd3e0998..f0a2265938 100644 --- a/libavcodec/cbs_mpeg2.c +++ b/libavcodec/cbs_mpeg2.c @@ -150,7 +150,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, start = avpriv_find_start_code(frag->data, frag->data + frag->data_size, &start_code); - if (start_code >> 8 != 0x000001) { + if (!start_code_is_valid(start_code)) { // No start code found. return AVERROR_INVALIDDATA; } @@ -172,7 +172,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, // (may be the last byte of fragment->data); end points to the byte // following the byte containing the start code identifier (or to // the end of fragment->data). - if (start_code >> 8 == 0x000001) { + if (start_code_is_valid(start_code)) { // Unit runs from start to the beginning of the start code // pointed to by end (including any padding zeroes). unit_size = (end - 4) - start; @@ -189,7 +189,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, start = end; // Do we have a further unit to add to the fragment? - } while ((start_code >> 8) == 0x000001); + } while (start_code_is_valid(start_code)); return 0; } diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c index 027a578af1..9c639933ee 100644 --- a/libavcodec/extract_extradata_bsf.c +++ b/libavcodec/extract_extradata_bsf.c @@ -239,7 +239,7 @@ static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt, ptr = avpriv_find_start_code(ptr, end, &state); if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) { has_extradata = 1; - } else if (has_extradata && IS_MARKER(state)) { + } else if (has_extradata && start_code_is_valid(state)) { extradata_size = ptr - 4 - pkt->data; break; } diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index 5520960b74..85f4f432bd 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -213,7 +213,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, pc->frame_start_found = 0; if (pc->frame_start_found < 4 && state == EXT_START_CODE) pc->frame_start_found++; - if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) { + if (pc->frame_start_found == 4 && start_code_is_valid(state)) { if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) { pc->frame_start_found = 0; pc->state = -1; diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index 860e86aa74..27fd61e848 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -2475,7 +2475,7 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, /* find next start code */ uint32_t start_code = -1; buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code); - if (start_code > 0x1ff) { + if (!start_code_is_valid(start_code)) { if (!skip_frame) { if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && diff --git a/libavcodec/mpegvideo_parser.c b/libavcodec/mpegvideo_parser.c index 22666c85d9..82fdd1a1bb 100644 --- a/libavcodec/mpegvideo_parser.c +++ b/libavcodec/mpegvideo_parser.c @@ -82,7 +82,7 @@ static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, pc->frame_start_found = 0; if (pc->frame_start_found < 4 && state == EXT_START_CODE) pc->frame_start_found++; - if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) { + if (pc->frame_start_found == 4 && start_code_is_valid(state)) { if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) { pc->frame_start_found = 0; pc->state = -1; diff --git a/libavcodec/remove_extradata_bsf.c b/libavcodec/remove_extradata_bsf.c index c698d1d7f9..55e4ceb662 100644 --- a/libavcodec/remove_extradata_bsf.c +++ b/libavcodec/remove_extradata_bsf.c @@ -35,8 +35,6 @@ enum RemoveFreq { REMOVE_FREQ_NONKEYFRAME, }; -#define START_CODE 0x000001 - typedef struct RemoveExtradataContext { const AVClass *class; int freq; @@ -73,7 +71,7 @@ static int h264_split(const uint8_t *buf, int buf_size) while (ptr < end) { ptr = avpriv_find_start_code(ptr, end, &state); - if ((state & 0xFFFFFF00) != 0x100) + if (!start_code_is_valid(state)) break; nalu_type = state & 0x1F; if (nalu_type == H264_NAL_SPS) { @@ -111,7 +109,7 @@ static int hevc_split(const uint8_t *buf, int buf_size) while (ptr < end) { ptr = avpriv_find_start_code(ptr, end, &state); - if ((state >> 8) != START_CODE) + if (!start_code_is_valid(state)) break; nut = (state >> 1) & 0x3F; if (nut == HEVC_NAL_VPS) @@ -171,7 +169,7 @@ static int vc1_split(const uint8_t *buf, int buf_size) ptr = avpriv_find_start_code(ptr, end, &state); if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) { charged = 1; - } else if (charged && IS_MARKER(state)) + } else if (charged && start_code_is_valid(state)) return ptr - 4 - buf; } diff --git a/libavcodec/startcode.h b/libavcodec/startcode.h index 8b75832aaf..833754af09 100644 --- a/libavcodec/startcode.h +++ b/libavcodec/startcode.h @@ -27,6 +27,23 @@ #include +#include "libavutil/attributes.h" + +/** + * @brief Test whether a start code found by avpriv_find_start_code() is valid. + * + * Use this to test the validity of a start code especially if a start code can + * be at the end of the buffer, where testing the return value of avpriv_find_start_code() + * would incorrectly imply that the start code is invalid (since the returned value + * equals @c end ). + * + * @param[in] start_code The start code to test. + * @return A boolean that is true if and only if @p start_code is valid + */ +static av_always_inline int start_code_is_valid(uint32_t start_code) { + return (start_code & 0xFFFFFF00) == 0x100; +} + const uint8_t *avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state); diff --git a/libavcodec/vaapi_vc1.c b/libavcodec/vaapi_vc1.c index d82b5b31f8..cd3ef16982 100644 --- a/libavcodec/vaapi_vc1.c +++ b/libavcodec/vaapi_vc1.c @@ -470,7 +470,7 @@ static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, int err; /* Current bit buffer is beyond any marker for VC-1, so skip it */ - if (avctx->codec_id == AV_CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) { + if (avctx->codec_id == AV_CODEC_ID_VC1 && start_code_is_valid(AV_RB32(buffer))) { buffer += 4; size -= 4; } diff --git a/libavcodec/vc1_common.h b/libavcodec/vc1_common.h index c0f0c8c2eb..8ff9802a51 100644 --- a/libavcodec/vc1_common.h +++ b/libavcodec/vc1_common.h @@ -41,8 +41,6 @@ enum VC1Code { }; //@} -#define IS_MARKER(x) (((x) & ~0xFF) == VC1_CODE_RES0) - /** Available Profiles */ //@{ enum Profile { @@ -61,7 +59,7 @@ static av_always_inline const uint8_t* find_next_marker(const uint8_t *src, cons if (end - src >= 4) { uint32_t mrk = 0xFFFFFFFF; src = avpriv_find_start_code(src, end, &mrk); - if (IS_MARKER(mrk)) + if (start_code_is_valid(mrk)) return src - 4; } return end; diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index 7ed5133cfa..6dcc28463e 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -664,7 +664,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, void *data, if (!buf2) return AVERROR(ENOMEM); - if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */ + if (start_code_is_valid(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */ const uint8_t *start, *end, *next; int size; diff --git a/libavformat/avs2dec.c b/libavformat/avs2dec.c index 0d5b23b65e..a5a7cee7d0 100644 --- a/libavformat/avs2dec.c +++ b/libavformat/avs2dec.c @@ -42,8 +42,8 @@ static int avs2_probe(const AVProbeData *p) while (ptr < end) { ptr = avpriv_find_start_code(ptr, end, &code); - state = code & 0xFF; - if ((code & 0xffffff00) == 0x100) { + if (start_code_is_valid(code)) { + state = code & 0xFF; if (AVS2_ISUNIT(state)) { if (sqb && !hds) { hds = ptr - sqb; diff --git a/libavformat/avs3dec.c b/libavformat/avs3dec.c index 2395df171b..112b28efa8 100644 --- a/libavformat/avs3dec.c +++ b/libavformat/avs3dec.c @@ -36,8 +36,8 @@ static int avs3video_probe(const AVProbeData *p) while (ptr < end) { ptr = avpriv_find_start_code(ptr, end, &code); - state = code & 0xFF; - if ((code & 0xFFFFFF00) == 0x100) { + if (start_code_is_valid(code)) { + state = code & 0xFF; if (state < AVS3_SEQ_START_CODE) { if (code < slice_pos) return 0; diff --git a/libavformat/cavsvideodec.c b/libavformat/cavsvideodec.c index 1fa3aa5e92..01cf069f5e 100644 --- a/libavformat/cavsvideodec.c +++ b/libavformat/cavsvideodec.c @@ -38,7 +38,7 @@ static int cavsvideo_probe(const AVProbeData *p) while (ptr < end) { ptr = avpriv_find_start_code(ptr, end, &code); - if ((code & 0xffffff00) == 0x100) { + if (start_code_is_valid(code)) { if(code < CAVS_SEQ_START_CODE) { /* slices have to be consecutive */ if(code < slice_pos) diff --git a/libavformat/mpegvideodec.c b/libavformat/mpegvideodec.c index 33c5d79794..88966af72a 100644 --- a/libavformat/mpegvideodec.c +++ b/libavformat/mpegvideodec.c @@ -44,7 +44,7 @@ static int mpegvideo_probe(const AVProbeData *p) while (ptr < end) { ptr = avpriv_find_start_code(ptr, end, &code); - if ((code & 0xffffff00) == 0x100) { + if (start_code_is_valid(code)) { switch(code){ case SEQ_START_CODE: if (!(ptr[3 + 1 + 2] & 0x20)) diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c index c18c75082f..8b6987b7f2 100644 --- a/libavformat/rtpenc_mpv.c +++ b/libavformat/rtpenc_mpv.c @@ -57,7 +57,7 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size) while (1) { start_code = -1; r = avpriv_find_start_code(r1, end, &start_code); - if((start_code & 0xFFFFFF00) == 0x100) { + if (start_code_is_valid(start_code)) { /* New start code found */ if (start_code == 0x100) { frame_type = (r[1] & 0x38) >> 3; From patchwork Wed Feb 9 03:28:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34198 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1371712iov; Tue, 8 Feb 2022 19:29:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJwFmeF0qyyurSqGNCzE2AHqEn1w8+d9fYcsUokBrUnAUOKVQj+VvTq0MD6uDEECzCmraVJi X-Received: by 2002:aa7:d286:: with SMTP id w6mr432339edq.452.1644377396587; Tue, 08 Feb 2022 19:29:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377396; cv=none; d=google.com; s=arc-20160816; b=iOtrriv0bHaZ64vwaqo3IK6qvYMQb5Oq0+2mKlqI1Kq/7DYuIFKT4Hu48lFM1a6nXf hVyKglFRD9UTDUeeBz6a7vcFnC2T6Vzozdoy2bPfQRBj3hWM/yvsta95/M0AUZrxgDEJ vQVcpJEFvbm1OPjv8FtDwx0ewF97meUw87sq+hypuN7BBGYMMZv4PaD/gcjizEZwDMYX titgxEXgZdhLTPVwLt01VO5e4c/W7SZMonDBiHLVItPfo/CeRnSi3qbgA88XG0b2yMJz 1tawhEWmovZ4PBkHsYcpsyu4mAtvwh9voUex9n3rysT/oxqkmKg//WxzCOKEI8X91sBq /xoA== 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=idqUcsFBjZaQOJ/Yz36GgaCiwScFCFRGZxJZa/EIEBw=; b=MZG/qe1601Pb5YgKz5pasv9KJKfIx5jOVpEDJzFLzOZJSLsLQnT5lxEeW6luI9PTol 6VxnimVKZpLTTFQBw2fL4Fa0rXORUrYIMU/q3SaeTQAlrnIVHDxCLAGkMq+0AAroxpUj NHsD27fO4tj47v7g8ktcsGh1F5R+vNyVTctFIzEm14uLnB9Jr5mW2gy28KfswbeewFoO jTRY0VH9gpxk/GF/2nN8FfMTqzuhiGLYrl25sswwOsG/emP8IWRViwyYHCjZP4jSydMK UgYHrQL9PYlpTWl4BK3zxrMzAjJsqP0We1k0xZkyw82pliSN7JgmI2dgqiCNNgP9LWnr Or1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=ZuU8ZHT+; 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 y9si11827770edd.54.2022.02.08.19.29.56; Tue, 08 Feb 2022 19:29:56 -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=ZuU8ZHT+; 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 6289B68B164; Wed, 9 Feb 2022 05:29:34 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8C75C68019D for ; Wed, 9 Feb 2022 05:29:25 +0200 (EET) Received: by mail-qk1-f179.google.com with SMTP id c189so645250qkg.11 for ; Tue, 08 Feb 2022 19:29:25 -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=saxN9HVlGEV0mlwhCAg8mmes1L9bOuJJdA/TucvEGPk=; b=ZuU8ZHT+fpZOb6E2WVxXs/9PbfeGx/FwVyBXr6SvcSA9aCJ0ShYVXMB9lJIQz0u6XG SEdWwLilyt2BPppVdpwF2jKBSOoVYp4p7xp1uti5w/pZMePNLPA+Q7GH1X2CSX6tl04n lRGwoE8sXfuBc7zz5wj6Mqi8DAEf9CNzvJ3Ft+C/LGbYS7obBgBCMJsQsScpnf4CND21 5101Xl7lQa0w6/o7Xyws4KeSKJU+BIzSIEcsCJI74ZtEh2Hv5dIZ9Az8/x/gEhaBZVyL nKZ0mFyfdbH1m+EZL3u6+TFNT/TTJBw4daN8NHLHN1os5Sez54WhNbLtmMgfYKiBEbxW wjLg== 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=saxN9HVlGEV0mlwhCAg8mmes1L9bOuJJdA/TucvEGPk=; b=ybKq2pUIoM4cOJWlP2RaURjYNlALWPxR+KbkGPx76y3EBqMXNnbyO0Ve9r6ZnIdQCr lF/0RwLoMqToStqzlYW1ENFjK6LXUErWSTRgRxC7HiM6mu7WA0pm4SAGGP/iOBNROp/C EwofNQdYadcotQgU9kpq1kjmt79ziYb5t/rE4cN5rUDUIO/T8J324Ux4lH1dD+/jBn8g 9QnsQUFszBH9mJBikAqwlOP5BPQibxzWLrmJpAX0OyLsC6/mWaAqMi0FByAkjGIc88jw 1DcHUvxhd+5TN6XR6cDvO6YF01ytWfMg8yckiyCvd/QaMBPDmyU+cEhkkTKKU4vVI+K0 mCJQ== X-Gm-Message-State: AOAM531G1wjMdcsYeN90JOODnbltnTvRsKP6FzQzEdcmfsIvakiR6Fzr mcdCTu4ssBc0suRCD6Y/dzkRy+62C3E5Hg== X-Received: by 2002:a05:620a:c4d:: with SMTP id u13mr147027qki.29.1644377363885; Tue, 08 Feb 2022 19:29:23 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:23 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:48 -0500 Message-Id: <20220209032854.565698-3-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 2/8] avpriv_find_start_code(): readability enhancement part 1 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: 6z8m+5X3W5Ng No functional change. --- libavcodec/utils.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libavcodec/utils.c b/libavcodec/utils.c index c7c7323351..42a1885d61 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -963,10 +963,11 @@ const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, } } - p = FFMIN(p, end) - 4; - *state = AV_RB32(p); - - return p + 4; + if (p > end) + p = end; + // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1} + *state = AV_RB32(p - 4); + return p; } AVCPBProperties *av_cpb_properties_alloc(size_t *size) From patchwork Wed Feb 9 03:28:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34199 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1371782iov; Tue, 8 Feb 2022 19:30:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJwoTE7qq7/8MNHXopJ3d8Z0cCVRW0pELII8iBt7mTVAjzxCjMejO1EKQqk1k7of1bBvw455 X-Received: by 2002:a50:fc0d:: with SMTP id i13mr435581edr.180.1644377405911; Tue, 08 Feb 2022 19:30:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377405; cv=none; d=google.com; s=arc-20160816; b=SuD9R2LaS7iiiv8a73x7zpnMWmYK2/Vsc0mb/JXlcBDGWqQWxG3ePNDVqyYuRM21Sh 1hw/57XoLVnS/LIyTBu2BiLi3Ok120/4rUaqRfflmJSblv2brwKtxBuRQT69j7rGShbt ChmloWGSo72Kh3JCxdSh101GdYLGNnmh4qYoZDZRpDkWNZCkZxFV2Px/L9iGIpq7tMtO DFzCnO47HlsTmz1GDLTcNvBLLZT0qRqjzR/vQaVwoCgUcIQaPOrJ2m16v+aaZcohuofM g9bXL5gY/FpwbqvqXxeYLIkrnoklymxnrW025cREffplLI3ZeFEtQz6SOc9Hw/n7a4Ud LRgQ== 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=vLCjw8NZ9UsMLOk/vT1h+KpSXIXKXLF3WOKGxSdYyEg=; b=OKBsI+97W/JP6PnvFFOfo79GZhJvDxQCbIAKI5v8Iz+xj5CDUM1uRraC/AhYSuNLEb wSlaWw6ilvUbtOOq5cF5DAEO3yg+NCK0tBYRA5EO6KLr8xcEMvjH/g44pqGSZrfPPKlx w8wqeQikua0lOotjFzIAlVS0CmU8czqj9z3Le79v6C6l3ZAFQ0V7JY96Shc7yORyXqYj GuQauawqOsYRulrEN90kXY2AKPMqp8i6+PqE54TUtdnlNW4iCR5XAZavWLFYc8+6FbSy B60SeM5YOJnLOSJbyvCLTiFiT5BFsAyskYFMhjHa8VCOaM1/OhRXaSH+EiY1P01dU1Nq DMFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=aeWM5IAC; 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 m21si9283865eda.471.2022.02.08.19.30.05; Tue, 08 Feb 2022 19:30:05 -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=aeWM5IAC; 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 5871568B17E; Wed, 9 Feb 2022 05:29:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3988D68B0A7 for ; Wed, 9 Feb 2022 05:29:26 +0200 (EET) Received: by mail-qk1-f171.google.com with SMTP id g145so683383qke.3 for ; Tue, 08 Feb 2022 19:29:26 -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=l3rEMpzdVAawVdf28yY7DNWLv6onDcGfeh3/qWeXGCs=; b=aeWM5IACfDlTbIKHgFg3mJ6LU2xDzVdtGFZ/MIyP3R86U2Dd4oskz8Fh/XoItNgyRT dNLR1ZXzUHXyAVRK2hAsv7yjnFylk2oMEqjCecF71KP6YECHQGeuaLu1nj61gljMeXI4 MHIvgS4JbBwDLIeIVzNamJi0rZeo8g9D519gjP379UI0gSCebUHb7Km2EgQVyMSsjWLK u31GFEEQ2ovYpgbad5CfdvxFoIwvh0qHmyUZltUWZzzSdDWvO43v04iZ8ovdSQMjjIBg qCShLD6G6oicEc5rks2nxldkkeALhdMc+HC55iUPiv9hhCeVokPLqg+Yl+q28O4T7JY9 Qqcw== 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=l3rEMpzdVAawVdf28yY7DNWLv6onDcGfeh3/qWeXGCs=; b=11TJDEzylG8QLr+l2KOxoUOziwfS7tVwalyfC8zoGieEWooJLZHji9a5ybUVgHKFwR 9Ro5GeWArB2AoqcmRLelhQPNYDoba98yDF61GiCOTC73M8YhBoMbtj2fo8MPPX9qiGol nx9M+kskTQp3lMvu7sfuPhClhte6Kc8e0e0/NKtFaDd36kGTNMRWvs2F2gwhaOY1F40R LpMFF+GGXCIbkYAdVC7FkbQrPp0s9E84or7pJhCjxw/F+37qOE/A2avtufC0m7pPLMXg teEsJub9iTuuR/gIp7R0y4M994Dv5XKUgQ4oz+7AZ33elqHSZImpQw86pa+EKNSrD8aR Yv1Q== X-Gm-Message-State: AOAM530j+/6R8UFC//igwK7CSuLvPLpEUU2jQizjeTti2YBY2+gfDb8r lRvCqH458AClTeZcZnrpQlg9XjMLOutPSw== X-Received: by 2002:a05:620a:15e8:: with SMTP id p8mr148258qkm.215.1644377364761; Tue, 08 Feb 2022 19:29:24 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:24 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:49 -0500 Message-Id: <20220209032854.565698-4-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 3/8] avpriv_find_start_code(): rewrite while loop 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: d3shr+Dt9og/ 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. For the check p[-2] != 0: This slightly reduces the number of iterations by starting with three new bytes on the next iteration, instead of keeping byte p[-3] which is invalid, since it is now known to be 01 when it must be 00. The comparisons (p[-1] > 1) and (p[-1] == 0) should be one comparison against 1 since p is unsigned, which makes (p[-1] == 0) equivalent to (p[-1] < 1) No other observable change. --- libavcodec/utils.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 42a1885d61..5b49657b44 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -953,12 +953,27 @@ 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++; - else { + // 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 + } + else if (/* UU UU 01 */ p[-2] != 0 || + /* UU 00 01 */ p[-3] != 0) { + p += 3; + } + else { /* 00 00 01 */ p++; + // p now points at the address following the start code value XX break; } } From patchwork Wed Feb 9 03:28: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: 34200 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1371848iov; Tue, 8 Feb 2022 19:30:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyFWTqyHGDj4AyEaEEyb8V6jKzSc15F3FQMAj+/l3Rcag7pMdizzBsg0os4XxXE7iyXnAe/ X-Received: by 2002:aa7:c043:: with SMTP id k3mr430500edo.184.1644377416424; Tue, 08 Feb 2022 19:30:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377416; cv=none; d=google.com; s=arc-20160816; b=Ipe5r+myaBjGnJYF7PAf6SfxRtccxz5TdJ4XXCGu3Hy/U/uCsZQeDZN+YthtDelQRm jQ17qE5YFKwLq2BNMzKA/bp61LqdgSkflTJLSbY1nTKgPLqwPs13tN8wwaMl0iNFerXr EJX9FHbm2O37ZbI7fkHYFcn8SYkm+7Xz5aKk3HMtQsvpKMRoNFOh5ck7CRuQHYeN8bd1 ac/Q7Nft5kLzLyBvbR2jaZXZswyGDn21r42D2EGaknSzZj0gIqWzYXahfMpa8n4GPVcf k0D+FE3iYAwAQ3H5CeCKdKNF+Ty3I1aijWYl4wuXJr9T1Ihwv/rQmq4UwRW+q00DJjec Cn5w== 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=H8gu/ObslYfVJV20H6CWIUScof98vNl2xi0pX/UI9HY=; b=ZvfkDOYzDrcgFF5KtkbhJxpw/W/9o/Mx1Do462hhEL/LWfLO1f20tciri3X2LCLe2L b6zcXnlBXG2z1/5W8fX9qEmjBCnSzIk/Pw8AT4R98hkpr2809bl5Mw2iN7AUzii7sIiY 8T1rDR7QilRSaXN3LCCdZctofis0P0z0oF5JgLv2xNBT+vYsgNCMrXER0AwJlIg4krNU Y67sasFecI8hakNUG7DNJGkGBXIJVbUEs+mTasaEpI8RV4yVYIylSJrLfltZ7np/IKUd 0A4pjUbqfYkpOzAzS8j3B5cD5uMLUhlD2MIV9ZjiCPFtfjoJCkt+lB8dFnZOzLN1F41k uqIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=WW06u4w2; 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 o9si11941992edc.202.2022.02.08.19.30.15; Tue, 08 Feb 2022 19:30:16 -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=WW06u4w2; 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 438A368B19F; Wed, 9 Feb 2022 05:29:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0B14868B0C6 for ; Wed, 9 Feb 2022 05:29:27 +0200 (EET) Received: by mail-qt1-f170.google.com with SMTP id p14so887151qtx.0 for ; Tue, 08 Feb 2022 19:29:26 -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=7hyE7DIsE8bBOtIfJHk8YcJcaAH6tcw3TDF4C5w6fCk=; b=WW06u4w2wqkT3eNgIv36TniPGj/P13ozhAaXAPUyuPUSMX+2LuYWSm3Sa8P7bFqJkv a3k1JePdWP6kmXhx2Trbxsv2haifY5khBoDcGQ+cKoZaFwZlfuvZBwQeZyb3YA4mvKZd aoemFOzOCi1iyx1ml5gpeG7uNdatDJR20e+tfHvoSrRBdFZFLEFxvqDnljI0Rl3n2wUc VXNphaOoxCg5BDy9XfafbWC50gkYL3TR3ZU0WjfMp0LdWgGwVs/mzxdqVHNma+PWYGaG 0DIscBbUH/y8U4lg/YrQjfNAujFDG+NNI4W+C3maJrI1J5SrgcagydSE2V8kWm9+y0J0 EQCQ== 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=7hyE7DIsE8bBOtIfJHk8YcJcaAH6tcw3TDF4C5w6fCk=; b=uffq3Nvn3+nqvPMhy9h/Y6vjHgj5tzSS+h+4aNgl+kL1DMvZ0/bEfJxtuwjUHwpszP y+MhH0X413pys7EqJ+oTYseNe3L3T7qcwEuAZU1MElRE3kMd25q1YQCxPPbwq1a4CdQv sI94gbd1K7r0gQxpkDHLLEDZgsLuV5Dj88kIEaIVBpeM+7NIKgY5tHgAihMcfVsVEVkk cpdlnd+pQE3ibbsxvNVBJ9OcI968s3uEqOA/naUeGb31XZ8Z+5eo8R8z27qsuQKexosk dabjSzKOwPji3inQQTxUX4MyfWGnKVYTSt8ND2BFlUwXZ5tFvelwhIZ5QWcsttkt8woc 4jgw== X-Gm-Message-State: AOAM533/c0dT03EehjUGShiNKmrKbAgHOm9V4+2ZS9GtdgBkVYH551ZL efUuTkX5tLmpElNN05b5CYgbhNhkAoTS9w== X-Received: by 2002:a05:622a:22a1:: with SMTP id ay33mr157221qtb.396.1644377365547; Tue, 08 Feb 2022 19:29:25 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:25 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:50 -0500 Message-Id: <20220209032854.565698-5-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 4/8] avpriv_find_start_code(): rewrite for loop 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: NFUFtgSVhbuy --- libavcodec/utils.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 5b49657b44..bdafdaa355 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -940,18 +940,20 @@ const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, const uint8_t *end, uint32_t *av_restrict state) { - int i; - av_assert0(p <= end); if (p >= end) return end; - for (i = 0; i < 3; i++) { - uint32_t tmp = *state << 8; - *state = tmp + *(p++); - if (tmp == 0x100 || p == end) + // read up to the first three bytes in p to enable reading a start code across + // two (to four) buffers + for (int i = 0; i < 3; i++) { + *state <<= 8; + *state += *p; + p++; + if (start_code_is_valid(*state) || p == end) return p; } + // p is now properly incremented for the negative indices in the while loop /* with memory address increasing left to right, we are looking for (in hexadecimal): * 00 00 01 XX From patchwork Wed Feb 9 03:28:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34201 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1371942iov; Tue, 8 Feb 2022 19:30:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJwLR+bxvsUVGVx8fjnUinG9bCRPeHJNArvOswTFhIQX1ATj10GoNt3oTHG0PKqIU4/ckCBp X-Received: by 2002:a17:907:6e8b:: with SMTP id sh11mr212257ejc.376.1644377426151; Tue, 08 Feb 2022 19:30:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377426; cv=none; d=google.com; s=arc-20160816; b=FIZ5ChVbboPPNMsjt41pBD62mXtkEikXyXGkZCiYORV3wle1qpZYrGLiPupyWEbSHP GJa9ScZa16nNWGqViblep7yN++QIuhmSr1osPEQjBRHoSOf7OLPOgfBDvyFc/1Ad/sX4 +y1dCqhDZicF458s/rx9nVvYeYLi/Ekhd9uPkhN+HmD2bQAG0qJekHLE861kJIPJRat7 aXXptBj+RwcENs0YeiJRrSbK5Ej3sXpko3UEjNY1bUuIlgFMpCPQAKInteqQjyHOclTK PGC8tMC/fSUTApLfcERNHHtt9RkWZD6C5elC5dzx2mhxTqK29WGxymnSdREE+rpFOnK1 2jXw== 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=jjopCoXZrTBOhSQN2AyPus0ajW9Al7yIRW79TXtPht8=; b=rgzT+zRIk1Zxx9nz0Ts4TxPyONNh5WdLGLnQ8ig5pOvTFb5A5vcH61FZdKe0RxzPU9 /yFH7AZ26q/EGdniuw19trtUBcs4Ja6FSJGkta/EDrQxP8BDV/8Muu7nc0iGREKo+1qU 6rPQF7Wh1aGxea0wI8fd+YX5t/FGQsKpygSZATISznL4SsjZ2EzivKyTG3nftZBdKApF Ow020CQ8He8Tasdna9UU2QLy+z4VipBViKyxj9IJWvi999pakAhwOLfOpgvs/IBhOupK lzzHydaZuFU91ppn6Ka9hOFtn11MNpGMwNdaHpYa90pRPt8IgVjbow3vLQaJ0EkLpMl7 zajA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=m00mCdDF; 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 qw39si1523903ejc.308.2022.02.08.19.30.25; Tue, 08 Feb 2022 19:30:26 -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=m00mCdDF; 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 70B8A68B1A6; Wed, 9 Feb 2022 05:29:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AE39968B00F for ; Wed, 9 Feb 2022 05:29:28 +0200 (EET) Received: by mail-qv1-f46.google.com with SMTP id g3so833996qvb.8 for ; Tue, 08 Feb 2022 19:29:28 -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=vOBugMRvj6yc2nVUCqLw4Q1l7m68ZlMjnbD/Pp4jlW0=; b=m00mCdDF4Kc8CFr2jEYR9mRHL8qxVSoSQhd9yQco4trC2DHi4qDuzcQMLmGZ24cnwT iHdDr+ZBvh9plO2NWznkahbG1NnJUInp+wDWfwJx6+aWnBLMcuNrYwniMYpLNS4xoDXD 03VR37ufp/r+F0QmCSjpePOZ93nLPs5VdLMMoQlZ7i33tWUsyzA894//ZHfrgTVOTnXE QRa4e8UJSZLQxEMGGxf4t38LLBUOE0UE6Jd4NEha4PCr0MB31CBqdHeDjJ2DnIIe8OqT m48kS0neP70o4Zwbs2ACAV+JCPhArYixyhJHmUSzhCOB9txq/LKDathMKCmp900YGS5R MHTQ== 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=vOBugMRvj6yc2nVUCqLw4Q1l7m68ZlMjnbD/Pp4jlW0=; b=lNl/7d9QXTzo5ogifJ4t0Qzg+v5X0dRp22B0q3FDojX4q7ZhA/7arw8qfcA1FXaMxN 8/U89SWGTebVl1zmNmEL4ihXTQs1V+EOOGFqhJbjcSJPdeBZA0IaCk7ewzykm/x30/CW xwU3luDl5jNettWVOHEA4Cokl+DgwDpV1yv5QtNnTJYeWdMQ6mCyRkxyQq+gxinfgDY+ +Abb07r6rtQ6hbIGt83zfGN8QrPBYSWmmntaGf+GsmPcq1Z2pNZpIWtPgw6GiD0vQXt/ u9jnMjrb9Oh8ZnNJoCnHgTVH3j097CoLWuHKJ7JO1id0DNPtA00e3QEsN74y0DT3gjgk S/VA== X-Gm-Message-State: AOAM532zKGOwySl5VTo4barVY1QKKveFdzi3x8u8qJXJJ1Vx88mYBNVK L5b7LMjgixPOtNlcq95eC/LMb+7MkBRlNw== X-Received: by 2002:ad4:5ccd:: with SMTP id iu13mr245927qvb.89.1644377366426; Tue, 08 Feb 2022 19:29:26 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:25 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:51 -0500 Message-Id: <20220209032854.565698-6-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 5/8] avpriv_find_start_code(): add doxygen comment and rename a parameter 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: ywVOl5eKRY1h --- libavcodec/startcode.h | 26 +++++++++++++++++++++++++- libavcodec/utils.c | 10 +++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/libavcodec/startcode.h b/libavcodec/startcode.h index 833754af09..69389c729c 100644 --- a/libavcodec/startcode.h +++ b/libavcodec/startcode.h @@ -44,9 +44,33 @@ static av_always_inline int start_code_is_valid(uint32_t start_code) { return (start_code & 0xFFFFFF00) == 0x100; } +/** + * @brief Find the first start code in the buffer @p p. + * + * A start code is a sequence of 4 bytes with the hexadecimal value 00 00 01 XX , + * where XX represents any value and memory address increases left to right. + * + * By preserving the @p start_code value between subsequent calls, the caller can + * detect start codes across buffer boundaries. + * + * @param[in] p A pointer to the start of the memory buffer to scan. + * @param[in] end A pointer to the past-the-end memory address for the buffer + * given by @p p. @p p must be ≤ @p end. + * + * @param[in,out] start_code A pointer to a mutable @c uint32_t.
+ * As input: For no history preset to @c ~0 , otherwise preset to the last + * returned start code to enable detecting start codes across + * buffer boundaries.
+ * On output: Set to the found start code if it exists or an invalid + * start code (the 4 bytes prior to the returned value, + * using the input history if @f$ end - p < 4 @f$). + * + * @return A pointer to the memory address following the found start code, or @p end + * if no start code was found. + */ const uint8_t *avpriv_find_start_code(const uint8_t *p, const uint8_t *end, - uint32_t *state); + uint32_t *start_code); int ff_startcode_find_candidate_c(const uint8_t *buf, int size); diff --git a/libavcodec/utils.c b/libavcodec/utils.c index bdafdaa355..68d126acd8 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -938,7 +938,7 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, in const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, const uint8_t *end, - uint32_t *av_restrict state) + uint32_t *av_restrict start_code) { av_assert0(p <= end); if (p >= end) @@ -947,10 +947,10 @@ const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, // read up to the first three bytes in p to enable reading a start code across // two (to four) buffers for (int i = 0; i < 3; i++) { - *state <<= 8; - *state += *p; + *start_code <<= 8; + *start_code += *p; p++; - if (start_code_is_valid(*state) || p == end) + if (start_code_is_valid(*start_code) || p == end) return p; } // p is now properly incremented for the negative indices in the while loop @@ -983,7 +983,7 @@ const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, if (p > end) p = end; // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1} - *state = AV_RB32(p - 4); + *start_code = AV_RB32(p - 4); return p; } From patchwork Wed Feb 9 03:28:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34202 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1372033iov; Tue, 8 Feb 2022 19:30:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJx++71J8aLrils+hUFMWeaL8nFJSMe4KTV3buIQqXHgxhJiunvzP4YJi9y5f1X9WgOvza3A X-Received: by 2002:a05:6402:2754:: with SMTP id z20mr409863edd.235.1644377435999; Tue, 08 Feb 2022 19:30:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377435; cv=none; d=google.com; s=arc-20160816; b=xctN1VuJ81nCZ3uC4ha9efjhmeRJSWWGJEEG8FjIQu8DanB27iRDNLsjRogTkf6yI/ gf7zoKjCkmvb9MDwi/PuRBKnEHC29nMM1t3C/kmRTBM2Cx9M29tvt9uDRvOsfjctnW9o wrIRfmJaeo8sRLGfmZdxHJf+kODV4vZo8Ps4QpJ+CUP4a0N1Gp/MrHqhB4DhOZ4/Scms l12kwL/w3SQeLH0KylcsF8DfOhwMReFniUxvQ4qm23ljp4ggJ8haejZS1UKaYs1+214h c3o4SgjKaE0hV+PFvTPIgEGm+8I1RGvM/r0478y3ZSQ8s13GXmKyyMVoUVuAtm5QsF1+ vtFQ== 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=8somVa71msxh7yIqs49oHTgVX1ERVA+/Qvx0lagIx8w=; b=TanRYZs+y9DkOFrlbIalfrqXTfijy1iAD/+oGE+LhOkv0BcaOpG5smMxfy5qDs94Es L3oTAlCbyXH4pBbLqr/CT/CTZ/47At3lv5B/bbLOzzqs222+sj6UKliULh+BMVePnY3M CNVH+tTa+a7foya9dei/97KMIx8vN8qRnlp56AmyY1Dm/n0g1HiriAo7T0cqHYL/KiwI t7h6lC059UhNx/4fuoSaBIMOtc3fRc+7QoTf7x7XakgRjd/anQLywPZrv0FHrAua7kB0 KWkfyucY1p6cdxCXOpCdLn4P3JryUnPG2i8Oh5dmEwKSHEE9+svjykIzSjukLHNQHKqV D63w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=PxcJZL9a; 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 g7si1870762edb.109.2022.02.08.19.30.35; Tue, 08 Feb 2022 19:30:35 -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=PxcJZL9a; 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 57A0868B193; Wed, 9 Feb 2022 05:29:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B4AC068B0EE for ; Wed, 9 Feb 2022 05:29:28 +0200 (EET) Received: by mail-qv1-f53.google.com with SMTP id k4so840949qvt.6 for ; Tue, 08 Feb 2022 19:29:28 -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=Ji7ATNk0mswiZcghW3xXnTq/PN51mpkUZXTeZr3x1e8=; b=PxcJZL9asX1FPCNjt2ag2gAbVIQ50unQZrL+MINgvR72AIUQGDdc7d9RrjO6NWDDVm yUe9Rk3PZTcvEynBJr3WgqjNJiw9hGWoLJVP14P0QxSQuFYHXnuM81DXn7x2l4hA2d2s nw70MPch+e90oWdF4oWvTrZNQ9veik0fP9lLbOxy3qOSlTDJvKAITz3nzVIuXhZa5ufe tlKZSjS8iYpExpEKrY9l5BS9g4Vt5OEQo04ocmQm65ZILXd/f0WgQAnOxuqtCFFCfvUK paqet9MKC2dWaYHFANNOjGlY1zZKwYTfozY4qT+KlddHIQfng/bvJAKqj4QRvkpcorg4 y+4Q== 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=Ji7ATNk0mswiZcghW3xXnTq/PN51mpkUZXTeZr3x1e8=; b=soWA2IpIlvFR6ttjnu2YFhoc9Bv9oldI8Bbdo4u3vjZgdjSunvBjslpzZViansxsIP Th5blI5/bUV0GQIldcVtr3LGEr9brUQUhyQBEAr/9opcs+YmvzXHCbRTYDc1AaiMEu9u SzMMRv24nLJO1hWSOnhzACq0UVMOC8hz6q7wrPqVh/uIJUSf8AYwc9v+yL+9FUZhVk0w 6LYxCAywoPsRRB91zLOg6krdPSpgTqJsenGj0HVW6MOvmBdH05+RrUxzLxtY2yFhS4JQ wJw1SwS6GZ2244kwReY86QKMqi+sq+3o4KSo4f2bzFocOdELNo+qf/dE5iotE92tJOqx gWsg== X-Gm-Message-State: AOAM532CTDS+60VYH1c/2MoBAvjqNXRC/eXJh6eJ+DeUQiXhTgD+5C06 Cke7MAd7/q1fIY7rcX/DlPLhfuZm238SgQ== X-Received: by 2002:a05:6214:b66:: with SMTP id ey6mr152700qvb.131.1644377367206; Tue, 08 Feb 2022 19:29:27 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:26 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:52 -0500 Message-Id: <20220209032854.565698-7-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 6/8] avpriv_find_start_code(): correct type of start_code parameter 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: HdXgaASCef/o --- libavcodec/mpeg12dec.c | 2 +- libavformat/rtpenc_mpv.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index 27fd61e848..24cd6aac77 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -1768,7 +1768,7 @@ static int mpeg_decode_slice(MpegEncContext *s, int mb_y, if (avctx->hwaccel && avctx->hwaccel->decode_slice) { const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */ - int start_code = -1; + uint32_t start_code = ~0; buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code); if (buf_end < *buf + buf_size) buf_end -= 4; diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c index 8b6987b7f2..9c0816ef95 100644 --- a/libavformat/rtpenc_mpv.c +++ b/libavformat/rtpenc_mpv.c @@ -51,11 +51,10 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size) end_of_slice = 1; } else { const uint8_t *r, *r1; - int start_code; r1 = buf1; while (1) { - start_code = -1; + uint32_t start_code = ~0; r = avpriv_find_start_code(r1, end, &start_code); if (start_code_is_valid(start_code)) { /* New start code found */ From patchwork Wed Feb 9 03:28:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34203 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1372099iov; Tue, 8 Feb 2022 19:30:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJy29IFRHnxNm4NsuI+ZiUeWmeJ58Gnvyw9BATl0tK2l9Bi3HxZaGBSGYfWaA0uMHgOGClqh X-Received: by 2002:a17:907:2d0e:: with SMTP id gs14mr186908ejc.225.1644377445127; Tue, 08 Feb 2022 19:30:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377445; cv=none; d=google.com; s=arc-20160816; b=S6LG6fmVuEoZGWj6nYSJDOWFdIFwqa0EbgAWR5pkQKQTKRfnnwZDPK/oEitYtmjuiY EOVgXwtC/A/8npFg0Fra+Wa1zOosHjYoqwl5ABCmOzsYhipWRfumlHfZNZvaJFKp5VUz dwliBzIhKOtHZo0m8uCzT6L448uLMRTjk2FqEvuB383a6aRN7syhDtXQiLivWJseAPRb WP+xAHUzNAUUKhlLWeJx1+rprDus5EZG1cH4UcuK4RKKee7RKWgNOV7AbHIUhjTHrL/z MKmzCjkksI4XnHz0oTtQswPXttDbfrLIDpYrDwbnmJmDzUMamnQRtna7nHd337lvOjUq T1uA== 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=jOTP8w/WFSWZ8a0CXiqALzX65y0P2OpEpeLeJZBAq+o=; b=j/z8tZKqsGQUcyIOigZzOKmszBXy2I9dJ14IZxVFswa/EfesC4FB5asReVmsd+9Uk6 2pptOIZTahznpb/qqtilEsNtYOPJFz/12J+O2P0GFaTfLyQ8ZAwXyfF2TeE4lrV2+e9W BPZj9HyS74fQgEEV+4/ZyaoIb1E/4c9qBG0+pWNfYh5DVB1UWUyBnVgZs4MS8Q6Zln8K C2irvCaqoRASwZ2irGhmkxdGBe3G9NnNS3LYjSOAZkutnncxBKWrrCKiFfUOSoUNXX99 ZzaXJwRrkBeq4I/rTcINmdLQNkvFTRJYeJ1B5biq6SN+Pk2uVxeQEFD2mMGrHc9bVsdF tVCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=mfpszVoE; 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 qk18si139617ejc.261.2022.02.08.19.30.44; Tue, 08 Feb 2022 19:30:45 -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=mfpszVoE; 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 26D8268B1D3; Wed, 9 Feb 2022 05:29:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7FA7468019D for ; Wed, 9 Feb 2022 05:29:29 +0200 (EET) Received: by mail-qv1-f43.google.com with SMTP id k9so826548qvv.9 for ; Tue, 08 Feb 2022 19:29:29 -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=MIFicUejfYlSIZZ1hMVx06bcgaqtI0dFCrsliFSKLIg=; b=mfpszVoExp2ZRJnC13svu1RkgVxUHA4MmrPrv3QnbHvh0zgekuUDIdiBkpfvOYgSrz ly4oULFjimiyvTIQ48cJd0l+zt0qbXIOMsYeN0E78N6e5s43v5oa2AmosgH1lGSOS8y2 fi1nDT+OoYXar2fjknspf9tMmkt0pSymyTWft7qWXEAcFf3JB/7Y3fQst5TeEs+D/iMf 11RXPyL34dRvO6kGAxtOrhpVI6nNvHzaV+DwQSYcVfEzK71bcHjH5g5a+kfArr3s5HqH RGC3iD6XMamY4rVcZUt4zHSCSpvRoOq8YcsMWzAuR7a3GqhCzfsMSnltYfOmuKZC1W/r NFvQ== 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=MIFicUejfYlSIZZ1hMVx06bcgaqtI0dFCrsliFSKLIg=; b=4TZriDuSjkiN6RyXMDEHr9ourQ26sUjIJvGK9dDsa7YqZixuUwVLIyExvRlscE0qS0 I4owtUOxsGoYFwJtSho0mYvbNdJ00ItaZ3tNGgH4QiscgnJWFzdFYiaDH1s2p1nH8BNd aye2JD+9D5b6ZXBF+I4prw81BeRUc0VRmMJwNFAnh+eUFCnkYcbbg3gDdvDKQbq2TR99 0sKlONLWmw9qE5s04JJ0SHShd5R5TenFOe7K0aKz1unfZhUtdT/Gk4NnzeIusQuHTXc6 D5+lX2nxpLe3ImJWqpL1TmoIuXwHFrtogbHpVIrNjb8qgMiLnNq1S1wVwYxmKXGjMBiZ Elmw== X-Gm-Message-State: AOAM533xHLYmOHte1MCN+rXQAWI3Lvrxghaf+1mJTJ6A5cPvaw8096h/ Uvk7T1NYo7JJjHTW0UNgxVLSwrgfUAgZYQ== X-Received: by 2002:a05:6214:c23:: with SMTP id a3mr270358qvd.52.1644377368102; Tue, 08 Feb 2022 19:29:28 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:27 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:53 -0500 Message-Id: <20220209032854.565698-8-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 7/8] avpriv_find_start_code(): constify pointer parameters 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: WnfvC+JVtyaq Have the compiler enforce not changing the addresses these parameters point to. No functional change. --- libavcodec/utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 68d126acd8..d11d92d21e 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -937,8 +937,8 @@ void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, in #endif const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, - const uint8_t *end, - uint32_t *av_restrict start_code) + const uint8_t * const end, + uint32_t * const av_restrict start_code) { av_assert0(p <= end); if (p >= end) From patchwork Wed Feb 9 03:28:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34204 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1372185iov; Tue, 8 Feb 2022 19:30:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJxP6N/DZdA4nZnHky5QuBdZK03S/MXaH3ciqaUxcgRqI5u5D2hXmjw1PBv7D43v0hSwdxkJ X-Received: by 2002:a05:6402:35d4:: with SMTP id z20mr430777edc.13.1644377455520; Tue, 08 Feb 2022 19:30:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644377455; cv=none; d=google.com; s=arc-20160816; b=odV/Zy16jdwzdLLXgDqOBUvpqL/gn7HC6FJXG5wIIEkryGgOfs1/ewWQotWyzVxX32 FcfixD2DZ48a04uIZsvorhAsdosWMYAABj+LC/abbyp08X/lkG+stJJuVHstNK+wpyEE 3Xot4AE+YAE7jvEqr5zgKKAmRDOC7g9FQ/1yYEfPdmnEAr0zyG4sFD8+UdW9bBYXgdvn p0ZzW1iRqrEPT8Glg71EkgTOegOhu4UT3A5FfVWQtaY9TwuLduc2FHHrf0y1u6ThqQXz i29YWkEVrXzIDFA+KXaQflvupOwuAF5/zGsKBMsoJ7uv79F+B38wpJGR1aROlvt+1abU mmgA== 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=jNDWp0XajFWmStwd/yvDgtLtxq7ON8ggWeLet0ygBOU=; b=qaFYrJYHBE8FDnB8vGb0tsSldMW8h67aO+Z0JnRUhdSoyr/Vc0sfS6GzZciBkrKO+7 A5ZGSj2P+8S392/fcjGuYMQraNj1i92jz1/4EKuvqI5UjE68Em4Z5HCFQu/1kGPnju+2 LSZEnkuvw9u4mPc7m8Bd/ux5LINnt3+coP/r4tqvpI/AF7taeIZDTFgNL1AgN9qdOxMm MHEZ5rzrTIvliQ/GBXdo22qxFb0u6BR+75II72R4CkX6Ngzo3F1aJsKifiHhJmBLrmqF 9NJ9xRHmL5HeRqSLY27NDnnKAh/Nkj+6BNSirAJV3K/nhjN5nHTyL74qOJ4AND5G0Lnc TvGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=V24LQfPc; 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 g17si10885024ejm.445.2022.02.08.19.30.55; Tue, 08 Feb 2022 19:30:55 -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=V24LQfPc; 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 319E068B1F4; Wed, 9 Feb 2022 05:29:40 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 71E9168B17F for ; Wed, 9 Feb 2022 05:29:30 +0200 (EET) Received: by mail-qk1-f172.google.com with SMTP id w8so658445qkw.8 for ; Tue, 08 Feb 2022 19:29:30 -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=kXT19gqDMjSue9hf7JlCKabkJvGJktKxK1V20rJ0toQ=; b=V24LQfPc2E7QRv+sheNt2Y9gjcbl3zLuv5hY04PTh3LMrZ3hqX74jtIEyBmescI5Xd n1zNCD1aOdDDBxBOTYiWmtgnIH9/j0JaMv2lcP1Gb8gFDiTksOGVE3zE1nGh0cs81Hwi RrZRbV849drg/TIPf9i9da6/iWRxUz/27OxrrfqZEeyINpsc5WMq6gY46785WT/ttfbx OeTvjPTzWHDfRytbsr3rALW13ZrbkBBpq8D9ioVcQx2lwtZAC49S10jLhE6SJ0NDjTnP CzaszOR27E6/D9VaLhfq4uNsIM78WlPh1Qmj1+Gp0dizCRstdJ7JIZR4Ub9yDrH227BK YsAA== 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=kXT19gqDMjSue9hf7JlCKabkJvGJktKxK1V20rJ0toQ=; b=sq32KW2udbB1OmcSwfgSgU4ckYI/h0ymqkPPiOoNkVJRK2W2Qzjrxa9/TbIc7rhAkA JxQPwjl+ZKerHKDC1YBU4oZmB+XJvDldus96GKgjEP86WSK/zkadn5rpGyHu+kzYp4Uj vaJvB5JbwH7ruPrMATvAKNxGIjrJV8gPMfZR6aI+CjkhCJD+lHlmlTRaHU0xksjjkAeN AeAoF0qG3ffz98DYFxkj8AXcThHZ7PEbkfVN64O2jYBFjEr9SkhjU3fyO7UKbEDwJMVt TWz3F/8TzbAofs+sgG1DKgupQDswsqRghWVJXeABZvhhhmOSWucUGchah881fGOrUdUD M/zg== X-Gm-Message-State: AOAM533B5yuCweU4zm9wAQYDG6ZderBj4KPc8XHnx+Dsp4ap/ADRx5P/ 4+FEhmfowwGTr+ekI02/k7XFusyPCBfGdg== X-Received: by 2002:a05:620a:4502:: with SMTP id t2mr142860qkp.311.1644377368867; Tue, 08 Feb 2022 19:29:28 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id o19sm8452524qta.19.2022.02.08.19.29.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 19:29:28 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 22:28:54 -0500 Message-Id: <20220209032854.565698-9-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220209032854.565698-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220209032854.565698-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 8/8] avpriv_find_start_code(): make start_code output only 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: jt67A5ZJ9atM The input/output functionality was used by only (ff_)mpeg1_find_frame_end(). If the state/start_code input is a local variable and only one buffer is used, then no history is needed. In loops and inline functions: if ignoring history, don't initialize start_code, so it isn't reset twice each time. There is a slight functional change: 00 00 01 00 01 XX no longer incorrectly returns a start code at offset 7 that overlaps the start code at offset 4 if the start_code input is not modified between the two calls. --- libavcodec/cbs_mpeg2.c | 5 ---- libavcodec/h264_parser.c | 2 +- libavcodec/mpeg12.c | 41 +++++++++++++++++++++++++- libavcodec/mpeg4_unpack_bframes_bsf.c | 1 - libavcodec/mpegvideo_parser.c | 42 +++++++++++++++++++++++++-- libavcodec/startcode.h | 14 +++------ libavcodec/utils.c | 16 ++++------ libavcodec/vc1_common.h | 2 +- libavformat/rtpenc_mpv.c | 2 +- 9 files changed, 92 insertions(+), 33 deletions(-) diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c index f0a2265938..870119bab0 100644 --- a/libavcodec/cbs_mpeg2.c +++ b/libavcodec/cbs_mpeg2.c @@ -160,11 +160,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx, const uint8_t *end; size_t unit_size; - // Reset start_code to ensure that avpriv_find_start_code() - // really reads a new start code and does not reuse the old - // start code in any way (as e.g. happens when there is a - // Sequence End unit at the very end of a packet). - start_code = UINT32_MAX; end = avpriv_find_start_code(start--, frag->data + frag->data_size, &start_code); diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c index 50810f1789..b67830d40e 100644 --- a/libavcodec/h264_parser.c +++ b/libavcodec/h264_parser.c @@ -69,7 +69,7 @@ typedef struct H264ParseContext { static int find_start_code(const uint8_t *buf, int buf_size, int buf_index, int next_avc) { - uint32_t state = -1; + uint32_t state; buf_index = avpriv_find_start_code(buf + buf_index, buf + next_avc + 1, &state) - buf - 1; diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index 85f4f432bd..9639423152 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -165,6 +165,45 @@ av_cold void ff_mpeg12_init_vlcs(void) } #if FF_API_FLAG_TRUNCATED +/** + * By preserving the @p start_code value between subsequent calls, the caller can + * detect start codes across buffer boundaries. + * + * @param[in,out] start_code A pointer to a mutable @c uint32_t.
+ * As input: For no history preset to @c ~0 , otherwise preset to the last + * returned start code to enable detecting start codes across + * buffer boundaries.
+ * On output: Set to the found start code if it exists or an invalid + * start code (the 4 bytes prior to the returned value, + * using the input history if @f$ end - p < 4 @f$). + * + * @sa avpriv_find_start_code() + */ +static const uint8_t *find_start_code_truncated(const uint8_t *av_restrict p, + const uint8_t * const end, + uint32_t * const av_restrict start_code) +{ + av_assert0(p <= end); + if (p >= end) + return end; + + if (*start_code == 0x100) + *start_code = ~0; + // invalidate byte 0 so overlapping start codes are not erroneously detected + + // read up to the first three bytes in p to enable reading a start code across + // two (to four) buffers + for (int i = 0; i < 3; i++) { + *start_code <<= 8; + *start_code += *p; + p++; + if (start_code_is_valid(*start_code) || p == end) + return p; + } + // buffer length is at least 4 + return avpriv_find_start_code(p - 3, end, start_code); +} + /** * Find the end of the current frame in the bitstream. * @return the position of the first byte of the next frame, or -1 @@ -199,7 +238,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, } state++; } else { - i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1; + i = find_start_code_truncated(buf + i, buf + buf_size, &state) - buf - 1; if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) { i++; pc->frame_start_found = 4; diff --git a/libavcodec/mpeg4_unpack_bframes_bsf.c b/libavcodec/mpeg4_unpack_bframes_bsf.c index ae2c129d88..1030dd60aa 100644 --- a/libavcodec/mpeg4_unpack_bframes_bsf.c +++ b/libavcodec/mpeg4_unpack_bframes_bsf.c @@ -36,7 +36,6 @@ static void scan_buffer(const uint8_t *buf, int buf_size, const uint8_t *end = buf + buf_size, *pos = buf; while (pos < end) { - startcode = -1; pos = avpriv_find_start_code(pos, end, &startcode); if (startcode == USER_DATA_STARTCODE && pos_p) { diff --git a/libavcodec/mpegvideo_parser.c b/libavcodec/mpegvideo_parser.c index 82fdd1a1bb..0c42c6397a 100644 --- a/libavcodec/mpegvideo_parser.c +++ b/libavcodec/mpegvideo_parser.c @@ -33,6 +33,45 @@ struct MpvParseContext { }; #if !FF_API_FLAG_TRUNCATED +/** + * By preserving the @p start_code value between subsequent calls, the caller can + * detect start codes across buffer boundaries. + * + * @param[in,out] start_code A pointer to a mutable @c uint32_t.
+ * As input: For no history preset to @c ~0 , otherwise preset to the last + * returned start code to enable detecting start codes across + * buffer boundaries.
+ * On output: Set to the found start code if it exists or an invalid + * start code (the 4 bytes prior to the returned value, + * using the input history if @f$ end - p < 4 @f$). + * + * @sa avpriv_find_start_code() + */ +static const uint8_t *find_start_code_truncated(const uint8_t *av_restrict p, + const uint8_t * const end, + uint32_t * const av_restrict start_code) +{ + av_assert0(p <= end); + if (p >= end) + return end; + + if (*start_code == 0x100) + *start_code = ~0; + // invalidate byte 0 so overlapping start codes are not erroneously detected + + // read up to the first three bytes in p to enable reading a start code across + // two (to four) buffers + for (int i = 0; i < 3; i++) { + *start_code <<= 8; + *start_code += *p; + p++; + if (start_code_is_valid(*start_code) || p == end) + return p; + } + // buffer length is at least 4 + return avpriv_find_start_code(p - 3, end, start_code); +} + /** * Find the end of the current frame in the bitstream. * @return the position of the first byte of the next frame, or -1 @@ -68,7 +107,7 @@ static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, } state++; } else { - i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1; + i = find_start_code_truncated(buf + i, buf + buf_size, &state) - buf - 1; if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) { i++; pc->frame_start_found = 4; @@ -120,7 +159,6 @@ static void mpegvideo_extract_headers(AVCodecParserContext *s, s->repeat_pict = 0; while (buf < buf_end) { - start_code= -1; buf= avpriv_find_start_code(buf, buf_end, &start_code); bytes_left = buf_end - buf; switch(start_code) { diff --git a/libavcodec/startcode.h b/libavcodec/startcode.h index 69389c729c..7e1df68a3b 100644 --- a/libavcodec/startcode.h +++ b/libavcodec/startcode.h @@ -50,20 +50,14 @@ static av_always_inline int start_code_is_valid(uint32_t start_code) { * A start code is a sequence of 4 bytes with the hexadecimal value 00 00 01 XX , * where XX represents any value and memory address increases left to right. * - * By preserving the @p start_code value between subsequent calls, the caller can - * detect start codes across buffer boundaries. - * * @param[in] p A pointer to the start of the memory buffer to scan. * @param[in] end A pointer to the past-the-end memory address for the buffer * given by @p p. @p p must be ≤ @p end. * - * @param[in,out] start_code A pointer to a mutable @c uint32_t.
- * As input: For no history preset to @c ~0 , otherwise preset to the last - * returned start code to enable detecting start codes across - * buffer boundaries.
- * On output: Set to the found start code if it exists or an invalid - * start code (the 4 bytes prior to the returned value, - * using the input history if @f$ end - p < 4 @f$). + * @param[out] start_code A pointer to a mutable @c uint32_t.
+ * Set to the found start code if it exists or an invalid start code + * (the 4 bytes prior to the returned value or @c ~0 if + * @f$ end - p < 4 @f$). * * @return A pointer to the memory address following the found start code, or @p end * if no start code was found. diff --git a/libavcodec/utils.c b/libavcodec/utils.c index d11d92d21e..b9a396b181 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -941,19 +941,13 @@ const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p, uint32_t * const av_restrict start_code) { av_assert0(p <= end); - if (p >= end) + // minimum length for a start code + if (p + 4 > end) { + *start_code = ~0; // set to an invalid start code return end; - - // read up to the first three bytes in p to enable reading a start code across - // two (to four) buffers - for (int i = 0; i < 3; i++) { - *start_code <<= 8; - *start_code += *p; - p++; - if (start_code_is_valid(*start_code) || p == end) - return p; } - // p is now properly incremented for the negative indices in the while loop + + p += 3; // offset for negative indices in while loop /* with memory address increasing left to right, we are looking for (in hexadecimal): * 00 00 01 XX diff --git a/libavcodec/vc1_common.h b/libavcodec/vc1_common.h index 8ff9802a51..ac8dbe3fb6 100644 --- a/libavcodec/vc1_common.h +++ b/libavcodec/vc1_common.h @@ -57,7 +57,7 @@ enum Profile { static av_always_inline const uint8_t* find_next_marker(const uint8_t *src, const uint8_t *end) { if (end - src >= 4) { - uint32_t mrk = 0xFFFFFFFF; + uint32_t mrk; src = avpriv_find_start_code(src, end, &mrk); if (start_code_is_valid(mrk)) return src - 4; diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c index 9c0816ef95..dbd4acd474 100644 --- a/libavformat/rtpenc_mpv.c +++ b/libavformat/rtpenc_mpv.c @@ -54,7 +54,7 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size) r1 = buf1; while (1) { - uint32_t start_code = ~0; + uint32_t start_code; r = avpriv_find_start_code(r1, end, &start_code); if (start_code_is_valid(start_code)) { /* New start code found */