1 /*****************************************************************************
2 * ffms.c: ffmpegsource input
3 *****************************************************************************
4 * Copyright (C) 2009-2012 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.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
29 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "ffms", __VA_ARGS__ )
31 #undef DECLARE_ALIGNED
32 #include <libavcodec/avcodec.h>
33 #include <libswscale/swscale.h>
38 #define SetConsoleTitle(t)
43 FFMS_VideoSource *video_source;
51 static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
53 int64_t *update_time = private;
54 int64_t oldtime = *update_time;
55 int64_t newtime = x264_mdate();
56 if( oldtime && newtime - oldtime < UPDATE_INTERVAL )
58 *update_time = newtime;
61 sprintf( buf, "ffms [info]: indexing input file [%.1f%%]", 100.0 * current / total );
62 fprintf( stderr, "%s \r", buf+5 );
63 SetConsoleTitle( buf );
68 /* handle the deprecated jpeg pixel formats */
69 static int handle_jpeg( int csp, int *fullrange )
73 case PIX_FMT_YUVJ420P: *fullrange = 1; return PIX_FMT_YUV420P;
74 case PIX_FMT_YUVJ422P: *fullrange = 1; return PIX_FMT_YUV422P;
75 case PIX_FMT_YUVJ444P: *fullrange = 1; return PIX_FMT_YUV444P;
80 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
82 ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
88 int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
90 FFMS_Index *idx = NULL;
93 struct stat index_s, input_s;
94 if( !stat( opt->index_file, &index_s ) && !stat( psz_filename, &input_s ) &&
95 input_s.st_mtime < index_s.st_mtime )
96 idx = FFMS_ReadIndex( opt->index_file, &e );
102 idx = FFMS_MakeIndex( psz_filename, 0, 0, NULL, NULL, 0, update_progress, &h->time, &e );
103 fprintf( stderr, " \r" );
106 idx = FFMS_MakeIndex( psz_filename, 0, 0, NULL, NULL, 0, NULL, NULL, &e );
107 FAIL_IF_ERROR( !idx, "could not create index\n" )
108 if( opt->index_file && FFMS_WriteIndex( opt->index_file, idx, &e ) )
109 x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
112 int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
113 FAIL_IF_ERROR( trackno < 0, "could not find video track\n" )
115 h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, idx, 1, seekmode, &e );
116 FAIL_IF_ERROR( !h->video_source, "could not create video source\n" )
118 h->track = FFMS_GetTrackFromVideo( h->video_source );
120 FFMS_DestroyIndex( idx );
121 const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
122 info->num_frames = h->num_frames = videop->NumFrames;
123 info->sar_height = videop->SARDen;
124 info->sar_width = videop->SARNum;
125 info->fps_den = videop->FPSDenominator;
126 info->fps_num = videop->FPSNumerator;
127 h->vfr_input = info->vfr;
128 /* ffms is thread unsafe as it uses a single frame buffer for all frame requests */
129 info->thread_safe = 0;
131 const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
132 FAIL_IF_ERROR( !frame, "could not read frame 0\n" )
135 info->width = frame->EncodedWidth;
136 info->height = frame->EncodedHeight;
137 info->csp = handle_jpeg( frame->EncodedPixelFormat, &info->fullrange ) | X264_CSP_OTHER;
138 info->interlaced = frame->InterlacedFrame;
139 info->tff = frame->TopFieldFirst;
140 info->fullrange |= frame->ColorRange == FFMS_CR_JPEG;
142 /* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
143 * so we need to reduce large timebases to prevent overflow */
146 const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
147 int64_t timebase_num = timebase->Num;
148 int64_t timebase_den = timebase->Den * 1000;
151 while( timebase_num > UINT32_MAX || timebase_den > INT32_MAX )
157 info->timebase_num = timebase_num;
158 info->timebase_den = timebase_den;
165 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
167 if( x264_cli_pic_alloc( pic, csp, width, height ) )
173 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
175 ffms_hnd_t *h = handle;
176 if( i_frame >= h->num_frames )
180 const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
181 FAIL_IF_ERROR( !frame, "could not read frame %d \n", i_frame )
183 memcpy( pic->img.stride, frame->Linesize, sizeof(pic->img.stride) );
184 memcpy( pic->img.plane, frame->Data, sizeof(pic->img.plane) );
188 const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
189 FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
190 "Use --force-cfr and specify a framerate with --fps\n" )
192 pic->pts = info->PTS >> h->reduce_pts;
198 static void picture_clean( cli_pic_t *pic )
200 memset( pic, 0, sizeof(cli_pic_t) );
203 static int close_file( hnd_t handle )
205 ffms_hnd_t *h = handle;
206 FFMS_DestroyVideoSource( h->video_source );
211 const cli_input_t ffms_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };