2010-10-27 17:30:11 +00:00
|
|
|
/*
|
2013-02-16 01:44:19 +00:00
|
|
|
* Copyright (C) 2009 Ole André Vadla Ravnås <oleavr@soundrop.com>
|
2014-04-09 09:55:03 +00:00
|
|
|
* Copyright (C) 2014 Collabora Ltd.
|
|
|
|
* Authors: Matthieu Bouron <matthieu.bouron@collabora.com>
|
2010-10-27 17:30:11 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-10-27 17:30:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "coremediabuffer.h"
|
|
|
|
|
|
|
|
static void
|
2012-02-28 07:13:04 +00:00
|
|
|
gst_core_media_meta_free (GstCoreMediaMeta * meta, GstBuffer * buf)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-11-07 20:37:29 +00:00
|
|
|
if (meta->image_buf != NULL) {
|
2013-04-24 20:15:01 +00:00
|
|
|
CVPixelBufferUnlockBaseAddress (meta->image_buf,
|
2012-02-28 07:13:04 +00:00
|
|
|
kCVPixelBufferLock_ReadOnly);
|
2014-03-11 10:18:22 +00:00
|
|
|
CVBufferRelease (meta->image_buf);
|
2013-11-07 20:37:29 +00:00
|
|
|
}
|
|
|
|
if (meta->block_buf != NULL) {
|
|
|
|
CFRelease (meta->block_buf);
|
|
|
|
}
|
2013-11-07 12:29:48 +00:00
|
|
|
|
|
|
|
CFRelease (meta->sample_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-05 06:53:41 +00:00
|
|
|
GType
|
|
|
|
gst_core_media_meta_api_get_type (void)
|
|
|
|
{
|
|
|
|
static volatile GType type;
|
|
|
|
static const gchar *tags[] = { "memory", NULL };
|
|
|
|
|
|
|
|
if (g_once_init_enter (&type)) {
|
|
|
|
GType _type = gst_meta_api_type_register ("GstCoreMediaMetaAPI", tags);
|
|
|
|
g_once_init_leave (&type, _type);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2012-02-28 07:13:04 +00:00
|
|
|
static const GstMetaInfo *
|
|
|
|
gst_core_media_meta_get_info (void)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2012-02-28 07:13:04 +00:00
|
|
|
static const GstMetaInfo *core_media_meta_info = NULL;
|
|
|
|
|
2012-10-03 18:05:06 +00:00
|
|
|
if (g_once_init_enter (&core_media_meta_info)) {
|
|
|
|
const GstMetaInfo *meta = gst_meta_register (GST_CORE_MEDIA_META_API_TYPE,
|
2012-02-28 07:13:04 +00:00
|
|
|
"GstCoreMediaMeta", sizeof (GstCoreMediaMeta),
|
|
|
|
(GstMetaInitFunction) NULL,
|
|
|
|
(GstMetaFreeFunction) gst_core_media_meta_free,
|
|
|
|
(GstMetaTransformFunction) NULL);
|
2012-10-03 18:05:06 +00:00
|
|
|
g_once_init_leave (&core_media_meta_info, meta);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
2012-02-28 07:13:04 +00:00
|
|
|
return core_media_meta_info;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
static GstVideoFormat
|
|
|
|
gst_core_media_buffer_get_video_format (OSType format)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2014-04-09 09:55:03 +00:00
|
|
|
switch (format) {
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
|
|
return GST_VIDEO_FORMAT_NV12;
|
|
|
|
case kCVPixelFormatType_422YpCbCr8_yuvs:
|
|
|
|
return GST_VIDEO_FORMAT_YUY2;
|
|
|
|
case kCVPixelFormatType_422YpCbCr8:
|
|
|
|
return GST_VIDEO_FORMAT_UYVY;
|
|
|
|
case kCVPixelFormatType_32BGRA:
|
|
|
|
return GST_VIDEO_FORMAT_BGRA;
|
|
|
|
default:
|
|
|
|
GST_WARNING ("Unknown OSType format: %d", (gint) format);
|
|
|
|
return GST_VIDEO_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
static gboolean
|
|
|
|
gst_core_media_buffer_wrap_pixel_buffer (GstBuffer * buf, GstVideoInfo * info,
|
|
|
|
CVPixelBufferRef pixel_buf, gboolean * has_padding)
|
|
|
|
{
|
|
|
|
guint n_planes;
|
|
|
|
gsize offset[GST_VIDEO_MAX_PLANES] = { 0 };
|
|
|
|
gint stride[GST_VIDEO_MAX_PLANES] = { 0 };
|
|
|
|
GstVideoMeta *video_meta;
|
|
|
|
UInt32 size;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
if (CVPixelBufferLockBaseAddress (pixel_buf,
|
|
|
|
kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess) {
|
|
|
|
GST_ERROR ("Could not lock pixel buffer base address");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
*has_padding = FALSE;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
if (CVPixelBufferIsPlanar (pixel_buf)) {
|
|
|
|
gint i, size = 0, plane_offset = 0;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
n_planes = CVPixelBufferGetPlaneCount (pixel_buf);
|
|
|
|
for (i = 0; i < n_planes; i++) {
|
|
|
|
stride[i] = CVPixelBufferGetBytesPerRowOfPlane (pixel_buf, i);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
if (stride[i] != GST_VIDEO_INFO_PLANE_STRIDE (info, i)) {
|
|
|
|
*has_padding = TRUE;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
2014-04-09 09:55:03 +00:00
|
|
|
|
|
|
|
size = stride[i] * CVPixelBufferGetHeightOfPlane (pixel_buf, i);
|
|
|
|
offset[i] = plane_offset;
|
|
|
|
plane_offset += size;
|
|
|
|
|
|
|
|
gst_buffer_append_memory (buf,
|
|
|
|
gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
|
|
|
|
CVPixelBufferGetBaseAddressOfPlane (pixel_buf, i), size, 0, size,
|
|
|
|
NULL, NULL));
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-09 09:55:03 +00:00
|
|
|
|
|
|
|
n_planes = 1;
|
|
|
|
stride[0] = CVPixelBufferGetBytesPerRow (pixel_buf);
|
|
|
|
offset[0] = 0;
|
|
|
|
size = stride[0] * CVPixelBufferGetHeight (pixel_buf);
|
|
|
|
|
|
|
|
gst_buffer_append_memory (buf,
|
|
|
|
gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
|
|
|
|
CVPixelBufferGetBaseAddress (pixel_buf), size, 0, size, NULL,
|
|
|
|
NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
video_meta =
|
|
|
|
gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
|
|
|
|
GST_VIDEO_INFO_FORMAT (info), info->width, info->height, n_planes, offset,
|
|
|
|
stride);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBuffer *
|
|
|
|
gst_core_media_buffer_new_from_buffer (GstBuffer * buf, GstVideoInfo * info)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
GstBuffer *copy_buf;
|
|
|
|
GstVideoFrame dest, src;
|
|
|
|
GstAllocator *allocator;
|
|
|
|
|
|
|
|
allocator = gst_allocator_find (GST_ALLOCATOR_SYSMEM);
|
|
|
|
if (!allocator) {
|
|
|
|
GST_ERROR ("Could not find SYSMEM allocator");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_buf = gst_buffer_new_allocate (allocator, info->size, NULL);
|
|
|
|
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&dest, info, copy_buf, GST_MAP_WRITE)) {
|
|
|
|
GST_ERROR ("Could not map destination frame");
|
2010-10-27 17:30:11 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
if (!gst_video_frame_map (&src, info, buf, GST_MAP_READ)) {
|
|
|
|
GST_ERROR ("Could not map source frame");
|
|
|
|
gst_video_frame_unmap (&dest);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_video_frame_copy (&dest, &src);
|
|
|
|
|
|
|
|
gst_video_frame_unmap (&dest);
|
|
|
|
gst_video_frame_unmap (&src);
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
GST_ERROR ("Could not copy frame");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return copy_buf;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (copy_buf) {
|
|
|
|
gst_buffer_unref (copy_buf);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_video_info_init_from_pixel_buffer (GstVideoInfo * info,
|
|
|
|
CVPixelBufferRef pixel_buf)
|
|
|
|
{
|
|
|
|
size_t width, height;
|
|
|
|
OSType format_type;
|
|
|
|
GstVideoFormat video_format;
|
|
|
|
|
|
|
|
width = CVPixelBufferGetWidth (pixel_buf);
|
|
|
|
height = CVPixelBufferGetHeight (pixel_buf);
|
|
|
|
format_type = CVPixelBufferGetPixelFormatType (pixel_buf);
|
|
|
|
video_format = gst_core_media_buffer_get_video_format (format_type);
|
|
|
|
|
|
|
|
if (video_format == GST_VIDEO_FORMAT_UNKNOWN) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_info_init (info);
|
|
|
|
gst_video_info_set_format (info, video_format, width, height);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstBuffer *
|
|
|
|
gst_core_media_buffer_new (CMSampleBufferRef sample_buf,
|
|
|
|
gboolean use_video_meta)
|
|
|
|
{
|
|
|
|
CVImageBufferRef image_buf;
|
|
|
|
CVPixelBufferRef pixel_buf;
|
|
|
|
CMBlockBufferRef block_buf;
|
|
|
|
GstCoreMediaMeta *meta;
|
|
|
|
GstBuffer *buf;
|
|
|
|
|
|
|
|
image_buf = CMSampleBufferGetImageBuffer (sample_buf);
|
|
|
|
pixel_buf = NULL;
|
|
|
|
block_buf = CMSampleBufferGetDataBuffer (sample_buf);
|
|
|
|
|
2012-02-28 07:13:04 +00:00
|
|
|
buf = gst_buffer_new ();
|
|
|
|
|
|
|
|
meta = (GstCoreMediaMeta *) gst_buffer_add_meta (buf,
|
|
|
|
gst_core_media_meta_get_info (), NULL);
|
2013-11-07 12:29:48 +00:00
|
|
|
CFRetain (sample_buf);
|
2013-11-07 20:37:29 +00:00
|
|
|
if (image_buf)
|
|
|
|
CVBufferRetain (image_buf);
|
|
|
|
if (block_buf)
|
|
|
|
CFRetain (block_buf);
|
2013-04-24 20:15:01 +00:00
|
|
|
meta->sample_buf = sample_buf;
|
2012-02-28 07:13:04 +00:00
|
|
|
meta->image_buf = image_buf;
|
|
|
|
meta->pixel_buf = pixel_buf;
|
|
|
|
meta->block_buf = block_buf;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2014-04-09 09:55:03 +00:00
|
|
|
if (image_buf != NULL && CFGetTypeID (image_buf) == CVPixelBufferGetTypeID ()) {
|
|
|
|
GstVideoInfo info;
|
|
|
|
gboolean has_padding = FALSE;
|
|
|
|
|
|
|
|
pixel_buf = (CVPixelBufferRef) image_buf;
|
|
|
|
if (!gst_video_info_init_from_pixel_buffer (&info, pixel_buf)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_core_media_buffer_wrap_pixel_buffer (buf, &info, pixel_buf,
|
|
|
|
&has_padding)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the video meta API is not supported, remove padding by
|
|
|
|
* copying the core media buffer to a system memory buffer */
|
|
|
|
if (has_padding && !use_video_meta) {
|
|
|
|
GstBuffer *copy_buf;
|
|
|
|
copy_buf = gst_core_media_buffer_new_from_buffer (buf, &info);
|
|
|
|
if (!copy_buf) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
buf = copy_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (block_buf != NULL) {
|
|
|
|
|
|
|
|
if (!gst_core_media_buffer_wrap_block_buffer (buf, block_buf)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
goto error;
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2012-02-28 07:13:04 +00:00
|
|
|
return buf;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
error:
|
2014-04-09 09:55:03 +00:00
|
|
|
if (buf) {
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVPixelBufferRef
|
2012-02-28 07:13:04 +00:00
|
|
|
gst_core_media_buffer_get_pixel_buffer (GstBuffer * buf)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2012-02-28 07:13:04 +00:00
|
|
|
GstCoreMediaMeta *meta = (GstCoreMediaMeta *) gst_buffer_get_meta (buf,
|
2012-04-05 06:53:41 +00:00
|
|
|
GST_CORE_MEDIA_META_API_TYPE);
|
2012-02-28 07:13:04 +00:00
|
|
|
g_return_val_if_fail (meta != NULL, NULL);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
return CVPixelBufferRetain (meta->pixel_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|