2012-09-16 11:23:09 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
2014-03-24 13:34:13 +00:00
|
|
|
* Copyright (C) 2012-2014 Matthew Waters <ystree00@gmail.com>
|
2012-09-16 11:23:09 +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-08 11:53:56 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2012-09-16 11:23:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-07-10 15:03:04 +00:00
|
|
|
#include "gl.h"
|
2012-09-16 11:23:09 +00:00
|
|
|
#include "gstglupload.h"
|
|
|
|
|
2014-05-06 10:59:24 +00:00
|
|
|
#if GST_GL_HAVE_PLATFORM_EGL
|
|
|
|
#include "egl/gsteglimagememory.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstglupload
|
|
|
|
* @short_description: an object that uploads to GL textures
|
|
|
|
* @see_also: #GstGLDownload, #GstGLMemory
|
|
|
|
*
|
|
|
|
* #GstGLUpload is an object that uploads data from system memory into GL textures.
|
|
|
|
*
|
2013-07-18 09:36:27 +00:00
|
|
|
* A #GstGLUpload can be created with gst_gl_upload_new()
|
2012-09-27 05:53:46 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
#define USING_OPENGL(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_OPENGL)
|
|
|
|
#define USING_OPENGL3(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_OPENGL3)
|
|
|
|
#define USING_GLES(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES)
|
|
|
|
#define USING_GLES2(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES2)
|
|
|
|
#define USING_GLES3(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES3)
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
static gboolean _upload_memory (GstGLUpload * upload);
|
|
|
|
//static gboolean _do_upload_fill (GstGLContext * context, GstGLUpload * upload);
|
|
|
|
//static gboolean _do_upload_make (GstGLContext * context, GstGLUpload * upload);
|
2014-05-08 05:40:33 +00:00
|
|
|
static gboolean _init_upload (GstGLUpload * upload);
|
2014-04-01 02:30:51 +00:00
|
|
|
//static gboolean _init_upload_fbo (GstGLContext * context, GstGLUpload * upload);
|
2013-07-18 09:36:27 +00:00
|
|
|
static gboolean _gst_gl_upload_perform_with_data_unlocked (GstGLUpload * upload,
|
2012-09-16 11:23:09 +00:00
|
|
|
GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES]);
|
2013-11-14 04:27:52 +00:00
|
|
|
static void _do_upload_with_meta (GstGLContext * context, GstGLUpload * upload);
|
2014-05-08 03:46:29 +00:00
|
|
|
static void gst_gl_upload_reset (GstGLUpload * upload);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2012-12-06 07:40:26 +00:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
|
2014-03-24 13:34:13 +00:00
|
|
|
#define YUV_TO_RGB_COEFFICIENTS \
|
|
|
|
"uniform vec3 offset;\n" \
|
|
|
|
"uniform vec3 rcoeff;\n" \
|
|
|
|
"uniform vec3 gcoeff;\n" \
|
|
|
|
"uniform vec3 bcoeff;\n"
|
|
|
|
|
|
|
|
/* FIXME: use the colormatrix support from videoconvert */
|
2014-01-28 19:42:40 +00:00
|
|
|
struct TexData
|
|
|
|
{
|
2014-04-01 02:30:51 +00:00
|
|
|
guint format, type, width, height;
|
2014-01-30 19:53:54 +00:00
|
|
|
gfloat tex_scaling[2];
|
|
|
|
guint unpack_length;
|
2014-01-28 19:42:40 +00:00
|
|
|
};
|
|
|
|
|
2012-12-06 07:40:26 +00:00
|
|
|
struct _GstGLUploadPrivate
|
|
|
|
{
|
2013-07-18 09:17:18 +00:00
|
|
|
gboolean result;
|
|
|
|
|
2014-01-28 19:42:40 +00:00
|
|
|
struct TexData texture_info[GST_VIDEO_MAX_PLANES];
|
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
GstBuffer *buffer;
|
|
|
|
GstVideoFrame frame;
|
|
|
|
GstVideoGLTextureUploadMeta *meta;
|
|
|
|
guint tex_id;
|
|
|
|
gboolean mapped;
|
2012-12-06 07:40:26 +00:00
|
|
|
};
|
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_gl_upload_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_gl_upload_debug
|
|
|
|
|
|
|
|
#define DEBUG_INIT \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_upload_debug, "glupload", 0, "upload");
|
|
|
|
|
2014-05-08 05:30:49 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLUpload, gst_gl_upload, GST_TYPE_OBJECT, DEBUG_INIT);
|
2012-09-16 11:23:09 +00:00
|
|
|
static void gst_gl_upload_finalize (GObject * object);
|
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
#define GST_GL_UPLOAD_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
|
|
|
|
GST_TYPE_GL_UPLOAD, GstGLUploadPrivate))
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_upload_class_init (GstGLUploadClass * klass)
|
|
|
|
{
|
2012-12-06 07:40:26 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (GstGLUploadPrivate));
|
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
G_OBJECT_CLASS (klass)->finalize = gst_gl_upload_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_upload_init (GstGLUpload * upload)
|
|
|
|
{
|
2012-12-06 07:40:26 +00:00
|
|
|
upload->priv = GST_GL_UPLOAD_GET_PRIVATE (upload);
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
upload->context = NULL;
|
2014-05-06 09:14:26 +00:00
|
|
|
upload->priv->tex_id = 0;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
gst_video_info_set_format (&upload->in_info, GST_VIDEO_FORMAT_ENCODED, 0, 0);
|
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
g_mutex_init (&upload->lock);
|
|
|
|
}
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_upload_new:
|
2013-09-15 04:23:43 +00:00
|
|
|
* @context: a #GstGLContext
|
2012-09-27 05:53:46 +00:00
|
|
|
*
|
|
|
|
* Returns: a new #GstGLUpload object
|
|
|
|
*/
|
2012-09-16 11:23:09 +00:00
|
|
|
GstGLUpload *
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_upload_new (GstGLContext * context)
|
2012-09-16 11:23:09 +00:00
|
|
|
{
|
|
|
|
GstGLUpload *upload;
|
|
|
|
|
|
|
|
upload = g_object_new (GST_TYPE_GL_UPLOAD, NULL);
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
upload->context = gst_object_ref (context);
|
2014-04-01 02:30:51 +00:00
|
|
|
upload->convert = gst_gl_color_convert_new (context);
|
2012-12-06 07:40:26 +00:00
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
return upload;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_upload_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstGLUpload *upload;
|
|
|
|
|
|
|
|
upload = GST_GL_UPLOAD (object);
|
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
gst_gl_upload_reset (upload);
|
|
|
|
|
|
|
|
if (upload->context) {
|
|
|
|
gst_object_unref (upload->context);
|
|
|
|
upload->context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_clear (&upload->lock);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_gl_upload_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_upload_reset (GstGLUpload * upload)
|
|
|
|
{
|
2014-05-10 13:33:24 +00:00
|
|
|
guint i;
|
|
|
|
|
2014-05-06 09:14:26 +00:00
|
|
|
if (upload->priv->tex_id) {
|
|
|
|
gst_gl_context_del_texture (upload->context, &upload->priv->tex_id);
|
|
|
|
upload->priv->tex_id = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-01 02:30:51 +00:00
|
|
|
if (upload->convert) {
|
|
|
|
gst_object_unref (upload->convert);
|
2014-05-08 03:46:29 +00:00
|
|
|
upload->convert = NULL;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
if (upload->out_tex) {
|
|
|
|
gst_memory_unref ((GstMemory *) upload->out_tex);
|
|
|
|
upload->out_tex = NULL;
|
|
|
|
}
|
2014-05-10 13:33:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
|
|
|
|
if (upload->in_tex[i]) {
|
|
|
|
gst_memory_unref ((GstMemory *) upload->in_tex[i]);
|
|
|
|
upload->in_tex[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
static void
|
|
|
|
_gst_gl_upload_set_format_unlocked (GstGLUpload * upload,
|
2014-04-01 02:30:51 +00:00
|
|
|
GstVideoInfo *in_info)
|
2013-11-14 04:27:52 +00:00
|
|
|
{
|
2014-05-08 07:28:11 +00:00
|
|
|
g_return_if_fail (upload != NULL);
|
|
|
|
g_return_if_fail (GST_VIDEO_INFO_FORMAT (in_info) !=
|
|
|
|
GST_VIDEO_FORMAT_UNKNOWN);
|
|
|
|
g_return_if_fail (GST_VIDEO_INFO_FORMAT (in_info) !=
|
|
|
|
GST_VIDEO_FORMAT_ENCODED);
|
2013-11-14 04:27:52 +00:00
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
if (gst_video_info_is_equal (&upload->in_info, in_info))
|
|
|
|
return;
|
2013-11-14 04:27:52 +00:00
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
gst_gl_upload_reset (upload);
|
|
|
|
upload->convert = gst_gl_color_convert_new (upload->context);
|
2014-04-02 12:05:47 +00:00
|
|
|
upload->in_info = *in_info;
|
2014-05-08 03:46:29 +00:00
|
|
|
upload->initted = FALSE;
|
2013-11-14 04:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
2014-05-08 03:46:29 +00:00
|
|
|
* gst_gl_upload_set_format:
|
2012-09-27 05:53:46 +00:00
|
|
|
* @upload: a #GstGLUpload
|
2014-01-29 08:14:54 +00:00
|
|
|
* @in_info: input #GstVideoInfo
|
2012-09-27 05:53:46 +00:00
|
|
|
*
|
|
|
|
* Initializes @upload with the information required for upload.
|
|
|
|
*/
|
2014-05-08 03:46:29 +00:00
|
|
|
void
|
|
|
|
gst_gl_upload_set_format (GstGLUpload * upload, GstVideoInfo * in_info)
|
2012-09-16 11:23:09 +00:00
|
|
|
{
|
2013-11-14 04:27:52 +00:00
|
|
|
g_mutex_lock (&upload->lock);
|
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
_gst_gl_upload_set_format_unlocked (upload, in_info);
|
2013-11-14 04:27:52 +00:00
|
|
|
|
|
|
|
g_mutex_unlock (&upload->lock);
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:59:42 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_upload_get_format:
|
|
|
|
* @upload: a #GstGLUpload
|
|
|
|
*
|
2014-05-08 03:46:29 +00:00
|
|
|
* Returns: (transfer none): The #GstVideoInfo set by gst_gl_upload_set_format()
|
2014-05-07 14:59:42 +00:00
|
|
|
*/
|
|
|
|
GstVideoInfo *
|
|
|
|
gst_gl_upload_get_format (GstGLUpload * upload)
|
|
|
|
{
|
|
|
|
GstVideoInfo *ret;
|
|
|
|
|
|
|
|
g_mutex_lock (&upload->lock);
|
|
|
|
|
|
|
|
ret = &upload->in_info;
|
|
|
|
|
|
|
|
g_mutex_unlock (&upload->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_upload_perform_with_buffer:
|
|
|
|
* @upload: a #GstGLUpload
|
|
|
|
* @buffer: a #GstBuffer
|
|
|
|
* @tex_id: resulting texture
|
|
|
|
*
|
2013-11-23 11:57:03 +00:00
|
|
|
* Uploads @buffer to the texture given by @tex_id. @tex_id is valid
|
|
|
|
* until gst_gl_upload_release_buffer() is called.
|
2013-11-14 04:27:52 +00:00
|
|
|
*
|
|
|
|
* Returns: whether the upload was successful
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_gl_upload_perform_with_buffer (GstGLUpload * upload, GstBuffer * buffer,
|
|
|
|
guint * tex_id)
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
GstVideoGLTextureUploadMeta *gl_tex_upload_meta;
|
2014-04-01 02:30:51 +00:00
|
|
|
guint texture_ids[] = { 0, 0, 0, 0 };
|
2014-04-02 06:43:52 +00:00
|
|
|
gint i;
|
2014-05-09 22:59:46 +00:00
|
|
|
gboolean ret;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (upload != NULL, FALSE);
|
2013-11-14 04:27:52 +00:00
|
|
|
g_return_val_if_fail (buffer != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (tex_id != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (gst_buffer_n_memory (buffer) > 0, FALSE);
|
|
|
|
|
|
|
|
/* GstGLMemory */
|
|
|
|
mem = gst_buffer_peek_memory (buffer, 0);
|
|
|
|
|
|
|
|
if (gst_is_gl_memory (mem)) {
|
2014-05-12 11:51:38 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&upload->in_info) == GST_VIDEO_FORMAT_RGBA) {
|
|
|
|
GstMapInfo map_info;
|
|
|
|
|
|
|
|
gst_memory_map (mem, &map_info, GST_MAP_READ | GST_MAP_GL);
|
|
|
|
gst_memory_unmap (mem, &map_info);
|
|
|
|
|
|
|
|
*tex_id = ((GstGLMemory *) mem)->tex_id;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
if (!upload->out_tex)
|
|
|
|
upload->out_tex = (GstGLMemory *) gst_gl_memory_alloc (upload->context,
|
|
|
|
GST_VIDEO_GL_TEXTURE_TYPE_RGBA, GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info),
|
2014-05-01 03:57:16 +00:00
|
|
|
GST_VIDEO_INFO_PLANE_STRIDE (&upload->in_info, 0));
|
2014-04-02 06:43:52 +00:00
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
GST_LOG_OBJECT (upload, "Attempting upload with GstGLMemory");
|
2014-04-02 06:43:52 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&upload->in_info); i++) {
|
|
|
|
upload->in_tex[i] = (GstGLMemory *) gst_buffer_peek_memory (buffer, i);
|
2013-11-14 04:27:52 +00:00
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-09 22:59:46 +00:00
|
|
|
ret = _upload_memory (upload);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
*tex_id = upload->out_tex->tex_id;
|
2014-05-10 13:33:24 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&upload->in_info); i++) {
|
|
|
|
upload->in_tex[i] = NULL;
|
|
|
|
}
|
2014-05-09 22:59:46 +00:00
|
|
|
return ret;
|
2013-11-14 04:27:52 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 10:59:24 +00:00
|
|
|
#if GST_GL_HAVE_PLATFORM_EGL
|
|
|
|
if (!upload->priv->tex_id && gst_is_egl_image_memory (mem))
|
|
|
|
gst_gl_context_gen_texture (upload->context, &upload->priv->tex_id,
|
|
|
|
GST_VIDEO_FORMAT_RGBA, 0, 0);
|
|
|
|
#endif
|
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
if (!upload->priv->tex_id)
|
|
|
|
gst_gl_context_gen_texture (upload->context, &upload->priv->tex_id,
|
2014-04-02 06:43:52 +00:00
|
|
|
GST_VIDEO_FORMAT_RGBA, GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
2014-01-29 08:14:54 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info));
|
2013-11-14 04:27:52 +00:00
|
|
|
|
|
|
|
/* GstVideoGLTextureUploadMeta */
|
|
|
|
gl_tex_upload_meta = gst_buffer_get_video_gl_texture_upload_meta (buffer);
|
|
|
|
if (gl_tex_upload_meta) {
|
|
|
|
GST_LOG_OBJECT (upload, "Attempting upload with "
|
|
|
|
"GstVideoGLTextureUploadMeta");
|
|
|
|
texture_ids[0] = upload->priv->tex_id;
|
|
|
|
|
|
|
|
if (!gst_gl_upload_perform_with_gl_texture_upload_meta (upload,
|
|
|
|
gl_tex_upload_meta, texture_ids)) {
|
|
|
|
GST_DEBUG_OBJECT (upload, "Upload with GstVideoGLTextureUploadMeta "
|
|
|
|
"failed");
|
|
|
|
} else {
|
|
|
|
upload->priv->mapped = FALSE;
|
|
|
|
*tex_id = upload->priv->tex_id;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (upload, "Attempting upload with raw data");
|
|
|
|
/* GstVideoMeta map */
|
2014-01-29 08:14:54 +00:00
|
|
|
if (!gst_video_frame_map (&upload->priv->frame, &upload->in_info, buffer,
|
2013-11-14 04:27:52 +00:00
|
|
|
GST_MAP_READ)) {
|
|
|
|
GST_ERROR_OBJECT (upload, "Failed to map memory");
|
2012-09-16 11:23:09 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-05-09 21:59:43 +00:00
|
|
|
/* update the video info from the one updated by frame_map using video meta */
|
2014-05-11 02:48:52 +00:00
|
|
|
gst_gl_upload_set_format (upload, &upload->priv->frame.info);
|
2014-05-09 21:59:43 +00:00
|
|
|
|
2014-05-12 17:50:47 +00:00
|
|
|
if (!upload->priv->tex_id)
|
|
|
|
gst_gl_context_gen_texture (upload->context, &upload->priv->tex_id,
|
|
|
|
GST_VIDEO_FORMAT_RGBA, GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info));
|
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
if (!gst_gl_upload_perform_with_data (upload, upload->priv->tex_id,
|
|
|
|
upload->priv->frame.data)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
upload->priv->mapped = TRUE;
|
|
|
|
*tex_id = upload->priv->tex_id;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
void
|
|
|
|
gst_gl_upload_release_buffer (GstGLUpload * upload)
|
|
|
|
{
|
|
|
|
g_return_if_fail (upload != NULL);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
if (upload->priv->mapped)
|
|
|
|
gst_video_frame_unmap (&upload->priv->frame);
|
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
/*
|
|
|
|
* Uploads as a result of a call to gst_video_gl_texture_upload_meta_upload().
|
|
|
|
* i.e. provider of GstVideoGLTextureUploadMeta
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
_do_upload_for_meta (GstGLUpload * upload, GstVideoGLTextureUploadMeta * meta)
|
|
|
|
{
|
2014-04-01 02:30:51 +00:00
|
|
|
GstVideoInfo in_info;
|
2013-11-14 04:27:52 +00:00
|
|
|
GstVideoFrame frame;
|
|
|
|
GstMemory *mem;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (upload != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (meta != NULL, FALSE);
|
|
|
|
|
|
|
|
/* GstGLMemory */
|
|
|
|
mem = gst_buffer_peek_memory (upload->priv->buffer, 0);
|
|
|
|
|
|
|
|
if (gst_is_gl_memory (mem)) {
|
2014-04-02 06:43:52 +00:00
|
|
|
GstGLMemory *gl_mem = (GstGLMemory *) mem;
|
|
|
|
|
|
|
|
upload->in_tex[0] = gl_mem;
|
2014-05-09 22:59:46 +00:00
|
|
|
ret = _upload_memory (upload);
|
2014-04-02 06:43:52 +00:00
|
|
|
upload->in_tex[0] = NULL;
|
2013-11-14 04:27:52 +00:00
|
|
|
|
2014-05-09 22:59:46 +00:00
|
|
|
if (ret)
|
2013-11-14 04:27:52 +00:00
|
|
|
return TRUE;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 08:14:54 +00:00
|
|
|
if (!gst_video_frame_map (&frame, &in_info, upload->priv->buffer,
|
|
|
|
GST_MAP_READ)) {
|
2013-11-14 04:27:52 +00:00
|
|
|
GST_ERROR ("failed to map video frame");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:57:18 +00:00
|
|
|
/* update the video info from the one updated by frame_map using video meta */
|
|
|
|
gst_gl_upload_set_format (upload, &upload->priv->frame.info);
|
|
|
|
|
2014-05-12 17:50:47 +00:00
|
|
|
if (!upload->priv->tex_id)
|
|
|
|
gst_gl_context_gen_texture (upload->context, &upload->priv->tex_id,
|
|
|
|
GST_VIDEO_FORMAT_RGBA, GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info));
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
ret = _gst_gl_upload_perform_with_data_unlocked (upload,
|
|
|
|
upload->out_tex->tex_id, frame.data);
|
2013-11-14 04:27:52 +00:00
|
|
|
|
|
|
|
gst_video_frame_unmap (&frame);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uploads using gst_video_gl_texture_upload_meta_upload().
|
|
|
|
* i.e. consumer of GstVideoGLTextureUploadMeta
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_do_upload_with_meta (GstGLContext * context, GstGLUpload * upload)
|
|
|
|
{
|
|
|
|
guint texture_ids[] = { upload->priv->tex_id, 0, 0, 0 };
|
|
|
|
|
|
|
|
if (!gst_video_gl_texture_upload_meta_upload (upload->priv->meta,
|
|
|
|
texture_ids))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
upload->priv->result = TRUE;
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
upload->priv->result = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_gl_upload_perform_with_gl_texture_upload_meta:
|
|
|
|
* @upload: a #GstGLUpload
|
|
|
|
* @meta: a #GstVideoGLTextureUploadMeta
|
|
|
|
* @texture_id: resulting GL textures to place the data into.
|
|
|
|
*
|
|
|
|
* Uploads @meta into @texture_id.
|
|
|
|
*
|
|
|
|
* Returns: whether the upload was successful
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_gl_upload_perform_with_gl_texture_upload_meta (GstGLUpload * upload,
|
|
|
|
GstVideoGLTextureUploadMeta * meta, guint texture_id[4])
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (upload != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (meta != NULL, FALSE);
|
|
|
|
|
|
|
|
if (meta->texture_orientation !=
|
|
|
|
GST_VIDEO_GL_TEXTURE_ORIENTATION_X_NORMAL_Y_NORMAL)
|
|
|
|
GST_FIXME_OBJECT (upload, "only x-normal,y-normal textures supported, "
|
|
|
|
"the images will not appear the right way up");
|
|
|
|
if (meta->texture_type[0] != GST_VIDEO_GL_TEXTURE_TYPE_RGBA) {
|
|
|
|
GST_FIXME_OBJECT (upload, "only single rgba texture supported");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_lock (&upload->lock);
|
|
|
|
|
|
|
|
upload->priv->meta = meta;
|
2014-04-02 06:43:52 +00:00
|
|
|
if (!upload->priv->tex_id)
|
|
|
|
gst_gl_context_gen_texture (upload->context, &upload->priv->tex_id,
|
|
|
|
GST_VIDEO_FORMAT_RGBA, GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info));
|
2013-11-14 04:27:52 +00:00
|
|
|
|
|
|
|
GST_LOG ("Uploading with GLTextureUploadMeta with textures %i,%i,%i,%i",
|
|
|
|
texture_id[0], texture_id[1], texture_id[2], texture_id[3]);
|
|
|
|
|
|
|
|
gst_gl_context_thread_add (upload->context,
|
|
|
|
(GstGLContextThreadFunc) _do_upload_with_meta, upload);
|
|
|
|
|
|
|
|
ret = upload->priv->result;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
g_mutex_unlock (&upload->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-16 14:06:37 +00:00
|
|
|
static gboolean
|
2013-11-14 04:27:52 +00:00
|
|
|
_gst_gl_upload_perform_for_gl_texture_upload_meta (GstVideoGLTextureUploadMeta *
|
|
|
|
meta, guint texture_id[4])
|
|
|
|
{
|
|
|
|
GstGLUpload *upload;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (meta != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (texture_id != NULL, FALSE);
|
|
|
|
|
|
|
|
upload = meta->user_data;
|
|
|
|
|
|
|
|
g_mutex_lock (&upload->lock);
|
|
|
|
|
2014-05-12 12:32:31 +00:00
|
|
|
if (!upload->initted) {
|
|
|
|
GstVideoInfo in_info;
|
|
|
|
GstVideoMeta *v_meta = gst_buffer_get_video_meta (upload->priv->buffer);
|
2014-05-12 16:59:59 +00:00
|
|
|
gint i;
|
2014-05-12 12:32:31 +00:00
|
|
|
|
|
|
|
if (v_meta == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-05-12 16:59:59 +00:00
|
|
|
gst_video_info_init (&in_info);
|
|
|
|
in_info.finfo = gst_video_format_get_info (v_meta->format);
|
|
|
|
in_info.width = v_meta->width;
|
|
|
|
in_info.height = v_meta->height;
|
|
|
|
|
|
|
|
for (i = 0; i < in_info.finfo->n_planes; i++) {
|
|
|
|
in_info.offset[i] = v_meta->offset[i];
|
|
|
|
in_info.stride[i] = v_meta->stride[i];
|
|
|
|
}
|
2014-05-12 12:32:31 +00:00
|
|
|
|
|
|
|
_gst_gl_upload_set_format_unlocked (upload, &in_info);
|
|
|
|
|
2014-05-10 12:48:05 +00:00
|
|
|
upload->out_tex = gst_gl_memory_wrapped_texture (upload->context,
|
|
|
|
texture_id[0], GST_VIDEO_GL_TEXTURE_TYPE_RGBA,
|
|
|
|
GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
2014-05-12 12:32:31 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info), NULL, NULL);
|
|
|
|
}
|
2014-05-10 12:48:05 +00:00
|
|
|
|
|
|
|
/* FIXME: kinda breaks the abstraction */
|
2014-05-11 00:27:02 +00:00
|
|
|
if (upload->out_tex->tex_id != texture_id[0]) {
|
|
|
|
upload->out_tex->tex_id = texture_id[0];
|
|
|
|
GST_GL_MEMORY_FLAG_SET (upload->out_tex, GST_GL_MEMORY_FLAG_NEED_DOWNLOAD);
|
|
|
|
}
|
2013-11-14 04:27:52 +00:00
|
|
|
|
|
|
|
GST_LOG ("Uploading for meta with textures %i,%i,%i,%i", texture_id[0],
|
|
|
|
texture_id[1], texture_id[2], texture_id[3]);
|
|
|
|
|
|
|
|
ret = _do_upload_for_meta (upload, meta);
|
|
|
|
|
|
|
|
g_mutex_unlock (&upload->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_gl_upload_add_video_gl_texture_upload_meta:
|
|
|
|
* @upload: a #GstGLUpload
|
|
|
|
* @buffer: a #GstBuffer
|
|
|
|
*
|
|
|
|
* Adds a #GstVideoGLTextureUploadMeta on @buffer using @upload
|
|
|
|
*
|
|
|
|
* Returns: whether it was successful
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_gl_upload_add_video_gl_texture_upload_meta (GstGLUpload * upload,
|
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
2014-04-02 06:43:52 +00:00
|
|
|
GstVideoGLTextureType texture_types[GST_VIDEO_MAX_PLANES];
|
|
|
|
GstVideoMeta *v_meta;
|
|
|
|
gint i;
|
2013-11-14 04:27:52 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (upload != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (buffer != NULL, FALSE);
|
2014-04-02 06:43:52 +00:00
|
|
|
v_meta = gst_buffer_get_video_meta (buffer);
|
|
|
|
g_return_val_if_fail (v_meta != NULL, FALSE);
|
2013-11-14 04:27:52 +00:00
|
|
|
|
|
|
|
upload->priv->buffer = buffer;
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
|
|
|
|
texture_types[i] = gst_gl_texture_type_from_format (v_meta->format, i);
|
|
|
|
}
|
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
gst_buffer_add_video_gl_texture_upload_meta (buffer,
|
|
|
|
GST_VIDEO_GL_TEXTURE_ORIENTATION_X_NORMAL_Y_NORMAL, 1, texture_types,
|
|
|
|
_gst_gl_upload_perform_for_gl_texture_upload_meta,
|
|
|
|
gst_object_ref (upload), gst_object_ref, gst_object_unref);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_upload_perform_with_data:
|
|
|
|
* @upload: a #GstGLUpload
|
|
|
|
* @texture_id: the texture id to download
|
|
|
|
* @data: where the downloaded data should go
|
|
|
|
*
|
2013-11-23 11:57:03 +00:00
|
|
|
* Uploads @data into @texture_id. data size and format is specified by
|
2014-05-08 03:46:29 +00:00
|
|
|
* the #GstVideoInfo<!-- -->s passed to gst_gl_upload_set_format()
|
2012-09-27 05:53:46 +00:00
|
|
|
*
|
|
|
|
* Returns: whether the upload was successful
|
|
|
|
*/
|
2012-09-16 11:23:09 +00:00
|
|
|
gboolean
|
|
|
|
gst_gl_upload_perform_with_data (GstGLUpload * upload, GLuint texture_id,
|
|
|
|
gpointer data[GST_VIDEO_MAX_PLANES])
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (upload != NULL, FALSE);
|
|
|
|
|
|
|
|
g_mutex_lock (&upload->lock);
|
|
|
|
|
2014-05-10 12:48:05 +00:00
|
|
|
if (!upload->out_tex)
|
|
|
|
upload->out_tex = gst_gl_memory_wrapped_texture (upload->context, texture_id,
|
|
|
|
GST_VIDEO_GL_TEXTURE_TYPE_RGBA, GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info), NULL, NULL);
|
2014-04-02 06:43:52 +00:00
|
|
|
|
2014-05-10 12:48:05 +00:00
|
|
|
/* FIXME: kinda breaks the abstraction */
|
2014-05-11 00:27:02 +00:00
|
|
|
if (upload->out_tex->tex_id != texture_id) {
|
|
|
|
upload->out_tex->tex_id = texture_id;
|
|
|
|
GST_GL_MEMORY_FLAG_SET (upload->out_tex, GST_GL_MEMORY_FLAG_NEED_DOWNLOAD);
|
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-10 12:48:05 +00:00
|
|
|
ret = _gst_gl_upload_perform_with_data_unlocked (upload, texture_id, data);
|
2014-04-02 06:43:52 +00:00
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
g_mutex_unlock (&upload->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:36:27 +00:00
|
|
|
static gboolean
|
|
|
|
_gst_gl_upload_perform_with_data_unlocked (GstGLUpload * upload,
|
2012-09-16 11:23:09 +00:00
|
|
|
GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES])
|
|
|
|
{
|
2014-05-11 00:27:02 +00:00
|
|
|
guint i;
|
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
g_return_val_if_fail (upload != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (texture_id > 0, FALSE);
|
|
|
|
|
2014-05-10 13:33:24 +00:00
|
|
|
if (!upload->in_tex[0])
|
|
|
|
gst_gl_memory_setup_wrapped (upload->context, &upload->in_info, data,
|
|
|
|
upload->in_tex);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-11 00:27:02 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
|
|
|
|
if (upload->in_tex[i] && upload->in_tex[i]->data != data[i]) {
|
|
|
|
upload->in_tex[i]->data = data[i];
|
|
|
|
GST_GL_MEMORY_FLAG_SET (upload->in_tex[i], GST_GL_MEMORY_FLAG_NEED_UPLOAD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 04:27:52 +00:00
|
|
|
GST_LOG ("Uploading data into texture %u", texture_id);
|
|
|
|
|
2014-05-10 13:33:24 +00:00
|
|
|
return _upload_memory (upload);
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called in the gl thread */
|
2014-05-08 05:40:33 +00:00
|
|
|
static gboolean
|
2014-05-08 03:46:29 +00:00
|
|
|
_init_upload (GstGLUpload * upload)
|
2012-09-16 11:23:09 +00:00
|
|
|
{
|
2013-01-16 04:21:44 +00:00
|
|
|
GstGLFuncs *gl;
|
2012-09-16 11:23:09 +00:00
|
|
|
GstVideoFormat v_format;
|
2014-04-01 02:30:51 +00:00
|
|
|
GstVideoInfo out_info;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-08 03:46:29 +00:00
|
|
|
gl = upload->context->gl_vtable;
|
2013-01-16 04:21:44 +00:00
|
|
|
|
2014-01-29 08:14:54 +00:00
|
|
|
v_format = GST_VIDEO_INFO_FORMAT (&upload->in_info);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-03-09 11:47:11 +00:00
|
|
|
GST_INFO ("Initializing texture upload for format:%s",
|
|
|
|
gst_video_format_to_string (v_format));
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-07-18 09:17:18 +00:00
|
|
|
if (!gl->CreateProgramObject && !gl->CreateProgram) {
|
2014-05-08 03:46:29 +00:00
|
|
|
gst_gl_context_set_error (upload->context,
|
2013-07-18 09:17:18 +00:00
|
|
|
"Cannot upload YUV formats without OpenGL shaders");
|
|
|
|
goto error;
|
2013-07-15 12:11:20 +00:00
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
gst_video_info_set_format (&out_info, GST_VIDEO_FORMAT_RGBA,
|
|
|
|
GST_VIDEO_INFO_WIDTH (&upload->in_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&upload->in_info));
|
2014-04-01 02:30:51 +00:00
|
|
|
|
2014-05-09 08:07:03 +00:00
|
|
|
gst_gl_color_convert_set_format (upload->convert, &upload->in_info, &out_info);
|
2013-07-18 09:17:18 +00:00
|
|
|
|
2014-05-08 14:38:24 +00:00
|
|
|
upload->initted = TRUE;
|
|
|
|
|
2014-05-08 05:40:33 +00:00
|
|
|
return TRUE;
|
2013-07-18 09:17:18 +00:00
|
|
|
|
|
|
|
error:
|
2014-05-08 05:40:33 +00:00
|
|
|
return FALSE;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
static gboolean
|
|
|
|
_upload_memory (GstGLUpload * upload)
|
2012-09-16 11:23:09 +00:00
|
|
|
{
|
2014-04-01 02:30:51 +00:00
|
|
|
guint in_width, in_height;
|
2014-04-02 06:43:52 +00:00
|
|
|
guint in_texture[GST_VIDEO_MAX_PLANES];
|
|
|
|
GstGLMemory *out_texture[GST_VIDEO_MAX_PLANES] = {upload->out_tex, 0, 0, 0};
|
|
|
|
gint i;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-01-29 08:14:54 +00:00
|
|
|
in_width = GST_VIDEO_INFO_WIDTH (&upload->in_info);
|
|
|
|
in_height = GST_VIDEO_INFO_HEIGHT (&upload->in_info);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-08 05:40:33 +00:00
|
|
|
if (!upload->initted) {
|
|
|
|
if (!_init_upload (upload)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 03:46:29 +00:00
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&upload->in_info); i++) {
|
|
|
|
in_texture[i] = upload->in_tex[i]->tex_id;
|
2013-07-18 09:17:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
GST_TRACE ("uploading to texture:%u with textures:%u,%u,%u dimensions:%ux%u",
|
|
|
|
out_texture[0]->tex_id, in_texture[0], in_texture[1], in_texture[2],
|
|
|
|
in_width, in_height);
|
2013-07-18 09:17:18 +00:00
|
|
|
|
2014-05-10 13:33:24 +00:00
|
|
|
return gst_gl_color_convert_perform (upload->convert, upload->in_tex, out_texture);
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|