anx_time.c

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2003 Commonwealth Scientific and Industrial Research
00003    Organisation (CSIRO) Australia
00004 
00005    Redistribution and use in source and binary forms, with or without
00006    modification, are permitted provided that the following conditions
00007    are met:
00008 
00009    - Redistributions of source code must retain the above copyright
00010    notice, this list of conditions and the following disclaimer.
00011 
00012    - Redistributions in binary form must reproduce the above copyright
00013    notice, this list of conditions and the following disclaimer in the
00014    documentation and/or other materials provided with the distribution.
00015 
00016    - Neither the name of CSIRO Australia nor the names of its
00017    contributors may be used to endorse or promote products derived from
00018    this software without specific prior written permission.
00019 
00020    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00024    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 */
00032 
00033 #include "anx_time.h"
00034 
00035 #include <stdio.h>
00036 #include <math.h>
00037 
00038 static double
00039 parse_npt (const char *str)
00040 {
00041   int h=0,m=0, n;
00042   float s;
00043   double result;
00044 
00045   n = sscanf (str, "%d:%d:%f",  &h, &m, &s);
00046   if (n == 3) {
00047     goto done;
00048   }
00049 
00050   n = sscanf (str, "%d:%f",  &m, &s);
00051   if (n == 2) {
00052     h = 0;
00053     goto done;
00054   }
00055 
00056   n = sscanf (str, "%f", &s);
00057   if (n == 1) {
00058     h = 0; m = 0;
00059     goto sec_only;
00060   }
00061 
00062   return -1.0;
00063 
00064  done:
00065 
00066   /* check valid time specs */
00067   if (h < 0) return -1;
00068   if (m > 59 || m < 0) return -1;
00069   if (s >= 60.0 || s < 0) return -1;
00070 
00071  sec_only:
00072 
00073   result = ((h * 3600.0) + (m * 60.0) + s);
00074 
00075   return result;
00076 }
00077 
00078 /* parse_smpte: parse a smpte-string */
00079 static double
00080 parse_smpte(const char *str, double framerate)
00081 {
00082   int h = 0, m = 0, s = 0, n;
00083   float frames;
00084   double result;
00085 
00086   n = sscanf (str, "%d:%d:%d:%f", &h, &m, &s, &frames);
00087   if (n == 4) {
00088     goto done;
00089   }
00090 
00091   n = sscanf (str, "%d:%d:%f", &m, &s, &frames);
00092   if (n == 3) {
00093     h = 0;
00094     goto done;
00095   }
00096 
00097   return -1.0;
00098 
00099  done:
00100 
00101   /* check valid time specs */
00102   if (h < 0) return -1;
00103   if (m > 59 || m < 0) return -1;
00104   if (s > 59 || s < 0) return -1;
00105   if (frames > (float)ceil(framerate) || frames < 0) return -1;
00106 
00107   result = ((h * 3600.0) + (m * 60.0) + s) + (frames/framerate);
00108 
00109   return result;
00110 
00111 #if 0
00112   char *p;
00113   int maxframes;
00114   maxframes = (int) ceil(framerate) - 1;
00115 
00116   hours = strtol(s, &p, 10);
00117   if (*p != ':' || !isdigit(*(p+1))) {
00118     secs = hours;
00119     hours = 0;
00120     goto return_time;
00121   }
00122   p++;
00123 
00124   mins = strtol(p, &p, 10);
00125   if (*p != ':' || !isdigit(*(p+1))) {
00126     secs = mins;
00127     mins = hours;
00128     hours = 0;
00129     goto return_time;
00130   }
00131   p++;
00132 
00133   secs = strtol(p, &p, 10);
00134   if (*p != ':' || !isdigit(*(p+1))) {
00135     goto return_time;
00136   }
00137 
00138   if (*p == ':') { 
00139     frames = strtol(++p, &p, 10);
00140   }
00141 
00142  return_time:
00143   if (*p == '.') {
00144     subframes = strtol(++p, &p, 10);
00145   }
00146 
00147   /*
00148   fprintf(stderr, "hours=%d min=%d sec=%d frames=%d subframes=%d\n", hours, mins
00149 , secs, frames, subframes);
00150   */
00151 
00152   /* check valid time specs */
00153   if (hours<0) return -1;
00154   if (mins>59 || mins<0) return -1;
00155   if (secs>59 || secs<0) return -1;
00156   if (frames>maxframes || frames<0) return -1;
00157   if (subframes>99 || subframes<0) return -1;
00158 
00159   return ( 1.0*hms_to_secs(hours, mins, secs)
00160            + (1.0*frames/framerate)
00161            + (1.0*subframes/(100.0*framerate)) );
00162 #endif
00163 }
00164 
00165 double
00166 anx_parse_time (const char * str)
00167 {
00168   char timespec[16];
00169 
00170   if (str == NULL) return -1.0;
00171 
00172   if (sscanf (str, "npt:%16s", timespec) == 1) {
00173     return parse_npt (timespec);
00174   }
00175 
00176   if (sscanf (str, "smpte-24:%16s", timespec) == 1) {
00177     return parse_smpte (timespec, 24.0);
00178   }
00179   
00180   if (sscanf (str, "smpte-24-drop:%16s", timespec) == 1) {
00181     return parse_smpte (timespec, 23.976);
00182   }
00183   
00184   if (sscanf (str, "smpte-25:%16s", timespec) == 1) {
00185     return parse_smpte (timespec, 25.0);
00186   }
00187   
00188   if (sscanf (str, "smpte-30:%16s", timespec) == 1) {
00189     return parse_smpte (timespec, 30.0);
00190   }
00191   
00192   if (sscanf (str, "smpte-30-drop:%16s", timespec) == 1) {
00193     return parse_smpte (timespec, 29.97);
00194   }
00195   
00196   if (sscanf (str, "smpte-50:%16s", timespec) == 1) {
00197     return parse_smpte (timespec, 50.0);
00198   }
00199   
00200   if (sscanf (str, "smpte-60:%16s", timespec) == 1) {
00201     return parse_smpte (timespec, 60);
00202   }
00203 
00204   if (sscanf (str, "smpte-60-drop:%16s", timespec) == 1) {
00205     return parse_smpte (timespec, 59.94);
00206   }
00207 
00208   return parse_npt(str);
00209 }

Generated on Tue Feb 15 14:54:23 2005 for oggdsf by  doxygen 1.3.9