首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于视频工具箱的H264编解码

基于视频工具箱的H264编解码
EN

Stack Overflow用户
提问于 2017-11-10 07:50:04
回答 1查看 1.4K关注 0票数 2

我正在使用视频工具箱测试编码和解码,将捕获的帧转换为H264,并使用这些数据在AVSampleBufferdisplayLayer中显示它。

使用错误代码-12712解压缩CMVideoFormatDescriptionCreateFromH264ParameterSets时出现错误

我跟着这段来自mobisoftinfotech.com的代码

代码语言:javascript
复制
status = CMVideoFormatDescriptionCreateFromH264ParameterSets(
    kCFAlloc‌​‌ atorDefault, 2,
    (const uint8_t const)parameterSetPointers, 
    parameterSetSizes, 4, &_formatDesc);

videoCompressionTest;有人能解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-21 09:12:13

我不确定你是否已经解决了这个问题。但是,我在代码中发现了两个导致错误的地方。在修复它们并在本地运行您的测试应用程序之后,它似乎运行得很好。(用Xcode 9.4.1,MacOS 10.13测试)

第一个-(void)CompressAndConvertToData:(CMSampleBufferRef)sampleBuffer方法中,where循环应该如下所示

代码语言:javascript
复制
while (bufferOffset < blockBufferLength - AVCCHeaderLength) {
    // Read the NAL unit length
    uint32_t NALUnitLength = 0;
    memcpy(&NALUnitLength, bufferDataPointer + bufferOffset, AVCCHeaderLength);
    // Convert the length value from Big-endian to Little-endian
    NALUnitLength = CFSwapInt32BigToHost(NALUnitLength);
    // Write start code to the elementary stream
    [elementaryStream appendBytes:startCode length:startCodeLength];
    // Write the NAL unit without the AVCC length header to the elementary stream
    [elementaryStream appendBytes:bufferDataPointer + bufferOffset + AVCCHeaderLength
                           length:NALUnitLength];
    // Move to the next NAL unit in the block buffer
    bufferOffset += AVCCHeaderLength + NALUnitLength;
}

uint8_t *bytes = (uint8_t*)[elementaryStream bytes];
int size = (int)[elementaryStream length];

[self receivedRawVideoFrame:bytes withSize:size];

第二位是您处理NALU类型8 ( if(nalu_type == 8)语句中的代码块)的解压缩代码。这是个棘手的问题。若要修复它,请更新

代码语言:javascript
复制
for (int i = _spsSize + 12; i < _spsSize + 50; i++)

代码语言:javascript
复制
for (int i = _spsSize + 12; i < _spsSize + 12 + 50; i++)

你可以自由地移除这个黑客

代码语言:javascript
复制
//was crashing here
    if(_ppsSize == 0)
        _ppsSize = 4;

为什么?让我们打印出帧包格式。po frame ▿ 4282 elements - 0 : 0 - 1 : 0 - 2 : 0 - 3 : 1 - 4 : 39 - 5 : 100 - 6 : 0 - 7 : 30 - 8 : 172 - 9 : 86 - 10 : 193 - 11 : 112 - 12 : 247 - 13 : 151 - 14 : 64 - 15 : 0 - 16 : 0 - 17 : 0 - 18 : 1 - 19 : 40 - 20 : 238 - 21 : 60 - 22 : 176 - 23 : 0 - 24 : 0 - 25 : 0 - 26 : 1 - 27 : 6 - 28 : 5 - 29 : 35 - 30 : 71 - 31 : 86 - 32 : 74 - 33 : 220 - 34 : 92 - 35 : 76 - 36 : 67 - 37 : 63 - 38 : 148 - 39 : 239 - 40 : 197 - 41 : 17 - 42 : 60 - 43 : 209 - 44 : 67 - 45 : 168 - 46 : 0 - 47 : 0 - 48 : 3 - 49 : 0 - 50 : 0 - 51 : 3 - 52 : 0 - 53 : 2 - 54 : 143 - 55 : 92 - 56 : 40 - 57 : 1 - 58 : 221 - 59 : 204 - 60 : 204 - 61 : 221 - 62 : 2 - 63 : 0 - 64 : 76 - 65 : 75 - 66 : 64 - 67 : 128 - 68 : 0 - 69 : 0 - 70 : 0 - 71 : 1 - 72 : 37 - 73 : 184 - 74 : 32 - 75 : 1 - 76 : 223 - 77 : 205 - 78 : 248 - 79 : 30 - 80 : 231 … more

第一个NALU启动代码if (nalu_type == 7)是0,0,0,1从索引15到18。接下来的0,0,0,1(从23到26)是类型6,类型8 NALU开始代码是68到71。这就是为什么我稍微修改for循环以扫描范围为50的起始索引(_spsSize + 12)。

我还没有对您的代码进行充分的测试,以确保编码和解码工作正常。不过,我希望这个发现能对你有所帮助。顺便说一句,如果有任何误解,我很乐意从你的评论中吸取教训。

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

https://stackoverflow.com/questions/47217998

复制
相关文章

相似问题

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