static gboolean gst_core_media_buffer_wrap_block_buffer (GstBuffer * buf, CMBlockBufferRef block_buf) { OSStatus status; gchar *data = NULL; size_t offset = 0, length_at_offset, total_length; /* CMBlockBuffer can contain multiple non-continuous memory blocks */ do { status = CMBlockBufferGetDataPointer (block_buf, offset, &length_at_offset, &total_length, &data); if (status != kCMBlockBufferNoErr) { return FALSE; } /* retaining the CMBlockBuffer so it won't go away for the lifetime of the GstMemory */ gst_buffer_append_memory (buf, gst_memory_new_wrapped (0, data, length_at_offset, 0, length_at_offset, (gpointer) CFRetain (block_buf), (GDestroyNotify) CFRelease)); offset += length_at_offset; } while (offset < total_length); return TRUE; }
void vtCallback(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus status, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer ) { CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sampleBuffer); CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, false); CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); bool isKeyframe = false; if(attachments != NULL) { CFDictionaryRef attachment; CFBooleanRef dependsOnOthers; attachment = (CFDictionaryRef)CFArrayGetValueAtIndex(attachments, 0); dependsOnOthers = (CFBooleanRef)CFDictionaryGetValue(attachment, kCMSampleAttachmentKey_DependsOnOthers); isKeyframe = (dependsOnOthers == kCFBooleanFalse); } if(isKeyframe) { // Send the SPS and PPS. CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuffer); size_t spsSize, ppsSize; size_t parmCount; const uint8_t* sps, *pps; CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 0, &sps, &spsSize, &parmCount, nullptr ); CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pps, &ppsSize, &parmCount, nullptr ); std::unique_ptr<uint8_t[]> sps_buf (new uint8_t[spsSize + 4]) ; std::unique_ptr<uint8_t[]> pps_buf (new uint8_t[ppsSize + 4]) ; memcpy(&sps_buf[4], sps, spsSize); spsSize+=4 ; memcpy(&sps_buf[0], &spsSize, 4); memcpy(&pps_buf[4], pps, ppsSize); ppsSize += 4; memcpy(&pps_buf[0], &ppsSize, 4); ((H264Encode*)outputCallbackRefCon)->compressionSessionOutput((uint8_t*)sps_buf.get(),spsSize, pts.value); ((H264Encode*)outputCallbackRefCon)->compressionSessionOutput((uint8_t*)pps_buf.get(),ppsSize, pts.value); } char* bufferData; size_t size; CMBlockBufferGetDataPointer(block, 0, NULL, &size, &bufferData); ((H264Encode*)outputCallbackRefCon)->compressionSessionOutput((uint8_t*)bufferData,size, pts.value); }
static void h264_enc_output_cb(VTH264EncCtx *ctx, void *sourceFrameRefCon, OSStatus status, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer) { MSQueue nalu_queue; CMBlockBufferRef block_buffer; size_t read_size, frame_size; bool_t is_keyframe = FALSE; mblk_t *nalu; int i; if(sampleBuffer == NULL || status != noErr) { ms_error("VideoToolbox: could not encode frame: error %d", status); return; } ms_mutex_lock(&ctx->mutex); if(ctx->is_configured) { ms_queue_init(&nalu_queue); block_buffer = CMSampleBufferGetDataBuffer(sampleBuffer); frame_size = CMBlockBufferGetDataLength(block_buffer); for(i=0, read_size=0; read_size < frame_size; i++) { char *chunk; size_t chunk_size; int idr_count; CMBlockBufferGetDataPointer(block_buffer, i, &chunk_size, NULL, &chunk); ms_h264_stream_to_nalus((uint8_t *)chunk, chunk_size, &nalu_queue, &idr_count); if(idr_count) is_keyframe = TRUE; read_size += chunk_size; } if(is_keyframe) { mblk_t *insertion_point = ms_queue_peek_first(&nalu_queue); const uint8_t *parameter_set; size_t parameter_set_size; size_t parameter_set_count; CMFormatDescriptionRef format_desc = CMSampleBufferGetFormatDescription(sampleBuffer); i=0; do { CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format_desc, i, ¶meter_set, ¶meter_set_size, ¶meter_set_count, NULL); nalu = allocb(parameter_set_size, 0); memcpy(nalu->b_wptr, parameter_set, parameter_set_size); nalu->b_wptr += parameter_set_size; ms_queue_insert(&nalu_queue, insertion_point, nalu); i++; } while(i < parameter_set_count); } rfc3984_pack(&ctx->packer_ctx, &nalu_queue, &ctx->queue, (uint32_t)(ctx->f->ticker->time * 90)); } ms_mutex_unlock(&ctx->mutex); }
static gboolean gst_core_media_buffer_wrap_block_buffer (GstBuffer * buf, CMBlockBufferRef block_buf) { OSStatus status; gchar *data = NULL; UInt32 size; status = CMBlockBufferGetDataPointer (block_buf, 0, 0, 0, &data); if (status != noErr) { return FALSE; } size = CMBlockBufferGetDataLength (block_buf); gst_buffer_append_memory (buf, gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, data, size, 0, size, NULL, NULL)); return TRUE; }
GstBuffer * gst_core_media_buffer_new (CMSampleBufferRef sample_buf) { CVImageBufferRef image_buf; CVPixelBufferRef pixel_buf; CMBlockBufferRef block_buf; gchar *data = NULL; UInt32 size; OSStatus status; GstBuffer *buf; GstCoreMediaMeta *meta; image_buf = CMSampleBufferGetImageBuffer (sample_buf); pixel_buf = NULL; block_buf = CMSampleBufferGetDataBuffer (sample_buf); if (image_buf != NULL && CFGetTypeID (image_buf) == CVPixelBufferGetTypeID ()) { pixel_buf = (CVPixelBufferRef) image_buf; if (CVPixelBufferLockBaseAddress (pixel_buf, kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess) { goto error; } if (CVPixelBufferIsPlanar (pixel_buf)) { gint plane_count, plane_idx; data = CVPixelBufferGetBaseAddressOfPlane (pixel_buf, 0); size = 0; plane_count = CVPixelBufferGetPlaneCount (pixel_buf); for (plane_idx = 0; plane_idx != plane_count; plane_idx++) { size += CVPixelBufferGetBytesPerRowOfPlane (pixel_buf, plane_idx) * CVPixelBufferGetHeightOfPlane (pixel_buf, plane_idx); } } else { data = CVPixelBufferGetBaseAddress (pixel_buf); size = CVPixelBufferGetBytesPerRow (pixel_buf) * CVPixelBufferGetHeight (pixel_buf); } } else if (block_buf != NULL) { status = CMBlockBufferGetDataPointer (block_buf, 0, 0, 0, &data); if (status != noErr) goto error; size = CMBlockBufferGetDataLength (block_buf); } else { goto error; } buf = gst_buffer_new (); meta = (GstCoreMediaMeta *) gst_buffer_add_meta (buf, gst_core_media_meta_get_info (), NULL); CVBufferRetain ((CVBufferRef)sample_buf); meta->sample_buf = sample_buf; meta->image_buf = image_buf; meta->pixel_buf = pixel_buf; meta->block_buf = block_buf; gst_buffer_append_memory (buf, gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, data, size, 0, size, NULL, NULL)); return buf; error: return NULL; }