RT,手头做的项目要求实时性比较高的视频通话。iOS端的视频压缩用的是AVAssetWriter硬编码,写入视频文件之后再读出视频数据,最后发送的方案。
考虑到编解码速度不能太慢,采用的是h264 baseline。没有双向预测编码,也就是说没有B帧。
但是创建AVAssetWriter的时候蛋疼了,allowFrameReordering 怎么也没法设成NO。
代码如下:
NSDictionary* settings = @ {AVVideoCodecKey:AVVideoCodecH264,
AVVideoWidthKey:@ (width),
AVVideoHeightKey:@ (height),
AVVideoCompressionPropertiesKey:@ {
AVVideoProfileLevelKey:AVVideoProfileLevelH264Baseline30,
AVVideoAllowFrameReorderingKey:@ NO,
AVVideoExpectedSourceFrameRateKey:@ (15)
}
};
_writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];
然后把AVVideoAllowFrameReorderingKey对应的值取出来,发现还是YES。
这是为什么呀为什么~~
翻过了苹果的文档和源代码里的注释,没有什么解释,反而是在AVVideoAllowFrameReorderingKey这里提到
In order to achieve the best compression while maintaining image quality, some video encoders can reorder frames. This means that the order in which the frames will be emitted and stored (the decode order) will be different from the order in which they are presented to the video encoder (the display order).
Encoding using frame reordering requires more system resources than encoding without frame reordering, so encoding performance should be taken into account when deciding whether to enable frame reordering. This is especially important when encoding video data from a real-time source, such as AVCaptureVideoDataOutput. In this situation, using a value of @ NO for AVVideoAllowFrameReorderingKey may yield the best results.
The default is @ YES, which means that the encoder decides whether to enable frame reordering.
默认是@ YES,如果视频来自AVCaptureVideoDataOutput这样的实时源(我这里就是),设为@ NO比较好,资源消耗少一些。
但是为什么设置不了!
按说根据h264的标准,baseline是不支持B帧的,也就是肯定不能reorder的,这里设不设理论上也没什么关系。
但是我编码的视频在另一个PC客户端那边看,是花的。
视频里的物体静止的时候是这样的:
一半还算清楚,一半好像是把视频的中线刷上去了。 然后物体一移动就会完全花掉,像这样
如果哪位高手能看出来这种画面是什么问题,就太好了~拜谢~