从CMSampleBufferRef获取sps和pps

时间:2014-06-20 00:03:53

标签: video-streaming video-processing video-encoding ios8

我使用新API将图像从相机推送到videoToolBox编码器 并从编码器回调中获取编码的CMSampleBufferRef

我需要这些sps和pts用于CMVideoFormatDescriptionCreateFromH264ParameterSets 配置解码器

任何人都可以帮助/指导我吗? )THX

2 个答案:

答案 0 :(得分:5)

反过来很容易做到,相关函数是CMVideoFormatDescriptionGetH264ParameterSetAtIndex,可以使用像

这样的函数
CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuffer);
size_t spsSize, ppsSize;
size_t parmCount;
const uint8_t* sps, *pps;

CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 0, &sps, &spsSize, &parmCount, nullptr );
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pps, &ppsSize, &parmCount, nullptr );

答案 1 :(得分:1)

提取' avcC' ' SampleDescriptionExtensionAtoms'的元素字典,然后使用' CFDataGetLength'和' CFDataGetBytePtr'要获得直接指向avcC结构的指针,可以通过以下方式解析此结构:

#pragma pack(push, 1)
struct AVCC {
    uint8_t  version;
    uint8_t  profile_idc;
    uint8_t  compatibility;
    uint8_t  level_idc;
    uint8_t  nalu_size  : 2;// indicates the length in bytes of the NALUnitLength field in an AVC video sample or AVC parameter set sample of the associated stream **minus one**
    uint8_t  reserved1  : 6;
    uint8_t  numSPS     : 5;// length size minus one
    uint8_t  reserved2  : 3;
    uint16_t SPSlen; 
    uint8_t  pSPS[15];      // Sequence parameter set
    uint8_t  numPPS;
    uint16_t PPSlen;
    uint32_t pPPS[1];       // Picture parameter set
};
#pragma pack(pop)

int _tmain(int argc, _TCHAR* argv[])
{
    AVCC* pAVCC = (AVCC*)g_pAVCC;

    pAVCC->SPSlen = ((pAVCC->SPSlen & 0x00FF) << 8) | ((pAVCC->SPSlen & 0xFF00) >> 8);
    pAVCC->PPSlen = ((pAVCC->PPSlen & 0x00FF) << 8) | ((pAVCC->PPSlen & 0xFF00) >> 8);


    uint8_t* pSPS = (uint8_t*)pAVCC->pSPS;
    uint8_t* pPPS = (uint8_t*)pAVCC->pPPS;

    ...

    return 0;
}