- (CVPixelBufferRef) decode:(uint8_t *)frameBuf frameLen:(int)frameLen {
//NSLog(@"frameLen:%d",frameLen);
CVPixelBufferRef outputPixelBuffer = NULL;
CMBlockBufferRef blockBuffer = NULL;
//创建CMBlockBuffer
OSStatus status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,(void*)frameBuf,frameLen,kCFAllocatorNull,NULL,0,frameLen,0,&blockBuffer);
if(status == kCMBlockBufferNoErr) {
CMSampleBufferRef sampleBuffer = NULL;
const size_t sampleSizeArray[] = {frameLen};
//创建CMSampleBuffer
status = CMSampleBufferCreateReady(kCFAllocatorDefault,blockBuffer,_decoderFormatDescription ,1, 0, NULL, 1, sampleSizeArray,&sampleBuffer);
if (status == kCMBlockBufferNoErr && sampleBuffer) {
VTDecodeFrameFlags flags = 0;
VTDecodeInfoFlags flagOut = 0;
if (_deocderSession == NULL || sampleBuffer == NULL) {
//printf("null pointer %p %p", _deocderSession, sampleBuffer);
return NULL;
}
//开始解码,解码后存放在outputPixelBuffer
OSStatus decodeStatus = VTDecompressionSessionDecodeFrame(_deocderSession,sampleBuffer,flags,&outputPixelBuffer,&flagOut);
if(decodeStatus == kVTInvalidSessionErr) {
[self initH264DecoderAndisValid:NO];
//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) {
[self clearH264Deocder];
[self initH264DecoderAndisValid:NO];
//NSLog(@"IOS8VT: decode failed status=%d", (int)decodeStatus);
return nil;
}
CFRelease(sampleBuffer);
sampleBuffer = NULL;
}
CFRelease(blockBuffer);
blockBuffer = NULL;
}
return outputPixelBuffer;
}