Browse Source

Cleanup section handling

Georgi Chorbadzhiyski 9 years ago
parent
commit
93fffc2e8e
12 changed files with 59 additions and 82 deletions
  1. 3
    3
      tsdata.h
  2. 3
    4
      tsfuncs_cat.c
  3. 3
    3
      tsfuncs_eit.c
  4. 3
    8
      tsfuncs_eit_desc.c
  5. 3
    3
      tsfuncs_nit.c
  6. 4
    9
      tsfuncs_nit_desc.c
  7. 3
    3
      tsfuncs_pat.c
  8. 4
    9
      tsfuncs_pat_desc.c
  9. 3
    3
      tsfuncs_pmt.c
  10. 3
    3
      tsfuncs_sdt.c
  11. 2
    8
      tsfuncs_sdt_desc.c
  12. 25
    26
      tsfuncs_sections.c

+ 3
- 3
tsdata.h View File

@@ -192,9 +192,6 @@ struct ts_section_header {
192 192
 	uint8_t		last_section_number;
193 193
 
194 194
 	// The variables bellow this line are not in the physical packet
195
-	uint16_t	data_size;						// Section length scaled within one packet
196
-	uint16_t	packet_section_len;				// Section length in the current packet minus - 4 (the CRC)
197
-
198 195
 	int			section_pos;					// Up to this pos the section data has come
199 196
 	int			initialized;					// Set to 1 when whole sectino is initialized
200 197
 
@@ -202,6 +199,9 @@ struct ts_section_header {
202 199
 	uint8_t		*packet_data;					// TS packet(s) that were used to transfer the table.
203 200
 
204 201
 	int			num_packets;					// From how much packets this section is build
202
+
203
+	int			data_len;						// Data size without the CRC32 (4 bytes)
204
+	uint8_t		*data;							// Offset into section_data (where the section data start without the section header)
205 205
 };
206 206
 
207 207
 struct ts_pat_program {

+ 3
- 4
tsfuncs_cat.c View File

@@ -73,15 +73,14 @@ ERROR:
73 73
 }
74 74
 
75 75
 int ts_cat_parse(struct ts_cat *cat) {
76
-	uint8_t *section_data = cat->section_header->section_data + 8; // + 8 to compensate for section table header
77
-	int section_len = cat->section_header->packet_section_len;
76
+	uint8_t *section_data = cat->section_header->data;
77
+	int section_len = cat->section_header->data_len;
78 78
 
79 79
 	/* Handle streams */
80 80
 	uint8_t *stream_data = section_data;
81 81
 	cat->program_info_size = section_len;
82 82
 	cat->program_info = malloc(cat->program_info_size);
83 83
 	memcpy(cat->program_info, stream_data, cat->program_info_size);
84
-//	ts_print_bytes("DEBUG", cat->program_info, cat->program_info_size);
85 84
 	stream_data += cat->program_info_size;
86 85
 
87 86
 	cat->CRC = (cat->CRC << 8) | stream_data[3];
@@ -89,7 +88,7 @@ int ts_cat_parse(struct ts_cat *cat) {
89 88
 	cat->CRC = (cat->CRC << 8) | stream_data[1];
90 89
 	cat->CRC = (cat->CRC << 8) | stream_data[0];
91 90
 
92
-	u_int32_t check_crc = ts_crc32(cat->section_header->section_data, cat->section_header->data_size);
91
+	u_int32_t check_crc = ts_crc32_section(cat->section_header);
93 92
 	if (check_crc != 0) {
94 93
 		ts_LOGf("!!! Wrong cat CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, cat->CRC);
95 94
 		return 0;

+ 3
- 3
tsfuncs_eit.c View File

@@ -87,8 +87,8 @@ ERROR:
87 87
 
88 88
 
89 89
 int ts_eit_parse(struct ts_eit *eit) {
90
-	uint8_t *section_data = eit->section_header->section_data + 8; // + 8 to compensate for section table header
91
-	int section_len = eit->section_header->packet_section_len;
90
+	uint8_t *section_data = eit->section_header->data;
91
+	int section_len = eit->section_header->data_len;
92 92
 
93 93
 	/* Table data (6 bytes) */
94 94
 	eit->transport_stream_id			= (section_data[0] << 8) | section_data[1];	// 11111111 11111111
@@ -141,7 +141,7 @@ int ts_eit_parse(struct ts_eit *eit) {
141 141
 	eit->CRC = (eit->CRC << 8) | stream_data[1];
142 142
 	eit->CRC = (eit->CRC << 8) | stream_data[0];
143 143
 
144
-	u_int32_t check_crc = ts_crc32(eit->section_header->section_data, eit->section_header->data_size);
144
+	u_int32_t check_crc = ts_crc32_section(eit->section_header);
145 145
 	if (check_crc != 0) {
146 146
 		ts_LOGf("!!! Wrong EIT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, eit->CRC);
147 147
 		return 0;

+ 3
- 8
tsfuncs_eit_desc.c View File

@@ -15,12 +15,6 @@ static void ts_eit_regenerate_packet_data(struct ts_eit *eit) {
15 15
 	free(ts_packets);
16 16
 }
17 17
 
18
-static void ts_eit_init_private_variables(struct ts_eit *eit) {
19
-	eit->section_header->data_size          = eit->section_header->section_length + 3;
20
-	eit->section_header->packet_section_len = eit->section_header->data_size - 8 - 4;	// -8 for the section header, -4 for the CRC at the end
21
-	ts_eit_regenerate_packet_data(eit);
22
-}
23
-
24 18
 struct ts_eit *ts_eit_alloc_init(uint16_t service_id, uint16_t transport_stream_id, uint16_t org_network_id, uint8_t table_id, uint8_t sec_number, uint8_t last_sec_number) {
25 19
 	struct ts_eit *eit = ts_eit_alloc();
26 20
 
@@ -49,7 +43,8 @@ struct ts_eit *ts_eit_alloc_init(uint16_t service_id, uint16_t transport_stream_
49 43
 
50 44
 	eit->initialized = 1;
51 45
 
52
-	ts_eit_init_private_variables(eit);
46
+	ts_eit_regenerate_packet_data(eit);
47
+
53 48
 	return eit;
54 49
 }
55 50
 
@@ -99,7 +94,7 @@ static int ts_eit_add_stream(struct ts_eit *eit, uint16_t event_id, uint8_t runn
99 94
 	eit->streams[eit->streams_num] = sinfo;
100 95
 	eit->streams_num++;
101 96
 
102
-	ts_eit_init_private_variables(eit);
97
+	ts_eit_regenerate_packet_data(eit);
103 98
 
104 99
 	return 1;
105 100
 }

+ 3
- 3
tsfuncs_nit.c View File

@@ -85,8 +85,8 @@ ERROR:
85 85
 
86 86
 
87 87
 int ts_nit_parse(struct ts_nit *nit) {
88
-	uint8_t *section_data = nit->section_header->section_data + 8; // + 8 to compensate for section table header
89
-	int section_len = nit->section_header->packet_section_len;
88
+	uint8_t *section_data = nit->section_header->data;
89
+	int section_len = nit->section_header->data_len;
90 90
 
91 91
 	/* Table data (2 bytes) */
92 92
 	nit->reserved1         =  (section_data[0] &~ 0x0F) >> 4;						// xxxx1111
@@ -142,7 +142,7 @@ int ts_nit_parse(struct ts_nit *nit) {
142 142
 	nit->CRC = (nit->CRC << 8) | stream_data[1];
143 143
 	nit->CRC = (nit->CRC << 8) | stream_data[0];
144 144
 
145
-	u_int32_t check_crc = ts_crc32(nit->section_header->section_data, nit->section_header->data_size);
145
+	u_int32_t check_crc = ts_crc32_section(nit->section_header);
146 146
 	if (check_crc != 0) {
147 147
 		ts_LOGf("!!! Wrong NIT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, nit->CRC);
148 148
 		return 0;

+ 4
- 9
tsfuncs_nit_desc.c View File

@@ -15,12 +15,6 @@ static void ts_nit_regenerate_packet_data(struct ts_nit *nit) {
15 15
 	free(ts_packets);
16 16
 }
17 17
 
18
-static void ts_nit_init_private_variables(struct ts_nit *nit) {
19
-	nit->section_header->data_size          = nit->section_header->section_length + 3;
20
-	nit->section_header->packet_section_len = nit->section_header->data_size - 8 - 4;	// -8 for the section header, -4 for the CRC at the end
21
-	ts_nit_regenerate_packet_data(nit);
22
-}
23
-
24 18
 struct ts_nit *ts_nit_alloc_init(uint16_t network_id) {
25 19
 	struct ts_nit *nit = ts_nit_alloc();
26 20
 
@@ -46,7 +40,7 @@ struct ts_nit *ts_nit_alloc_init(uint16_t network_id) {
46 40
 
47 41
 	nit->initialized = 1;
48 42
 
49
-	ts_nit_init_private_variables(nit);
43
+	ts_nit_regenerate_packet_data(nit);
50 44
 
51 45
 	return nit;
52 46
 }
@@ -70,7 +64,8 @@ int ts_nit_add_network_name_descriptor(struct ts_nit *nit, char *network_name) {
70 64
 	nit->network_info = descriptor;
71 65
 	nit->section_header->section_length += nit->network_info_size;
72 66
 
73
-	ts_nit_init_private_variables(nit);
67
+	ts_nit_regenerate_packet_data(nit);
68
+
74 69
 	return 1;
75 70
 }
76 71
 
@@ -102,7 +97,7 @@ static int ts_nit_add_stream(struct ts_nit *nit, uint16_t ts_id, uint16_t org_ne
102 97
 	nit->streams[nit->streams_num] = sinfo;
103 98
 	nit->streams_num++;
104 99
 
105
-	ts_nit_init_private_variables(nit);
100
+	ts_nit_regenerate_packet_data(nit);
106 101
 
107 102
 	return 1;
108 103
 }

+ 3
- 3
tsfuncs_pat.c View File

@@ -82,8 +82,8 @@ ERROR:
82 82
 }
83 83
 
84 84
 int ts_pat_parse(struct ts_pat *pat) {
85
-	uint8_t *section_data = pat->section_header->section_data + 8; // + 8 to compensate for section table header
86
-	int section_len = pat->section_header->packet_section_len;
85
+	uint8_t *section_data = pat->section_header->data;
86
+	int section_len = pat->section_header->data_len;
87 87
 
88 88
 	while (section_len > 0) {
89 89
 		if (pat->programs_num == pat->programs_max) {
@@ -107,7 +107,7 @@ int ts_pat_parse(struct ts_pat *pat) {
107 107
 	pat->CRC = (pat->CRC << 8) | section_data[1];
108 108
 	pat->CRC = (pat->CRC << 8) | section_data[0];
109 109
 
110
-	u_int32_t check_crc = ts_crc32(pat->section_header->section_data, pat->section_header->data_size);
110
+	u_int32_t check_crc = ts_crc32_section(pat->section_header);
111 111
 	if (check_crc != 0) {
112 112
 		ts_LOGf("!!! Wrong PAT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, pat->CRC);
113 113
 		return 0;

+ 4
- 9
tsfuncs_pat_desc.c View File

@@ -15,12 +15,6 @@ static void ts_pat_regenerate_packet_data(struct ts_pat *pat) {
15 15
 	free(ts_packets);
16 16
 }
17 17
 
18
-static void ts_pat_init_private_variables(struct ts_pat *pat) {
19
-	pat->section_header->data_size          = pat->section_header->section_length + 3;
20
-	pat->section_header->packet_section_len = pat->section_header->data_size - 8 - 4;	// -8 for the section header, -4 for the CRC at the end
21
-	ts_pat_regenerate_packet_data(pat);
22
-}
23
-
24 18
 struct ts_pat *ts_pat_alloc_init(uint16_t transport_stream_id) {
25 19
 	struct ts_pat *pat = ts_pat_alloc();
26 20
 
@@ -42,7 +36,7 @@ struct ts_pat *ts_pat_alloc_init(uint16_t transport_stream_id) {
42 36
 
43 37
 	pat->initialized = 1;
44 38
 
45
-	ts_pat_init_private_variables(pat);
39
+	ts_pat_regenerate_packet_data(pat);
46 40
 
47 41
 	return pat;
48 42
 }
@@ -71,7 +65,8 @@ int ts_pat_add_program(struct ts_pat *pat, uint16_t program, uint16_t pat_pid) {
71 65
 	pat->programs[pat->programs_num] = pinfo;
72 66
 	pat->programs_num++;
73 67
 
74
-	ts_pat_init_private_variables(pat);
68
+	ts_pat_regenerate_packet_data(pat);
69
+
75 70
 	return 1;
76 71
 }
77 72
 
@@ -110,7 +105,7 @@ int ts_pat_del_program(struct ts_pat *pat, uint16_t program) {
110 105
 	pat->section_header->section_length -= 4;
111 106
 	pat->programs_num--;
112 107
 
113
-	ts_pat_init_private_variables(pat);
108
+	ts_pat_regenerate_packet_data(pat);
114 109
 
115 110
 	return 1;
116 111
 }

+ 3
- 3
tsfuncs_pmt.c View File

@@ -83,8 +83,8 @@ ERROR:
83 83
 }
84 84
 
85 85
 int ts_pmt_parse(struct ts_pmt *pmt) {
86
-	uint8_t *section_data = pmt->section_header->section_data + 8; // + 8 to compensate for section table header
87
-	int section_len = pmt->section_header->packet_section_len;
86
+	uint8_t *section_data = pmt->section_header->data;
87
+	int section_len = pmt->section_header->data_len;
88 88
 
89 89
 	pmt->reserved1         =  (section_data[0] &~ 0x1F) >> 5;						// xxx11111
90 90
 	pmt->PCR_pid           = ((section_data[0] &~ 0xE0) << 8) | section_data[1];	// 111xxxxx xxxxxxxx
@@ -136,7 +136,7 @@ int ts_pmt_parse(struct ts_pmt *pmt) {
136 136
 	pmt->CRC = (pmt->CRC << 8) | stream_data[1];
137 137
 	pmt->CRC = (pmt->CRC << 8) | stream_data[0];
138 138
 
139
-	u_int32_t check_crc = ts_crc32(pmt->section_header->section_data, pmt->section_header->data_size);
139
+	u_int32_t check_crc = ts_crc32_section(pmt->section_header);
140 140
 	if (check_crc != 0) {
141 141
 		ts_LOGf("!!! Wrong PMT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, pmt->CRC);
142 142
 		return 0;

+ 3
- 3
tsfuncs_sdt.c View File

@@ -83,8 +83,8 @@ ERROR:
83 83
 }
84 84
 
85 85
 int ts_sdt_parse(struct ts_sdt *sdt) {
86
-	uint8_t *section_data = sdt->section_header->section_data + 8; // + 8 to compensate for section table header
87
-	int section_len = sdt->section_header->packet_section_len;
86
+	uint8_t *section_data = sdt->section_header->data;
87
+	int section_len = sdt->section_header->data_len;
88 88
 
89 89
 	// 3 bytes
90 90
 	sdt->original_network_id = (section_data[0] << 8) | section_data[1];
@@ -127,7 +127,7 @@ int ts_sdt_parse(struct ts_sdt *sdt) {
127 127
 	sdt->CRC = (sdt->CRC << 8) | section_data[1];
128 128
 	sdt->CRC = (sdt->CRC << 8) | section_data[0];
129 129
 
130
-	u_int32_t check_crc = ts_crc32(sdt->section_header->section_data, sdt->section_header->data_size);
130
+	u_int32_t check_crc = ts_crc32_section(sdt->section_header);
131 131
 	if (check_crc != 0) {
132 132
 		ts_LOGf("!!! Wrong SDT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, sdt->CRC);
133 133
 		return 0;

+ 2
- 8
tsfuncs_sdt_desc.c View File

@@ -15,12 +15,6 @@ static void ts_sdt_regenerate_packet_data(struct ts_sdt *sdt) {
15 15
 	free(ts_packets);
16 16
 }
17 17
 
18
-static void ts_sdt_init_private_variables(struct ts_sdt *sdt) {
19
-	sdt->section_header->data_size          = sdt->section_header->section_length + 3;
20
-	sdt->section_header->packet_section_len = sdt->section_header->data_size - 8 - 4;	// -8 for the section header, -4 for the CRC at the end
21
-	ts_sdt_regenerate_packet_data(sdt);
22
-}
23
-
24 18
 struct ts_sdt *ts_sdt_alloc_init(uint16_t org_network_id, uint16_t transport_stream_id) {
25 19
 	struct ts_sdt *sdt = ts_sdt_alloc();
26 20
 
@@ -44,7 +38,7 @@ struct ts_sdt *ts_sdt_alloc_init(uint16_t org_network_id, uint16_t transport_str
44 38
 
45 39
 	sdt->initialized = 1;
46 40
 
47
-	ts_sdt_init_private_variables(sdt);
41
+	ts_sdt_regenerate_packet_data(sdt);
48 42
 
49 43
 	return sdt;
50 44
 }
@@ -80,7 +74,7 @@ static int ts_sdt_add_stream(struct ts_sdt *sdt, uint16_t service_id, uint8_t *d
80 74
 	sdt->streams[sdt->streams_num] = sinfo;
81 75
 	sdt->streams_num++;
82 76
 
83
-	ts_sdt_init_private_variables(sdt);
77
+	ts_sdt_regenerate_packet_data(sdt);
84 78
 
85 79
 	return 1;
86 80
 }

+ 25
- 26
tsfuncs_sections.c View File

@@ -33,27 +33,24 @@ uint8_t *ts_section_header_parse(uint8_t *ts_packet, struct ts_header *ts_header
33 33
 	if (ts_section_header->table_id == 0x72)
34 34
 		return NULL;
35 35
 
36
-	ts_section_header->ts_id_number             = (data[3] << 8) | data[4]; // xxxxxxx xxxxxxx
36
+	if (ts_section_header->section_syntax_indicator) {
37
+		ts_section_header->ts_id_number             = (data[3] << 8) | data[4]; // xxxxxxx xxxxxxx
37 38
 
38
-	ts_section_header->reserved2                = data[5] >> 6;				// xx111111
39
-	ts_section_header->version_number           = (data[5] &~ 0xC1) >> 1;	// 11xxxxx1
40
-	ts_section_header->current_next_indicator   = data[5] &~ 0xFE;			// 1111111x
39
+		ts_section_header->reserved2                = data[5] >> 6;				// xx111111
40
+		ts_section_header->version_number           = (data[5] &~ 0xC1) >> 1;	// 11xxxxx1
41
+		ts_section_header->current_next_indicator   = data[5] &~ 0xFE;			// 1111111x
41 42
 
42
-	ts_section_header->section_number           = data[6];
43
-	ts_section_header->last_section_number      = data[7];
43
+		ts_section_header->section_number           = data[6];
44
+		ts_section_header->last_section_number      = data[7];
44 45
 
45
-	if (!ts_section_header->section_syntax_indicator) {
46
-		ts_LOGf("!!! Table 0x%02x have no section_syntax_indicator set!\n",
47
-			ts_section_header->table_id);
48
-		ts_packet_header_dump(ts_header);
49
-		ts_section_header_dump(ts_section_header);
50
-		return NULL;
46
+		ts_section_header->data = ts_section_header->section_data + 8; // Skip header
47
+		ts_section_header->data_len = ts_section_header->section_length - (5 + 4);	// 5 for extended syntax, 4 for crc at the end
48
+		return data + 8;
49
+	} else {
50
+		ts_section_header->data = ts_section_header->section_data + 3; // Skip header
51
+		ts_section_header->data_len = ts_section_header->section_length;
52
+		return data + 3;
51 53
 	}
52
-
53
-	ts_section_header->data_size = ts_section_header->section_length + 3;
54
-	ts_section_header->packet_section_len = ts_section_header->data_size - 8 - 4;	// -8 for the section header, -4 for the CRC at the end
55
-
56
-	return data + 8;
57 54
 }
58 55
 
59 56
 void ts_section_header_generate(uint8_t *ts_packet, struct ts_section_header *ts_section_header, uint8_t start) {
@@ -118,15 +115,17 @@ void ts_section_header_dump(struct ts_section_header *t) {
118 115
 		t->table_id == 0xff         ? "reserved" : "Impossible!"
119 116
 	);
120 117
 	ts_LOGf("    - Section length     : %03x (%d)\n", t->section_length, t->section_length);
121
-	ts_LOGf("    - TS ID / Program No : %04x (%d)\n", t->ts_id_number, t->ts_id_number);
122
-	ts_LOGf("    - Version number %d, current next %d, section number %d, last section number %d\n",
123
-			t->version_number,
124
-			t->current_next_indicator,
125
-			t->section_number,
126
-			t->last_section_number);
127
-	ts_LOGf("    - Private vars       : data_size:%d packet_section_len:%d num_packets:%d section_pos:%d\n",
128
-			t->data_size,
129
-			t->packet_section_len,
118
+	if (!t->section_syntax_indicator) {
119
+		ts_LOGf("    - Private section syntax\n");
120
+	} else {
121
+		ts_LOGf("    - TS ID / Program No : %04x (%d)\n", t->ts_id_number, t->ts_id_number);
122
+		ts_LOGf("    - Version number %d, current next %d, section number %d, last section number %d\n",
123
+				t->version_number,
124
+				t->current_next_indicator,
125
+				t->section_number,
126
+				t->last_section_number);
127
+	}
128
+	ts_LOGf("    - Private vars       : num_packets:%d section_pos:%d\n",
130 129
 			t->num_packets,
131 130
 			t->section_pos);
132 131
 }

Loading…
Cancel
Save