解碼時候,需要用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;