VideoToolbox
ハードウェアアクセラレーションを利用して動画のエンコード/デコードを行うための Framework。低レベルな API であるため、ポインタ型を扱う必要があったり、release を意識する必要があったりする。 エンコード
大まかな流れ
1. Session を生成する
2. 必要に応じてプロパティを設定する
3. 動画のフレームをエンコードする
4. 待ち状態のフレームについて強制的に完了させる
5. 圧縮が終了した場合は、session を release する必要がある
VTCompressionSession の設定項目
セッション作成時に設定できるエンコード設定がかなり多く、かつドキュメントが少なめなので難しい。
code:swift
func VTCompressionSessionCreate(allocator: CFAllocator?,
width: Int32,
height: Int32,
codecType: CMVideoCodecType,
encoderSpecification: CFDictionary?,
imageBufferAttributes sourceImageBufferAttributes: CFDictionary?,
compressedDataAllocator: CFAllocator?,
outputCallback: VTCompressionOutputCallback?,
refcon outputCallbackRefCon: UnsafeMutableRawPointer?,
compressionSessionOut: UnsafeMutablePointer<VTCompressionSession?>) -> OSStatus
allocator
CFAllocator は、CoreFoundation のオブジェクトの生成時に受け渡すことができ、生成対象のオブジェクトがメモリの割り当て/開放等のメモリ操作を行うときの振る舞いを決定する。ざっくりいうと、生成対象のオブジェクトがメモリ操作を行う際に利用するオブジェクト、になるようだ。 Predefined な Allocator としては下記があるけれど、特別な事情がない場合は NULL = kCFAllocatorDefault を利用するのが推奨されている。
CFAllocator provides the following predefined allocators. In general, you should use kCFAllocatorDefault unless one of the special circumstances exist below.
width, height
映像フレームのピクセル単位の幅と高さを指定する。
codecType
エンコードに利用するコーデックを渡す。以下に一覧があるので、利用したいものを利用すれば良い。未指定、すなわち raw データをそのまま利用する、といった指定はできない。圧縮処理の引数なのでそれはそう。
encoderSpecification
To specify a particular video encoder when creating a compression session, pass an encoderSpecification CFDictionary containing this key and the EncoderID as its value. The EncoderID CFString may be obtained from the kVTVideoEncoderList_EncoderID entry in the array returned by VTCopyVideoEncoderList.
imageBufferAttributes
ピクセルバッファプールを作成する時に利用する設定値を渡す。nil を受け渡すとバッファープールが作られないが、その場合には VideoToolBox が画像データをコピーする機会が増加してしまうという。 compressedDataAllocator
圧縮データ用の Allocator を指定する。macOS 10.12 以降だと、指定すると余分なバッファコピーが発生してしまうと注記されている。基本的には null で良さそう。
outputCallback
refcon
callback に受け渡したいオブジェクトを指定する。
compressionsSessionOut
これも C ライクな API だけど、この関数で生成した session を格納する変数へのポインタを受け渡す。
Compression Property