2010-10-27 17:30:11 +00:00
|
|
|
/*
|
2013-02-16 01:44:19 +00:00
|
|
|
* Copyright (C) 2010 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 "corevideobuffer.h"
|
|
|
|
|
|
|
|
static void
|
2012-02-28 07:13:04 +00:00
|
|
|
gst_core_video_meta_free (GstCoreVideoMeta * meta, GstBuffer * buf)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2012-02-28 07:13:04 +00:00
|
|
|
if (meta->pixbuf != NULL) {
|
2014-09-19 11:32:46 +00:00
|
|
|
CVPixelBufferUnlockBaseAddress (meta->pixbuf, 0);
|
2010-11-08 14:08:19 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
CVBufferRelease (meta->cvbuf);
|
2012-02-28 07:13:04 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2012-04-05 06:54:28 +00:00
|
|
|
GType
|
|
|
|
gst_core_video_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 ("GstCoreVideoMetaAPI", tags);
|
|
|
|
g_once_init_leave (&type, _type);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2012-02-28 07:13:04 +00:00
|
|
|
static const GstMetaInfo *
|
|
|
|
gst_core_video_meta_get_info (void)
|
|
|
|
{
|
|
|
|
static const GstMetaInfo *core_video_meta_info = NULL;
|
|
|
|
|
2012-10-03 18:05:06 +00:00
|
|
|
if (g_once_init_enter (&core_video_meta_info)) {
|
|
|
|
const GstMetaInfo *meta = gst_meta_register (GST_CORE_VIDEO_META_API_TYPE,
|
2012-02-28 07:13:04 +00:00
|
|
|
"GstCoreVideoMeta", sizeof (GstCoreVideoMeta),
|
|
|
|
(GstMetaInitFunction) NULL,
|
|
|
|
(GstMetaFreeFunction) gst_core_video_meta_free,
|
|
|
|
(GstMetaTransformFunction) NULL);
|
2012-10-03 18:05:06 +00:00
|
|
|
g_once_init_leave (&core_video_meta_info, meta);
|
2012-02-28 07:13:04 +00:00
|
|
|
}
|
|
|
|
return core_video_meta_info;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstBuffer *
|
2015-01-22 02:17:11 +00:00
|
|
|
gst_core_video_buffer_new (CVBufferRef cvbuf, GstVideoInfo * vinfo,
|
|
|
|
gboolean map)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
|
|
|
CVPixelBufferRef pixbuf = NULL;
|
2012-02-28 07:13:04 +00:00
|
|
|
GstBuffer *buf;
|
|
|
|
GstCoreVideoMeta *meta;
|
2013-12-19 07:57:41 +00:00
|
|
|
guint n_planes;
|
2012-03-03 18:03:11 +00:00
|
|
|
gsize offset[GST_VIDEO_MAX_PLANES];
|
|
|
|
gint stride[GST_VIDEO_MAX_PLANES];
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
if (CFGetTypeID (cvbuf) != CVPixelBufferGetTypeID ())
|
2010-10-27 17:30:11 +00:00
|
|
|
/* TODO: Do we need to handle other buffer types? */
|
|
|
|
goto error;
|
2012-03-03 18:03:11 +00:00
|
|
|
|
|
|
|
pixbuf = (CVPixelBufferRef) cvbuf;
|
|
|
|
|
2015-01-22 02:17:11 +00:00
|
|
|
if (map && CVPixelBufferLockBaseAddress (pixbuf, 0) != kCVReturnSuccess) {
|
2012-03-03 18:03:11 +00:00
|
|
|
goto error;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 07:13:04 +00:00
|
|
|
buf = gst_buffer_new ();
|
2012-03-03 18:03:11 +00:00
|
|
|
|
|
|
|
/* add the corevideo meta to free the underlying corevideo buffer */
|
2012-02-28 07:13:04 +00:00
|
|
|
meta = (GstCoreVideoMeta *) gst_buffer_add_meta (buf,
|
|
|
|
gst_core_video_meta_get_info (), NULL);
|
2013-04-24 20:15:01 +00:00
|
|
|
meta->cvbuf = CVBufferRetain (cvbuf);
|
2012-02-28 07:13:04 +00:00
|
|
|
meta->pixbuf = pixbuf;
|
2012-03-03 18:03:11 +00:00
|
|
|
|
|
|
|
/* set stride, offset and size */
|
|
|
|
memset (&offset, 0, sizeof (offset));
|
|
|
|
memset (&stride, 0, sizeof (stride));
|
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
if (CVPixelBufferIsPlanar (pixbuf)) {
|
2013-12-19 07:57:41 +00:00
|
|
|
int i, size, off;
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2013-04-24 20:15:01 +00:00
|
|
|
n_planes = CVPixelBufferGetPlaneCount (pixbuf);
|
2013-12-19 07:57:41 +00:00
|
|
|
off = 0;
|
|
|
|
for (i = 0; i < n_planes; ++i) {
|
2013-04-24 20:15:01 +00:00
|
|
|
stride[i] = CVPixelBufferGetBytesPerRowOfPlane (pixbuf, i);
|
2013-12-19 07:57:41 +00:00
|
|
|
size = stride[i] * CVPixelBufferGetHeightOfPlane (pixbuf, i);
|
|
|
|
offset[i] = off;
|
|
|
|
off += size;
|
|
|
|
|
2015-01-22 02:17:11 +00:00
|
|
|
if (map) {
|
|
|
|
gst_buffer_append_memory (buf,
|
|
|
|
gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
|
|
|
|
CVPixelBufferGetBaseAddressOfPlane (pixbuf, i), size, 0, size,
|
|
|
|
NULL, NULL));
|
|
|
|
}
|
2013-12-19 07:57:41 +00:00
|
|
|
}
|
2012-03-03 18:03:11 +00:00
|
|
|
} else {
|
2013-12-19 07:57:41 +00:00
|
|
|
int size;
|
|
|
|
|
2012-03-03 18:03:11 +00:00
|
|
|
n_planes = 1;
|
2013-12-08 15:46:58 +00:00
|
|
|
stride[0] = CVPixelBufferGetBytesPerRow (pixbuf);
|
2013-12-19 07:57:41 +00:00
|
|
|
offset[0] = 0;
|
2013-12-22 16:46:40 +00:00
|
|
|
size = stride[0] * CVPixelBufferGetHeight (pixbuf);
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2015-01-22 02:17:11 +00:00
|
|
|
if (map) {
|
|
|
|
gst_buffer_append_memory (buf,
|
|
|
|
gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
|
|
|
|
CVPixelBufferGetBaseAddress (pixbuf), size, 0, size, NULL, NULL));
|
|
|
|
}
|
2013-12-19 07:57:41 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2012-03-03 18:03:11 +00:00
|
|
|
if (vinfo) {
|
|
|
|
GstVideoMeta *video_meta;
|
|
|
|
|
2012-04-19 10:29:56 +00:00
|
|
|
video_meta =
|
2012-04-19 12:24:45 +00:00
|
|
|
gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
|
2013-12-22 16:46:40 +00:00
|
|
|
vinfo->finfo->format, CVPixelBufferGetWidth (pixbuf),
|
|
|
|
CVPixelBufferGetHeight (pixbuf), n_planes, offset, stride);
|
2012-03-03 18:03:11 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 07:13:04 +00:00
|
|
|
return buf;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
return NULL;
|
|
|
|
}
|