-
Notifications
You must be signed in to change notification settings - Fork 95
/
fmtx.c
135 lines (119 loc) · 3.36 KB
/
fmtx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Support for formatting time stamps
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the MPEG TS, PS and ES tools.
*
* The Initial Developer of the Original Code is Amino Communications Ltd.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Amino Communications Ltd, Swavesey, Cambridge UK
*
* ***** END LICENSE BLOCK *****
*/
#include <stdio.h>
#ifdef _WIN32
#include <stddef.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include <string.h>
#include "compat.h"
#include "fmtx.h"
static TCHAR fmtx_buffers[FMTX_BUFFERS_COUNT][FMTX_BUFFER_SIZE];
static int fmtx_buf_no = 0;
TCHAR *fmtx_alloc()
{
const int n = fmtx_buf_no++ % FMTX_BUFFERS_COUNT;
return fmtx_buffers[n];
}
int frac_27MHz(int64_t n)
{
return (int)((n < 0 ? -n : n) % 300LL);
}
const TCHAR *fmtx_timestamp(int64_t n, unsigned int flags)
{
TCHAR *buf = fmtx_alloc();
int64_t n27 = n * ((flags & FMTX_TS_N_27MHz) != 0 ? 1LL : 300LL);
switch (flags & FMTX_TS_DISPLAY_MASK)
{
default:
case FMTX_TS_DISPLAY_90kHz_RAW:
_sntprintf(buf, FMTX_BUFFER_SIZE, _T("%") I64FMT _T("dt"), n27 / 300LL);
break;
case FMTX_TS_DISPLAY_27MHz_RAW:
_sntprintf(buf, FMTX_BUFFER_SIZE, _T("%") I64FMT _T("d:%03dt"), n27 / 300LL, frac_27MHz(n27));
break;
case FMTX_TS_DISPLAY_90kHz_32BIT:
{
int64_t n90 = n27 / 300LL;
TCHAR * p = buf;
if (n90 < 0)
*p++ = _T('-');
_sntprintf(p, FMTX_BUFFER_SIZE, _T("%ut"), (unsigned int)(n90 < 0 ? -n90 : n90));
break;
}
case FMTX_TS_DISPLAY_ms:
// No timestamp when converted into ms should exceed 32bits
_sntprintf(buf, FMTX_BUFFER_SIZE, _T("%dms"), (int)(n27 / 27000LL));
break;
case FMTX_TS_DISPLAY_HMS:
{
unsigned int h, m, s, f;
int64_t a27 = n27 < 0 ? -n27 : n27;
a27 /= I64K(27); //us
f = (unsigned int)(a27 % I64K(1000000));
a27 /= I64K(1000000);
s = (unsigned int)(a27 % I64K(60));
a27 /= I64K(60);
m = (unsigned int)(a27 % I64K(60));
h = (unsigned int)(a27 / I64K(60));
_sntprintf(buf, FMTX_BUFFER_SIZE, _T("%s%u:%02u:%02u.%04u"), n27 < 0 ? _T("-") : _T(""), h, m, s, f/1000);
break;
}
}
return (const TCHAR *)buf;
}
static const struct s2tsfss
{
const char * str;
int flags;
} s2tsf[] =
{
{"hms", FMTX_TS_DISPLAY_HMS},
{"ms", FMTX_TS_DISPLAY_ms},
{"90", FMTX_TS_DISPLAY_90kHz_RAW},
{"32", FMTX_TS_DISPLAY_90kHz_32BIT},
{"27", FMTX_TS_DISPLAY_27MHz_RAW},
{NULL, -1}
};
int fmtx_str_to_timestamp_flags(const TCHAR *arg_str)
{
const struct s2tsfss * p;
for (p = s2tsf; p->str != NULL; ++p)
{
if (strcmp(p->str, arg_str) == 0)
break;
}
return p->flags;
}
// Local Variables:
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 2
// End:
// vim: set tabstop=8 shiftwidth=2 expandtab: