]> git.sesse.net Git - x264/blobdiff - input/ffms.c
cli: Use memory-mapped input frames for yuv and y4m
[x264] / input / ffms.c
index b6809671b5cc99613e34e862c7507eec3812e170..42b992c1b221c72559c57b4ad51c64d5a0e9a4e2 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
- * ffms.c: x264 ffmpegsource input module
+ * ffms.c: ffmpegsource input
  *****************************************************************************
- * Copyright (C) 2009 x264 project
+ * Copyright (C) 2009-2015 x264 project
  *
  * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
  *          Steven Walters <kemuri9@gmail.com>
+ *          Henrik Gramner <henrik@gramner.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 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"
 #include <ffms.h>
+#define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "ffms", __VA_ARGS__ )
+
 #undef DECLARE_ALIGNED
 #include <libavcodec/avcodec.h>
 #include <libswscale/swscale.h>
 
 #ifdef _WIN32
 #include <windows.h>
-#else
-#define SetConsoleTitle(t)
 #endif
 
 typedef struct
 {
     FFMS_VideoSource *video_source;
     FFMS_Track *track;
-    int total_frames;
-    struct SwsContext *scaler;
-    int pts_offset_flag;
-    int64_t pts_offset;
     int reduce_pts;
     int vfr_input;
-
-    int init_width;
-    int init_height;
-
-    int cur_width;
-    int cur_height;
-    int cur_pix_fmt;
+    int num_frames;
+    int64_t time;
 } ffms_hnd_t;
 
 static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
 {
-    if( current % 10 )
+    int64_t *update_time = private;
+    int64_t oldtime = *update_time;
+    int64_t newtime = x264_mdate();
+    if( oldtime && newtime - oldtime < UPDATE_INTERVAL )
         return 0;
+    *update_time = newtime;
+
     char buf[200];
     sprintf( buf, "ffms [info]: indexing input file [%.1f%%]", 100.0 * current / total );
     fprintf( stderr, "%s  \r", buf+5 );
-    SetConsoleTitle( buf );
+    x264_cli_set_console_title( buf );
     fflush( stderr );
     return 0;
 }
 
+/* handle the deprecated jpeg pixel formats */
+static int handle_jpeg( int csp, int *fullrange )
+{
+    switch( csp )
+    {
+        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;
+    }
+}
+
 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
 {
     ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
     if( !h )
         return -1;
-    FFMS_Init( 0 );
+
+#ifdef __MINGW32__
+    /* FFMS supports UTF-8 filenames, but it uses std::fstream internally which is broken with Unicode in MinGW. */
+    FFMS_Init( 0, 0 );
+    char src_filename[MAX_PATH];
+    char idx_filename[MAX_PATH];
+    FAIL_IF_ERROR( !x264_ansi_filename( psz_filename, src_filename, MAX_PATH, 0 ), "invalid ansi filename\n" );
+    if( opt->index_file )
+        FAIL_IF_ERROR( !x264_ansi_filename( opt->index_file, idx_filename, MAX_PATH, 1 ), "invalid ansi filename\n" );
+#else
+    FFMS_Init( 0, 1 );
+    char *src_filename = psz_filename;
+    char *idx_filename = opt->index_file;
+#endif
+
     FFMS_ErrorInfo e;
     e.BufferSize = 0;
     int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
 
-    FFMS_Index *index = NULL;
-    if( opt->index )
+    FFMS_Index *idx = NULL;
+    if( opt->index_file )
     {
-        struct stat index_s, input_s;
-        if( !stat( opt->index, &index_s ) && !stat( psz_filename, &input_s ) &&
-            input_s.st_mtime < index_s.st_mtime )
-            index = FFMS_ReadIndex( opt->index, &e );
+        x264_struct_stat index_s, input_s;
+        if( !x264_stat( opt->index_file, &index_s ) && !x264_stat( psz_filename, &input_s ) &&
+            input_s.st_mtime < index_s.st_mtime && index_s.st_size )
+            idx = FFMS_ReadIndex( idx_filename, &e );
     }
-    if( !index )
+    if( !idx )
     {
-        index = FFMS_MakeIndex( psz_filename, 0, 0, NULL, NULL, 0, update_progress, NULL, &e );
-        fprintf( stderr, "                                            \r" );
-        if( !index )
+        if( opt->progress )
         {
-            fprintf( stderr, "ffms [error]: could not create index\n" );
-            return -1;
+            idx = FFMS_MakeIndex( src_filename, 0, 0, NULL, NULL, 0, update_progress, &h->time, &e );
+            fprintf( stderr, "                                            \r" );
         }
-        if( opt->index && FFMS_WriteIndex( opt->index, index, &e ) )
-            fprintf( stderr, "ffms [warning]: could not write index file\n" );
+        else
+            idx = FFMS_MakeIndex( src_filename, 0, 0, NULL, NULL, 0, NULL, NULL, &e );
+        FAIL_IF_ERROR( !idx, "could not create index\n" )
+        if( opt->index_file && FFMS_WriteIndex( idx_filename, idx, &e ) )
+            x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
     }
 
-    int trackno = FFMS_GetFirstTrackOfType( index, FFMS_TYPE_VIDEO, &e );
-    if( trackno < 0 )
-    {
-        fprintf( stderr, "ffms [error]: could not find video track\n" );
-        return -1;
-    }
+    int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
+    FAIL_IF_ERROR( trackno < 0, "could not find video track\n" )
 
-    h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, index, 1, seekmode, &e );
-    if( !h->video_source )
-    {
-        fprintf( stderr, "ffms [error]: could not create video source\n" );
-        return -1;
-    }
+    h->video_source = FFMS_CreateVideoSource( src_filename, trackno, idx, 1, seekmode, &e );
+    FAIL_IF_ERROR( !h->video_source, "could not create video source\n" )
 
     h->track = FFMS_GetTrackFromVideo( h->video_source );
