]> git.sesse.net Git - x264/blobdiff - input/timecode.c
Bump dates to 2015
[x264] / input / timecode.c
index 69e1aade305fad54abae45c3759e4f18fc2006e2..b7beaebb8a7cd0c8d44328daa5aa7c33e3a58176 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * timecode.c: x264 timecode format file input module
+ * timecode.c: timecode file input
  *****************************************************************************
- * Copyright (C) 2010 x264 project
+ * Copyright (C) 2010-2015 x264 project
  *
  * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at licensing@x264.com.
  *****************************************************************************/
 
 #include "input.h"
 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "timecode", __VA_ARGS__ )
-#include <math.h>
 
 typedef struct
 {
@@ -99,18 +101,18 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
 
     ret = fscanf( tcfile_in, "# timecode format v%d", &tcfv );
     FAIL_IF_ERROR( ret != 1 || (tcfv != 1 && tcfv != 2), "unsupported timecode format\n" )
-
+#define NO_TIMECODE_LINE (buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r')
     if( tcfv == 1 )
     {
         uint64_t file_pos;
         double assume_fps, seq_fps;
-        int start, end;
+        int start, end = -1;
         int prev_start = -1, prev_end = -1;
 
         h->assume_fps = 0;
         for( num = 2; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
         {
-            if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
+            if( NO_TIMECODE_LINE )
                 continue;
             FAIL_IF_ERROR( sscanf( buff, "assume %lf", &h->assume_fps ) != 1 && sscanf( buff, "Assume %lf", &h->assume_fps ) != 1,
                            "tcfile parsing error: assumed fps not found\n" )
@@ -122,7 +124,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
         h->stored_pts_num = 0;
         for( seq_num = 0; fgets( buff, sizeof(buff), tcfile_in ) != NULL; num++ )
         {
-            if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
+            if( NO_TIMECODE_LINE )
             {
                 if( sscanf( buff, "# TDecimate Mode 3:  Last Frame = %d", &end ) == 1 )
                     h->stored_pts_num = end + 1;
@@ -138,7 +140,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
                 ++seq_num;
         }
         if( !h->stored_pts_num )
-            h->stored_pts_num = end + 1;
+            h->stored_pts_num = end + 2;
         timecodes_num = h->stored_pts_num;
         fseek( tcfile_in, file_pos, SEEK_SET );
 
@@ -156,10 +158,9 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
         if( assume_fps < 0 )
             goto fail;
         timecodes[0] = 0;
-        for( num = seq_num = 0; num < timecodes_num - 1; )
+        for( num = seq_num = 0; num < timecodes_num - 1 && fgets( buff, sizeof(buff), tcfile_in ) != NULL; )
         {
-            fgets( buff, sizeof(buff), tcfile_in );
-            if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
+            if( NO_TIMECODE_LINE )
                 continue;
             ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
             if( ret != 3 )
@@ -177,6 +178,8 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
                     timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
             }
         }
+        for( ; num < timecodes_num - 1; num++ )
+            timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
         if( h->auto_timebase_den || h->auto_timebase_num )
             fpss[seq_num] = h->assume_fps;
 
@@ -189,10 +192,9 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
             fseek( tcfile_in, file_pos, SEEK_SET );
             assume_fps_sig = sigexp10( h->assume_fps, &exponent );
             assume_fps = MKV_TIMEBASE_DEN / ( round( MKV_TIMEBASE_DEN / assume_fps_sig ) / exponent );
-            for( num = 0; num < timecodes_num - 1; )
+            for( num = 0; num < timecodes_num - 1 && fgets( buff, sizeof(buff), tcfile_in ) != NULL; )
             {
-                fgets( buff, sizeof(buff), tcfile_in );
-                if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
+                if( NO_TIMECODE_LINE )
                     continue;
                 ret = sscanf( buff, "%d,%d,%lf", &start, &end, &seq_fps );
                 if( ret != 3 )
@@ -204,9 +206,14 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
                 for( num = start; num <= end && num < timecodes_num - 1; num++ )
                     timecodes[num + 1] = timecodes[num] + 1 / seq_fps;
             }
+            for( ; num < timecodes_num - 1; num++ )
+                timecodes[num + 1] = timecodes[num] + 1 / assume_fps;
         }
         if( fpss )
+        {
             free( fpss );
+            fpss = NULL;
+        }
 
         h->assume_fps = assume_fps;
         h->last_timecode = timecodes[timecodes_num - 1];
@@ -218,7 +225,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
         h->stored_pts_num = 0;
         while( fgets( buff, sizeof(buff), tcfile_in ) != NULL )
         {
-            if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
+            if( NO_TIMECODE_LINE )
             {
                 if( !h->stored_pts_num )
                     file_pos = ftell( tcfile_in );
@@ -234,20 +241,24 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
         if( !timecodes )
             return -1;
 
-        fgets( buff, sizeof(buff), tcfile_in );
-        ret = sscanf( buff, "%lf", &timecodes[0] );
-        FAIL_IF_ERROR( ret != 1, "invalid input tcfile for frame 0\n" )
-        for( num = 1; num < timecodes_num; )
+        num = 0;
+        if( fgets( buff, sizeof(buff), tcfile_in ) != NULL )
         {
-            fgets( buff, sizeof(buff), tcfile_in );
-            if( buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' )
-                continue;
-            ret = sscanf( buff, "%lf", &timecodes[num] );
-            timecodes[num] *= 1e-3;         /* Timecode format v2 is expressed in milliseconds. */
-            FAIL_IF_ERROR( ret != 1 || timecodes[num] <= timecodes[num - 1],
-                           "invalid input tcfile for frame %d\n", num )
-            ++num;
+            ret = sscanf( buff, "%lf", &timecodes[0] );
+            timecodes[0] *= 1e-3;         /* Timecode format v2 is expressed in milliseconds. */
+            FAIL_IF_ERROR( ret != 1, "invalid input tcfile for frame 0\n" )
+            for( num = 1; num < timecodes_num && fgets( buff, sizeof(buff), tcfile_in ) != NULL; )
+            {
+                if( NO_TIMECODE_LINE )
+                    continue;
+                ret = sscanf( buff, "%lf", &timecodes[num] );
+                timecodes[num] *= 1e-3;         /* Timecode format v2 is expressed in milliseconds. */
+                FAIL_IF_ERROR( ret != 1 || timecodes[num] <= timecodes[num - 1],
+                               "invalid input tcfile for frame %d\n", num )
+                ++num;
+            }
         }
+        FAIL_IF_ERROR( num < timecodes_num, "failed to read input tcfile for frame %d", num )
 
         if( timecodes_num == 1 )
             h->timebase_den = info->fps_num;
@@ -259,7 +270,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
             for( num = 0; num < timecodes_num - 1; num++ )
             {
                 fpss[num] = 1 / (timecodes[num + 1] - timecodes[num]);
-                if( h->timebase_den >= 0 )
+                if( h->auto_timebase_den )
                 {
                     int i = 1;
                     uint64_t fps_num, fps_den;
@@ -285,6 +296,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
                 if( try_mkv_timebase_den( fpss, h, timecodes_num - 1 ) < 0 )
                     goto fail;
             free( fpss );
+            fpss = NULL;
         }
 
         if( timecodes_num > 1 )
@@ -293,7 +305,7 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
             h->assume_fps = (double)info->fps_num / info->fps_den;
         h->last_timecode = timecodes[timecodes_num - 1];
     }
-
+#undef NO_TIMECODE_LINE
     if( h->auto_timebase_den || h->auto_timebase_num )
     {
         uint64_t i = gcd( h->timebase_num, h->timebase_den );
@@ -307,11 +319,10 @@ static int parse_tcfile( FILE *tcfile_in, timecode_hnd_t *h, video_info_t *info
     h->pts = malloc( h->stored_pts_num * sizeof(int64_t) );
     if( !h->pts )
         goto fail;
-    h->pts[0] = 0;
-    for( num = 1; num < h->stored_pts_num; num++ )
+    for( num = 0; num < h->stored_pts_num; num++ )
     {
         h->pts[num] = timecodes[num] * ((double)h->timebase_den / h->timebase_num) + 0.5;
-        FAIL_IF_ERROR( h->pts[num] <= h->pts[num - 1], "invalid timebase or timecode for frame %d\n", num )
+        FAIL_IF_ERROR( num > 0 && h->pts[num] <= h->pts[num - 1], "invalid timebase or timecode for frame %d\n", num )
     }
 
     free( timecodes );
@@ -334,13 +345,17 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
     FILE *tcfile_in;
     timecode_hnd_t *h = malloc( sizeof(timecode_hnd_t) );
     FAIL_IF_ERROR( !h, "malloc failed\n" )
-    h->input = input;
+    h->input = cli_input;
     h->p_handle = *p_handle;
+    h->pts = NULL;
     if( opt->timebase )
     {
         ret = sscanf( opt->timebase, "%"SCNu64"/%"SCNu64, &h->timebase_num, &h->timebase_den );
         if( ret == 1 )
+        {
             h->timebase_num = strtoul( opt->timebase, NULL, 10 );
+            h->timebase_den = 0; /* set later by auto timebase generation */
+        }
         FAIL_IF_ERROR( h->timebase_num > UINT32_MAX || h->timebase_den > UINT32_MAX,
                        "timebase you specified exceeds H.264 maximum\n" )
     }
@@ -353,9 +368,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
     timecode_input.picture_alloc = h->input.picture_alloc;
     timecode_input.picture_clean = h->input.picture_clean;
 
-    *p_handle = h;
-
-    tcfile_in = fopen( psz_filename, "rb" );
+    tcfile_in = x264_fopen( psz_filename, "rb" );
     FAIL_IF_ERROR( !tcfile_in, "can't open `%s'\n", psz_filename )
     else if( !x264_is_regular_file( tcfile_in ) )
     {
@@ -377,6 +390,7 @@ static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, c
     info->timebase_den = h->timebase_den;
     info->vfr = 1;
 
+    *p_handle = h;
     return 0;
 }
 
@@ -403,12 +417,13 @@ static int64_t get_frame_pts( timecode_hnd_t *h, int frame, int real_frame )
 static int read_frame( cli_pic_t *pic, hnd_t handle, int frame )
 {
     timecode_hnd_t *h = handle;
-    int ret = h->input.read_frame( pic, h->p_handle, frame );
+    if( h->input.read_frame( pic, h->p_handle, frame ) )
+        return -1;
 
     pic->pts = get_frame_pts( h, frame, 1 );
     pic->duration = get_frame_pts( h, frame + 1, 0 ) - pic->pts;
 
-    return ret;
+    return 0;
 }
 
 static int release_frame( cli_pic_t *pic, hnd_t handle )