[x264-devel] Update to the current lavf API and fix memory leak when using --seek

Anton Mitrofanov git at videolan.org
Tue Aug 26 18:23:14 CEST 2014


x264 | branch: master | Anton Mitrofanov <BugMaster at narod.ru> | Wed Jul 30 03:03:32 2014 +0400| [dd79a61e0e354a432907f2d1f7137b27a12dfce7] | committer: Fiona Glaser

Update to the current lavf API and fix memory leak when using --seek

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

 configure    |    2 +-
 input/ffms.c |    3 ++-
 input/lavf.c |   67 ++++++++++++++++++++++------------------------------------
 3 files changed, 28 insertions(+), 44 deletions(-)

diff --git a/configure b/configure
index 8f54a34..b09149c 100755
--- a/configure
+++ b/configure
@@ -926,7 +926,7 @@ if [ "$lavf" = "auto" ] ; then
         done
     fi
     LAVF_LIBS="-L. $LAVF_LIBS"
-    if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" "avformat_close_input(0);" ; then
+    if cc_check libavformat/avformat.h "$LAVF_CFLAGS $LAVF_LIBS" "av_frame_free(0);" ; then
         if [ "$swscale" = "yes" ]; then
             lavf="yes"
         else
diff --git a/input/ffms.c b/input/ffms.c
index d9cb5f3..2201a58 100644
--- a/input/ffms.c
+++ b/input/ffms.c
@@ -177,8 +177,9 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
 
 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
 {
-    if( x264_cli_pic_alloc( pic, csp, width, height ) )
+    if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
         return -1;
+    pic->img.csp = csp;
     pic->img.planes = 4;
     return 0;
 }
diff --git a/input/lavf.c b/input/lavf.c
index 27a61d9..be5e530 100644
--- a/input/lavf.c
+++ b/input/lavf.c
@@ -42,12 +42,6 @@ typedef struct
     cli_pic_t *first_pic;
 } lavf_hnd_t;
 
-#define x264_free_packet( pkt )\
-{\
-    av_free_packet( pkt );\
-    av_init_packet( pkt );\
-}
-
 /* handle the deprecated jpeg pixel formats */
 static int handle_jpeg( int csp, int *fullrange )
 {
@@ -70,9 +64,7 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
         {
             XCHG( cli_image_t, p_pic->img, h->first_pic->img );
             p_pic->pts = h->first_pic->pts;
-            XCHG( void*, p_pic->opaque, h->first_pic->opaque );
         }
-        lavf_input.release_frame( h->first_pic, NULL );
         lavf_input.picture_clean( h->first_pic );
         free( h->first_pic );
         h->first_pic = NULL;
@@ -81,9 +73,11 @@ 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;
 
-    avcodec_get_frame_defaults( h->frame );
+    AVPacket pkt;
+    av_init_packet( &pkt );
+    pkt.data = NULL;
+    pkt.size = 0;
 
     while( i_frame >= h->next_frame )
     {
@@ -91,20 +85,23 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
         int ret = 0;
         do
         {
-            ret = av_read_frame( h->lavf, pkt );
+            ret = av_read_frame( h->lavf, &pkt );
 
-            if( pkt->stream_index == h->stream_id )
+            if( ret < 0 )
             {
-                if( ret < 0 )
-                    pkt->size = 0;
+                av_init_packet( &pkt );
+                pkt.data = NULL;
+                pkt.size = 0;
+            }
 
-                c->reordered_opaque = pkt->pts;
-                if( avcodec_decode_video2( c, h->frame, &finished, pkt ) < 0 )
+            if( ret < 0 || pkt.stream_index == h->stream_id )
+            {
+                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 */
-            else if( ret >= 0 )
-                x264_free_packet( pkt );
+
+            if( ret >= 0 )
+                av_free_packet( &pkt );
         } while( !finished && ret >= 0 );
 
         if( !finished )
@@ -130,10 +127,10 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
     if( h->vfr_input )
     {
         p_pic->pts = p_pic->duration = 0;
-        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
+        if( h->frame->pkt_pts != AV_NOPTS_VALUE )
+            p_pic->pts = h->frame->pkt_pts;
+        else if( h->frame->pkt_dts != AV_NOPTS_VALUE )
+            p_pic->pts = h->frame->pkt_dts; // for AVI files
         else if( info )
         {
             h->vfr_input = info->vfr = 0;
@@ -153,7 +150,7 @@ 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();
+    h->frame = av_frame_alloc();
     if( !h->frame )
         return -1;
 
@@ -220,13 +217,10 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
 
 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
 {
-    if( x264_cli_pic_alloc( pic, csp, width, height ) )
+    if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
         return -1;
+    pic->img.csp = csp;
     pic->img.planes = 4;
-    pic->opaque = malloc( sizeof(AVPacket) );
-    if( !pic->opaque )
-        return -1;
-    av_init_packet( pic->opaque );
     return 0;
 }
 
@@ -235,15 +229,8 @@ static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
     return read_frame_internal( pic, handle, i_frame, NULL );
 }
 
-static int release_frame( cli_pic_t *pic, hnd_t handle )
-{
-    x264_free_packet( pic->opaque );
-    return 0;
-}
-
 static void picture_clean( cli_pic_t *pic )
 {
-    free( pic->opaque );
     memset( pic, 0, sizeof(cli_pic_t) );
 }
 
@@ -252,13 +239,9 @@ 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
+    av_frame_free( &h->frame );
     free( h );
     return 0;
 }
 
-const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };
+const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };



More information about the x264-devel mailing list