GstBuffer * gst_core_media_buffer_new (GstCoreMediaCtx * ctx, CMSampleBufferRef sample_buf) { GstCVApi *cv = ctx->cv; GstCMApi *cm = ctx->cm; CVImageBufferRef image_buf; CVPixelBufferRef pixel_buf; CMBlockBufferRef block_buf; Byte *data = NULL; UInt32 size; OSStatus status; GstBuffer *buf; GstCoreMediaMeta *meta; image_buf = cm->CMSampleBufferGetImageBuffer (sample_buf); pixel_buf = NULL; block_buf = cm->CMSampleBufferGetDataBuffer (sample_buf); if (image_buf != NULL && CFGetTypeID (image_buf) == cv->CVPixelBufferGetTypeID ()) { pixel_buf = (CVPixelBufferRef) image_buf; if (cv->CVPixelBufferLockBaseAddress (pixel_buf, kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess) { goto error; } if (cv->CVPixelBufferIsPlanar (pixel_buf)) { gint plane_count, plane_idx; data = cv->CVPixelBufferGetBaseAddressOfPlane (pixel_buf, 0); size = 0; plane_count = cv->CVPixelBufferGetPlaneCount (pixel_buf); for (plane_idx = 0; plane_idx != plane_count; plane_idx++) { size += cv->CVPixelBufferGetBytesPerRowOfPlane (pixel_buf, plane_idx) * cv->CVPixelBufferGetHeightOfPlane (pixel_buf, plane_idx); } } else { data = cv->CVPixelBufferGetBaseAddress (pixel_buf); size = cv->CVPixelBufferGetBytesPerRow (pixel_buf) * cv->CVPixelBufferGetHeight (pixel_buf); } } else if (block_buf != NULL) { status = cm->CMBlockBufferGetDataPointer (block_buf, 0, 0, 0, &data); if (status != noErr) goto error; size = cm->CMBlockBufferGetDataLength (block_buf); } else { goto error; } buf = gst_buffer_new (); meta = (GstCoreMediaMeta *) gst_buffer_add_meta (buf, gst_core_media_meta_get_info (), NULL); meta->ctx = g_object_ref (ctx); meta->sample_buf = cm->FigSampleBufferRetain (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; }
GstBuffer * gst_core_video_buffer_new (GstCoreMediaCtx * ctx, CVBufferRef cvbuf, GstVideoInfo * vinfo) { GstCVApi *cv = ctx->cv; void *data; size_t size; CVPixelBufferRef pixbuf = NULL; GstBuffer *buf; GstCoreVideoMeta *meta; guint width, height, n_planes, i; gsize offset[GST_VIDEO_MAX_PLANES]; gint stride[GST_VIDEO_MAX_PLANES]; if (CFGetTypeID (cvbuf) != cv->CVPixelBufferGetTypeID ()) /* TODO: Do we need to handle other buffer types? */ goto error; pixbuf = (CVPixelBufferRef) cvbuf; if (cv->CVPixelBufferLockBaseAddress (pixbuf, kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess) { goto error; } buf = gst_buffer_new (); /* add the corevideo meta to free the underlying corevideo buffer */ meta = (GstCoreVideoMeta *) gst_buffer_add_meta (buf, gst_core_video_meta_get_info (), NULL); meta->ctx = g_object_ref (ctx); meta->cvbuf = cv->CVBufferRetain (cvbuf); meta->pixbuf = pixbuf; /* set stride, offset and size */ memset (&offset, 0, sizeof (offset)); memset (&stride, 0, sizeof (stride)); data = cv->CVPixelBufferGetBaseAddress (pixbuf); height = cv->CVPixelBufferGetHeight (pixbuf); if (cv->CVPixelBufferIsPlanar (pixbuf)) { GstVideoInfo tmp_vinfo; n_planes = cv->CVPixelBufferGetPlaneCount (pixbuf); for (i = 0; i < n_planes; ++i) stride[i] = cv->CVPixelBufferGetBytesPerRowOfPlane (pixbuf, i); /* FIXME: don't hardcode NV12 */ gst_video_info_init (&tmp_vinfo); gst_video_info_set_format (&tmp_vinfo, GST_VIDEO_FORMAT_NV12, stride[0], height); offset[1] = tmp_vinfo.offset[1]; size = tmp_vinfo.size; } else { n_planes = 1; size = cv->CVPixelBufferGetBytesPerRow (pixbuf) * height; } gst_buffer_append_memory (buf, gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, data, size, 0, size, NULL, NULL)); if (vinfo) { GstVideoMeta *video_meta; width = vinfo->width; video_meta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_FORMAT_NV12, width, height, n_planes, offset, stride); } return buf; error: return NULL; }