Srinivas
Srinivas

Reputation: 23

CMVideoFormatDescriptionGetH264ParameterSetAtIndex does not work in iOS

I am using VTCompressionSessionRef to compress CVImageBuffer in H.264 format . The compression session works fine, I am get the compression callbacks with the CMSampleBuffer. When I try to get the NALU parameter set using the following code, it does not work.

const uint8_t *paramenterSetPointerOut =  NULL;
size_t parameterSetSizeOut,parameterSizeCountOut;
int nalUnitHeaderLengthOut;

CMFormatDescriptionRef fdesc = CMSampleBufferGetFormatDescription(sampleBuffer);
OSStatus status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(fdesc, 0, &paramenterSetPointerOut,&parameterSetSizeOut, &parameterSizeCountOut, &nalUnitHeaderLengthOut);

CMVideoFormatDescriptionGetH264ParameterSetAtIndex API returns -12712 error. Could you please let me know if I am doing something wrong here?

Upvotes: 0

Views: 992

Answers (2)

Alex Crow
Alex Crow

Reputation: 439

Maybe it's too late, but this implementation works great:

CMFormatDescriptionRef description = CMSampleBufferGetFormatDescription(sampleBuffer);

size_t numberOfParameterSets;
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(description,
                                                   0, NULL, NULL,
                                                   &numberOfParameterSets,
                                                   NULL);

// Write each parameter set to the elementary stream
NSLog(@"NUMBER OF PARAMETERS %lu", numberOfParameterSets);
for (int i = 0; i < numberOfParameterSets; i++) {
    const uint8_t *parameterSetPointer;
    size_t parameterSetLength;
    CMVideoFormatDescriptionGetH264ParameterSetAtIndex(description,
                                                       i,
                                                       &parameterSetPointer,
                                                       &parameterSetLength,
                                                       NULL, NULL);
    
    // Write the parameter set to the elementary stream
    [elementaryStream appendBytes:startCode length:startCodeLength];
    [elementaryStream appendBytes:parameterSetPointer length:parameterSetLength];
}

Upvotes: 0

The function has a bug, file a Radar. You can just iterate from index 0 until the function returns an error. The pset data and size variables will be fine.

Upvotes: 0

Related Questions