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

本文介绍如何从 CMSampleBufferRef 中获取 h264 编码的 char* 数据,并详细阐述了提取 SPS 和 PPS 的过程,适用于多媒体处理和视频编码相关的开发工作。

   直接看代码:


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...");
            }
        }
            
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值