CMSampleBuffer 分析
CMSampleBuffer的使用
- 通過攝像頭取樣的視訊資料。
- 通過mic採集的音訊資訊。
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;
- AVAssetWriter儲存視訊AVAssetWriterInput
- (BOOL)appendSampleBuffer:(CMSampleBufferRef)sampleBuffer;
- 讀取視訊檔案的輸出流AVAssetReaderOutput
- (nullable CMSampleBufferRef)copyNextSampleBuffer CF_RETURNS_RETAINED;
- AVSampleBufferDisplayLayer 直接顯示解碼後的sampleBuffer
- (void)enqueueSampleBuffer:(CMSampleBufferRef)sampleBuffer;
- ARKit ARSessionObserver輸出捕獲的audioSampleBuffer
/**
This is called when the session outputs a new audio sample buffer.
@param session The session being run.
@param audioSampleBuffer The captured audio sample buffer.
*/
- (void)session:(ARSession *)session didOutputAudioSampleBuffer:(CMSampleBufferRef)audioSampleBuffer;
- VTCompressionSession硬體編碼作為VTCompressionOutputCallback輸出
typedef void (*VTCompressionOutputCallback)(
void * CM_NULLABLE outputCallbackRefCon,
void * CM_NULLABLE sourceFrameRefCon,
OSStatus status,
VTEncodeInfoFlags infoFlags,
CM_NULLABLE CMSampleBufferRef sampleBuffer );
注意:
Clients of CMSampleBuffer must explicitly manage the retain count by calling CFRetain and CFRelease, even in processes using garbage collection.
CMSampleBuffer 資料結構
- CMSampleBufferRef 這是一個包含零個或多個解碼後(未解碼)特定媒體型別的樣本(音訊,視訊,多路複用等)。
API註釋
A reference to a CMSampleBuffer, a CF object containing zero or more compressed (or uncompressed) samples of a particular media type (audio, video, muxed, etc).
- CaptureOutput輸出video CMSampleBuffer,編碼前內容
(lldb) po sampleBuffer
CMSampleBuffer retainCount: 1 allocator: 0x1b6708538
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
buffer-level attachments:
Orientation(P) = 1
{Exif} (P) {//省略}
DPIWidth (P) = 72
{TIFF} (P) {//省略}
DPIHeight (P) = 72
formatDescription = {
mediaType:'vide'
mediaSubType:'420f'
mediaSpecific: {
codecType: '420f' dimensions: 480 x 640
}
extensions: {{type = immutable dict, count = 6,
entries =>
0 :{contents = "CVImageBufferYCbCrMatrix"} = {contents = "ITU_R_601_4"}
1 : {contents = "CVImageBufferTransferFunction"} ={contents = "ITU_R_709_2"}
2 : {contents = "CVBytesPerRow"} ={value = +724, type = kCFNumberSInt32Type}
3 :{contents = "Version"} = {value = +2, type = kCFNumberSInt32Type}
5 : {contents = "CVImageBufferColorPrimaries"} = {contents = "ITU_R_709_2"}
6 : {contents = "CVImageBufferChromaLocationTopField"} = {contents = "Center"}
}
}
}
sbufToTrackReadiness = 0x0
numSamples = 1
sampleTimingArray[1] = {
{PTS = {6510691197875/1000000000 = 6510.691}, DTS = {INVALID}, duration = {INVALID}},
}
imageBuffer = 0x1c4134c80
(lldb)
- 硬編碼後的資料,相比編碼前多了sampleAttachmentsArray,顏色空間是'avc1'
CMSampleBuffer 0x11be1fda0 retainCount:
1 allocator: 0x1b6708538
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = {
mediaType:'vide'
mediaSubType:'avc1'
mediaSpecific: {
codecType: 'avc1' dimensions: 480 x 640
}
2 : {contents = "FormatName"} = H.264
sbufToTrackReadiness = 0x0
numSamples = 1
sampleTimingArray[1] = {
{PTS = {65/1000 = 0.065}, DTS = {64/1000 = 0.064}, duration = {INVALID}},
}
sampleSizeArray[1] = {
sampleSize = 212,
}
sampleAttachmentsArray[1] = {
sample 0:
NotSync = true
DependsOnOthers = true
}
dataBuffer = 0x1c410ac20
- CaptureOutput輸出音訊,buffer內容
CMSampleBuffer 0x125e41940 retainCount: 1 allocator: 0x1b6708538
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
mSampleRate: 44100.000000
mFormatID: 'lpcm'
mFormatFlags: 0xc
mBytesPerPacket: 2
mFramesPerPacket: 1
mBytesPerFrame: 2
mChannelsPerFrame: 1
mBitsPerChannel: 16 }
cookie: {(null)}
ACL: {(null)}
FormatList Array: {(null)}
}
extensions: {(null)}
}
sbufToTrackReadiness = 0x0
numSamples = 1024
sampleTimingArray[1] = {
{PTS = {10713597355917/1000000000 = 10713.597, rounded}, DTS = {INVALID}, duration = {1/44100 = 0.000}},
}
sampleSizeArray[1] = {
sampleSize = 2,
}
dataBuffer = 0x1c4304650
- AVAssetReaderOutput讀取MP4檔案sampleBuffer內容
CMSampleBuffer retainCount: 1 allocator: 0x1b6708538
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = {
mediaType:'vide'
mediaSubType:'BGRA'
mediaSpecific: {
codecType: 'BGRA' dimensions: 640 x 360
}
extensions: {{type = immutable dict, count = 9,
entries =>
0 : {contents = "CVFieldCount"} = {value = +1, type = kCFNumberSInt32Type}
1 : {contents = "CVImageBufferYCbCrMatrix"} = {contents = "YCbCrMatrix#5"}
2 : {contents = "CVBytesPerRow"} ={value = +2560, type = kCFNumberSInt32Type}
3 : {contents = "CVPixelAspectRatio"} = {type = immutable dict, count = 2,
entries =>
1 : {contents = "HorizontalSpacing"} = {value = +1, type = kCFNumberSInt32Type}
2 : {contents = "VerticalSpacing"} = {value = +1, type = kCFNumberSInt32Type}
}
4 : {contents = "Version"} = {value = +2, type = kCFNumberSInt32Type}
5 : {contents = "CVImageBufferColorPrimaries"} = SMPTE_C
8 : {contents = "CVImageBufferTransferFunction"} = {contents = "TransferFunction#5"}
10 : {contents = "CVImageBufferChromaLocationBottomField"} = Left
12 : {contents = "CVImageBufferChromaLocationTopField"} = Left
}
}
}
sbufToTrackReadiness = 0x0
numSamples = 1
sampleTimingArray[1] = {
{PTS = {0/600 = 0.000}, DTS = {INVALID}, duration = {INVALID}},
}
imageBuffer = 0x1c4139280
以上可以看出未編碼\解碼後的CMSampleBuffer都包含了imageBuffer資料、formatDescription資訊和sampleTimingArray資訊。錄影多了exif和tiff資訊。
CVPixelBuffer:編碼前和解碼後的影象資料結構.
無論是讀取還是錄製
output的settings的kCVPixelBufferPixelFormatTypeKey欄位 控制的mediaSubType輸出的色彩空間,轉換色彩空間過程有GPU參與,效能較高。可以按需設定。
CMSampleBuffer的建立及示例
- (void)appendVideoPixelBuffer:(CVPixelBufferRef)pixelBuffer withPresentationTime:(CMTime)presentationTime
{
CMSampleBufferRef sampleBuffer = NULL;
CMFormatDescriptionRef outputFormatDescription = NULL;
CMVideoFormatDescriptionCreateForImageBuffer( kCFAllocatorDefault, pixelBuffer, &outputFormatDescription );
CMSampleTimingInfo timingInfo = {0,};
timingInfo.duration = kCMTimeInvalid;
timingInfo.decodeTimeStamp = kCMTimeInvalid;
timingInfo.presentationTimeStamp = presentationTime;
OSStatus err = CMSampleBufferCreateForImageBuffer( kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, outputFormatDescription, &timingInfo, &sampleBuffer );
if ( sampleBuffer ) {
// do some thing
CFRelease( sampleBuffer );
}
else {
NSString *exceptionReason = [NSString stringWithFormat:@"sample buffer create failed (%i)", (int)err];
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:exceptionReason userInfo:nil];
}
}
- CMSampleBufferCreateReady與CMSampleBufferCreate相同,只是dataReady始終為true,因此不需要傳遞makeDataReadyCallback或refcon。
相關文章
- 後端分析/前端分析/邊緣分析後端前端
- 【漏洞分析】KaoyaSwap 安全事件分析事件
- USB 邏輯分析儀分析丟包怎麼分析(lecroy USB 邏輯分析儀)
- EOS原始碼分析(3)案例分析原始碼
- 日誌分析-apache日誌分析Apache
- Unity效能分析(二)CPU/GPU分析UnityGPU
- Giraph原始碼分析(六)——Edge 分析原始碼
- Java集合框架分析(五)LinkedHashMap分析Java框架HashMap
- Netty原始碼分析-- FastThreadLocal分析(十)Netty原始碼ASTthread
- 資料分析師招聘分析2.0
- 故障分析 | MySQL死鎖案例分析MySql
- 經營分析、資料分析、財務分析有什麼關係?
- 以太坊原始碼分析(13)RPC分析原始碼RPC
- Retrofit原始碼分析三 原始碼分析原始碼
- MyCAT原始碼分析——分析環境部署原始碼
- Unity效能分析(三)記憶體分析Unity記憶體
- 從電影分析到遊戲分析遊戲
- 詞法分析的前奏:字元分析(三)詞法分析字元
- 今日分析
- javaBean分析JavaBean
- SparseArray分析
- ThreadLocal分析thread
- Facebook 分析
- RACSignal分析
- LinkedList分析
- ArrayList分析
- SpringAOP分析Spring
- 文法分析
- 文字分析
- CyclicBarrier 分析
- 集合原始碼分析[2]-AbstractList 原始碼分析原始碼
- 集合原始碼分析[3]-ArrayList 原始碼分析原始碼
- Guava 原始碼分析之 EventBus 原始碼分析Guava原始碼
- 以太坊原始碼分析(8)區塊分析原始碼
- 以太坊原始碼分析(9)cmd包分析原始碼
- 以太坊原始碼分析(16)挖礦分析原始碼
- 【JDK原始碼分析系列】ArrayBlockingQueue原始碼分析JDK原始碼BloC
- 【架構分析】MESA (EGL/GLES)架構分析架構