[xiph-commits] r16857 - experimental/derf/theora-ptalarbvorm/examples

tterribe at svn.xiph.org tterribe at svn.xiph.org
Sat Jan 30 12:05:26 PST 2010


Author: tterribe
Date: 2010-01-30 12:05:26 -0800 (Sat, 30 Jan 2010)
New Revision: 16857

Modified:
   experimental/derf/theora-ptalarbvorm/examples/encoder_example.c
Log:
Fix begin and end time handling.
The previous code was borken if you specified more than 6 fractional digits.
There were also easily triggerable overflows when mapping those times to frame
 and sample numbers.


Modified: experimental/derf/theora-ptalarbvorm/examples/encoder_example.c
===================================================================
--- experimental/derf/theora-ptalarbvorm/examples/encoder_example.c	2010-01-30 14:30:57 UTC (rev 16856)
+++ experimental/derf/theora-ptalarbvorm/examples/encoder_example.c	2010-01-30 20:05:26 UTC (rev 16857)
@@ -940,8 +940,8 @@
   static ogg_int64_t samples_sofar=0;
   ogg_packet op;
   int i,j;
-  ogg_int64_t beginsample = audio_hz*begin_sec + audio_hz*begin_usec*.000001;
-  ogg_int64_t endsample = audio_hz*end_sec + audio_hz*end_usec*.000001;
+  ogg_int64_t beginsample = audio_hz*(begin_sec+begin_usec*.000001);
+  ogg_int64_t endsample = audio_hz*(end_sec+end_usec*.000001);
 
   while(audio && !audioflag){
     /* process any audio already buffered */
@@ -1031,8 +1031,8 @@
   ogg_int64_t                beginframe;
   ogg_int64_t                endframe;
   spinnit();
-  beginframe=(video_fps_n*begin_sec+video_fps_n*begin_usec*.000001)/video_fps_d;
-  endframe=(video_fps_n*end_sec+video_fps_n*end_usec*.000001)/video_fps_d;
+  beginframe=video_fps_n*(begin_sec+begin_usec*.000001)/video_fps_d;
+  endframe=video_fps_n*(end_sec+end_usec*.000001)/video_fps_d;
   if(frame_state==-1){
     /* initialize the double frame buffer */
     yuvframe[0]=(unsigned char *)malloc(y4m_dst_buf_sz);
@@ -1211,6 +1211,35 @@
   return ret;
 }
 
+static int parse_time(long *_sec,long *_usec,const char *_optarg){
+  double      secf;
+  long        secl;
+  const char *pos;
+  char       *end;
+  int         err;
+  err=0;
+  secl=0;
+  pos=strchr(_optarg,':');
+  if(pos!=NULL){
+    char *pos2;
+    secl=strtol(_optarg,&end,10)*60;
+    err|=pos!=end;
+    pos2=strchr(++pos,':');
+    if(pos2!=NULL){
+      secl=(secl+strtol(pos,&end,10))*60;
+      err|=pos2!=end;
+      pos=pos2+1;
+    }
+  }
+  else pos=_optarg;
+  secf=strtod(pos,&end);
+  printf("%G %G\n",secf,secf-floor(secf));
+  if(err||*end!='\0')return -1;
+  *_sec=secl+(long)floor(secf);
+  *_usec=(long)((secf-floor(secf))*1E6+0.5);
+  return 0;
+}
+
 int main(int argc,char *argv[]){
   int c,long_option_index,ret;
 
@@ -1353,51 +1382,17 @@
 
     case 'b':
       {
-        char *pos=strchr(optarg,':');
-        begin_sec=atol(optarg);
-        if(pos){
-          char *pos2=strchr(++pos,':');
-          begin_sec*=60;
-          begin_sec+=atol(pos);
-          if(pos2){
-            pos2++;
-            begin_sec*=60;
-            begin_sec+=atol(pos2);
-            pos=pos2;
-          }
-        }else
-          pos=optarg;
-        pos=strchr(pos,'.');
-        if(pos){
-          int digits = strlen(++pos);
-          begin_usec=atol(pos);
-          while(digits++ < 6)
-            begin_usec*=10;
+        if(parse_time(&begin_sec,&begin_usec,optarg)<0){
+          fprintf(stderr,"Error parsing begin time '%s'.\n",optarg);
+          exit(1);
         }
       }
       break;
     case 'e':
       {
-        char *pos=strchr(optarg,':');
-        end_sec=atol(optarg);
-        if(pos){
-          char *pos2=strchr(++pos,':');
-          end_sec*=60;
-          end_sec+=atol(pos);
-          if(pos2){
-            pos2++;
-            end_sec*=60;
-            end_sec+=atol(pos2);
-            pos=pos2;
-          }
-        }else
-          pos=optarg;
-        pos=strchr(pos,'.');
-        if(pos){
-          int digits = strlen(++pos);
-          end_usec=atol(pos);
-          while(digits++ < 6)
-            end_usec*=10;
+        if(parse_time(&end_sec,&end_usec,optarg)<0){
+          fprintf(stderr,"Error parsing end time '%s'.\n",optarg);
+          exit(1);
         }
       }
       break;



More information about the commits mailing list