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; }