Patches attached. - Andreas
From dd62fb6eeb9a5bc9a870430a93c6de1aca4be1d6 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 14:03:25 +0200 Subject: [PATCH 01/21] avutil/avassert: Add av_unreachable and av_assume() macros
Useful to let the compiler and static analyzers know that something is unreachable without adding an av_assert (which would be either dead for the compiler or add runtime overhead) for this. The implementation used here enforces the use of a message to provide a reason why a particular code is supposed to be unreachable. Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- doc/APIchanges | 3 +++ libavutil/avassert.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/doc/APIchanges b/doc/APIchanges index 75d66f87f3..78dcaa8009 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28 API changes, most recent first: +2025-05-06 - xxxxxxxxxx - lavu 60.xx.100 - avassert.h + Add av_unreachable() and av_assume() macros. + 2025-04-21 - xxxxxxxxxx - lavu 60.2.100 - log.h Add AV_CLASS_CATEGORY_HWDEVICE. diff --git a/libavutil/avassert.h b/libavutil/avassert.h index 1895fb7551..d0d5aa0c7e 100644 --- a/libavutil/avassert.h +++ b/libavutil/avassert.h @@ -31,6 +31,7 @@ #ifdef HAVE_AV_CONFIG_H # include "config.h" #endif +#include "attributes.h" #include "log.h" #include "macros.h" @@ -75,4 +76,45 @@ */ void av_assert0_fpu(void); +/** + * Asserts that are used as compiler optimization hints depending + * upon ASSERT_LEVEL and NBDEBUG. + * + * Undefined behaviour occurs if execution reaches a point marked + * with av_unreachable() or if a condition used with av_assume() + * is false. + * + * The condition used with av_assume() should not have side-effects + * and should be visible to the compiler. + */ +#if defined(ASSERT_LEVEL) ? ASSERT_LEVEL > 0 : !defined(HAVE_AV_CONFIG_H) && !defined(NDEBUG) +#define av_unreachable(msg) \ +do { \ + av_log(NULL, AV_LOG_PANIC, \ + "Code at %s:%d that was supposedly unreachable due to '%s' reached\n", \ + __FILE__, __LINE__, msg); \ + abort(); \ +} while (0) +#define av_assume(cond) av_assert0(cond) +#else +#if AV_GCC_VERSION_AT_LEAST(4, 5) || AV_HAS_BUILTIN(__builtin_unreachable) +#define av_unreachable(msg) __builtin_unreachable() +#elif defined(_MSC_VER) +#define av_unreachable(msg) __assume(0) +#define av_assume(cond) __assume(cond) +#elif __STDC_VERSION__ >= 202311L +#include <stddef.h> +#define av_unreachable(msg) unreachable() +#else +#define av_unreachable(msg) ((void)0) +#endif + +#ifndef av_assume +#define av_assume(cond) do { \ + if (!(cond)) \ + av_unreachable(); \ +} while (0) +#endif +#endif + #endif /* AVUTIL_AVASSERT_H */ -- 2.45.2
From 042dc507caa21b688e2fde067607ababd8df7c69 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 14:19:33 +0200 Subject: [PATCH 02/21] avcodec/amrwbdec: Mark default switch as unreachable Alternative fix for Coverity issue #1473499 instead of a3bb269db92601e2dc0e99352468d02f7b26c7c2. Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/amrwbdec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/amrwbdec.c b/libavcodec/amrwbdec.c index 929fc30a3c..91fb870a64 100644 --- a/libavcodec/amrwbdec.c +++ b/libavcodec/amrwbdec.c @@ -556,7 +556,8 @@ static void decode_fixed_vector(float *fixed_vector, const uint16_t *pulse_hi, ((int) pulse_hi[i] << 11), 4, 1); break; default: - av_assert2(0); + av_unreachable("Everything >= MODE_SID is impossible: MODE_SID is patchwelcome," + "> MODE_SID is invalid"); } memset(fixed_vector, 0, sizeof(float) * AMRWB_SFR_SIZE); -- 2.45.2
From ff5bf386642b1d4917455b035880a84b23b9b2a2 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 14:24:33 +0200 Subject: [PATCH 03/21] avcodec/proresenc_anatoliy: Mark impossible case as unreachable Alternative fix for fix Coverity issue 1440385 (instead of 6106177ad66ab28f44520534f386239d2405eeab). Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/proresenc_anatoliy.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c index fc69b94780..2abb554bd4 100644 --- a/libavcodec/proresenc_anatoliy.c +++ b/libavcodec/proresenc_anatoliy.c @@ -27,6 +27,7 @@ * Known FOURCCs: 'ap4h' (444), 'apch' (HQ), 'apcn' (422), 'apcs' (LT), 'acpo' (Proxy) */ +#include "libavutil/avassert.h" #include "libavutil/mem.h" #include "libavutil/mem_internal.h" #include "libavutil/opt.h" @@ -845,20 +846,25 @@ static av_cold int prores_encode_init(AVCodecContext *avctx) } if (avctx->profile == AV_PROFILE_UNKNOWN) { - if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10) { + switch (avctx->pix_fmt) { + case AV_PIX_FMT_YUV422P10: avctx->profile = AV_PROFILE_PRORES_STANDARD; av_log(avctx, AV_LOG_INFO, "encoding with ProRes standard (apcn) profile\n"); - } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P10) { + break; + case AV_PIX_FMT_YUV444P10: avctx->profile = AV_PROFILE_PRORES_4444; av_log(avctx, AV_LOG_INFO, "encoding with ProRes 4444 (ap4h) profile\n"); - } else if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P10) { + break; + case AV_PIX_FMT_YUVA444P10: avctx->profile = AV_PROFILE_PRORES_4444; av_log(avctx, AV_LOG_INFO, "encoding with ProRes 4444+ (ap4h) profile\n"); - } else - av_assert0(0); + break; + default: + av_unreachable("Already checked via AVCodec.pix_fmts"); + } } else if (avctx->profile < AV_PROFILE_PRORES_PROXY || avctx->profile > AV_PROFILE_PRORES_XQ) { av_log( -- 2.45.2
From da05e644ba3f3e7b44e13c2ec36823fbe783db23 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 14:35:31 +0200 Subject: [PATCH 04/21] avcodec/mpeg4videodec: Mark impossible switch case as unreachable Alternative to 8fc649b931a3cbc3a2dd9b50b75a9261a2fb4b49. Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/mpeg4videodec.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c index e21f1d24a2..45087f3650 100644 --- a/libavcodec/mpeg4videodec.c +++ b/libavcodec/mpeg4videodec.c @@ -24,6 +24,7 @@ #include "config_components.h" +#include "libavutil/avassert.h" #include "libavutil/internal.h" #include "libavutil/opt.h" #include "libavutil/thread.h" @@ -605,7 +606,9 @@ static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *g ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2; break; default: - av_assert0(0); + av_unreachable("num_sprite_warping_points outside of 0..3 results in an error" + "in which num_sprite_warping_points is reset to zero"); + break; } /* try to simplify the situation */ if (sprite_delta[0][0] == a << ctx->sprite_shift[0] && -- 2.45.2
From 987145fd34dba2266f9bc489cb4f9cba308fecd7 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 14:49:59 +0200 Subject: [PATCH 05/21] avcodec/pcm-dvdenc: Mark unreachable default cases as unreachable Fixes a Clang warning when asserts are disabled: "variable 'quant' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]" Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/pcm-dvdenc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavcodec/pcm-dvdenc.c b/libavcodec/pcm-dvdenc.c index b1f01ee323..10ce478125 100644 --- a/libavcodec/pcm-dvdenc.c +++ b/libavcodec/pcm-dvdenc.c @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avassert.h" #include "libavutil/channel_layout.h" #include "avcodec.h" #include "bytestream.h" @@ -45,7 +46,7 @@ static av_cold int pcm_dvd_encode_init(AVCodecContext *avctx) freq = 1; break; default: - av_assert1(0); + av_unreachable("Already checked via AVCodec.supported_samplerates"); } switch (avctx->sample_fmt) { @@ -58,7 +59,7 @@ static av_cold int pcm_dvd_encode_init(AVCodecContext *avctx) quant = 2; break; default: - av_assert1(0); + av_unreachable("Already checked via AVCodec.sample_fmts"); } avctx->bits_per_coded_sample = 16 + quant * 4; -- 2.45.2
From a082a95be463337f8e10342b7eec380cf54d2ea6 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 15:18:45 +0200 Subject: [PATCH 06/21] avcodec/vlc: Make code more readable with av_unreachable Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/vlc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavcodec/vlc.c b/libavcodec/vlc.c index c49c801181..ee6ea37736 100644 --- a/libavcodec/vlc.c +++ b/libavcodec/vlc.c @@ -49,10 +49,11 @@ v = *(const uint16_t *)ptr; \ break; \ case 4: \ - default: \ - av_assert1(size == 4); \ v = *(const uint32_t *)ptr; \ break; \ + default: \ + av_unreachable("Only uint8/16/32_t are used"); \ + break; \ } \ } -- 2.45.2
From 595ea69554f46aee7924c0fe89d6bf344cc4bf38 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 15:32:18 +0200 Subject: [PATCH 07/21] avcodec/utvideoenc: Remove always-false pixel format check Mark it as unreachable instead. Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/utvideoenc.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libavcodec/utvideoenc.c b/libavcodec/utvideoenc.c index be503d78c6..e35f0a82f4 100644 --- a/libavcodec/utvideoenc.c +++ b/libavcodec/utvideoenc.c @@ -24,6 +24,7 @@ * Ut Video encoder */ +#include "libavutil/avassert.h" #include "libavutil/imgutils.h" #include "libavutil/intreadwrite.h" #include "libavutil/mem.h" @@ -143,9 +144,7 @@ static av_cold int utvideo_encode_init(AVCodecContext *avctx) original_format = UTVIDEO_444; break; default: - av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n", - avctx->pix_fmt); - return AVERROR_INVALIDDATA; + av_unreachable("Already checked via AVCodec.pix_fmts"); } ff_bswapdsp_init(&c->bdsp); -- 2.45.2
From a4d92e242acb6c840b61577d2850483f3946a938 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 15:51:32 +0200 Subject: [PATCH 08/21] avcodec/dolby_e_parse: Use av_unreachable instead of av_assert0(0) Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/dolby_e_parse.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/dolby_e_parse.c b/libavcodec/dolby_e_parse.c index ffedcd99a4..fc20eae5b4 100644 --- a/libavcodec/dolby_e_parse.c +++ b/libavcodec/dolby_e_parse.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avassert.h" #include "get_bits.h" #include "put_bits.h" #include "dolby_e.h" @@ -88,7 +89,7 @@ int ff_dolby_e_convert_input(DBEContext *s, int nb_words, int key) AV_WB24(dst, AV_RB24(src) ^ key); break; default: - av_assert0(0); + av_unreachable("ff_dolby_e_parse_header() only sets 16, 20, 24 and errors out otherwise"); } return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits); -- 2.45.2
From 9b87978fb97f8bed9d53c41830c1bf457dd63703 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Fri, 24 May 2024 16:33:47 +0200 Subject: [PATCH 09/21] avcodec/put_bits: Allow to mark places where PutBitContext is flushed This will allow the compiler to optimize the "is the cache full?" branches away from some put_bits(). Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/put_bits.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h index 56c3f4cc6d..c3eee622d4 100644 --- a/libavcodec/put_bits.h +++ b/libavcodec/put_bits.h @@ -74,6 +74,16 @@ static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, s->bit_buf = 0; } +/** + * Inform the compiler that a PutBitContext is flushed (i.e. if it has just + * been initialized or flushed). Undefined behaviour occurs if this is used + * with a PutBitContext for which this is not true. + */ +static inline void put_bits_assume_flushed(const PutBitContext *s) +{ + av_assume(s->bit_left == BUF_BITS); +} + /** * @return the total number of bits written to the bitstream. */ -- 2.45.2
From acddc7a58f3f36f9360282938e935079666865f4 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Fri, 24 May 2024 16:43:31 +0200 Subject: [PATCH 10/21] avcodec/e?ac3enc: Inform compiler about PutBitContext being blank This turned out to be very beneficial: For GCC 13, the codesize of ac3_output_frame_header went down from 4522B to 1247B and from 10762B to 9298B for eac3_output_frame_header. For Clang 17, the numbers went down from 3923B to 2477B and from 8338B to 6548B (always with -O3). Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/ac3enc.c | 2 ++ libavcodec/eac3enc.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index 3649289865..a1783577c5 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -1638,6 +1638,8 @@ static void ac3_output_frame_header(AC3EncodeContext *s, PutBitContext *pb) { AC3EncOptions *opt = &s->options; + put_bits_assume_flushed(pb); + put_bits(pb, 16, 0x0b77); /* frame header */ put_bits(pb, 16, 0); /* crc1: will be filled later */ put_bits(pb, 2, s->bit_alloc.sr_code); diff --git a/libavcodec/eac3enc.c b/libavcodec/eac3enc.c index 3590b821a3..10b1ab337c 100644 --- a/libavcodec/eac3enc.c +++ b/libavcodec/eac3enc.c @@ -135,6 +135,8 @@ static void eac3_output_frame_header(AC3EncodeContext *s, PutBitContext *pb) int blk, ch; AC3EncOptions *opt = &s->options; + put_bits_assume_flushed(pb); + put_bits(pb, 16, 0x0b77); /* sync word */ /* BSI header */ -- 2.45.2
From 9948d8ca50aa17998218af236966d8cad7388bf4 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Fri, 24 May 2024 17:24:59 +0200 Subject: [PATCH 11/21] avcodec/speedhqenc: Use av_unreachable() for unreachable condition Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/speedhqenc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/speedhqenc.c b/libavcodec/speedhqenc.c index ecba2cd840..b2e5eda152 100644 --- a/libavcodec/speedhqenc.c +++ b/libavcodec/speedhqenc.c @@ -27,6 +27,7 @@ * SpeedHQ encoder. */ +#include "libavutil/avassert.h" #include "libavutil/thread.h" #include "avcodec.h" @@ -259,7 +260,7 @@ static av_cold int speedhq_encode_init(AVCodecContext *avctx) avctx->codec_tag = MKTAG('S','H','Q','4'); break; default: - av_assert0(0); + av_unreachable("Already checked via AVCodec.pix_fmts"); } m->encode_picture_header = speedhq_encode_picture_header; -- 2.45.2
From 7b2f71bf623a7105b65ac1ca95186cbda80c8122 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sat, 25 May 2024 00:55:18 +0200 Subject: [PATCH 12/21] avcodec/wmaenc: Use av_unreachable() instead of av_assert0(0) Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/wmaenc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c index 889306aebd..51487b72b5 100644 --- a/libavcodec/wmaenc.c +++ b/libavcodec/wmaenc.c @@ -79,7 +79,7 @@ static av_cold int encode_init(AVCodecContext *avctx) AV_WL32(extradata, flags1); AV_WL16(extradata + 4, flags2); } else { - av_assert0(0); + av_unreachable("This function is only used with WMAV1/2 encoders"); } avctx->extradata = extradata; s->use_exp_vlc = flags2 & 0x0001; @@ -206,7 +206,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], // FIXME remove duplication relative to decoder if (s->use_variable_block_len) { - av_assert0(0); // FIXME not implemented + av_unreachable("use_variable_block_len unimplemented, set to 0 during init"); } else { /* fixed block len */ s->next_block_len_bits = s->frame_len_bits; @@ -306,7 +306,8 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], if (s->use_exp_vlc) { encode_exp_vlc(s, ch, fixed_exp); } else { - av_assert0(0); // FIXME not implemented + av_unreachable("use_exp_vlc always set to 1 during init"); + // FIXME not implemented // encode_exp_lsp(s, ch); } } @@ -365,7 +366,7 @@ static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], init_put_bits(&s->pb, buf, buf_size); if (s->use_bit_reservoir) - av_assert0(0); // FIXME not implemented + av_unreachable("use_bit_reseroir unimplemented, set to 0 during init"); else if (encode_block(s, src_coefs, total_gain) < 0) return INT_MAX; -- 2.45.2
From bb47bd8cd86cbb9a5dd4567912895ad136ce4c73 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Fri, 24 May 2024 18:16:00 +0200 Subject: [PATCH 13/21] avcodec/mpegvideo_{dec,motion}: Mark unreachable code as unreachable Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/mpegvideo_dec.c | 2 +- libavcodec/mpegvideo_motion.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c index 4019b4f0da..47b86d5897 100644 --- a/libavcodec/mpegvideo_dec.c +++ b/libavcodec/mpegvideo_dec.c @@ -834,7 +834,7 @@ static inline void MPV_motion_lowres(MpegEncContext *s, } break; default: - av_assert2(0); + av_unreachable("No other mpegvideo MV types exist"); } } diff --git a/libavcodec/mpegvideo_motion.c b/libavcodec/mpegvideo_motion.c index edc4931092..a48b898dac 100644 --- a/libavcodec/mpegvideo_motion.c +++ b/libavcodec/mpegvideo_motion.c @@ -813,7 +813,8 @@ static av_always_inline void mpv_motion_internal(MpegEncContext *s, } break; } - default: av_assert2(0); + default: + av_unreachable("No other mpegvideo MV types exist"); } } -- 2.45.2
From 7f4ef07e63ca348320017e826c97cfe31ceb7352 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Sun, 3 Oct 2021 15:19:06 +0200 Subject: [PATCH 14/21] avcodec/bitstream: Make assert check more strict The earlier code allowed callers to use arbitrary crap as symbols_size as long as no symbols were present. Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/vlc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/vlc.c b/libavcodec/vlc.c index ee6ea37736..af09e83132 100644 --- a/libavcodec/vlc.c +++ b/libavcodec/vlc.c @@ -261,7 +261,7 @@ int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes, if (ret < 0) return ret; - av_assert0(symbols_size <= 2 || !symbols); + av_assert0(symbols_size <= 2U); j = 0; #define COPY(condition)\ for (int i = 0; i < nb_codes; i++) { \ -- 2.45.2
From c4dbee4a3f1166093abb370be7572e804a5a8ec9 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Tue, 6 May 2025 13:03:17 +0200 Subject: [PATCH 15/21] avcodec/mpegvideo encs: Add put_bits_assume_flushed() to encode_header This allows the compiler to remove the implicit "Do I need to output the PutBitContext buffer here?" checks. Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/flvenc.c | 3 ++- libavcodec/h261enc.c | 3 ++- libavcodec/ituh263enc.c | 5 +++-- libavcodec/mpeg12enc.c | 5 +++++ libavcodec/mpeg4videoenc.c | 3 +++ libavcodec/msmpeg4enc.c | 3 ++- libavcodec/rv10enc.c | 2 +- libavcodec/rv20enc.c | 2 ++ libavcodec/speedhqenc.c | 3 +++ libavcodec/wmv2enc.c | 3 +++ 10 files changed, 26 insertions(+), 6 deletions(-) diff --git a/libavcodec/flvenc.c b/libavcodec/flvenc.c index df1a650222..8f07c3c778 100644 --- a/libavcodec/flvenc.c +++ b/libavcodec/flvenc.c @@ -22,13 +22,14 @@ #include "flvenc.h" #include "mpegvideo.h" #include "mpegvideoenc.h" +#include "put_bits.h" int ff_flv_encode_picture_header(MPVMainEncContext *const m) { MPVEncContext *const s = &m->s; int format; - align_put_bits(&s->pb); + put_bits_assume_flushed(&s->pb); put_bits(&s->pb, 17, 1); /* 0: H.263 escape codes 1: 11-bit escape codes */ diff --git a/libavcodec/h261enc.c b/libavcodec/h261enc.c index 70f5f2b09c..c217fb6233 100644 --- a/libavcodec/h261enc.c +++ b/libavcodec/h261enc.c @@ -35,6 +35,7 @@ #include "h261.h" #include "h261enc.h" #include "mpegvideoenc.h" +#include "put_bits.h" #define H261_MAX_RUN 26 #define H261_MAX_LEVEL 15 @@ -72,7 +73,7 @@ static int h261_encode_picture_header(MPVMainEncContext *const m) MPVEncContext *const s = &h->s.s; int temp_ref; - align_put_bits(&s->pb); + put_bits_assume_flushed(&s->pb); put_bits(&s->pb, 20, 0x10); /* PSC */ diff --git a/libavcodec/ituh263enc.c b/libavcodec/ituh263enc.c index 8be7ee4636..b9d903a220 100644 --- a/libavcodec/ituh263enc.c +++ b/libavcodec/ituh263enc.c @@ -46,6 +46,7 @@ #include "mathops.h" #include "mpegutils.h" #include "internal.h" +#include "put_bits.h" /** * Table of number of bits a motion vector component needs. @@ -230,6 +231,8 @@ static int h263_encode_picture_header(MPVMainEncContext *const m) int best_error= INT_MAX; int custom_pcf; + put_bits_assume_flushed(&s->pb); + if(s->c.h263_plus){ for(i=0; i<2; i++){ int div, error; @@ -247,8 +250,6 @@ static int h263_encode_picture_header(MPVMainEncContext *const m) coded_frame_rate= 1800000; coded_frame_rate_base= (1000+best_clock_code)*best_divisor; - align_put_bits(&s->pb); - put_bits(&s->pb, 22, 0x20); /* PSC */ temp_ref= s->c.picture_number * (int64_t)coded_frame_rate * s->c.avctx->time_base.num / //FIXME use timestamp (coded_frame_rate_base * (int64_t)s->c.avctx->time_base.den); diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c index 8364368fde..e045a64d49 100644 --- a/libavcodec/mpeg12enc.c +++ b/libavcodec/mpeg12enc.c @@ -49,6 +49,7 @@ #include "mpegvideo.h" #include "mpegvideoenc.h" #include "profiles.h" +#include "put_bits.h" #include "rl.h" #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER @@ -155,6 +156,8 @@ static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12) AVRational aspect_ratio = s->c.avctx->sample_aspect_ratio; int aspect_ratio_info; + put_bits_assume_flushed(&s->pb); + if (!(s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)) return; @@ -339,6 +342,8 @@ static int mpeg1_encode_picture_header(MPVMainEncContext *const m) MPVEncContext *const s = &m->s; const AVFrameSideData *side_data; + put_bits_assume_flushed(&s->pb); + mpeg1_encode_sequence_header(mpeg12); /* MPEG-1 picture header */ diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c index 775dab0d4a..f16dae8bf9 100644 --- a/libavcodec/mpeg4videoenc.c +++ b/libavcodec/mpeg4videoenc.c @@ -35,6 +35,7 @@ #include "mpeg4videoenc.h" #include "mpegvideoenc.h" #include "profiles.h" +#include "put_bits.h" #include "version.h" /** @@ -1070,6 +1071,8 @@ static int mpeg4_encode_picture_header(MPVMainEncContext *const m) uint64_t time_incr; int64_t time_div, time_mod; + put_bits_assume_flushed(&s->pb); + if (s->c.pict_type == AV_PICTURE_TYPE_I) { if (!(s->c.avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) { if (s->c.avctx->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) // HACK, the reference sw is buggy diff --git a/libavcodec/msmpeg4enc.c b/libavcodec/msmpeg4enc.c index 4b03a7c10b..991d166c53 100644 --- a/libavcodec/msmpeg4enc.c +++ b/libavcodec/msmpeg4enc.c @@ -221,7 +221,8 @@ static int msmpeg4_encode_picture_header(MPVMainEncContext *const m) find_best_tables(ms); - align_put_bits(&s->pb); + put_bits_assume_flushed(&s->pb); + put_bits(&s->pb, 2, s->c.pict_type - 1); put_bits(&s->pb, 5, s->c.qscale); diff --git a/libavcodec/rv10enc.c b/libavcodec/rv10enc.c index 984fe3379d..534b93fd81 100644 --- a/libavcodec/rv10enc.c +++ b/libavcodec/rv10enc.c @@ -36,7 +36,7 @@ int ff_rv10_encode_picture_header(MPVMainEncContext *const m) MPVEncContext *const s = &m->s; int full_frame= 0; - align_put_bits(&s->pb); + put_bits_assume_flushed(&s->pb); put_bits(&s->pb, 1, 1); /* marker */ diff --git a/libavcodec/rv20enc.c b/libavcodec/rv20enc.c index 3211700f76..c91a2db57f 100644 --- a/libavcodec/rv20enc.c +++ b/libavcodec/rv20enc.c @@ -38,6 +38,8 @@ int ff_rv20_encode_picture_header(MPVMainEncContext *const m) { MPVEncContext *const s = &m->s; + put_bits_assume_flushed(&s->pb); + put_bits(&s->pb, 2, s->c.pict_type); //I 0 vs. 1 ? put_bits(&s->pb, 1, 0); /* unknown bit */ put_bits(&s->pb, 5, s->c.qscale); diff --git a/libavcodec/speedhqenc.c b/libavcodec/speedhqenc.c index b2e5eda152..71df115d58 100644 --- a/libavcodec/speedhqenc.c +++ b/libavcodec/speedhqenc.c @@ -37,6 +37,7 @@ #include "mpegvideo.h" #include "mpegvideodata.h" #include "mpegvideoenc.h" +#include "put_bits.h" #include "rl.h" #include "speedhq.h" #include "speedhqenc.h" @@ -101,6 +102,8 @@ static int speedhq_encode_picture_header(MPVMainEncContext *const m) SpeedHQEncContext *const ctx = (SpeedHQEncContext*)m; MPVEncContext *const s = &m->s; + put_bits_assume_flushed(&s->pb); + put_bits_le(&s->pb, 8, 100 - s->c.qscale * 2); /* FIXME why doubled */ put_bits_le(&s->pb, 24, 4); /* no second field */ diff --git a/libavcodec/wmv2enc.c b/libavcodec/wmv2enc.c index f9fd918dbf..592d1060d3 100644 --- a/libavcodec/wmv2enc.c +++ b/libavcodec/wmv2enc.c @@ -28,6 +28,7 @@ #include "msmpeg4enc.h" #include "msmpeg4data.h" #include "msmpeg4_vc1_data.h" +#include "put_bits.h" #include "wmv2.h" #define WMV2_EXTRADATA_SIZE 4 @@ -78,6 +79,8 @@ static int wmv2_encode_picture_header(MPVMainEncContext *const m) MSMPEG4EncContext *const ms = &w->msmpeg4; MPVEncContext *const s = &m->s; + put_bits_assume_flushed(&s->pb); + put_bits(&s->pb, 1, s->c.pict_type - 1); if (s->c.pict_type == AV_PICTURE_TYPE_I) put_bits(&s->pb, 7, 0); -- 2.45.2
From 86d3dc6b365e22c2c9fb4ce1525bb5e6918d5576 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Tue, 6 May 2025 13:23:48 +0200 Subject: [PATCH 16/21] avcodec/rv20enc: Use av_assert1() instead of av_assert0() There is really no good reason to perform these checks in release builds. Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/rv20enc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libavcodec/rv20enc.c b/libavcodec/rv20enc.c index c91a2db57f..092dc7d75b 100644 --- a/libavcodec/rv20enc.c +++ b/libavcodec/rv20enc.c @@ -50,12 +50,12 @@ int ff_rv20_encode_picture_header(MPVMainEncContext *const m) put_bits(&s->pb, 1, s->c.no_rounding); - av_assert0(s->c.f_code == 1); - av_assert0(!s->c.unrestricted_mv); - av_assert0(!s->c.alt_inter_vlc); - av_assert0(!s->c.umvplus); - av_assert0(s->c.modified_quant==1); - av_assert0(s->c.loop_filter==1); + av_assert1(s->c.f_code == 1); + av_assert1(!s->c.unrestricted_mv); + av_assert1(!s->c.alt_inter_vlc); + av_assert1(!s->c.umvplus); + av_assert1(s->c.modified_quant == 1); + av_assert1(s->c.loop_filter == 1); s->c.h263_aic = s->c.pict_type == AV_PICTURE_TYPE_I; if (s->c.h263_aic) { -- 2.45.2
From a32835e7db07ce1ac3c222b0e1cc738afb962a56 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Tue, 6 May 2025 16:06:40 +0200 Subject: [PATCH 17/21] avcodec/vp9: Replace av_assert(0) by av_unreachable() Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/vp9.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index fd416eed3a..141f0941b4 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -1140,7 +1140,8 @@ static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl, uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1); break; default: - av_assert0(0); + av_unreachable("ff_vp9_partition_tree only has " + "the four PARTITION_* terminal codes"); } } else if (vpx_rac_get_prob_branchy(td->c, p[1])) { bp = PARTITION_SPLIT; -- 2.45.2
From e92bf7933f70ae7c6fdcc5a40eb46a2a1e59f7a8 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Tue, 6 May 2025 16:27:44 +0200 Subject: [PATCH 18/21] avcodec/adpcm: Use av_unreachable() instead of av_assert0() Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/adpcm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index e20b60e05f..622cf54b40 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -2319,7 +2319,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } ) /* End of CASE */ default: - av_assert0(0); // unsupported codec_id should not happen + av_unreachable("There are cases for all codec ids using adpcm_decode_frame"); } if (avpkt->size && bytestream2_tell(&gb) == 0) { -- 2.45.2
From 2f457f8a6f0828c3479c89cc4405858fb74e640e Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Tue, 6 May 2025 17:06:15 +0200 Subject: [PATCH 19/21] avcodec/mjpegenc_common: Use av_unreachable() instead of av_assert0(0) Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/mjpegenc_common.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavcodec/mjpegenc_common.c b/libavcodec/mjpegenc_common.c index e7a4f8f16a..21b3b19b93 100644 --- a/libavcodec/mjpegenc_common.c +++ b/libavcodec/mjpegenc_common.c @@ -304,7 +304,8 @@ void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, switch (avctx->codec_id) { case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break; case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break; - default: av_assert0(0); + default: av_unreachable("ff_mjpeg_encode_picture_header only called by " + "AMV, LJPEG, MJPEG and the former has been ruled out"); } put_bits(pb, 16, 8 + 3 * components); @@ -375,7 +376,7 @@ void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, switch (avctx->codec_id) { case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */ case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */ - default: av_assert0(0); + default: av_unreachable("Only LJPEG, MJPEG possible here"); } put_bits(pb, 8, 0); /* Ah/Al (not used) */ -- 2.45.2
From 43b37e3074a8ff298c1bbf7f0db320ef88c9bf49 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Tue, 6 May 2025 17:56:43 +0200 Subject: [PATCH 20/21] avcodec/4xm: Use av_unreachable() instead of av_assert0(0) Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/4xm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c index fd3a45f093..11a61a253e 100644 --- a/libavcodec/4xm.c +++ b/libavcodec/4xm.c @@ -337,7 +337,8 @@ static inline void mcdc(uint16_t *dst, const uint16_t *src, int log2w, } break; default: - av_assert0(0); + av_unreachable("log2w starts at 3 and gets only decremented during " + "recursive calls to decode_p_block"); } } -- 2.45.2
From f2d151368382d8657c1a10d4a9edac619ff1ef7a Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt <andreas.rheinha...@outlook.com> Date: Tue, 6 May 2025 18:03:59 +0200 Subject: [PATCH 21/21] avcodec/atrac3: Use av_unreachable() instead of av_assert1(0) Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com> --- libavcodec/atrac3.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c index faa3daa9e6..fe156fa482 100644 --- a/libavcodec/atrac3.c +++ b/libavcodec/atrac3.c @@ -526,7 +526,7 @@ static void reverse_matrixing(float *su1, float *su2, int *prev_code, } break; default: - av_assert1(0); + av_unreachable("curr_code/matrix_coeff_index_* values are stored in two bits"); } } } -- 2.45.2
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".