forked from fsphil/tsmerge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.c
204 lines (170 loc) · 4.55 KB
/
info.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <inttypes.h>
#include <time.h>
#include "ts/ts.h"
#include "info.h"
void _print_version(void)
{
printf(
"tsinfo (tsmerge) "BUILD_VERSION" (built "BUILD_DATE")\n"
);
}
void _print_usage(void)
{
printf(
"\n"
"Usage: cat <ts file> | tsinfo [options]\n"
"\n"
" -V, --version Print version string and exit.\n"
" -d, --dump Dump CSV of TS Packet PCR & Continuity Counter Data.\n"
"\n"
);
}
static void _reset_info(ts_info_t *ts_info)
{
ts_info->first_pcr_base = 0;
ts_info->first_pcr_extension = 0;
ts_info->last_pcr_base = 0;
ts_info->last_pcr_extension = 0;
ts_info->pcr_extension_used = 0;
ts_info->packet_count = 0;
ts_info->padding_count = 0;
ts_info->invalid_count = 0;
ts_info->pcr_count = 0;
}
int main(int argc, char *argv[])
{
int c;
FILE *f = stdin;
uint8_t data[TS_PACKET_SIZE];
ts_header_t ts;
ts_info_t ts_info;
int opt;
FILE *fp;
int csvEnabled = 0;
char csvText[128];
char csvFilename[64];
uint8_t csvTextSize;
time_t now;
char stime[20];
static const struct option long_options[] = {
{ "version", no_argument, 0, 'V' },
{ "dump", no_argument, 0, 'd' },
{ 0, 0, 0, 0 }
};
while((c = getopt_long(argc, argv, "Vd", long_options, &opt)) != -1)
{
switch(c)
{
case 'V': /* --version */
_print_version();
return 0;
break;
case 'd': /* --dump */
csvEnabled = 1;
break;
case '?':
_print_usage();
return 0;
}
}
_reset_info(&ts_info);
if(csvEnabled == 1)
{
time(&now);
strftime(stime, sizeof stime, "%F_%TZ", gmtime(&now));
snprintf(csvFilename,63,"tsinfo-%s.csv",stime);
printf("Dumping TS description CSV to: %s\n", csvFilename);
fp = fopen(csvFilename, "a+");
}
/* Stream the data */
while(fread(data, 1, TS_PACKET_SIZE, f) == TS_PACKET_SIZE)
{
if(data[0] != TS_HEADER_SYNC)
{
/* Re-align input to the TS sync byte */
uint8_t *p = memchr(data, TS_HEADER_SYNC, TS_PACKET_SIZE);
if(p == NULL) continue;
c = p - data;
memmove(data, p, TS_PACKET_SIZE - c);
if(fread(&data[TS_PACKET_SIZE - c], 1, c, f) != (size_t)c)
{
break;
}
}
ts_info.packet_count++;
/* Invalid Header Packets */
if(ts_parse_header(&ts, data) != TS_OK)
{
ts_info.invalid_count++;
continue;
}
/* NULL/padding packets */
if(ts.pid == TS_NULL_PID)
{
ts_info.padding_count++;
continue;
}
if(ts.pcr_flag)
{
ts_info.pcr_count++;
if(ts_info.first_pcr_base == 0 && ts_info.first_pcr_extension == 0)
{
ts_info.first_pcr_base = ts.pcr_base;
ts_info.first_pcr_extension = ts.pcr_extension;
}
ts_info.last_pcr_base = ts.pcr_base;
ts_info.last_pcr_extension = ts.pcr_extension;
if(ts.pcr_extension > 0)
{
ts_info.pcr_extension_used = 1;
}
}
if(csvEnabled == 1)
{
/* Create text [packet counter, packet pcr base, packet pcr extension, packet pid, packet continuity counter] */
csvTextSize = snprintf(csvText, 127, "%"PRIu32",%"PRIu64",%"PRIu16",%"PRIu16",%"PRIu8"\n",
ts_info.packet_count,
ts.pcr_base,
ts.pcr_extension,
ts.pid,
ts.continuity_counter
);
(void)fwrite(csvText, csvTextSize, 1, fp);
}
}
if(csvEnabled == 1)
{
fclose(fp);
}
if(f != stdin)
{
fclose(f);
}
/* Print report */
printf("TS Duration: %ldms\n", (ts_info.last_pcr_base - ts_info.first_pcr_base) / 90);
printf("TS Bitrate: ~%.03fMbps\n", (double)(ts_info.packet_count*204*8)/((ts_info.last_pcr_base - ts_info.first_pcr_base) / 90000)/(1000*1000));
if(ts_info.pcr_extension_used)
{
printf("TS Start PCR: %ld:%d\n", ts_info.first_pcr_base, ts_info.first_pcr_extension);
printf("TS End PCR : %ld:%d\n", ts_info.last_pcr_base, ts_info.last_pcr_extension);
}
else
{
printf("TS Start PCR: %ld\n", ts_info.first_pcr_base);
printf("TS End PCR : %ld\n", ts_info.last_pcr_base);
printf(" (PCR Extension Value is not used in this sample)\n");
}
printf("Total Packets: %d\n", ts_info.packet_count);
printf("Invalid Packets: %d\n", ts_info.invalid_count);
printf("Padding/Null Packets: %d (%.2f%%)\n",
ts_info.padding_count, (double)(100*ts_info.padding_count)/ts_info.packet_count);
printf("PCR Packets: %d (every ~%ldms)\n", ts_info.pcr_count,
(ts_info.last_pcr_base - ts_info.first_pcr_base) / (90 * ts_info.pcr_count));
return(0);
}