diff options
Diffstat (limited to 'src/av_format_identify.c')
| -rw-r--r-- | src/av_format_identify.c | 85 |
1 files changed, 24 insertions, 61 deletions
diff --git a/src/av_format_identify.c b/src/av_format_identify.c index ef6f0c1..fa1c63e 100644 --- a/src/av_format_identify.c +++ b/src/av_format_identify.c @@ -30,22 +30,6 @@ const char *g_mp4_box_type_all[] = { "ftyp","pdin","moov","mvhd","meta", "mere","styp","sidx","ssix","prft", "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.*/ - -/*To identify whether the whole byte stream belongs to MPEG_TS container*/ - - - -/*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.*/ - -/*To identify whether the whole byte stream belongs to FLV container*/ - -/*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.*/ - - /*To identify whether the whole byte stream belongs to MP4 container*/ #pragma pack (1) typedef struct ts_pkg_t { @@ -57,6 +41,8 @@ typedef struct ts_pkg_t #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; @@ -64,18 +50,13 @@ int is_mpeg_ts_each(const char *buff, size_t size) const char *p = buff; const ts_pkg *pkg = NULL; - //uint16_t cont_cnt_pre=0, cont_cnt_cur=0; - //cont_cnt_pre = (*(uint16_t *)(pkg->con_cnt)) & 0x0f; uint8_t cont_cnt_pre = 0, cont_cnt_cur = 0; - //cont_cnt_pre = pkg->con_cnt; uint16_t pid_pre=0, pid_cur=0; - //pid_pre = (htons(*(uint16_t *)(pkg->con_pid))) << 3 >> 3; int group[3] = { 1,2,4 }; - //p += TS_PKT_SIZE; /*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) @@ -85,11 +66,11 @@ int is_mpeg_ts_each(const char *buff, size_t size) 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;//(pkg->con_cnt) >> 4; + cont_cnt_pre = (pkg->con_cnt) & 0x0f; p += TS_PKT_SIZE; fit_times++; - while (p - buff + MIN_TS_PKT_LEN <= size)//while (p < (buff + size)) + while (p - buff + MIN_TS_PKT_LEN <= size) { pkg = (const ts_pkg *)p; if (pkg->sync_byte != TS_SYNC_BYTE) @@ -97,11 +78,10 @@ int is_mpeg_ts_each(const char *buff, size_t size) break; } - pid_cur = htons(pkg->con_pid) & 0x1fff;//(htons(pkg->con_pid)) << 3 >> 3; - //pid_cur = ((htonl(*(uint32_t *)(p+TS_PKT_SIZE)) & 0x000fff00) >> 8) + ((((htonl(*(uint32_t *)(p + TS_PKT_SIZE)) & 0x00f00000) >> 20) % 2) << 12); + pid_cur = htons(pkg->con_pid) & 0x1fff; if (pid_cur == pid_pre && pid_cur != 8191)// { - cont_cnt_cur = (pkg->con_cnt) & 0x0f;//(pkg->con_cnt) >> 4;// p + TS_PKT_SIZE;//cont_cnt_cur = (*(uint16_t *)(p + TS_PKT_SIZE)) & 0x0f; + cont_cnt_cur = (pkg->con_cnt) & 0x0f; if (cont_cnt_cur != (cont_cnt_pre + 1) % 16) { break; @@ -122,7 +102,7 @@ int is_mpeg_ts_each(const char *buff, size_t size) const char *pkg_seg = buff + whole_pkg_cnt / group[i] * TS_PKT_SIZE; if (*pkg_seg != TS_SYNC_BYTE) { - return ret;//break; + return ret; } } ret = 1; @@ -136,6 +116,7 @@ int is_mpeg_ts_each(const char *buff, size_t 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; @@ -179,11 +160,14 @@ typedef struct flv_tag_t { uint32_t pre_len; uint8_t tag_type; - uint32_t con_pyld_len;//uint8_t pyld_len[3];//payload length + uint32_t con_pyld_len;//payload length + char *pyld_data; }flv_tag; #pragma pack () -#define MIN_FLV_TAG_LEN (sizeof(flv_tag))//(sizeof(flv_tag) + 1)//? +#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; @@ -191,9 +175,6 @@ int is_flv_each(const char* buff, size_t size) const char *p = buff; const flv_tag *tag = NULL; - //uint32_t tag_len_show = 0; - //uint32_t tag_len_cnt = 0;// = 11 + (htonl(*(size_t *)p) & 0x00ffffff); - //p += (tag_len_cnt + 4); uint32_t tag_len_cnt = 0; uint32_t tag_len_show = 0; if (p - buff + MIN_FLV_TAG_LEN - 4 > size) @@ -205,7 +186,7 @@ int is_flv_each(const char* buff, size_t size) p += tag_len_cnt;//p is the first previous_length after buff fit_times++; - while (p - buff + MIN_FLV_TAG_LEN <= size)//while (p < (buff + size)) + while (p - buff + MIN_FLV_TAG_LEN <= size) { tag = (const flv_tag *)p; tag_len_show = htonl(tag->pre_len); @@ -219,7 +200,7 @@ int is_flv_each(const char* buff, size_t size) break; } - uint32_t tag_len_cnt = (htonl(tag->con_pyld_len) >> 8) + 11;//11 + (htonl(*(uint32_t *)(tag->pyld_len)) >> 8);//(htonl(*(uint32_t *)tag)) & 0x00ffffff; + uint32_t tag_len_cnt = (htonl(tag->con_pyld_len) >> 8) + 11; fit_times++; if (fit_times >= FIT_TIMES) @@ -232,6 +213,7 @@ int is_flv_each(const char* buff, size_t size) 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; @@ -272,15 +254,10 @@ int is_mp4_boxname_deep(const char *buff) -int is_mp4_boxname(const char *buff)// , size_t size) +int is_mp4_boxname(const char *buff) { int i=0,raw_filter=0; - /* - if (size<4) - { - return 0; - } - */ + for (i = 0; i < 4; i++) { if (((*(buff + i) >= '0') && (*(buff + i) <= '9')) || ((*(buff + i) >= 'a') && (*(buff + i) <= 'z'))) @@ -313,7 +290,6 @@ void *memmem_mp4(const char *buff, size_t size) const char *p = buff; while (p + 3 < buff + size) { - //if (is_mp4_boxname(p) == 1) if ((is_mp4_boxname(p) == 1) && (is_mp4_boxname_deep(p) == 1)) { ret_memmp4 = (void *)p; @@ -337,12 +313,14 @@ typedef struct mp4_box_t #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 char *q = NULL; const mp4_box *box=NULL; uint64_t offset = 0; @@ -351,15 +329,6 @@ int is_mp4_each(const char* buff, size_t size) box = (const mp4_box *)p;//? if(!is_mp4_boxname(box->name))//?,sizeof(box->name)) { - /*if (offset <= 0xffffffff) - { - p = q + 8; - box = (const mp4_box *)p; - } - if (!is_mp4_boxname(box->name)) - { - break; - }*/ break; } fit_times++; @@ -374,7 +343,7 @@ int is_mp4_each(const char* buff, size_t size) { if(size-(p-buff)>sizeof(mp4_box))//??? { - offset = htonll(box->large_size);//htonll(&(box->large_size));//p += htonll(&box->large_size);//p+=htonll(box->large_size);//TODO: Implement htonll + offset = htonll(box->large_size); } else { @@ -383,22 +352,16 @@ int is_mp4_each(const char* buff, size_t size) } else { - offset = htonl(box->atom_size);//p+=htonl(box->atom_size); + offset = htonl(box->atom_size); } - //q = p; p += offset; } - /*else - { - ret = 0; - }// - */ 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; |
