视频编解码
问题
iOS 硬件编解码的原理和使用方式?
答案
编解码概念
原始视频帧 (YUV/RGB) → 编码器 (H.264/HEVC) → 压缩数据 → 解码器 → 原始帧
VideoToolbox 硬编码
import VideoToolbox
class H264Encoder {
private var session: VTCompressionSession?
func setup(width: Int32, height: Int32) {
VTCompressionSessionCreate(
allocator: kCFAllocatorDefault,
width: width,
height: height,
codecType: kCMVideoCodecType_H264,
encoderSpecification: nil,
imageBufferAttributes: nil,
compressedDataAllocator: nil,
outputCallback: encoderCallback,
refcon: Unmanaged.passUnretained(self).toOpaque(),
compressionSessionOut: &session
)
// 设置编码参数
guard let session = session else { return }
VTSessionSetProperty(session, key: kVTCompressionPropertyKey_RealTime, value: kCFBooleanTrue)
VTSessionSetProperty(session, key: kVTCompressionPropertyKey_ProfileLevel,
value: kVTProfileLevel_H264_High_AutoLevel)
VTCompressionSessionPrepareToEncodeFrames(session)
}
func encode(sampleBuffer: CMSampleBuffer) {
guard let session = session,
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
let pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
VTCompressionSessionEncodeFrame(session, imageBuffer: pixelBuffer,
presentationTimeStamp: pts, duration: .invalid,
frameProperties: nil, sourceFrameRefcon: nil, infoFlagsOut: nil)
}
}
编码格式对比
| 格式 | 压缩率 | 兼容性 | iOS 支持 |
|---|---|---|---|
| H.264 | 中 | 最广 | iOS 4+ |
| H.265 (HEVC) | 高(比 H.264 节省 50%) | 较新设备 | iOS 11+ |
常见面试问题
Q1: 软编码和硬编码的区别?
答案:
- 软编码:用 CPU 运算(如 FFmpeg x264),兼容性好但耗电、发热
- 硬编码:用 GPU/专用芯片(VideoToolbox),功耗低、速度快,iOS 推荐