0
CVPixelBufferRef outputPixelBuffer = NULL;

CMBlockBufferRef blockBuffer = NULL;
void* buffer = (void*)[videoUnit bufferWithH265LengthHeader];
OSStatus status  = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
                                                      buffer,
                                                      videoUnit.length,
                                                      kCFAllocatorNull,
                                                      NULL, 0, videoUnit.length,
                                                      0, &blockBuffer);
if(status == kCMBlockBufferNoErr) {
    CMSampleBufferRef sampleBuffer = NULL;
    const size_t sampleSizeArray[] = {videoUnit.length};
    status = CMSampleBufferCreateReady(kCFAllocatorDefault,
                                       blockBuffer,
                                       _decoderFormatDescription ,
                                       1, 0, NULL, 1, sampleSizeArray,
                                       &sampleBuffer);
    if (status == kCMBlockBufferNoErr && sampleBuffer && _deocderSession) {
        VTDecodeFrameFlags flags = 0;
        VTDecodeInfoFlags flagOut = 0;
        OSStatus decodeStatus = VTDecompressionSessionDecodeFrame(_deocderSession,
                                                                  sampleBuffer,
                                                                  flags,
                                                                  &outputPixelBuffer,
                                                                  &flagOut);

        if(decodeStatus == kVTInvalidSessionErr) {
            NSLog(@"IOS8VT: Invalid session, reset decoder session");
        } else if(decodeStatus == kVTVideoDecoderBadDataErr) {
            NSLog(@"IOS8VT: decode failed status=%d(Bad data)", decodeStatus);
        } else if(decodeStatus != noErr) {
            NSLog(@"IOS8VT: decode failed status=%d", decodeStatus);
        }

        CFRelease(sampleBuffer);
    }
    CFRelease(blockBuffer);
}

return outputPixelBuffer;

这是我解码流数据的代码。它在 iPhone 6s 上运行良好,但是当代码在 iPhoneX 或 iphone11 上运行时,“outputPixelBuffer”返回 nil。任何人都可以帮忙吗?

4

1 回答 1

1

没有看到你的 decompressionSession 创建的代码,很难说。可能是您的 decompressionSession 正在为创建时提供的回调函数提供 outputBuffer,因此我强烈建议您也添加这部分代码。

通过在以下位置提供 &outputPixelBuffer:

OSStatus decodeStatus = VTDecompressionSessionDecodeFrame(_deocderSession,
                                                                  sampleBuffer,
                                                                  flags,
                                                                  &outputPixelBuffer,
                                                                  &flagOut);

仅表示您提供了参考,并不表示一定会同步填写。

我还建议您打印出以下 OSStatus:

CMBlockBufferCreateWithMemoryBlock

CMSampleBufferCreateReady

如果这些步骤有问题,应该有办法知道。

于 2020-05-09T01:51:43.950 回答