1 /*
2  * NIT table parser and generator
3  * Copyright (C) 2010-2011 Unix Solutions Ltd.
4  *
5  * Released under MIT license.
6  * See LICENSE-MIT.txt for license terms.
7  */
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <netdb.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "tsfuncs.h"
15 
ts_nit_alloc()16 struct ts_nit *ts_nit_alloc() {
17 	struct ts_nit *nit = calloc(1, sizeof(struct ts_nit));
18 	nit->section_header = ts_section_data_alloc();
19 	nit->streams_max = 128;
20 	nit->streams = calloc(nit->streams_max, sizeof(void *));
21 	return nit;
22 }
23 
ts_nit_streams_data_free(struct ts_nit * nit)24 static void ts_nit_streams_data_free(struct ts_nit *nit) {
25 	int i;
26 	for (i=0;i<nit->streams_num;i++) {
27 		if (nit->streams[i]) {
28 			FREE(nit->streams[i]->descriptor_data);
29 			FREE(nit->streams[i]);
30 		}
31 	}
32 }
33 
ts_nit_clear(struct ts_nit * nit)34 void ts_nit_clear(struct ts_nit *nit) {
35 	if (!nit)
36 		return;
37 	// save
38 	struct ts_section_header *section_header = nit->section_header;
39 	struct ts_nit_stream **streams = nit->streams;
40 	int streams_max = nit->streams_max;
41 	// free
42 	FREE(nit->network_info);
43 	ts_nit_streams_data_free(nit);
44 	// clear
45 	ts_section_data_clear(section_header);
46 	memset(nit, 0, sizeof(struct ts_nit));
47 	// restore
48 	nit->section_header = section_header;
49 	nit->streams = streams;
50 	nit->streams_max = streams_max;
51 }
52 
ts_nit_free(struct ts_nit ** pnit)53 void ts_nit_free(struct ts_nit **pnit) {
54 	struct ts_nit *nit = *pnit;
55 	if (nit) {
56 		ts_section_data_free(&nit->section_header);
57 		FREE(nit->network_info);
58 		ts_nit_streams_data_free(nit);
59 		FREE(nit->streams);
60 		FREE(*pnit);
61 	}
62 }
63 
ts_nit_push_packet(struct ts_nit * nit,uint8_t * ts_packet)64 struct ts_nit *ts_nit_push_packet(struct ts_nit *nit, uint8_t *ts_packet) {
65 	struct ts_header ts_header;
66 	memset(&ts_header, 0, sizeof(struct ts_header));
67 
68 	if (ts_packet_header_parse(ts_packet, &ts_header)) {
69 		// NIT should be with PID 0x10
70 		if (ts_header.pid != 0x10)
71 			goto OUT;
72 		// Received PUSI packet before table END, clear the table to start gathering new one
73 		if (ts_header.pusi && nit->ts_header.pusi)
74 			ts_nit_clear(nit);
75 		if (!nit->ts_header.pusi)
76 			nit->ts_header = ts_header;
77 	}
78 
79 	if (ts_header.pusi) {
80 		struct ts_section_header section_header;
81 		memset(&section_header, 0, sizeof(struct ts_section_header));
82 
83 		uint8_t *section_data = ts_section_header_parse(ts_packet, &nit->ts_header, &section_header);
84 		if (!section_data)
85 			goto OUT;
86 		// table_id should be 0x40 (network_information_section - actual_network)
87 		if (section_header.table_id != 0x40) {
88 			memset(&nit->ts_header, 0, sizeof(struct ts_header));
89 			goto OUT;
90 		}
91 
92 		// Set correct section_header
93 		ts_section_header_parse(ts_packet, &nit->ts_header, nit->section_header);
94 	}
95 
96 	if (!nit->initialized) {
97 		ts_section_add_packet(nit->section_header, &ts_header, ts_packet);
98 		if (nit->section_header->initialized) {
99 			if (!ts_nit_parse(nit))
100 				goto ERROR;
101 		}
102 	}
103 
104 OUT:
105 	return nit;
106 
107 ERROR:
108 	ts_nit_clear(nit);
109 	return nit;
110 }
111 
112 
ts_nit_parse(struct ts_nit * nit)113 int ts_nit_parse(struct ts_nit *nit) {
114 	uint8_t *section_data = nit->section_header->data;
115 	int section_len = nit->section_header->data_len;
116 
117 	/* Table data (2 bytes) */
118 	nit->reserved1         =  (section_data[0] &~ 0x0F) >> 4;						// xxxx1111
119 	nit->network_info_size = ((section_data[0] &~ 0xF0) << 8) | section_data[1];	// 1111xxxx xxxxxxxx
120 
121 	/* Handle streams */
122 	uint8_t *stream_data = section_data + 2 + nit->network_info_size;	// +2 is to compensate for reserved1 and network_info_size
123 	int stream_len       = section_len - nit->network_info_size - 4;	// -4 for the CRC at the end
124 
125 	nit->network_info = NULL;
126 	if (nit->network_info_size) {
127 		nit->network_info = malloc(nit->network_info_size);
128 		if (nit->network_info) {
129 			memcpy(nit->network_info, stream_data - nit->network_info_size, nit->network_info_size);
130 		}
131 	}
132 
133 	// Before the table there are two more fields
134 	nit->reserved2    =  (stream_data[0] &~ 0x0F) >> 4;						// xxxx1111
135 	nit->ts_loop_size = ((stream_data[0] &~ 0xF0) << 8) | stream_data[1];	// 1111xxxx xxxxxxxx
136 
137 	stream_data += 2;
138 	stream_len   = nit->ts_loop_size;
139 
140 	while (stream_len > 0) {
141 		if (nit->streams_num == nit->streams_max) {
142 			ts_LOGf("!!! Too many streams in NIT, max %d\n", nit->streams_max);
143 			break;
144 		}
145 
146 		struct ts_nit_stream *sinfo = calloc(1, sizeof(struct ts_nit_stream));
147 
148 		sinfo->transport_stream_id = (stream_data[0] << 8) | stream_data[1];
149 		sinfo->original_network_id = (stream_data[2] << 8) | stream_data[3];
150 
151 		sinfo->reserved1           =  (stream_data[4] &~ 0x0F) >> 4;					// xxxx1111
152 		sinfo->descriptor_size     = ((stream_data[4] &~ 0xF0) << 8) | stream_data[5];	// 1111xxxx xxxxxxxx
153 
154 		sinfo->descriptor_data      = NULL;
155 		if (sinfo->descriptor_size > 0) {
156 			sinfo->descriptor_data = malloc(sinfo->descriptor_size);
157 			memcpy(sinfo->descriptor_data, &stream_data[6], sinfo->descriptor_size);
158 		}
159 		nit->streams[nit->streams_num] = sinfo;
160 		nit->streams_num++;
161 
162 		stream_data += 6 + sinfo->descriptor_size;
163 		stream_len  -= 6 + sinfo->descriptor_size;
164 	}
165 
166 	if (!ts_crc32_section_check(nit->section_header, "NIT"))
167 		return 0;
168 
169 	nit->initialized = 1;
170 	return 1;
171 }
172 
ts_nit_generate(struct ts_nit * nit,uint8_t ** ts_packets,int * num_packets)173 void ts_nit_generate(struct ts_nit *nit, uint8_t **ts_packets, int *num_packets) {
174 	uint8_t *secdata = ts_section_data_alloc_section();
175 	ts_section_header_generate(secdata, nit->section_header, 0);
176 	int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
177 
178 	secdata[curpos + 0]  = nit->reserved1 << 4;			// xxxx1111
179 	secdata[curpos + 0] |= nit->network_info_size >> 8;	// 1111xxxx xxxxxxxx
180 	secdata[curpos + 1]  = nit->network_info_size &~ 0xff00;
181 	curpos += 2; // For the fields above
182 
183 	if (nit->network_info_size) {
184 		memcpy(secdata + curpos, nit->network_info, nit->network_info_size);
185 		curpos += nit->network_info_size;
186 	}
187 
188 	// Before the table there are two more fields
189 	secdata[curpos + 0]  = nit->reserved2 << 4;			// xxxx1111
190 	secdata[curpos + 0] |= nit->ts_loop_size >> 8;		// 1111xxxx xxxxxxxx
191 	secdata[curpos + 1]  = nit->ts_loop_size &~ 0xff00;
192 	curpos += 2; // For the fields above
193 
194 	int i;
195 	for(i=0;i<nit->streams_num;i++) {
196 		struct ts_nit_stream *stream = nit->streams[i];
197 
198 		secdata[curpos + 0]  = stream->transport_stream_id >> 8;			// xxxxxxxx xxxxxxxx
199 		secdata[curpos + 1]  = stream->transport_stream_id &~ 0xff00;
200 
201 		secdata[curpos + 2]  = stream->original_network_id >> 8;			// xxxxxxxx xxxxxxxx
202 		secdata[curpos + 3]  = stream->original_network_id &~ 0xff00;
203 
204 		secdata[curpos + 4]  = stream->reserved1 << 4;						// xxxx1111
205 		secdata[curpos + 4] |= stream->descriptor_size >> 8;				// 1111xxxx xxxxxxxx
206 
207 		secdata[curpos + 5]  = stream->descriptor_size &~ 0xff00;
208 
209 		curpos += 6; // Compensate for the above
210 
211 		if (stream->descriptor_size > 0) {
212 			memcpy(secdata + curpos, stream->descriptor_data, stream->descriptor_size);
213 			curpos += stream->descriptor_size;
214 		}
215 	}
216 	nit->section_header->CRC = ts_section_data_calculate_crc(secdata, curpos);
217 	curpos += 4; // CRC
218 
219 	ts_section_data_gen_ts_packets(&nit->ts_header, secdata, curpos, nit->section_header->pointer_field, ts_packets, num_packets);
220 
221 	FREE(secdata);
222 }
223 
ts_nit_copy(struct ts_nit * nit)224 struct ts_nit *ts_nit_copy(struct ts_nit *nit) {
225 	struct ts_nit *newnit = ts_nit_alloc();
226 	int i;
227 	for (i=0;i<nit->section_header->num_packets; i++) {
228 		newnit = ts_nit_push_packet(newnit, nit->section_header->packet_data + (i * TS_PACKET_SIZE));
229 	}
230 	if (newnit->initialized) {
231 		return newnit;
232 	} else {
233 		ts_LOGf("Error copying nit!\n");
234 		ts_nit_free(&newnit);
235 		return NULL;
236 	}
237 }
238 
ts_nit_check_generator(struct ts_nit * nit)239 void ts_nit_check_generator(struct ts_nit *nit) {
240 	struct ts_nit *nit1 = ts_nit_alloc();
241 	int i;
242 	for (i=0;i<nit->section_header->num_packets;i++) {
243 		nit1 = ts_nit_push_packet(nit1, nit->section_header->packet_data + (i * TS_PACKET_SIZE));
244 	}
245 	ts_compare_data("NIT (tspacket->struct)",
246 		nit1->section_header->packet_data,
247 		nit->section_header->packet_data,
248 		nit->section_header->num_packets * TS_PACKET_SIZE);
249 	ts_nit_free(&nit1);
250 
251 	uint8_t *ts_packets;
252 	int num_packets;
253 	ts_nit_generate(nit, &ts_packets, &num_packets);
254 	if (num_packets != nit->section_header->num_packets) {
255 		ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, nit->section_header->num_packets);
256 	}
257 	ts_compare_data("NIT (struct->tspacket)", nit->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
258 	free(ts_packets);
259 }
260 
ts_nit_dump(struct ts_nit * nit)261 void ts_nit_dump(struct ts_nit *nit) {
262 	struct ts_section_header *sec = nit->section_header;
263 	int i;
264 
265 	ts_section_dump(sec);
266 
267 	ts_LOGf("  * NIT data\n");
268 	ts_LOGf("    * PID         : 0x%04x (%d)\n", nit->ts_header.pid, nit->ts_header.pid);
269 	ts_LOGf("    * reserved1   : 0x%02x\n", nit->reserved1);
270 	ts_LOGf("    * network_len : 0x%02x (%d)\n", nit->network_info_size, nit->network_info_size);
271 	ts_LOGf("    * reserved2   : 0x%02x\n", nit->reserved1);
272 	ts_LOGf("    * ts_loop_len : %d\n", nit->ts_loop_size);
273 	ts_LOGf("    * num_streams : %d\n", nit->streams_num);
274 
275 	if (nit->network_info_size > 0) {
276 		ts_LOGf("  * Network info:\n");
277 		ts_LOGf("      * network info size: %d\n", nit->network_info_size);
278 		ts_descriptor_dump(nit->network_info, nit->network_info_size);
279 	}
280 
281 	for(i=0;i<nit->streams_num;i++) {
282 		struct ts_nit_stream *stream = nit->streams[i];
283 		ts_LOGf("    - [%02d/%02d] | TS_id: 0x%04x (%d) ORG_net_id: 0x%04x (%d) Reserved: 0x%0x Desc_size: %d\n",
284 			i+1, nit->streams_num,
285 			stream->transport_stream_id, stream->transport_stream_id,
286 			stream->original_network_id, stream->original_network_id,
287 			stream->reserved1,
288 			stream->descriptor_size);
289 		if (stream->descriptor_data) {
290 			ts_descriptor_dump(stream->descriptor_data, stream->descriptor_size);
291 		}
292 	}
293 
294 	ts_nit_check_generator(nit);
295 }
296 
ts_nit_is_same(struct ts_nit * nit1,struct ts_nit * nit2)297 int ts_nit_is_same(struct ts_nit *nit1, struct ts_nit *nit2) {
298 	if (nit1 == nit2) return 1; // Same
299 	if ((!nit1 && nit2) || (nit1 && !nit2)) return 0; // Not same (one is NULL)
300 	return ts_section_is_same(nit1->section_header, nit2->section_header);
301 }
302