本文我们会着重介绍如果在iOS11上使用系统API进行265硬编硬解功能,读者需要有使用VideoToolBox进行H.264硬编/解码的相关经验。
一、什么是HEVC(H.265)
HEVC全称High Efficiency Video Coding(高效率视频编码),是比H.264更加优秀的一种视频压缩标准(也称为H.265)。HEVC在低码率视频压缩上,提升质量、减少容量和节省带宽方面都有突出表现,因此除了拍摄占用的容量减少外,在视频通话时也能更加流畅清晰。据9to5Mac的测试结果,原来的H.264标准需要需要60MB才能达到的画质,HEVC仅需要33MB。从下图的压缩效果可以看出,HEVC的压缩算法更加智能,虽然图片细节丢失的情况更高,但是却不会严重影响视频的画质。
H.264与H.265的压缩效果对比
需要注意的是,H.265的硬编/解功能,并不是ios的所有设备升级到新系统上都可以使用,目前苹果公布可使用HEVC编/解码的移动设备要求如下:
H.265硬编.png
H.265硬解.png
二、VideoToolBox编码
使用VideoToolBox进行H.264和H.265编码的流程完全相同,只在创建和配置编码器上存在少量差异,下面以VideoToolBox的编码流程为线索,说明使用两种编码格式时的区别。
1. 创建VTCompressionSession
VT_EXPORT OSStatus
VTCompressionSessionCreate(
CM_NULLABLE CFAllocatorRef allocator,
int32_t width,
int32_t height,
CMVideoCodecType codecType,
CM_NULLABLE CFDictionaryRef encoderSpecification,
CM_NULLABLE CFDictionaryRef sourceImageBufferAttributes,
CM_NULLABLE CFAllocatorRef compressedDataAllocator,
CM_NULLABLE VTCompressionOutputCallback outputCallback,
void * CM_NULLABLE outputCallbackRefCon,
CM_RETURNS_RETAINED_PARAMETER CM_NULLABLE VTCompressionSessionRef * CM_NONNULL compressionSessionOut) API_AVAILABLE(macosx(10.8), ios(8.0), tvos(10.2));
- 如果使用H.264编码功能,参数
codecType
需要设置为kCMVideoCodecType_H264
; - 如果使用H.265编码功能,参数
codecType
需要设置为kCMVideoCodecType_HEVC
;
其他参数在使用两种编码格式时没有区别。
2. 设置编码相关参数
VT_EXPORT OSStatus
VTSessionSetProperty(
CM_NONNULL VTSessionRef session,
CM_NONNULL CFStringRef propertyKey,
CM_NULLABLE CFTypeRef propertyValue ) API_AVAILABLE(macosx(10.8), ios(8.0), tvos(10.2));
其中在配置kVTCompressionPropertyKey_ProfileLevel
属性时,H.264和H.265有各自不同的ProfileLevel定义,与H.265相关的只有两个,如下所示:
VT_EXPORT const CFStringRef kVTProfileLevel_HEVC_Main_AutoLevel API_AVAILABLE(macosx(10.13), ios(11.0), tvos(11.0));
VT_EXPORT const CFStringRef kVTProfileLevel_HEVC_Main10_AutoLevel API_AVAILABLE(macosx(10.13), ios(11.0), tvos(11.0));
3. 启动编码
VT_EXPORT OSStatus
VTCompressionSessionPrepareToEncodeFrames( CM_NONNULL VTCompressionSessionRef session ) API_AVAILABLE(macosx(10.9), ios(8.0), tvos(10.2));
4. 循环输入源数据(yuv类型)
VT_EXPORT OSStatus
VTCompressionSessionEncodeFrame(
CM_NONNULL VTCompressionSessionRef session,
CM_NONNULL CVImageBufferRef imageBuffer,
CMTime presentationTimeStamp,
CMTime duration, // may be kCMTimeInvalid
CM_NULLABLE CFDictionaryRef frameProperties,
void * CM_NULLABLE sourceFrameRefCon,
VTEncodeInfoFlags * CM_NULLABLE infoFlagsOut ) API_AVAILABLE(macosx(10.8), ios(8.0), tvos(10.2));
5. 获取编码后的数据
通过在创建VTCompressionSession传入回调函数,获取编码后的数据。
typedef void (*VTCompressionOutputCallback)(
void * CM_NULLABLE outputCallbackRefCon,
void * CM_NULLABLE sourceFrameRefCon,
OSStatus status,
VTEncodeInfoFlags infoFlags,
CM_NULLABLE CMSampleBufferRef sampleBuffer );
至此针对使用VideoToolBox进行H.264/H.265编码的基本流程已经介绍完毕。
本文福利, 免费领取C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,编解码,推拉流,srs)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓
三、VideoToolBox解码
VideoToolBox的解码主要涉及以下几个函数:
VTDecompressionSessionCreate 创建解码session
VTDecompressionSessionDecodeFrame 解码一个frame
VTDecompressionSessionInvalidate 销毁解码session
其中VTDecompressionSessionCreate
创建session时需要CMVideoFormatDescriptionRef
类型的视频格式描述,而对于CMVideoFormatDescriptionRef
,VideoToolBox中提供了多个方法可以创建:
CMVideoFormatDescriptionCreate
CMVideoFormatDescriptionCreateForImageBuffer
CMVideoFormatDescriptionCreateFromH264ParameterSets
在iOS11中新增了一个方法:
CMVideoFormatDescriptionCreateFromHEVCParameterSets
用以创建H.265视频格式的描述。
对于H.264和H.265的解码,在VideoToolBox层面的操作完全一致,唯一不同的就是视频格式的描述类型不同。最常使用也最容易理解的为后两个通过ParameterSets来创建的函数,前两个函数的创建方式未作详细了解。
至此,对使用VideoToolBox解码H.265视频的重点就放在如何获取ParameterSets(即VPS、SPS和PPS)上。
3.1 H.265 NALU类型
同H.264一样,H.265数据也是以NALU
的形式组织起来,区别在于H.264的NALU Header为一个字节,而H.265的为两个字节,其结构如下:
所以,H.265编码格式的NALU
类型判断方式如下,code为NALU Header
的第一个字节:
int type = (code & 0x7E)>>1;
其中type类型为32、33、34的NALU分别为VPS
、SPS
和PPS
,其他类型参见H.265规范《T-REC-H.265-201304-I!!PDF-E》。
3.2 HEVCDecoderConfigurationRecord
使用ffmpeg读取MP4文件后,视频的AVCodecContext
结构的extradata
存储的即为包含有SPS
、PPS
信息的数据结构。
对于H.264格式的视频来说,为AVCDecoderConfigurationRecord
,该结构在标准文档《ISO-14496-15 AVC file format》中有详细说明。
// The CodecPrivate syntax shall follow the
// syntax of HEVCDecoderConfigurationRecord
// defined in ISO/IEC 14496-15.
//
// The number zero (0) shall be written to
// the configurationVersion variable until
// official finalization of 14496-15, 3rd ed.
//
// After its finalization, this field and the
// following CodecPrivate structure shall
// follow the definition of the
// HEVCDecoderConfigurationRecord in 14496-15.
unsigned int(8) configurationVersion;
unsigned int(2) general_profile_space;
unsigned int(1) general_tier_flag;
unsigned int(5) general_profile_idc;
unsigned int(32) general_profile_compatibility_flags;
unsigned int(48) general_constraint_indicator_flags;
unsigned int(8) general_level_idc;
bit(4) reserved = ‘1111’b;
unsigned int(12) min_spatial_segmentation_idc;
bit(6) reserved = ‘111111’b;
unsigned int(2) parallelismType;
bit(6) reserved = ‘111111’b;
unsigned int(2) chromaFormat;
bit(5) reserved = ‘11111’b;
unsigned int(3) bitDepthLumaMinus8;
bit(5) reserved = ‘11111’b;
unsigned int(3) bitDepthChromaMinus8;
bit(16) avgFrameRate;
bit(2) constantFrameRate;
bit(3) numTemporalLayers;
bit(1) temporalIdNested;
unsigned int(2) lengthSizeMinusOne;
unsigned int(8) numOfArrays;
for (j=0; j < numOfArrays; j++) {
bit(1) array_completeness;
unsigned int(1) reserved = 0;
unsigned int(6) NAL_unit_type;
unsigned int(16) numNalus;
for (i=0; i< numNalus; i++) {
unsigned int(16) nalUnitLength;
bit(8*nalUnitLength) nalUnit;
}
}
其中最后的nalUnit存储的即为VPS、SPS和PPS信息。
获取到VPS、SPS和PPS之后,就可以通过
CMVideoFormatDescriptionCreateFromHEVCParameterSets
来创建H.265视频格式的描述信息,再创建解码session即可使用VideoToolBox进行解码。
四、结束语
本文仅仅简单介绍了在iOS11系统上如何使用VideoTooBox进行HEVC硬编/硬解功能。