解码时候,需要用h264数据长度替换0x00 0x00 0x00 0x01,注意长度大小端,否则会出现-8969错误
-(int)decode:(char*)buf len:(int)len {
CVPixelBufferRef outputPixelBuffer = NULL;
CMBlockBufferRef blockBuffer = NULL;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
(void*)buf, len,
kCFAllocatorNull,
NULL, 0, len,
0, &blockBuffer);
if(status == kCMBlockBufferNoErr) {
int h264Len = len - 4;
//memcpy(buf, &h264Len, sizeof(int));//这样不对,大小端错误,解码会出现-8969错误
buf[0] = (h264Len>>24)&0xff;//长度替换0 0 0 1
buf[1] = (h264Len>>16)&0xff;
buf[2] = (h264Len>>8)&0xff;
buf[3] = h264Len&0xff;
CMSampleBufferRef sampleBuffer = NULL;
const size_t sampleSizeArray[] = {len};
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)", decodeStatus);
} else if(decodeStatus != noErr) {
NSLog(@"IOS8VT: decode failed status=%d ,len = %d", decodeStatus,len);
}
CFRelease(sampleBuffer);
}
CFRelease(blockBuffer);
}
return 0;
}
memcpy(buf, &h264Len, sizeof(int));//错误,大小端错误,解码会出现-8969错误
buf[0] = (h264Len>>24)&0xff;//正确
buf[1] = (h264Len>>16)&0xff;
buf[2] = (h264Len>>8)&0xff;
buf[3] = h264Len&0xff;