From: Scott Theisen <scott.the.elm@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Scott Theisen <scott.the.elm@gmail.com>
Subject: [FFmpeg-devel] [PATCH v2 01/13] avcodec/internal.h: create avpriv_start_code_is_valid()
Date: Thu, 3 Feb 2022 13:44:38 -0500
Message-ID: <20220203184450.5491-2-scott.the.elm@gmail.com> (raw)
In-Reply-To: <20220203184450.5491-1-scott.the.elm@gmail.com>
---
libavcodec/cavsdec.c | 2 +-
libavcodec/cbs_mpeg2.c | 4 ++--
| 2 +-
libavcodec/internal.h | 15 +++++++++++++++
libavcodec/mpeg12.c | 2 +-
libavcodec/mpeg12dec.c | 2 +-
libavcodec/mpegvideo_parser.c | 2 +-
| 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, 35 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 26400f279f..eb45929132 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;
}
@@ -175,7 +175,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;
--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 72ca1553f6..005f308b70 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -285,6 +285,21 @@ 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 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 <b>@c end </b>).
+ *
+ * @param[in] start_code The start code to test.
+ * @return A boolean that is true if and only if <b>@p start_code</b> is valid
+ */
+static av_always_inline int avpriv_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/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 4a7bd6d466..65b66d11f8 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -2477,7 +2477,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 (!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 c5dc867d24..f0897e7e2c 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;
--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 4b45f51772..05a77fa11c 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 (avpriv_start_code_is_valid(start_code)) {
/* New start code found */
if (start_code == 0x100) {
frame_type = (r[1] & 0x38) >> 3;
--
2.32.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2022-02-03 18:45 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-01 21:20 [FFmpeg-devel] [PATCH 0/8] rewrite avpriv_find_start_code() for clarity Scott Theisen
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 1/8] avpriv_find_start_code(): readability enhancement part 1 Scott Theisen
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 2/8] avpriv_find_start_code(): rewrite while loop and add comments for clarity Scott Theisen
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 3/8] avpriv_find_start_code(): make the state parameter output only Scott Theisen
2022-02-02 0:15 ` Andreas Rheinhardt
2022-02-02 3:25 ` Scott Theisen
2022-02-02 3:29 ` Andreas Rheinhardt
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 4/8] avpriv_find_start_code(): add doxygen comment and rename a parameter Scott Theisen
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 5/8] avpriv_find_start_code(): replace unnecessary for loop Scott Theisen
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 6/8] avcodec/internal.h: create avpriv_start_code_is_valid() Scott Theisen
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 7/8] cbs_mpeg2.c: use a while loop with a loop condition instead of an infinite loop Scott Theisen
2022-02-01 21:20 ` [FFmpeg-devel] [PATCH 8/8] avpriv_find_start_code(): reduce the number of iterations Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 0/13] rewrite avpriv_find_start_code() for clarity Scott Theisen
2022-02-03 18:44 ` Scott Theisen [this message]
2022-02-05 5:42 ` [FFmpeg-devel] [PATCH v2 01/13] avcodec/internal.h: create avpriv_start_code_is_valid() Andreas Rheinhardt
2022-02-05 5:57 ` Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 02/13] avpriv_find_start_code(): readability enhancement part 1 Scott Theisen
2022-02-05 6:26 ` Andreas Rheinhardt
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 03/13] avpriv_find_start_code(): rewrite while loop and add comments for clarity Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 04/13] avpriv_find_start_code(): rewrite for loop " Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 05/13] avpriv_find_start_code(): add doxygen comment and rename a parameter Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 06/13] avpriv_find_start_code(): correct type of start_code parameter Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 07/13] avpriv_find_start_code(): constify pointer parameters Scott Theisen
2022-02-05 5:49 ` Andreas Rheinhardt
2022-02-05 6:08 ` Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 08/13] avpriv_find_start_code(): add parameter for ignoring history Scott Theisen
2022-02-05 6:10 ` Andreas Rheinhardt
2022-02-05 9:00 ` Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 09/13] avpriv_find_start_code(): fix indent from previous commit Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 10/13] cbs_mpeg2.c: use a while loop with a loop condition instead of an infinite loop Scott Theisen
2022-02-05 1:48 ` Andreas Rheinhardt
2022-02-05 3:47 ` Scott Theisen
2022-02-05 4:06 ` Andreas Rheinhardt
2022-02-05 4:09 ` Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 11/13] cbs_mpeg2.c: improve readability of start code search (part 1) Scott Theisen
2022-02-05 2:16 ` Andreas Rheinhardt
2022-02-05 3:53 ` Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 12/13] cbs_mpeg2.c: improve readability of start code search (part 2) Scott Theisen
2022-02-05 2:24 ` Andreas Rheinhardt
2022-02-05 3:59 ` Scott Theisen
2022-02-03 18:44 ` [FFmpeg-devel] [PATCH v2 13/13] cbs_mpeg2.c: improve readability of start code search (part 3) Scott Theisen
2022-02-05 3:54 ` Andreas Rheinhardt
2022-02-05 4:26 ` Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 0/8] rewrite avpriv_find_start_code() for clarity Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 1/8] avcodec/startcode.h: create start_code_is_valid() Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 2/8] avpriv_find_start_code(): readability enhancement part 1 Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 3/8] avpriv_find_start_code(): rewrite while loop Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 4/8] avpriv_find_start_code(): rewrite for loop for clarity Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 5/8] avpriv_find_start_code(): add doxygen comment and rename a parameter Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 6/8] avpriv_find_start_code(): correct type of start_code parameter Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 7/8] avpriv_find_start_code(): constify pointer parameters Scott Theisen
2022-02-09 3:28 ` [FFmpeg-devel] [PATCH v3 8/8] avpriv_find_start_code(): make start_code output only Scott Theisen
2022-03-07 18:44 ` [FFmpeg-devel] [PATCH v3 0/8] rewrite avpriv_find_start_code() for clarity Scott Theisen
2022-09-16 18:19 ` [FFmpeg-devel] [PATCH v4 " Scott Theisen
2022-09-16 18:19 ` [FFmpeg-devel] [PATCH v4 1/8] avcodec/startcode.h: create start_code_is_valid() Scott Theisen
2022-09-16 18:19 ` [FFmpeg-devel] [PATCH v4 2/8] avpriv_find_start_code(): readability enhancement part 1 Scott Theisen
2022-09-16 18:19 ` [FFmpeg-devel] [PATCH v4 3/8] avpriv_find_start_code(): rewrite while loop Scott Theisen
2022-09-16 18:19 ` [FFmpeg-devel] [PATCH v4 4/8] avpriv_find_start_code(): rewrite for loop for clarity Scott Theisen
2022-09-16 18:19 ` [FFmpeg-devel] [PATCH v4 5/8] avpriv_find_start_code(): add doxygen comment and rename a parameter Scott Theisen
2022-09-16 18:20 ` [FFmpeg-devel] [PATCH v4 6/8] avpriv_find_start_code(): correct type of start_code parameter Scott Theisen
2022-09-16 18:20 ` [FFmpeg-devel] [PATCH v4 7/8] avpriv_find_start_code(): constify pointer parameters Scott Theisen
2022-09-16 18:20 ` [FFmpeg-devel] [PATCH v4 8/8] avpriv_find_start_code(): make start_code output only Scott Theisen
2022-09-16 18:26 ` [FFmpeg-devel] [PATCH v4 0/8] rewrite avpriv_find_start_code() for clarity Scott Theisen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220203184450.5491-2-scott.the.elm@gmail.com \
--to=scott.the.elm@gmail.com \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git