VTDecompressionSessionDecodeFrame失败,代码为-kVTVideoDecoderBadDataErr

时间:2014-09-26 15:30:53

标签: ios avfoundation

我一直在尝试使用VTDecompressionSessionDecodeFrame解码H264,但却出错了。参数集之前已经创建并且看起来很好,到目前为止没有任何错误,因此它可能与我对CMSampleBufferRef中的时序信息的理解有关。任何意见都将非常感激

void didDecompress( void *decompressionOutputRefCon, void *sourceFrameRefCon, OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef imageBuffer, CMTime presentationTimeStamp, CMTime presentationDuration ){

    NSLog(@"In decompression callback routine"); 

}

void decodeH264 { 

VTDecodeInfoFlags infoFlags;
[NALPacket appendBytes: NalPacketSize length:4];
                    [NALPacket appendBytes: &NALCODE length:1];
                    [NALPacket appendBytes: startPointer length:buflen];
                    void *samples = (void *)[NALTestPacket bytes];

                    blockBuffer = NULL;

                    // add the nal raw data to the CMBlockBuffer
                    status = CMBlockBufferCreateWithMemoryBlock(
                                                                kCFAllocatorDefault,
                                                                samples,
                                                                [NALPacket length],
                                                                kCFAllocatorDefault,
                                                                NULL,
                                                                0,
                                                                [NALPacket length],
                                                                0,
                                                                &blockBuffer);

                        const size_t * samplesizeArrayPointer;
                        size_t sampleSizeArray= buflen;
                        samplesizeArrayPointer = &sampleSizeArray;

                        int32_t timeSpan = 1000000;
                        CMTime PTime = CMTimeMake(presentationTime, timeSpan);
                        CMSampleTimingInfo timingInfo;
                        timingInfo.presentationTimeStamp = PTime;
                        timingInfo.duration =  kCMTimeZero;
                        timingInfo.decodeTimeStamp = kCMTimeInvalid;

                        status = CMSampleBufferCreate(kCFAllocatorDefault, blockBuffer, YES, NULL, NULL, formatDescription, 1, 1, &timingInfo, 0, samplesizeArrayPointer, &sampleBuffer);

                        CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true);
                        for (CFIndex i = 0; i < CFArrayGetCount(attachmentsArray); ++i) {
                            CFMutableDictionaryRef attachments = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachmentsArray, i);
                            CFDictionarySetValue(attachments, kCMSampleAttachmentKey_DoNotDisplay, kCFBooleanFalse);
                            CFDictionarySetValue(attachments, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);
                        }

// I Frame
                        status = VTDecompressionSessionDecodeFrame(decoder, sampleBuffer, kVTDecodeFrame_1xRealTimePlayback, (void*)CFBridgingRetain(currentTime), &infoFlags);
                         if (status != noErr) {
                             NSLog(@"Decode error");
                         }

1 个答案:

答案 0 :(得分:0)

发现为什么这不起作用,我忘记每次捕获一个新样本时将CMSampleBufferRef设置为NULL。

samples = NULL;

status = CMSampleBufferCreate(kCFAllocatorDefault, blockBuffer, YES, NULL, NULL, formatDescription, 1, 1, &timingInfo, 0, samplesizeArrayPointer, &sampleBuffer);