-    const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
 
-    FFMS_DestroyIndex( index );
+    FFMS_DestroyIndex( idx );
     const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
-    h->total_frames    = videop->NumFrames;
+    info->num_frames   = h->num_frames = videop->NumFrames;
     info->sar_height   = videop->SARDen;
     info->sar_width    = videop->SARNum;
     info->fps_den      = videop->FPSDenominator;
     info->fps_num      = videop->FPSNumerator;
-    info->timebase_num = (int)timebase->Num;
     h->vfr_input       = info->vfr;
+    /* ffms is thread unsafe as it uses a single frame buffer for all frame requests */
+    info->thread_safe  = 0;
 
     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
-    if( !frame )
-    {
-        fprintf( stderr, "ffms [error]: could not read frame 0\n" );
-        return -1;
-    }
+    FAIL_IF_ERROR( !frame, "could not read frame 0\n" )
 
-    h->init_width  = h->cur_width  = info->width  = frame->EncodedWidth;
-    h->init_height = h->cur_height = info->height = frame->EncodedHeight;
-    h->cur_pix_fmt = frame->EncodedPixelFormat;
+    info->fullrange  = 0;
+    info->width      = frame->EncodedWidth;
+    info->height     = frame->EncodedHeight;
+    info->csp        = handle_jpeg( frame->EncodedPixelFormat, &info->fullrange ) | X264_CSP_OTHER;
     info->interlaced = frame->InterlacedFrame;
+    info->tff        = frame->TopFieldFirst;
+    info->fullrange |= frame->ColorRange == FFMS_CR_JPEG;
 
-    if( h->cur_pix_fmt != PIX_FMT_YUV420P )
-        fprintf( stderr, "ffms [warning]: converting from %s to YV12\n",
-                 avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
-
-    /* ffms timestamps are in milliseconds. Increasing timebase denominator could cause integer overflow.
-     * Conversely, reducing PTS may lose too much accuracy */
+    /* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
+     * so we need to reduce large timebases to prevent overflow */
     if( h->vfr_input )
     {
-        int64_t timebase_den = (int64_t)timebase->Den * 1000;
+        const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
+        int64_t timebase_num = timebase->Num;
+        int64_t timebase_den = timebase->Den * 1000;
+        h->reduce_pts = 0;
 
-        if( timebase_den > INT_MAX )
+        while( timebase_num > UINT32_MAX || timebase_den > INT32_MAX )
         {
-            info->timebase_den = (int)timebase->Den;
-            h->reduce_pts = 1;
-        }
-        else
-        {
-            info->timebase_den = (int)timebase->Den * 1000;
-            h->reduce_pts = 0;
+            timebase_num >>= 1;
+            timebase_den >>= 1;
+            h->reduce_pts++;
         }
+        info->timebase_num = timebase_num;
+        info->timebase_den = timebase_den;
     }
 
     *p_handle = h;
     return 0;
 }
 
