From patchwork Fri Sep 16 18:19:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 37960 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp1139466pzh; Fri, 16 Sep 2022 11:20:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6gvs1kQKCJmYP3SFKMP8tcexxFzbom9gsqdcn9A1uERqMu0H7ItgHbrO/bbXuLI5HC+qeV X-Received: by 2002:aa7:d883:0:b0:44e:bbbe:d661 with SMTP id u3-20020aa7d883000000b0044ebbbed661mr5022381edq.248.1663352429046; Fri, 16 Sep 2022 11:20:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663352429; cv=none; d=google.com; s=arc-20160816; b=j2BxNlXUmFQxYu91q/hmVvZUCC1bnaCDDFPmBWcVWhNgfncBQYJy2ZIbRizWAcFf39 m8lXf01iH565UMHrIaWLRB2lHdPwWIGCRNYS728o/fqloJJOoF/m5lheuLwFtRCOm/8+ OgQ7cMe9HFou2expnmEzY5NMj/I9ikbltKvWTn6Sjn86g8cLepsIp+hRAbGS91PP5bBW UXaBMWkxlH1bgWgeYxxPhIftTJhqSrAkXzmJ46XX1rx/w2Zt3rX83tt5I9xOn3dlI8fm H5+157lQVxKencpucgB268B/Uu/rJqPrucBx14epzJUmy+aq5xzVnRplMP75HBtYrl30 hORw== 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=fNcR1KsoBYcon8659fmJ2mzFcweTCrNsbt8FBQXc+v0=; b=Xjhmn8UIFUbKD9QFleCevkBeqZWSUAT044TFzWSYjDKgYoHxdql/mYM/qwAk6JWX9U BBagRTaTzQfT5Lvw7OsXedOTVDhJxah5U1Y+y1/XDDaMopBjQWJ+M7w2RDmcKAx2vz+C FlfQxeyrFJtzn5QrPkaK9N91ACkMJkVMS6g1Dk7fhuv3Ft2SM55IbPF0E9MbyUU4Ko4Y FqHEzf10Wtq2A2ZojYf6zkf6tEBM057jTxye9q2Thpn41/ceL76phpWZVSo8aYqs4BqE Jt2pZfxMUGcEXBBl9fMTLgAuWziiVXJkHz7Tys4LgeZfjzkCv3AuRee1K+vuqFVvH6X6 3FNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=P6YweL0K; 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 di11-20020a170906730b00b00780afde0bb3si2083735ejc.70.2022.09.16.11.20.28; Fri, 16 Sep 2022 11:20:29 -0700 (PDT) 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=P6YweL0K; 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 0306C68BC3B; Fri, 16 Sep 2022 21:20:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 25F1868BAEB for ; Fri, 16 Sep 2022 21:20:10 +0300 (EEST) Received: by mail-qk1-f169.google.com with SMTP id d17so15566074qko.13 for ; Fri, 16 Sep 2022 11:20:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=ccmXZCu/hsDZvFg+vY7Vhhk0Jq50BcGjVu/mkJl/Y2g=; b=P6YweL0K0RLiHTQlYvd9QNAt5XzHvJbB1xSwiHz/ALsGSmxjNvkWrSum8oO7d/+hgm /lXcSPMID+qfVyo26a9xLHFPjcxYO17T8GmfrUyVYEjZUlmSJ8xkYl5gLc2hhP+C4eKL r377NI/Ps2QlU9YyqzqMuXuWDThxy0napRDBnO/qk4XE70vNc/BTu6IvQQwAsl7TkJv6 /rlKuJN0n4HFR/jsHIDfT0eiCUuAY3rf99pqqVEJW1yS3vDhRP5BUCj2LfVUDE7dGhBf Wixko7YJjcTMbo4RGO97K5G1M2lLmZ94bJFdTVXUiiuUEM8YuvdfZ2dfNCmrrYtQYiBW MNoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=ccmXZCu/hsDZvFg+vY7Vhhk0Jq50BcGjVu/mkJl/Y2g=; b=6ZZrV7wFe0+f2wp3p1gEisu60CVuW8Pmtgxk+aLO4xvs6s4Al1qsg2ueKsN2QkICrs HsX52SzGLa9lT01fJKN1SxQlOiAlRlZUUdlSL40EkjjY4EqrzgJw1YfcL35pQMgoD/Kt QFExRyCjFATOUZJiKXgHNtnxrVsekKQoRVKR5K7QN5nxy6J+A2EksE+dsIxn2c4qNLIf MwaqL5O2X4pizbdBPfVjzXCU6zHscqoguuGBZe0MxpgVzf6EyeIqIHuGEZdrsMTuzZ/T uHGrmWa6kVrHEx/7gRGiJOkBH6PIwgdWQeONjGLPI6Y0XkBfhYYv17TN8bNgGYXqQWcI SUFg== X-Gm-Message-State: ACrzQf0W04AYj5DiD44OSTnFcaoB0euCuuTbR9EaFSmPYCv6b6i4uWPG INFc14wde2eEjvSphw+cQrKNtjDWCzA= X-Received: by 2002:a05:620a:2588:b0:6c9:2daa:9c6d with SMTP id x8-20020a05620a258800b006c92daa9c6dmr4964718qko.630.1663352408391; Fri, 16 Sep 2022 11:20:08 -0700 (PDT) Received: from scott-desktop.. (pool-71-114-80-52.washdc.fios.verizon.net. [71.114.80.52]) by smtp.gmail.com with ESMTPSA id l10-20020ac8724a000000b0035cb9531851sm5148098qtp.65.2022.09.16.11.20.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Sep 2022 11:20:07 -0700 (PDT) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Fri, 16 Sep 2022 14:19:55 -0400 Message-Id: <20220916182002.122699-2-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220916182002.122699-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> <20220916182002.122699-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 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: reHV9HfSPjsn 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 3e8be65968..0c19d0e3f6 100644 --- a/libavcodec/cavsdec.c +++ b/libavcodec/cavsdec.c @@ -1257,7 +1257,7 @@ static int cavs_decode_frame(AVCodecContext *avctx, AVFrame *rframe, 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 04b0c7f87d..23839ca47b 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 329b1a6174..e698a2632e 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 d78e25a777..5ff1830496 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -214,7 +214,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 7133696f3c..b857353ab8 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -2446,7 +2446,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 ac6efb6909..f5afa95981 100644 --- a/libavcodec/mpegvideo_parser.c +++ b/libavcodec/mpegvideo_parser.c @@ -83,7 +83,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 66b7d00bd8..1914cf19d5 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 d82336a3b3..33de4a63c8 100644 --- a/libavcodec/vaapi_vc1.c +++ b/libavcodec/vaapi_vc1.c @@ -473,7 +473,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 9f32e82f9e..d98c15f2e4 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -667,7 +667,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, 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 b89851fbbb..398f1192dd 100644 --- a/libavformat/cavsvideodec.c +++ b/libavformat/cavsvideodec.c @@ -39,7 +39,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;