1 /*****************************************************************************
2 * ffms.c: x264 ffmpegsource input module
3 *****************************************************************************
4 * Copyright (C) 2009 x264 project
6 * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
7 * Steven Walters <kemuri9@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "ffms", __VA_ARGS__ )
28 #undef DECLARE_ALIGNED
29 #include <libavcodec/avcodec.h>
30 #include <libswscale/swscale.h>
35 #define SetConsoleTitle(t)
40 FFMS_VideoSource *video_source;
43 struct SwsContext *scaler;
57 static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
62 sprintf( buf, "ffms [info]: indexing input file [%.1f%%]", 100.0 * current / total );
63 fprintf( stderr, "%s \r", buf+5 );
64 SetConsoleTitle( buf );
69 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
71 ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
77 int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
79 FFMS_Index *idx = NULL;
82 struct stat index_s, input_s;
83 if( !stat( opt->index_file, &index_s ) && !stat( psz_filename, &input_s ) &&
84 input_s.st_mtime < index_s.st_mtime )
85 idx = FFMS_ReadIndex( opt->index_file, &e );
89 idx = FFMS_MakeIndex( psz_filename, 0, 0, NULL, NULL, 0, update_progress, NULL, &e );
90 fprintf( stderr, " \r" );
91 FAIL_IF_ERROR( !idx, "could not create index\n" )
92 if( opt->index_file && FFMS_WriteIndex( opt->index_file, idx, &e ) )
93 x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
96 int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
97 FAIL_IF_ERROR( trackno < 0, "could not find video track\n" )
99 h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, idx, 1, seekmode, &e );
100 FAIL_IF_ERROR( !h->video_source, "could not create video source\n" )
102 h->track = FFMS_GetTrackFromVideo( h->video_source );
104 FFMS_DestroyIndex( idx );
105 const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
106 h->total_frames = videop->NumFrames;
107 info->sar_height = videop->SARDen;
108 info->sar_width = videop->SARNum;
109 info->fps_den = videop->FPSDenominator;
110 info->fps_num = videop->FPSNumerator;
111 h->vfr_input = info->vfr;
113 const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
114 FAIL_IF_ERROR( !frame, "could not read frame 0\n" )
116 h->init_width = h->cur_width = info->width = frame->EncodedWidth;
117 h->init_height = h->cur_height = info->height = frame->EncodedHeight;
118 h->cur_pix_fmt = frame->EncodedPixelFormat;
119 info->interlaced = frame->InterlacedFrame;
120 info->tff = frame->TopFieldFirst;
122 if( h->cur_pix_fmt != PIX_FMT_YUV420P )
123 x264_cli_log( "ffms", X264_LOG_WARNING, "converting from %s to YV12\n",
124 avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
126 /* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
127 * so we need to reduce large timebases to prevent overflow */
130 const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
131 int64_t timebase_num = timebase->Num;
132 int64_t timebase_den = timebase->Den * 1000;
135 while( timebase_num > UINT32_MAX || timebase_den > INT32_MAX )
141 info->timebase_num = timebase_num;
142 info->timebase_den = timebase_den;
149 static int get_frame_total( hnd_t handle )
151 return ((ffms_hnd_t*)handle)->total_frames;
154 static int check_swscale( ffms_hnd_t *h, const FFMS_Frame *frame, int i_frame )
156 if( h->scaler && h->cur_width == frame->EncodedWidth && h->cur_height == frame->EncodedHeight &&
157 h->cur_pix_fmt == frame->EncodedPixelFormat )
161 sws_freeContext( h->scaler );
162 x264_cli_log( "ffms", X264_LOG_WARNING, "stream properties changed to %dx%d, %s at frame %d \n", frame->EncodedWidth,
163 frame->EncodedHeight, avcodec_get_pix_fmt_name( frame->EncodedPixelFormat ), i_frame );
164 h->cur_width = frame->EncodedWidth;
165 h->cur_height = frame->EncodedHeight;
166 h->cur_pix_fmt = frame->EncodedPixelFormat;
168 h->scaler = sws_getContext( h->cur_width, h->cur_height, h->cur_pix_fmt, h->init_width, h->init_height,
169 PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
170 FAIL_IF_ERROR( !h->scaler, "could not open swscale context\n" )
174 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
176 ffms_hnd_t *h = handle;
179 const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
180 FAIL_IF_ERROR( !frame, "could not read frame %d\n", i_frame )
182 if( check_swscale( h, frame, i_frame ) )
184 /* FFMS_VideoSource has a single FFMS_Frame buffer for all calls to GetFrame.
185 * With threaded input, copying the pointers would result in the data changing during encoding.
186 * FIXME: don't do redundant sws_scales for singlethreaded input, or fix FFMS to allow
187 * multiple FFMS_Frame buffers. */
188 sws_scale( h->scaler, (uint8_t**)frame->Data, (int*)frame->Linesize, 0,
189 frame->EncodedHeight, p_pic->img.plane, p_pic->img.i_stride );
191 const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
195 FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
196 "Use --force-cfr and specify a framerate with --fps\n" )
198 if( !h->pts_offset_flag )
200 h->pts_offset = info->PTS;
201 h->pts_offset_flag = 1;
204 p_pic->i_pts = (info->PTS - h->pts_offset) >> h->reduce_pts;
209 static int close_file( hnd_t handle )
211 ffms_hnd_t *h = handle;
212 sws_freeContext( h->scaler );
213 FFMS_DestroyVideoSource( h->video_source );
218 const cli_input_t ffms_input = { open_file, get_frame_total, x264_picture_alloc, read_frame, NULL, x264_picture_clean, close_file };