summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXiaoqing MA <[email protected]>2018-07-10 18:05:20 +0800
committerXiaoqing MA <[email protected]>2018-07-10 18:05:20 +0800
commitc0405d10f515512c37d7af340a87a30782d5d7a7 (patch)
tree5eb11c227db80b6c6ab3990e68650395d3400dfd
parentb59440857329f75ab5d64d4b1cc4d6e7fc7edd10 (diff)
new ed9
-rw-r--r--src/vdefine.c593
1 files changed, 302 insertions, 291 deletions
diff --git a/src/vdefine.c b/src/vdefine.c
index 86fa7f0..f7af817 100644
--- a/src/vdefine.c
+++ b/src/vdefine.c
@@ -1,360 +1,371 @@
-#include "vdefine.h"
#define AV_CONTAINER_MPEGTS 1
#define AV_CONTAINER_FLV 2
#define AV_CONTAINER_MP4 3
#define UNKNOWN 4
#define FIT_TIMES 1//#define FIT_TIMES 2
+#define MAX_FLV_FRAME max_flv_frame;
+
+#include "vdefine.h"
const char *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" };
+ "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" };
int count_type = sizeof(mp4_box_type_all) / sizeof(char *);
int is_mpeg_ts_each(const char *buff_each, size_t size_each, const char *buff, size_t size)
{
- int ret_ts_each = 0;
- const char *p = buff_each;
- int fit_times = 1;
- p += 188;
+ int ret_ts_each = 0;
+ const char *p = buff_each;
+ int fit_times = 1;
+ p += 188;
+
+ size_t cont_cout_ori, cont_cout_end, cont_cout_pre, cont_cout_aft;
+ cont_cout_ori = htonl(*(size_t *)buff_each) & 0x0000000f;
+ cont_cout_pre = cont_cout_ori;
+ while (p < (buff_each + size_each))
+ {
+ if (*p == 0x47)
+ {
+ if ((p + 3) < (buff_each + size_each))
+ {
+ cont_cout_aft = htonl(*(size_t *)p) & 0x0000000f;
+ if (cont_cout_aft == (cont_cout_pre + 1) % 16)
+ {
+ fit_times++;
+ }
+ else
+ {
+ break;
+ }
+ }
+ if (fit_times > FIT_TIMES)
+ {
+ size_t whole_pack_cout = size_each / 188;
+ if (size_each % 188 == 0)
+ {
+ whole_pack_cout--;
+ }
+ size_t whole_pack_bites = whole_pack_cout * 188;
+ const char *pack_tail = buff_each + whole_pack_bites;
+ if (*pack_tail == 0x47 && *(pack_tail-188) == 0x47)
+ {
+ ret_ts_each = 1;
+ //printf("p-buff is %d.\n",p-buff);
+ //printf("pack_tail - buff is %d.\n",pack_tail-buff);
+ break;
+ }
+ break;
+ }
+ }
+ else
+ {
+ break;
+ }
- size_t cont_cout_ori, cont_cout_end, cont_cout_pre, cont_cout_aft;
- cont_cout_ori = htonl(*(size_t *)buff_each) & 0x0000000f;
- cont_cout_pre = cont_cout_ori;
- while (p < (buff_each + size_each))
- {
- if (*p == 0x47)
- {
- if ((p + 3) < (buff_each + size_each))
- {
- cont_cout_aft = htonl(*(size_t *)p) & 0x0000000f;
- if (cont_cout_aft == (cont_cout_pre + 1) % 16)
- {
- fit_times++;
- }
- else
- {
- break;
- }
- }
- if (fit_times > FIT_TIMES)
- {
- size_t whole_pack_cout = size_each / 188;
- if (size_each % 188 == 0)
- {
- whole_pack_cout--;
- }
- size_t whole_pack_bites = whole_pack_cout * 188;
- const char *pack_tail = buff_each + whole_pack_bites;
- if (*pack_tail == 0x47 && *(pack_tail-188) == 0x47)
- {
- ret_ts_each = 1;
- break;
- }
- break;
- }
+ cont_cout_pre = cont_cout_aft;
+ p += 188;
}
- else
- {
- break;
- }
-
- cont_cout_pre = cont_cout_aft;
- p += 188;
- }
- return ret_ts_each;
+ return ret_ts_each;
}
int is_mpeg_ts(const char* buff, size_t size)
{
- int ret_ts = 0;
- const char *p_each = buff;
- int ret_ts_each;
- while (p_each < buff + size)
- {
- p_each = memchr(p_each, 0x47, size - (p_each - buff));
- if (p_each == NULL)
- {
- break;
- }
- else if (p_each +3 >= buff + size)
- {
- break;
- }
- else
- {
- ret_ts_each = is_mpeg_ts_each(p_each, size - (p_each - buff), buff, size);
- if (ret_ts_each == 1)
- {
- ret_ts = 1;
- break;
- }
- p_each++;
- }
- }
- return ret_ts;
+ int ret_ts = 0;
+ const char *p_each = buff;
+ int ret_ts_each;
+ while (p_each < buff + size)
+ {
+ p_each = memchr(p_each, 0x47, size - (p_each - buff));
+ if (p_each == NULL)
+ {
+ break;
+ }
+ else if (p_each +3 >= buff + size)
+ {
+ break;
+ }
+ else
+ {
+ ret_ts_each = is_mpeg_ts_each(p_each, size - (p_each - buff), buff, size);
+ if (ret_ts_each == 1)
+ {
+ ret_ts = 1;
+ break;
+ }
+ p_each++;
+ }
+ }
+ return ret_ts;
}
void *memchr_flv(const char *buff_each, size_t size_each)
{
- void *ret_memflv = NULL;
- const char *p = buff_each;
- while (p < buff_each + size_each)
- {
- if (*p == 0x08 || *p == 0x09 || *p == 0x12)
- {
- ret_memflv = (void *)p;
- break;
- }
- p++;
- }
- return ret_memflv;
+ void *ret_memflv = NULL;
+ //int i;
+ const char *p = buff_each;
+ while (p < buff_each + size_each)
+ {
+ if (*p == 0x08 || *p == 0x09 || *p == 0x12)
+ {
+ ret_memflv = (void *)p;
+ break;
+ }
+ p++;
+ }
+ return ret_memflv;
}
int is_flv_each(const char* buff_each, size_t size_each, const char *buff)
{
- int ret_flv_each = 0;
- const char *p = buff_each;
- int fit_times = 1;
- size_t left;
- size_t right = 11 + htonl(*(size_t *)p) & 0x00ffffff;
- p += (right + 4);
+ int ret_flv_each = 0;
+ const char *p = buff_each;
+ int fit_times = 1;
+ size_t left;
+ size_t right = 11 + htonl(*(size_t *)p) & 0x00ffffff;
+ p += (right + 4);
- while (p < (buff_each + size_each))
- {
- left = htonl(*(size_t *)(p - 4));
- if (right==left&&(*(p) == 0x08 || *(p) == 0x09 || *(p) == 0x12))
- {
- fit_times++;
- if (fit_times > FIT_TIMES)
- {
- ret_flv_each = 1;
- break;
- }
- }
- else
- {
- break;
- }
- if ((p + 3) >= (buff_each + size_each))
- {
- break;
- }
- right = 11 + htonl(*(size_t *)p) & 0x00ffffff;
- p += (right + 4);
- }
- return ret_flv_each;
+ while (p < (buff_each + size_each))
+ {
+ left = htonl(*(size_t *)(p - 4));
+ if (right==left&&(*(p) == 0x08 || *(p) == 0x09 || *(p) == 0x12))
+ {
+ fit_times++;
+ if (fit_times > FIT_TIMES)
+ {
+ ret_flv_each = 1;
+ //printf("p-buff is %d.\n",p-buff);
+ break;
+ }
+ }
+ else
+ {
+ break;
+ }
+ if ((p + 3) >= (buff_each + size_each))
+ {
+ 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 = buff;
- const char *p_each = buff;
+ int ret_flv = 0;
+ int ret_flv_each;
+ const char*p = buff;
+ 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),buff);
- if (ret_flv_each == 1)
- {
- ret_flv = 1;
- break;
- }
- p_each++;
- }
- }
- return ret_flv;
+ 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),buff);
+ if (ret_flv_each == 1)
+ {
+ ret_flv = 1;
+ break;
+ }
+ p_each++;
+ }
+ }
+ return ret_flv;
}
int is_mp4_part_spe(const char *buff)
{
- int i;
- size_t size_type_h;
- for (i = 0; i < count_type; i++)
- {
- size_type_h = strlen(mp4_box_type_all[i]);
- if (memcmp(buff, mp4_box_type_all[i], size_type_h) == 0)
- return 1;
- }
- return 0;
+ int i;
+ size_t size_type_h;
+ for (i = 0; i < count_type; i++)
+ {
+ size_type_h = strlen(mp4_box_type_all[i]);
+ if (memcmp(buff, mp4_box_type_all[i], size_type_h) == 0)
+ return 1;
+ }
+ return 0;
}
int c_is_mp3(char c)
{
- if (c >= 'a'&&c <= 'z' || c >= '0'&&c <= '9')
- {
- return 1;
- }
- else
- {
- return 0;
- }
+ if (c >= 'a'&&c <= 'z' || c >= '0'&&c <= '9')
+ {
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
}
int is_mp4_part(const char *buff_part)
{
- if (c_is_mp3(*buff_part) == 1 && c_is_mp3(*(buff_part + 1)) == 1 && c_is_mp3(*(buff_part + 2)) == 1 && c_is_mp3(*(buff_part + 3)) == 1)
- {
- return 1;
- }
- return 0;
+ if (c_is_mp3(*buff_part) == 1 && c_is_mp3(*(buff_part + 1)) == 1 && c_is_mp3(*(buff_part + 2)) == 1 && c_is_mp3(*(buff_part + 3)) == 1)
+ {
+ return 1;
+ }
+ return 0;
}
void *memmem_mp4(const char *buff_each, size_t size_each)
{
- void *ret_memmp4 = NULL;
- char s;
- const char *p = buff_each;
- while (p + 3 < buff_each + size_each)
- {
- if (is_mp4_part(p) == 1)
+ void *ret_memmp4 = NULL;
+ char s;
+ const char *p = buff_each;
+ //int i;
+ while (p + 3 < buff_each + size_each)
{
- ret_memmp4 = (void *)p;
- break;
- }
- p++;
- }
- return ret_memmp4;
+ //if (is_mp4_part_spe(p) == 1)
+ //if (is_mp4_part(p) == 1 && is_mp4_part_spe(p) == 1)
+ //if (c_is_mp3(*p) == 1 && is_mp4_part_spe(p) == 1)
+ if (is_mp4_part(p) == 1)
+ {
+ ret_memmp4 = (void *)p;
+ break;
+ }
+ p++;
+ }
+ return ret_memmp4;
}
int is_mp4_each(const char* buff_each, size_t size_each, const char *buff)
{
- int ret_mp4_each = 0;
- int ret_mp4_part;
- int sig_spe = 0;
- if (is_mp4_part_spe(buff_each) == 1)
- {
- sig_spe = 1;
- }
- const char *p = buff_each;
- int fit_times = 1;
-
- size_t size_h;
-
- p += 8;
- while (p + 4 < (buff_each + size_each))
- {
- ret_mp4_part = is_mp4_part(p);
- if (ret_mp4_part == 1)
- {
- fit_times++;
- if (is_mp4_part_spe(p) == 1)
- {
- sig_spe++;
- }
- if (fit_times > FIT_TIMES&&sig_spe >= 1)
- {
- ret_mp4_each = 1;
- break;
- }
- if (fit_times > FIT_TIMES+1)
- {
- break;
- }
- }
-
- else
- {
- p -= 8;
- size_h = htonl(*(size_t *)(p - 4));
- p += size_h;
- if (p + 4 < buff_each + size_each)
- {
- ret_mp4_part = is_mp4_part(p);
- if (ret_mp4_part == 1)
- {
- fit_times++;
- if (is_mp4_part_spe(p) == 1)
- {
- sig_spe++;
- }
- if (fit_times > FIT_TIMES&&sig_spe >= 1)
- {
- ret_mp4_each = 1;
- break;
- }
- if (fit_times > FIT_TIMES + 1)
- {
- break;
- }
- }
- else
- {
- break;
- }
- }
- }
- p += 8;
- }
- return ret_mp4_each;
+ int ret_mp4_each = 0;
+ int ret_mp4_part;
+ int sig_spe = 0;
+ if (is_mp4_part_spe(buff_each) == 1)
+ {
+ sig_spe = 1;
+ }
+ const char *p = buff_each;
+ int fit_times = 1;
+
+ size_t size_h;
+
+ p += 8;
+ while (p + 4 < (buff_each + size_each))
+ {
+ ret_mp4_part = is_mp4_part(p);
+ if (ret_mp4_part == 1)
+ {
+ fit_times++;
+ if (is_mp4_part_spe(p) == 1)
+ {
+ sig_spe++;
+ }
+ if (fit_times > FIT_TIMES&&sig_spe >= 1)
+ {
+ ret_mp4_each = 1;
+ //printf("p-buff is %d.\n",p-buff);
+ break;
+ }
+ if (fit_times > FIT_TIMES+1)
+ {
+ break;
+ }
+ }
+
+ else
+ {
+ p -= 8;
+ size_h = htonl(*(size_t *)(p - 4));
+ p += size_h;
+ if (p + 4 < buff_each + size_each)
+ {
+ ret_mp4_part = is_mp4_part(p);
+ if (ret_mp4_part == 1)
+ {
+ fit_times++;
+ if (is_mp4_part_spe(p) == 1)
+ {
+ sig_spe++;
+ }
+ if (fit_times > FIT_TIMES&&sig_spe >= 1)
+ {
+ ret_mp4_each = 1;
+ break;
+ }
+ if (fit_times > FIT_TIMES + 1)
+ {
+ break;
+ }
+ }
+ else
+ {
+ 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 = buff;
- const char *p_each = buff;
+ int ret_mp4 = 0;
+ int ret_mp4_each;
+ const char *p = buff;
+ 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 || p_each - 4 < buff)
- {
- break;
- }
- else
- {
- ret_mp4_each = is_mp4_each(p_each, size - (p_each - buff), buff);
- if (ret_mp4_each == 1)
- {
- ret_mp4 = 1;
- break;
- }
- p_each++;
- }
- }
- return ret_mp4;
+ 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 || p_each - 4 < buff)
+ {
+ break;
+ }
+ else
+ {
+ ret_mp4_each = is_mp4_each(p_each, size - (p_each - buff), buff);//,count_type);//int ret = is_mp4_each(p_each, size - (p_each - buff), buff,count_type);
+ 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;
+ 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;
}