File: pmt.c

package info (click to toggle)
tsdecrypt 10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,288 kB
  • sloc: ansic: 14,377; makefile: 245; sh: 166
file content (300 lines) | stat: -rw-r--r-- 9,428 bytes parent folder | download | duplicates (6)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * PMT table parser and generator
 * Copyright (C) 2010-2011 Unix Solutions Ltd.
 *
 * Released under MIT license.
 * See LICENSE-MIT.txt for license terms.
 */
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>

#include "tsfuncs.h"

struct ts_pmt *ts_pmt_alloc() {
	struct ts_pmt *pmt = calloc(1, sizeof(struct ts_pmt));
	pmt->section_header	= ts_section_data_alloc();
	pmt->streams_max	= 128;
	pmt->streams		= calloc(pmt->streams_max, sizeof(void *));
	return pmt;
}

static void ts_pmt_streams_data_free(struct ts_pmt *pmt) {
	int i;
	for (i=0;i<pmt->streams_num;i++) {
		if (pmt->streams[i]) {
			FREE(pmt->streams[i]->ES_info);
			FREE(pmt->streams[i]);
		}
	}
}

void ts_pmt_clear(struct ts_pmt *pmt) {
	if (!pmt)
		return;
	// save
	struct ts_section_header *section_header = pmt->section_header;
	struct ts_pmt_stream **streams = pmt->streams;
	int streams_max = pmt->streams_max;
	// free
	FREE(pmt->program_info);
	ts_pmt_streams_data_free(pmt);
	// clear
	ts_section_data_clear(section_header);
	memset(pmt, 0, sizeof(struct ts_pmt));
	// restore
	pmt->section_header = section_header;
	pmt->streams = streams;
	pmt->streams_max = streams_max;
}


void ts_pmt_free(struct ts_pmt **ppmt) {
	struct ts_pmt *pmt = *ppmt;
	if (pmt) {
		ts_section_data_free(&pmt->section_header);
		ts_pmt_streams_data_free(pmt);
		FREE(pmt->program_info);
		FREE(pmt->streams);
		FREE(*ppmt);
	}
}

struct ts_pmt *ts_pmt_push_packet(struct ts_pmt *pmt, uint8_t *ts_packet) {
	struct ts_header ts_header;
	memset(&ts_header, 0, sizeof(struct ts_header));

	if (ts_packet_header_parse(ts_packet, &ts_header)) {
		// Received PUSI packet before table END, clear the table to start gathering new one
		if (ts_header.pusi && pmt->ts_header.pusi)
			ts_pmt_clear(pmt);
		if (!pmt->ts_header.pusi)
			pmt->ts_header = ts_header;
	}

	if (ts_header.pusi) {
		struct ts_section_header section_header;
		memset(&section_header, 0, sizeof(struct ts_section_header));

		uint8_t *section_data = ts_section_header_parse(ts_packet, &pmt->ts_header, &section_header);
		if (!section_data) {
			memset(&pmt->ts_header, 0, sizeof(struct ts_header));
			goto OUT;
		}
		// table_id should be 0x02 (program_map_section)
		if (section_header.table_id != 0x02) {
			memset(&pmt->ts_header, 0, sizeof(struct ts_header));
			goto OUT;
		}

		// Set correct section_header
		ts_section_header_parse(ts_packet, &pmt->ts_header, pmt->section_header);
	}

	if (!pmt->initialized) {
		ts_section_add_packet(pmt->section_header, &ts_header, ts_packet);
		if (pmt->section_header->initialized) {
			if (!ts_pmt_parse(pmt))
				goto ERROR;
		}
	}

OUT:
	return pmt;

ERROR:
	ts_pmt_clear(pmt);
	return pmt;
}

