4

I'm trying to decode a raw stream of .H264 video data but I can't find a way to create a proper

    - (void)decodeFrameWithNSData:(NSData*)data presentationTime:

(CMTime)presentationTime
{

    @autoreleasepool {

    CMSampleBufferRef sampleBuffer = NULL;
    CMBlockBufferRef blockBuffer = NULL;
    VTDecodeInfoFlags infoFlags;
    int sourceFrame;

    if( dSessionRef == NULL )
        [self createDecompressionSession];


    CMSampleTimingInfo timingInfo ;
    timingInfo.presentationTimeStamp = presentationTime;
    timingInfo.duration =  CMTimeMake(1,100000000);
    timingInfo.decodeTimeStamp = kCMTimeInvalid;

    //Creates block buffer from NSData
    OSStatus status  = CMBlockBufferCreateWithMemoryBlock(CFAllocatorGetDefault(), (void*)data.bytes,data.length*sizeof(char), CFAllocatorGetDefault(), NULL, 0, data.length*sizeof(char), 0, &blockBuffer);

    //Creates CMSampleBuffer to feed decompression session    
    status = CMSampleBufferCreateReady(CFAllocatorGetDefault(), blockBuffer,self.encoderVideoFormat,1,1,&timingInfo, 0, 0, &sampleBuffer);

    status = VTDecompressionSessionDecodeFrame(dSessionRef,sampleBuffer, kVTDecodeFrame_1xRealTimePlayback, &sourceFrame,&infoFlags);


    if(status != noErr) {
        NSLog(@"Decode with data error %d",status);
    }


    }
}

At the end of the call I'm getting -12911 error in VTDecompressionSessionDecodeFrame that translates to kVTVideoDecoderMalfunctionErr which after reading this [post] pointed me that I should make a VideoFormatDescriptor using CMVideoFormatDescriptionCreateFromH264ParameterSets. But how can I create a new VideoFormatDescription if I don't have information of the currentSps or currentPps? How can I get that information from my raw .H264 streaming?

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);

Thanks in advance,

Marcos

[post] : Decoding H264 VideoToolkit API fails with Error -8971 in VTDecompressionSessionCreate

Fattie
  • 27,874
  • 70
  • 431
  • 719
user1274720
  • 41
  • 1
  • 4

1 Answers1

5

You you MUST call CMVideoFormatDescriptionCreateFromH264ParameterSets first. The SPS/PPS may be stored/transmitted separately from the video stream. Or may come inline.

Note that for VTDecompressionSessionDecodeFrame your NALUs must be preceded with a size, and not a start code.

You can read more here: Possible Locations for Sequence/Picture Parameter Set(s) for H.264 Stream

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • Your blog appears to be down. – tmm1 Feb 11 '17 at 02:26
  • 1
    I sent a bounty for this amazing old answer, @szatmary , thanks! :) (We are having a similar problem with "SPS/PPS recognition" in the 265 era, on iOS builds :/ ) – Fattie Jan 29 '19 at 11:03