本文共 2169 字,大约阅读时间需要 7 分钟。
导入框架和头文件#import <VideoToolbox/VideoToolbox.h>
{//声明的全局变量 NSData *spsData ; NSData *ppsData; uint8_t pFrameData[BUFFER_SIZE]; CMVideoFormatDescriptionRef videoFormatDescription ; VTDecompressionSessionRef decompressionSession; VTDecompressionOutputCallback decompressionSessionDecodeFrameCallback;}-(void)createDecompSession{ VTDecompressionOutputCallbackRecord callBackRecord; callBackRecord.decompressionOutputCallback = decompressionSessionDecodeFrameCallback; callBackRecord.decompressionOutputRefCon = (__bridge void *)self; //attr是传递给decode session的属性词典,两种方式// NSDictionary *destinationImageBufferAttributes =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO],(id)kCVPixelBufferOpenGLESCompatibilityKey,// [NSNumber numberWithInt:kCVPixelFormatType_24RGB],(id)kCVPixelBufferPixelFormatTypeKey,nil]; //kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,kCVPixelFormatType_24RGB //(__bridge CFDictionaryRef)(destinationImageBufferAttributes) //attr是传递给decode session的属性词典 CFDictionaryRef attrs = NULL; //kCVPixelBufferPixelFormatTypeKey,指定解码后的图像格式,必须指定成NV12,苹果的硬解码器只支持NV12。 const void *keys[] = { kCVPixelBufferPixelFormatTypeKey }; // kCVPixelFormatType_420YpCbCr8Planar is YUV420 // kCVPixelFormatType_420YpCbCr8BiPlanarFullRange is NV12 uint32_t v = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; const void *values[] = { CFNumberCreate(NULL, kCFNumberSInt32Type, &v) }; attrs = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL); //创建解码session OSStatus status = VTDecompressionSessionCreate(kCFAllocatorDefault, videoFormatDescription, NULL, attrs , // attrs, // NULL &callBackRecord, &decompressionSession);}-(void)decodeData:(char*)pFromeData Leng:(int)length{ //去掉开始码,提取sps和pps int naluType = ((uint8_t)pFromeData[4]&0x1f);// for (int i=0; i
转载地址:http://mqnvb.baihongyu.com/