-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathh264rtp.c
executable file
·414 lines (341 loc) · 11.8 KB
/
h264rtp.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <netinet/in.h>
#include "vsh264.h"
/* preform a bubble sort on the linked list, reference happycodings.com */
void bubblesortAggregateNALDON(struct AggregateNAL** head) {
struct AggregateNAL *a = NULL;
struct AggregateNAL *b = NULL;
struct AggregateNAL *c = NULL;
struct AggregateNAL *e = NULL;
struct AggregateNAL *tmp = NULL;
/*
// the `c' node precedes the `a' and `e' node
// pointing up the node to which the comparisons
// are being made.
*/
while(e != (*head)->next) {
c = a = *head;
b = a->next;
while(a != e) {
if(a->dondistance > b->dondistance) {
if(a == *head) {
tmp = b -> next;
b->next = a;
a->next = tmp;
*head = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else {
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}
void parseH264MTAPNAL(u_char **value, int packetDataLength, char *file, FILE *fp, int *writeNALPrefixCode, int *writePacket, int type){
int pdon = 0;
++(*value);
int DONB = 0;
memcpy(&DONB,*value,2);
DONB = ntohs(DONB);
*value += 2;
int nalusize = 0;
int totalnalusize = 0;
memcpy(&nalusize,*value,2);
nalusize = ntohs(nalusize);
char pd[4] = { '\0' };
*writeNALPrefixCode = 0;
*writePacket = 0;
if(nalusize != 0){
*value += 2;
}
else{
return;
}
int DOND = 0;
memcpy(&DOND,*value,1);
++(*value);
/* Skipping the timestamp offset */
if(type == 16){
*value += 2;
}
else if(type == 24){
*value += 3;
}
int bytes = 0;
if(type == 16){
bytes = packetDataLength - 8;
}
else if(type == 24){
bytes = packetDataLength - 9;
}
/* 8/9 bytes, 1st byte is NAL HDR(MTAP16), next 2 bytes are DONB, next 2 bytes are NALU size, next 1 byte is DOND and last 2/3 bytes are TS offset */
struct AggregateNAL *firstMTAP = NULL;
struct AggregateNAL *previousMTAP = NULL;
while(totalnalusize < bytes){
totalnalusize += nalusize;
struct AggregateNAL *mtap = NULL;
if(firstMTAP == NULL){
mtap = (struct AggregateNAL *) calloc(1,sizeof(struct AggregateNAL));
if(mtap == NULL){
fprintf(stderr,"[-]Error: Allocating memory for stapb NAL unit\n");
return;
}
mtap->bufferLength = 0;
mtap->dondistance = 0;
mtap->next = NULL;
firstMTAP = mtap;
previousMTAP = mtap;
}
else{
mtap = (struct AggregateNAL *) calloc(1,sizeof(struct AggregateNAL));
if(mtap == NULL){
fprintf(stderr,"[-]Error: Allocating memory for stapb NAL unit\n");
return;
}
mtap->bufferLength = 0;
mtap->dondistance = 0;
mtap->next = NULL;
previousMTAP->next = mtap;
previousMTAP = mtap;
}
mtap->buffer = (u_char *) calloc(nalusize,sizeof(u_char));
if(mtap->buffer == NULL){
fprintf(stderr,"[-]Error: Allocating memory for stapb buffer\n");
return;
}
memcpy(mtap->buffer,*value,nalusize);
mtap->bufferLength = nalusize;
int don = (DONB + DOND) % 65536;
if(don > pdon)
mtap->dondistance = don - pdon;
else
mtap->dondistance = 65535 - pdon + don + 1;
*value += nalusize;
if(totalnalusize < bytes){
memcpy(&nalusize,*value,2);
nalusize = ntohs(nalusize);
if(nalusize != 0){
*value += 2;
}
else{
break;
}
bytes =- 2;
memcpy(&DOND,*value,1);
*value += 1;
bytes =- 1;
/* Skipping 2/3 byte TS offset */
if(type == 16){
*value += 2;
bytes =- 2;
}
else if(type == 24){
*value += 3;
bytes =- 3;
}
}
}
struct AggregateNAL *currentMTAP = firstMTAP;
bubblesortAggregateNALDON(¤tMTAP);
while(currentMTAP != NULL){
phtonl(pd,0x00000001);
fwrite(pd,1,4,fp);
int ret = fwrite(currentMTAP->buffer, sizeof(u_char), currentMTAP->bufferLength, fp);
if(ret < currentMTAP->bufferLength){
printf("[-]Error: Writing data to file %s:%s \n",file,strerror(errno));
return;
}
free(currentMTAP->buffer);
struct AggregateNAL* lastMTAP = currentMTAP;
currentMTAP = currentMTAP->next;
free(lastMTAP);
}
}
void parseH264STAPBNAL(u_char **value, int packetDataLength, char *file, FILE *fp, int *writeNALPrefixCode, int *writePacket){
++(*value);
int initialDON = 0;
memcpy(&initialDON,*value,2);
initialDON = ntohs(initialDON);
*value += 2;
int nalusize = 0;
int totalnalusize = 0;
memcpy(&nalusize,*value,2);
nalusize = ntohs(nalusize);
//totalnalusize = nalusize;
int pdon = 0;
char pd[4] = { '\0' };
*writeNALPrefixCode = 0;
*writePacket = 0;
struct AggregateNAL *firstSTAPB = NULL;
struct AggregateNAL *previousSTAPB = NULL;
int bytes = packetDataLength - 5;
/* 5, first byte is the size of STAP-A NAL HDR , second 2 bytes are the DON, last 2 bytes are NALU size*/
if(nalusize != 0){
*value += 2;
}
else{
return;
}
while(totalnalusize < bytes){
totalnalusize = totalnalusize + nalusize;
struct AggregateNAL *stapb = NULL;
if(firstSTAPB == NULL){
stapb = (struct AggregateNAL *) calloc(1,sizeof(struct AggregateNAL));
if(stapb == NULL){
fprintf(stderr,"[-]Error: Allocating memory for stapb NAL unit\n");
return;
}
stapb->bufferLength = 0;
stapb->dondistance = 0;
stapb->next = NULL;
firstSTAPB = stapb;
previousSTAPB = stapb;
}
else{
stapb = (struct AggregateNAL *) calloc(1,sizeof(struct AggregateNAL));
if(stapb == NULL){
fprintf(stderr,"[-]Error: Allocating memory for stapb NAL unit\n");
return;
}
stapb->bufferLength = 0;
stapb->dondistance = 0;
stapb->next = NULL;
previousSTAPB->next = stapb;
previousSTAPB = stapb;
}
stapb->buffer = (u_char *) calloc(nalusize,sizeof(u_char));
if(stapb->buffer == NULL){
fprintf(stderr,"[-]Error: Allocating memory for stapb buffer\n");
return;
}
memcpy(stapb->buffer,*value,nalusize);
stapb->bufferLength = nalusize;
if(initialDON > pdon)
stapb->dondistance = initialDON - pdon;
else
stapb->dondistance = 65535 - pdon + initialDON + 1;
*value = *value + nalusize;
nalusize = 0;
if(totalnalusize < bytes){
memcpy(&nalusize,*value,2);
nalusize = ntohs(nalusize);
bytes = bytes - 2;
initialDON += 1 % 65536;
}
if(nalusize != 0){
*value = *value + 2;
}
else{
break;
}
}
struct AggregateNAL *currentSTAPB = firstSTAPB;
bubblesortAggregateNALDON(¤tSTAPB);
while(currentSTAPB != NULL){
phtonl(pd,0x00000001);
fwrite(pd,1,4,fp);
int ret = fwrite(currentSTAPB->buffer, sizeof(u_char), currentSTAPB->bufferLength, fp);
if(ret < currentSTAPB->bufferLength){
printf("[-]Error: Writing data to file %s:%s \n",file,strerror(errno));
return;
}
free(currentSTAPB->buffer);
struct AggregateNAL* lastSTAPB = currentSTAPB;
currentSTAPB = currentSTAPB->next;
free(lastSTAPB);
}
}
void parseH264STAPANAL(u_char **value, int packetDataLength, char *file, FILE *fp, int *writeNALPrefixCode, int *writePacket){
++(*value);
int nalusize = 0;
int totalnalusize = 0;
memcpy(&nalusize,*value,2);
nalusize = ntohs(nalusize);
//totalnalusize = nalusize;
char pd[4] = { '\0' };
int bytes = packetDataLength - 3;
/* 3 bytes are, first byte is the size of STAP-A NAL HDR and last 2 bytes are the NALU Size*/
*writeNALPrefixCode = 0;
*writePacket = 0;
if(nalusize != 0){
*value += 2;
}
else{
return;
}
while(totalnalusize < bytes){
totalnalusize = totalnalusize + nalusize;
phtonl(pd,0x00000001);
fwrite(pd,1,4,fp);
int ret = fwrite(*value, sizeof(u_char), nalusize, fp);
if(ret < nalusize){
printf("[-]Error: Writing data to file %s:%s \n",file,strerror(errno));
return;
}
*value = *value + nalusize;
nalusize = 0;
if(totalnalusize < bytes){
memcpy(&nalusize,*value,2);
nalusize = ntohs(nalusize);
bytes = bytes - 2;
}
if(nalusize != 0){
*value = *value + 2;
}
else{
break;
}
}
}
void parseH264FUANAL(struct naluHeader *naluHeaderValue, u_char **value, int *offset, int *writeNALPrefixCode, int *writePacket, int *fua_start){
struct fuaHeader{
unsigned start:1;
unsigned end:1;
unsigned reserved:1;
unsigned type:5;
};
++(*value);
++(*offset);
struct fuaHeader fuaHeaderValue;
fuaHeaderValue.start = (**value & 0x80) >> 7;
fuaHeaderValue.end = (**value & 0x40) >> 6;
fuaHeaderValue.reserved = (**value & 0x20) >> 5;
fuaHeaderValue.type = (**value) & 0x1F;
if(fuaHeaderValue.start){
/* Start of fragmented NAL unit */
unsigned char naluHeaderValue1;
naluHeaderValue1 = naluHeaderValue->forbidden << 7;
naluHeaderValue1 |= naluHeaderValue->nri << 5;
naluHeaderValue1 |= fuaHeaderValue.type;
memcpy(*value,&naluHeaderValue1,1);
*fua_start = 1;
}
else if((!fuaHeaderValue.start) && (*fua_start == 1)){
/* Subsequent NAL fragments */
++(*value);
++(*offset);
*writeNALPrefixCode = 0;
if(fuaHeaderValue.end){
*fua_start = 0;
}
}
else{
*writeNALPrefixCode = 0;
*writePacket = 0;
}
}