1、解码的基本流程
与编码器流程基本一致,流程如下:
-
创建解码器实例。
-
配置解码器。
-
解码。
-
释放解码器。
下面我们看一下几个关键函数的原型。
1.1 创建解器
在 iOS 中使用 VTDecompressionSessionCreate 方法创建解码器。其函数原型如下:
VTDecompressionSessionCreate(
allocator: CFAllocator, //session分配器,NULL使用默认分配器
videoFormatDescription: CMVideoFormatDescription, //视频帧格式描述信息
videoDecoderSpecification: CFDictionary, //视频解码器,如果NULL,表示让VideoToolbox选择视频解码器。
destinationImageBufferAttributes: CFDictionary, //像素缓冲区要求的属性
outputCallback: VTDecompressionOutputCallbackRecord *, //解码完一帧后的回调函数。
decompressionSessionOut: VTDecompressionSession* //创建出的解码Session实例。
) -> OSStatus
各参数详细介绍:
-
allocator : session分配器,NULL使用默认分配器。
-
videoFormatDescription : 源视频帧格式描述信息。
-
videoDecoderSpecification : 视频解码器。如果是NULL表式让 VideoToolbox自己选择视频解码器。
-
destinationImageBufferAttributes: 像素缓冲区要求的属性。
-
outputCallback: 解码后的回调函数。
-
decompressionSessionOut: 输出Session实列。
1.2 配置参数
在iOS下通过VTSessionSetProperty函数来配置 CompressionSession。函数原型如下:
VTSessionSetProperty(
session: VTSession, //就是上面创建的 VTCompressionSession 对象。
propertyKey: CFString, // 属性
propertyValue: CFTypeRef //属性值
) -> OSStatus //返回的状态值
其与编码器的设置是一样的。在设置解码器时只需要设置是否实时解码即可。
1.3 解码
使用 VTDecompressionSessionDecodeFrame 函数进行解码。其原型如下:
OSStatus
VTDecompressionSessionDecodeFrame(
VTDecompressionSessionRef session, //解码器 Session
CMSampleBufferRef sampleBuffer, // 源视频帧
VTDecodeFrameFlags decodeFlags, // 解码标志位。bit 0 is enableAsynchronousDecompression
void * sourceFrameRefCon, //用户自定义参数指针。
VTDecodeInfoFlags * infoFlagsOut //解码输出标志
)
-
session : 创建解码器时创建的 Session。