Append the NAL data by reference instead of copying into a new buffer

This commit is contained in:
Cameron Gutman 2014-10-20 04:48:48 -04:00
parent f3b3d2bd08
commit f909371a90
2 changed files with 18 additions and 13 deletions

View File

@ -14,7 +14,7 @@
- (id)initWithView:(UIView*)view; - (id)initWithView:(UIView*)view;
- (size_t)updateBufferForRange:(CMBlockBufferRef)existingBuffer data:(unsigned char *)data offset:(int)offset length:(int)nalLength; - (void)updateBufferForRange:(CMBlockBufferRef)existingBuffer data:(unsigned char *)data offset:(int)offset length:(int)nalLength;
- (void)submitDecodeBuffer:(unsigned char *)data length:(int)length; - (void)submitDecodeBuffer:(unsigned char *)data length:(int)length;

View File

@ -39,38 +39,43 @@
#define FRAME_START_PREFIX_SIZE 4 #define FRAME_START_PREFIX_SIZE 4
#define NALU_START_PREFIX_SIZE 3 #define NALU_START_PREFIX_SIZE 3
- (size_t)updateBufferForRange:(CMBlockBufferRef)existingBuffer data:(unsigned char *)data offset:(int)offset length:(int)nalLength #define NAL_LENGTH_PREFIX_SIZE 4
- (void)updateBufferForRange:(CMBlockBufferRef)existingBuffer data:(unsigned char *)data offset:(int)offset length:(int)nalLength
{ {
OSStatus status; OSStatus status;
size_t oldOffset = CMBlockBufferGetDataLength(existingBuffer); size_t oldOffset = CMBlockBufferGetDataLength(existingBuffer);
// Append a 4 byte buffer to this block for the length prefix
status = CMBlockBufferAppendMemoryBlock(existingBuffer, NULL, status = CMBlockBufferAppendMemoryBlock(existingBuffer, NULL,
((4 + nalLength) - NALU_START_PREFIX_SIZE), NAL_LENGTH_PREFIX_SIZE,
kCFAllocatorDefault, NULL, 0, kCFAllocatorDefault, NULL, 0,
((4 + nalLength) - NALU_START_PREFIX_SIZE), 0); NAL_LENGTH_PREFIX_SIZE, 0);
if (status != noErr) { if (status != noErr) {
NSLog(@"CMBlockBufferAppendMemoryBlock failed: %d", (int)status); NSLog(@"CMBlockBufferAppendMemoryBlock failed: %d", (int)status);
return 0; return;
} }
// Write the length prefix to the new buffer
int dataLength = nalLength - NALU_START_PREFIX_SIZE; int dataLength = nalLength - NALU_START_PREFIX_SIZE;
const uint8_t lengthBytes[] = {(uint8_t)(dataLength >> 24), (uint8_t)(dataLength >> 16), const uint8_t lengthBytes[] = {(uint8_t)(dataLength >> 24), (uint8_t)(dataLength >> 16),
(uint8_t)(dataLength >> 8), (uint8_t)dataLength}; (uint8_t)(dataLength >> 8), (uint8_t)dataLength};
status = CMBlockBufferReplaceDataBytes(lengthBytes, existingBuffer, status = CMBlockBufferReplaceDataBytes(lengthBytes, existingBuffer,
oldOffset, 4); oldOffset, NAL_LENGTH_PREFIX_SIZE);
if (status != noErr) { if (status != noErr) {
NSLog(@"CMBlockBufferReplaceDataBytes failed: %d", (int)status); NSLog(@"CMBlockBufferReplaceDataBytes failed: %d", (int)status);
return 0; return;
} }
status = CMBlockBufferReplaceDataBytes(&data[offset+NALU_START_PREFIX_SIZE], existingBuffer, // Append the rest of the data by simply attaching a reference to the buffer
oldOffset + 4, dataLength); status = CMBlockBufferAppendMemoryBlock(existingBuffer, &data[offset+NALU_START_PREFIX_SIZE],
dataLength,
kCFAllocatorNull, // Don't deallocate data on free
NULL, 0, dataLength, 0);
if (status != noErr) { if (status != noErr) {
NSLog(@"CMBlockBufferReplaceDataBytes failed: %d", (int)status); NSLog(@"CMBlockBufferReplaceDataBytes failed: %d", (int)status);
return 0; return;
} }
return 4 + dataLength;
} }
- (void)submitDecodeBuffer:(unsigned char *)data length:(int)length - (void)submitDecodeBuffer:(unsigned char *)data length:(int)length
@ -110,7 +115,7 @@
2, /* count of parameter sets */ 2, /* count of parameter sets */
parameterSetPointers, parameterSetPointers,
parameterSetSizes, parameterSetSizes,
4 /* size of length prefix */, NAL_LENGTH_PREFIX_SIZE,
&formatDesc); &formatDesc);
if (status != noErr) { if (status != noErr) {
NSLog(@"Failed to create format description: %d", (int)status); NSLog(@"Failed to create format description: %d", (int)status);