summaryrefslogtreecommitdiff
path: root/src/av_format_identify.c
blob: 533d554d46d368315f2de404b2868b0546a10ac3 (plain)
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#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 1
#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", "trak", "tkhd", "tref", "edts", "elst", "mdia", "mdhd",
"hdlr", "minf", "vmhd", "smhd", "hmhd", "nmhd", "dinf", "dref", "stbl", "stsd", "stts", "ctts", "stsc", "stsz",
"stz2", "stco", "co64", "stss", "stsh", "padb", "stdp", "sdtp", "sbgp", "sgpd", "subs", "mvex", "mehd", "trex",
"ipmc", "moof", "mfhd", "traf", "tfhd", "trun", "sdtp", "sbgp", "subs", "mfra", "tfra", "mfro", "mdat", "free",
"skip", "udta", "cprt", "meta", "hdlr", "dinf", "dref", "ipmc", "iloc", "ipro", "sinf", "frma", "imif", "schm",
"schi", "iinf", "xml", "bxml", "pitm", "fiin", "paen", "fpar", "fecr", "segr", "gitn", "tsel", "meco", "mere", "uuid" };
const int g_mp4_box_type_count = sizeof(g_mp4_box_type_all) / sizeof(char *);

/*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);
/*To identify whether the whole byte stream belongs to MPEG_TS container*/
int is_mpeg_ts(const char* buff, size_t size);


/*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);
/*To identify whether the whole byte stream belongs to FLV container*/
int is_flv(const char* buff, size_t size);

/*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);
/*To identify whether the whole byte stream belongs to MP4 container*/
int is_mp4(const char* buff, size_t size);


int is_mpeg_ts_each(const char *buff, size_t size)
{
	int ret_ts_each = 0;
	const char *p = buff;
	int fit_times = 0;
	
	size_t  cont_cout_pre=0, cont_cout_aft=0;
	cont_cout_pre = htonl(*(size_t *)buff) & 0x0000000f;

    uint32_t pid_pre=0, pid_aft=0;
    pid_pre = ((htonl(*(uint32_t *)buff) & 0x000fff00) >> 8) + ((((htonl(*(uint32_t *)buff) & 0x00f00000) >> 20) % 2) << 12);

	const char *pack_tail=NULL;
	const char *pack_half=NULL;
	const char *pack_quarter=NULL;

	size_t whole_pack_cnt=0;
	size_t half_pack_cnt=0;
	size_t quarter_pack_cnt=0;
	
	p += TS_PKT_SIZE;
	while (p < (buff + size))
	{
		if (*p != TS_SYNC_BYTE)
		{
			break;
		}
		
		if ((p + 3) < (buff + size))
		{
            pid_aft = ((htonl(*(uint32_t *)p) & 0x000fff00) >> 8) + ((((htonl(*(uint32_t *)p) & 0x00f00000) >> 20) % 2) << 12);
            if (pid_aft == pid_pre && pid_aft!=8191)
            {
            	cont_cout_aft = htonl(*(size_t *)p) & 0x0000000f;
			    if (cont_cout_aft == (cont_cout_pre + 1) % 16)
			    {
				    fit_times++;
			    }
			    else
			    {
				    break;
			    }
            }
            else
            {
                fit_times++;
            }
		}
		if (fit_times >= FIT_TIMES)
		{
			whole_pack_cnt = size / TS_PKT_SIZE;
			if (size % TS_PKT_SIZE == 0)
			{
				whole_pack_cnt--;
			}
            half_pack_cnt = whole_pack_cnt/2;
            quarter_pack_cnt = whole_pack_cnt/4;
            if (whole_pack_cnt < 3 || half_pack_cnt < 2 || quarter_pack_cnt < 1)
            {
                break;
            }
			pack_tail = buff + whole_pack_cnt * TS_PKT_SIZE;
            pack_half = buff + half_pack_cnt * TS_PKT_SIZE;
			pack_quarter = buff + quarter_pack_cnt * TS_PKT_SIZE;
            //if (*pack_tail == TS_SYNC_BYTE && *(pack_tail - TS_PKT_SIZE) == TS_SYNC_BYTE)
            if (*pack_tail == TS_SYNC_BYTE && *pack_half == TS_SYNC_BYTE && *pack_quarter == TS_SYNC_BYTE)
			{
				ret_ts_each = 1;
				break;
			}
			break;
		}

		cont_cout_pre = cont_cout_aft;
		pid_pre = pid_aft;
        p += TS_PKT_SIZE;
	}
	return ret_ts_each;
}

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||p + 3 >= buff + size)
		{
			break;
		}
		is_ts = is_mpeg_ts_each(p, size - (p - buff));
		if (is_ts == 1)
		{
			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;
}

