1 /*****************************************************************************
2 * timecode.c: x264 timecode format file input module
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.
21 *****************************************************************************/
26 extern cli_input_t input;
33 int auto_timebase_num;
34 int auto_timebase_den;
35 uint64_t timebase_num;
36 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 if( fps_num > UINT32_MAX )
66 fprintf( stderr, "timecode [error]: tcfile fps correction failed.\n"
67 " Specify an appropriate timebase manually or remake tcfile.\n" );
70 if( fabs( ((double)fps_num / fps_den) / exponent - fps_sig ) < DOUBLE_EPSILON )
74 if( h->auto_timebase_den )
76 h->timebase_den = h->timebase_den ? lcm( h->timebase_den, fps_num ) : fps_num;
77 if( h->timebase_den > UINT32_MAX )
78 h->auto_timebase_den = 0;
80 return (double)fps_num / fps_den;
83 static int try_mkv_timebase_den( double *fpss, timecode_hnd_t *h, int loop_num )
86 h->timebase_den = MKV_TIMEBASE_DEN;
87 for( int num = 0; num < loop_num; num++ )
91 double fps_sig = sigexp10( fpss[num], &exponent );
92 fps_den = round( MKV_TIMEBASE_DEN / fps_sig ) / exponent;
93 h->timebase_num = fps_den && h->timebase_num ? gcd( h->timebase_num, fps_den ) : fps_den;
94 if( h->timebase_num > UINT32_MAX || !h->timebase_num )
96 fprintf( stderr, "timecode [error]: automatic timebase generation failed.\n"
97 " Specify timebase manually.\n" );
104 static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info )
107 int ret, tcfv, num, seq_num, timecodes_num;
108 int64_t pts_seek_offset;
109 double *timecodes = NULL;
112 ret = fscanf( tcfile_in, "# timecode format v%d", &tcfv );
113 if( ret != 1 || (tcfv != 1 && tcfv != 2) )
115 fprintf( stderr, "timecode [error]: unsupported timecode format\n" );
122 double assume_fps, seq_fps;
123 int start, end = h->seek;
124 int prev_start = -1, prev_end = -1;
127 for( num = 2; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
129 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
131 if( sscanf( buff, "assume %lf", &h->assume_fps ) != 1 && sscanf( buff, "Assume %lf", &h->assume_fps ) != 1 )
133 fprintf( stderr, "timecode [error]: tcfile parsing error: assumed fps not found\n" );
138 if( h->assume_fps <= 0 )
140 fprintf( stderr, "timecode [error]: invalid assumed fps %.6f\n", h->assume_fps );
144 file_pos = ftell( tcfile_in );
145 h->stored_pts_num = 0;
146 for( seq_num = 0; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
148 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
150 if( sscanf( buff, "# TDecimate Mode 3: Last Frame = %d", &end ) == 1 )
151 h->stored_pts_num = end + 1 - h->seek;
154 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
155 if( ret != 3 && ret != EOF )
157 fprintf( stderr, "timecode [error]: invalid input tcfile\n" );
160 if( start > end || start <= prev_start || end <= prev_end || seq_fps <= 0 )
162 fprintf( stderr, "timecode [error]: invalid input tcfile at line %d: %s\n", num, buff );
167 if( h->auto_timebase_den || h->auto_timebase_num )
170 if( !h->stored_pts_num )
171 h->stored_pts_num = end + 1 - h->seek;
172 timecodes_num = h->stored_pts_num + h->seek;
173 fseek( tcfile_in, file_pos, SEEK_SET );
175 timecodes = malloc( timecodes_num * sizeof(double) );
178 if( h->auto_timebase_den || h->auto_timebase_num )
180 fpss = malloc( (seq_num + 1) * sizeof(double) );
185 assume_fps = correct_fps( h->assume_fps, h );
189 for( num = seq_num = 0; num < timecodes_num - 1; )
191 fgets( buff, sizeof(buff), tcfile_in );
192 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
194 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
196 start = end = timecodes_num - 1;
197 for( ; num < start && num < timecodes_num - 1; num++ )
198 timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
199 if( num < timecodes_num - 1 )
201 if( h->auto_timebase_den || h->auto_timebase_num )
202 fpss[seq_num++] = seq_fps;
203 seq_fps = correct_fps( seq_fps, h );
206 for( num = start; num <= end && num < timecodes_num - 1; num++ )
207 timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
210 if( h->auto_timebase_den || h->auto_timebase_num )
211 fpss[seq_num] = h->assume_fps;
213 if( h->auto_timebase_num && !h->auto_timebase_den )
216 double assume_fps_sig, seq_fps_sig;
217 if( try_mkv_timebase_den( fpss, h, seq_num + 1 ) < 0 )
219 fseek( tcfile_in, file_pos, SEEK_SET );
220 assume_fps_sig = sigexp10( h->assume_fps, &exponent );
221 assume_fps = MKV_TIMEBASE_DEN / ( round( MKV_TIMEBASE_DEN / assume_fps_sig ) / exponent );
222 for( num = 0; num < timecodes_num - 1; )
224 fgets( buff, sizeof(buff), tcfile_in );
225 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
227 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
229 start = end = timecodes_num - 1;
230 seq_fps_sig = sigexp10( seq_fps, &exponent );
231 seq_fps = MKV_TIMEBASE_DEN / ( round( MKV_TIMEBASE_DEN / seq_fps_sig ) / exponent );
232 for( ; num < start && num < timecodes_num - 1; num++ )
233 timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
234 for( num = start; num <= end && num < timecodes_num - 1; num++ )
235 timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
241 h->assume_fps = assume_fps;
242 h->last_timecode = timecodes[timecodes_num - 1];
246 uint64_t file_pos = ftell( tcfile_in );
248 num = h->stored_pts_num = 0;
249 while( fgets( buff, sizeof(buff), tcfile_in ) != NULL )
251 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
254 file_pos = ftell( tcfile_in );
261 timecodes_num = h->stored_pts_num + h->seek;
264 fprintf( stderr, "timecode [error]: input tcfile doesn't have any timecodes!\n" );
267 fseek( tcfile_in, file_pos, SEEK_SET );
269 timecodes = malloc( timecodes_num * sizeof(double) );
273 fgets( buff, sizeof(buff), tcfile_in );
274 ret = sscanf( buff, "%lf", &timecodes[0] );
277 fprintf( stderr, "timecode [error]: invalid input tcfile for frame 0\n" );
280 for( num = 1; num < timecodes_num; )
282 fgets( buff, sizeof(buff), tcfile_in );
283 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
285 ret = sscanf( buff, "%lf", &timecodes[num] );
286 timecodes[num] *= 1e-3; /* Timecode format v2 is expressed in milliseconds. */
287 if( ret != 1 || timecodes[num] <= timecodes[num - 1] )
289 fprintf( stderr, "timecode [error]: invalid input tcfile for frame %d\n", num );
295 if( timecodes_num == 1 )
296 h->timebase_den = info->fps_num;
297 else if( h->auto_timebase_den )
299 fpss = malloc( (timecodes_num - 1) * sizeof(double) );
302 for( num = 0; num < timecodes_num - 1; num++ )
304 fpss[num] = 1 / (timecodes[num + 1] - timecodes[num]);
305 if( h->timebase_den >= 0 )
308 uint64_t fps_num, fps_den;
310 double fps_sig = sigexp10( fpss[num], &exponent );
313 fps_den = i * h->timebase_num;
314 fps_num = round( fps_den * fps_sig ) * exponent;
315 if( fps_num > UINT32_MAX || fabs( ((double)fps_num / fps_den) / exponent - fps_sig ) < DOUBLE_EPSILON )
319 h->timebase_den = fps_num && h->timebase_den ? lcm( h->timebase_den, fps_num ) : fps_num;
320 if( h->timebase_den > UINT32_MAX )
322 h->auto_timebase_den = 0;
327 if( h->auto_timebase_num && !h->auto_timebase_den )
328 if( try_mkv_timebase_den( fpss, h, timecodes_num - 1 ) < 0 )
333 if( timecodes_num > 1 )
334 h->assume_fps = 1 / (timecodes[timecodes_num - 1] - timecodes[timecodes_num - 2]);
336 h->assume_fps = (double)info->fps_num / info->fps_den;
337 h->last_timecode = timecodes[timecodes_num - 1];
340 if( h->auto_timebase_den || h->auto_timebase_num )
342 uint64_t i = gcd( h->timebase_num, h->timebase_den );
343 h->timebase_num /= i;
344 h->timebase_den /= i;
345 fprintf( stderr, "timecode [info]: automatic timebase generation %"PRIu64"/%"PRIu64"\n", h->timebase_num, h->timebase_den );
347 else if( h->timebase_den > UINT32_MAX || !h->timebase_den )
349 fprintf( stderr, "timecode [error]: automatic timebase generation failed.\n"
350 " Specify an appropriate timebase manually.\n" );
354 h->pts = malloc( h->stored_pts_num * sizeof(int64_t) );
357 pts_seek_offset = (int64_t)( timecodes[h->seek] * ((double)h->timebase_den / h->timebase_num) + 0.5 );
359 for( num = 1; num < h->stored_pts_num; num++ )
361 h->pts[num] = (int64_t)( timecodes[h->seek + num] * ((double)h->timebase_den / h->timebase_num) + 0.5 );
362 h->pts[num] -= pts_seek_offset;
363 if( h->pts[num] <= h->pts[num - 1] )
365 fprintf( stderr, "timecode [error]: invalid timebase or timecode for frame %d\n", num );
381 #undef DOUBLE_EPSILON
382 #undef MKV_TIMEBASE_DEN
384 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
388 timecode_hnd_t *h = malloc( sizeof(timecode_hnd_t) );
391 fprintf( stderr, "timecode [error]: malloc failed\n" );
395 h->p_handle = *p_handle;
396 h->frame_total = input.get_frame_total( h->p_handle );
400 ret = sscanf( opt->timebase, "%"SCNu64"/%"SCNu64, &h->timebase_num, &h->timebase_den );
402 h->timebase_num = strtoul( opt->timebase, NULL, 10 );
403 if( h->timebase_num > UINT32_MAX || h->timebase_den > UINT32_MAX )
405 fprintf( stderr, "timecode [error]: timebase you specified exceeds H.264 maximum\n" );
409 h->auto_timebase_num = !ret;
410 h->auto_timebase_den = ret < 2;
411 if( h->auto_timebase_num )
412 h->timebase_num = info->fps_den; /* can be changed later by auto timebase generation */
413 if( h->auto_timebase_den )
414 h->timebase_den = 0; /* set later by auto timebase generation */
415 timecode_input.picture_alloc = h->input.picture_alloc;
416 timecode_input.picture_clean = h->input.picture_clean;
420 tcfile_in = fopen( psz_filename, "rb" );
423 fprintf( stderr, "timecode [error]: can't open `%s'\n", psz_filename );
426 else if( !x264_is_regular_file( tcfile_in ) )
428 fprintf( stderr, "timecode [error]: tcfile input incompatible with non-regular file `%s'\n", psz_filename );
433 if( parse_tcfile( tcfile_in, h, info ) < 0 )
442 info->timebase_num = h->timebase_num;
443 info->timebase_den = h->timebase_den;
449 static int get_frame_total( hnd_t handle )
451 timecode_hnd_t *h = handle;
452 return h->frame_total;
455 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
457 timecode_hnd_t *h = handle;
458 int ret = h->input.read_frame( p_pic, h->p_handle, i_frame );
460 if( i_frame - h->seek < h->stored_pts_num )
462 assert( i_frame >= h->seek );
463 p_pic->i_pts = h->pts[i_frame - h->seek];
469 fprintf( stderr, "timecode [info]: input timecode file missing data for frame %d and later\n"
470 " assuming constant fps %.6f\n", i_frame, h->assume_fps );
474 h->last_timecode += 1 / h->assume_fps;
475 p_pic->i_pts = (int64_t)( h->last_timecode * ((double)h->timebase_den / h->timebase_num) + 0.5 );
481 static int release_frame( x264_picture_t *pic, hnd_t handle )
483 timecode_hnd_t *h = handle;
484 if( h->input.release_frame )
485 return h->input.release_frame( pic, h->p_handle );
489 static int close_file( hnd_t handle )
491 timecode_hnd_t *h = handle;
494 h->input.close_file( h->p_handle );
499 cli_input_t timecode_input = { open_file, get_frame_total, NULL, read_frame, release_frame, NULL, close_file };