ios 如何为流媒体执行硬件加速的 H.264 编码和解码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10313308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:03:43  来源:igfitidea点击:

How can I perform hardware-accelerated H.264 encoding and decoding for streaming?

iosdecodeencodeh.264

提问by king

I am able to get the RGBA frame data from the camera, and I want to encode it in H.264 format. I've used FFmpeg to encode and decode H.264 video, but at a frame size of 640x480 it's too slow for my needs.

我能够从相机获取 RGBA 帧数据,我想将其编码为 H.264 格式。我已经使用 FFmpeg 对 H.264 视频进行编码和解码,但是在 640x480 的帧大小下它对于我的需要来说太慢了。

I'd like to use hardware acceleration to speed up the encoding and decoding, so how would I do that?

我想使用硬件加速来加速编码和解码,那么我该怎么做呢?

Also, I need to be able to stream the encoded video across the network and decode it on the other end. How can this be done?

此外,我需要能够通过网络传输编码视频并在另一端对其进行解码。如何才能做到这一点?

回答by Brad Larson

If you want to do hardware-accelerated video encoding and decoding of H.264 video on iOS, the only way to go is AVFoundation. Don't use third-party libraries for the encoding or decoding, because they all currently are CPU-bound, and are much slower than what you get from AVFoundation. The one reason to use a third-party encoder or decoder would be if you are working with a format not supported in iOS by default.

如果你想在iOS上做H.264视频的硬件加速视频编解码,唯一的出路就是AVFoundation。不要使用第三方库进行编码或解码,因为它们目前都受 CPU 限制,并且比您从 AVFoundation 获得的要慢得多。使用第三方编码器或解码器的一个原因是,如果您使用的是 iOS 默认不支持的格式。

For hardware-accelerated decoding, you'll want to use an AVAssetReader instance (or one of the player classes for pure playback). With an AVAssetReader, I regularly get 2X or higher playback speeds for reading H.264-encoded video, so the iOS devices use some pretty good hardware acceleration for that.

对于硬件加速解码,您需要使用 AVAssetReader 实例(或用于纯播放的播放器类之一)。使用 AVAssetReader,我经常获得 2 倍或更高的播放速度来读取 H.264 编码的视频,因此 iOS 设备为此使用了一些非常好的硬件加速。

Similarly, for accelerated encoding, you'll use an AVAssetWriter. There are some tricks to getting AVAssetWriter to encode at the best speed (feeding in BGRA frames, using a pixel buffer pool, using the iOS 5.0 texture caches if reading from OpenGL ES), which I describe in detail within this answer.

同样,对于加速编码,您将使用 AVAssetWriter。有一些技巧可以让 AVAssetWriter 以最佳速度进行编码(输入 BGRA 帧,使用像素缓冲池,如果从 OpenGL ES 读取,则使用 iOS 5.0 纹理缓存),我在这个答案中详细描述了这些技巧。

If you want to see some code that uses the fastest paths I've found for accelerated encoding and decoding, you can look at my open source GPUImageframework, which, unlike the one linked by Anastasia, is totally free to use.

如果您想查看一些使用我为加速编码和解码找到的最快路径的代码,您可以查看我的开源GPUImage框架,它与 Anastasia 链接的框架不同,完全免费使用。