|
Packit |
a38265 |
/*
|
|
Packit |
a38265 |
Copyright (C) 2008 Annodex Association
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
Based on anx_time.c from libannodex, Copyright (C) 2003 Commonwealth
|
|
Packit |
a38265 |
Scientific and Industrial Research Organisation (CSIRO) Australia
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
Redistribution and use in source and binary forms, with or without
|
|
Packit |
a38265 |
modification, are permitted provided that the following conditions
|
|
Packit |
a38265 |
are met:
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
- Redistributions of source code must retain the above copyright
|
|
Packit |
a38265 |
notice, this list of conditions and the following disclaimer.
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
- Redistributions in binary form must reproduce the above copyright
|
|
Packit |
a38265 |
notice, this list of conditions and the following disclaimer in the
|
|
Packit |
a38265 |
documentation and/or other materials provided with the distribution.
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
- Neither the name of CSIRO Australia nor the names of its
|
|
Packit |
a38265 |
contributors may be used to endorse or promote products derived from
|
|
Packit |
a38265 |
this software without specific prior written permission.
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
a38265 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
a38265 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
Packit |
a38265 |
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
|
|
Packit |
a38265 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
Packit |
a38265 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
Packit |
a38265 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
Packit |
a38265 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
Packit |
a38265 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
Packit |
a38265 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
Packit |
a38265 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
a38265 |
*/
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
#include "config.h"
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
#include <stdio.h>
|
|
Packit |
a38265 |
#include <stdlib.h>
|
|
Packit |
a38265 |
#include <string.h>
|
|
Packit |
a38265 |
#include <math.h>
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
static double
|
|
Packit |
a38265 |
parse_npt (const char *str)
|
|
Packit |
a38265 |
{
|
|
Packit |
a38265 |
int h=0,m=0, n;
|
|
Packit |
a38265 |
float s;
|
|
Packit |
a38265 |
double result;
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
n = sscanf (str, "%d:%d:%f", &h, &m, &s);
|
|
Packit |
a38265 |
if (n == 3) {
|
|
Packit |
a38265 |
goto done;
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
n = sscanf (str, "%d:%f", &m, &s);
|
|
Packit |
a38265 |
if (n == 2) {
|
|
Packit |
a38265 |
h = 0;
|
|
Packit |
a38265 |
goto done;
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
n = sscanf (str, "%f", &s);
|
|
Packit |
a38265 |
if (n == 1) {
|
|
Packit |
a38265 |
h = 0; m = 0;
|
|
Packit |
a38265 |
goto sec_only;
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
return -1.0;
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
done:
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
/* check valid time specs */
|
|
Packit |
a38265 |
if (h < 0) return -1;
|
|
Packit |
a38265 |
if (m > 59 || m < 0) return -1;
|
|
Packit |
a38265 |
if (s >= 60.0 || s < 0) return -1;
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
sec_only:
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
result = ((h * 3600.0) + (m * 60.0) + s);
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
return result;
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
/* parse_smpte: parse a smpte-string */
|
|
Packit |
a38265 |
static double
|
|
Packit |
a38265 |
parse_smpte(const char *str, double framerate)
|
|
Packit |
a38265 |
{
|
|
Packit |
a38265 |
int h = 0, m = 0, s = 0, n;
|
|
Packit |
a38265 |
float frames;
|
|
Packit |
a38265 |
double result;
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
n = sscanf (str, "%d:%d:%d:%f", &h, &m, &s, &frames);
|
|
Packit |
a38265 |
if (n == 4) {
|
|
Packit |
a38265 |
goto done;
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
n = sscanf (str, "%d:%d:%f", &m, &s, &frames);
|
|
Packit |
a38265 |
if (n == 3) {
|
|
Packit |
a38265 |
h = 0;
|
|
Packit |
a38265 |
goto done;
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
return -1.0;
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
done:
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
/* check valid time specs */
|
|
Packit |
a38265 |
if (h < 0) return -1;
|
|
Packit |
a38265 |
if (m > 59 || m < 0) return -1;
|
|
Packit |
a38265 |
if (s > 59 || s < 0) return -1;
|
|
Packit |
a38265 |
if (frames > (float)ceil(framerate) || frames < 0) return -1;
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
result = ((h * 3600.0) + (m * 60.0) + s) + (frames/framerate);
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
return result;
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
double
|
|
Packit |
a38265 |
parse_timespec (const char * str)
|
|
Packit |
a38265 |
{
|
|
Packit |
a38265 |
char timespec[16];
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (str == NULL) return -1.0;
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "npt:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_npt (timespec);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-24:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 24.0);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-24-drop:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 23.976);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-25:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 25.0);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-30:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 30.0);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-30-drop:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 29.97);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-50:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 50.0);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-60:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 60);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
if (sscanf (str, "smpte-60-drop:%16s", timespec) == 1) {
|
|
Packit |
a38265 |
return parse_smpte (timespec, 59.94);
|
|
Packit |
a38265 |
}
|
|
Packit |
a38265 |
|
|
Packit |
a38265 |
return parse_npt(str);
|
|
Packit |
a38265 |
}
|