]> git.sesse.net Git - x264/blobdiff - input/lavf.c
Fix deprecation in libavformat usage
[x264] / input / lavf.c
index 52e890c5f817fb509b2e144e973d2766336617d6..424d3cb0568b182ec711f145c4eab0e42db03ede 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * lavf.c: x264 libavformat input module
+ * lavf.c: libavformat input
  *****************************************************************************
- * Copyright (C) 2009 x264 project
+ * Copyright (C) 2009-2011 x264 project
  *
  * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
  *          Steven Walters <kemuri9@gmail.com>
@@ -19,6 +19,9 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at licensing@x264.com.
  *****************************************************************************/
 
 #include "input.h"
@@ -26,6 +29,7 @@
 #undef DECLARE_ALIGNED
 #include <libavformat/avformat.h>
 #include <libavutil/pixdesc.h>
+#include <libavutil/dict.h>
 
 typedef struct
 {
@@ -36,6 +40,12 @@ typedef struct
     cli_pic_t *first_pic;
 } lavf_hnd_t;
 
+#define x264_free_packet( pkt )\
+{\
+    av_free_packet( pkt );\
+    av_init_packet( pkt );\
+}
+
 static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
 {
     if( h->first_pic && !info )
@@ -64,20 +74,28 @@ static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, vi
     while( i_frame >= h->next_frame )
     {
         int finished = 0;
-        while( !finished && av_read_frame( h->lavf, pkt ) >= 0 )
+        int ret = 0;
+        do
+        {
+            ret = av_read_frame( h->lavf, pkt );
+
             if( pkt->stream_index == h->stream_id )
             {
+                if( ret < 0 )
+                    pkt->size = 0;
+
                 c->reordered_opaque = pkt->pts;
                 if( avcodec_decode_video2( c, &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 );
+        } while( !finished && ret >= 0 );
+
         if( !finished )
-        {
-            if( avcodec_decode_video2( c, &frame, &finished, pkt ) < 0 )
-                x264_cli_log( "lavf", X264_LOG_WARNING, "video decoding failed on frame %d\n", h->next_frame );
-            if( !finished )
-                return -1;
-        }
+            return -1;
+
         h->next_frame++;
     }
 
@@ -119,24 +137,27 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
     if( !strcmp( psz_filename, "-" ) )
         psz_filename = "pipe:";
 
-    /* if resolution was passed in, parse it and colorspace into parameters. this allows raw video support */
-    AVFormatParameters *param = NULL;
+    /* if resolution was passed in, place it and colorspace into options. this allows raw video support */
+    AVDictionary *options = NULL;
     if( opt->resolution )
     {
-        param = calloc( 1, sizeof(AVFormatParameters) );
-        if( !param )
-            return -1;
-        sscanf( opt->resolution, "%dx%d", &param->width, &param->height );
-        param->pix_fmt = opt->colorspace ? av_get_pix_fmt( opt->colorspace ) : PIX_FMT_YUV420P;
+        av_dict_set( &options, "video_size", opt->resolution, 0 );
+        const char *csp = opt->colorspace ? opt->colorspace : av_get_pix_fmt_name( PIX_FMT_YUV420P );
+        av_dict_set( &options, "pixel_format", csp, 0 );
     }
 
-    FAIL_IF_ERROR( av_open_input_file( &h->lavf, psz_filename, NULL, 0, param ), "could not open input file\n" )
-    if( param )
-        free( param );
+    /* specify the input format. this is helpful when lavf fails to guess */
+    AVInputFormat *format = NULL;
+    if( opt->format )
+        FAIL_IF_ERROR( !(format = av_find_input_format( opt->format )), "unknown file format: %s\n", opt->format );
+
+    FAIL_IF_ERROR( avformat_open_input( &h->lavf, psz_filename, format, &options ), "could not open input file\n" )
+    if( options )
+        av_dict_free( &options );
     FAIL_IF_ERROR( av_find_stream_info( h->lavf ) < 0, "could not find input stream info\n" )
 
     int i = 0;
-    while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != CODEC_TYPE_VIDEO )
+    while( i < h->lavf->nb_streams && h->lavf->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO )
         i++;
     FAIL_IF_ERROR( i == h->lavf->nb_streams, "could not find video stream\n" )
     h->stream_id       = i;
@@ -195,8 +216,7 @@ static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
 
 static int release_frame( cli_pic_t *pic, hnd_t handle )
 {
-    av_free_packet( pic->opaque );
-    av_init_packet( pic->opaque );
+    x264_free_packet( pic->opaque );
     return 0;
 }