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
|
|
|
*/
|
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2010-10-27 17:30:11 +00:00
|
|
|
#include "corevideobuffer.h"
|
2015-04-02 17:04:18 +00:00
|
|
|
#include "corevideomemory.h"
|
2016-08-26 07:37:54 +00:00
|
|
|
#if !HAVE_IOS
|
2019-09-02 05:00:27 +00:00
|
|
|
#include "iosurfaceglmemory.h"
|
2016-08-26 07:37:54 +00:00
|
|
|
#endif
|
2019-08-28 08:59:35 +00:00
|
|
|
#include "videotexturecache-gl.h"
|
2019-09-03 03:56:22 +00:00
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
#include "videotexturecache-vulkan.h"
|
|
|
|
#if !HAVE_IOS
|
|
|
|
#include "iosurfacevulkanmemory.h"
|
|
|
|
#endif
|
|
|
|
#endif
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-04-18 21:30:48 +00:00
|
|
|
static const GstMetaInfo *gst_core_video_meta_get_info (void);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_core_video_meta_add (GstBuffer * buffer, CVBufferRef cvbuf)
|
|
|
|
{
|
|
|
|
GstCoreVideoMeta *meta;
|
|
|
|
|
|
|
|
meta = (GstCoreVideoMeta *) gst_buffer_add_meta (buffer,
|
|
|
|
gst_core_video_meta_get_info (), NULL);
|
|
|
|
meta->cvbuf = CVBufferRetain (cvbuf);
|
|
|
|
meta->pixbuf = (CVPixelBufferRef) cvbuf;
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:17:24 +00:00
|
|
|
static gboolean
|
|
|
|
gst_core_video_meta_init (GstCoreVideoMeta * meta, gpointer params,
|
|
|
|
GstBuffer * buf)
|
|
|
|
{
|
|
|
|
meta->cvbuf = NULL;
|
|
|
|
meta->pixbuf = NULL;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-10-27 17:30:11 +00:00
|
|
|
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
|
|
|
{
|
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
|
|
|
|
2015-04-18 21:30:48 +00:00
|
|
|
static gboolean
|
|
|
|
gst_core_video_meta_transform (GstBuffer * transbuf, GstCoreVideoMeta * meta,
|
|
|
|
GstBuffer * buffer, GQuark type, GstMetaTransformCopy * data)
|
|
|
|
{
|
|
|
|
if (!data->region) {
|
|
|
|
/* only copy if the complete data is copied as well */
|
|
|
|
gst_core_video_meta_add (transbuf, meta->cvbuf);
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (transbuf,
|
|
|
|
"dropping Core Video metadata due to partial buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE; /* retval unused */
|
|
|
|
}
|
|
|
|
|
2012-04-05 06:54:28 +00:00
|
|
|
GType
|
|
|
|
gst_core_video_meta_api_get_type (void)
|
|
|
|
{
|
2021-03-22 03:34:36 +00:00
|
|
|
static GType type;
|
2012-04-05 06:54:28 +00:00
|
|
|
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),
|
2016-05-10 16:14:36 +00:00
|
|
|
(GstMetaInitFunction) gst_core_video_meta_init,
|
2012-02-28 07:13:04 +00:00
|
|
|
(GstMetaFreeFunction) gst_core_video_meta_free,
|
2015-04-18 21:30:48 +00:00
|
|
|
(GstMetaTransformFunction) gst_core_video_meta_transform);
|
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
|
|
|
}
|
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
static GstMemory *
|
|
|
|
_create_glmem (GstAppleCoreVideoPixelBuffer * gpixbuf,
|
|
|
|
GstVideoInfo * info, guint plane, gsize size, GstVideoTextureCache * cache)
|
|
|
|
{
|
|
|
|
#if HAVE_IOS
|
|
|
|
return gst_video_texture_cache_create_memory (cache, gpixbuf, plane, size);
|
|
|
|
#else
|
2019-09-02 05:00:27 +00:00
|
|
|
GstIOSurfaceGLMemory *mem;
|
2016-08-26 07:37:54 +00:00
|
|
|
CVPixelBufferRef pixel_buf = gpixbuf->buf;
|
|
|
|
IOSurfaceRef surface = CVPixelBufferGetIOSurface (pixel_buf);
|
2019-08-28 08:59:35 +00:00
|
|
|
GstGLFormat tex_format;
|
|
|
|
GstVideoTextureCacheGL *cache_gl = GST_VIDEO_TEXTURE_CACHE_GL (cache);
|
|
|
|
|
|
|
|
tex_format = gst_gl_format_from_video_info (cache_gl->ctx, info, plane);
|
2016-08-26 07:37:54 +00:00
|
|
|
|
|
|
|
CFRetain (pixel_buf);
|
2019-09-02 05:00:27 +00:00
|
|
|
mem = gst_io_surface_gl_memory_wrapped (cache_gl->ctx,
|
2017-03-13 03:28:47 +00:00
|
|
|
surface, GST_GL_TEXTURE_TARGET_RECTANGLE, tex_format,
|
2016-08-26 07:37:54 +00:00
|
|
|
info, plane, NULL, pixel_buf, (GDestroyNotify) CFRelease);
|
|
|
|
return GST_MEMORY_CAST (mem);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-03 03:56:22 +00:00
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
/* in videotexturecache-vulkan.m to avoid objc-ism from Metal being included
|
|
|
|
* in a non-objc file */
|
|
|
|
extern GstMemory *_create_vulkan_memory (GstAppleCoreVideoPixelBuffer * gpixbuf,
|
|
|
|
GstVideoInfo * info, guint plane, gsize size, GstVideoTextureCache * cache);
|
|
|
|
#endif
|
|
|
|
|
2015-04-02 17:04:18 +00:00
|
|
|
void
|
2016-08-26 07:37:54 +00:00
|
|
|
gst_core_video_wrap_pixel_buffer (GstBuffer * buf,
|
|
|
|
GstVideoInfo * info,
|
|
|
|
CVPixelBufferRef pixel_buf,
|
|
|
|
GstVideoTextureCache * cache, gboolean * has_padding)
|
2010-10-27 17:30:11 +00:00
|
|
|
{
|
2013-12-19 07:57:41 +00:00
|
|
|
guint n_planes;
|
2015-04-02 17:04:18 +00:00
|
|
|
gsize offset[GST_VIDEO_MAX_PLANES] = { 0 };
|
|
|
|
gint stride[GST_VIDEO_MAX_PLANES] = { 0 };
|
|
|
|
UInt32 size;
|
2016-01-19 04:50:22 +00:00
|
|
|
GstAppleCoreVideoPixelBuffer *gpixbuf;
|
2016-08-26 07:37:54 +00:00
|
|
|
GstMemory *mem = NULL;
|
2019-08-28 08:59:35 +00:00
|
|
|
gboolean do_gl = GST_IS_VIDEO_TEXTURE_CACHE_GL (cache);
|
2019-09-03 03:56:22 +00:00
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
gboolean do_vulkan = GST_IS_VIDEO_TEXTURE_CACHE_VULKAN (cache);
|
|
|
|
#endif
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2016-01-19 04:50:22 +00:00
|
|
|
gpixbuf = gst_apple_core_video_pixel_buffer_new (pixel_buf);
|
2016-08-26 07:37:54 +00:00
|
|
|
|
|
|
|
if (has_padding)
|
|
|
|
*has_padding = FALSE;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-04-02 17:04:18 +00:00
|
|
|
if (CVPixelBufferIsPlanar (pixel_buf)) {
|
|
|
|
gint i, size = 0, plane_offset = 0;
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2015-04-02 17:04:18 +00:00
|
|
|
n_planes = CVPixelBufferGetPlaneCount (pixel_buf);
|
|
|
|
for (i = 0; i < n_planes; i++) {
|
|
|
|
stride[i] = CVPixelBufferGetBytesPerRowOfPlane (pixel_buf, i);
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
if (stride[i] != GST_VIDEO_INFO_PLANE_STRIDE (info, i) && has_padding)
|
2015-04-02 17:04:18 +00:00
|
|
|
*has_padding = TRUE;
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2015-04-02 17:04:18 +00:00
|
|
|
size = stride[i] * CVPixelBufferGetHeightOfPlane (pixel_buf, i);
|
|
|
|
offset[i] = plane_offset;
|
|
|
|
plane_offset += size;
|
2013-12-19 07:57:41 +00:00
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
if (do_gl)
|
|
|
|
mem = _create_glmem (gpixbuf, info, i, size, cache);
|
2019-09-03 03:56:22 +00:00
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
else if (do_vulkan)
|
|
|
|
mem = _create_vulkan_memory (gpixbuf, info, i, size, cache);
|
|
|
|
#endif
|
2016-08-26 07:37:54 +00:00
|
|
|
else
|
|
|
|
mem =
|
|
|
|
GST_MEMORY_CAST (gst_apple_core_video_memory_new_wrapped (gpixbuf,
|
|
|
|
i, size));
|
|
|
|
gst_buffer_append_memory (buf, mem);
|
2015-04-02 17:04:18 +00:00
|
|
|
}
|
2012-03-03 18:03:11 +00:00
|
|
|
} else {
|
|
|
|
n_planes = 1;
|
2015-04-02 17:04:18 +00:00
|
|
|
stride[0] = CVPixelBufferGetBytesPerRow (pixel_buf);
|
2013-12-19 07:57:41 +00:00
|
|
|
offset[0] = 0;
|
2015-04-02 17:04:18 +00:00
|
|
|
size = stride[0] * CVPixelBufferGetHeight (pixel_buf);
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
if (do_gl)
|
|
|
|
mem = _create_glmem (gpixbuf, info, 0, size, cache);
|
2019-09-03 03:56:22 +00:00
|
|
|
#if defined(APPLEMEDIA_MOLTENVK)
|
|
|
|
else if (do_vulkan)
|
|
|
|
mem = _create_vulkan_memory (gpixbuf, info, 0, size, cache);
|
|
|
|
#endif
|
2016-08-26 07:37:54 +00:00
|
|
|
else
|
|
|
|
mem =
|
|
|
|
GST_MEMORY_CAST (gst_apple_core_video_memory_new_wrapped (gpixbuf, 0,
|
|
|
|
size));
|
|
|
|
gst_buffer_append_memory (buf, mem);
|
2013-12-19 07:57:41 +00:00
|
|
|
}
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2016-01-19 04:50:22 +00:00
|
|
|
gst_apple_core_video_pixel_buffer_unref (gpixbuf);
|
|
|
|
|
2015-04-02 17:04:18 +00:00
|
|
|
if (info) {
|
2012-03-03 18:03:11 +00:00
|
|
|
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,
|
2015-04-02 17:04:18 +00:00
|
|
|
GST_VIDEO_INFO_FORMAT (info), info->width, info->height, n_planes,
|
|
|
|
offset, stride);
|
2012-03-03 18:03:11 +00:00
|
|
|
}
|
2015-04-02 17:04:18 +00:00
|
|
|
}
|
2012-03-03 18:03:11 +00:00
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
static GstVideoFormat
|
|
|
|
gst_core_video_get_video_format (OSType format)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
case kCVPixelFormatType_32RGBA:
|
|
|
|
return GST_VIDEO_FORMAT_RGBA;
|
|
|
|
default:
|
|
|
|
GST_WARNING ("Unknown OSType format: %d", (gint) format);
|
|
|
|
return GST_VIDEO_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_core_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_video_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-02 17:04:18 +00:00
|
|
|
GstBuffer *
|
2016-08-26 07:37:54 +00:00
|
|
|
gst_core_video_buffer_new (CVBufferRef cvbuf, GstVideoInfo * vinfo,
|
|
|
|
GstVideoTextureCache * cache)
|
2015-04-02 17:04:18 +00:00
|
|
|
{
|
|
|
|
CVPixelBufferRef pixbuf = NULL;
|
|
|
|
GstBuffer *buf;
|
|
|
|
GstCoreVideoMeta *meta;
|
|
|
|
|
|
|
|
if (CFGetTypeID (cvbuf) != CVPixelBufferGetTypeID ())
|
|
|
|
/* TODO: Do we need to handle other buffer types? */
|
|
|
|
return NULL;
|
2010-10-27 17:30:11 +00:00
|
|
|
|
2015-04-02 17:04:18 +00:00
|
|
|
pixbuf = (CVPixelBufferRef) cvbuf;
|
|
|
|
|
|
|
|
buf = gst_buffer_new ();
|
|
|
|
|
2015-04-18 21:30:48 +00:00
|
|
|
/* add the corevideo meta to pass the underlying corevideo buffer */
|
2015-04-02 17:04:18 +00:00
|
|
|
meta = (GstCoreVideoMeta *) gst_buffer_add_meta (buf,
|
|
|
|
gst_core_video_meta_get_info (), NULL);
|
|
|
|
meta->cvbuf = CVBufferRetain (cvbuf);
|
|
|
|
meta->pixbuf = pixbuf;
|
|
|
|
|
2016-08-26 07:37:54 +00:00
|
|
|
gst_core_video_wrap_pixel_buffer (buf, vinfo, pixbuf, cache, NULL);
|
2015-04-02 17:04:18 +00:00
|
|
|
|
|
|
|
return buf;
|
2010-10-27 17:30:11 +00:00
|
|
|
}
|