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>
|
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);
|
2013-11-07 20:37:29 +00:00
|
|
|
CVBufferRelease(meta->image_buf);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
GstBuffer *
|
2013-04-24 20:15:01 +00:00
|
|
|
gst_core_media_buffer_new (CMSampleBufferRef sample_buf)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
CVImageBufferRef image_buf;
|
|
|
|
CVPixelBufferRef pixel_buf;
|
2010-11-02 21:53:33 +00:00
|
|
|
CMBlockBufferRef block_buf;
|
2013-04-24 20:15:01 +00:00
|
|
|
gchar *data = NULL;
|
2010-10-27 17:30:11 +00:00
|
|
|
UInt32 size;
|
2010-11-02 21:53:33 +00:00
|
|
|
OSStatus status;
|
2012-02-28 07:13:04 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
GstCoreMediaMeta *meta;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
image_buf = CMSampleBufferGetImageBuffer (sample_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
pixel_buf = NULL;
|
2013-04-24 20:15:01 +00:00
|
|
|
block_buf = CMSampleBufferGetDataBuffer (sample_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-11-07 12:29:48 +00:00
|
|
|
if (image_buf != NULL && CFGetTypeID (image_buf) == CVPixelBufferGetTypeID ()) {
|
2010-10-27 17:30:11 +00:00
|
|
|
pixel_buf = (CVPixelBufferRef) image_buf;
|
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
if (CVPixelBufferLockBaseAddress (pixel_buf,
|
2010-11-08 14:08:19 +00:00
|
|
|
kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess) {
|
2010-10-27 17:30:11 +00:00
|
|
|
goto error;
|
2010-11-08 14:08:19 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
if (CVPixelBufferIsPlanar (pixel_buf)) {
|
2010-10-27 17:30:11 +00:00
|
|
|
gint plane_count, plane_idx;
|
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
data = CVPixelBufferGetBaseAddressOfPlane (pixel_buf, 0);
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
size = 0;
|
2013-04-24 20:15:01 +00:00
|
|
|
plane_count = CVPixelBufferGetPlaneCount (pixel_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
for (plane_idx = 0; plane_idx != plane_count; plane_idx++) {
|
2013-04-24 20:15:01 +00:00
|
|
|
size += CVPixelBufferGetBytesPerRowOfPlane (pixel_buf, plane_idx) *
|
|
|
|
CVPixelBufferGetHeightOfPlane (pixel_buf, plane_idx);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 20:15:01 +00:00
|
|
|
data = CVPixelBufferGetBaseAddress (pixel_buf);
|
|
|
|
size = CVPixelBufferGetBytesPerRow (pixel_buf) *
|
|
|
|
CVPixelBufferGetHeight (pixel_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
} else if (block_buf != NULL) {
|
2013-04-24 20:15:01 +00:00
|
|
|
status = CMBlockBufferGetDataPointer (block_buf, 0, 0, 0, &data);
|
2010-11-02 21:53:33 +00:00
|
|
|
if (status != noErr)
|
2010-10-27 17:30:11 +00:00
|
|
|
goto error;
|
2013-04-24 20:15:01 +00:00
|
|
|
size = CMBlockBufferGetDataLength (block_buf);
|
2010-10-27 17:30:11 +00:00
|
|
|
} else {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-03-30 16:13:26 +00:00
|
|
|
gst_buffer_append_memory (buf,
|
2012-02-28 07:13:04 +00:00
|
|
|
gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE, data,
|
|
|
|
size, 0, size, NULL, NULL));
|
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:
|
|
|
|
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
|
|
|
}
|