int is_flv_each(const char* buff, size_t size)
{
	int ret_flv_each = 0;
	const char *p = buff;
	int fit_times = 0;
	uint32_t left=0;
	uint32_t right = 11 + (htonl(*(size_t *)p) & 0x00ffffff);
	p += (right + 4);

	while (p < (buff + size))
	{
		left = htonl(*(uint32_t *)(p - 4));
		if (right == left && (*(p) == FLV_TAG_AUDIO || *(p) == FLV_TAG_VIDEO || *(p) == FLV_TAG_SCRIPT_DATA))
		{
			fit_times++;
			if (fit_times >= FIT_TIMES)
			{
				ret_flv_each = 1;
				break;
			}
		}
		else
		{
			break;
		}
		if ((p + 3) >= (buff + size))
		{
			break;
		}
		right = 11 + (htonl(*(size_t *)p) & 0x00ffffff);
		p += (right + 4);
	}
	return ret_flv_each;
}

int is_flv(const char* buff, size_t size)
{
	int ret_flv = 0;
	int ret_flv_each;
	const char *p_each = buff;

	while (p_each < size + buff)
	{
		p_each = memchr_flv(p_each, size - (p_each - buff));
		if (p_each == NULL)
		{
			break;
		}
		else if (p_each + 3 >= buff + size)
		{
			break;
		}
		else
		{
			ret_flv_each = is_flv_each(p_each, size - (p_each - buff));
			if (ret_flv_each == 1)
			{
				ret_flv = 1;
				break;
			}
			p_each++;
		}
	}
	return ret_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;
	for (i = 0; i < 4; i++)
	{
		if (*(buff + i) < '0' || (*(buff + i) > '9'&&*(buff + i) < 'a') || *(buff + i) > 'z')
		{
			return 0;
		}
	}
	return 1;
}

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)
		{
			ret_memmp4 = (void *)p;
			break;
		}
		p++;
	}
	return ret_memmp4;
}

int is_mp4_each(const char* buff, size_t size)
{
	int ret_mp4_each = 0;
	int ret_mp4_boxname;
	int sig_mp4_boxtype = 0;
	if (is_mp4_boxname_deep(buff) == 1)
	{
		sig_mp4_boxtype = 1;
	}
	const char *p = buff;
	int fit_times = 0;

	size_t size_h;
	p += 8;
	while (p + 4 < (buff + size))
	{
		if (memcmp(p - 8, "mdat", 4) == 0 && htonl(*(size_t *)(p - 12)) == 1)//large size
		{
			p -= 8;
			if (p + 12 < buff + size)
			{
				size_h = htonl(*(size_t *)(p + 4));
				size_h = (size_h<<32)+ htonl(*(size_t *)(p + 8));
				p += size_h;
				if (p + 4 < buff + size)
				{
					ret_mp4_boxname = is_mp4_boxname(p);
					if (!ret_mp4_boxname)
					{
						p -= size_h;
						if (p + 16 < buff + size)
						{
							size_h = htonl(*(size_t *)(p + 8));
							size_h = (size_h << 32) + htonl(*(size_t *)(p + 12));
							p += size_h;
							if (p + 4 < buff + size)
							{
								ret_mp4_boxname = is_mp4_boxname(p);
								if (!ret_mp4_boxname)
								{
									p -= size_h;
								}
							}
                            else
                            {
                                    break;
                            }

						}
                        else
                        {
                                break;
                        }

					}
				}
                else
                {
                        break;
                }

			}
            else
            {
                break;
            }
		}
		else
		{
			ret_mp4_boxname = is_mp4_boxname(p);
			if (!ret_mp4_boxname)
			{
				p -= 8;
				size_h = htonl(*(size_t *)(p - 4));
				p += size_h;
				if (p + 4 < buff + size)
				{
					ret_mp4_boxname = is_mp4_boxname(p);
					if (!ret_mp4_boxname)
					{
						break;
					}
				}
                else
                {
                    break;
                }
			}
		}
		fit_times++;
		if (is_mp4_boxname_deep(p) == 1)
		{
			sig_mp4_boxtype++;
		}
		if (fit_times >= FIT_TIMES && sig_mp4_boxtype >= 1)
		{
			ret_mp4_each = 1;
			break;
		}
		if (fit_times > FIT_TIMES)
		{
			break;
		}
		
		p += 8;
	}
	return ret_mp4_each;
}


int is_mp4(const char* buff, size_t size)
{
	int ret_mp4 = 0;
	int ret_mp4_each;
	const char *p_each = buff;

	while (p_each < buff + size)
	{
		p_each = (const char *)memmem_mp4(p_each, size - (p_each - buff));
		if (p_each == NULL)
		{
			break;
		}
		else if (p_each + 12 >= buff + size)
		{
			break;
		}
		else if (p_each - 4 < buff)
		{
			p_each++;
			continue;
		}
		else
		{
			ret_mp4_each = is_mp4_each(p_each, size - (p_each - buff));
			if (ret_mp4_each == 1)
			{
				ret_mp4 = 1;
				break;
			}
			p_each++;
		}
	}
	return ret_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 UNKNOWN;
}