00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
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
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
00149
00150
00151
00152
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 }