[x264-devel] lavf input: allocate AVFrame correctly

Anton Khirnov git at videolan.org
Wed Jan 9 19:32:21 CET 2013


x264 | branch: master | Anton Khirnov <anton at khirnov.net> | Tue Nov 13 21:01:24 2012 +0100| [6e68ab73908f339cdd91c40943fef46fd1f832fa] | committer: Jason Garrett-Glaser

lavf input: allocate AVFrame correctly
Allocate AVFrames correctly with avcodec_alloc_frame().
This caused crashes with newer libavcodecs that try to free frame extradata.

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=6e68ab73908f339cdd91c40943fef46fd1f832fa
---

 input/lavf.c |   29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/input/lavf.c b/input/lavf.c
index 8ca5fe7..91b2e98 100644
--- a/input/lavf.c
+++ b/input/lavf.c
@@ -28,12 +28,14 @@
 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "lavf", __VA_ARGS__ )
 #undef DECLARE_ALIGNED
 #include <libavformat/avformat.h>
+#include <libavutil/mem.h>
 #include <libavutil/pixdesc.h>
 #include <libavutil/dict.h>
 
 typedef struct
 {
     AVFormatContext *lavf;
+    AVFrame *frame;
     int stream_id;
     int next_frame;
     int vfr_input;
@@ -80,8 +82,8 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
 
     AVCodecContext *c = h->lavf->streams[h->stream_id]->codec;
     AVPacket *pkt = p_pic->opaque;
-    AVFrame frame;
-    avcodec_get_frame_defaults( &frame );
+
+    avcodec_get_frame_defaults( h->frame );
 
     while( i_frame >= h->next_frame )
     {
@@ -97,7 +99,7 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
                     pkt->size = 0;
 
                 c->reordered_opaque = pkt->pts;
-                if( avcodec_decode_video2( c, &frame, &finished, pkt ) < 0 )
+                if( avcodec_decode_video2( c, h->frame, &finished, pkt ) < 0 )
                     x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame );
             }
             /* if the packet successfully decoded but the data from it is not desired, free it */
@@ -111,8 +113,8 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
         h->next_frame++;
     }
 
-    memcpy( p_pic->img.stride, frame.linesize, sizeof(p_pic->img.stride) );
-    memcpy( p_pic->img.plane, frame.data, sizeof(p_pic->img.plane) );
+    memcpy( p_pic->img.stride, h->frame->linesize, sizeof(p_pic->img.stride) );
+    memcpy( p_pic->img.plane, h->frame->data, sizeof(p_pic->img.plane) );
     int is_fullrange   = 0;
     p_pic->img.width   = c->width;
     p_pic->img.height  = c->height;
@@ -121,15 +123,15 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
     if( info )
     {
         info->fullrange  = is_fullrange;
-        info->interlaced = frame.interlaced_frame;
-        info->tff        = frame.top_field_first;
+        info->interlaced = h->frame->interlaced_frame;
+        info->tff        = h->frame->top_field_first;
     }
 
     if( h->vfr_input )
     {
         p_pic->pts = p_pic->duration = 0;
-        if( c->has_b_frames && frame.reordered_opaque != AV_NOPTS_VALUE )
-            p_pic->pts = frame.reordered_opaque;
+        if( c->has_b_frames && h->frame->reordered_opaque != AV_NOPTS_VALUE )
+            p_pic->pts = h->frame->reordered_opaque;
         else if( pkt->dts != AV_NOPTS_VALUE )
             p_pic->pts = pkt->dts; // for AVI files
         else if( info )
@@ -151,6 +153,10 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
     if( !strcmp( psz_filename, "-" ) )
         psz_filename = "pipe:";
 
+    h->frame = avcodec_alloc_frame();
+    if( !h->frame )
+        return -1;
+
     /* if resolution was passed in, place it and colorspace into options. this allows raw video support */
     AVDictionary *options = NULL;
     if( opt->resolution )
@@ -246,6 +252,11 @@ static int close_file( hnd_t handle )
     lavf_hnd_t *h = handle;
     avcodec_close( h->lavf->streams[h->stream_id]->codec );
     avformat_close_input( &h->lavf );
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
+    avcodec_free_frame( &h->frame );
+#else
+    av_freep( &h->frame );
+#endif
     free( h );
     return 0;
 }



More information about the x264-devel mailing list