This avoids allocating/freeing the buffer for each frame and simplifies error
handling in transcode_video().
---
 avconv.c |   30 +++++++++++++++++-------------
 1 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/avconv.c b/avconv.c
index 54aa89b..f3daf30 100644
--- a/avconv.c
+++ b/avconv.c
@@ -147,6 +147,8 @@ typedef struct InputStream {
     AVCodec *dec;
     AVFrame *decoded_frame;
     AVFrame *filtered_frame;
+    uint8_t *deinterlace_buffer;
+    int deinterlace_buffer_size;
 
     int64_t       start;     /* time when read started */
     int64_t       next_pts;  /* synthetic pts for cases where pkt.pts
@@ -528,6 +530,7 @@ void exit_program(int ret)
     for (i = 0; i < nb_input_streams; i++) {
         av_freep(&input_streams[i].decoded_frame);
         av_freep(&input_streams[i].filtered_frame);
+        av_freep(&input_streams[i].deinterlace_buffer);
         av_dict_free(&input_streams[i].opts);
     }
 
@@ -982,7 +985,7 @@ need_realloc:
     }
 }
 
-static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void 
**bufp)
+static int pre_process_video_frame(InputStream *ist, AVPicture *picture)
 {
     AVCodecContext *dec;
     AVPicture *picture2;
@@ -997,9 +1000,13 @@ static void pre_process_video_frame(InputStream *ist, 
AVPicture *picture, void *
 
         /* create temporary picture */
         size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
-        buf = av_malloc(size);
-        if (!buf)
-            return;
+        if (!ist->deinterlace_buffer || ist->deinterlace_buffer_size < size) {
+            if (!(buf = av_realloc(ist->deinterlace_buffer, size)))
+                return AVERROR(ENOMEM);
+            ist->deinterlace_buffer      = buf;
+            ist->deinterlace_buffer_size = size;
+        } else
+            buf = ist->deinterlace_buffer;
 
         picture2 = &picture_tmp;
         avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
@@ -1008,8 +1015,6 @@ static void pre_process_video_frame(InputStream *ist, 
AVPicture *picture, void *
                                  dec->pix_fmt, dec->width, dec->height) < 0) {
             /* if error, do not deinterlace */
             av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
-            av_free(buf);
-            buf = NULL;
             picture2 = picture;
         }
     } else {
@@ -1018,7 +1023,8 @@ static void pre_process_video_frame(InputStream *ist, 
AVPicture *picture, void *
 
     if (picture != picture2)
         *picture = *picture2;
-    *bufp = buf;
+
+    return 0;
 }
 
 static void do_subtitle_out(AVFormatContext *s,
@@ -1727,7 +1733,6 @@ static int transcode_audio(InputStream *ist, AVPacket 
*pkt, int *got_output)
 static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, 
int64_t *pkt_pts)
 {
     AVFrame *decoded_frame, *filtered_frame = NULL;
-    void *buffer_to_free = NULL;
     int i, ret = 0;
     float quality;
 #if CONFIG_AVFILTER
@@ -1765,7 +1770,8 @@ static int transcode_video(InputStream *ist, AVPacket 
*pkt, int *got_output, int
                           ist->st->codec->time_base.den;
     }
     pkt->size = 0;
-    pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
+    if ((ret = pre_process_video_frame(ist, (AVPicture *)decoded_frame)))
+        return ret;
 
     rate_emu_sleep(ist);
 
@@ -1784,10 +1790,9 @@ static int transcode_video(InputStream *ist, AVPacket 
*pkt, int *got_output, int
             else
                 sar = ist->st->codec->sample_aspect_ratio;
             av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, 
ist->pts, sar);
-            if (!ist->filtered_frame && !(ist->filtered_frame = 
avcodec_alloc_frame())) {
-                av_free(buffer_to_free);
+            if (!ist->filtered_frame && !(ist->filtered_frame = 
avcodec_alloc_frame()))
                 return AVERROR(ENOMEM);
-            } else
+            else
                 avcodec_get_frame_defaults(ist->filtered_frame);
             filtered_frame = ist->filtered_frame;
             frame_available = 
avfilter_poll_frame(ost->output_video_filter->inputs[0]);
@@ -1816,7 +1821,6 @@ static int transcode_video(InputStream *ist, AVPacket 
*pkt, int *got_output, int
 #endif
     }
 
-    av_free(buffer_to_free);
     return ret;
 }
 
-- 
1.7.1

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to