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