Browse Source

avcodec: Remove cumbersome way of checking for amount of bytes left

Several encoders used code like the following to check for the amount of
bytes left in a PutBitContext:
pb->buf_end - pb->buf - (put_bits_count(pb) >> 3)
Besides the fact that using the pointers directly might pose
a maintainence burden in the future this also leads to suboptimal code:
The above code reads all three pointers (buf, buf_ptr and buf_end), but
touching buf is unnecessary and switching to put_bytes_left()
automatically fixes this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
master
Andreas Rheinhardt Andreas Rheinhardt 4 years ago
parent
commit
67f6e7ed6d
7 changed files with 16 additions and 19 deletions
  1. +1
    -1
      libavcodec/asvenc.c
  2. +1
    -1
      libavcodec/ffv1enc_template.c
  3. +4
    -5
      libavcodec/huffyuvenc.c
  4. +2
    -2
      libavcodec/ljpegenc.c
  5. +6
    -7
      libavcodec/mpegvideo_enc.c
  6. +1
    -2
      libavcodec/svq1enc.c
  7. +1
    -1
      libavcodec/xsubenc.c

+ 1
- 1
libavcodec/asvenc.c View File

@@ -167,7 +167,7 @@ static inline int encode_mb(ASV1Context *a, int16_t block[6][64])
{
int i;

av_assert0(a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) >= MAX_MB_SIZE);
av_assert0(put_bytes_left(&a->pb, 0) >= MAX_MB_SIZE);

if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
for (i = 0; i < 6; i++)


+ 1
- 1
libavcodec/ffv1enc_template.c View File

@@ -37,7 +37,7 @@ static av_always_inline int RENAME(encode_line)(FFV1Context *s, int w,
return AVERROR_INVALIDDATA;
}
} else {
if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
if (put_bytes_left(&s->pb, 0) < w * 4) {
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return AVERROR_INVALIDDATA;
}


+ 4
- 5
libavcodec/huffyuvenc.c View File

@@ -443,7 +443,7 @@ static int encode_422_bitstream(HYuvContext *s, int offset, int count)
const uint8_t *u = s->temp[1] + offset / 2;
const uint8_t *v = s->temp[2] + offset / 2;

if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) {
if (put_bytes_left(&s->pb, 0) < 2 * 4 * count) {
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
@@ -495,7 +495,7 @@ static int encode_plane_bitstream(HYuvContext *s, int width, int plane)
{
int i, count = width/2;

if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < count * s->bps / 2) {
if (put_bytes_left(&s->pb, 0) < count * s->bps / 2) {
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
@@ -657,7 +657,7 @@ static int encode_gray_bitstream(HYuvContext *s, int count)
{
int i;

if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) {
if (put_bytes_left(&s->pb, 0) < 4 * count) {
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
@@ -702,8 +702,7 @@ static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
{
int i;

if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
4 * planes * count) {
if (put_bytes_left(&s->pb, 0) < 4 * planes * count) {
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}


+ 2
- 2
libavcodec/ljpegenc.c View File

@@ -86,7 +86,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
const int modified_predictor = y ? s->pred : 1;
uint8_t *ptr = frame->data[0] + (linesize * y);

if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 4 * 4) {
if (put_bytes_left(pb, 0) < width * 4 * 4) {
av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
@@ -211,7 +211,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
#endif

for (mb_y = 0; mb_y < mb_height; mb_y++) {
if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) <
if (put_bytes_left(pb, 0) <
mb_width * 4 * 3 * s->hsample[0] * s->vsample[0]) {
av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;


+ 6
- 7
libavcodec/mpegvideo_enc.c View File

@@ -1993,8 +1993,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
stuffing_count = ff_vbv_update(s, s->frame_bits);
s->stuffing_bits = 8*stuffing_count;
if (stuffing_count) {
if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
stuffing_count + 50) {
if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
return -1;
}
@@ -2911,7 +2910,7 @@ static void update_mb_info(MpegEncContext *s, int startcode)

int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
{
if ( s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold
if (put_bytes_left(&s->pb, 0) < threshold
&& s->slice_context_count == 1
&& s->pb.buf == s->avctx->internal->byte_buffer) {
int lastgob_pos = s->ptr_lastgob - s->pb.buf;
@@ -2940,7 +2939,7 @@ int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t s
s->ptr_lastgob = s->pb.buf + lastgob_pos;
s->vbv_delay_ptr = s->pb.buf + vbv_pos;
}
if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold)
if (put_bytes_left(&s->pb, 0) < threshold)
return AVERROR(EINVAL);
return 0;
}
@@ -3032,13 +3031,13 @@ static int encode_thread(AVCodecContext *c, void *arg){
+ s->mb_width*MAX_MB_BYTES;

ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
if(s->data_partitioning){
if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
|| s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
if (put_bytes_left(&s->pb2, 0) < MAX_MB_BYTES ||
put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
return -1;
}


+ 1
- 2
libavcodec/svq1enc.c View File

@@ -372,8 +372,7 @@ static int svq1_encode_plane(SVQ1EncContext *s, int plane,
int score[4] = { 0, 0, 0, 0 }, best;
uint8_t *temp = s->scratchbuf;

if (s->pb.buf_end - s->pb.buf -
(put_bits_count(&s->pb) >> 3) < 3000) { // FIXME: check size
if (put_bytes_left(&s->pb, 0) < 3000) { // FIXME: check size
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}


+ 1
- 1
libavcodec/xsubenc.c View File

@@ -63,7 +63,7 @@ static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
x0 = 0;
while (x0 < w) {
// Make sure we have enough room for at least one run and padding
if (pb->size_in_bits - put_bits_count(pb) < 7*8)
if (put_bytes_left(pb, 1) < 7)
return AVERROR_BUFFER_TOO_SMALL;

x1 = x0;


Loading…
Cancel
Save