diff options
| author | Xiaoqing MA <[email protected]> | 2018-07-30 18:31:18 +0800 |
|---|---|---|
| committer | Xiaoqing MA <[email protected]> | 2018-07-30 18:31:18 +0800 |
| commit | 01396aec34cc41ebf99e43abba68046884fc6282 (patch) | |
| tree | 7c1fb67c7ed228bb7e1f75895f5f745fced9e95a | |
| parent | ab89bcab1283a5e2ee86e701582460261ef8dfe3 (diff) | |
1.对照编码规范和开发指南修改
| -rw-r--r-- | inc/av_format_identify.h | 26 | ||||
| -rw-r--r-- | src/av_format_identify.c | 127 |
2 files changed, 83 insertions, 70 deletions
diff --git a/inc/av_format_identify.h b/inc/av_format_identify.h index e136d43..328d0a9 100644 --- a/inc/av_format_identify.h +++ b/inc/av_format_identify.h @@ -1,15 +1,29 @@ +#ifndef AV_CONTAINER_IDENTIFY_H_INC +#define AV_CONTAINER_IDENTIFY_H_INC #include <stdio.h> -#ifndef _AV_FORMAT_IDENTIFY_H -#define _AV_FORMAT_IDENTIFY_H - - +#ifdef __cplusplus +extern "C" +{ +#endif #define AV_CONTAINER_UNKNOWN 0 #define AV_CONTAINER_MPEGTS 1 #define AV_CONTAINER_FLV 2 #define AV_CONTAINER_MP4 3 - -/*To identify the container type of the whole byte stream*/ +/* +Description:to identify the container type of the whole bytestream; +Paraments: + para1:[IN] buff, a pointer to the beginning of this bytestream; + para1:[IN] size, how long is this bytestream; +Return: + AV_CONTAINER_UNKNOWN(0):can not identifying its container format; + AV_CONTAINER_MPEGTS(1):this bytestream belongs to MPEG-TS container; + AV_CONTAINER_FLV(2):this bytestream belongs to FLV container; + AV_CONTAINER_MP4(3):this bytestream belongs to MP4 container; +*/ int AV_container_identify(const char* buff, size_t size); +#ifdef __cplusplus +} +#endif #endif diff --git a/src/av_format_identify.c b/src/av_format_identify.c index fa1c63e..b568e33 100644 --- a/src/av_format_identify.c +++ b/src/av_format_identify.c @@ -18,6 +18,7 @@ #define FLV_TAG_VIDEO 0x09 #define FLV_TAG_SCRIPT_DATA 0x12 +//g_mp4_box_type_all: various box type in mp4 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", @@ -28,18 +29,21 @@ const char *g_mp4_box_type_all[] = { "ftyp","pdin","moov","mvhd","meta", "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" }; +//g_mp4_box_type_count:how many kinds of box type in mp4 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; + uint8_t sync_byte;//sync_byte + uint16_t con_pid;//3 bits+pid(13 bits) + uint8_t con_ctt_cnt;//4 bits+continuity_counter(4 bits) + char *pkt_data;//packet data }ts_pkg; #pragma pack () -#define MIN_TS_PKT_LEN (sizeof(ts_pkg)-8) +#define MIN_TS_PKT_LEN (sizeof(ts_pkg) - 8) + +#define MIN_TS_PKT_CNTS 4 /*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.*/ @@ -49,40 +53,34 @@ int is_mpeg_ts_each(const char *buff, size_t size) 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 }; + uint8_t ctt_cnt_pre = 0, ctt_cnt_cur = 0; + uint16_t pid_pre = 0, pid_cur = 0; - /*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) + //record the first PID and continuous_count + if (p + MIN_TS_PKT_LEN - buff > size)//which is: while (p + (MIN_TS_PKT_LEN - 1) - buff >= 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; + pid_pre = htons(pkg->con_pid) & 0x1fff; + ctt_cnt_pre = (pkg->con_ctt_cnt) & 0x0f; p += TS_PKT_SIZE; fit_times++; - while (p - buff + MIN_TS_PKT_LEN <= size) + int pkt_cnt_divs[3] = { 1,2,4 }; + while (p + MIN_TS_PKT_LEN - buff <= size)//which is: while (p + (MIN_TS_PKT_LEN - 1) - buff < 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)// + if (pid_cur == pid_pre && pid_cur != 8191)//no continuous_count rule for PID:8191 { - cont_cnt_cur = (pkg->con_cnt) & 0x0f; - if (cont_cnt_cur != (cont_cnt_pre + 1) % 16) + ctt_cnt_cur = (pkg->con_ctt_cnt) & 0x0f; + if (ctt_cnt_cur != (ctt_cnt_pre + 1) % 16) { break; } @@ -92,14 +90,14 @@ int is_mpeg_ts_each(const char *buff, size_t size) if (fit_times >= FIT_TIMES) { size_t whole_pkg_cnt = (size - 1) / TS_PKT_SIZE; - if (whole_pkg_cnt < 4) + if (whole_pkg_cnt < MIN_TS_PKT_CNTS) { break; } int i; for (i = 0; i < 3; i++) { - const char *pkg_seg = buff + whole_pkg_cnt / group[i] * TS_PKT_SIZE; + const char *pkg_seg = buff + whole_pkg_cnt / pkt_cnt_divs[i] * TS_PKT_SIZE; if (*pkg_seg != TS_SYNC_BYTE) { return ret; @@ -109,19 +107,20 @@ int is_mpeg_ts_each(const char *buff, size_t size) break; } - cont_cnt_pre = cont_cnt_cur; + ctt_cnt_pre = ctt_cnt_cur; pid_pre = pid_cur; p += TS_PKT_SIZE; } return ret; } -/*To identify whether the whole byte stream belongs to MPEG_TS container*/ + +/*To identify whether the whole bytestream 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) + while (p - buff < size) { p = memchr(p, TS_SYNC_BYTE, size - (p - buff)); if (p == NULL) @@ -131,7 +130,6 @@ int is_mpeg_ts(const char* buff, size_t size) is_ts = is_mpeg_ts_each(p, size - (p - buff)); if (is_ts == 1) { - printf("p-buff is %lx.\n",p-buff); break; } p++; @@ -144,7 +142,7 @@ void *memchr_flv(const char *buff, size_t size) { void *ret_memflv = NULL; const char *p = buff; - while (p < buff + size) + while (p - buff < size) { if (*p == FLV_TAG_AUDIO || *p == FLV_TAG_VIDEO || *p == FLV_TAG_SCRIPT_DATA) { @@ -155,16 +153,17 @@ void *memchr_flv(const char *buff, size_t size) } 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; + uint32_t pre_tag_size;//Previous Tag Size + uint8_t tag_type;//Tag Type, in Tag Header + uint32_t con_data_size;//Data Size(3 Bytes) + 1 Byte, in Tag Header }flv_tag; #pragma pack () -#define MIN_FLV_TAG_LEN (sizeof(flv_tag)-8);//(sizeof(flv_tag) + 1)//? +#define MIN_FLV_TAG_LEN (sizeof(flv_tag)) /*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.*/ @@ -177,19 +176,20 @@ int is_flv_each(const char* buff, size_t size) uint32_t tag_len_cnt = 0; uint32_t tag_len_show = 0; - if (p - buff + MIN_FLV_TAG_LEN - 4 > size) + + //record the first Tag Size and Data Size without Previous Tag Size. + if (p + MIN_FLV_TAG_LEN - 4 - buff > size)//which is (p + (MIN_FLV_TAG_LEN - 4 - 1) - buff >= size), p is located in tag_type; { 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); + tag_len_cnt = (htonl(*(uint32_t *)p) & 0x00ffffff) + 11;//?? 11 + (htonl(tag->con_data_size) >> 8) p += tag_len_cnt;//p is the first previous_length after buff fit_times++; - while (p - buff + MIN_FLV_TAG_LEN <= size) + while (p + MIN_FLV_TAG_LEN - buff <= size)//which is: while (p + (MIN_FLV_TAG_LEN - 1) - buff < size), during this while loop, p is located in pre_tag_size { tag = (const flv_tag *)p; - tag_len_show = htonl(tag->pre_len); + tag_len_show = htonl(tag->pre_tag_size); if ((tag->tag_type) != FLV_TAG_AUDIO && (tag->tag_type) != FLV_TAG_VIDEO && (tag->tag_type) != FLV_TAG_SCRIPT_DATA) { @@ -200,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; + uint32_t tag_len_cnt = (htonl(tag->con_data_size) >> 8) + 11; fit_times++; if (fit_times >= FIT_TIMES) @@ -213,13 +213,14 @@ int is_flv_each(const char* buff, size_t size) return ret; } -/*To identify whether the whole byte stream belongs to FLV container*/ + +/*To identify whether the whole bytestream 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) + while (p - buff < size) { p = memchr_flv(p, size - (p - buff)); if (p == NULL) @@ -229,7 +230,6 @@ int is_flv(const char* buff, size_t size) is_flv = is_flv_each(p, size - (p - buff)); if (is_flv == 1) { - printf("p-buff is %lx.\n",p-buff); break; } p++; @@ -238,7 +238,6 @@ int is_flv(const char* buff, size_t size) } - int is_mp4_boxname_deep(const char *buff) { int i; @@ -253,10 +252,9 @@ int is_mp4_boxname_deep(const char *buff) } - int is_mp4_boxname(const char *buff) { - int i=0,raw_filter=0; + int i = 0, raw_filter = 0; for (i = 0; i < 4; i++) { @@ -284,11 +282,12 @@ int is_mp4_boxname(const char *buff) return 0; } + void *memmem_mp4(const char *buff, size_t size) { void *ret_memmp4 = NULL; const char *p = buff; - while (p + 3 < buff + size) + while (p + 3 - buff < size) { if ((is_mp4_boxname(p) == 1) && (is_mp4_boxname_deep(p) == 1)) { @@ -299,14 +298,16 @@ void *memmem_mp4(const char *buff, size_t size) } return ret_memmp4; } + + #pragma pack (1) typedef struct mp4_box_t { - uint32_t atom_size; - char name[4]; + uint32_t atom_size;//Box Size + char name[4];//Box Type, in BoxHeader union { - uint64_t large_size; + uint64_t large_size;//when the Box Size is larger than 0xffffffff, using Largesize to save the size of atom char* box_data; }; }mp4_box; @@ -322,12 +323,11 @@ int is_mp4_each(const char* buff, size_t size) 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 + while (p + MIN_MP4_BOX_SIZE - buff <= size)//which is while (p + (MIN_MP4_BOX_SIZE - 1) - buff <= size), p is located in atom_size { - box = (const mp4_box *)p;//? - if(!is_mp4_boxname(box->name))//?,sizeof(box->name)) + box = (const mp4_box *)p; + if(!is_mp4_boxname(box->name)) { break; } @@ -339,11 +339,11 @@ int is_mp4_each(const char* buff, size_t size) }// - if(htonl(box->atom_size)==1)//?if(box->atom_size == 1) + if(htonl(box->atom_size)==1) { - if(size-(p-buff)>sizeof(mp4_box))//??? + if (p + sizeof(mp4_box) - buff < size)//which is (p + (sizeof(mp4_box) - 1) - buff < size) { - offset = htonll(box->large_size); + p += htonll(box->large_size); } else { @@ -352,29 +352,28 @@ int is_mp4_each(const char* buff, size_t size) } else { - offset = htonl(box->atom_size); + p += htonl(box->atom_size); } - p += offset; - } return ret; } -/*To identify whether the whole byte stream belongs to MP4 container*/ + +/*To identify whether the whole bytestream 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) + 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 + if (p - 4 - buff < 0)//if there is no atom_size for the first box existing, forcing the next iteration of the loop { p++; continue; @@ -383,7 +382,6 @@ int is_mp4(const char* buff, size_t size) 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++; @@ -391,6 +389,7 @@ int is_mp4(const char* buff, size_t size) return is_mp4; } + int AV_container_identify(const char* buff, size_t size) { if (1 == is_mpeg_ts(buff, size)) |
