常用音视频头的写法

一、如何判断视频I_FRAME

bool IsH264IFrame(unsigned char data) {

    const unsigned char SPS_TYPE = 7;
    const unsigned char PPS_TYPE = 8;
    const unsigned char IDR_TYPE = 5;


    unsigned char naltype = data & 0x1f;
    //log.Infof("IsH264IFrame:0x%02x", naltype)
    if (naltype == IDR_TYPE || naltype == SPS_TYPE || naltype == PPS_TYPE) {
        return true;
    }
    return false;

}

二、生成AAC的ADTS

adts如何定义,相关文章(http://blog.csdn.net/jay100500/article/details/52955232)

int mpeg4_aac_adts_save(size_t payload, uint8_t* data)
{
    const uint8_t ID = 0; // 0-MPEG4/1-MPEG2
    size_t len = payload + 7;
    if (len >= (1 << 12)) return -1;


    int profile = 1;
    int channel_configuration = 2;
    int sampling_frequency_index = 4;

    data[0] = 0xFF; /* 12-syncword */
    data[1] = 0xF0 /* 12-syncword */ | (ID << 3)/*1-ID*/ | (0x00 << 2) /*2-layer*/ | 0x01 /*1-protection_absent*/;
    data[2] = ((profile - 1) << 6) | ((sampling_frequency_index & 0x0F) << 2) | ((channel_configuration >> 2) & 0x01);
    data[3] = ((channel_configuration & 0x03) << 6) | ((len >> 11) & 0x03); /*0-original_copy*/ /*0-home*/ /*0-copyright_identification_bit*/ /*0-copyright_identification_start*/
    data[4] = (uint8_t)(len >> 3);
    data[5] = ((len & 0x07) << 5) | 0x1F;
    data[6] = 0xFC | ((len / 1024) & 0x03);
    return 7;
}

三、AVC decoder configuration record

在ffmpeg编程中,sps,pps是放在AVCodecContext->extradata, AVCodecContext->extradata_size中

extradata中的数据中,sps和pps的是按照AVC decoder configuration record的方式排列的,如下所示:

aligned(8) class AVCDecoderConfigurationRecord {

unsigned int(8) configurationVersion = 1;

unsigned int(8) AVCProfileIndication;

unsigned int(8) profile_compatibility;

unsigned int(8) AVCLevelIndication;

bit(6) reserved = ‘111111’b;
unsigned int(2) lengthSizeMinusOne;
bit(3) reserved = ‘111’b;
unsigned int(5) numOfSequenceParameterSets;
for (i=0; i< numOfSequenceParameterSets; i++) {

    unsigned int(16) sequenceParameterSetLength ;

    bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;

}

unsigned int(8) numOfPictureParameterSets;
for (i=0; i< numOfPictureParameterSets; i++) {

    unsigned int(16) pictureParameterSetLength;

    bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;

}

.......

具体算法: 

sps_len  = htons(*(unsigned short*)(_pVideoStream->codecpar->extradata + 6));
sps_data = _pVideoStream->codecpar->extradata + 6 + sizeof(sps_len);
pps_len  = htons(*(unsigned short*)(_pVideoStream->codecpar->extradata + 6 + sizeof(sps_len) + sps_len + 1));
pps_data = _pVideoStream->codecpar->extradata + 6 + 2 + sps_len + 1 + sizeof(pps_len);



发布了21 篇原创文章 · 获赞 18 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/sweibd/article/details/79175246