int ts_pmt_parse(struct ts_pmt *pmt) {
	uint8_t *section_data = pmt->section_header->data;
	int section_len = pmt->section_header->data_len;

	pmt->reserved1         =  (section_data[0] &~ 0x1F) >> 5;						// xxx11111
	pmt->PCR_pid           = ((section_data[0] &~ 0xE0) << 8) | section_data[1];	// 111xxxxx xxxxxxxx

	pmt->reserved2         =  (section_data[2] &~ 0x0F) >> 4;						// xxxx1111
	pmt->program_info_size = ((section_data[2] &~ 0xF0) << 8) | section_data[3];	// 1111xxxx xxxxxxxx

	/* Handle streams */
	uint8_t *stream_data = section_data + 4 + pmt->program_info_size;	// +4 is to compensate for reserved1,PCR,reserved2,program_info_size
	int stream_len       = section_len - pmt->program_info_size - 4;		// -4 for the CRC at the end

	pmt->program_info = NULL;
	if (pmt->program_info_size) {
		pmt->program_info = malloc(pmt->program_info_size);
		if (pmt->program_info) {
			memcpy(pmt->program_info, stream_data - pmt->program_info_size, pmt->program_info_size);
		}
	}

	while (stream_len > 0) {
		if (pmt->streams_num == pmt->streams_max) {
			ts_LOGf("PMT contains too many streams (>%d), not all are initialized!\n", pmt->streams_max);
			break;
		}

		struct ts_pmt_stream *sinfo = calloc(1, sizeof(struct ts_pmt_stream));

		sinfo->stream_type  = stream_data[0];

		sinfo->reserved1    =  (stream_data[1] &~ 0x1F) >> 5;					// xxx11111
		sinfo->pid          = ((stream_data[1] &~ 0xE0) << 8) | stream_data[2];	// 111xxxxx xxxxxxxx

		sinfo->reserved2    =  (stream_data[3] &~ 0x0F) >> 4;					// xxxx1111
		sinfo->ES_info_size = ((stream_data[3] &~ 0xF0) << 8) | stream_data[4];	// 1111xxxx xxxxxxxx

		sinfo->ES_info      = NULL;
		if (sinfo->ES_info_size > 0) {
			sinfo->ES_info = malloc(sinfo->ES_info_size);
			memcpy(sinfo->ES_info, &stream_data[5], sinfo->ES_info_size);
		}
		pmt->streams[pmt->streams_num] = sinfo;
		pmt->streams_num++;

		stream_data += 5 + sinfo->ES_info_size;
		stream_len  -= 5 + sinfo->ES_info_size;
	}

	if (!ts_crc32_section_check(pmt->section_header, "PMT"))
		return 0;

	pmt->initialized = 1;
	return 1;
}

void ts_pmt_generate(struct ts_pmt *pmt, uint8_t **ts_packets, int *num_packets) {
	uint8_t *secdata = ts_section_data_alloc_section();
	ts_section_header_generate(secdata, pmt->section_header, 0);
	int curpos = 8; // Compensate for the section header, frist data byte is at offset 8

	secdata[curpos + 0]  = pmt->reserved1 << 5;			// xxx11111
	secdata[curpos + 0] |= pmt->PCR_pid >> 8;			// 111xxxxx xxxxxxxx
	secdata[curpos + 1]  = pmt->PCR_pid &~ 0xff00;

	secdata[curpos + 2]  = pmt->reserved2 << 4;			// xxxx1111
	secdata[curpos + 2] |= pmt->program_info_size >> 8;	// 1111xxxx xxxxxxxx
	secdata[curpos + 3]  = pmt->program_info_size &~ 0xff00;
	curpos += 4; // For thje fields above

	if (pmt->program_info_size) {
		memcpy(secdata + curpos, pmt->program_info, pmt->program_info_size);
		curpos += pmt->program_info_size;
	}

	int i;
	for(i=0;i<pmt->streams_num;i++) {
		struct ts_pmt_stream *stream = pmt->streams[i];
		secdata[curpos + 0] = stream->stream_type;

		secdata[curpos + 1]  = stream->reserved1 << 5;		// xxx11111
		secdata[curpos + 1] |= stream->pid >> 8;			// 111xxxxx xxxxxxxx
		secdata[curpos + 2]  = stream->pid &~ 0xff00;

		secdata[curpos + 3]  = stream->reserved2 << 4;		// xxxx1111
		secdata[curpos + 3] |= stream->ES_info_size >> 8;	// 1111xxxx xxxxxxxx
		secdata[curpos + 4]  = stream->ES_info_size &~ 0xff00;
		curpos += 5; // Compensate for the above

		if (stream->ES_info_size > 0) {
			memcpy(secdata + curpos, stream->ES_info, stream->ES_info_size);
			curpos += stream->ES_info_size;
		}
	}
    pmt->section_header->CRC = ts_section_data_calculate_crc(secdata, curpos);
    curpos += 4; // CRC

    ts_section_data_gen_ts_packets(&pmt->ts_header, secdata, curpos, pmt->section_header->pointer_field, ts_packets, num_packets);

    FREE(secdata);
}

