And use it to avoid accesses to GetBitContext.buffer_end.
Unified · Split
| @@ -82,6 +82,7 @@ · 81 unmodified lines | ||
| 82 | 82 | # define bits_init8 bits_init8_le |
| 83 | 83 | # define bits_tell bits_tell_le |
| 84 | 84 | # define bits_size bits_size_le |
| 85 | +# define bits_bytesize bits_bytesize_le | |
| 85 | 86 | # define bits_left bits_left_le |
| 86 | 87 | # define bits_read_bit bits_read_bit_le |
| 87 | 88 | # define bits_read_nz bits_read_nz_le |
| @@ -111,6 +112,7 @@ · 23 unmodified lines | ||
| 111 | 112 | # define bits_init8 bits_init8_be |
| 112 | 113 | # define bits_tell bits_tell_be |
| 113 | 114 | # define bits_size bits_size_be |
| 115 | +# define bits_bytesize bits_bytesize_be | |
| 114 | 116 | # define bits_left bits_left_be |
| 115 | 117 | # define bits_read_bit bits_read_bit_be |
| 116 | 118 | # define bits_read_nz bits_read_nz_be |
| @@ -156,6 +156,14 @@ static inline int BS_FUNC(size)(const BSCTX *bc) · 155 unmodified lines | ||
| 156 | 156 | return bc->size_in_bits; |
| 157 | 157 | } |
| 158 | 158 | |
| 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 | + | |
| 159 | 167 | /** |
| 160 | 168 | * Return the number of the bits left in a buffer. |
| 161 | 169 | */ |
| @@ -76,6 +76,7 @@ · 75 unmodified lines | ||
| 76 | 76 | typedef BitstreamContext GetBitContext; |
| 77 | 77 | |
| 78 | 78 | #define get_bits_count bits_tell |
| 79 | +#define get_bits_bytesize bits_bytesize | |
| 79 | 80 | #define get_bits_left bits_left |
| 80 | 81 | #define skip_bits_long bits_skip |
| 81 | 82 | #define skip_bits bits_skip |
| @@ -251,6 +252,20 @@ static inline int get_bits_count(const GetBitContext *s) · 169 unmodified lines | ||
| 251 | 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 | + */ | |
| 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 | + | |
| 254 | 269 | /** |
| 255 | 270 | * Skips the specified number of bits. |
| 256 | 271 | * @param n the number of bits to skip, |
| @@ -208,7 +208,8 @@ static int decode_slice(H263DecContext *const h) · 207 unmodified lines | ||
| 208 | 208 | |
| 209 | 209 | if (h->c.avctx->hwaccel) { |
| 210 | 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 | 213 | // ensure we exit decode loop |
| 213 | 214 | h->c.mb_y = h->c.mb_height; |
| 214 | 215 | return ret; |
| @@ -372,7 +373,7 @@ static int decode_slice(H263DecContext *const h) · 157 unmodified lines | ||
| 372 | 373 | if (h->c.codec_id == AV_CODEC_ID_H263 && |
| 373 | 374 | (h->c.workaround_bugs & FF_BUG_AUTODETECT) && |
| 374 | 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 | 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 | 548 | if (avctx->hwaccel) { |
| 548 | 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 | 551 | if (ret < 0 ) |
| 551 | 552 | return ret; |
| 552 | 553 | } |
| @@ -294,7 +294,7 @@ int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx, · 293 unmodified lines | ||
| 294 | 294 | if (!sps) |
| 295 | 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 | 298 | if (sps->data_size > sizeof(sps->data)) { |
| 299 | 299 | av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n"); |
| 300 | 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 | 712 | if (!pps) |
| 713 | 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 | 716 | if (pps->data_size > sizeof(pps->data)) { |
| 717 | 717 | av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS " |
| 718 | 718 | "(%"SIZE_SPECIFIER" > %"SIZE_SPECIFIER")\n", |
| @@ -2752,7 +2752,7 @@ static int hls_decode_entry(HEVCContext *s, GetBitContext *gb) · 2751 unmodified lines | ||
| 2752 | 2752 | const HEVCPPS *const pps = s->pps; |
| 2753 | 2753 | const HEVCSPS *const sps = pps->sps; |
| 2754 | 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 | 2756 | int ctb_size = 1 << sps->log2_ctb_size; |
| 2757 | 2757 | int more_data = 1; |
| 2758 | 2758 | int x_ctb = 0; |
| @@ -763,7 +763,7 @@ int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, · 762 unmodified lines | ||
| 763 | 763 | { |
| 764 | 764 | int i; |
| 765 | 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 | 767 | int ret = AVERROR_INVALIDDATA; |
| 768 | 768 | uint64_t layer1_id_included = 0; |
| 769 | 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 | 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 | 1714 | sps->data = av_memdup(gb->buffer, sps->data_size); |
| 1715 | 1715 | if (!sps->data) { |
| 1716 | 1716 | ret = AVERROR(ENOMEM); |
| @@ -2165,7 +2165,7 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, · 448 unmodified lines | ||
| 2165 | 2165 | const HEVCSPS *sps = NULL; |
| 2166 | 2166 | const HEVCVPS *vps = NULL; |
| 2167 | 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 | 2169 | unsigned int pps_id = get_ue_golomb_long(gb); |
| 2170 | 2170 | unsigned log2_parallel_merge_level_minus2; |
| 2171 | 2171 | HEVCPPS *pps; |
| @@ -1635,6 +1635,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg) · 1634 unmodified lines | ||
| 1635 | 1635 | { |
| 1636 | 1636 | Mpeg12SliceContext *const s = *(void **) arg; |
| 1637 | 1637 | const uint8_t *buf = s->gb.buffer; |
| 1638 | + const uint8_t *end = buf + get_bits_bytesize(&s->gb, 0); | |
| 1638 | 1639 | int mb_y = s->c.start_mb_y; |
| 1639 | 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 | 1645 | uint32_t start_code; |
| 1645 | 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 | 1649 | emms_c(); |
| 1649 | 1650 | ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n", |
| 1650 | 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 | 1667 | return 0; |
| 1667 | 1668 | |
| 1668 | 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 | 1671 | if (start_code < SLICE_MIN_START_CODE || start_code > SLICE_MAX_START_CODE) |
| 1671 | 1672 | return AVERROR_INVALIDDATA; |
| 1672 | 1673 | mb_y = start_code - SLICE_MIN_START_CODE; |