首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用视频工具箱在iOS 8中解码h264

用视频工具箱在iOS 8中解码h264
EN

Stack Overflow用户
提问于 2014-09-24 16:30:07
回答 2查看 5.5K关注 0票数 7

需要解码h264流并获取像素缓冲区

我知道在iOS 8上使用视频工具箱是可行的

1.如何将h264流转换为CMSampleBufferRef

2.如何使用视频工具箱进行解码?

EN

回答 2

Stack Overflow用户

发布于 2014-10-01 17:34:47

我假设你得到了附件B格式的流,如果它已经是AVCC格式(阅读MP4),那么你就可以使用AssetReader了,不需要做太多的事情。

对于附件B流(这是ppl。通常称为原始h264流)。

  1. 提取SPS/PPS NAL单元并从中创建参数集。你会定期收到它们。它们包含解码信息、帧应该如何decoded.
  2. create TimingInfo数组、持续时间(您可以从解析SPS的VUI部分获取)和呈现时间戳以及解码timestamp.Iif流作为MPEG2接收TS从PESr获取时间戳。如果不是仅仅提供基于您的calculations.
  3. Wrap的缺失信息,则CMBlockBuffer中的VLC NAL单元。您可以将多个或一个放入其中。
  4. 在CMBlockbuffer中包装NAL单元时,使用长度报头替换3字节或4字节的起始码。
  5. 将信息提供给CMSampleBufferCreate,您可以在RTP中对帧进行解码

WWDC提供了一个预设值,更详细地解释了这些步骤,并提供了示例代码。

票数 7
EN

Stack Overflow用户

发布于 2015-02-02 14:19:42

尝试将编码的CMSampleBufferRef转换为sampleBuffer。

代码语言:javascript
复制
if(!decompressionSession)
    {
    CMFormatDescriptionRef formatDescription=CMSampleBufferGetFormatDescription(sampleBuffer);
    decompressionSession = NULL;
    VTDecompressionOutputCallbackRecord callBackRecord;
    callBackRecord.decompressionOutputCallback=didDecompress;
    callBackRecord.decompressionOutputRefCon = (__bridge void *)self;
    OSStatus status1= VTDecompressionSessionCreate(kCFAllocatorDefault, formatDescription, NULL, NULL, &callBackRecord, &decompressionSession);
    }
    else
    {
        VTDecodeFrameFlags flags = kVTDecodeFrame_EnableAsynchronousDecompression;
        VTDecodeInfoFlags flagOut;
        VTDecompressionSessionDecodeFrame(decompressionSession, sampleBuffer, flags, NULL, &flagOut);
        VTDecompressionSessionWaitForAsynchronousFrames(decompressionSession);

    }



Decompression all back

static void didDecompress( void *decompressionOutputRefCon, void *sourceFrameRefCon, OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef imageBuffer, CMTime presentationTimeStamp, CMTime presentationDuration ){
 if(status==noErr)
    {
        NSLog(@"SUCCESS PROCEED FROM HERE !!!!");
    }
}

//请记住,您提供了正确的演示时间,而encoding.Here我正在为您提供编码详细信息。

代码语言:javascript
复制
//ENCODING-------------------ENCODING---------------ENCODING
if(!_compression_session)
{
  NSDictionary* pixelBufferOptions = @{
                                          (NSString*) kCVPixelBufferWidthKey : @(widthOFCaptureImage),
                                          (NSString*) kCVPixelBufferHeightKey : @(heightOFCaptureImage),
                                          (NSString*) kCVPixelBufferOpenGLESCompatibilityKey : @YES,
                                          (NSString*) kCVPixelBufferIOSurfacePropertiesKey : @{}};
_compression_session=NULL;
 CFMutableDictionaryRef encoderSpecifications = NULL;
  err = VTCompressionSessionCreate(
                                     kCFAllocatorDefault,
                                     widthOFCaptureImage,
                                     heightOFCaptureImage,
                                     kCMVideoCodecType_H264,
                                     encoderSpecifications,
                                     (__bridge CFDictionaryRef)pixelBufferOptions,
                                     NULL,
                                     compressionCallback,
                                     (__bridge void *)self,
                                     &_compression_session);
}
else
{


 CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBufferIs);
  CVPixelBufferRef pixelbufferPassing= CMSampleBufferGetImageBuffer(sampleBufferIs);
          OSStatus status1= VTCompressionSessionEncodeFrame(_compression_session, pixelbufferPassing, presentationTimeStamp, kCMTimeInvalid, NULL, NULL, NULL);
 VTCompressionSessionEndPass(_compression_session, NO, NULL);
}

//编码回调

代码语言:javascript
复制
  static void compressionCallback(void *outputCallbackRefCon,
                             void *sourceFrameRefCon,
                             OSStatus status,
                             VTEncodeInfoFlags infoFlags,
                             CMSampleBufferRef sampleBuffer ){
    }

//最好的祝福:)编码快乐:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26012146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档