-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsup.c
176 lines (137 loc) · 2.98 KB
/
sup.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "sup.h"
#include <stddef.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
int sup_read_packet(void *p, int len, sup_packet_t *pkg) {
size_t hsize = offsetof(sup_packet_t, data);
if (len < hsize) {
return SUP_ERROR;
}
if (memcmp(p, "PG", 2) != 0) {
return SUP_ERROR;
}
memcpy(pkg, p, hsize);
pkg->pts = ntohl(pkg->pts);
pkg->dts = ntohl(pkg->dts);
pkg->size = ntohs(pkg->size);
pkg->data = p + hsize;
return SUP_OK;
}
int
sup_walk(void *p, int len, sup_packet_handler_pt func, void *arg)
{
int iv;
sup_packet_t pkg;
while (len > 0) {
if (sup_read_packet(p, len, &pkg) != SUP_OK) {
return SUP_ERROR;
}
iv = func(&pkg, p, arg);
if (iv != SUP_OK) {
return iv;
}
iv = offsetof(sup_packet_t, data) + pkg.size;
p += iv;
len -= iv;
}
return SUP_OK;
}
int
sup_run(const char *fpath, sup_packet_handler_pt func, void *arg)
{
int iv;
sup_str_t map;
if (sup_load(fpath, &map) != SUP_OK) {
return SUP_ERROR;
}
iv = sup_walk(map.data, map.len, func, arg);
sup_close(&map);
return iv;
}
int
sup_load(const char *file, sup_str_t *s) {
int fd;
void *p;
struct stat st;
if (stat(file, &st) != 0) {
perror("error stat sup file");
return SUP_ERROR;
}
fd = open(file, O_RDONLY);
if (fd == -1) {
perror("error open sup file");
return SUP_ERROR;
}
p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (p == NULL) {
perror("error mmap sup file");
return SUP_ERROR;
}
s->data = p;
s->len = st.st_size;
return SUP_OK;
}
void
sup_close(sup_str_t *s)
{
if (s->data) {
munmap(s->data, s->len);
s->data = NULL;
}
}
int
sup_parse_pcs(sup_packet_t *pkg, sup_pcs_t *pcs)
{
if (pkg->size < 11) {
return SUP_ERROR;
}
memcpy(pcs, pkg->data, sizeof(sup_pcs_t));
pcs->video_width = ntohs(pcs->video_width);
pcs->video_height = ntohs(pcs->video_height);
pcs->comp_id = ntohs(pcs->comp_id);
if (pkg->size != pcs->num_of_objects * 8 + sizeof(sup_pcs_t)) {
fprintf(stderr, "Invalid PCS segment length\n");
return SUP_ERROR;
}
return SUP_OK;
}
char *
sup_pts2time(uint32_t pts)
{
int iv[4], ms;
static char buf[sizeof("XX00:00:00.000")];
ms = sup_pts2ms(pts);
iv[0] = ms / 0x36ee80;
ms -= iv[0] * 60 * 60 * 1000;
iv[1] = ms / 60000;
ms -= iv[1] * 60 * 1000;
iv[2] = ms / 1000;
ms -= iv[2] * 1000;
iv[3] = ms;
snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%03d", iv[0], iv[1], iv[2], iv[3]);
return buf;
}
char *
sup_state2name(int s)
{
switch (s) {
case SUP_PCS_STATE_EPOCH_START:
return "START";
case SUP_PCS_STATE_NORMAL:
return "NORMAL";
case SUP_PCS_STATE_EPOCH_CONTINUE:
return "CONTINUE";
case SUP_PCS_STATE_ACQU_POINT:
return "ACQU";
default:
break;
}
return "UNK";
}