1 /*****************************************************************************
2 * timecode.c: timecode file input
3 *****************************************************************************
4 * Copyright (C) 2010 x264 project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
27 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "timecode", __VA_ARGS__ )
34 int auto_timebase_num;
35 int auto_timebase_den;
36 uint64_t timebase_num;
37 uint64_t timebase_den;
44 static inline double sigexp10( double value, double *exponent )
46 /* This function separates significand and exp10 from double floating point. */
47 *exponent = pow( 10, floor( log10( value ) ) );
48 return value / *exponent;
51 #define DOUBLE_EPSILON 5e-6
52 #define MKV_TIMEBASE_DEN 1000000000
54 static double correct_fps( double fps, timecode_hnd_t *h )
57 uint64_t fps_num, fps_den;
59 double fps_sig = sigexp10( fps, &exponent );
62 fps_den = i * h->timebase_num;
63 fps_num = round( fps_den * fps_sig ) * exponent;
64 FAIL_IF_ERROR( fps_num > UINT32_MAX, "tcfile fps correction failed.\n"
65 " Specify an appropriate timebase manually or remake tcfile.\n" )
66 if( fabs( ((double)fps_num / fps_den) / exponent - fps_sig ) < DOUBLE_EPSILON )
70 if( h->auto_timebase_den )
72 h->timebase_den = h->timebase_den ? lcm( h->timebase_den, fps_num ) : fps_num;
73 if( h->timebase_den > UINT32_MAX )
74 h->auto_timebase_den = 0;
76 return (double)fps_num / fps_den;
79 static int try_mkv_timebase_den( double *fpss, timecode_hnd_t *h, int loop_num )
82 h->timebase_den = MKV_TIMEBASE_DEN;
83 for( int num = 0; num < loop_num; num++ )
87 double fps_sig = sigexp10( fpss[num], &exponent );
88 fps_den = round( MKV_TIMEBASE_DEN / fps_sig ) / exponent;
89 h->timebase_num = fps_den && h->timebase_num ? gcd( h->timebase_num, fps_den ) : fps_den;
90 FAIL_IF_ERROR( h->timebase_num > UINT32_MAX || !h->timebase_num, "automatic timebase generation failed.\n"
91 " Specify timebase manually.\n" )
96 static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info )
99 int ret, tcfv, num, seq_num, timecodes_num;
100 double *timecodes = NULL;
103 ret = fscanf( tcfile_in, "# timecode format v%d", &tcfv );
104 FAIL_IF_ERROR( ret != 1 || (tcfv != 1 && tcfv != 2), "unsupported timecode format\n" )
109 double assume_fps, seq_fps;
111 int prev_start = -1, prev_end = -1;
114 for( num = 2; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
116 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
118 FAIL_IF_ERROR( sscanf( buff, "assume %lf", &h->assume_fps ) != 1 && sscanf( buff, "Assume %lf", &h->assume_fps ) != 1,
119 "tcfile parsing error: assumed fps not found\n" )
122 FAIL_IF_ERROR( h->assume_fps <= 0, "invalid assumed fps %.6f\n", h->assume_fps )
124 file_pos = ftell( tcfile_in );
125 h->stored_pts_num = 0;
126 for( seq_num = 0; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
128 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
130 if( sscanf( buff, "# TDecimate Mode 3: Last Frame = %d", &end ) == 1 )
131 h->stored_pts_num = end + 1;
134 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
135 FAIL_IF_ERROR( ret != 3 && ret != EOF, "invalid input tcfile\n" )
136 FAIL_IF_ERROR( start > end || start <= prev_start || end <= prev_end || seq_fps <= 0,
137 "invalid input tcfile at line %d: %s\n", num, buff )
140 if( h->auto_timebase_den || h->auto_timebase_num )
143 if( !h->stored_pts_num )
144 h->stored_pts_num = end + 1;
145 timecodes_num = h->stored_pts_num;
146 fseek( tcfile_in, file_pos, SEEK_SET );
148 timecodes = malloc( timecodes_num * sizeof(double) );
151 if( h->auto_timebase_den || h->auto_timebase_num )
153 fpss = malloc( (seq_num + 1) * sizeof(double) );
158 assume_fps = correct_fps( h->assume_fps, h );
162 for( num = seq_num = 0; num < timecodes_num - 1; )
164 fgets( buff, sizeof(buff), tcfile_in );
165 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
167 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
169 start = end = timecodes_num - 1;
170 for( ; num < start && num < timecodes_num - 1; num++ )
171 timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
172 if( num < timecodes_num - 1 )
174 if( h->auto_timebase_den || h->auto_timebase_num )
175 fpss[seq_num++] = seq_fps;
176 seq_fps = correct_fps( seq_fps, h );
179 for( num = start; num <= end && num < timecodes_num - 1; num++ )
180 timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
183 if( h->auto_timebase_den || h->auto_timebase_num )
184 fpss[seq_num] = h->assume_fps;
186 if( h->auto_timebase_num && !h->auto_timebase_den )
189 double assume_fps_sig, seq_fps_sig;
190 if( try_mkv_timebase_den( fpss, h, seq_num + 1 ) < 0 )
192 fseek( tcfile_in, file_pos, SEEK_SET );
193 assume_fps_sig = sigexp10( h->assume_fps, &exponent );
194 assume_fps = MKV_TIMEBASE_DEN / ( round( MKV_TIMEBASE_DEN / assume_fps_sig ) / exponent );
195 for( num = 0; num < timecodes_num - 1; )
197 fgets( buff, sizeof(buff), tcfile_in );
198 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
200 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
202 start = end = timecodes_num - 1;
203 seq_fps_sig = sigexp10( seq_fps, &exponent );
204 seq_fps = MKV_TIMEBASE_DEN / ( round( MKV_TIMEBASE_DEN / seq_fps_sig ) / exponent );
205 for( ; num < start && num < timecodes_num - 1; num++ )
206 timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
207 for( num = start; num <= end && num < timecodes_num - 1; num++ )
208 timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
217 h->assume_fps = assume_fps;
218 h->last_timecode = timecodes[timecodes_num - 1];
222 uint64_t file_pos = ftell( tcfile_in );
224 h->stored_pts_num = 0;
225 while( fgets( buff, sizeof(buff), tcfile_in ) != NULL )
227 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
229 if( !h->stored_pts_num )
230 file_pos = ftell( tcfile_in );
235 timecodes_num = h->stored_pts_num;
236 FAIL_IF_ERROR( !timecodes_num, "input tcfile doesn't have any timecodes!\n" )
237 fseek( tcfile_in, file_pos, SEEK_SET );
239 timecodes = malloc( timecodes_num * sizeof(double) );
243 fgets( buff, sizeof(buff), tcfile_in );
244 ret = sscanf( buff, "%lf", &timecodes[0] );
245 timecodes[0] *= 1e-3; /* Timecode format v2 is expressed in milliseconds. */
246 FAIL_IF_ERROR( ret != 1, "invalid input tcfile for frame 0\n" )
247 for( num = 1; num < timecodes_num; )
249 fgets( buff, sizeof(buff), tcfile_in );
250 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
252 ret = sscanf( buff, "%lf", &timecodes[num] );
253 timecodes[num] *= 1e-3; /* Timecode format v2 is expressed in milliseconds. */
254 FAIL_IF_ERROR( ret != 1 || timecodes[num] <= timecodes[num - 1],
255 "invalid input tcfile for frame %d\n", num )
259 if( timecodes_num == 1 )
260 h->timebase_den = info->fps_num;
261 else if( h->auto_timebase_den )
263 fpss = malloc( (timecodes_num - 1) * sizeof(double) );
266 for( num = 0; num < timecodes_num - 1; num++ )
268 fpss[num] = 1 / (timecodes[num + 1] - timecodes[num]);
269 if( h->auto_timebase_den )
272 uint64_t fps_num, fps_den;
274 double fps_sig = sigexp10( fpss[num], &exponent );
277 fps_den = i * h->timebase_num;
278 fps_num = round( fps_den * fps_sig ) * exponent;
279 if( fps_num > UINT32_MAX || fabs( ((double)fps_num / fps_den) / exponent - fps_sig ) < DOUBLE_EPSILON )
283 h->timebase_den = fps_num && h->timebase_den ? lcm( h->timebase_den, fps_num ) : fps_num;
284 if( h->timebase_den > UINT32_MAX )
286 h->auto_timebase_den = 0;
291 if( h->auto_timebase_num && !h->auto_timebase_den )
292 if( try_mkv_timebase_den( fpss, h, timecodes_num - 1 ) < 0 )
298 if( timecodes_num > 1 )
299 h->assume_fps = 1 / (timecodes[timecodes_num - 1] - timecodes[timecodes_num - 2]);
301 h->assume_fps = (double)info->fps_num / info->fps_den;
302 h->last_timecode = timecodes[timecodes_num - 1];
305 if( h->auto_timebase_den || h->auto_timebase_num )
307 uint64_t i = gcd( h->timebase_num, h->timebase_den );
308 h->timebase_num /= i;
309 h->timebase_den /= i;
310 x264_cli_log( "timecode", X264_LOG_INFO, "automatic timebase generation %"PRIu64"/%"PRIu64"\n", h->timebase_num, h->timebase_den );
312 else FAIL_IF_ERROR( h->timebase_den > UINT32_MAX || !h->timebase_den, "automatic timebase generation failed.\n"
313 " Specify an appropriate timebase manually.\n" )
315 h->pts = malloc( h->stored_pts_num * sizeof(int64_t) );
318 for( num = 0; num < h->stored_pts_num; num++ )
320 h->pts[num] = timecodes[num] * ((double)h->timebase_den / h->timebase_num) + 0.5;
321 FAIL_IF_ERROR( num > 0 && h->pts[num] <= h->pts[num - 1], "invalid timebase or timecode for frame %d\n", num )
335 #undef DOUBLE_EPSILON
336 #undef MKV_TIMEBASE_DEN
338 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
342 timecode_hnd_t *h = malloc( sizeof(timecode_hnd_t) );
343 FAIL_IF_ERROR( !h, "malloc failed\n" )
345 h->p_handle = *p_handle;
348 ret = sscanf( opt->timebase, "%"SCNu64"/%"SCNu64, &h->timebase_num, &h->timebase_den );
350 h->timebase_num = strtoul( opt->timebase, NULL, 10 );
351 FAIL_IF_ERROR( h->timebase_num > UINT32_MAX || h->timebase_den > UINT32_MAX,
352 "timebase you specified exceeds H.264 maximum\n" )
354 h->auto_timebase_num = !ret;
355 h->auto_timebase_den = ret < 2;
356 if( h->auto_timebase_num )
357 h->timebase_num = info->fps_den; /* can be changed later by auto timebase generation */
358 if( h->auto_timebase_den )
359 h->timebase_den = 0; /* set later by auto timebase generation */
360 timecode_input.picture_alloc = h->input.picture_alloc;
361 timecode_input.picture_clean = h->input.picture_clean;
365 tcfile_in = fopen( psz_filename, "rb" );
366 FAIL_IF_ERROR( !tcfile_in, "can't open `%s'\n", psz_filename )
367 else if( !x264_is_regular_file( tcfile_in ) )
369 x264_cli_log( "timecode", X264_LOG_ERROR, "tcfile input incompatible with non-regular file `%s'\n", psz_filename );
374 if( parse_tcfile( tcfile_in, h, info ) < 0 )
383 info->timebase_num = h->timebase_num;
384 info->timebase_den = h->timebase_den;
390 static int64_t get_frame_pts( timecode_hnd_t *h, int frame, int real_frame )
392 if( frame < h->stored_pts_num )
393 return h->pts[frame];
396 if( h->pts && real_frame )
398 x264_cli_log( "timecode", X264_LOG_INFO, "input timecode file missing data for frame %d and later\n"
399 " assuming constant fps %.6f\n", frame, h->assume_fps );
403 double timecode = h->last_timecode + 1 / h->assume_fps;
405 h->last_timecode = timecode;
406 return timecode * ((double)h->timebase_den / h->timebase_num) + 0.5;
410 static int read_frame( cli_pic_t *pic, hnd_t handle, int frame )
412 timecode_hnd_t *h = handle;
413 int ret = h->input.read_frame( pic, h->p_handle, frame );
415 pic->pts = get_frame_pts( h, frame, 1 );
416 pic->duration = get_frame_pts( h, frame + 1, 0 ) - pic->pts;
421 static int release_frame( cli_pic_t *pic, hnd_t handle )
423 timecode_hnd_t *h = handle;
424 if( h->input.release_frame )
425 return h->input.release_frame( pic, h->p_handle );
429 static int close_file( hnd_t handle )
431 timecode_hnd_t *h = handle;
434 h->input.close_file( h->p_handle );
439 cli_input_t timecode_input = { open_file, NULL, read_frame, release_frame, NULL, close_file };