[FFmpeg-devel] [PATCH 1/3] avcodec/mpegvideo_enc: Avoid using ->internal->byte_buffer*

Michael Niedermayer michaelni at gmx.at
Tue Aug 4 10:44:22 CEST 2015


From: Michael Niedermayer <michael at niedermayer.cc>

This causes a speedloss from 8.837 to 8.864 sec when encoding matrixbench to mpeg4
similar speedloss occurs for high and low bitrate encoding
This should not be applied unless more elaborate benchmarking indicates that above
is the exception

Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
---
 libavcodec/mpegvideo.h     |    3 +++
 libavcodec/mpegvideo_enc.c |   34 +++++++++++++++-------------------
 2 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 8492045..62ee505 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -162,6 +162,9 @@ typedef struct MpegEncContext {
     struct MpegEncContext *thread_context[MAX_THREADS];
     int slice_context_count;   ///< number of used thread_contexts
 
+    int growable_size;
+    AVPacket *growable_pkt;
+
     /**
      * copy of the previous picture structure.
      * note, linesize & data, might not match the previous picture (for field pictures)
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 488ec51..80b1a45 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1749,6 +1749,8 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
     int i, stuffing_count, ret;
     int context_count = s->slice_context_count;
 
+    s->growable_pkt = NULL;
+
     s->vbv_ignore_qmax = 0;
 
     s->picture_in_gop_number++;
@@ -1763,11 +1765,13 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
     /* output? */
     if (s->new_picture.f->data[0]) {
         int growing_buffer = context_count == 1 && !pkt->data && !s->data_partitioning;
-        int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - AV_INPUT_BUFFER_PADDING_SIZE
+        int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, s->growable_size) - AV_INPUT_BUFFER_PADDING_SIZE
                                               :
                                               s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
-        if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
+        if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, pkt_size)) < 0)
             return ret;
+        s->growable_pkt  = pkt;
+        s->growable_size = pkt->size;
         if (s->mb_info) {
             s->mb_info_ptr = av_packet_new_side_data(pkt,
                                  AV_PKT_DATA_H263_MB_INFO,
@@ -1793,9 +1797,8 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
 vbv_retry:
         ret = encode_picture(s, s->picture_number);
         if (growing_buffer) {
-            av_assert0(s->pb.buf == avctx->internal->byte_buffer);
-            pkt->data = s->pb.buf;
-            pkt->size = avctx->internal->byte_buffer_size;
+            av_assert0(s->pb.buf == pkt->data);
+            av_assert0(pkt->size == s->growable_size);
         }
         if (ret < 0)
             return -1;
@@ -2773,25 +2776,18 @@ int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t s
 {
     if (   s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold
         && s->slice_context_count == 1
-        && s->pb.buf == s->avctx->internal->byte_buffer) {
+        && s->pb.buf == s->growable_pkt->data) {
         int lastgob_pos = s->ptr_lastgob - s->pb.buf;
         int vbv_pos     = s->vbv_delay_ptr - s->pb.buf;
 
-        uint8_t *new_buffer = NULL;
-        int new_buffer_size = 0;
-
-        av_fast_padded_malloc(&new_buffer, &new_buffer_size,
-                              s->avctx->internal->byte_buffer_size + size_increase);
-        if (!new_buffer)
-            return AVERROR(ENOMEM);
+        int ret = av_grow_packet(s->growable_pkt, size_increase);
+        if (ret < 0)
+            return ret;
 
-        memcpy(new_buffer, s->avctx->internal->byte_buffer, s->avctx->internal->byte_buffer_size);
-        av_free(s->avctx->internal->byte_buffer);
-        s->avctx->internal->byte_buffer      = new_buffer;
-        s->avctx->internal->byte_buffer_size = new_buffer_size;
-        rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
+        rebase_put_bits(&s->pb, s->growable_pkt->data, s->growable_pkt->size);
         s->ptr_lastgob   = s->pb.buf + lastgob_pos;
         s->vbv_delay_ptr = s->pb.buf + vbv_pos;
+        s->growable_size = s->growable_pkt->size;
     }
     if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold)
         return AVERROR(EINVAL);
@@ -2874,7 +2870,7 @@ static int encode_thread(AVCodecContext *c, void *arg){
 //            int d;
             int dmin= INT_MAX;
             int dir;
-            int size_increase =  s->avctx->internal->byte_buffer_size/4
+            int size_increase =  s->growable_pkt->size/4
                                + s->mb_width*MAX_MB_BYTES;
 
             ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list