]> git.sesse.net Git - x264/blob - input/ffms.c
Add TFF/BFF detection to all demuxers
[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 "muxers.h"
25 #include <ffms.h>
26 #undef DECLARE_ALIGNED
27 #include <libavcodec/avcodec.h>
28 #include <libswscale/swscale.h>
29
30 #ifdef _WIN32
31 #include <windows.h>
32 #else
33 #define SetConsoleTitle(t)
34 #endif
35
36 typedef struct
37 {
38     FFMS_VideoSource *video_source;
39     FFMS_Track *track;
40     int total_frames;
41     struct SwsContext *scaler;
42     int pts_offset_flag;
43     int64_t pts_offset;
44     int reduce_pts;
45     int vfr_input;
46
47     int init_width;
48     int init_height;
49
50     int cur_width;
51     int cur_height;
52     int cur_pix_fmt;
53 } ffms_hnd_t;
54
55 static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
56 {
57     if( current % 10 )
58         return 0;
59     char buf[200];
60     sprintf( buf, "ffms [info]: indexing input file [%.1f%%]", 100.0 * current / total );
61     fprintf( stderr, "%s  \r", buf+5 );
62     SetConsoleTitle( buf );
63     fflush( stderr );
64     return 0;
65 }
66
67 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
68 {
69     ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
70     if( !h )
71         return -1;
72     FFMS_Init( 0 );
73     FFMS_ErrorInfo e;
74     e.BufferSize = 0;
75     int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
76
77     FFMS_Index *index = NULL;
78     if( opt->index )
79     {
80         struct stat index_s, input_s;
81         if( !stat( opt->index, &index_s ) && !stat( psz_filename, &input_s ) &&
82             input_s.st_mtime < index_s.st_mtime )
83             index = FFMS_ReadIndex( opt->index, &e );
84     }
85     if( !index )
86     {
87         index = FFMS_MakeIndex( psz_filename, 0, 0, NULL, NULL, 0, update_progress, NULL, &e );
88         fprintf( stderr, "                                            \r" );
89         if( !index )
90         {
91             fprintf( stderr, "ffms [error]: could not create index\n" );
92             return -1;
93         }
94         if( opt->index && FFMS_WriteIndex( opt->index, index, &e ) )
95             fprintf( stderr, "ffms [warning]: could not write index file\n" );
96     }
97
98     int trackno = FFMS_GetFirstTrackOfType( index, FFMS_TYPE_VIDEO, &e );
99     if( trackno < 0 )
100     {
101         fprintf( stderr, "ffms [error]: could not find video track\n" );
102         return -1;
103     }
104
105     h->video_source = FFMS_CreateVideoSource( psz_filename, trackno, index, 1, seekmode, &e );
106     if( !h->video_source )
107     {
108         fprintf( stderr, "ffms [error]: could not create video source\n" );
109         return -1;
110     }
111
112     h->track = FFMS_GetTrackFromVideo( h->video_source );
113     const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
114
115     FFMS_DestroyIndex( index );
116     const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
117     h->total_frames    = videop->NumFrames;
118     info->sar_height   = videop->SARDen;
119     info->sar_width    = videop->SARNum;
120     info->fps_den      = videop->FPSDenominator;
121     info->fps_num      = videop->FPSNumerator;
122     info->timebase_num = (int)timebase->Num;
123     h->vfr_input       = info->vfr;
124
125     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
126     if( !frame )
127     {
128         fprintf( stderr, "ffms [error]: could not read frame 0\n" );
129         return -1;
130     }
131
132     h->init_width  = h->cur_width  = info->width  = frame->EncodedWidth;
133     h->init_height = h->cur_height = info->height = frame->EncodedHeight;
134     h->cur_pix_fmt = frame->EncodedPixelFormat;
135     info->interlaced = frame->InterlacedFrame;
136     info->tff        = frame->TopFieldFirst;
137
138     if( h->cur_pix_fmt != PIX_FMT_YUV420P )
139         fprintf( stderr, "ffms [warning]: converting from %s to YV12\n",
140                  avcodec_get_pix_fmt_name( h->cur_pix_fmt ) );
141
142     /* ffms timestamps are in milliseconds. Increasing timebase denominator could cause integer overflow.
143      * Conversely, reducing PTS may lose too much accuracy */
144     if( h->vfr_input )
145     {
146         int64_t timebase_den = (int64_t)timebase->Den * 1000;
147
148         if( timebase_den > INT_MAX )
149         {
150             info->timebase_den = (int)timebase->Den;
151             h->reduce_pts = 1;
152         }
153         else
154         {
155             info->timebase_den = (int)timebase->Den * 1000;
156             h->reduce_pts = 0;
157         }
158     }
159
160     *p_handle = h;
161     return 0;
162 }
163
164 static int get_frame_total( hnd_t handle )
165 {
166     return ((ffms_hnd_t*)handle)->total_frames;
167 }
168
169 static int check_swscale( ffms_hnd_t *h, const FFMS_Frame *frame, int i_frame )
170 {
171     if( h->scaler && h->cur_width == frame->EncodedWidth && h->cur_height == frame->EncodedHeight &&
172         h->cur_pix_fmt == frame->EncodedPixelFormat )
173         return 0;
174     if( h->scaler )
175     {
176         sws_freeContext( h->scaler );
177         fprintf( stderr, "ffms [warning]: stream properties changed to %dx%d, %s at frame %d  \n", frame->EncodedWidth,
178                  frame->EncodedHeight, avcodec_get_pix_fmt_name( frame->EncodedPixelFormat ), i_frame );
179         h->cur_width   = frame->EncodedWidth;
180         h->cur_height  = frame->EncodedHeight;
181         h->cur_pix_fmt = frame->EncodedPixelFormat;
182     }
183     h->scaler = sws_getContext( h->cur_width, h->cur_height, h->cur_pix_fmt, h->init_width, h->init_height,
184                                 PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
185     if( !h->scaler )
186     {
187         fprintf( stderr, "ffms [error]: could not open swscale context\n" );
188         return -1;
189     }
190     return 0;
191 }
192
193 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
194 {
195     ffms_hnd_t *h = handle;
196     FFMS_ErrorInfo e;
197     e.BufferSize = 0;
198     const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
199     if( !frame )
200     {
201         fprintf( stderr, "ffms [error]: could not read frame %d\n", i_frame );
202         return -1;
203     }
204
205     if( check_swscale( h, frame, i_frame ) )
206         return -1;
207     /* FFMS_VideoSource has a single FFMS_Frame buffer for all calls to GetFrame.
208      * With threaded input, copying the pointers would result in the data changing during encoding.
209      * FIXME: don't do redundant sws_scales for singlethreaded input, or fix FFMS to allow
210      * multiple FFMS_Frame buffers. */
211     sws_scale( h->scaler, (uint8_t**)frame->Data, (int*)frame->Linesize, 0,
212                frame->EncodedHeight, p_pic->img.plane, p_pic->img.i_stride );
213
214     const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
215
216     if( h->vfr_input )
217     {
218         if( info->PTS == AV_NOPTS_VALUE )
219         {
220             fprintf( stderr, "ffms [error]: invalid timestamp. "
221                      "Use --force-cfr and specify a framerate with --fps\n" );
222             return -1;
223         }
224
225         if( !h->pts_offset_flag )
226         {
227             h->pts_offset = info->PTS;
228             h->pts_offset_flag = 1;
229         }
230
231         if( h->reduce_pts )
232             p_pic->i_pts = (int64_t)(((info->PTS - h->pts_offset) / 1000) + 0.5);
233         else
234             p_pic->i_pts = info->PTS - h->pts_offset;
235     }
236     return 0;
237 }
238
239 static int close_file( hnd_t handle )
240 {
241     ffms_hnd_t *h = handle;
242     sws_freeContext( h->scaler );
243     FFMS_DestroyVideoSource( h->video_source );
244     free( h );
245     return 0;
246 }
247
248 const cli_input_t ffms_input = { open_file, get_frame_total, x264_picture_alloc, read_frame, NULL, x264_picture_clean, close_file };