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
82# define bits_init8 bits_init8_le82# define bits_init8 bits_init8_le
83# define bits_tell bits_tell_le83# define bits_tell bits_tell_le
84# define bits_size bits_size_le84# define bits_size bits_size_le
85# define bits_bytesize bits_bytesize_le
85# define bits_left bits_left_le86# define bits_left bits_left_le
86# define bits_read_bit bits_read_bit_le87# define bits_read_bit bits_read_bit_le
87# define bits_read_nz bits_read_nz_le88# define bits_read_nz bits_read_nz_le
@@ -111,6 +112,7 @@ · 23 unmodified lines
111# define bits_init8 bits_init8_be112# define bits_init8 bits_init8_be
112# define bits_tell bits_tell_be113# define bits_tell bits_tell_be
113# define bits_size bits_size_be114# define bits_size bits_size_be
115# define bits_bytesize bits_bytesize_be
114# define bits_left bits_left_be116# define bits_left bits_left_be
115# define bits_read_bit bits_read_bit_be117# define bits_read_bit bits_read_bit_be
116# define bits_read_nz bits_read_nz_be118# 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
156 return bc->size_in_bits;156 return bc->size_in_bits;
157}157}
158 158
159/**
160 * Return buffer size in bytes.
161 */
162static 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
159/**167/**
160 * Return the number of the bits left in a buffer.168 * Return the number of the bits left in a buffer.
161 */169 */
libavcodec/get_bits.h+15−0
@@ -76,6 +76,7 @@ · 75 unmodified lines
76typedef BitstreamContext GetBitContext;76typedef BitstreamContext GetBitContext;
77 77
78#define get_bits_count bits_tell78#define get_bits_count bits_tell
79#define get_bits_bytesize bits_bytesize
79#define get_bits_left bits_left80#define get_bits_left bits_left
80#define skip_bits_long bits_skip81#define skip_bits_long bits_skip
81#define skip_bits bits_skip82#define skip_bits bits_skip
@@ -251,6 +252,20 @@ static inline int get_bits_count(const GetBitContext *s) · 169 unmodified lines
251 return s->index;252 return s->index;
252}253}
253 254
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 */
264static 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
254/**269/**
255 * Skips the specified number of bits.270 * Skips the specified number of bits.
256 * @param n the number of bits to skip,271 * @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
208 208
209 if (h->c.avctx->hwaccel) {209 if (h->c.avctx->hwaccel) {
210 const uint8_t *start = h->gb.buffer + get_bits_count(&h->gb) / 8;210 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);
212 // ensure we exit decode loop213 // ensure we exit decode loop
213 h->c.mb_y = h->c.mb_height;214 h->c.mb_y = h->c.mb_height;
214 return ret;215 return ret;
@@ -372,7 +373,7 @@ static int decode_slice(H263DecContext *const h) · 157 unmodified lines
372 if (h->c.codec_id == AV_CODEC_ID_H263 &&373 if (h->c.codec_id == AV_CODEC_ID_H263 &&
373 (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&374 (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
374 get_bits_left(&h->gb) >= 64 &&375 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) {
376 377
377 h->padding_bug_score += 32;378 h->padding_bug_score += 32;
378 }379 }
@@ -546,7 +547,7 @@ int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict, · 167 unmodified lines
546 547
547 if (avctx->hwaccel) {548 if (avctx->hwaccel) {
548 ret = FF_HW_CALL(avctx, start_frame, NULL,549 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));
550 if (ret < 0 )551 if (ret < 0 )
551 return ret;552 return ret;
552 }553 }
libavcodec/h264_ps.c+2−2
@@ -294,7 +294,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx, · 293 unmodified lines
294 if (!sps)294 if (!sps)
295 return AVERROR(ENOMEM);295 return AVERROR(ENOMEM);
296 296
297 sps->data_size = gb->buffer_end - gb->buffer;297 sps->data_size = get_bits_bytesize(gb, 1);
298 if (sps->data_size > sizeof(sps->data)) {298 if (sps->data_size > sizeof(sps->data)) {
299 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n");299 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n");
300 sps->data_size = sizeof(sps->data);300 sps->data_size = sizeof(sps->data);
@@ -712,7 +712,7 @@ int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avct · 411 unmodified lines
712 if (!pps)712 if (!pps)
713 return AVERROR(ENOMEM);713 return AVERROR(ENOMEM);
714 714
715 pps->data_size = gb->buffer_end - gb->buffer;715 pps->data_size = get_bits_bytesize(gb, 1);
716 if (pps->data_size > sizeof(pps->data)) {716 if (pps->data_size > sizeof(pps->data)) {
717 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS "717 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS "
718 "(%"SIZE_SPECIFIER" > %"SIZE_SPECIFIER")\n",718 "(%"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
2752 const HEVCPPS *const pps = s->pps;2752 const HEVCPPS *const pps = s->pps;
2753 const HEVCSPS *const sps = pps->sps;2753 const HEVCSPS *const sps = pps->sps;
2754 const uint8_t *slice_data = gb->buffer + s->sh.data_offset;2754 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;
2756 int ctb_size = 1 << sps->log2_ctb_size;2756 int ctb_size = 1 << sps->log2_ctb_size;
2757 int more_data = 1;2757 int more_data = 1;
2758 int x_ctb = 0;2758 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
763{763{
764 int i;764 int i;
765 int vps_id = get_bits(gb, 4);765 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);
767 int ret = AVERROR_INVALIDDATA;767 int ret = AVERROR_INVALIDDATA;
768 uint64_t layer1_id_included = 0;768 uint64_t layer1_id_included = 0;
769 unsigned vps_base_layer_internal_flag, vps_base_layer_available_flag;769 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
1710 1710
1711 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");1711 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1712 1712
1713 sps->data_size = gb->buffer_end - gb->buffer;1713 sps->data_size = get_bits_bytesize(gb, 1);
1714 sps->data = av_memdup(gb->buffer, sps->data_size);1714 sps->data = av_memdup(gb->buffer, sps->data_size);
1715 if (!sps->data) {1715 if (!sps->data) {
1716 ret = AVERROR(ENOMEM);1716 ret = AVERROR(ENOMEM);
@@ -2165,7 +2165,7 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, · 448 unmodified lines
2165 const HEVCSPS *sps = NULL;2165 const HEVCSPS *sps = NULL;
2166 const HEVCVPS *vps = NULL;2166 const HEVCVPS *vps = NULL;
2167 int i, ret = 0;2167 int i, ret = 0;
2168 ptrdiff_t nal_size = gb->buffer_end - gb->buffer;2168 ptrdiff_t nal_size = get_bits_bytesize(gb, 1);
2169 unsigned int pps_id = get_ue_golomb_long(gb);2169 unsigned int pps_id = get_ue_golomb_long(gb);
2170 unsigned log2_parallel_merge_level_minus2;2170 unsigned log2_parallel_merge_level_minus2;
2171 HEVCPPS *pps;2171 HEVCPPS *pps;
libavcodec/mpeg12dec.c+3−2
@@ -1635,6 +1635,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) · 1634 unmodified lines
1635{1635{
1636 Mpeg12SliceContext *const s = *(void **) arg;1636 Mpeg12SliceContext *const s = *(void **) arg;
1637 const uint8_t *buf = s->gb.buffer;1637 const uint8_t *buf = s->gb.buffer;
1638 const uint8_t *end = buf + get_bits_bytesize(&s->gb, 0);
1638 int mb_y = s->c.start_mb_y;1639 int mb_y = s->c.start_mb_y;
1639 const int field_pic = s->c.picture_structure != PICT_FRAME;1640 const int field_pic = s->c.picture_structure != PICT_FRAME;
1640 1641
@@ -1644,7 +1645,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) · 3 unmodified lines
1644 uint32_t start_code;1645 uint32_t start_code;
1645 int ret;1646 int ret;
1646 1647
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);
1648 emms_c();1649 emms_c();
1649 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",1650 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1650 ret, s->c.resync_mb_x, s->c.resync_mb_y, s->c.mb_x, s->c.mb_y,1651 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
1666 return 0;1667 return 0;
1667 1668
1668 start_code = -1;1669 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);
1670 if (start_code < SLICE_MIN_START_CODE || start_code > SLICE_MAX_START_CODE)1671 if (start_code < SLICE_MIN_START_CODE || start_code > SLICE_MAX_START_CODE)
1671 return AVERROR_INVALIDDATA;1672 return AVERROR_INVALIDDATA;
1672 mb_y = start_code - SLICE_MIN_START_CODE;1673 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