]> git.sesse.net Git - x264/blob - input/ffms.c
Update ffms2 support for its latest API break.
[x264] / input / ffms.c
1 /*****************************************************************************
2  * ffms.c: ffmpegsource input
3  *****************************************************************************
4  * Copyright (C) 2009-2010 x264 project
5  *
6  * Authors: Mike Gurlitz <mike.gurlitz@gmail.com>
7  *          Steven Walters <kemuri9@gmail.com>
8  *
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.
13  *
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.
18  *
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  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26
27 #include "input.h"
28 #include <ffms.h>
29 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "ffms", __VA_ARGS__ )
30
31 #undef DECLARE_ALIGNED
32 #include <libavcodec/avcodec.h>
33 #include <libswscale/swscale.h>
34
35 #ifdef _WIN32
36 #include <windows.h>
37 #else
38 #define SetConsoleTitle(t)
39 #endif
40
41 typedef struct
42 {
43     FFMS_VideoSource *video_source;
44     FFMS_Track *track;
45     int reduce_pts;
46     int vfr_input;
47     int num_frames;
48 } ffms_hnd_t;
49
50 static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
51 {
52     if( current % 10 )
53         return 0;
54     char buf[200];
55     sprintf( buf, "ffms [info]: indexing input file [%.1f%%]", 100.0 * current / total );
56     fprintf( stderr, "%s  \r", buf+5 );
57     SetConsoleTitle( buf );
58     fflush( stderr );
59     return 0;
60 }
61
62 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
63 {
64     ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
65     if( !h )
66         return -1;
67     FFMS_Init( 0, 0 );
68     FFMS_ErrorInfo e;
69     e.BufferSize = 0;
70     int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
71
72     FFMS_Index *idx = NULL;
73     if( opt->index_file )
74     {
75         struct stat index_s, input_s;
76         if( !stat( opt->index_file, &index_s ) && !stat( psz_filename, &input_s ) &&
77             input_s.st_mtime < index_s.st_mtime )
78             idx = FFMS_ReadIndex( opt->index_file, &e );
79     }
80     if( !idx )
81     {
82         idx = FFMS_MakeIndex( psz_filename, 0, 0, NULL, NULL, 0, update_progress, NULL, &e );
83         fprintf( stderr, "                                            \r" );
84         FAIL_IF_ERROR( !idx, "could not create index\n" )
85         if( opt->index_file && FFMS_WriteIndex( opt->index_file, idx, &e ) )
86             x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
87     }
88
89     int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
90     FAIL_IF_ERROR( trackno < 0, "could not find video track\n" )
91
92     h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, idx, 1, seekmode, &e );
93     FAIL_IF_ERROR( !h->video_source, "could not create video source\n" )
94
95     h->track = FFMS_GetTrackFromVideo( h->video_source );
96
97     FFMS_DestroyIndex( idx );
98     const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
99     info->num_frames   = h->num_frames = videop->NumFrames;
100     info->sar_height   = videop->SARDen;
101     info->sar_width    = videop->SARNum;
102     info->fps_den      = videop->FPSDenominator;
103     info->fps_num      = videop->FPSNumerator;
104     h->vfr_input       = info->vfr;
105     /* ffms is thread unsafe as it uses a single frame buffer for all frame requests */
106     info->thread_safe  = 0;
107
108     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
109     FAIL_IF_ERROR( !frame, "could not read frame 0\n" )
110
111     info->width      = frame->EncodedWidth;
112     info->height     = frame->EncodedHeight;
113     info->csp        = frame->EncodedPixelFormat | X264_CSP_OTHER;
114     info->interlaced = frame->InterlacedFrame;
115     info->tff        = frame->TopFieldFirst;
116
117     /* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
118      * so we need to reduce large timebases to prevent overflow */
119     if( h->vfr_input )
120     {
121         const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
122         int64_t timebase_num = timebase->Num;
123         int64_t timebase_den = timebase->Den * 1000;
124         h->reduce_pts = 0;
125
126         while( timebase_num > UINT32_MAX || timebase_den > INT32_MAX )
127         {
128             timebase_num >>= 1;
129             timebase_den >>= 1;
130             h->reduce_pts++;
131         }
132         info->timebase_num = timebase_num;
133         info->timebase_den = timebase_den;
134     }
135
136     *p_handle = h;
137     return 0;
138 }
139
140 static int picture_alloc( cli_pic_t *pic, int csp, int width, int height )
141 {
142     if( x264_cli_pic_alloc( pic, csp, width, height ) )
143         return -1;
144     pic->img.planes = 4;
145     return 0;
146 }
147
148 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
149 {
150     ffms_hnd_t *h = handle;
151     if( i_frame >= h->num_frames )
152         return -1;
153     FFMS_ErrorInfo e;
154     e.BufferSize = 0;
155     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
156     FAIL_IF_ERROR( !frame, "could not read frame %d \n", i_frame )
157
158     memcpy( pic->img.stride, frame->Linesize, sizeof(pic->img.stride) );
159     memcpy( pic->img.plane, frame->Data, sizeof(pic->img.plane) );
160
161     if( h->vfr_input )
162     {
163         const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
164         FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
165                        "Use --force-cfr and specify a framerate with --fps\n" )
166
167         pic->pts = info->PTS >> h->reduce_pts;
168         pic->duration = 0;
169     }
170     return 0;
171 }
172
173 static void picture_clean( cli_pic_t *pic )
174 {
175     memset( pic, 0, sizeof(cli_pic_t) );
176 }
177
178 static int close_file( hnd_t handle )
179 {
180     ffms_hnd_t *h = handle;
181     FFMS_DestroyVideoSource( h->video_source );
182     free( h );
183     return 0;
184 }
185
186 const cli_input_t ffms_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };