介紹
iOS 8.0 之后,蘋果開放了硬解碼和硬解碼的API。VideoToolbox
是一套純C語言API。其中包含了很多C語言函數; VideoToolbox
是一個低級框架,可直接訪問硬件編碼器和解碼器。它提供視頻壓縮和解壓縮服務,本文主要針對H.264硬編碼
來進行編解碼說明.關于H.264相關知識請參考H.264介紹&編碼原理本文不做過多解釋!
編碼
-
硬解碼
:用GPU來解碼,減少CPU運算- 優點:播放流暢、低功耗,解碼速度快
- 缺點:兼容不好
-
軟解碼
:用CPU來解碼,比如(ffmpeg)- 優點:兼容好
- 缺點:加大CPU負擔,耗電增加、沒有硬解碼流暢,解碼速度相對慢
VideoToolbox編碼:
1. 首先需要導入#import <VideoToolbox/VideoToolbox.h>
2. 初始化編碼會話
@property (nonatomic, assign) VTCompressionSessionRef compressionSession;
// 初始化編碼器
- (void)setupVideoSession {
// 1.用于記錄當前是第幾幀數據
self.frameID = 0;
// 2.錄制視頻的寬度&高度,根據實際需求修改
int width = 720;
int height = 1280;
// 3.創建CompressionSession對象,該對象用于對畫面進行編碼
OSStatus status = VTCompressionSessionCreate(NULL, // 會話的分配器。傳遞NULL以使用默認分配器。
width, // 幀的寬度,以像素為單位。
height, // 幀的高度,以像素為單位。
kCMVideoCodecType_H264, // 編解碼器的類型,表示使用h.264進行編碼
NULL, // 指定必須使用的特定視頻編碼器。傳遞NULL讓視頻工具箱選擇編碼器。
NULL, // 源像素緩沖區所需的屬性,用于創建像素緩沖池。如果不希望視頻工具箱為您創建一個,請傳遞NULL
NULL, // 壓縮數據的分配器。傳遞NULL以使用默認分配器。
didCompressH264, // 當一次編碼結束會在該函數進行回調,可以在該函數中將數據,寫入文件中
(__bridge void *)(self), // outputCallbackRefCon
&_compressionSession); // 指向一個變量以接收的壓縮會話。
if (status != 0){
NSLog(@"H264: session 創建失敗");
return ;
}
// 4.設置實時編碼輸出(直播必然是實時輸出,否則會有延遲)
VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Baseline_AutoLevel);
// 5.設置關鍵幀(GOPsize)間隔
int frameInterval = 60;
CFNumberRef frameIntervalRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &frameInterval);
VTSessionSetProperty(self.compressionSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, frameIntervalRef);
// 6.設置期望幀率(每秒多少幀,如果幀率過低,會造成畫面卡頓)
int fps = 24;
CFNumberRef fpsRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &fps);
VTSessionSetProperty(self.compressionSession, kVTCompressionPropertyKey_ExpectedFrameRate, fpsRef);
// 7.設置碼率(碼率: 編碼效率, 碼率越高,則畫面越清晰, 如果碼率較低會引起馬賽克 --> 碼率高有利于還原原始畫面,但是也不利于傳輸)
int bitRate = width * height * 3 * 4 * 8;
CFNumberRef bitRateRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bitRate);
VTSessionSetProperty(self.compressionSession, kVTCompressionPropertyKey_AverageBitRate, bitRateRef);
// 8.設置碼率,均值,單位是byte 這是一個算法
NSArray *limit = @[@(bitRate * 1.5/8), @(1)];
VTSessionSetProperty(self.compressionSession, kVTCompressionPropertyKey_DataRateLimits, (__bridge CFArrayRef)limit);
// 9.基本設置結束, 準備進行編碼
VTCompressionSessionPrepareToEncodeFrames(_compressionSession);
}
3. 編碼完成回調函數
// 編碼完成回調
void didCompressH264(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus status, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer) {
// 1.判斷狀態是否等于沒有錯誤
if (status != noErr) {
return;
}
if (!CMSampleBufferDataIsReady(sampleBuffer)) {
NSLog(@"didCompressH264 data is not ready ");
return;
}
// 2.根據傳入的參數獲取對象
VideoH264EnCode* encoder = (__bridge VideoH264EnCode*)outputCallbackRefCon;
// 3.判斷是否是關鍵幀
bool isKeyframe = !CFDictionaryContainsKey( (CFArrayGetValueAtIndex(CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), 0)), kCMSampleAttachmentKey_NotSync);
// 判斷當前幀是否為關鍵幀
// 獲取sps & pps數據
if (isKeyframe)
{
// 獲取編碼后的信息(存儲于CMFormatDescriptionRef中)
CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuffer);
// 獲取SPS信息
size_t sparameterSetSize, sparameterSetCount;
const uint8_t *sparameterSet;
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 0, &sparameterSet, &sparameterSetSize, &sparameterSetCount, 0 );
// 獲取PPS信息
size_t pparameterSetSize, pparameterSetCount;
const uint8_t *pparameterSet;
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pparameterSet, &pparameterSetSize, &pparameterSetCount, 0 );
// 裝sps/pps轉成NSData
NSData *sps = [NSData dataWithBytes:sparameterSet length:sparameterSetSize];
NSData *pps = [NSData dataWithBytes:pparameterSet length:pparameterSetSize];
// 寫入文件
[encoder gotSpsPps:sps pps:pps];
}
// 獲取數據塊
CMBlockBufferRef dataBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t length, totalLength;
char *dataPointer;
OSStatus statusCodeRet = CMBlockBufferGetDataPointer(dataBuffer, 0, &length, &totalLength, &dataPointer);
if (statusCodeRet == noErr) {
size_t bufferOffset = 0;
static const int AVCCHeaderLength = 4; // 返回的nalu數據前四個字節不是0001的startcode,而是大端模式的幀長度length
// 循環獲取nalu數據
while (bufferOffset < totalLength - AVCCHeaderLength) {
uint32_t NALUnitLength = 0;
// Read the NAL unit length
memcpy(&NALUnitLength, dataPointer + bufferOffset, AVCCHeaderLength);
// 從大端轉系統端
NALUnitLength = CFSwapInt32BigToHost(NALUnitLength);
NSData* data = [[NSData alloc] initWithBytes:(dataPointer + bufferOffset + AVCCHeaderLength) length:NALUnitLength];
[encoder gotEncodedData:data isKeyFrame:isKeyframe];
// 移動到寫一個塊,轉成NALU單元
// Move to the next NAL unit in the block buffer
bufferOffset += AVCCHeaderLength + NALUnitLength;
}
}
}
4. 獲取SPS/PPS,以及I,P,B 幀數據,并將其通過 block 回調
// 獲取 sps 以及 pps,并進行StartCode
- (void)gotSpsPps:(NSData*)sps pps:(NSData*)pps{
// 拼接NALU的 StartCode,默認規定使用 00000001
const char bytes[] = "\x00\x00\x00\x01";
size_t length = (sizeof bytes) - 1;
NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];
NSMutableData *h264Data = [[NSMutableData alloc] init];
[h264Data appendData:ByteHeader];
[h264Data appendData:sps];
if (self.h264DataBlock) {
self.h264DataBlock(h264Data);
}
[h264Data resetBytesInRange:NSMakeRange(0, [h264Data length])];
[h264Data setLength:0];
[h264Data appendData:ByteHeader];
[h264Data appendData:pps];
if (self.h264DataBlock) {
self.h264DataBlock(h264Data);
}
}
- (void)gotEncodedData:(NSData*)data isKeyFrame:(BOOL)isKeyFrame{
const char bytes[] = "\x00\x00\x00\x01";
size_t length = (sizeof bytes) - 1; //string literals have implicit trailing '\0'
NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];
NSMutableData *h264Data = [[NSMutableData alloc] init];
[h264Data appendData:ByteHeader];
[h264Data appendData:data];
if (self.h264DataBlock) {
self.h264DataBlock(h264Data);
}
}
5. 通過傳入原始幀數據進行調用并回調
// 將 sampleBuffer(攝像頭捕捉數據,原始幀數據) 編碼為H.264
- (void)encodeSampleBuffer:(CMSampleBufferRef)sampleBuffer H264DataBlock:(void (^)(NSData * _Nonnull))h264DataBlock{
if (!self.compressionSession) {
return;
}
// 1.保存 block 塊
self.h264DataBlock = h264DataBlock;
// 2.將sampleBuffer轉成imageBuffer
CVImageBufferRef imageBuffer = (CVImageBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
// 3.根據當前的幀數,創建CMTime的時間
CMTime presentationTimeStamp = CMTimeMake(self.frameID++, 1000);
VTEncodeInfoFlags flags;
// 4.開始編碼該幀數據
OSStatus statusCode = VTCompressionSessionEncodeFrame(
self.compressionSession,
imageBuffer,
presentationTimeStamp,
kCMTimeInvalid,
NULL,
(__bridge void * _Nullable)(self),
&flags
);
if (statusCode != noErr) {
NSLog(@"H264: VTCompressionSessionEncodeFrame failed with %d", (int)statusCode);
VTCompressionSessionInvalidate(self.compressionSession);
CFRelease(self.compressionSession);
self.compressionSession = NULL;
return;
}
}
通過上述幾個方法,可以完成原始幀數據的H.264編碼工作,并將其回調給調用者, 具體如何拼接以及使用,根據自身項目需求來進行使用
VideoToolbox解碼:
解碼與編碼正好相反,在拿到H.264的每一幀數據后進行解碼操作,最后獲取 原始幀數據進行展示
1. 初始化解碼器
- (BOOL)initH264Decoder {
if(_deocderSession) {
return YES;
}
const uint8_t* const parameterSetPointers[2] = { _sps, _pps };
const size_t parameterSetSizes[2] = { _spsSize, _ppsSize };
OSStatus status = CMVideoFormatDescriptionCreateFromH264ParameterSets(kCFAllocatorDefault,
2, //param count
parameterSetPointers,
parameterSetSizes,
4, //nal start code size
&_decoderFormatDescription);
if(status == noErr) {
NSDictionary* destinationPixelBufferAttributes = @{
(id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange], //硬解必須是 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange 或者是kCVPixelFormatType_420YpCbCr8Planar
//這里款高和編碼反的
(id)kCVPixelBufferOpenGLCompatibilityKey : [NSNumber numberWithBool:YES]
};
VTDecompressionOutputCallbackRecord callBackRecord;
callBackRecord.decompressionOutputCallback = didDecompress;
callBackRecord.decompressionOutputRefCon = (__bridge void *)self;
status = VTDecompressionSessionCreate(kCFAllocatorDefault,
_decoderFormatDescription,
NULL,
(__bridge CFDictionaryRef)destinationPixelBufferAttributes,
&callBackRecord,
&_deocderSession);
VTSessionSetProperty(_deocderSession, kVTDecompressionPropertyKey_ThreadCount, (__bridge CFTypeRef)[NSNumber numberWithInt:1]);
VTSessionSetProperty(_deocderSession, kVTDecompressionPropertyKey_RealTime, kCFBooleanTrue);
} else {
NSLog(@"IOS8VT: reset decoder session failed status=%d", (int)status);
}
return YES;
}
2. 解碼操作
// 解碼操作,外部調用
- (void)decodeNalu:(uint8_t *)frame size:(uint32_t) frameSize{
int nalu_type = (frame[4] & 0x1F);
CVPixelBufferRef pixelBuffer = NULL;
uint32_t nalSize = (uint32_t)(frameSize - 4);
uint8_t *pNalSize = (uint8_t*)(&nalSize);
frame[0] = *(pNalSize + 3);
frame[1] = *(pNalSize + 2);
frame[2] = *(pNalSize + 1);
frame[3] = *(pNalSize);
//傳輸的時候。關鍵幀不能丟數據 否則綠屏 B/P可以丟 這樣會卡頓
switch (nalu_type)
{
case 0x05:
// 關鍵幀
if([self initH264Decoder])
{
pixelBuffer = [self decode:frame withSize:frameSize];
}
break;
case 0x07:
// sps
_spsSize = frameSize - 4;
_sps = malloc(_spsSize);
memcpy(_sps, &frame[4], _spsSize);
break;
case 0x08:
{
// pps
_ppsSize = frameSize - 4;
_pps = malloc(_ppsSize);
memcpy(_pps, &frame[4], _ppsSize);
break;
}
default:
{
// B/P其他幀
if([self initH264Decoder]){
pixelBuffer = [self decode:frame withSize:frameSize];
}
break;
}
}
}
- (CVPixelBufferRef)decode:(uint8_t *)frame withSize:(uint32_t)frameSize{
CVPixelBufferRef outputPixelBuffer = NULL;
CMBlockBufferRef blockBuffer = NULL;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(NULL,
(void *)frame,
frameSize,
kCFAllocatorNull,
NULL,
0,
frameSize,
FALSE,
&blockBuffer);
if(status == kCMBlockBufferNoErr) {
CMSampleBufferRef sampleBuffer = NULL;
const size_t sampleSizeArray[] = {frameSize};
status = CMSampleBufferCreateReady(kCFAllocatorDefault,
blockBuffer,
_decoderFormatDescription ,
1, 0, NULL, 1, sampleSizeArray,
&sampleBuffer);
if (status == kCMBlockBufferNoErr && sampleBuffer) {
VTDecodeFrameFlags flags = 0;
VTDecodeInfoFlags flagOut = 0;
OSStatus decodeStatus = VTDecompressionSessionDecodeFrame(_deocderSession,
sampleBuffer,
flags,
&outputPixelBuffer,
&flagOut);
if(decodeStatus == kVTInvalidSessionErr) {
NSLog(@"IOS8VT: Invalid session, reset decoder session");
} else if(decodeStatus == kVTVideoDecoderBadDataErr) {
NSLog(@"IOS8VT: decode failed status=%d(Bad data)", (int)decodeStatus);
} else if(decodeStatus != noErr) {
NSLog(@"IOS8VT: decode failed status=%d", (int)decodeStatus);
}
CFRelease(sampleBuffer);
}
CFRelease(blockBuffer);
}
return outputPixelBuffer;
}
3. 解碼完成回調
// 解碼回調函數
static void didDecompress( void *decompressionOutputRefCon, void *sourceFrameRefCon, OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef pixelBuffer, CMTime presentationTimeStamp, CMTime presentationDuration ){
CVPixelBufferRef *outputPixelBuffer = (CVPixelBufferRef *)sourceFrameRefCon;
*outputPixelBuffer = CVPixelBufferRetain(pixelBuffer);
VideoH264Decoder *decoder = (__bridge VideoH264Decoder *)decompressionOutputRefCon;
if ([decoder.delegate respondsToSelector:@selector(decoder:didDecodingFrame:)]) {
[decoder.delegate decoder: decoder didDecodingFrame:pixelBuffer];
}
}
4. 通過 OpenGL 進行 幀數據展示
代碼就不貼出來了,可以通過 demo 進行查看.
本文主要通過 VideoToolbox 對 iPhone 手機攝像頭拍攝的視頻流進行 編碼和解碼
,并進行展示,僅僅提供了基本的編解碼功能,具體在項目中如何使用還要根據自身項目來定,關于視頻流傳輸,可以參考 Socket & CocoaAsyncSocket介紹與使用,以及如何處理粘包等問題;
更詳細使用請查看: VideoToolbox官方說明文檔