C_TimeStamp.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2004, 2005 Zentaro Kavanagh
00003 //
00004 //Copyright (C) 2004, 2005 Commonwealth Scientific and Industrial Research
00005 //   Organisation (CSIRO) Australia
00006 //
00007 //Redistribution and use in source and binary forms, with or without
00008 //modification, are permitted provided that the following conditions
00009 //are met:
00010 //
00011 //- Redistributions of source code must retain the above copyright
00012 //  notice, this list of conditions and the following disclaimer.
00013 //
00014 //- Redistributions in binary form must reproduce the above copyright
00015 //  notice, this list of conditions and the following disclaimer in the
00016 //  documentation and/or other materials provided with the distribution.
00017 //
00018 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00019 //  may be used to endorse or promote products derived from this software 
00020 //  without specific prior written permission.
00021 //
00022 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00026 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 //===========================================================================
00034 
00035 #include "stdafx.h"
00036 #include "C_TimeStamp.h"
00037 
00038 C_TimeStamp::C_TimeStamp(void)
00039         :       mSecs(0)
00040         ,       mHuns(0)
00041         ,       mStampType(TS_NONE)
00042 {
00043 }
00044 
00045 
00046 C_TimeStamp::~C_TimeStamp(void)
00047 {
00048 }
00049 
00050 bool C_TimeStamp::parseNPT(string inTimeStamp, sFourPartTime* inFPT) {
00051 
00052         string locLeftOver = "";
00053 
00054         bool locWasOK = parseThreePartTime(inTimeStamp, inFPT, &locLeftOver);
00055 
00056         if (locWasOK) {
00057                 if ( (locLeftOver.find(".") == 0) || (locLeftOver == "")) {
00058                         if (locLeftOver != "" ) {
00059                                 locLeftOver = locLeftOver.substr(1);
00060                         }
00061 
00062                         if (locLeftOver == "") {
00063                                 inFPT->partials = 0;
00064                                 return true;
00065                         } else {
00066                                 LOOG_UINT64 locSubSec = StringHelper::stringToFractNum(locLeftOver);
00067                                 
00068                                 if (locSubSec >= 0) {
00069                                         inFPT->partials = locSubSec;
00070                                         return true;
00071                                 } else {
00072                                         return false;
00073                                 }
00074                         }
00075                 } else {
00076                         return false;
00077                 }
00078         } else {
00079                 return false;
00080         }
00081 }
00082 
00083 bool C_TimeStamp::parseSMPT(string inTimeStamp, sFourPartTime* inFPT) {
00084         string locLeftOver = "";
00085 
00086         bool locWasOK = parseThreePartTime(inTimeStamp, inFPT, &locLeftOver);
00087 
00088         if (locWasOK) {
00089                 if ( (locLeftOver.find(":") == 0) || (locLeftOver == "")) {
00090                         locLeftOver = locLeftOver.substr(1);
00091                         if (locLeftOver == "") {
00092                                 inFPT->partials = 0;
00093                                 return true;
00094                         } else {
00095                                 long locSubSec = (long)StringHelper::stringToNum(locLeftOver);
00096                                 
00097                                 if (locSubSec >= 0) {
00098                                         //TODO::: Verify frames < numframes for type.
00099                                         inFPT->partials = locSubSec;
00100 
00101                                         return true;
00102                                 } else {
00103                                         return false;
00104                                 }
00105                         }
00106                 } else {
00107                         return false;
00108                 }
00109         } else {
00110                 return false;
00111         }
00112 }
00113 
00114 bool C_TimeStamp::parseThreePartTime(string inTimeStamp, sFourPartTime* inFPT, string* outLeftOver) {
00115         //This is *H:MM:SS
00116         size_t locColonPos = inTimeStamp.find(":");
00117         
00118         string locHours;
00119         string locMins;
00120         string locSecs;
00121 
00122         LOOG_INT64 locHH = 0;
00123         LOOG_INT64 locMM = 0;
00124         LOOG_INT64 locSS = 0;
00125 
00126         if (locColonPos != string::npos) {
00127                 locHours = inTimeStamp.substr(0, locColonPos);
00128         } else {
00129                 return false;
00130         }
00131 
00132         inTimeStamp = inTimeStamp.substr(locColonPos + 1);
00133         locColonPos = inTimeStamp.find(":");
00134 
00135         if (locColonPos != string::npos) {
00136                 locMins = inTimeStamp.substr(0, locColonPos);   
00137         } else {
00138                 return false;
00139         }
00140 
00141         inTimeStamp = inTimeStamp.substr(locColonPos + 1);
00142         locColonPos = inTimeStamp.find(":");
00143 
00144         if (locColonPos != string::npos) {
00145                 locSecs = inTimeStamp.substr(0, locColonPos);   
00146         } else {
00147                 locColonPos = inTimeStamp.find(".");
00148 
00149                 if (locColonPos != string::npos) {
00150                         locSecs = inTimeStamp.substr(0, locColonPos);
00151                 } else {
00152                         locSecs = inTimeStamp.substr(0);
00153                 }
00154         }
00155 
00156         if (locColonPos == string::npos) {
00157                 inTimeStamp = "";
00158         } else {
00159                 inTimeStamp = inTimeStamp.substr(locColonPos);
00160         }
00161 
00162         locHH = StringHelper::stringToNum(locHours);
00163         locMM = StringHelper::stringToNum(locMins);
00164         locSS = StringHelper::stringToNum(locSecs);
00165 
00166         if (            (locHH >= 0) && (locMM >= 0) && (locSS >= 0) && (locMM <= 59) && (locSS <=59) ) {
00167                 //The upper bound on hh is not checked here... it's upto the smpt time parser to ensure it <= 99
00168 
00169                 //Other wise, everything is ok.
00170                 inFPT->hours = locHH;
00171                 inFPT->minutes = (short)locMM;
00172                 inFPT->seconds = (short)locSS;
00173                 inFPT->partials = 0;
00174                 *outLeftOver = inTimeStamp;
00175                 return true;
00176 
00177         } else {
00178                 return false;
00179         }
00180 
00181 
00182         
00183 
00184 }
00185 
00186 bool C_TimeStamp::parseSecsOnly(string inTimeStamp) {
00187         
00188         string locSecs;
00189         string locHuns;
00190         LOOG_UINT64 locSS;
00191         LOOG_UINT64 locNN;
00192 
00193         size_t locDotPos = inTimeStamp.find(".");
00194 
00195         if (locDotPos == string::npos) {
00196                 //No dot here
00197                 mSecs = StringHelper::stringToNum(inTimeStamp);
00198                 mHuns = 0;
00199                 mStampType = TS_NPT_SECS;
00200                 return true;
00201         } else {
00202                 //Dotted time
00203 
00204                 locSecs = inTimeStamp.substr(0, locDotPos);
00205                 locHuns = inTimeStamp.substr(locDotPos + 1);
00206 
00207                 locSS = StringHelper::stringToNum(locSecs);
00208 
00209                 if (locHuns == "") {
00210                         locNN = 0;
00211                 } else {
00212                         locNN = StringHelper::stringToFractNum(locHuns);
00213                 }
00214 
00215                 if (    (locSS >= 0) && (locNN >= 0)) {
00216                         mSecs = locSS;
00217                         mHuns = locNN;
00218                         mStampType = TS_NPT_SECS;
00219                         return true;
00220                 } else {
00221                         mStampType = TS_NONE;
00222                         return false;
00223                 }
00224         }
00225 }
00226 
00227 LOOG_INT64 C_TimeStamp::toHunNanos() {
00228         switch (mStampType) {
00229                 case TS_NPT_SECS:
00230                         return (mSecs * 10000000) + (mHuns);
00231                 case TS_NPT_FULL:
00232                         return ((LOOG_INT64)mFPT.hours * (LOOG_INT64)3600 * (LOOG_INT64)10000000) + ((LOOG_INT64)mFPT.minutes * (LOOG_INT64)60 * (LOOG_INT64)10000000) + ((LOOG_INT64)mFPT.seconds * (LOOG_INT64)10000000) + ((LOOG_INT64)mFPT.partials);
00233                 default:
00234                         return  -1;
00235 
00236         };
00237 }
00238 
00239 bool C_TimeStamp::parseTimeStamp(double inTimeStampInSeconds)
00240 {
00241         char *locTimeCString = new char[64];
00242         if (sprintf(locTimeCString, "%lf", inTimeStampInSeconds) < 0) {
00243                 delete [] locTimeCString;
00244                 return false;
00245         }
00246 
00247         bool locReturnValue = parseTimeStamp(locTimeCString);
00248 
00249         delete [] locTimeCString;
00250 
00251         return locReturnValue;
00252 }
00253 
00254 bool C_TimeStamp::parseTimeStamp(string inTimeStamp)
00255 {
00256         try {
00257                 if (inTimeStamp.find("npt:") == 0) {
00258                         //NPT time stamp
00259                         inTimeStamp = inTimeStamp.substr(4);
00260 
00261                         if (inTimeStamp.find(":") != string::npos) {
00262                                 //We have four part time
00263                                 sFourPartTime locFPT;
00264                                 if ( parseNPT(inTimeStamp, &locFPT) ) {
00265                                         mFPT = locFPT;
00266                                         mStampType = TS_NPT_FULL;
00267                                         return true;
00268                                 } else {
00269                                         mStampType = TS_NONE;
00270                                         return false;
00271                                 }
00272                                 
00273                         } else {
00274                                 bool locIsOK = parseSecsOnly(inTimeStamp);
00275                                 if (locIsOK) {
00276                                         mStampType = TS_NPT_SECS;
00277                                         return true;
00278                                 } else {
00279                                         mStampType = TS_NONE;
00280                                         return false;
00281                                 }       
00282                         }
00283 
00284                 } else if (inTimeStamp.find("smpte-") == 0) {
00285                         //One of the smpt stamps
00286                         inTimeStamp = inTimeStamp.substr(6);
00287 
00288                         size_t locColonPos = inTimeStamp.find(":");
00289 
00290                         if (locColonPos != string::npos) {
00291                                 string locFrameRate = inTimeStamp.substr(0, locColonPos);
00292 
00293                                 //TODO::: Need to set the spec type here...
00294                                 inTimeStamp = inTimeStamp.substr(locColonPos + 1);
00295 
00296                                 sFourPartTime locFPT;
00297                                 if ( parseSMPT(inTimeStamp, &locFPT) ) {
00298                                         mFPT = locFPT;
00299                                         mStampType = TS_SMPT;
00300                                         return true;
00301                                 } else {
00302                                         mStampType = TS_NONE;
00303                                         return false;
00304                                 }
00305                         } else {
00306                                 mStampType = TS_NONE;
00307                                 return false;
00308                         }
00309 
00310 
00311 
00312                 } else {
00313                         //Assume it's default numeric npt
00314                         bool locIsOK = parseSecsOnly(inTimeStamp);
00315                         if (locIsOK) {
00316                                 mStampType = TS_NPT_SECS;
00317                                 return true;
00318                         } else {
00319                                 mStampType = TS_NONE;
00320                                 return false;
00321                         }       
00322 
00323 
00324                 }
00325         } catch (...) {
00326                 mStampType = TS_NONE;
00327                 return false;
00328 
00329         }
00330 
00331 }

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