1/*
2 * Copyright (C) 2010 Ole André Vadla Ravnås <oleavr@soundrop.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#  include "config.h"
22#endif
23
24#if !HAVE_IOS
25#import <AppKit/AppKit.h>
26#include "iosurfacememory.h"
27#endif
28#include "iosglmemory.h"
29#include "videotexturecache.h"
30#include "coremediabuffer.h"
31#include "corevideobuffer.h"
32#include "vtutil.h"
33
34typedef struct _ContextThreadData
35{
36  GstVideoTextureCache *cache;
37  GstAppleCoreVideoPixelBuffer *gpixbuf;
38  guint plane;
39  gsize size;
40  GstMemory *memory;
41} ContextThreadData;
42
43typedef struct _TextureWrapper
44{
45#if HAVE_IOS
46    CVOpenGLESTextureCacheRef cache;
47    CVOpenGLESTextureRef texture;
48#else
49    CVOpenGLTextureCacheRef cache;
50    CVOpenGLTextureRef texture;
51#endif
52
53} TextureWrapper;
54
55GstVideoTextureCache *
56gst_video_texture_cache_new (GstGLContext * ctx)
57{
58  g_return_val_if_fail (ctx != NULL, NULL);
59
60  GstVideoTextureCache *cache = g_new0 (GstVideoTextureCache, 1);
61
62  cache->ctx = gst_object_ref (ctx);
63  gst_video_info_init (&cache->input_info);
64
65#if HAVE_IOS
66  CFMutableDictionaryRef cache_attrs =
67      CFDictionaryCreateMutable (NULL, 0, &kCFTypeDictionaryKeyCallBacks,
68      &kCFTypeDictionaryValueCallBacks);
69  CVOpenGLESTextureCacheCreate (kCFAllocatorDefault, (CFDictionaryRef) cache_attrs,
70      (__bridge CVEAGLContext) (gpointer)gst_gl_context_get_gl_context (ctx), NULL, &cache->cache);
71#else
72  gst_ios_surface_memory_init ();
73#if 0
74  cache->pool = GST_BUFFER_POOL (gst_gl_buffer_pool_new (ctx));
75#endif
76#endif
77
78  return cache;
79}
80
81void
82gst_video_texture_cache_free (GstVideoTextureCache * cache)
83{
84  g_return_if_fail (cache != NULL);
85
86#if HAVE_IOS
87  CFRelease (cache->cache); /* iOS has no "CVOpenGLESTextureCacheRelease" */
88#else
89#if 0
90  gst_buffer_pool_set_active (cache->pool, FALSE);
91  gst_object_unref (cache->pool);
92#endif
93#endif
94  gst_object_unref (cache->ctx);
95  if (cache->in_caps)
96    gst_caps_unref (cache->in_caps);
97  if (cache->out_caps)
98    gst_caps_unref (cache->out_caps);
99  g_free (cache);
100}
101
102void
103gst_video_texture_cache_set_format (GstVideoTextureCache * cache,
104    GstVideoFormat in_format, GstCaps * out_caps)
105{
106  GstCaps *in_caps;
107  GstCapsFeatures *features;
108
109  g_return_if_fail (gst_caps_is_fixed (out_caps));
110
111  out_caps = gst_caps_copy (out_caps);
112  features = gst_caps_get_features (out_caps, 0);
113  gst_video_info_from_caps (&cache->output_info, out_caps);
114
115  in_caps = gst_caps_copy (out_caps);
116  gst_caps_set_simple (in_caps, "format",
117          G_TYPE_STRING, gst_video_format_to_string (in_format), NULL);
118  features = gst_caps_get_features (in_caps, 0);
119  gst_video_info_from_caps (&cache->input_info, in_caps);
120
121  if (cache->in_caps)
122    gst_caps_unref (cache->in_caps);
123  if (cache->out_caps)
124    gst_caps_unref (cache->out_caps);
125  cache->in_caps = in_caps;
126  cache->out_caps = out_caps;
127
128#if 0
129  GstStructure *config = gst_buffer_pool_get_config (cache->pool);
130  gst_buffer_pool_config_set_params (config, cache->in_caps,
131          GST_VIDEO_INFO_SIZE (&cache->input_info), 0, 0);
132  gst_buffer_pool_config_set_allocator (config,
133          gst_allocator_find (GST_IO_SURFACE_MEMORY_ALLOCATOR_NAME), NULL);
134  gst_buffer_pool_config_add_option (config,
135          GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE);
136  gst_buffer_pool_set_config (cache->pool, config);
137  gst_buffer_pool_set_active (cache->pool, TRUE);
138#endif
139}
140
141#if HAVE_IOS
142void gst_video_texture_cache_release_texture(TextureWrapper *data) {
143    CFRelease(data->texture);
144    CFRelease(data->cache);
145    g_free(data);
146}
147
148static void
149_do_create_memory (GstGLContext * context, ContextThreadData * data)
150{
151  CVOpenGLESTextureRef texture = NULL;
152  GstVideoTextureCache *cache = data->cache;
153  GstAppleCoreVideoPixelBuffer *gpixbuf = data->gpixbuf;
154  CVPixelBufferRef pixel_buf = gpixbuf->buf;
155  guint plane = data->plane;
156  gssize size = data->size;
157  GstGLTextureTarget gl_target;
158  GstAppleCoreVideoMemory *memory;
159  GstIOSGLMemory *gl_memory;
160  GstGLFormat texformat;
161
162  switch (GST_VIDEO_INFO_FORMAT (&cache->input_info)) {
163      case GST_VIDEO_FORMAT_BGRA:
164        if (CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
165              cache->cache, pixel_buf, NULL, GL_TEXTURE_2D, GL_RGBA,
166              GST_VIDEO_INFO_WIDTH (&cache->input_info),
167              GST_VIDEO_INFO_HEIGHT (&cache->input_info),
168              GL_RGBA, GL_UNSIGNED_BYTE, 0, &texture) != kCVReturnSuccess)
169          goto error;
170
171        texformat = GST_GL_RGBA;
172        plane = 0;
173        goto success;
174      case GST_VIDEO_FORMAT_NV12: {
175        GstGLFormat texifmt, texfmt;
176
177        if (plane == 0)
178          texformat = GST_GL_LUMINANCE;
179        else
180          texformat = GST_GL_LUMINANCE_ALPHA;
181        texfmt = gst_gl_sized_gl_format_from_gl_format_type (cache->ctx, texformat, GL_UNSIGNED_BYTE);
182
183        if (CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
184              cache->cache, pixel_buf, NULL, GL_TEXTURE_2D, texformat,
185              GST_VIDEO_INFO_COMP_WIDTH (&cache->input_info, plane),
186              GST_VIDEO_INFO_COMP_HEIGHT (&cache->input_info, plane),
187              texfmt, GL_UNSIGNED_BYTE, plane, &texture) != kCVReturnSuccess)
188          goto error;
189
190        goto success;
191      }
192    default:
193      g_warn_if_reached ();
194      goto error;
195  }
196
197success: {
198  TextureWrapper *texture_data = g_new(TextureWrapper, 1);
199  CFRetain(cache->cache);
200  texture_data->cache = cache->cache;
201  texture_data->texture = texture;
202  gl_target = gst_gl_texture_target_from_gl (CVOpenGLESTextureGetTarget (texture));
203  memory = gst_apple_core_video_memory_new_wrapped (gpixbuf, plane, size);
204  gl_memory = gst_ios_gl_memory_new_wrapped (context, memory,
205      gl_target, texformat, CVOpenGLESTextureGetName (texture), &cache->input_info,
206     plane, NULL, texture_data, (GDestroyNotify) gst_video_texture_cache_release_texture);
207
208  data->memory = GST_MEMORY_CAST (gl_memory);
209
210  return;
211}
212
213error:
214  data->memory = NULL;
215}
216#endif
217
218GstMemory *
219gst_video_texture_cache_create_memory (GstVideoTextureCache * cache,
220      GstAppleCoreVideoPixelBuffer *gpixbuf,
221      guint plane,
222      gsize size)
223{
224  ContextThreadData data = {cache, gpixbuf, plane, size, NULL};
225
226#if HAVE_IOS
227  gst_gl_context_thread_add (cache->ctx,
228      (GstGLContextThreadFunc) _do_create_memory, &data);
229#endif
230
231  return data.memory;
232}
233