2015-01-08 13:16:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Ole André Vadla Ravnås <oleavr@soundrop.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !HAVE_IOS
|
|
|
|
#import <AppKit/AppKit.h>
|
2015-01-23 04:02:55 +00:00
|
|
|
#include <gst/gl/cocoa/gstglcontext_cocoa.h>
|
2015-12-11 02:20:05 +00:00
|
|
|
#include <gst/gl/gstglbufferpool.h>
|
|
|
|
#include "iosurfacememory.h"
|
2015-01-08 13:16:17 +00:00
|
|
|
#endif
|
2015-12-10 05:22:08 +00:00
|
|
|
#include "videotexturecache.h"
|
2015-01-08 13:16:17 +00:00
|
|
|
#include "coremediabuffer.h"
|
2015-01-15 03:09:43 +00:00
|
|
|
#include "corevideobuffer.h"
|
2015-02-09 10:18:24 +00:00
|
|
|
#include "vtutil.h"
|
|
|
|
|
|
|
|
typedef struct _ContextThreadData
|
|
|
|
{
|
2015-12-10 05:22:08 +00:00
|
|
|
GstVideoTextureCache *cache;
|
2015-02-09 10:18:24 +00:00
|
|
|
GstBuffer *input_buffer;
|
|
|
|
GstBuffer *output_buffer;
|
|
|
|
} ContextThreadData;
|
2015-01-08 13:16:17 +00:00
|
|
|
|
2015-12-10 05:22:08 +00:00
|
|
|
GstVideoTextureCache *
|
|
|
|
gst_video_texture_cache_new (GstGLContext * ctx)
|
2015-01-08 13:16:17 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (ctx != NULL, NULL);
|
|
|
|
|
2015-12-10 05:22:08 +00:00
|
|
|
GstVideoTextureCache *cache = g_new0 (GstVideoTextureCache, 1);
|
2015-12-11 02:20:05 +00:00
|
|
|
|
2015-01-08 13:16:17 +00:00
|
|
|
cache->ctx = gst_object_ref (ctx);
|
2015-01-29 13:28:18 +00:00
|
|
|
gst_video_info_init (&cache->input_info);
|
2015-01-08 13:16:17 +00:00
|
|
|
|
2015-12-11 02:20:05 +00:00
|
|
|
#if HAVE_IOS
|
2015-02-09 10:18:24 +00:00
|
|
|
CFMutableDictionaryRef cache_attrs =
|
|
|
|
CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
gst_vtutil_dict_set_i32 (cache_attrs,
|
|
|
|
kCVOpenGLESTextureCacheMaximumTextureAgeKey, 0);
|
|
|
|
CVOpenGLESTextureCacheCreate (kCFAllocatorDefault, (CFDictionaryRef) cache_attrs,
|
2015-01-15 03:09:43 +00:00
|
|
|
(CVEAGLContext) gst_gl_context_get_gl_context (ctx), NULL, &cache->cache);
|
2015-12-11 02:20:05 +00:00
|
|
|
#else
|
|
|
|
gst_ios_surface_memory_init ();
|
|
|
|
#if 0
|
|
|
|
cache->pool = GST_BUFFER_POOL (gst_gl_buffer_pool_new (ctx));
|
|
|
|
#endif
|
2015-01-08 13:16:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2015-01-15 03:09:43 +00:00
|
|
|
void
|
2015-12-10 05:22:08 +00:00
|
|
|
gst_video_texture_cache_free (GstVideoTextureCache * cache)
|
2015-01-08 13:16:17 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (cache != NULL);
|
|
|
|
|
2015-12-11 02:20:05 +00:00
|
|
|
#if HAVE_IOS
|
2015-04-18 22:15:07 +00:00
|
|
|
CFRelease (cache->cache); /* iOS has no "CVOpenGLESTextureCacheRelease" */
|
2015-12-11 02:20:05 +00:00
|
|
|
#else
|
|
|
|
#if 0
|
|
|
|
gst_buffer_pool_set_active (cache->pool, FALSE);
|
|
|
|
gst_object_unref (cache->pool);
|
|
|
|
#endif
|
2015-01-08 13:16:17 +00:00
|
|
|
#endif
|
|
|
|
gst_object_unref (cache->ctx);
|
2015-10-29 02:04:31 +00:00
|
|
|
if (cache->in_caps)
|
|
|
|
gst_caps_unref (cache->in_caps);
|
|
|
|
if (cache->out_caps)
|
|
|
|
gst_caps_unref (cache->out_caps);
|
2015-01-08 13:16:17 +00:00
|
|
|
g_free (cache);
|
|
|
|
}
|
|
|
|
|
2015-01-29 13:28:18 +00:00
|
|
|
void
|
2015-12-10 05:22:08 +00:00
|
|
|
gst_video_texture_cache_set_format (GstVideoTextureCache * cache,
|
2015-04-20 07:08:23 +00:00
|
|
|
GstVideoFormat in_format, GstCaps * out_caps)
|
2015-01-08 13:16:17 +00:00
|
|
|
{
|
2015-01-29 13:28:18 +00:00
|
|
|
GstCaps *in_caps;
|
|
|
|
GstCapsFeatures *features;
|
|
|
|
|
|
|
|
g_return_if_fail (gst_caps_is_fixed (out_caps));
|
|
|
|
|
|
|
|
out_caps = gst_caps_copy (out_caps);
|
|
|
|
features = gst_caps_get_features (out_caps, 0);
|
|
|
|
gst_caps_features_add (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
|
2015-12-11 02:20:05 +00:00
|
|
|
gst_video_info_from_caps (&cache->output_info, out_caps);
|
|
|
|
|
2015-01-29 13:28:18 +00:00
|
|
|
in_caps = gst_caps_copy (out_caps);
|
2015-04-20 07:08:23 +00:00
|
|
|
gst_caps_set_simple (in_caps, "format",
|
|
|
|
G_TYPE_STRING, gst_video_format_to_string (in_format), NULL);
|
2015-01-29 13:28:18 +00:00
|
|
|
features = gst_caps_get_features (in_caps, 0);
|
|
|
|
gst_caps_features_add (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
|
|
|
|
gst_video_info_from_caps (&cache->input_info, in_caps);
|
|
|
|
|
2015-10-29 02:04:31 +00:00
|
|
|
if (cache->in_caps)
|
|
|
|
gst_caps_unref (cache->in_caps);
|
|
|
|
if (cache->out_caps)
|
|
|
|
gst_caps_unref (cache->out_caps);
|
|
|
|
cache->in_caps = in_caps;
|
|
|
|
cache->out_caps = out_caps;
|
2015-12-11 02:20:05 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
GstStructure *config = gst_buffer_pool_get_config (cache->pool);
|
|
|
|
gst_buffer_pool_config_set_params (config, cache->in_caps,
|
|
|
|
GST_VIDEO_INFO_SIZE (&cache->input_info), 0, 0);
|
|
|
|
gst_buffer_pool_config_set_allocator (config,
|
|
|
|
gst_allocator_find (GST_IO_SURFACE_MEMORY_ALLOCATOR_NAME), NULL);
|
|
|
|
gst_buffer_pool_config_add_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE);
|
|
|
|
gst_buffer_pool_set_config (cache->pool, config);
|
|
|
|
gst_buffer_pool_set_active (cache->pool, TRUE);
|
|
|
|
#endif
|
2015-01-29 13:28:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 10:18:24 +00:00
|
|
|
static CVPixelBufferRef
|
|
|
|
cv_pixel_buffer_from_gst_buffer (GstBuffer * buffer)
|
2015-01-29 13:28:18 +00:00
|
|
|
{
|
2015-01-15 03:09:43 +00:00
|
|
|
GstCoreMediaMeta *cm_meta =
|
2015-01-29 13:28:18 +00:00
|
|
|
(GstCoreMediaMeta *) gst_buffer_get_meta (buffer,
|
2015-01-15 03:09:43 +00:00
|
|
|
gst_core_media_meta_api_get_type ());
|
|
|
|
GstCoreVideoMeta *cv_meta =
|
2015-01-29 13:28:18 +00:00
|
|
|
(GstCoreVideoMeta *) gst_buffer_get_meta (buffer,
|
2015-01-15 03:09:43 +00:00
|
|
|
gst_core_video_meta_api_get_type ());
|
2015-02-03 16:32:03 +00:00
|
|
|
|
2015-02-09 10:18:24 +00:00
|
|
|
g_return_val_if_fail (cm_meta || cv_meta, NULL);
|
|
|
|
|
|
|
|
return cm_meta ? cm_meta->pixel_buf : cv_meta->pixbuf;
|
|
|
|
}
|
|
|
|
|
2015-12-11 02:20:05 +00:00
|
|
|
#if HAVE_IOS
|
2015-02-09 10:18:24 +00:00
|
|
|
static gboolean
|
2015-12-10 05:22:08 +00:00
|
|
|
gl_mem_from_buffer (GstVideoTextureCache * cache,
|
2015-02-09 10:18:24 +00:00
|
|
|
GstBuffer * buffer, GstMemory **mem1, GstMemory **mem2)
|
|
|
|
{
|
|
|
|
CVOpenGLESTextureRef texture = NULL;
|
|
|
|
CVPixelBufferRef pixel_buf = cv_pixel_buffer_from_gst_buffer (buffer);
|
2015-10-29 02:04:31 +00:00
|
|
|
GstGLTextureTarget gl_target;
|
2015-02-09 10:18:24 +00:00
|
|
|
|
|
|
|
*mem1 = NULL;
|
|
|
|
*mem2 = NULL;
|
2015-02-03 16:32:03 +00:00
|
|
|
|
2015-02-09 10:18:24 +00:00
|
|
|
CVOpenGLESTextureCacheFlush (cache->cache, 0);
|
|
|
|
|
2015-04-20 07:08:23 +00:00
|
|
|
switch (GST_VIDEO_INFO_FORMAT (&cache->input_info)) {
|
|
|
|
case GST_VIDEO_FORMAT_BGRA:
|
|
|
|
/* avfvideosrc does BGRA on iOS when doing GLMemory */
|
|
|
|
if (CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
|
|
|
|
cache->cache, pixel_buf, NULL, GL_TEXTURE_2D, GL_RGBA,
|
|
|
|
GST_VIDEO_INFO_WIDTH (&cache->input_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&cache->input_info),
|
|
|
|
GL_RGBA, GL_UNSIGNED_BYTE, 0, &texture) != kCVReturnSuccess)
|
|
|
|
goto error;
|
|
|
|
|
2015-10-30 05:38:17 +00:00
|
|
|
gl_target = gst_gl_texture_target_from_gl (CVOpenGLESTextureGetTarget (texture));
|
2015-10-29 02:04:31 +00:00
|
|
|
|
2015-12-14 08:18:14 +00:00
|
|
|
*mem1 = (GstMemory *) gst_gl_memory_pbo_wrapped_texture (cache->ctx,
|
2015-10-29 02:04:31 +00:00
|
|
|
CVOpenGLESTextureGetName (texture), gl_target,
|
2015-04-20 07:08:23 +00:00
|
|
|
&cache->input_info, 0, NULL, texture, (GDestroyNotify) CFRelease);
|
|
|
|
break;
|
2015-09-11 14:51:52 +00:00
|
|
|
case GST_VIDEO_FORMAT_NV12: {
|
|
|
|
GstVideoGLTextureType textype;
|
2015-11-10 03:21:33 +00:00
|
|
|
GLenum texifmt, texfmt;
|
2015-09-11 14:51:52 +00:00
|
|
|
|
|
|
|
textype = gst_gl_texture_type_from_format (cache->ctx, GST_VIDEO_FORMAT_NV12, 0);
|
2015-11-10 03:21:33 +00:00
|
|
|
texifmt = gst_gl_format_from_gl_texture_type (textype);
|
|
|
|
texfmt = gst_gl_sized_gl_format_from_gl_format_type (cache->ctx, texifmt, GL_UNSIGNED_BYTE);
|
2015-09-11 14:51:52 +00:00
|
|
|
|
2015-04-20 07:08:23 +00:00
|
|
|
/* vtdec does NV12 on iOS when doing GLMemory */
|
|
|
|
if (CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
|
2015-11-10 03:21:33 +00:00
|
|
|
cache->cache, pixel_buf, NULL, GL_TEXTURE_2D, texifmt,
|
2015-04-20 07:08:23 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&cache->input_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&cache->input_info),
|
2015-09-11 14:51:52 +00:00
|
|
|
texfmt, GL_UNSIGNED_BYTE, 0, &texture) != kCVReturnSuccess)
|
2015-04-20 07:08:23 +00:00
|
|
|
goto error;
|
|
|
|
|
2015-11-10 03:21:33 +00:00
|
|
|
gl_target = gst_gl_texture_target_from_gl (CVOpenGLESTextureGetTarget (texture));
|
2015-12-14 08:18:14 +00:00
|
|
|
*mem1 = (GstMemory *) gst_gl_memory_pbo_wrapped_texture (cache->ctx,
|
2015-10-29 02:04:31 +00:00
|
|
|
CVOpenGLESTextureGetName (texture), gl_target,
|
2015-04-20 07:08:23 +00:00
|
|
|
&cache->input_info, 0, NULL, texture, (GDestroyNotify) CFRelease);
|
|
|
|
|
2015-09-11 14:51:52 +00:00
|
|
|
textype = gst_gl_texture_type_from_format (cache->ctx, GST_VIDEO_FORMAT_NV12, 1);
|
2015-11-10 03:21:33 +00:00
|
|
|
texifmt = gst_gl_format_from_gl_texture_type (textype);
|
|
|
|
texfmt = gst_gl_sized_gl_format_from_gl_format_type (cache->ctx, texifmt, GL_UNSIGNED_BYTE);
|
2015-09-11 14:51:52 +00:00
|
|
|
|
2015-04-20 07:08:23 +00:00
|
|
|
if (CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
|
2015-11-10 03:21:33 +00:00
|
|
|
cache->cache, pixel_buf, NULL, GL_TEXTURE_2D, texifmt,
|
2015-04-20 07:08:23 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&cache->input_info) / 2,
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&cache->input_info) / 2,
|
2015-09-11 14:51:52 +00:00
|
|
|
texfmt, GL_UNSIGNED_BYTE, 1, &texture) != kCVReturnSuccess)
|
2015-04-20 07:08:23 +00:00
|
|
|
goto error;
|
|
|
|
|
2015-11-10 03:21:33 +00:00
|
|
|
gl_target = gst_gl_texture_target_from_gl (CVOpenGLESTextureGetTarget (texture));
|
2015-12-14 08:18:14 +00:00
|
|
|
*mem2 = (GstMemory *) gst_gl_memory_pbo_wrapped_texture (cache->ctx,
|
2015-10-29 02:04:31 +00:00
|
|
|
CVOpenGLESTextureGetName (texture), gl_target,
|
2015-04-20 07:08:23 +00:00
|
|
|
&cache->input_info, 0, NULL, texture, (GDestroyNotify) CFRelease);
|
|
|
|
break;
|
2015-09-11 14:51:52 +00:00
|
|
|
}
|
2015-12-11 02:20:05 +00:00
|
|
|
default:
|
|
|
|
g_warn_if_reached ();
|
|
|
|
goto error;
|
2015-10-29 02:04:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 02:20:05 +00:00
|
|
|
return TRUE;
|
2015-02-09 10:18:24 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (*mem1)
|
|
|
|
gst_memory_unref (*mem1);
|
|
|
|
if (*mem2)
|
|
|
|
gst_memory_unref (*mem2);
|
|
|
|
|
2015-12-11 02:20:05 +00:00
|
|
|
return FALSE;
|
2015-02-09 10:18:24 +00:00
|
|
|
}
|
2015-12-11 02:20:05 +00:00
|
|
|
#else /* !HAVE_IOS */
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gl_mem_from_buffer (GstVideoTextureCache * cache,
|
|
|
|
GstBuffer * buffer, GstMemory **mem1, GstMemory **mem2)
|
|
|
|
{
|
|
|
|
CVPixelBufferRef pixel_buf = cv_pixel_buffer_from_gst_buffer (buffer);
|
|
|
|
IOSurfaceRef surface = CVPixelBufferGetIOSurface(pixel_buf);
|
|
|
|
|
|
|
|
*mem1 = *mem2 = NULL;
|
|
|
|
for (int i = 0; i < GST_VIDEO_INFO_N_PLANES (&cache->input_info); i++) {
|
|
|
|
GstIOSurfaceMemory *mem;
|
|
|
|
|
|
|
|
CFRetain (pixel_buf);
|
|
|
|
mem = gst_io_surface_memory_wrapped (cache->ctx,
|
|
|
|
surface, GST_GL_TEXTURE_TARGET_RECTANGLE, &cache->input_info,
|
|
|
|
i, NULL, pixel_buf, (GDestroyNotify) CFRelease);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
*mem1 = (GstMemory *) mem;
|
|
|
|
else
|
|
|
|
*mem2 = (GstMemory *) mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
2015-02-09 10:18:24 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
_do_get_gl_buffer (GstGLContext * context, ContextThreadData * data)
|
|
|
|
{
|
|
|
|
GstMemory *mem1 = NULL, *mem2 = NULL;
|
2015-12-10 05:22:08 +00:00
|
|
|
GstVideoTextureCache *cache = data->cache;
|
2015-02-09 10:18:24 +00:00
|
|
|
GstBuffer *buffer = data->input_buffer;
|
|
|
|
|
|
|
|
if (!gl_mem_from_buffer (cache, buffer, &mem1, &mem2)) {
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_append_memory (buffer, mem1);
|
|
|
|
if (mem2)
|
|
|
|
gst_buffer_append_memory (buffer, mem2);
|
|
|
|
|
2015-12-14 05:10:01 +00:00
|
|
|
data->output_buffer = buffer;
|
2015-01-29 13:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstBuffer *
|
2015-12-10 05:22:08 +00:00
|
|
|
gst_video_texture_cache_get_gl_buffer (GstVideoTextureCache * cache,
|
2015-01-29 13:28:18 +00:00
|
|
|
GstBuffer * cv_buffer)
|
|
|
|
{
|
2015-02-09 10:18:24 +00:00
|
|
|
ContextThreadData data = {cache, cv_buffer, NULL};
|
|
|
|
gst_gl_context_thread_add (cache->ctx,
|
|
|
|
(GstGLContextThreadFunc) _do_get_gl_buffer, &data);
|
|
|
|
return data.output_buffer;
|
2015-01-08 13:16:17 +00:00
|
|
|
}
|