]> git.sesse.net Git - x264/blob - input/ffms.c
Add speedcontrol file.
[x264] / input / ffms.c
1 /*****************************************************************************
2  * ffms.c: ffmpegsource input
3  *****************************************************************************
4  * Copyright (C) 2009-2016 x264 project
5  *
6  * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
7  *          Steven Walters <kemuri9@gmail.com>
8  *          Henrik Gramner <henrik@gramner.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "input.h"
29 #include <ffms.h>
30 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "ffms", __VA_ARGS__ )
31
32 #undef DECLARE_ALIGNED
33 #include <libavcodec/avcodec.h>
34 #include <libswscale/swscale.h>
35
36 #ifdef _WIN32
37 #include <windows.h>
38 #endif
39
40 #define PROGRESS_LENGTH 36
41
42 typedef struct
43 {
44     FFMS_VideoSource *video_source;
45     FFMS_Track *track;
46     int reduce_pts;
47     int vfr_input;
48     int num_frames;
49     int64_t time;
50 } ffms_hnd_t;
51
52 static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
53 {
54     int64_t *update_time = private;
55     int64_t oldtime = *update_time;
56     int64_t newtime = x264_mdate();
57     if( oldtime && newtime - oldtime < UPDATE_INTERVAL )
58         return 0;
59     *update_time = newtime;
60
61     char buf[PROGRESS_LENGTH+5+1];
62     snprintf( buf, sizeof(buf), "ffms [info]: indexing input file [%.1f%%]", 100.0 * current / total );
63     fprintf( stderr, "%-*s\r", PROGRESS_LENGTH, buf+5 );
64     x264_cli_set_console_title( buf );
65     fflush( stderr );
66     return 0;
67 }
68
69 /* handle the deprecated jpeg pixel formats */
70 static int handle_jpeg( int csp, int *fullrange )
71 {
72     switch( csp )
73     {
74         case AV_PIX_FMT_YUVJ420P: *fullrange = 1; return AV_PIX_FMT_YUV420P;
75         case AV_PIX_FMT_YUVJ422P: *fullrange = 1; return AV_PIX_FMT_YUV422P;
76         case AV_PIX_FMT_YUVJ444P: *fullrange = 1; return AV_PIX_FMT_YUV444P;
77         default:                               return csp;
78     }
79 }
80
81 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
82 {
83     ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
84     if( !h )
85         return -1;
86
87     FFMS_Init( 0, 1 );
88     FFMS_ErrorInfo e;
89     e.BufferSize = 0;
90     int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
91
92     FFMS_Index *idx = NULL;
93     if( opt->index_file )
94     {
95         x264_struct_stat index_s, input_s;
96         if( !x264_stat( opt->index_file, &index_s ) && !x264_stat( psz_filename, &input_s ) && input_s.st_mtime < index_s.st_mtime )
97         {
98             idx = FFMS_ReadIndex( opt->index_file, &e );
99             if( idx && FFMS_IndexBelongsToFile( idx, psz_filename, &e ) )
100             {
101                 FFMS_DestroyIndex( idx );
102                 idx = NULL;
103             }
104         }
105     }
106     if( !idx )
107     {
108         FFMS_Indexer *indexer = FFMS_CreateIndexer( psz_filename, &e );
109         FAIL_IF_ERROR( !indexer, "could not create indexer\n" )
110
111         if( opt->progress )
112             FFMS_SetProgressCallback( indexer, update_progress, &h->time );
113
114         idx = FFMS_DoIndexing2( indexer, FFMS_IEH_ABORT, &e );
115         fprintf( stderr, "%*c", PROGRESS_LENGTH+1, '\r' );
116         FAIL_IF_ERROR( !idx, "could not create index\n" )
117
118         if( opt->index_file && FFMS_WriteIndex( opt->index_file, idx, &e ) )
119             x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
120     }
121
122     int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
123     if( trackno >= 0 )
124         h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, idx, 1, seekmode, &e );
125     FFMS_DestroyIndex( idx );
126
127     FAIL_IF_ERROR( trackno < 0, "could not find video track\n" )
128     FAIL_IF_ERROR( !h->video_source, "could not create video source\n" )
129
130     const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
131     info->num_frames   = h->num_frames = videop->NumFrames;
132     info->sar_height   = videop->SARDen;
133     info->sar_width    = videop->SARNum;
134     info->fps_den      = videop->FPSDenominator;
135     info->fps_num      = videop->FPSNumerator;
136     h->vfr_input       = info->vfr;
137     /* ffms is thread unsafe as it uses a single frame buffer for all frame requests */
138     info->thread_safe  = 0;
139
140     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
141     FAIL_IF_ERROR( !frame, "could not read frame 0\n" )
142
143     info->fullrange  = 0;
144     info->width      = frame->EncodedWidth;
145     info->height     = frame->EncodedHeight;
146     info->csp        = handle_jpeg( frame->EncodedPixelFormat, &info->fullrange ) | X264_CSP_OTHER;
147     info->interlaced = frame->InterlacedFrame;
148     info->tff        = frame->TopFieldFirst;
149     info->fullrange |= frame->ColorRange == FFMS_CR_JPEG;
150
151     /* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
152      * so we need to reduce large timebases to prevent overflow */
153     if( h->vfr_input )
154     {
155         h->track = FFMS_GetTrackFromVideo( h->video_source );
156         const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
157         int64_t timebase_num = timebase->Num;
158         int64_t timebase_den = timebase->Den * 1000;
159         h->reduce_pts = 0;
160
161         while( timebase_num > UINT32_MAX || timebase_den > INT32_MAX )
162         {
163             timebase_num >>= 1;
164             timebase_den >>= 1;
165             h->reduce_pts++;
166         }
167         info->timebase_num = timebase_num;
168         info->timebase_den = timebase_den;
169     }
170
171     *p_handle = h;
172     return 0;
173 }
174
175 static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
176 {
177     if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
178         return -1;
179     pic->img.csp = csp;
180     pic->img.planes = 4;
181     return 0;
182 }
183
184 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
185 {
186     ffms_hnd_t *h = handle;
187     if( i_frame >= h->num_frames )
188         return -1;
189     FFMS_ErrorInfo e;
190     e.BufferSize = 0;
191     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
192     FAIL_IF_ERROR( !frame, "could not read frame %d \n", i_frame )
193
194     memcpy( pic->img.stride, frame->Linesize, sizeof(pic->img.stride) );
195     memcpy( pic->img.plane, frame->Data, sizeof(pic->img.plane) );
196
197     if( h->vfr_input )
198     {
199         const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
200         FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
201                        "Use --force-cfr and specify a framerate with --fps\n" )
202
203         pic->pts = info->PTS >> h->reduce_pts;
204         pic->duration = 0;
205     }
206     return 0;
207 }
208
209 static void picture_clean( cli_pic_t *pic, hnd_t handle )
210 {
211     memset( pic, 0, sizeof(cli_pic_t) );
212 }
213
214 static int close_file( hnd_t handle )
215 {
216     ffms_hnd_t *h = handle;
217     FFMS_DestroyVideoSource( h->video_source );
218     free( h );
219     return 0;
220 }
221
222 const cli_input_t ffms_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };