在VTDecompressionSessionCreate中解码H264 VideoToolkit API失败,错误为-8971

时间:2014-06-04 13:52:39

标签: ios xcode macos h.264 video-processing

我正在尝试使用硬件支持的视频工具包解码器编写视频解码器。但是,如果我尝试初始化解码会话,如下面发布的示例,我在调用VTDecompressionSessionCreate时收到错误-8971。谁能告诉我这里我做错了什么?

谢谢你,最诚挚的问候,

奥利弗

OSStatus status;

int tmpWidth = sps.EncodedWidth();
int tmpHeight = sps.EncodedHeight();
NSLog(@"Got new Width and Height from SPS - %dx%d", tmpWidth, tmpHeight);

const VTDecompressionOutputCallbackRecord callback = { ReceivedDecompressedFrame, self };
status = CMVideoFormatDescriptionCreate(NULL,
                                       kCMVideoCodecType_H264,
                                       tmpWidth,
                                       tmpHeight,
                                       NULL,
                                       &decoderFormatDescription);

if (status == noErr)
{
    // Set the pixel attributes for the destination buffer
    CFMutableDictionaryRef destinationPixelBufferAttributes = CFDictionaryCreateMutable(
                                                                 NULL, // CFAllocatorRef allocator
                                                                 0,    // CFIndex capacity
                                                                 &kCFTypeDictionaryKeyCallBacks, 
                                                                 &kCFTypeDictionaryValueCallBacks);

    SInt32 destinationPixelType = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
    CFDictionarySetValue(destinationPixelBufferAttributes,kCVPixelBufferPixelFormatTypeKey, CFNumberCreate(NULL, kCFNumberSInt32Type, &destinationPixelType));
    CFDictionarySetValue(destinationPixelBufferAttributes,kCVPixelBufferWidthKey, CFNumberCreate(NULL, kCFNumberSInt32Type, &tmpWidth));
    CFDictionarySetValue(destinationPixelBufferAttributes, kCVPixelBufferHeightKey, CFNumberCreate(NULL, kCFNumberSInt32Type, &tmpHeight));
    CFDictionarySetValue(destinationPixelBufferAttributes, kCVPixelBufferOpenGLCompatibilityKey, kCFBooleanTrue);

    // Set the Decoder Parameters
    CFMutableDictionaryRef decoderParameters = CFDictionaryCreateMutable(
                                                        NULL, // CFAllocatorRef allocator
                                                        0,    // CFIndex capacity
                                                        &kCFTypeDictionaryKeyCallBacks,
                                                        &kCFTypeDictionaryValueCallBacks);

    CFDictionarySetValue(decoderParameters,kVTDecompressionPropertyKey_RealTime, kCFBooleanTrue);

    // Create the decompression session
    // Throws Error -8971 (codecExtensionNotFoundErr)
    status = VTDecompressionSessionCreate(NULL, decoderFormatDescription, decoderParameters, destinationPixelBufferAttributes, &callback, &decoderDecompressionSession);

    // release the dictionaries
    CFRelease(destinationPixelBufferAttributes);
    CFRelease(decoderParameters);

    // Check the Status
    if(status != noErr)
    {
        NSLog(@"Error %d while creating Video Decompression Session.", (int)status);
        continue;
    }
}
else
{
    NSLog(@"Error %d while creating Video Format Descripttion.", (int)status);
    continue;
}

2 个答案:

答案 0 :(得分:8)

我也偶然发现kVTVideoDecoderBadDataErr。在我的情况下,我正在使用NAL包的大小更改标头0x00000001,其中包含此标头的4个字节,这就是原因。我将大小更改为不包括这4个字节(frame_size = sizeof(NAL) - 4)。这个大小应该用big-endian编码。

答案 1 :(得分:6)

您需要从SPS和PPS创建CMFormatDescriptionRef,如

CMFormatDescriptionRef decoderFormatDescription;
const uint8_t* const parameterSetPointers[2] = { (const uint8_t*)[currentSps bytes], (const uint8_t*)[currentPps bytes] };
const size_t parameterSetSizes[2] = { [currentSps length], [currentPps length] };
status = CMVideoFormatDescriptionCreateFromH264ParameterSets(NULL,
                                                             2,
                                                             parameterSetPointers,
                                                             parameterSetSizes,
                                                             4,
                                                             &decoderFormatDescription);

此外,如果您以附件B格式获取视频数据,则需要删除起始代码并将其替换为4字节大小信息,以便解码器将其识别为avcc格式化(这是CMVideoFormatDescriptionCreateFromH264ParameterSets的第5个参数是为了。)