From: Andreas Rheinhardt <ffmpegagent-at-gmail.com@ffmpeg.org> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 3/4] avcodec/get_bits: Add get_bits_bytesize() Date: Fri, 04 Jul 2025 13:44:56 +0000 Message-ID: <a5a677dcf07ff86c64edcae4092b957e7907321b.1751636697.git.ffmpegagent@gmail.com> (raw) In-Reply-To: <pull.106.ffstaging.FFmpeg.1751636697.ffmpegagent@gmail.com> From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> And use it to avoid accesses to GetBitContext.buffer_end. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/bitstream.h | 2 ++ libavcodec/bitstream_template.h | 8 ++++++++ libavcodec/get_bits.h | 15 +++++++++++++++ libavcodec/h263dec.c | 7 ++++--- libavcodec/h264_ps.c | 4 ++-- libavcodec/hevc/hevcdec.c | 2 +- libavcodec/hevc/ps.c | 6 +++--- libavcodec/mpeg12dec.c | 5 +++-- 8 files changed, 38 insertions(+), 11 deletions(-) diff --git a/libavcodec/bitstream.h b/libavcodec/bitstream.h index 35b7873b9c..e4d96af710 100644 --- a/libavcodec/bitstream.h +++ b/libavcodec/bitstream.h @@ -82,6 +82,7 @@ # define bits_init8 bits_init8_le # define bits_tell bits_tell_le # define bits_size bits_size_le +# define bits_bytesize bits_bytesize_le # define bits_left bits_left_le # define bits_read_bit bits_read_bit_le # define bits_read_nz bits_read_nz_le @@ -111,6 +112,7 @@ # define bits_init8 bits_init8_be # define bits_tell bits_tell_be # define bits_size bits_size_be +# define bits_bytesize bits_bytesize_be # define bits_left bits_left_be # define bits_read_bit bits_read_bit_be # define bits_read_nz bits_read_nz_be diff --git a/libavcodec/bitstream_template.h b/libavcodec/bitstream_template.h index bbb8dfa555..773d40ef14 100644 --- a/libavcodec/bitstream_template.h +++ b/libavcodec/bitstream_template.h @@ -156,6 +156,14 @@ static inline int BS_FUNC(size)(const BSCTX *bc) return bc->size_in_bits; } +/** + * Return buffer size in bytes. + */ +static inline int BS_FUNC(bytesize)(const BSCTX *bc, int round_up) +{ + return (bc->size_in_bits + (round_up ? 7 : 0)) >> 3; +} + /** * Return the number of the bits left in a buffer. */ diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h index 1954296569..c64540cf95 100644 --- a/libavcodec/get_bits.h +++ b/libavcodec/get_bits.h @@ -76,6 +76,7 @@ typedef BitstreamContext GetBitContext; #define get_bits_count bits_tell +#define get_bits_bytesize bits_bytesize #define get_bits_left bits_left #define skip_bits_long bits_skip #define skip_bits bits_skip @@ -251,6 +252,20 @@ static inline int get_bits_count(const GetBitContext *s) return s->index; } +/** + * Get the size of the GetBitContext's buffer in bytes. + * + * @param s the GetBitContext + * @param round_up If set, the number of bits will be rounded up to full bytes; + * this does not matter if the number of bits is known to be + * a multiple of eight, e.g. if the GetBitContext has been + * initialized with init_get_bits8. + */ +static inline int get_bits_bytesize(const GetBitContext *s, int round_up) +{ + return (s->size_in_bits + (round_up ? 7 : 0)) >> 3; +} + /** * Skips the specified number of bits. * @param n the number of bits to skip, diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c index b2d0f9c409..3821472e91 100644 --- a/libavcodec/h263dec.c +++ b/libavcodec/h263dec.c @@ -208,7 +208,8 @@ static int decode_slice(H263DecContext *const h) if (h->c.avctx->hwaccel) { const uint8_t *start = h->gb.buffer + get_bits_count(&h->gb) / 8; - ret = FF_HW_CALL(h->c.avctx, decode_slice, start, h->gb.buffer_end - start); + ret = FF_HW_CALL(h->c.avctx, decode_slice, start, + get_bits_bytesize(&h->gb, 0) - get_bits_count(&h->gb) / 8); // ensure we exit decode loop h->c.mb_y = h->c.mb_height; return ret; @@ -372,7 +373,7 @@ static int decode_slice(H263DecContext *const h) if (h->c.codec_id == AV_CODEC_ID_H263 && (h->c.workaround_bugs & FF_BUG_AUTODETECT) && get_bits_left(&h->gb) >= 64 && - AV_RB64(h->gb.buffer_end - 8) == 0xCDCDCDCDFC7F0000) { + AV_RB64(h->gb.buffer + (get_bits_bytesize(&h->gb, 0) - 8)) == 0xCDCDCDCDFC7F0000) { h->padding_bug_score += 32; } @@ -546,7 +547,7 @@ int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict, if (avctx->hwaccel) { ret = FF_HW_CALL(avctx, start_frame, NULL, - h->gb.buffer, h->gb.buffer_end - h->gb.buffer); + h->gb.buffer, get_bits_bytesize(&h->gb, 0)); if (ret < 0 ) return ret; } diff --git a/libavcodec/h264_ps.c b/libavcodec/h264_ps.c index c698f1b80d..3a3cad7de7 100644 --- a/libavcodec/h264_ps.c +++ b/libavcodec/h264_ps.c @@ -294,7 +294,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx, if (!sps) return AVERROR(ENOMEM); - sps->data_size = gb->buffer_end - gb->buffer; + sps->data_size = get_bits_bytesize(gb, 1); if (sps->data_size > sizeof(sps->data)) { av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n"); sps->data_size = sizeof(sps->data); @@ -712,7 +712,7 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct if (!pps) return AVERROR(ENOMEM); - pps->data_size = gb->buffer_end - gb->buffer; + pps->data_size = get_bits_bytesize(gb, 1); if (pps->data_size > sizeof(pps->data)) { av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS " "(%"SIZE_SPECIFIER" > %"SIZE_SPECIFIER")\n", diff --git a/libavcodec/hevc/hevcdec.c b/libavcodec/hevc/hevcdec.c index 797c9c76c9..21ecf063c5 100644 --- a/libavcodec/hevc/hevcdec.c +++ b/libavcodec/hevc/hevcdec.c @@ -2752,7 +2752,7 @@ static int hls_decode_entry(HEVCContext *s, GetBitContext *gb) const HEVCPPS *const pps = s->pps; const HEVCSPS *const sps = pps->sps; const uint8_t *slice_data = gb->buffer + s->sh.data_offset; - const size_t slice_size = gb->buffer_end - gb->buffer - s->sh.data_offset; + const size_t slice_size = get_bits_bytesize(gb, 1) - s->sh.data_offset; int ctb_size = 1 << sps->log2_ctb_size; int more_data = 1; int x_ctb = 0; diff --git a/libavcodec/hevc/ps.c b/libavcodec/hevc/ps.c index 4b021ea9c1..57125d59c1 100644 --- a/libavcodec/hevc/ps.c +++ b/libavcodec/hevc/ps.c @@ -763,7 +763,7 @@ int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, { int i; int vps_id = get_bits(gb, 4); - ptrdiff_t nal_size = gb->buffer_end - gb->buffer; + ptrdiff_t nal_size = get_bits_bytesize(gb, 1); int ret = AVERROR_INVALIDDATA; uint64_t layer1_id_included = 0; unsigned vps_base_layer_internal_flag, vps_base_layer_available_flag; @@ -1710,7 +1710,7 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n"); - sps->data_size = gb->buffer_end - gb->buffer; + sps->data_size = get_bits_bytesize(gb, 1); sps->data = av_memdup(gb->buffer, sps->data_size); if (!sps->data) { ret = AVERROR(ENOMEM); @@ -2165,7 +2165,7 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, const HEVCSPS *sps = NULL; const HEVCVPS *vps = NULL; int i, ret = 0; - ptrdiff_t nal_size = gb->buffer_end - gb->buffer; + ptrdiff_t nal_size = get_bits_bytesize(gb, 1); unsigned int pps_id = get_ue_golomb_long(gb); unsigned log2_parallel_merge_level_minus2; HEVCPPS *pps; diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index 998f6aa2e5..3ea8d02e1b 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -1635,6 +1635,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) { Mpeg12SliceContext *const s = *(void **) arg; const uint8_t *buf = s->gb.buffer; + const uint8_t *end = buf + get_bits_bytesize(&s->gb, 0); int mb_y = s->c.start_mb_y; const int field_pic = s->c.picture_structure != PICT_FRAME; @@ -1644,7 +1645,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) uint32_t start_code; int ret; - ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf); + ret = mpeg_decode_slice(s, mb_y, &buf, end - buf); emms_c(); ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n", ret, s->c.resync_mb_x, s->c.resync_mb_y, s->c.mb_x, s->c.mb_y, @@ -1666,7 +1667,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) return 0; start_code = -1; - buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code); + buf = avpriv_find_start_code(buf, end, &start_code); if (start_code < SLICE_MIN_START_CODE || start_code > SLICE_MAX_START_CODE) return AVERROR_INVALIDDATA; mb_y = start_code - SLICE_MIN_START_CODE; -- ffmpeg-codebot _______________________________________________ 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:[~2025-07-04 13:45 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-07-04 13:44 [FFmpeg-devel] [PATCH 0/4] Get bits buffer end ffmpegagent 2025-07-04 13:44 ` [FFmpeg-devel] [PATCH 1/4] avcodec/bytestream: Add const where appropriate Andreas Rheinhardt 2025-07-04 13:44 ` [FFmpeg-devel] [PATCH 2/4] avcodec/vvc/dec: Don't use GetBit-API when byte-aligned Andreas Rheinhardt 2025-07-04 13:44 ` Andreas Rheinhardt [this message] 2025-07-04 13:44 ` [FFmpeg-devel] [PATCH 4/4] avcodec/get_bits: Remove GetBitContext.buffer_end Andreas Rheinhardt
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=a5a677dcf07ff86c64edcae4092b957e7907321b.1751636697.git.ffmpegagent@gmail.com \ --to=ffmpegagent-at-gmail.com@ffmpeg.org \ --cc=andreas.rheinhardt@outlook.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