Allocate CMBlockBuffers using a memory pool.

Bug: webrtc:5258
Change-Id: Iae7549d618f797f4dc413671f0f2e53ed23be3e7
Reviewed-on: https://webrtc-review.googlesource.com/c/107738
Reviewed-by: Anders Carlsson <andersc@webrtc.org>
Commit-Queue: Kári Helgason <kthelgason@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25383}
This commit is contained in:
Kári Tristan Helgason
2018-10-26 10:59:28 +02:00
committed by Commit Bot
parent c35096d975
commit 0d247729a6
3 changed files with 26 additions and 11 deletions

View File

@ -71,12 +71,23 @@ void decompressionOutputCallback(void *decoderRef,
// Decoder.
@implementation RTCVideoDecoderH264 {
CMVideoFormatDescriptionRef _videoFormat;
CMMemoryPoolRef _memoryPool;
VTDecompressionSessionRef _decompressionSession;
RTCVideoDecoderCallback _callback;
OSStatus _error;
}
- (instancetype)init {
self = [super init];
if (self) {
_memoryPool = CMMemoryPoolCreate(nil);
}
return self;
}
- (void)dealloc {
CMMemoryPoolInvalidate(_memoryPool);
CFRelease(_memoryPool);
[self destroyDecompressionSession];
[self setVideoFormat:nullptr];
}
@ -129,7 +140,8 @@ void decompressionOutputCallback(void *decoderRef,
if (!webrtc::H264AnnexBBufferToCMSampleBuffer((uint8_t *)inputImage.buffer.bytes,
inputImage.buffer.length,
_videoFormat,
&sampleBuffer)) {
&sampleBuffer,
_memoryPool)) {
return WEBRTC_VIDEO_CODEC_ERROR;
}
RTC_DCHECK(sampleBuffer);

View File

@ -160,7 +160,8 @@ bool H264CMSampleBufferToAnnexBBuffer(
bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
size_t annexb_buffer_size,
CMVideoFormatDescriptionRef video_format,
CMSampleBufferRef* out_sample_buffer) {
CMSampleBufferRef* out_sample_buffer,
CMMemoryPoolRef memory_pool) {
RTC_DCHECK(annexb_buffer);
RTC_DCHECK(out_sample_buffer);
RTC_DCHECK(video_format);
@ -185,11 +186,11 @@ bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
}
// Allocate memory as a block buffer.
// TODO(tkchin): figure out how to use a pool.
CMBlockBufferRef block_buffer = nullptr;
CFAllocatorRef block_allocator = CMMemoryPoolGetAllocator(memory_pool);
OSStatus status = CMBlockBufferCreateWithMemoryBlock(
nullptr, nullptr, reader.BytesRemaining(), nullptr, nullptr, 0,
reader.BytesRemaining(), kCMBlockBufferAssureMemoryNowFlag,
kCFAllocatorDefault, nullptr, reader.BytesRemaining(), block_allocator,
nullptr, 0, reader.BytesRemaining(), kCMBlockBufferAssureMemoryNowFlag,
&block_buffer);
if (status != kCMBlockBufferNoErr) {
RTC_LOG(LS_ERROR) << "Failed to create block buffer.";
@ -199,8 +200,9 @@ bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
// Make sure block buffer is contiguous.
CMBlockBufferRef contiguous_buffer = nullptr;
if (!CMBlockBufferIsRangeContiguous(block_buffer, 0, 0)) {
status = CMBlockBufferCreateContiguous(
nullptr, block_buffer, nullptr, nullptr, 0, 0, 0, &contiguous_buffer);
status = CMBlockBufferCreateContiguous(kCFAllocatorDefault, block_buffer,
block_allocator, nullptr, 0, 0, 0,
&contiguous_buffer);
if (status != noErr) {
RTC_LOG(LS_ERROR) << "Failed to flatten non-contiguous block buffer: "
<< status;
@ -236,9 +238,9 @@ bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
}
// Create sample buffer.
status = CMSampleBufferCreate(nullptr, contiguous_buffer, true, nullptr,
nullptr, video_format, 1, 0, nullptr, 0,
nullptr, out_sample_buffer);
status = CMSampleBufferCreate(kCFAllocatorDefault, contiguous_buffer, true,
nullptr, nullptr, video_format, 1, 0, nullptr,
0, nullptr, out_sample_buffer);
if (status != noErr) {
RTC_LOG(LS_ERROR) << "Failed to create sample buffer.";
CFRelease(contiguous_buffer);

View File

@ -44,7 +44,8 @@ bool H264CMSampleBufferToAnnexBBuffer(
bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
size_t annexb_buffer_size,
CMVideoFormatDescriptionRef video_format,
CMSampleBufferRef* out_sample_buffer);
CMSampleBufferRef* out_sample_buffer,
CMMemoryPoolRef memory_pool);
// Returns a video format description created from the sps/pps information in
// the Annex B buffer. If there is no such information, nullptr is returned.