]> git.sesse.net Git - x264/blobdiff - input/lavf.c
Switch to exponential interpolation between presets.
[x264] / input / lavf.c
index 180e509f46c65ea7460085177084be676d2cd3a5..4a1e59119f00c65bf5a3849537216c83500e13e8 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * lavf.c: x264 libavformat input module
+ * lavf.c: libavformat input
  *****************************************************************************
- * Copyright (C) 2009 x264 project
+ * Copyright (C) 2009-2016 x264 project
  *
  * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
  *          Steven Walters <kemuri9@gmail.com>
  * 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 "muxers.h"
+#include "input.h"
+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "lavf", __VA_ARGS__ )
 #undef DECLARE_ALIGNED
 #include <libavformat/avformat.h>
-#include <libswscale/swscale.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;
-    int vertical_flip;
-    struct SwsContext *scaler;
-    int pts_offset_flag;
-    int64_t pts_offset;
-    x264_picture_t *first_pic;
-
-    int init_width;
-    int init_height;
-
-    int cur_width;
-    int cur_height;
-    enum PixelFormat cur_pix_fmt;
+    cli_pic_t *first_pic;
 } lavf_hnd_t;
 
-typedef struct
-{
-    AVFrame frame;
-    AVPacket packet;
-} lavf_pic_t;
-
-static int check_swscale( lavf_hnd_t *h, AVCodecContext *c, int i_frame )
+/* handle the deprecated jpeg pixel formats */
+static int handle_jpeg( int csp, int *fullrange )
 {
-    if( h->scaler && (h->cur_width == c->width) && (h->cur_height == c->height) && (h->cur_pix_fmt == c->pix_fmt) )
-        return 0;
-    if( h->scaler )
+    switch( csp )
     {
-        sws_freeContext( h->scaler );
-        fprintf( stderr, "lavf [warning]: stream properties changed to %dx%d, %s at frame %d  \n",
-                 c->width, c->height, avcodec_get_pix_fmt_name( c->pix_fmt ), i_frame );
-        h->cur_width   = c->width;
-        h->cur_height  = c->height;
-        h->cur_pix_fmt = c->pix_fmt;
-    }
-    h->scaler = sws_getContext( h->cur_width, h->cur_height, h->cur_pix_fmt, h->init_width, h->init_height,
-                                PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
-    if( !h->scaler )
-    {
-        fprintf( stderr, "lavf [error]: could not open swscale context\n" );
-        return -1;
+        case AV_PIX_FMT_YUVJ420P: *fullrange = 1; return AV_PIX_FMT_YUV420P;
+        case AV_PIX_FMT_YUVJ422P: *fullrange = 1; return AV_PIX_FMT_YUV422P;
+        case AV_PIX_FMT_YUVJ444P: *fullrange = 1; return AV_PIX_FMT_YUV444P;
+        default:                               return csp;
     }
-    return 0;
 }
 
-static int read_frame_internal( x264_picture_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
+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 )
     {
@@ -83,10 +62,10 @@ static int read_frame_internal( x264_picture_t *p_pic, lavf_hnd_t *h, int i_fram
          * if so, retrieve the pts and image data before freeing it. */
         if( !i_frame )
         {
-            XCHG( x264_image_t, p_pic->img, h->first_pic->img );
-            p_pic->i_pts = h->first_pic->i_pts;
+            XCHG( cli_image_t, p_pic->img, h->first_pic->img );
+            p_pic->pts = h->first_pic->pts;
         }
-        lavf_input.picture_clean( h->first_pic );
+        lavf_input.picture_clean( h->first_pic, h );
         free( h->first_pic );
         h->first_pic = NULL;
         if( !i_frame )
@@ -94,179 +73,175 @@ static int read_frame_internal( x264_picture_t *p_pic, lavf_hnd_t *h, int i_fram
     }
 
     AVCodecContext *c = h->lavf->streams[h->stream_id]->codec;
-    lavf_pic_t *pic_h = p_pic->opaque;
-    AVPacket *pkt = &pic_h->packet;
-    AVFrame *frame = &pic_h->frame;
+
+    AVPacket pkt;
+    av_init_packet( &pkt );
+    pkt.data = NULL;
+    pkt.size = 0;
 
     while( i_frame >= h->next_frame )
     {
         int finished = 0;
-        while( !finished && av_read_frame( h->lavf, pkt ) >= 0 )
-            if( pkt->stream_index == h->stream_id )
+        int ret = 0;
+        do
+        {
+            ret = av_read_frame( h->lavf, &pkt );
+
+            if( ret < 0 )
+            {
+                av_init_packet( &pkt );
+                pkt.data = NULL;
+                pkt.size = 0;
+            }
+
+            if( ret < 0 || pkt.stream_index == h->stream_id )
             {
-                c->reordered_opaque = pkt->pts;
-                if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
-                    fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
+                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( ret >= 0 )
+                av_free_packet( &pkt );
+        } while( !finished && ret >= 0 );
+
         if( !finished )
-        {
-            if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
-                fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
-            if( !finished )
-                return -1;
-        }
+            return -1;
+
         h->next_frame++;
     }
 
-    if( check_swscale( h, c, i_frame ) )
-        return -1;
-    /* FIXME: avoid sws_scale where possible (no colorspace conversion). */
-    sws_scale( h->scaler, frame->data, frame->linesize, 0, c->height, p_pic->img.plane, p_pic->img.i_stride );
+    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;
+    p_pic->img.csp     = handle_jpeg( c->pix_fmt, &is_fullrange ) | X264_CSP_OTHER;
 
     if( info )
-        info->interlaced = frame->interlaced_frame;
+    {
+        info->fullrange  = is_fullrange;
+        info->interlaced = h->frame->interlaced_frame;
+        info->tff        = h->frame->top_field_first;
+    }
 
     if( h->vfr_input )
     {
-        p_pic->i_pts = 0;
-        if( frame->reordered_opaque != AV_NOPTS_VALUE )
-            p_pic->i_pts = frame->reordered_opaque;
-        else if( pkt->dts != AV_NOPTS_VALUE )
-            p_pic->i_pts = pkt->dts; // for AVI files
+        p_pic->pts = p_pic->duration = 0;
+        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;
-            goto exit;
-        }
-        if( !h->pts_offset_flag )
-        {
-            h->pts_offset = p_pic->i_pts;
-            h->pts_offset_flag = 1;
+            return 0;
         }
-        p_pic->i_pts -= h->pts_offset;
     }
 
-exit:
-    if( pkt->destruct )
-        pkt->destruct( pkt );
-    avcodec_get_frame_defaults( frame );
     return 0;
 }
 
 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
 {
-    lavf_hnd_t *h = malloc( sizeof(lavf_hnd_t) );
+    lavf_hnd_t *h = calloc( 1, sizeof(lavf_hnd_t) );
     if( !h )
         return -1;
     av_register_all();
-    h->scaler = NULL;
     if( !strcmp( psz_filename, "-" ) )
         psz_filename = "pipe:";
 
-    if( av_open_input_file( &h->lavf, psz_filename, NULL, 0, NULL ) )
-    {
-        fprintf( stderr, "lavf [error]: could not open input file\n" );
+    h->frame = av_frame_alloc();
+    if( !h->frame )
         return -1;
-    }
 
-    if( av_find_stream_info( h->lavf ) < 0 )
+    /* if resolution was passed in, place it and colorspace into options. this allows raw video support */
+    AVDictionary *options = NULL;
+    if( opt->resolution )
     {
-        fprintf( stderr, "lavf [error]: could not find input stream info\n" );
-        return -1;
+        av_dict_set( &options, "video_size", opt->resolution, 0 );
+        const char *csp = opt->colorspace ? opt->colorspace : av_get_pix_fmt_name( AV_PIX_FMT_YUV420P );
+        av_dict_set( &options, "pixel_format", csp, 0 );
     }
 
+    /* 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( avformat_find_stream_info( h->lavf, NULL ) < 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++;
-    if( i == h->lavf->nb_streams )
-    {
-        fprintf( stderr, "lavf [error]: could not find video stream\n" );
-        return -1;
-    }
+    FAIL_IF_ERROR( i == h->lavf->nb_streams, "could not find video stream\n" )
     h->stream_id       = i;
     h->next_frame      = 0;
-    h->pts_offset_flag = 0;
-    h->pts_offset      = 0;
     AVCodecContext *c  = h->lavf->streams[i]->codec;
-    h->init_width      = h->cur_width  = info->width  = c->width;
-    h->init_height     = h->cur_height = info->height = c->height;
-    h->cur_pix_fmt     = c->pix_fmt;
-    info->fps_num      = h->lavf->streams[i]->r_frame_rate.num;
-    info->fps_den      = h->lavf->streams[i]->r_frame_rate.den;
+    info->fps_num      = h->lavf->streams[i]->avg_frame_rate.num;
+    info->fps_den      = h->lavf->streams[i]->avg_frame_rate.den;
     info->timebase_num = h->lavf->streams[i]->time_base.num;
     info->timebase_den = h->lavf->streams[i]->time_base.den;
+    /* lavf is thread unsafe as calling av_read_frame invalidates previously read AVPackets */
+    info->thread_safe  = 0;
     h->vfr_input       = info->vfr;
-    h->vertical_flip   = 0;
-
-    /* avisynth stores rgb data vertically flipped. */
-    if( !strcasecmp( get_filename_extension( psz_filename ), "avs" ) &&
-        (h->cur_pix_fmt == PIX_FMT_BGRA || h->cur_pix_fmt == PIX_FMT_BGR24) )
-        info->csp |= X264_CSP_VFLIP;
-
-    if( h->cur_pix_fmt != PIX_FMT_YUV420P )
-        fprintf( stderr, "lavf [warning]: converting from %s to YV12\n",
-                 avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
-
-    if( avcodec_open( c, avcodec_find_decoder( c->codec_id ) ) )
-    {
-        fprintf( stderr, "lavf [error]: could not find decoder for video stream\n" );
-        return -1;
-    }
+    FAIL_IF_ERROR( avcodec_open2( c, avcodec_find_decoder( c->codec_id ), NULL ),
+                   "could not find decoder for video stream\n" )
 
     /* prefetch the first frame and set/confirm flags */
-    h->first_pic = malloc( sizeof(x264_picture_t) );
-    if( !h->first_pic || lavf_input.picture_alloc( h->first_pic, info->csp, info->width, info->height ) )
-    {
-        fprintf( stderr, "lavf [error]: malloc failed\n" );
-        return -1;
-    }
+    h->first_pic = malloc( sizeof(cli_pic_t) );
+    FAIL_IF_ERROR( !h->first_pic || lavf_input.picture_alloc( h->first_pic, h, X264_CSP_OTHER, info->width, info->height ),
+                   "malloc failed\n" )
     else if( read_frame_internal( h->first_pic, h, 0, info ) )
         return -1;
 
+    info->width      = c->width;
+    info->height     = c->height;
+    info->csp        = h->first_pic->img.csp;
+    info->num_frames = h->lavf->streams[i]->nb_frames;
     info->sar_height = c->sample_aspect_ratio.den;
     info->sar_width  = c->sample_aspect_ratio.num;
+    info->fullrange |= c->color_range == AVCOL_RANGE_JPEG;
+
+    /* avisynth stores rgb data vertically flipped. */
+    if( !strcasecmp( get_filename_extension( psz_filename ), "avs" ) &&
+        (c->pix_fmt == AV_PIX_FMT_BGRA || c->pix_fmt == AV_PIX_FMT_BGR24) )
+        info->csp |= X264_CSP_VFLIP;
+
     *p_handle = h;
 
     return 0;
 }
 
-static int picture_alloc( x264_picture_t *pic, int i_csp, int i_width, int i_height )
+static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
 {
-    if( x264_picture_alloc( pic, i_csp, i_width, i_height ) )
-        return -1;
-    lavf_pic_t *pic_h = pic->opaque = malloc( sizeof(lavf_pic_t) );
-    if( !pic_h )
+    if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
         return -1;
-    avcodec_get_frame_defaults( &pic_h->frame );
-    av_init_packet( &pic_h->packet );
-    return 0;
-}
-
-/* FIXME */
-static int get_frame_total( hnd_t handle )
-{
+    pic->img.csp = csp;
+    pic->img.planes = 4;
     return 0;
 }
 
-static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
+static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
 {
-    return read_frame_internal( p_pic, handle, i_frame, NULL );
+    return read_frame_internal( pic, handle, i_frame, NULL );
 }
 
-static void picture_clean( x264_picture_t *pic )
+static void picture_clean( cli_pic_t *pic, hnd_t handle )
 {
-    free( pic->opaque );
-    x264_picture_clean( pic );
+    memset( pic, 0, sizeof(cli_pic_t) );
 }
 
 static int close_file( hnd_t handle )
 {
     lavf_hnd_t *h = handle;
-    sws_freeContext( h->scaler );
     avcodec_close( h->lavf->streams[h->stream_id]->codec );
-    av_close_input_file( h->lavf );
+    avformat_close_input( &h->lavf );
+    av_frame_free( &h->frame );
     free( h );
     return 0;
 }
 
-cli_input_t lavf_input = { open_file, get_frame_total, picture_alloc, read_frame, NULL, picture_clean, close_file };
+const cli_input_t lavf_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };