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 */