Bump dates to 2011
[x262.git] / 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 #include <math.h>
29
30 typedef struct
31 {
32     cli_input_t input;
33     hnd_t p_handle;
34     int auto_timebase_num;
35     int auto_timebase_den;
36     uint64_t timebase_num;
37     uint64_t timebase_den;
38     int stored_pts_num;
39     int64_t *pts;
40     double assume_fps;
41     double last_timecode;
42 } timecode_hnd_t;
43
44 static inline double sigexp10( double value, double *exponent )
45 {
46     /* This function separates significand and exp10 from double floating point. */
47     *exponent = pow( 10, floor( log10( value ) ) );
48     return value / *exponent;
49 }
50
51 #define DOUBLE_EPSILON 5e-6
52 #define MKV_TIMEBASE_DEN 1000000000
53
54 static double correct_fps( double fps, timecode_hnd_t *h )
55 {
56     int i = 1;
57     uint64_t fps_num, fps_den;
58     double exponent;
59     double fps_sig = sigexp10( fps, &exponent );
60     while( 1 )
61     {
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 )
67             break;
68         ++i;
69     }
70     if( h->auto_timebase_den )
71     {
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;
75     }
76     return (double)fps_num / fps_den;
77 }
78
79 static int try_mkv_timebase_den( double *fpss, timecode_hnd_t *h, int loop_num )
80 {
81     h->timebase_num = 0;
82     h->timebase_den = MKV_TIMEBASE_DEN;
83     for( int num = 0; num < loop_num; num++ )
84     {
85         uint64_t fps_den;
86         double exponent;
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" )
92     }
93     return 0;
94 }
95
96 static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info )
97 {
98     char buff[256];
99     int ret, tcfv, num, seq_num, timecodes_num;
100     double *timecodes = NULL;
101     double *fpss = NULL;
102
103     ret = fscanf( tcfile_in, "# timecode format v%d", &tcfv );
104     FAIL_IF_ERROR( ret != 1 || (tcfv != 1 && tcfv != 2), "unsupported timecode format\n" )
105
106     if( tcfv == 1 )
107     {
108         uint64_t file_pos;
109         double assume_fps, seq_fps;
110         int start, end;
111         int prev_start = -1, prev_end = -1;
112
113         h->assume_fps = 0;
114         for( num = 2; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
115         {
116             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
117                 continue;
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" )
120             break;
121         }
122         FAIL_IF_ERROR( h->assume_fps <= 0, "invalid assumed fps %.6f\n", h->assume_fps )
123
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++ )
127         {
128             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
129             {
130                 if( sscanf( buff, "# TDecimate Mode 3:  Last Frame = %d", &end ) == 1 )
131                     h->stored_pts_num = end + 1;
132                 continue;
133             }
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 )
138             prev_start = start;
139             prev_end = end;
140             if( h->auto_timebase_den || h->auto_timebase_num )
141                 ++seq_num;
142         }
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 );
147
148         timecodes = malloc( timecodes_num * sizeof(double) );
149         if( !timecodes )
150             return -1;
151         if( h->auto_timebase_den || h->auto_timebase_num )
152         {
153             fpss = malloc( (seq_num + 1) * sizeof(double) );
154             if( !fpss )
155                 goto fail;
156         }
157
158         assume_fps = correct_fps( h->assume_fps, h );
159         if( assume_fps < 0 )
160             goto fail;
161         timecodes[0] = 0;
162         for( num = seq_num = 0; num < timecodes_num - 1; )
163         {
164             fgets( buff, sizeof(buff), tcfile_in );
165             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
166                 continue;
167             ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
168             if( ret != 3 )
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 )
173             {
174                 if( h->auto_timebase_den || h->auto_timebase_num )
175                     fpss[seq_num++] = seq_fps;
176                 seq_fps = correct_fps( seq_fps, h );
177                 if( seq_fps < 0 )
178                     goto fail;
179                 for( num = start; num <= end && num < timecodes_num - 1; num++ )
180                     timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
181             }
182         }
183         if( h->auto_timebase_den || h->auto_timebase_num )
184             fpss[seq_num] = h->assume_fps;
185
186         if( h->auto_timebase_num && !h->auto_timebase_den )
187         {
188             double exponent;
189             double assume_fps_sig, seq_fps_sig;
190             if( try_mkv_timebase_den( fpss, h, seq_num + 1 ) < 0 )
191                 goto fail;
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; )
196             {
197                 fgets( buff, sizeof(buff), tcfile_in );
198                 if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
199                     continue;
200                 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
201                 if( ret != 3 )
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;
209             }
210         }
211         if( fpss )
212         {
213             free( fpss );
214             fpss = NULL;
215         }
216
217         h->assume_fps = assume_fps;
218         h->last_timecode = timecodes[timecodes_num - 1];
219     }
220     else    /* tcfv == 2 */
221     {
222         uint64_t file_pos = ftell( tcfile_in );
223
224         h->stored_pts_num = 0;
225         while( fgets( buff, sizeof(buff), tcfile_in ) != NULL )
226         {
227             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
228             {
229                 if( !h->stored_pts_num )
230                     file_pos = ftell( tcfile_in );
231                 continue;
232             }
233             h->stored_pts_num++;
234         }
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 );
238
239         timecodes = malloc( timecodes_num * sizeof(double) );
240         if( !timecodes )
241             return -1;
242
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; )
248         {
249             fgets( buff, sizeof(buff), tcfile_in );
250             if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
251                 continue;
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 )
256             ++num;
257         }
258
259         if( timecodes_num == 1 )
260             h->timebase_den = info->fps_num;
261         else if( h->auto_timebase_den )
262         {
263             fpss = malloc( (timecodes_num - 1) * sizeof(double) );
264             if( !fpss )
265                 goto fail;
266             for( num = 0; num < timecodes_num - 1; num++ )
267             {
268                 fpss[num] = 1 / (timecodes[num + 1] - timecodes[num]);
269                 if( h->auto_timebase_den )
270                 {
271                     int i = 1;
272                     uint64_t fps_num, fps_den;
273                     double exponent;
274                     double fps_sig = sigexp10( fpss[num], &exponent );
275                     while( 1 )
276                     {
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 )
280                             break;
281                         ++i;
282                     }
283                     h->timebase_den = fps_num && h->timebase_den ? lcm( h->timebase_den, fps_num ) : fps_num;
284                     if( h->timebase_den > UINT32_MAX )
285                     {
286                         h->auto_timebase_den = 0;
287                         continue;
288                     }
289                 }
290             }
291             if( h->auto_timebase_num && !h->auto_timebase_den )
292                 if( try_mkv_timebase_den( fpss, h, timecodes_num - 1 ) < 0 )
293                     goto fail;
294             free( fpss );
295             fpss = NULL;
296         }
297
298         if( timecodes_num > 1 )
299             h->assume_fps = 1 / (timecodes[timecodes_num - 1] - timecodes[timecodes_num - 2]);
300         else
301             h->assume_fps = (double)info->fps_num / info->fps_den;
302         h->last_timecode = timecodes[timecodes_num - 1];
303     }
304
305     if( h->auto_timebase_den || h->auto_timebase_num )
306     {
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 );
311     }
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" )
314
315     h->pts = malloc( h->stored_pts_num * sizeof(int64_t) );
316     if( !h->pts )
317         goto fail;
318     for( num = 0; num < h->stored_pts_num; num++ )
319     {
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 )
322     }
323
324     free( timecodes );
325     return 0;
326
327 fail:
328     if( timecodes )
329         free( timecodes );
330     if( fpss )
331         free( fpss );
332     return -1;
333 }
334
335 #undef DOUBLE_EPSILON
336 #undef MKV_TIMEBASE_DEN
337
338 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
339 {
340     int ret = 0;
341     FILE *tcfile_in;
342     timecode_hnd_t *h = malloc( sizeof(timecode_hnd_t) );
343     FAIL_IF_ERROR( !h, "malloc failed\n" )
344     h->input = input;
345     h->p_handle = *p_handle;
346     if( opt->timebase )
347     {
348         ret = sscanf( opt->timebase, "%"SCNu64"/%"SCNu64, &h->timebase_num, &h->timebase_den );
349         if( ret == 1 )
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" )
353     }
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;
362
363     *p_handle = h;
364
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 ) )
368     {
369         x264_cli_log( "timecode", X264_LOG_ERROR, "tcfile input incompatible with non-regular file `%s'\n", psz_filename );
370         fclose( tcfile_in );
371         return -1;
372     }
373
374     if( parse_tcfile( tcfile_in, h, info ) < 0 )
375     {
376         if( h->pts )
377             free( h->pts );
378         fclose( tcfile_in );
379         return -1;
380     }
381     fclose( tcfile_in );
382
383     info->timebase_num = h->timebase_num;
384     info->timebase_den = h->timebase_den;
385     info->vfr = 1;
386
387     return 0;
388 }
389
390 static int64_t get_frame_pts( timecode_hnd_t *h, int frame, int real_frame )
391 {
392     if( frame < h->stored_pts_num )
393         return h->pts[frame];
394     else
395     {
396         if( h->pts && real_frame )
397         {
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 );
400             free( h->pts );
401             h->pts = NULL;
402         }
403         double timecode = h->last_timecode + 1 / h->assume_fps;
404         if( real_frame )
405             h->last_timecode = timecode;
406         return timecode * ((double)h->timebase_den / h->timebase_num) + 0.5;
407     }
408 }
409
410 static int read_frame( cli_pic_t *pic, hnd_t handle, int frame )
411 {
412     timecode_hnd_t *h = handle;
413     if( h->input.read_frame( pic, h->p_handle, frame ) )
414         return -1;
415
416     pic->pts = get_frame_pts( h, frame, 1 );
417     pic->duration = get_frame_pts( h, frame + 1, 0 ) - pic->pts;
418
419     return 0;
420 }
421
422 static int release_frame( cli_pic_t *pic, hnd_t handle )
423 {
424     timecode_hnd_t *h = handle;
425     if( h->input.release_frame )
426         return h->input.release_frame( pic, h->p_handle );
427     return 0;
428 }
429
430 static int close_file( hnd_t handle )
431 {
432     timecode_hnd_t *h = handle;
433     if( h->pts )
434         free( h->pts );
435     h->input.close_file( h->p_handle );
436     free( h );
437     return 0;
438 }
439
440 cli_input_t timecode_input = { open_file, NULL, read_frame, release_frame, NULL, close_file };