-
Notifications
You must be signed in to change notification settings - Fork 18
/
na2ts.c
126 lines (107 loc) · 3.34 KB
/
na2ts.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
#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=0x0;
int i_last_cc = -1;
static const struct option long_options[] = {
{ "pid", required_argument, NULL, 'p' },
{ "offset (-3:only TS-sync byte; 12:pid mode)", 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 != -3 && offset != 12) {
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]);
}
}
if (offset == -3) {
INFO("Encapsulation mode: only 0x47 TS-Sync byte at each 188 bytes");
pid = 0;
} else if (offset == 12) {
INFO("Encapsulation mode: payload over pid 0x%04x (%d)", pid, pid);
}
offset += 4;
unsigned long int packets=0;
while (!feof(inputfile) && !ferror(inputfile)) {
uint8_t p_ts[TS_SIZE];
if (offset == 1) {
p_ts[0] = 0x47;
} else {
ts_pad(p_ts);
}
size_t i_ret = fread(p_ts+offset, TS_SIZE - offset, 1, inputfile);
if (i_ret != 1) {
WARN("Can't read input ts");
break;
}
if(offset >= 16) {
ts_set_pid(p_ts, pid);
ts_set_cc(p_ts, ++i_last_cc);
}
size_t o_ret = fwrite(p_ts, TS_SIZE, 1, outputfile);
if (o_ret != 1) {
WARN("Can't write output ts");
break;
}
packets++;
}
if(packets){
INFO("Successfully writed %ld ts-packets", packets);
}
//mainErr:
fclose(inputfile);
fclose(outputfile);
return 0;
}