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