From patchwork Tue Feb 1 21:20:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Theisen X-Patchwork-Id: 34050 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp983623iov; Tue, 1 Feb 2022 13:22:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFr+4K9uwUVTFlNechA4+g8hG1ISv4IKK+DM6hou7jhyejNI+xR3DFIQsaOtNf9/YSnVI2 X-Received: by 2002:aa7:d312:: with SMTP id p18mr26442986edq.49.1643750542273; Tue, 01 Feb 2022 13:22:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643750542; cv=none; d=google.com; s=arc-20160816; b=aHuOJDtHgA1Hqp1Ixwbsb/dY8zAuS1lO3bh5a6dDBR5KQrnq2VqeD0BuZJZoInYas3 6Jyn2+XUAP8napvqX2x2ve/bdaIBDbvQQOLi8R5U/wkIzupWdZLt6NpKWTlFld1fsz5B DoJBpGgxJFufTKWo15+lmiUAmurzZvVqTlQWu1m0BKUPGl19BscTcLiqTVjBQFXyfffY 6PEOHgpiWzsNHBpjOk62gSqoeUrxi8GyT95CnbUOjzpp3cP24hO6o5DAUVtGprG3pt4X HffiejcAzlQvlY0ymkrxULlTMk27F6j8yi9e4LOCBLfh8m3nXpAqhY4ANyWGwEsNWwZy VqAw== 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=hrn2z/O7F42z/QO5oC+Bl78S/+yaoW/fNbrf4HcLxPo=; b=QeMwZ5YfkrfaJ47LDvE0gRNTVK5WoY+Cb6GSl6x31I+LizDlXdgWMqCIHE+YV4bHiF zEjghmBtg0hpMVt75Qr75FA0OUIBQjkRx4UyiWC3IZt+LHYp8RYeKRc/uvGPphEgSHq+ zm/jCW8FyJ1tA1LZ1BaVrGJ/aiMS7W4LXq1b09ygEqiC9VFo9MDN5kyUffEw2TI0UxJX Rj8BpK86FR5uyBaUOuzCZaip1pmTtqXj99/CuNsYqJ7lXO0WZu3ywGYHpcBW1u8RLS66 a8ZSVhv5cVK+hzJKWtyVQL4BEgv1iUYputLk8YbSlqZcbQoBfc2bkt16Xc2VmvAUAWSm LXGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b="mVwD/WYx"; 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 d1si9631233edq.633.2022.02.01.13.22.21; Tue, 01 Feb 2022 13:22:22 -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="mVwD/WYx"; 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 D6C4668B094; Tue, 1 Feb 2022 23:21:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 335E1688180 for ; Tue, 1 Feb 2022 23:21:12 +0200 (EET) Received: by mail-qv1-f45.google.com with SMTP id a7so17255995qvl.1 for ; Tue, 01 Feb 2022 13:21:12 -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=YwfZ84bqx6sfEVAEs3drKRehb16E0Ayb1E2OZ8KCxTQ=; b=mVwD/WYxMF6osp8XIy0KTLYGX6evqvhbNRzfbVZKmc73TZp4q4MOn+ByHnAWBxh73E 34VYRZaU0pYovh/ol53I/VlwZnRjbMpVXUYCQXQ0q/onJ7iL4BD9FsTmvJlHrIq6du9b zLz8Nbof73TbzpAFtwqema56+p5xLPKHLnjYHebpyW5tPKu7rZQXe/wDRorIsdPULC+c hcvf0TXhCNTfl3uFbMJyE419zlo7Akh2YWH4zWGyvHt7UjqQ1pJhC5jPoYVn0nr+cgzt 8Cl6hjMIlMfM0cdNTDqTxImd+/bnDgq6l9utq/FolhoG0gLimX8JVS2fB1eeblKjOJOm l8lg== 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=YwfZ84bqx6sfEVAEs3drKRehb16E0Ayb1E2OZ8KCxTQ=; b=dOv0Xy9pdxwNDy7NuVgLoCg6tanlYC2IZOpJg2pMyQcKM3PpljAjvvnRYVT49Swn6y StiNqlF39q1ijptyjXbXbJBOXH1XhAxJhZLuTflw53NZ0U1wA+X9hYcoD6Q+MjARR690 uo+m+54ZpyDBvZlYncVs/5PTKn3B7e4AFhBavoZph/Qrh/W5qZbc4q4GQxj4gw8Sfn2O cSm17GhdYgMhA8qKFKBecoNXlusZecSYr01Glu3IuTx35vD0/BqcsbHk5is8LxSK/nFQ FbYCVB114vSZSXJ8jPAvikd00rk68d/UJUcqJM+eu6yt8cflhQBnXdT4dsfWBgDJryZh Qx+w== X-Gm-Message-State: AOAM533Py5qk/rVdvGr6g84EZv0zxFMcLDEDOKZMcHJG9+Lxp1G9RoP/ b5KiyIbSdWWGeEfaEn3JYabyhkBFTuAwbA== X-Received: by 2002:ad4:5bc4:: with SMTP id t4mr24225731qvt.38.1643750470476; Tue, 01 Feb 2022 13:21:10 -0800 (PST) Received: from localhost.localdomain ([151.200.235.219]) by smtp.gmail.com with ESMTPSA id ay18sm2928225qkb.8.2022.02.01.13.21.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 13:21:10 -0800 (PST) From: Scott Theisen To: ffmpeg-devel@ffmpeg.org Date: Tue, 1 Feb 2022 16:20:54 -0500 Message-Id: <20220201212056.29712-7-scott.the.elm@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220201212056.29712-1-scott.the.elm@gmail.com> References: <20220201212056.29712-1-scott.the.elm@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/8] avcodec/internal.h: create avpriv_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: j5/J+uMGJf9+ --- libavcodec/cavsdec.c | 2 +- libavcodec/cbs_mpeg2.c | 4 ++-- libavcodec/extract_extradata_bsf.c | 2 +- libavcodec/internal.h | 14 ++++++++++++++ libavcodec/mpeg12.c | 2 +- libavcodec/mpeg12dec.c | 2 +- libavcodec/mpegvideo_parser.c | 2 +- libavcodec/remove_extradata_bsf.c | 8 +++----- 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, 34 insertions(+), 24 deletions(-) diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c index 692c77eb39..a62177d520 100644 --- a/libavcodec/cavsdec.c +++ b/libavcodec/cavsdec.c @@ -1249,7 +1249,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 (!avpriv_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 03ea49cbca..648b270f44 100644 --- a/libavcodec/cbs_mpeg2.c +++ b/libavcodec/cbs_mpeg2.c @@ -152,7 +152,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 (!avpriv_start_code_is_valid(start_code)) { // No start code found. return AVERROR_INVALIDDATA; } @@ -167,7 +167,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 (avpriv_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; diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c index dbcb8508b0..4df1c97139 100644 --- a/libavcodec/extract_extradata_bsf.c +++ b/libavcodec/extract_extradata_bsf.c @@ -240,7 +240,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 && avpriv_start_code_is_valid(state)) { extradata_size = ptr - 4 - pkt->data; break; } diff --git a/libavcodec/internal.h b/libavcodec/internal.h index 07098e1522..2f9f99482c 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -285,6 +285,20 @@ int ff_thread_can_start_frame(AVCodecContext *avctx); int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx); +/** + * @brief Test whether a start code found by avpriv_find_start_code() is valid. + * + * Use this to test the validity of a start code or 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. + * + * @param[in] start_code The start code to test. + * @return A boolean that is true if and only if start_code is valid + */ +static av_always_inline int avpriv_start_code_is_valid(uint32_t start_code) { + return (start_code & 0xFFFFFF00) == 0x100; +} + /** * Find the first start code in the buffer p. A start code is a sequence of 4 * bytes, with memory address increasing left to right and in hexadecimal, with diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index 58e03c05d4..e45bc74479 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -217,7 +217,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 && avpriv_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 1110fcb319..691b535bfc 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -2476,7 +2476,7 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture, /* find next start code */ uint32_t start_code; buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code); - if (start_code > 0x1ff) { + if (!avpriv_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 c991a82405..9fd145586f 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 && avpriv_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 1d5f193f89..0e42174912 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 (!avpriv_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 (!avpriv_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 && avpriv_start_code_is_valid(state)) return ptr - 4 - buf; } diff --git a/libavcodec/vaapi_vc1.c b/libavcodec/vaapi_vc1.c index 4e9607d9be..379104f688 100644 --- a/libavcodec/vaapi_vc1.c +++ b/libavcodec/vaapi_vc1.c @@ -471,7 +471,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 && avpriv_start_code_is_valid(AV_RB32(buffer))) { buffer += 4; size -= 4; } diff --git a/libavcodec/vc1_common.h b/libavcodec/vc1_common.h index b46c33f7e2..483f86a4ee 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 (avpriv_start_code_is_valid(mrk)) return src - 4; } return end; diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index 7ed5133cfa..86749e5973 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 (avpriv_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 51908d2b63..bdeb746fc7 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 (avpriv_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 253caa7c1d..2daccd3d15 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 (avpriv_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 8900b97597..1d007708cc 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 (avpriv_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 2d6f81aaa1..a9829dc1df 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 (avpriv_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 bb63c9bdc6..abfe3b32eb 100644 --- a/libavformat/rtpenc_mpv.c +++ b/libavformat/rtpenc_mpv.c @@ -56,7 +56,7 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size) while (1) { uint32_t start_code; r = avpriv_find_start_code(r1, end, &start_code); - if((start_code & 0xFFFFFF00) == 0x100) { + if (avpriv_start_code_is_valid(start_code)) { /* New start code found */ if (start_code == 0x100) { frame_type = (r[1] & 0x38) >> 3;