-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathts2na.c
155 lines (135 loc) · 4.66 KB
/
ts2na.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include "ts.h"
#define DEBUG(format, ...) fprintf (stderr, "DEBUG: "format"\n", ## __VA_ARGS__)
#define INFO(format, ...) fprintf (stderr, "INFO: "format"\n", ## __VA_ARGS__)
#define WARN(format, ...) fprintf (stderr, "WARN: "format"\n", ## __VA_ARGS__)
#define ERROR(format, ...) fprintf (stderr, "ERROR: "format"\n", ## __VA_ARGS__)
/*****************************************************************************
* Main loop
*****************************************************************************/
static void usage(const char *psz)
{
fprintf(stderr, "usage: %s [-p pid] [-s offset] [-i <inputfile>] [-o <outputfile>]\n", psz);
exit(EXIT_FAILURE);
}
int main(int i_argc, char **ppsz_argv)
{
int c;
FILE *inputfile=stdin;
FILE *outputfile=stdout;
int offset=12, pid=0x0426;
int i_last_cc = -1;
static const struct option long_options[] = {
{ "pid", required_argument, NULL, 'p' },
{ "offset", required_argument, NULL, 's' },
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o' },
{ 0, 0, 0, 0 }
};
while ((c = getopt_long(i_argc, ppsz_argv, "p:s:i:o:h", long_options, NULL)) != -1)
{
switch (c) {
case 'p':
pid=strtoul(optarg, NULL, 0);
if(pid >= 8192) {
ERROR("bad pid value: %d!", pid);
exit(1);
}
break;
case 's':
offset=strtol(optarg, NULL, 0);
if(offset >= 184 || offset <= -4) {
ERROR("bad offset value: %d!", offset);
exit(1);
}
break;
case 'i':
inputfile = fopen(optarg, "r");
if(!inputfile) {
ERROR("cant open input file!");
exit(1);
}
break;
case 'o':
outputfile = fopen(optarg, "w");
if(!outputfile) {
ERROR("cant open output file!");
exit(1);
}
break;
case 'h':
default:
usage(ppsz_argv[0]);
}
}
INFO("Using pid: 0x%04x (%d)", pid, pid);
unsigned long int packets=0;
while (!feof(inputfile) && !ferror(inputfile)) {
uint8_t p_ts[TS_SIZE];
size_t i_ret = fread(p_ts, TS_SIZE, 1, inputfile);
if (i_ret != 1) {
WARN("Can't read input ts");
break;
}
if (ts_validate(p_ts)) {
if(offset >= 0 && ts_get_pid(p_ts)==pid) {
if(i_last_cc > 0 && (0x0f & (i_last_cc+1)) != ts_get_cc(p_ts)) {
WARN("TS Discontinuity");
}
i_last_cc = ts_get_cc(p_ts);
uint8_t *payload;
if(ts_get_transporterror(p_ts)) {
WARN("TS Packed error!");
payload = p_ts + TS_HEADER_SIZE; // When error is detected, pass the entire payload.
} else {
payload = ts_payload(p_ts);
}
if(offset) {
payload+=offset;
}
if(p_ts+TS_SIZE < payload) {
ERROR("payload is out of ts by %ld bytes", payload-p_ts+TS_SIZE);
break;
}
size_t o_ret = fwrite(payload, p_ts+TS_SIZE-payload, 1, outputfile);
if (o_ret != 1) {
WARN("Can't write output ts");
break;
}
packets++;
} else if(offset < 0) {
uint8_t *payload = &p_ts[TS_HEADER_SIZE + offset];
if(p_ts+TS_SIZE < payload || p_ts > payload) {
ERROR("payload is out of ts by %ld bytes", payload-p_ts+TS_SIZE);
break;
}
size_t o_ret = fwrite(payload, p_ts+TS_SIZE-payload, 1, outputfile);
if (o_ret != 1) {
WARN("Can't write output ts");
break;
}
packets++;
}
} else {
do {
memmove(p_ts, &p_ts[1], TS_SIZE-1);
size_t i_ret = fread(&p_ts[TS_SIZE-1], 1, 1, inputfile);
if (i_ret != 1) {
WARN("Can't read input ts");
break;
}
} while (!ts_validate(p_ts) && !feof(inputfile) && !ferror(inputfile));
}
}
if(packets){
INFO("Successfully read %ld ts-packets", packets);
}
//mainErr:
fclose(inputfile);
fclose(outputfile);
return 0;
}