CMSampleBufferRef获取h264 char*数据及sps/pps

来源:互联网 发布:索尼a5000 知乎 编辑:程序博客网 时间:2024/06/05 09:13

   直接看代码:


CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuf);    size_t len_offset = 0 , len_total = 0 ;    char * addr = NULL ;    OSStatus status = CMBlockBufferGetDataPointer(blockBuffer, 0, &len_offset, &len_total, &addr);    if(addr){        NSLog(@"h264 data : %d,%d,%d,%d,%d,%d,%d,%d,%d,%d",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5],addr[6],addr[7],addr[8],addr[9]);    }    CFArrayRef cfArr =  CMSampleBufferGetSampleAttachmentsArray(sampleBuf, true);    if (!cfArr) {        return YES;    }    CFDictionaryRef cfDict = (CFDictionaryRef)(CFArrayGetValueAtIndex(cfArr, 0));    bool keyframe = !CFDictionaryContainsKey(cfDict, kCMSampleAttachmentKey_NotSync);    if (status == noErr)    {        if (keyframe)        {            CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuf);            size_t sparameterSetSize, sparameterSetCount;            const uint8_t *sparameterSet;            OSStatus statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 0, &sparameterSet, &sparameterSetSize, &sparameterSetCount, 0 );            if (statusCode == noErr)            {                // Found sps and now check for pps                size_t pparameterSetSize, pparameterSetCount;                const uint8_t *pparameterSet;                OSStatus statusCode = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pparameterSet, &pparameterSetSize, &pparameterSetCount, 0 );                if (statusCode == noErr)                {                }                            }            else            {                NSLog(@"================ error GetH264Parameter...");            }        }                }


原创粉丝点击