]> git.sesse.net Git - x264/blob - input/timecode.c
Disable progress for FFMS input with --no-progress
[x264] / input / timecode.c
1 /*****************************************************************************
2  * timecode.c: timecode file input
3  *****************************************************************************
4  * Copyright (C) 2010-2011 x264 project
5  *
6  * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7  *
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.
12  *
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.
17  *
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  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25
26 #include "input.h"
27 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "timecode", __VA_ARGS__ )
28
29 typedef struct
30 {
31     cli_input_t input;
32     hnd_t p_handle;
33     int auto_timebase_num;
34     int auto_timebase_den;
35     uint64_t timebase_num;
36     uint64_t timebase_den;
37     int stored_pts_num;
38     int64_t *pts;
39     double assume_fps;
40     double last_timecode;
41 } timecode_hnd_t;
42
43 static inline double sigexp10( double value, double *exponent )
44 {
45     /* This function separates significand and exp10 from double floating point. */
46     *exponent = pow( 10, floor( log10( value ) ) );
47     return value / *exponent;
48 }
49
50 #define DOUBLE_EPSILON 5e-6
51 #define MKV_TIMEBASE_DEN 1000000000
52
53 static double correct_fps( double fps, timecode_hnd_t *h )
54 {
55     int i = 1;
56     uint64_t fps_num, fps_den;
57     double exponent;
58     double fps_sig = sigexp10( fps, &exponent );
59     while( 1 )
60     {
61         fps_den = i * h->timebase_num;
62         fps_num = round( fps_den * fps_sig ) * exponent;
63         FAIL_IF_ERROR( fps_num > UINT32_MAX, "tcfile fps correction failed.\n"
64                        "                  Specify an appropriate timebase manually or remake tcfile.\n" )
65         if( fabs( ((double)fps_num / fps_den) / exponent - fps_sig ) < DOUBLE_EPSILON )
66             break;
67         ++i;
68     }
69     if( h->auto_timebase_den )
70     {
71         h->timebase_den = h->timebase_den ? lcm( h->timebase_den, fps_num ) : fps_num;
72         if( h->timebase_den > UINT32_MAX )
73             h->auto_timebase_den = 0;
74     }
75     return (double)fps_num / fps_den;
76 }
77
78 static int try_mkv_timebase_den( double *fpss, timecode_hnd_t *h, int loop_num )
79 {
80     h->timebase_num = 0;
81     h->timebase_den = MKV_TIMEBASE_DEN;
82     for( int num = 0; num < loop_num; num++ )
83     {
84         uint64_t fps_den;
85         double exponent;
86         double fps_sig = sigexp10( fpss[num], &exponent );
87         fps_den = round( MKV_TIMEBASE_DEN / fps_sig ) / exponent;
88         h->timebase_num = fps_den && h->timebase_num ? gcd( h->timebase_num, fps_den ) : fps_den;
89         FAIL_IF_ERROR( h->timebase_num > UINT32_MAX || !h->timebase_num, "automatic timebase generation failed.\n"
90                        "                  Specify timebase manually.\n" )
91     }
92     return 0;
93 }
94
95 static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info )
96 {
97     char buff[256];
98     int ret, tcfv, num, seq_num, timecodes_num;
99     double *timecodes = NULL;
100     double *fpss = NULL;
101
102     ret = fscanf( tcfile_in, "# timecode format v%d", &tcfv );
103     FAIL_IF_ERROR( ret != 1 || (tcfv != 1 && tcfv != 2), "unsupported timecode format\n" )
104
105     if( tcfv == 1 )
106     {
107         uint64_t file_pos;
108         double assume_fps, seq_fps;
109         int start, end;
110         int prev_start = -1, prev_end = -1;
111
112         h->assume_fps = 0;
113         for( num = 2; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
114         {
115             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
116                 continue;
117             FAIL_IF_ERROR( sscanf( buff, "assume %lf", &h->assume_fps ) != 1 && sscanf( buff, "Assume %lf", &h->assume_fps ) != 1,
118                            "tcfile parsing error: assumed fps not found\n" )
119             break;
120         }
121         FAIL_IF_ERROR( h->assume_fps <= 0, "invalid assumed fps %.6f\n", h->assume_fps )
122
123         file_pos = ftell( tcfile_in );
124         h->stored_pts_num = 0;
125         for( seq_num = 0; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
126         {
127             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
128             {
129                 if( sscanf( buff, "# TDecimate Mode 3:  Last Frame = %d", &end ) == 1 )
130                     h->stored_pts_num = end + 1;
131                 continue;
132             }
133             ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
134             FAIL_IF_ERROR( ret != 3 && ret != EOF, "invalid input tcfile\n" )
135             FAIL_IF_ERROR( start > end || start <= prev_start || end <= prev_end || seq_fps <= 0,
136                            "invalid input tcfile at line %d: %s\n", num, buff )
137             prev_start = start;
138             prev_end = end;
139             if( h->auto_timebase_den || h->auto_timebase_num )
140                 ++seq_num;
141         }
142         if( !h->stored_pts_num )
143             h->stored_pts_num = end + 1;
144         timecodes_num = h->stored_pts_num;
145         fseek( tcfile_in, file_pos, SEEK_SET );
146
147         timecodes = malloc( timecodes_num * sizeof(double) );
148         if( !timecodes )
149             return -1;
150         if( h->auto_timebase_den || h->auto_timebase_num )
151         {
152             fpss = malloc( (seq_num + 1) * sizeof(double) );
153             if( !fpss )
154                 goto fail;
155         }
156
157         assume_fps = correct_fps( h->assume_fps, h );
158         if( assume_fps < 0 )
159             goto fail;
160         timecodes[0] = 0;
161         for( num = seq_num = 0; num < timecodes_num - 1; )
162         {
163             fgets( buff, sizeof(buff), tcfile_in );
164             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
165                 continue;
166             ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
167             if( ret != 3 )
168                 start = end = timecodes_num - 1;
169             for( ; num < start && num < timecodes_num - 1; num++ )
170                 timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
171             if( num < timecodes_num - 1 )
172             {
173                 if( h->auto_timebase_den || h->auto_timebase_num )
174                     fpss[seq_num++] = seq_fps;
175                 seq_fps = correct_fps( seq_fps, h );
176                 if( seq_fps < 0 )
177                     goto fail;
178                 for( num = start; num <= end && num < timecodes_num - 1; num++ )
179                     timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
180             }
181         }
182         if( h->auto_timebase_den || h->auto_timebase_num )
183             fpss[seq_num] = h->assume_fps;
184
185         if( h->auto_timebase_num && !h->auto_timebase_den )
186         {
187             double exponent;
188             double assume_fps_sig, seq_fps_sig;
189             if( try_mkv_timebase_den( fpss, h, seq_num + 1 ) < 0 )
190                 goto fail;
191             fseek( tcfile_in, file_pos, SEEK_SET );
192             assume_fps_sig = sigexp10( h->assume_fps, &exponent );
193             assume_fps = MKV_TIMEBASE_DEN / ( round( MKV_TIMEBASE_DEN / assume_fps_sig ) / exponent );
194             for( num = 0; num < timecodes_num - 1; )
195             {
196                 fgets( buff, sizeof(buff), tcfile_in );
197                 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
198                     continue;
199                 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
200                 if( ret != 3 )
201                     start = end = timecodes_num - 1;
202                 seq_fps_sig = sigexp10( seq_fps, &exponent );
203                 seq_fps = MKV_TIMEBASE_DEN / ( round( MKV_TIMEBASE_DEN / seq_fps_sig ) / exponent );
204                 for( ; num < start && num < timecodes_num - 1; num++ )
205                     timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
206                 for( num = start; num <= end && num < timecodes_num - 1; num++ )
207                     timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
208             }
209         }
210         if( fpss )
211         {
212             free( fpss );
213             fpss = NULL;
214         }
215
216         h->assume_fps = assume_fps;
217         h->last_timecode = timecodes[timecodes_num - 1];
218     }
219     else    /* tcfv == 2 */
220     {
221         uint64_t file_pos = ftell( tcfile_in );
222
223         h->stored_pts_num = 0;
224         while( fgets( buff, sizeof(buff), tcfile_in ) != NULL )
225         {
226             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
227             {
228                 if( !h->stored_pts_num )
229                     file_pos = ftell( tcfile_in );
230                 continue;
231             }
232             h->stored_pts_num++;
233         }
234         timecodes_num = h->stored_pts_num;
235         FAIL_IF_ERROR( !timecodes_num, "input tcfile doesn't have any timecodes!\n" )
236         fseek( tcfile_in, file_pos, SEEK_SET );
237
238         timecodes = malloc( timecodes_num * sizeof(double) );
239         if( !timecodes )
240             return -1;
241
242         fgets( buff, sizeof(buff), tcfile_in );
243         ret = sscanf( buff, "%lf", &timecodes[0] );
244         timecodes[0] *= 1e-3;         /* Timecode format v2 is expressed in milliseconds. */
245         FAIL_IF_ERROR( ret != 1, "invalid input tcfile for frame 0\n" )
246         for( num = 1; num < timecodes_num; )
247         {
248             fgets( buff, sizeof(buff), tcfile_in );
249             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
250                 continue;
251             ret = sscanf( buff, "%lf", &timecodes[num] );
252             timecodes[num] *= 1e-3;         /* Timecode format v2 is expressed in milliseconds. */
253             FAIL_IF_ERROR( ret != 1 || timecodes[num] <= timecodes[num - 1],
254                            "invalid input tcfile for frame %d\n", num )
255             ++num;
256         }
257
258         if( timecodes_num == 1 )
259             h->timebase_den = info->fps_num;
260         else if( h->auto_timebase_den )
261         {
262             fpss = malloc( (timecodes_num - 1) * sizeof(double) );
263             if( !fpss )
264                 goto fail;
265             for( num = 0; num < timecodes_num - 1; num++ )
266             {
267                 fpss[num] = 1 / (timecodes[num + 1] - timecodes[num]);
268                 if( h->auto_timebase_den )
269                 {
270                     int i = 1;
271                     uint64_t fps_num, fps_den;
272                     double exponent;
273                     double fps_sig = sigexp10( fpss[num], &exponent );
274                     while( 1 )
275                     {
276                         fps_den = i * h->timebase_num;
277                         fps_num = round( fps_den * fps_sig ) * exponent;
278                         if( fps_num > UINT32_MAX || fabs( ((double)fps_num / fps_den) / exponent - fps_sig ) < DOUBLE_EPSILON )
279                             break;
280                         ++i;
281                     }
282                     h->timebase_den = fps_num && h->timebase_den ? lcm( h->timebase_den, fps_num ) : fps_num;
283                     if( h->timebase_den > UINT32_MAX )
284                     {
285                         h->auto_timebase_den = 0;
286                         continue;
287                     }
288                 }
289             }
290             if( h->auto_timebase_num && !h->auto_timebase_den )
291                 if( try_mkv_timebase_den( fpss, h, timecodes_num - 1 ) < 0 )
292                     goto fail;
293             free( fpss );
294             fpss = NULL;
295         }
296
297         if( timecodes_num > 1 )
298             h->assume_fps = 1 / (timecodes[timecodes_num - 1] - timecodes[timecodes_num - 2]);
299         else
300             h->assume_fps = (double)info->fps_num / info->fps_den;
301         h->last_timecode = timecodes[timecodes_num - 1];
302     }
303
304     if( h->auto_timebase_den || h->auto_timebase_num )
305     {
306         uint64_t i = gcd( h->timebase_num, h->timebase_den );
307         h->timebase_num /= i;
308         h->timebase_den /= i;
309         x264_cli_log( "timecode", X264_LOG_INFO, "automatic timebase generation %"PRIu64"/%"PRIu64"\n", h->timebase_num, h->timebase_den );
310     }
311     else FAIL_IF_ERROR( h->timebase_den > UINT32_MAX || !h->timebase_den, "automatic timebase generation failed.\n"
312                         "                  Specify an appropriate timebase manually.\n" )
313
314     h->pts = malloc( h->stored_pts_num * sizeof(int64_t) );
315     if( !h->pts )
316         goto fail;
317     for( num = 0; num < h->stored_pts_num; num++ )
318     {
319         h->pts[num] = timecodes[num] * ((double)h->timebase_den / h->timebase_num) + 0.5;
320         FAIL_IF_ERROR( num > 0 && h->pts[num] <= h->pts[num - 1], "invalid timebase or timecode for frame %d\n", num )
321     }
322
323     free( timecodes );
324     return 0;
325
326 fail:
327     if( timecodes )
328         free( timecodes );
329     if( fpss )
330         free( fpss );
331     return -1;
332 }
333
334 #undef DOUBLE_EPSILON
335 #undef MKV_TIMEBASE_DEN
336
337 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
338 {
339     int ret = 0;
340     FILE *tcfile_in;
341     timecode_hnd_t *h = malloc( sizeof(timecode_hnd_t) );
342     FAIL_IF_ERROR( !h, "malloc failed\n" )
343     h->input = input;
344     h->p_handle = *p_handle;
345     if( opt->timebase )
346     {
347         ret = sscanf( opt->timebase, "%"SCNu64"/%"SCNu64, &h->timebase_num, &h->timebase_den );
348         if( ret == 1 )
349             h->timebase_num = strtoul( opt->timebase, NULL, 10 );
350         FAIL_IF_ERROR( h->timebase_num > UINT32_MAX || h->timebase_den > UINT32_MAX,
351                        "timebase you specified exceeds H.264 maximum\n" )
352     }
353     h->auto_timebase_num = !ret;
354     h->auto_timebase_den = ret < 2;
355     if( h->auto_timebase_num )
356         h->timebase_num = info->fps_den; /* can be changed later by auto timebase generation */
357     if( h->auto_timebase_den )
358         h->timebase_den = 0;             /* set later by auto timebase generation */
359     timecode_input.picture_alloc = h->input.picture_alloc;
360     timecode_input.picture_clean = h->input.picture_clean;
361
362     *p_handle = h;
363
364     tcfile_in = fopen( psz_filename, "rb" );
365     FAIL_IF_ERROR( !tcfile_in, "can't open `%s'\n", psz_filename )
366     else if( !x264_is_regular_file( tcfile_in ) )
367     {
368         x264_cli_log( "timecode", X264_LOG_ERROR, "tcfile input incompatible with non-regular file `%s'\n", psz_filename );
369         fclose( tcfile_in );
370         return -1;
371     }
372
373     if( parse_tcfile( tcfile_in, h, info ) < 0 )
374     {
375         if( h->pts )
376             free( h->pts );
377         fclose( tcfile_in );
378         return -1;
379     }
380     fclose( tcfile_in );
381
382     info->timebase_num = h->timebase_num;
383     info->timebase_den = h->timebase_den;
384     info->vfr = 1;
385
386     return 0;
387 }
388
389 static int64_t get_frame_pts( timecode_hnd_t *h, int frame, int real_frame )
390 {
391     if( frame < h->stored_pts_num )
392         return h->pts[frame];
393     else
394     {
395         if( h->pts && real_frame )
396         {
397             x264_cli_log( "timecode", X264_LOG_INFO, "input timecode file missing data for frame %d and later\n"
398                           "                 assuming constant fps %.6f\n", frame, h->assume_fps );
399             free( h->pts );
400             h->pts = NULL;
401         }
402         double timecode = h->last_timecode + 1 / h->assume_fps;
403         if( real_frame )
404             h->last_timecode = timecode;
405         return timecode * ((double)h->timebase_den / h->timebase_num) + 0.5;
406     }
407 }
408
409 static int read_frame( cli_pic_t *pic, hnd_t handle, int frame )
410 {
411     timecode_hnd_t *h = handle;
412     if( h->input.read_frame( pic, h->p_handle, frame ) )
413         return -1;
414
415     pic->pts = get_frame_pts( h, frame, 1 );
416     pic->duration = get_frame_pts( h, frame + 1, 0 ) - pic->pts;
417
418     return 0;
419 }
420
421 static int release_frame( cli_pic_t *pic, hnd_t handle )
422 {
423     timecode_hnd_t *h = handle;
424     if( h->input.release_frame )
425         return h->input.release_frame( pic, h->p_handle );
426     return 0;
427 }
428
429 static int close_file( hnd_t handle )
430 {
431     timecode_hnd_t *h = handle;
432     if( h->pts )
433         free( h->pts );
434     h->input.close_file( h->p_handle );
435     free( h );
436     return 0;
437 }
438
439 cli_input_t timecode_input = { open_file, NULL, read_frame, release_frame, NULL, close_file };