History

avcodec/get_bits: Add get_bits_bytesize()

And use it to avoid accesses to GetBitContext.buffer_end.

Andreas Rheinhardt committed 1 year ago

Commit 367cf961 parent d20a4ef8 files · +38 −11Browse files

Unified · Split

8 files
  1. libavcodec/bitstream.h +2 −0
  2. libavcodec/bitstream_template.h +8 −0
  3. libavcodec/get_bits.h +15 −0
  4. libavcodec/h263dec.c +4 −3
  5. libavcodec/h264_ps.c +2 −2
  6. libavcodec/hevc/hevcdec.c +1 −1
  7. libavcodec/hevc/ps.c +3 −3
  8. libavcodec/mpeg12dec.c +3 −2
libavcodec/bitstream.h+2−0
@@ -82,6 +82,7 @@ · 81 unmodified lines
8282 # define bits_init8 bits_init8_le
8383 # define bits_tell bits_tell_le
8484 # define bits_size bits_size_le
85+# define bits_bytesize bits_bytesize_le
8586 # define bits_left bits_left_le
8687 # define bits_read_bit bits_read_bit_le
8788 # define bits_read_nz bits_read_nz_le
@@ -111,6 +112,7 @@ · 23 unmodified lines
111112 # define bits_init8 bits_init8_be
112113 # define bits_tell bits_tell_be
113114 # define bits_size bits_size_be
115+# define bits_bytesize bits_bytesize_be
114116 # define bits_left bits_left_be
115117 # define bits_read_bit bits_read_bit_be
116118 # define bits_read_nz bits_read_nz_be
libavcodec/bitstream_template.h+8−0
@@ -156,6 +156,14 @@ static inline int BS_FUNC(size)(const BSCTX *bc) · 155 unmodified lines
156156 return bc->size_in_bits;
157157 }
158158
159+/**
160+ * Return buffer size in bytes.
161+ */
162+static inline int BS_FUNC(bytesize)(const BSCTX *bc, int round_up)
163+{
164+ return (bc->size_in_bits + (round_up ? 7 : 0)) >> 3;
165+}
166+
159167 /**
160168 * Return the number of the bits left in a buffer.
161169 */
libavcodec/get_bits.h+15−0
@@ -76,6 +76,7 @@ · 75 unmodified lines
7676 typedef BitstreamContext GetBitContext;
7777
7878 #define get_bits_count bits_tell
79+#define get_bits_bytesize bits_bytesize
7980 #define get_bits_left bits_left
8081 #define skip_bits_long bits_skip
8182 #define skip_bits bits_skip
@@ -251,6 +252,20 @@ static inline int get_bits_count(const GetBitContext *s) · 169 unmodified lines
251252 return s->index;
252253 }
253254
255+/**
256+ * Get the size of the GetBitContext's buffer in bytes.
257+ *
258+ * @param s the GetBitContext
259+ * @param round_up If set, the number of bits will be rounded up to full bytes;
260+ * this does not matter if the number of bits is known to be
261+ * a multiple of eight, e.g. if the GetBitContext has been
262+ * initialized with init_get_bits8.
263+ */
264+static inline int get_bits_bytesize(const GetBitContext *s, int round_up)
265+{
266+ return (s->size_in_bits + (round_up ? 7 : 0)) >> 3;
267+}
268+
254269 /**
255270 * Skips the specified number of bits.
256271 * @param n the number of bits to skip,
libavcodec/h263dec.c+4−3
@@ -208,7 +208,8 @@ static int decode_slice(H263DecContext *const h) · 207 unmodified lines
208208
209209 if (h->c.avctx->hwaccel) {
210210 const uint8_t *start = h->gb.buffer + get_bits_count(&h->gb) / 8;
211− ret = FF_HW_CALL(h->c.avctx, decode_slice, start, h->gb.buffer_end - start);
211+ ret = FF_HW_CALL(h->c.avctx, decode_slice, start,
212+ get_bits_bytesize(&h->gb, 0) - get_bits_count(&h->gb) / 8);
212213 // ensure we exit decode loop
213214 h->c.mb_y = h->c.mb_height;
214215 return ret;
@@ -372,7 +373,7 @@ static int decode_slice(H263DecContext *const h) · 157 unmodified lines
372373 if (h->c.codec_id == AV_CODEC_ID_H263 &&
373374 (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
374375 get_bits_left(&h->gb) >= 64 &&
375− AV_RB64(h->gb.buffer_end - 8) == 0xCDCDCDCDFC7F0000) {
376+ AV_RB64(h->gb.buffer + (get_bits_bytesize(&h->gb, 0) - 8)) == 0xCDCDCDCDFC7F0000) {
376377
377378 h->padding_bug_score += 32;
378379 }
@@ -546,7 +547,7 @@ int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict, · 167 unmodified lines
546547
547548 if (avctx->hwaccel) {
548549 ret = FF_HW_CALL(avctx, start_frame, NULL,
549− h->gb.buffer, h->gb.buffer_end - h->gb.buffer);
550+ h->gb.buffer, get_bits_bytesize(&h->gb, 0));
550551 if (ret < 0 )
551552 return ret;
552553 }
libavcodec/h264_ps.c+2−2
@@ -294,7 +294,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx, · 293 unmodified lines
294294 if (!sps)
295295 return AVERROR(ENOMEM);
296296
297− sps->data_size = gb->buffer_end - gb->buffer;
297+ sps->data_size = get_bits_bytesize(gb, 1);
298298 if (sps->data_size > sizeof(sps->data)) {
299299 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n");
300300 sps->data_size = sizeof(sps->data);
@@ -712,7 +712,7 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct · 411 unmodified lines
712712 if (!pps)
713713 return AVERROR(ENOMEM);
714714
715− pps->data_size = gb->buffer_end - gb->buffer;
715+ pps->data_size = get_bits_bytesize(gb, 1);
716716 if (pps->data_size > sizeof(pps->data)) {
717717 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS "
718718 "(%"SIZE_SPECIFIER" > %"SIZE_SPECIFIER")\n",
libavcodec/hevc/hevcdec.c+1−1
@@ -2752,7 +2752,7 @@ static int hls_decode_entry(HEVCContext *s, GetBitContext *gb) · 2751 unmodified lines
27522752 const HEVCPPS *const pps = s->pps;
27532753 const HEVCSPS *const sps = pps->sps;
27542754 const uint8_t *slice_data = gb->buffer + s->sh.data_offset;
2755− const size_t slice_size = gb->buffer_end - gb->buffer - s->sh.data_offset;
2755+ const size_t slice_size = get_bits_bytesize(gb, 1) - s->sh.data_offset;
27562756 int ctb_size = 1 << sps->log2_ctb_size;
27572757 int more_data = 1;
27582758 int x_ctb = 0;
libavcodec/hevc/ps.c+3−3
@@ -763,7 +763,7 @@ int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, · 762 unmodified lines
763763 {
764764 int i;
765765 int vps_id = get_bits(gb, 4);
766− ptrdiff_t nal_size = gb->buffer_end - gb->buffer;
766+ ptrdiff_t nal_size = get_bits_bytesize(gb, 1);
767767 int ret = AVERROR_INVALIDDATA;
768768 uint64_t layer1_id_included = 0;
769769 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, · 940 unmodified lines
17101710
17111711 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
17121712
1713− sps->data_size = gb->buffer_end - gb->buffer;
1713+ sps->data_size = get_bits_bytesize(gb, 1);
17141714 sps->data = av_memdup(gb->buffer, sps->data_size);
17151715 if (!sps->data) {
17161716 ret = AVERROR(ENOMEM);
@@ -2165,7 +2165,7 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, · 448 unmodified lines
21652165 const HEVCSPS *sps = NULL;
21662166 const HEVCVPS *vps = NULL;
21672167 int i, ret = 0;
2168− ptrdiff_t nal_size = gb->buffer_end - gb->buffer;
2168+ ptrdiff_t nal_size = get_bits_bytesize(gb, 1);
21692169 unsigned int pps_id = get_ue_golomb_long(gb);
21702170 unsigned log2_parallel_merge_level_minus2;
21712171 HEVCPPS *pps;
libavcodec/mpeg12dec.c+3−2
@@ -1635,6 +1635,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) · 1634 unmodified lines
16351635 {
16361636 Mpeg12SliceContext *const s = *(void **) arg;
16371637 const uint8_t *buf = s->gb.buffer;
1638+ const uint8_t *end = buf + get_bits_bytesize(&s->gb, 0);
16381639 int mb_y = s->c.start_mb_y;
16391640 const int field_pic = s->c.picture_structure != PICT_FRAME;
16401641
@@ -1644,7 +1645,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) · 3 unmodified lines
16441645 uint32_t start_code;
16451646 int ret;
16461647
1647− ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1648+ ret = mpeg_decode_slice(s, mb_y, &buf, end - buf);
16481649 emms_c();
16491650 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
16501651 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) · 15 unmodified lines
16661667 return 0;
16671668
16681669 start_code = -1;
1669− buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1670+ buf = avpriv_find_start_code(buf, end, &start_code);
16701671 if (start_code < SLICE_MIN_START_CODE || start_code > SLICE_MAX_START_CODE)
16711672 return AVERROR_INVALIDDATA;
16721673 mb_y = start_code - SLICE_MIN_START_CODE;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t