Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52866 views
1
/*****************************************************************************
2
* ffms.c: ffmpegsource input
3
*****************************************************************************
4
* Copyright (C) 2009-2016 x264 project
5
*
6
* Authors: Mike Gurlitz <[email protected]>
7
* Steven Walters <[email protected]>
8
* Henrik Gramner <[email protected]>
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 [email protected].
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
typedef struct
41
{
42
FFMS_VideoSource *video_source;
43
FFMS_Track *track;
44
int reduce_pts;
45
int vfr_input;
46
int num_frames;
47
int64_t time;
48
} ffms_hnd_t;
49
50
static int FFMS_CC update_progress( int64_t current, int64_t total, void *private )
51
{
52
int64_t *update_time = private;
53
int64_t oldtime = *update_time;
54
int64_t newtime = x264_mdate();
55
if( oldtime && newtime - oldtime < UPDATE_INTERVAL )
56
return 0;
57
*update_time = newtime;
58
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
x264_cli_set_console_title( buf );
63
fflush( stderr );
64
return 0;
65
}
66
67
/* handle the deprecated jpeg pixel formats */
68
static int handle_jpeg( int csp, int *fullrange )
69
{
70
switch( csp )
71
{
72
case AV_PIX_FMT_YUVJ420P: *fullrange = 1; return AV_PIX_FMT_YUV420P;
73
case AV_PIX_FMT_YUVJ422P: *fullrange = 1; return AV_PIX_FMT_YUV422P;
74
case AV_PIX_FMT_YUVJ444P: *fullrange = 1; return AV_PIX_FMT_YUV444P;
75
default: return csp;
76
}
77
}
78
79
static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
80
{
81
ffms_hnd_t *h = calloc( 1, sizeof(ffms_hnd_t) );
82
if( !h )
83
return -1;
84
85
#ifdef __MINGW32__
86
/* FFMS supports UTF-8 filenames, but it uses std::fstream internally which is broken with Unicode in MinGW. */
87
FFMS_Init( 0, 0 );
88
char src_filename[MAX_PATH];
89
char idx_filename[MAX_PATH];
90
FAIL_IF_ERROR( !x264_ansi_filename( psz_filename, src_filename, MAX_PATH, 0 ), "invalid ansi filename\n" );
91
if( opt->index_file )
92
FAIL_IF_ERROR( !x264_ansi_filename( opt->index_file, idx_filename, MAX_PATH, 1 ), "invalid ansi filename\n" );
93
#else
94
FFMS_Init( 0, 1 );
95
char *src_filename = psz_filename;
96
char *idx_filename = opt->index_file;
97
#endif
98
99
FFMS_ErrorInfo e;
100
e.BufferSize = 0;
101
int seekmode = opt->seek ? FFMS_SEEK_NORMAL : FFMS_SEEK_LINEAR_NO_RW;
102
103
FFMS_Index *idx = NULL;
104
if( opt->index_file )
105
{
106
x264_struct_stat index_s, input_s;
107
if( !x264_stat( opt->index_file, &index_s ) && !x264_stat( psz_filename, &input_s ) &&
108
input_s.st_mtime < index_s.st_mtime && index_s.st_size )
109
idx = FFMS_ReadIndex( idx_filename, &e );
110
}
111
if( !idx )
112
{
113
if( opt->progress )
114
{
115
idx = FFMS_MakeIndex( src_filename, 0, 0, NULL, NULL, 0, update_progress, &h->time, &e );
116
fprintf( stderr, " \r" );
117
}
118
else
119
idx = FFMS_MakeIndex( src_filename, 0, 0, NULL, NULL, 0, NULL, NULL, &e );
120
FAIL_IF_ERROR( !idx, "could not create index\n" )
121
if( opt->index_file && FFMS_WriteIndex( idx_filename, idx, &e ) )
122
x264_cli_log( "ffms", X264_LOG_WARNING, "could not write index file\n" );
123
}
124
125
int trackno = FFMS_GetFirstTrackOfType( idx, FFMS_TYPE_VIDEO, &e );
126
FAIL_IF_ERROR( trackno < 0, "could not find video track\n" )
127
128
h->video_source = FFMS_CreateVideoSource( src_filename, trackno, idx, 1, seekmode, &e );
129
FAIL_IF_ERROR( !h->video_source, "could not create video source\n" )
130
131
h->track = FFMS_GetTrackFromVideo( h->video_source );
132
133
FFMS_DestroyIndex( idx );
134
const FFMS_VideoProperties *videop = FFMS_GetVideoProperties( h->video_source );
135
info->num_frames = h->num_frames = videop->NumFrames;
136
info->sar_height = videop->SARDen;
137
info->sar_width = videop->SARNum;
138
info->fps_den = videop->FPSDenominator;
139
info->fps_num = videop->FPSNumerator;
140
h->vfr_input = info->vfr;
141
/* ffms is thread unsafe as it uses a single frame buffer for all frame requests */
142
info->thread_safe = 0;
143
144
const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, 0, &e );
145
FAIL_IF_ERROR( !frame, "could not read frame 0\n" )
146
147
info->fullrange = 0;
148
info->width = frame->EncodedWidth;
149
info->height = frame->EncodedHeight;
150
info->csp = handle_jpeg( frame->EncodedPixelFormat, &info->fullrange ) | X264_CSP_OTHER;
151
info->interlaced = frame->InterlacedFrame;
152
info->tff = frame->TopFieldFirst;
153
info->fullrange |= frame->ColorRange == FFMS_CR_JPEG;
154
155
/* ffms timestamps are in milliseconds. ffms also uses int64_ts for timebase,
156
* so we need to reduce large timebases to prevent overflow */
157
if( h->vfr_input )
158
{
159
const FFMS_TrackTimeBase *timebase = FFMS_GetTimeBase( h->track );
160
int64_t timebase_num = timebase->Num;
161
int64_t timebase_den = timebase->Den * 1000;
162
h->reduce_pts = 0;
163
164
while( timebase_num > UINT32_MAX || timebase_den > INT32_MAX )
165
{
166
timebase_num >>= 1;
167
timebase_den >>= 1;
168
h->reduce_pts++;
169
}
170
info->timebase_num = timebase_num;
171
info->timebase_den = timebase_den;
172
}
173
174
*p_handle = h;
175
return 0;
176
}
177
178
static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
179
{
180
if( x264_cli_pic_alloc( pic, X264_CSP_NONE, width, height ) )
181
return -1;
182
pic->img.csp = csp;
183
pic->img.planes = 4;
184
return 0;
185
}
186
187
static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
188
{
189
ffms_hnd_t *h = handle;
190
if( i_frame >= h->num_frames )
191
return -1;
192
FFMS_ErrorInfo e;
193
e.BufferSize = 0;
194
const FFMS_Frame *frame = FFMS_GetFrame( h->video_source, i_frame, &e );
195
FAIL_IF_ERROR( !frame, "could not read frame %d \n", i_frame )
196
197
memcpy( pic->img.stride, frame->Linesize, sizeof(pic->img.stride) );
198
memcpy( pic->img.plane, frame->Data, sizeof(pic->img.plane) );
199
200
if( h->vfr_input )
201
{
202
const FFMS_FrameInfo *info = FFMS_GetFrameInfo( h->track, i_frame );
203
FAIL_IF_ERROR( info->PTS == AV_NOPTS_VALUE, "invalid timestamp. "
204
"Use --force-cfr and specify a framerate with --fps\n" )
205
206
pic->pts = info->PTS >> h->reduce_pts;
207
pic->duration = 0;
208
}
209
return 0;
210
}
211
212
static void picture_clean( cli_pic_t *pic, hnd_t handle )
213
{
214
memset( pic, 0, sizeof(cli_pic_t) );
215
}
216
217
static int close_file( hnd_t handle )
218
{
219
ffms_hnd_t *h = handle;
220
FFMS_DestroyVideoSource( h->video_source );
221
free( h );
222
return 0;
223
}
224
225
const cli_input_t ffms_input = { open_file, picture_alloc, read_frame, NULL, picture_clean, close_file };
226
227