基于iOS11的HEVC(H.265)硬编码/硬解码功能开发指南

 

苹果于北京时间2017年6月6日凌晨召开WWDC 2017大会,在此次发布会上按照惯例推出了iOS 11系统,同时也带来了许多新鲜特性,如录屏功能、相册查看gif图片、Siri支持翻译等。作为音视频开发者,我们更着重关注的是此次苹果为iPhone为推出了两种全新的媒体格式"HEVC和HEIF":视频方面使用HEVC来代替H.264,而图片则是用HEIF来代替JPG。这两种格式号称可以在保证画质的情况下,大大减少视频、照片的大小,经常因为容量不足而头疼的朋友将成为最大受益者。

由此可见,如果在直播过程中使用H.265的视频编码格式,可使用较少的带宽,得到更优的画质。在iOS系统推出H.265的硬编硬解功能之前,我们在iphone手机上使用的H.265编解码功能,均为软件编解码,其缺点是cpu占用高,手机发热量大,无法支撑高分辨、高帧率的实时场景。相信此次iOS11的发布,会推动H.265在直播市场的发展。

本文我们会着重介绍如果在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的压缩效果对比.png

需要注意的是,H.265的硬编/解功能,并不是ios的所有设备升级到新系统上都可以使用,目前苹果公布可使用HEVC编/解码的移动设备要求如下:

iOS 11 HEVC Encode

H.265硬编.png

iOS 11 HEVC Decode

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编码的基本流程已经介绍完毕。

三、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的为两个字节,其结构如下:

H265 nalu header

所以,H.265编码格式的NALU类型判断方式如下,code为NALU Header的第一个字节:

int type = (code & 0x7E)>>1;

其中type类型为32、33、34的NALU分别为VPSSPSPPS,其他类型参见H.265规范《T-REC-H.265-201304-I!!PDF-E》。

3.2 HEVCDecoderConfigurationRecord

使用ffmpeg读取MP4文件后,视频的AVCodecContext结构的extradata存储的即为包含有SPSPPS信息的数据结构。

对于H.264格式的视频来说,为AVCDecoderConfigurationRecord,该结构在标准文档《ISO-14496-15 AVC file format》中有详细说明。

对于H.265格式的视频来说,为HEVCDecoderConfigurationRecord,此结构的定义目前未找到官方的定义,参考 https://lists.matroska.org/pipermail/matroska-devel/2013-September/004567.html 及ffmpeg中hevc.c文件中的实现,该结构详细如下:

// 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进行解码。

3.3 说明

iOS 11 beta2的版本中

VTDecompressionSessionWaitForAsynchronousFrames

此函数调用会失败,目前不确定是beta系统的问题还是什么其他原因,在该版本的iOS系统上使用ijkplayer VideoToolBox解码会发现播放卡顿,就是因为该函数的问题导致,留待后续版本观察(7月10日已经推出beta3版本,还未确认)

四、结束语

本文仅仅简单介绍了在iOS11系统上如何使用VideoTooBox进行HEVC硬编/硬解功能,目前我们的SDK中已经支持该功能,但是由于iOS11系统还未正式发布,所以该功能暂时未上线。如果您想尝鲜使用,可以通过下面的联系方式向我们获取。iOS11系统发布后,我们会第一时间上线该功能。

转载请注明:
作者金山视频云,首发简书 Jianshu.com

也欢迎大家使用我们的直播/短视频SDK,支持高效H.265软编,同时也适配了iOS 11 HEVC系统编解码能力:

iOS直播版(推流 + 播放):https://github.com/ksvc/KSYLive_iOS

iOS播放 :https://github.com/ksvc/KSYMediaPlayer_iOS

有关音视频的更多精彩内容,请参考https://github.com/ksvc

金山云SDK相关的QQ交流群:

  • 视频云技术交流群:574179720
  • 视频云iOS技术交流:621137661
作者:金山视频云
链接:http://www.jianshu.com/p/cb93e618e041
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://my.oschina.net/jlongtian/blog/1588845

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值