硬編碼錯誤碼OSStatus(VideoToolbox/VTError.h):
? kVTPropertyNotSupportedErr = -12900,
? kVTPropertyReadOnlyErr = -12901,
? kVTParameterErr = -12902,
? kVTInvalidSessionErr = -12903,
? kVTAllocationFailedErr = -12904,
? kVTPixelTransferNotSupportedErr = -12905, // c.f. -8961
? kVTCouldNotFindVideoDecoderErr = -12906,
? kVTCouldNotCreateInstanceErr = -12907,
? kVTCouldNotFindVideoEncoderErr = -12908,
? kVTVideoDecoderBadDataErr = -12909, // c.f. -8969
? kVTVideoDecoderUnsupportedDataFormatErr = -12910, // c.f. -8970
? kVTVideoDecoderMalfunctionErr = -12911, // c.f. -8960
? kVTVideoEncoderMalfunctionErr = -12912,//頻繁的啟停編碼器蚜点,報錯
? kVTVideoDecoderNotAvailableNowErr = -12913,
? kVTImageRotationNotSupportedErr = -12914,
? kVTVideoEncoderNotAvailableNowErr = -12915,
? kVTFormatDescriptionChangeNotSupportedErr = -12916,
? kVTCouldNotCreateColorCorrectionDataErr = -12918,
? kVTColorSyncTransformConvertFailedErr = -12919,
? kVTVideoDecoderAuthorizationErr = -12210,
? kVTVideoEncoderAuthorizationErr = -12211,
? kVTColorCorrectionPixelTransferFailedErr = -12212,
? kVTMultiPassStorageIdentifierMismatchErr = -12213,
? kVTMultiPassStorageInvalidErr = -12214,
? kVTFrameSiloInvalidTimeStampErr = -12215,
? kVTFrameSiloInvalidTimeRangeErr = -12216,
? kVTCouldNotFindTemporalFilterErr = -12217,
? kVTPixelTransferNotPermittedErr = -12218,
? kVTColorCorrectionImageRotationFailedErr = -12219,
? kVTVideoDecoderRemovedErr = -17690,
除了上述VTErrors鸠澈,補充遇到CMFormatDescription拜英,CMBlockBuffer篱瞎,CMSampleBuffer錯誤蚊夫。
? kCMFormatDescriptionError_InvalidParameter = -12710,
? kCMFormatDescriptionError_AllocationFailed = -12711,
? kCMFormatDescriptionError_ValueNotAvailable = -12718,
? kCMBlockBufferNoErr = 0,
? kCMBlockBufferStructureAllocationFailedErr = -12700,
? kCMBlockBufferBlockAllocationFailedErr = -12701,
? kCMBlockBufferBadCustomBlockSourceErr = -12702,
? kCMBlockBufferBadOffsetParameterErr = -12703,
? kCMBlockBufferBadLengthParameterErr = -12704,
? kCMBlockBufferBadPointerParameterErr = -12705,
? kCMBlockBufferEmptyBBufErr = -12706,
? kCMBlockBufferUnallocatedBlockErr = -12707,
? kCMBlockBufferInsufficientSpaceErr = -12708,
? kCMSampleBufferError_AllocationFailed = -12730,
? kCMSampleBufferError_RequiredParameterMissing = -12731,
? kCMSampleBufferError_AlreadyHasDataBuffer = -12732,
? kCMSampleBufferError_BufferNotReady = -12733,
? kCMSampleBufferError_SampleIndexOutOfRange = -12734,
? kCMSampleBufferError_BufferHasNoSampleSizes = -12735,
? kCMSampleBufferError_BufferHasNoSampleTimingInfo = -12736,
? kCMSampleBufferError_ArrayTooSmall = -12737,
? kCMSampleBufferError_InvalidEntryCount = -12738,
? kCMSampleBufferError_CannotSubdivide = -12739,
? kCMSampleBufferError_SampleTimingInfoInvalid = -12740,
? kCMSampleBufferError_InvalidMediaTypeForOperation = -12741,
? kCMSampleBufferError_InvalidSampleData = -12742,
? kCMSampleBufferError_InvalidMediaFormat = -12743,
? kCMSampleBufferError_Invalidated = -12744,
? kCMSampleBufferError_DataFailed = -16750,
? kCMSampleBufferError_DataCanceled = -16751,
編碼示例:
VTCompressionSessionRef _encoderSession;
CVPixelBufferPoolRef _pixelBufferPool = VTCompressionSessionGetPixelBufferPool(_encoderSession);
if(_pixelBufferPool)
{
CVPixelBufferPoolRetain(_pixelBufferPool);
}
else
{
printf(this," Failed to create Pixel Buffer in current session.");
}
CVPixelBufferRef pixbuf = NULL;
CVReturn r = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, _pixelBufferPool, &pixbuf);
if (kCVReturnSuccess != r || NULL == pixbuf)
{
printf(this," create pixel buffer failed! status=%d", r);
return false;
}
CVPixelBufferLockBaseAddress( pixbuf, 0 );
//copy yuv數(shù)據(jù)
....
CVPixelBufferUnlockBaseAddress( pixbuf, 0);
CMTime timestamp = CMTimeMake(pts - _timestampBase, 1000);
CMTime duration = CMTimeMake(1, _frameRate);
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
VTEncodeInfoFlags infoFlags = 0;
status = VTCompressionSessionEncodeFrame(_encoderSession,
pixbuf,
timestamp,
duration,
NULL,
imageBuffer,
&infoFlags);
CVPixelBufferRelease(pixbuf);
....
//獲取sps pps
CFArrayRef array = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true);
const void * value = CFArrayGetValueAtIndex(array, 0);
bool keyframe = !CFDictionaryContainsKey(value, kCMSampleAttachmentKey_NotSync);
CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuf);
size_t spsSize = 0, ppsSize = 0;
const uint8_t *sps = 0, *pps = 0;
size_t parmCount = 0;
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 0, &sps, &spsSize, &parmCount, NULL);
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pps, &ppsSize, &parmCount, NULL);
memcpy(encoder->_sps, sps, spsSize);
encoder->_spsLen = spsSize;
memcpy(encoder->_pps, pps, ppsSize);
encoder->_ppsLen = ppsSize;
....
if (_encoderSession)
{
VTCompressionSessionCompleteFrames(_encoderSession, kCMTimeInvalid);//如果completeUntilPresentationTimeStamp是數(shù)字的話,包括當(dāng)前時間和之前時間的幀都會在方法返回前發(fā)出摊鸡。如果不是數(shù)字的話,全部的未處理的幀都會在方法返回前發(fā)出谴餐。
VTCompressionSessionInvalidate(_encoderSession);
CFRelease(_encoderSession);
_encoderSession = NULL;
CVPixelBufferPoolRelease(_pixelBufferPool);
_pixelBufferPool = NULL;
}