本文主要是介绍H264码流中 分离和解析 nal,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
H.264码流解析的步骤就是首先从码流中搜索0x000001和0x00000001,
然后再分离出NALU;分析NALU的各个字段。
找到00 00 01 或 00 00 00 01
if (3 != fread (Buf, 1, 3, h264bitstream)){ free(Buf); return 0; } info2 = FindStartCode2 (Buf); if(info2 != 1) { if(1 != fread(Buf+3, 1, 1, h264bitstream)){ free(Buf); return 0; } info3 = FindStartCode3 (Buf); if (info3 != 1){ free(Buf); return -1; } else { pos = 4; nalu->startcodeprefix_len = 4; } } else{ nalu->startcodeprefix_len = 3; pos = 3; } StartCodeFound = 0; info2 = 0; info3 = 0;
分离NALU程序:
static int FindStartCode2 (unsigned char *Buf){ if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) return 0; //0x000001? else return 1;
} static int FindStartCode3 (unsigned char *Buf){ if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1) return 0;//0x00000001? else return 1;
} while (!StartCodeFound){ if (feof (h264bitstream)){ nalu->len = (pos-1)-nalu->startcodeprefix_len; memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len); nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit free(Buf); return pos-1; } Buf[pos++] = fgetc (h264bitstream); info3 = FindStartCode3(&Buf[pos-4]); if(info3 != 1) info2 = FindStartCode2(&Buf[pos-3]); StartCodeFound = (info2 == 1 || info3 == 1); }
解析:
rewind = (info3 == 1)? -4 : -3;
fseek (h264bitstream, rewind, SEEK_CUR)nalu->len = (pos+rewind)-nalu->startcodeprefix_len; memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);// nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit
这篇关于H264码流中 分离和解析 nal的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!