H265帧类型判断
H264的Nal Unit头是一个字节,265变成两个字节
和H264的&0x1f不同。H265是
int type =(code & 0x7E)>>1;
0-9是P帧,16-21是关键(I)帧,32-40是VPS SPS PPS SUFFIX SEI NUT等
int read_one_frame(FILE* fp, char** outdate) {
int size = 0;
int pps = 0;
char* ptr = calloc(1, 1024);
static unsigned char ts[5] = { 0 };
//防止文件数据错误
if (fread(ptr, 1, 4, fp) < 4) {
log_i("read start error\n");
free(ptr);
return size;
}
if ((*ptr == 0x00) && (*(ptr + 1) == 0x00) && (*(ptr + 2) == 0x00) && (*(ptr + 3) == 0x01)) {
size = 4;
while (1) {
int len = fread(ptr + size, 1, 1024 - size % 1024, fp);
if (len>0) {
for (int i = 0; i < len;)
{
ts[0] = ts[1];
ts[1] = ts[2];
ts[2] = ts[3];
ts[3] = ts[4];
ts[4] = *(ptr + size);
size++;
i++;
if ((ts[0] == 0x00) && (ts[1] == 0x00) && (ts[2] == 0x00) && (ts[3] == 0x01)) {
//读取到下一帧的起始码,即表示这帧读完了,后移文件指针
int code = (ts[4] & 0x7E) >> 1;
if (code>31)
{
pps = 1;
continue;
}if (15 < code && code < 22 && pps)
{
continue;
}
size -= 5;
fseek(fp, -5 - len + i, SEEK_CUR);
goto END;
}
}
if (0 == size % 1024)
{
ptr = realloc(ptr, size + 1024);
}
}
else
{
fseek(fp, 0, SEEK_SET);
break;//读完文件退出循环
}
}
END: //读取到的数据大小
*outdate = ptr;
}
else {
}
return size;
}