ios硬编解码videotoolbox---硬解码

一、videoToolbox的基本数据

Video Toolbox视频编解码前后需要应用的数据结构进行说明。

CVPixelBuffer:编码前和解码后的图像数据结构。此内容包含一系列的CVPixelBufferPool内容

CMTime、CMClock和CMTimebase:时间戳相关。时间以64-bit/32-bit的形式出现。

pixelBufferAttributes:字典设置.可能包括Width/height、pixel format type、Compatibility (e.g., OpenGL ES, Core Animation)

CMBlockBuffer:编码后,结果图像的数据结构。

CMVideoFormatDescription:图像存储方式,编解码器等格式描述。

CMTimebase: 关于CMClock的一个控制视图,包含CMClock、时间映射(Time mapping)、速率控制(Rate control)

CMSampleBuffer:存放编解码前后的视频图像的容器数据结构。

编码前后的视频图像都封装在 CMSampleBuffer中,编码前以 CVPixelBuffer 进行存储;编码后以 CMBlockBuffer进行存储。除此之外两者都括 CMTim 、 CMVideoFormatDesc。

ios硬编解码videotoolbox---硬解码_第1张图片

二、硬解码基本流程

1、由sps和pps创建CMVideoFormatDescription,每次重新来sps和pps都要重新创建。

const uint8_t* parameterSetPointers[2] = {mSPS, mPPS};
const size_t parameterSetSizes[2] = {mSPSSize, mPPSSize};
OSStatus status = 
CMVideoFormatDescriptionCreateFromH264ParameterSets(kCFAllocatorDefault,
                                                                 2, //param count
                                                                 parameterSetPointers,
                                                                 parameterSetSizes,
                                                                 4, //nal start code size
                                                                 &mFormatDescription);

2、由CMVideoFormatDescription创建mDecodeSession,以及指定获取解码数据的回调函数。创建解码会话需要提供回调函数以便系统解码完成时将解码数据、状态等信息返回给用户。

            CFDictionaryRef attrs = NULL;
            const void *keys[] = { kCVPixelBufferPixelFormatTypeKey };
            //      kCVPixelFormatType_420YpCbCr8Planar is YUV420
            //      kCVPixelFormatType_420YpCbCr8BiPlanarFullRange is NV12
            uint32_t v = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
            const void *values[] = { CFNumberCreate(NULL, kCFNumberSInt32Type, &v) };
            attrs = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
            
            VTDecompressionOutputCallbackRecord callBackRecord;
            callBackRecord.decompressionOutputCallback = didDecompress;
            callBackRecord.decompressionOutputRefCon = NULL;
//这个是用来做异步解码实现的,decompressionOutputRefCon 为需要指定的对象,即自己。didDecompress则为回调函数。
            
            status = VTDecompressionSessionCreate(kCFAllocatorDefault,
                                                  mFormatDescription,
                                                  NULL, attrs,
                                                  &callBackRecord,
                                                  &mDecodeSession);
            CFRelease(attrs);

3、由编码的数据(I、P、B帧数据)创建CMBlockBuffer。

 CMBlockBufferRef blockBuffer = NULL;
 OSStatus status  = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
                                                      (void*)packetBuffer, packetSize,
                                                      kCFAllocatorNull,
                                                      NULL, 0, packetSize,
                                                      0, &blockBuffer);

4、由CMBlockBuffer和CMVideoFormatDescription创建CMSampleBuffer

CMSampleBufferRef sampleBuffer = NULL;
const size_t sampleSizeArray[] = {packetSize};
status = CMSampleBufferCreateReady(kCFAllocatorDefault,
                                               blockBuffer,
                                               mFormatDescription,
                                               1, 0, NULL, 1, sampleSizeArray,
                                               &sampleBuffer);

5、解码
VideoToolbox支持同、异步解码,由VTDecodeFrameFlags指定,VTDecodeFrameFlags flags = kVTDecodeFrame_EnableAsynchronousDecompression; ,默认为同步解码。
同步解码时,调用解码函数 VTDecompressionSessionDecodeFrame 后系统回调我们提供的回调函数,然后解码函数才结束调用。异步解码则回调顺序不确定,故需要自行整理帧序。

VTDecodeFrameFlags flags = 0;
VTDecodeInfoFlags flagOut = 0;
// 默认是同步操作。
// 调用didDecompress,返回后再回调
OSStatus decodeStatus = VTDecompressionSessionDecodeFrame(mDecodeSession,
                                           sampleBuffer,
                                           flags,
                                           &outputPixelBuffer,//输出解码数据
                                           &flagOut);

6、异步解码回调,调用解码函数后回掉函数didDecompress,用来获取解码出的数据。

void didDecompress(void *decompressionOutputRefCon, void *sourceFrameRefCon, 
OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef pixelBuffer, 
CMTime presentationTimeStamp, CMTime presentationDuration ){

    CVPixelBufferRef *outputPixelBuffer = (CVPixelBufferRef *)sourceFrameRefCon;

    *outputPixelBuffer = CVPixelBufferRetain(pixelBuffer);

}

你可能感兴趣的:(ios硬编解码videotoolbox---硬解码)