音视频笔记:H264、H265的NAL单元类型

一、NALU概念

NAL的全称是Network abstraction layer,叫网络抽象层,它保存了H264相关的参数信息和图像信息,NAL层由多个单元NALU组成,NALU由了NALU头(00 00 00 01或者00 00 01)、sps(序列参数集)、pps(图像参数集合)、slice、sei、IDR帧、I帧、P帧、B帧等数据。

二、有啥用

在一个完整的视频流 第一个图像帧是IDR帧,IDR帧是强制刷新帧,在解码过程中,当出现了IDR帧时,要更新sps、pps,原因是防止前面I帧错误,导致sps,pps参考I帧导致无法纠正。

三、H264 NAUL

 #define NALU_TYPE_SLICE 1
 #define NALU_TYPE_DPA 2
 #define NALU_TYPE_DPB 3
 #define NALU_TYPE_DPC 4
 #define NALU_TYPE_IDR 5
 #define NALU_TYPE_SEI 6
 #define NALU_TYPE_SPS 7
 #define NALU_TYPE_PPS 8
 #define NALU_TYPE_AUD 9
 #define NALU_TYPE_EOSEQ 10
 #define NALU_TYPE_EOSTREAM 11
 #define NALU_TYPE_FILL 12

其中重要的是 IDR 5

四、H265 NAUL

NAL_TRAIL_N = 0, 
NAL_TRAIL_R = 1, 
NAL_TSA_N = 2, 
NAL_TSA_R = 3, 
NAL_STSA_N = 4, 
NAL_STSA_R = 5, 
NAL_RADL_N = 6, 
NAL_RADL_R = 7, 
NAL_RASL_N = 8, 
NAL_RASL_R = 9, 
NAL_BLA_W_LP = 16, 
NAL_BLA_W_RADL = 17, 
NAL_BLA_N_LP = 18, 
NAL_IDR_W_RADL = 19, 
NAL_IDR_N_LP = 20, 
NAL_CRA_NUT = 21, 
NAL_VPS = 32, 
NAL_SPS = 33, 
NAL_PPS = 34, 
NAL_AUD = 35, 
NAL_EOS_NUT = 36, 
NAL_EOB_NUT = 37, 
NAL_FD_NUT = 38, 
NAL_SEI_PREFIX = 39, 
NAL_SEI_SUFFIX = 40,

其中重要的是 I帧 19

五、获取NaulType

  private static int getH264NaluType(byte[] buffer) {
    if (buffer.length > 4 && (buffer[0] == 0x0) && (0x0 == buffer[1]) && (buffer[2] == 0x0)
            && (0x01 == buffer[3])) {
        return buffer[4] & 0x1F;
    } else if (buffer.length > 3 && (buffer[0] == 0x0) && (0x0 == buffer[1]) && (0x01 == buffer[2])) {
        return buffer[3] & 0x1F;
    } else if (buffer.length > 1) {
        return buffer[0] & 0x1F;
    } else {
        return 0;
    }
}

private static int getH265NaluType(byte[] buffer) {
    if (buffer.length > 4 && (buffer[0] == 0x0) && (0x0 == buffer[1]) && (buffer[2] == 0x0)
            && (0x01 == buffer[3])) {
        return (buffer[4] >> 1) & 0x3F;
    } else if (buffer.length > 3 && (buffer[0] == 0x0) && (0x0 == buffer[1]) && (0x01 == buffer[2])) {
        return (buffer[3] >> 1) & 0x3F;
    } else if (buffer.length > 1) {
        return (buffer[0] >> 1) & 0x3F;

    } else {
        return 0;
    }
}

你可能感兴趣的:(音视频笔记:H264、H265的NAL单元类型)