void ts_pmt_regenerate_packets(struct ts_pmt *pmt) {
	uint8_t *ts_packets;
	int num_packets;
	ts_pmt_generate(pmt, &ts_packets, &num_packets);
	FREE(pmt->section_header->packet_data);
	pmt->section_header->packet_data = ts_packets;
	pmt->section_header->num_packets = num_packets;
}

struct ts_pmt *ts_pmt_copy(struct ts_pmt *pmt) {
	struct ts_pmt *newpmt = ts_pmt_alloc();
	int i;
	for (i=0;i<pmt->section_header->num_packets; i++) {
		newpmt = ts_pmt_push_packet(newpmt, pmt->section_header->packet_data + (i * TS_PACKET_SIZE));
	}
	if (newpmt->initialized) {
		return newpmt;
	} else {
		ts_LOGf("Error copying PMT!\n");
		ts_pmt_free(&newpmt);
		return NULL;
	}
}

void ts_pmt_check_generator(struct ts_pmt *pmt) {
	struct ts_pmt *pmt1 = ts_pmt_copy(pmt);
	if (pmt1) {
		ts_compare_data("PMT (tspacket->struct)",
			pmt1->section_header->packet_data,
			pmt->section_header->packet_data,
			pmt->section_header->num_packets * TS_PACKET_SIZE);
		ts_pmt_free(&pmt1);
	}

	uint8_t *ts_packets;
	int num_packets;
	ts_pmt_generate(pmt, &ts_packets, &num_packets);
	if (num_packets != pmt->section_header->num_packets) {
		ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, pmt->section_header->num_packets);
	}
	ts_compare_data("PMT (struct->tspacket)", pmt->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
	free(ts_packets);
}

void ts_pmt_dump(struct ts_pmt *pmt) {
	struct ts_section_header *sec = pmt->section_header;
	int i;

	ts_section_dump(sec);

	ts_LOGf("  * PMT data\n");
	ts_LOGf("    * PID         : %04x (%d)\n", pmt->ts_header.pid, pmt->ts_header.pid);
	ts_LOGf("    * reserved1   : %d\n", pmt->reserved1);
	ts_LOGf("    * PCR PID     : %04x (%d)\n", pmt->PCR_pid, pmt->PCR_pid);
	ts_LOGf("    * reserved2   : %d\n", pmt->reserved2);
	ts_LOGf("    * program_len : %d\n", pmt->program_info_size);
	ts_LOGf("    * num_streams : %d\n", pmt->streams_num);

	if (pmt->program_info_size > 0) {
		ts_LOGf("  * Program info:\n");
		ts_LOGf("      * program info size: %d\n", pmt->program_info_size);
		ts_descriptor_dump(pmt->program_info, pmt->program_info_size);
	}

	for(i=0;i<pmt->streams_num;i++) {
		struct ts_pmt_stream *stream = pmt->streams[i];
		ts_LOGf("    * [%02d/%02d] PID %04x (%d) -> Stream type: 0x%02x (%d) /es_info_size: %d/ %s\n",
			i+1, pmt->streams_num,
			stream->pid, stream->pid,
			stream->stream_type, stream->stream_type,
			stream->ES_info_size,
			h222_stream_type_desc(stream->stream_type)
		);
		if (stream->ES_info) {
			ts_descriptor_dump(stream->ES_info, stream->ES_info_size);
		}
	}

	ts_pmt_check_generator(pmt);
}

int ts_pmt_is_same(struct ts_pmt *pmt1, struct ts_pmt *pmt2) {
	if (pmt1 == pmt2) return 1; // Same
	if ((!pmt1 && pmt2) || (pmt1 && !pmt2)) return 0; // Not same (one is NULL)
	return ts_section_is_same(pmt1->section_header, pmt2->section_header);
}