Skip to content

Commit 5b4c010

Browse files
committed
put all buffer data into one struct
1 parent 077af25 commit 5b4c010

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/ts.c

100755100644
+34-28
Original file line numberDiff line numberDiff line change
@@ -70,61 +70,67 @@ static int mpegts_probe(unsigned char *buf, int buf_size)
7070
return -1;
7171
}
7272

73+
struct section_parser
74+
{
75+
int total_len;
76+
uint8_t cc;
77+
uint8_t buffer[65535];
78+
int32_t limit_len;
79+
};
80+
7381
// do memcpy if section length greater than one packet
7482
int16_t section_preproc(uint16_t pid, uint8_t *pkt, uint16_t len, uint8_t **buffering,
7583
uint8_t payload_unit_start_indicator, uint8_t continuity_counter, uint8_t psi_or_pes)
7684
{
77-
static int total_len[8192] = { 0 };
78-
static uint8_t cc[8192] = { 0 };
79-
static uint8_t buffer[8192][65535];
80-
static int32_t limit_len[8192] = { 0 };
85+
static struct section_parser sec[8192];
8186
uint8_t pointer_field = 0;
87+
struct section_parser *p = &sec[pid];
8288
*buffering = NULL;
8389
// indicate start of PES or PSI
8490
if (payload_unit_start_indicator == 1) {
85-
cc[pid] = continuity_counter;
91+
p->cc = continuity_counter;
8692
if (psi_or_pes == 0) // PSI
8793
{
8894
pointer_field = pkt[0];
8995
/*skip pointer_field, valid for PSI and stream_type 0x05 private_sections*/
90-
total_len[pid] = len - 1 - pointer_field;
91-
limit_len[pid] = (int32_t)(((int16_t)pkt[2 + pointer_field] << 8) | pkt[3 + pointer_field]) & 0x0FFF;
92-
limit_len[pid] += 3;
96+
p->total_len = len - 1 - pointer_field;
97+
p->limit_len = (int32_t)(((int16_t)pkt[2 + pointer_field] << 8) | pkt[3 + pointer_field]) & 0x0FFF;
98+
p->limit_len += 3;
9399
/*section in one pkt , go without buffering*/
94-
if (limit_len[pid] <= (total_len[pid])) {
100+
if (p->limit_len <= (p->total_len)) {
95101
*buffering = (pkt + 1 + pointer_field);
96-
return limit_len[pid];
102+
return p->limit_len;
97103
} else {
98-
memset(buffer[pid], 0, 4096); // PSI length less than this
99-
memcpy(buffer[pid], pkt + 1 + pointer_field, total_len[pid]);
104+
memset(p->buffer, 0, 4096); // PSI length less than this
105+
memcpy(p->buffer, pkt + 1 + pointer_field, p->total_len);
100106
}
101-
} else {
102-
total_len[pid] = len;
103-
limit_len[pid] = ((int32_t)pkt[4] << 8 | pkt[5]);
104-
if (limit_len[pid] == 0) {
107+
} else { // PES
108+
p->total_len = len;
109+
p->limit_len = ((int32_t)pkt[4] << 8 | pkt[5]);
110+
if (p->limit_len == 0) {
105111
// for video stream, unlimit length
106112
}
107-
limit_len[pid] += 6;
108-
if (limit_len[pid] <= (total_len[pid])) {
113+
p->limit_len += 6;
114+
if (p->limit_len <= (p->total_len)) {
109115
*buffering = pkt;
110-
return limit_len[pid];
116+
return p->limit_len;
111117
} else {
112-
memset(buffer[pid], 0, 65535);
113-
memcpy(buffer[pid], pkt, total_len[pid]);
118+
memset(p->buffer, 0, 65535);
119+
memcpy(p->buffer, pkt, p->total_len);
114120
}
115121
}
116122

117123
} else {
118124
// if(psi_or_pes==0)//PSI
119125
{
120-
if (total_len[pid] == 0)
126+
if (p->total_len == 0)
121127
return -1;
122-
memcpy(buffer[pid] + total_len[pid], pkt, len);
123-
total_len[pid] += len;
124-
if (total_len[pid] >= limit_len[pid]) {
125-
*buffering = buffer[pid];
126-
total_len[pid] = 0;
127-
return limit_len[pid];
128+
memcpy(p->buffer + p->total_len, pkt, len);
129+
p->total_len += len;
130+
if (p->total_len >= p->limit_len) {
131+
*buffering = p->buffer;
132+
p->total_len = 0;
133+
return p->limit_len;
128134
}
129135
}
130136
}

0 commit comments

Comments
 (0)