参考链接: 1. HEVC码流解析 https://blog.csdn.net/CrystalShaw/article/details/80624804
2. HEVC编码结构:序列参数集SPS、图像参数集PPS、视频参数集VPS https://blog.csdn.net/lin453701006/article/details/52797104
3. H265码流结构分析 https://blog.csdn.net/u011003120/article/details/83411445#11_H265_4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h> #define TAB44 " "
#define PRINTF_DEBUG #define PRTNTF_STR_LEN 10 typedef enum e_hevc_nalu_type {
HEVC_NAL_TRAIL_N = ,
HEVC_NAL_TRAIL_R = ,
HEVC_NAL_TSA_N = ,
HEVC_NAL_TSA_R = ,
HEVC_NAL_STSA_N = ,
HEVC_NAL_STSA_R = ,
HEVC_NAL_RADL_N = ,
HEVC_NAL_RADL_R = ,
HEVC_NAL_RASL_N = ,
HEVC_NAL_RASL_R = ,
HEVC_NAL_VCL_N10 = ,
HEVC_NAL_VCL_R11 = ,
HEVC_NAL_VCL_N12 = ,
HEVC_NAL_VCL_R13 = ,
HEVC_NAL_VCL_N14 = ,
HEVC_NAL_VCL_R15 = ,
HEVC_NAL_BLA_W_LP = ,
HEVC_NAL_BLA_W_RADL = ,
HEVC_NAL_BLA_N_LP = ,
HEVC_NAL_IDR_W_RADL = ,
HEVC_NAL_IDR_N_LP = ,
HEVC_NAL_CRA_NUT = ,
HEVC_NAL_IRAP_VCL22 = ,
HEVC_NAL_IRAP_VCL23 = ,
HEVC_NAL_RSV_VCL24 = ,
HEVC_NAL_RSV_VCL25 = ,
HEVC_NAL_RSV_VCL26 = ,
HEVC_NAL_RSV_VCL27 = ,
HEVC_NAL_RSV_VCL28 = ,
HEVC_NAL_RSV_VCL29 = ,
HEVC_NAL_RSV_VCL30 = ,
HEVC_NAL_RSV_VCL31 = ,
HEVC_NAL_VPS = ,
HEVC_NAL_SPS = ,
HEVC_NAL_PPS = ,
HEVC_NAL_AUD = ,
HEVC_NAL_EOS_NUT = ,
HEVC_NAL_EOB_NUT = ,
HEVC_NAL_FD_NUT = ,
HEVC_NAL_SEI_PREFIX = ,
HEVC_NAL_SEI_SUFFIX =
} E_HEVC_NALU_TYPE; /********************************************************************************
typedef struct t_h264_nalu_header
{
unsigned char forbidden_bit:1, nal_reference_idc:2, nal_unit_type:5;
} T_H264_NALU_HEADER; (1个字节, hevc header为2个字节)
*********************************************************************************/
typedef struct t_h265_nalu_header
{
unsigned short forbidden_zero_bit:, nal_unit_type:, nuh_layer_id:, nuh_temporal_id_plus1:;
} T_H265_NALU_HEADER; typedef struct t_h265_nalu
{
int startCodeLen; T_H265_NALU_HEADER h265NaluHeader; unsigned int bodyLen; unsigned char *bodyData;
} T_H265_NALU; /**********************************************************************************
1. h265的起始码: 0x000001(3 Bytes)或0x00000001(4 Bytes);
2. 文件流中用起始码来区分NALU;
3. 如果NALU类型为vps, sps, pps, 或者解码顺序为第一个AU的第一个NALU, 起始码前面再加一个0x00
视频流的首个NALU的起始码前加入0x00(4 Bytes的由来).
***********************************************************************************/
static int FindStartCode3Bytes(unsigned char *scData)
{
int isFind = ; if ((==scData[]) && (==scData[]) && (==scData[]))
{
isFind = ;
} return isFind;
} static int FindStartCode4Bytes(unsigned char *scData)
{
int isFind = ; if ((==scData[]) && (==scData[]) && (==scData[]) && ( == scData[]))
{
isFind = ;
} return isFind;
} static int GetNaluDataLen(int startPos, int h265BitsSize, unsigned char *h265Bits)
{
int parsePos = ; parsePos = startPos; while (parsePos < h265BitsSize)
{
if (FindStartCode3Bytes(&h265Bits[parsePos]))
{
return parsePos - startPos;
}
else if (FindStartCode4Bytes(&h265Bits[parsePos]))
{
return parsePos - startPos;
}
else
{
parsePos++;
}
} return parsePos - startPos; // if file is end
} static void ParseNaluData(const unsigned int naluLen, unsigned char* const nuluData)
{
static int naluNum = ; unsigned char *data = NULL;
unsigned char typeStr[PRTNTF_STR_LEN+] = {}; T_H265_NALU_HEADER h265NaluHeader = {}; data = nuluData; memset(&h265NaluHeader, 0x0, sizeof(T_H265_NALU_HEADER)); h265NaluHeader.nal_unit_type = ((data[]>>) & 0x3f); naluNum++; #ifdef PRINTF_DEBUG
switch (h265NaluHeader.nal_unit_type)
{
case HEVC_NAL_TRAIL_N:
sprintf(typeStr, "B SLICE");
break; case HEVC_NAL_TRAIL_R:
sprintf(typeStr, "P SLICE");
break; case HEVC_NAL_IDR_W_RADL:
sprintf(typeStr, "IDR");
break; case HEVC_NAL_VPS:
sprintf(typeStr, "VPS");
break; case HEVC_NAL_SPS:
sprintf(typeStr, "SPS");
break; case HEVC_NAL_PPS:
sprintf(typeStr, "PPS");
break; case HEVC_NAL_SEI_PREFIX:
sprintf(typeStr, "SEI");
break; default:
sprintf(typeStr, "NTYPE(%d)", h265NaluHeader.nal_unit_type);
break;
} printf("%5d| %7s| %8d|\n", naluNum, typeStr, naluLen);
#endif
} int main(int argc, char *argv[])
{
int fileLen = ;
int naluLen = ;
int h265BitsPos = ; /* h265, hevc; h264, avc系列, Advanced Video Coding */ unsigned char *h265Bits = NULL;
unsigned char *naluData = NULL; FILE *fp = NULL; if ( != argc)
{
printf("Usage: flvparse **.flv\n"); return -;
} fp = fopen(argv[], "rb");
if (!fp)
{
printf("open file[%s] error!\n", argv[]); return -;
} fseek(fp, , SEEK_END); fileLen = ftell(fp); fseek(fp, , SEEK_SET); h265Bits = (unsigned char*)malloc(fileLen);
if (!h265Bits)
{
printf("maybe file is too long, or memery is not enough!\n"); fclose(fp); return -;
} memset(h265Bits, 0x0, fileLen); if (fread(h265Bits, , fileLen, fp) < )
{
printf("read file data to h265Bits error!\n"); fclose(fp);
free(h265Bits); h265Bits = NULL; return -;
} fclose(fp); printf("-----+--- NALU Table --+\n");
printf(" NUM | TYPE | LEN |\n");
printf("-----+-------+---------+\n"); while (h265BitsPos < (fileLen-))
{
if (FindStartCode3Bytes(&h265Bits[h265BitsPos]))
{
naluLen = GetNaluDataLen(h265BitsPos+, fileLen, h265Bits); naluData = (unsigned char*)malloc(naluLen);
if (naluData)
{
memset(naluData, 0x0, naluLen); memcpy(naluData, h265Bits+h265BitsPos+, naluLen); ParseNaluData(naluLen, naluData); free(naluData);
naluData = NULL;
} h265BitsPos += (naluLen+);
}
else if (FindStartCode4Bytes(&h265Bits[h265BitsPos]))
{
naluLen = GetNaluDataLen(h265BitsPos+, fileLen, h265Bits); naluData = (unsigned char*)malloc(naluLen);
if (naluData)
{
memset(naluData, 0x0, naluLen); memcpy(naluData, h265Bits+h265BitsPos+, naluLen); ParseNaluData(naluLen, naluData); free(naluData);
naluData = NULL;
} h265BitsPos += (naluLen+);
}
else
{
h265BitsPos++;
}
} return ;
}
最后如果您觉得本篇对您有帮助,可以打赏下,谢谢!!!