iOS系统中H264硬解及显示详解
来源:互联网 发布:photoshop cc mac 下载 编辑:程序博客网 时间:2024/05/16 12:14
iOS系统中H264硬解及显示详解
iOS系统中H264硬解及显示详解
苹果在iOS 8.0系统之前,没有开放系统的硬件编码解码功能,不过Mac OS系统一直有,被称为
VideoToolBox
的框架来处理硬件的编码和解码,终于在iOS 8.0后,苹果将该框架引入iOS系统。
一、VideoToolbox基本数据结构:
1、CVPixelBuffer
:编码前和解码后的图像数据结构;
2、CMTime
、CMClock
和CMTimebase
:时间戳相关。时间以64-bit/32-bit的形式出现;
3、CMBlockBuffer
:编码后,结果图像的数据结构;
4、CMVideoFormatDescription
:图像存储方式,编解码器等格式描述;
5、CMSampleBuffer
:存放编解码前后的视频图像的容器数据结构。
下图为H264解码前后数据结构示意图:
二、硬解使用方法:
H264的码流由NALU单元组成,NALU单元包含视频图像数据和H264的参数信息。其中视频图像数据就是
CMBlockBuffer
,而H264的参数信息则可以组合成FormatDesc
。具体来说参数信息包含SPS
(Sequence Parameter Set
)和PPS
(Picture Parameter Set
)。下图显示一个H264码流的结构:
解码方式一:(通过系统提供的AVSampleBufferDisplayLayer
来解码并显示)
1、初始化H264硬解
param
:1) 提取
sps
和pps
生成format description
://sps _spsSize = format.getCsd_0_size()-4; _sps = (uint8_t *)malloc(_spsSize); memcpy(_sps, format.getCsd_0()+4, _spsSize); //pps _ppsSize = format.getCsd_1_size()-4; _pps = (uint8_t *)malloc(_ppsSize); memcpy(_pps, format.getCsd_1()+4, _ppsSize);
2) 使用
CMVideoFormatDescriptionCreateFromH264ParameterSets
函数来构建CMVideoFormatDescriptionRef
:CMVideoFormatDescriptionCreateFromH264ParameterSets(kCFAllocatorDefault, 2, //param count parameterSetPointers, parameterSetSizes, 4, //nal start code size &_decoderFormatDescription);
2、将H264码流转换成解码前的
CMSampleBuffer
:
1) 使用CMBlockBufferCreateWithMemoryBlock
接口构造CMBlockBufferRef
;CMBlockBufferRef blockBuffer = NULL;CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, (void*)frame.bytes, frame.length, kCFAllocatorNull, NULL, 0, frame.length, 0, &blockBuffer);
2)根据上述得到CMVideoFormatDescriptionRef、CMBlockBufferRef和可选的时间信息,使用CMSampleBufferCreate接口得到CMSampleBuffer数据这个待解码的原始的数据。
CMSampleBufferRef sampleBuffer = NULL;CMSampleBufferCreateReady(kCFAllocatorDefault, blockBuffer, _decoderFormatDescription, 1, 0, NULL, 1, sampleSizeArray, &sampleBuffer);
3、硬解图像显示:
通过系统提供的AVSampleBufferDisplayLayer
来解码并显示。AVSampleBufferDisplayLayer
是苹果提供的一个专门显示编码后的H264数据的显示层,它是CALayer
的子类,因此使用方式和其它CALayer类似。该层内置了硬件解码功能,将原始的CMSampleBuffer
解码后的图像直接显示在屏幕上面,非常的简单方便。CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, YES);CFMutableDictionaryRef dict = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);if (status == kCMBlockBufferNoErr) { if ([_avslayer isReadyForMoreMediaData]) { dispatch_sync(dispatch_get_main_queue(),^{ [_avslayer enqueueSampleBuffer:sampleBuffer]; }); } CFRelease(sampleBuffer);}
解码方式二:(通过VTDecompression
接口,将CMSampleBuffer
解码成图像,将图像通过UIImageView
或者OpenGL
来显示)
1、初始化H264硬解
param
:在方式一的基础上,使用
VTDecompressionSessionCreate
接口构造VTDecompressionSessionRef
;(初始化VTDecompressionSession
,设置解码器的相关信息)VTDecompressionSessionRef _deocderSession;VTDecompressionSessionCreate(kCFAllocatorDefault, _decoderFormatDescription, NULL, attrs, &callBackRecord, &_deocderSession);
2、将H264码流转换成解码前的
CMSampleBuffer
:同方式一
3、将
CMSampleBuffer
数据使用VTDecompressionSessionDecodeFrame
接口解码成CVPixelBufferRef
数据:CVPixelBufferRef outputPixelBuffer = NULL;VTDecompressionSessionDecodeFrame(_deocderSession, sampleBuffer, flags, &outputPixelBuffer, &flagOut);
4、将CVPixelBufferRef数据转换成UIImage并显示:
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];CIContext *temporaryContext = [CIContext contextWithOptions:nil];//requiredCGImageRef videoImage = [temporaryContext createCGImage:ciImage fromRect:CGRectMake(0, 0, CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer))];//requiredUIImage *uiImage = [UIImage imageWithCGImage:videoImage];CGImageRelease(videoImage);
三、程序流程框图:
解码方式一 解码方式二
四、两种解码方式比较:
解码方式一:
- 优点: 该方式通过系统提供的
AVSampleBufferDisplayLayer
显示层来解码并显示。该层内置了硬件解码功能,将原始的CMSampleBuffer
解码后的图像直接显示在屏幕上,非常的简单方便,且执行效率高,占用内存相对较少。 - 缺点: 从解码的数据中不能直接获取图像数据并对其做相应处理,解码后的数据不能直接进行其他方面的应用(一般要做较复杂的转换)。
解码方式二:
- 优点: 该方式通过
VTDecompressionSessionDecodeFrame
接口,得到CVPixelBufferRef
数据,我们可以直接从CVPixelBufferRef
数据中获取图像数据并对其做相应处理,方便于其他应用。 - 缺点: 解码中执行效率相对降低,占用的内存也会相对较大。
- iOS系统中H264硬解及显示详解
- iOS h264 硬解
- Android Mediacodec硬解H264并显示
- iOS硬解处理H264数据
- iOS h264 硬解码
- iOS h264硬编码
- ios 硬解码h264
- H264 VDPAU 硬解及环境布置
- RTSP H264硬解码显示
- iOS videotool h264硬解码
- iOS 视频h264硬解码
- iOS 视频h264硬编码
- 基于iOS的网络音视频实时传输系统(五)- 使用VideoToolbox硬解码H264
- ios 硬解码h264视频的坑
- iOS使用VideoToolbox硬编码录制H264视频
- iOS使用VideoToolbox硬编码录制H264视频
- iOS使用VideoToolbox硬编码录制H264视频
- iOS硬解码H264,创建VTDecompressionSessionRef时,返回 -12913
- ffmpeg中HLS和DASH切片
- PackageManagerService的启动过程分析
- MFC响应键盘方向键消息
- ps人像磨皮皮肤处理教程
- VC/MFC如何设置对话框、static背景颜色
- iOS系统中H264硬解及显示详解
- eact Native开发IDE安装及配置
- struts2学习---struts2常用标签(上)
- Deep Learning Book 第十章--序列模型:循环和递归网络
- C++实验5—数组分离
- JAVA解析XML配置文件的读取操作
- android使用WebView来打开文件选择器
- 【JAVA】排序算法总览
- iOS 清除缓存功能详细代码 可过滤特不删除特殊格式文件