-static int get_frame_total( hnd_t handle )
+static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
 {
-    return ((ffms_hnd_t*)handle)->total_frames;
-}
-
-static int check_swscale( ffms_hnd_t *h, const FFMS_Frame *frame, int i_frame )
-{
-    if( h->scaler && h->cur_width == frame->EncodedWidth && h->cur_height == frame->EncodedHeight &&
-        h->cur_pix_fmt == frame->EncodedPixelFormat )
-        return 0;
-    if( h->scaler )
-    {
-        sws_freeContext( h->scaler );
-        fprintf( stderr, "ffms [warning]: stream properties changed to %dx%d, %s at frame %d  \n", frame->EncodedWidth,
-                 frame->EncodedHeight, avcodec_get_pix_fmt_name( frame->EncodedPixelFormat ), i_frame );
-        h->cur_width   = frame->EncodedWidth;
-        h->cur_height  = frame->EncodedHeight;
-        h->cur_pix_fmt = frame->EncodedPixelFormat;
-    }
-    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, "ffms [error]: could not open swscale context\n" );
+    if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
         return -1;
-    }
+    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 )
 {
     ffms_hnd_t *h = handle;
+    if( i_frame >= h->num_frames )
+        return -1;
     FFMS_ErrorInfo e;
     e.BufferSize = 0;
     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
-    if( !frame )
-    {
-        fprintf( stderr, "ffms [error]: could not read frame %d\n", i_frame );
-        return -1;
-    }
-
-    if( check_swscale( h, frame, i_frame ) )
-        return -1;
-    /* FFMS_VideoSource has a single FFMS_Frame buffer for all calls to GetFrame.
-     * With threaded input, copying the pointers would result in the data changing during encoding.
-     * FIXME: don't do redundant sws_scales for singlethreaded input, or fix FFMS to allow
-     * multiple FFMS_Frame buffers. */
-    sws_scale( h->scaler, (uint8_t**)frame->Data, (int*)frame->Linesize, 0,
-               frame->EncodedHeight, p_pic->img.plane, p_pic->img.i_stride );
+    FAIL_IF_ERROR( !frame, "could not read frame %d \n", i_frame )
 
-    const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
+    memcpy( pic->img.stride, frame->Linesize, sizeof(pic->img.stride) );
+    memcpy( pic->img.plane, frame->Data, sizeof(pic->img.plane) );
 
     if( h->vfr_input )
     {
-        if( info->PTS == AV_NOPTS_VALUE )
-        {
-            fprintf( stderr, "ffms [error]: invalid timestamp. "
-                     "Use --force-cfr and specify a framerate with --fps\n" );
-            return -1;
-        }
+        const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
+        FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
+                       "Use --force-cfr and specify a framerate with --fps\n" )
 
-        if( !h->pts_offset_flag )
-        {
-            h->pts_offset = info->PTS;
-            h->pts_offset_flag = 1;
-        }
-
-        if( h->reduce_pts )
-            p_pic->i_pts = (int64_t)(((info->PTS - h->pts_offset) / 1000) + 0.5);
-        else
-            p_pic->i_pts = info->PTS - h->pts_offset;
+        pic->pts = info->PTS >> h->reduce_pts;
+        pic->duration = 0;
     }
     return 0;
 }
 
+static void picture_clean( cli_pic_t *pic, hnd_t handle )
+{
+    memset( pic, 0, sizeof(cli_pic_t) );
+}
+
 static int close_file( hnd_t handle )
 {
     ffms_hnd_t *h = handle;
-    sws_freeContext( h->scaler );
     FFMS_DestroyVideoSource( h->video_source );
     free( h );
     return 0;
 }
 
-cli_input_t ffms_input = { open_file, get_frame_total, x264_picture_alloc, read_frame, NULL, x264_picture_clean, close_file };
+const cli_input_t ffms_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };