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
|
#include "av_format_identify.h"
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <sys/types.h>
#include <sys/io.h>
#include <arpa/inet.h>
#define FIT_TIMES 2
#define TS_SYNC_BYTE 0x47
#define TS_PKT_SIZE 188
#define FLV_TAG_AUDIO 0x08
#define FLV_TAG_VIDEO 0x09
#define FLV_TAG_SCRIPT_DATA 0x12
const char *g_mp4_box_type_all[] = { "ftyp","pdin","moov","mvhd","meta",
"trak","tkhd","tref","trgr","edts","elst","mdia","mdhd","hdlr","elng",
"minf","vmhd","smhd","hmhd","sthd","nmhd","dinf","dref","stbl","stsd",
"stts","ctts","cslg","stsc","stsz","stz2","stco","co64","stss","stsh",
"padb","stdp","sdtp","sbgp","sgpd","subs","saiz","saio","udta","mvex",
"mehd","trex","leva","moof","mfhd","traf","tfhd","trun","tfdt","mfra",
"tfra","mfro","mdat","free","skip","cprt","tsel","strk","stri","strd",
"iloc","ipro","sinf","frma","schm","schi","iinf","xml","bxml","pitm",
"fiin","paen","fire","fpar","fecr","segr","gitn","idat","iref","meco",
"mere","styp","sidx","ssix","prft", "uuid" };
const int g_mp4_box_type_count = sizeof(g_mp4_box_type_all) / sizeof(char *);
#pragma pack (1)
typedef struct ts_pkg_t
{
uint8_t sync_byte;
uint16_t con_pid;
uint8_t con_cnt;
char *pyld_data;
}ts_pkg;
#pragma pack ()
#define MIN_TS_PKT_LEN (sizeof(ts_pkg)-8)
/*To find that whether the initial size bytes pointed to by buff satisfies the rules of MPEG-TS AV Container
* by supposing that its first 1 byte is sync_byte in ts header.*/
int is_mpeg_ts_each(const char *buff, size_t size)
{
int ret = 0;
int fit_times = 0;
const char *p = buff;
const ts_pkg *pkg = NULL;
uint8_t cont_cnt_pre = 0, cont_cnt_cur = 0;
uint16_t pid_pre=0, pid_cur=0;
int group[3] = { 1,2,4 };
/*when p == buff(first of while), record pid and continuous_count of package head and go to fit_times++ directly.
special for mpeg-ts cause of the format requirement for two packages together.*/
if (p - buff + MIN_TS_PKT_LEN > size)
{
return ret;
}
pkg = (const ts_pkg *)p;
pid_pre = htons(pkg->con_pid) & 0x1fff;//(htons(pkg->con_pid)) << 3 >> 3;//?
cont_cnt_pre = (pkg->con_cnt) & 0x0f;
p += TS_PKT_SIZE;
fit_times++;
while (p - buff + MIN_TS_PKT_LEN <= size)
{
pkg = (const ts_pkg *)p;
if (pkg->sync_byte != TS_SYNC_BYTE)
{
break;
}
pid_cur = htons(pkg->con_pid) & 0x1fff;
if (pid_cur == pid_pre && pid_cur != 8191)//
{
cont_cnt_cur = (pkg->con_cnt) & 0x0f;
if (cont_cnt_cur != (cont_cnt_pre + 1) % 16)
{
break;
}
}
fit_times++;
if (fit_times >= FIT_TIMES)
{
size_t whole_pkg_cnt = (size - 1) / TS_PKT_SIZE;
if (whole_pkg_cnt < 4)
{
break;
}
int i;
for (i = 0; i < 3; i++)
{
const char *pkg_seg = buff + whole_pkg_cnt / group[i] * TS_PKT_SIZE;
if (*pkg_seg != TS_SYNC_BYTE)
{
return ret;
}
}
ret = 1;
break;
}
cont_cnt_pre = cont_cnt_cur;
pid_pre = pid_cur;
p += TS_PKT_SIZE;
}
return ret;
}
/*To identify whether the whole byte stream belongs to MPEG_TS container*/
int is_mpeg_ts(const char* buff, size_t size)
{
const char *p = buff;
int is_ts=0;
while (p < buff + size)
{
p = memchr(p, TS_SYNC_BYTE, size - (p - buff));
if (p == NULL)
{
break;
}
is_ts = is_mpeg_ts_each(p, size - (p - buff));
if (is_ts == 1)
{
printf("p-buff is %lx.\n",p-buff);
break;
}
p++;
}
return is_ts;
}
void *memchr_flv(const char *buff, size_t size)
{
void *ret_memflv = NULL;
const char *p = buff;
while (p < buff + size)
{
if (*p == FLV_TAG_AUDIO || *p == FLV_TAG_VIDEO || *p == FLV_TAG_SCRIPT_DATA)
{
ret_memflv = (void *)p;
break;
}
p++;
}
return ret_memflv;
}
#pragma pack (1)
typedef struct flv_tag_t
{
uint32_t pre_len;
uint8_t tag_type;
uint32_t con_pyld_len;//payload length
char *pyld_data;
}flv_tag;
#pragma pack ()
#define MIN_FLV_TAG_LEN (sizeof(flv_tag)-8);//(sizeof(flv_tag) + 1)//?
/*To find that whether the initial size bytes pointed to by buff satisfies the rules of FLV AV Container
* by supposing that its first 1 byte is TagType in FLV Tag Header.*/
int is_flv_each(const char* buff, size_t size)
{
int ret = 0;
int fit_times = 0;
const char *p = buff;
const flv_tag *tag = NULL;
uint32_t tag_len_cnt = 0;
uint32_t tag_len_show = 0;
if (p - buff + MIN_FLV_TAG_LEN - 4 > size)
{
return ret;
}
tag_len_cnt = (htonl(*(uint32_t *)p) & 0x00ffffff) + 11;//?? 11 + (htonl(tag->con_pyld_len) >> 8);//11 + (htonl(*(uint32_t *)tag->pyld_len) >> 8);
p += tag_len_cnt;//p is the first previous_length after buff
fit_times++;
while (p - buff + MIN_FLV_TAG_LEN <= size)
{
tag = (const flv_tag *)p;
tag_len_show = htonl(tag->pre_len);
if ((tag->tag_type) != FLV_TAG_AUDIO && (tag->tag_type) != FLV_TAG_VIDEO && (tag->tag_type) != FLV_TAG_SCRIPT_DATA)
{
break;
}
if (tag_len_cnt != tag_len_show)
{
break;
}
uint32_t tag_len_cnt = (htonl(tag->con_pyld_len) >> 8) + 11;
fit_times++;
if (fit_times >= FIT_TIMES)
{
ret = 1;
break;
}
p += (4 + tag_len_cnt);
}
return ret;
}
/*To identify whether the whole byte stream belongs to FLV container*/
int is_flv(const char* buff, size_t size)
{
int is_flv = 0;
const char *p = buff;
while (p - buff < size)//same as p - buff + 1 <= size//while (p < size + buff)
{
p = memchr_flv(p, size - (p - buff));
if (p == NULL)
{
break;
}
is_flv = is_flv_each(p, size - (p - buff));
if (is_flv == 1)
{
printf("p-buff is %lx.\n",p-buff);
break;
}
p++;
}
return is_flv;
}
int is_mp4_boxname_deep(const char *buff)
{
int i;
size_t size_type_h;
for (i = 0; i < g_mp4_box_type_count; i++)
{
size_type_h = strlen(g_mp4_box_type_all[i]);
if (memcmp(buff, g_mp4_box_type_all[i], size_type_h) == 0)
return 1;
}
return 0;
}
int is_mp4_boxname(const char *buff)
{
int i=0,raw_filter=0;
for (i = 0; i < 4; i++)
{
if (((*(buff + i) >= '0') && (*(buff + i) <= '9')) || ((*(buff + i) >= 'a') && (*(buff + i) <= 'z')))
{
raw_filter++;
}
else
{
break;
}
}
if (raw_filter == 4)//box type:xmf
{
return 1;
}
else if (raw_filter == 3)
{
if (memcmp(buff, "xmf", 3) == 0)
{
return 1;
}
}
return 0;
}
void *memmem_mp4(const char *buff, size_t size)
{
void *ret_memmp4 = NULL;
const char *p = buff;
while (p + 3 < buff + size)
{
if ((is_mp4_boxname(p) == 1) && (is_mp4_boxname_deep(p) == 1))
{
ret_memmp4 = (void *)p;
break;
}
p++;
}
return ret_memmp4;
}
#pragma pack (1)
typedef struct mp4_box_t
{
uint32_t atom_size;
char name[4];
union
{
uint64_t large_size;
char* box_data;
};
}mp4_box;
#pragma pack ()
#define MIN_MP4_BOX_SIZE (sizeof(mp4_box)-8)
#define htonll(buff) (((uint64_t) htonl(buff)) << 32) + htonl(buff >> 32)//(((uint64_t)(htonl(*(uint32_t *)buff))<<32)+htonl(*(uint32_t *)(buff+4)))
/*To find that whether the initial size bytes pointed to by buff satisfies the rules of MP4 AV Container
* by supposing that its first 4 byte is Boxtype in Box of MP4 file.*/
int is_mp4_each(const char* buff, size_t size)
{
int ret = 0;
int fit_times = 0;
const char *p = buff;
const mp4_box *box=NULL;
uint64_t offset = 0;
while (p - buff + MIN_MP4_BOX_SIZE <= size)//while(p-buff+MIN_MP4_BOX_SIZE<size)//at least contain atom_size and name of mp4 box for current p
{
box = (const mp4_box *)p;//?
if(!is_mp4_boxname(box->name))//?,sizeof(box->name))
{
break;
}
fit_times++;
if (fit_times >= FIT_TIMES)
{
ret = 1;
break;
}//
if(htonl(box->atom_size)==1)//?if(box->atom_size == 1)
{
if(size-(p-buff)>sizeof(mp4_box))//???
{
offset = htonll(box->large_size);
}
else
{
break;
}
}
else
{
offset = htonl(box->atom_size);
}
p += offset;
}
return ret;
}
/*To identify whether the whole byte stream belongs to MP4 container*/
int is_mp4(const char* buff, size_t size)
{
int is_mp4 = 0;
const char *p = buff;
while (p < buff + size)
{
p = (const char *)memmem_mp4(p, size - (p - buff));
if (p == NULL)
{
break;
}
if (p - 4 < buff)//if there is no atom_size for the first box existing, the exit
{
p++;
continue;
}
is_mp4 = is_mp4_each(p - 4, size - (p - buff) + 4);
if (is_mp4 == 1)
{
printf("p-buff is %lx.\n",p-buff);
break;
}
p++;
}
return is_mp4;
}
int AV_container_identify(const char* buff, size_t size)
{
if (1 == is_mpeg_ts(buff, size))
{
return AV_CONTAINER_MPEGTS;
}
if (1 == is_flv(buff, size))
{
return AV_CONTAINER_FLV;
}
if (1 == is_mp4(buff, size))
{
return AV_CONTAINER_MP4;
}
return AV_CONTAINER_UNKNOWN;
}
|