2012-09-16 11:23:09 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2012 Matthew Waters <ystree00@gmail.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
|
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 "gstgldownload.h"
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstgldownload
|
|
|
|
* @short_description: an object that downloads GL textures
|
|
|
|
* @see_also: #GstGLUpload, #GstGLMemory
|
|
|
|
*
|
|
|
|
* #GstGLDownload is an object that downloads GL textures into system memory.
|
|
|
|
*
|
2013-07-18 09:37:32 +00:00
|
|
|
* A #GstGLDownload can be created with gst_gl_download_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)
|
|
|
|
|
|
|
|
static void _do_download (GstGLContext * context, GstGLDownload * download);
|
|
|
|
static void _init_download (GstGLContext * context, GstGLDownload * download);
|
2013-07-18 09:37:32 +00:00
|
|
|
static gboolean _gst_gl_download_perform_with_data_unlocked (GstGLDownload *
|
2012-09-16 11:23:09 +00:00
|
|
|
download, GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES]);
|
|
|
|
|
2012-12-06 07:40:26 +00:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
struct _GstGLDownloadPrivate
|
|
|
|
{
|
|
|
|
const gchar *YUY2_UYVY;
|
|
|
|
const gchar *I420_YV12;
|
|
|
|
const gchar *AYUV;
|
2013-07-15 15:08:42 +00:00
|
|
|
const gchar *ARGB;
|
2012-12-06 07:40:26 +00:00
|
|
|
const gchar *vert_shader;
|
|
|
|
|
2013-07-19 06:48:03 +00:00
|
|
|
gboolean result;
|
2012-12-06 07:40:26 +00:00
|
|
|
};
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_gl_download_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_gl_download_debug
|
|
|
|
|
|
|
|
#define DEBUG_INIT \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_download_debug, "gldownload", 0, "download");
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLDownload, gst_gl_download, G_TYPE_OBJECT,
|
|
|
|
DEBUG_INIT);
|
|
|
|
static void gst_gl_download_finalize (GObject * object);
|
|
|
|
|
|
|
|
#define GST_GL_DOWNLOAD_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
|
|
|
|
GST_TYPE_GL_DOWNLOAD, GstGLDownloadPrivate))
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_download_class_init (GstGLDownloadClass * klass)
|
|
|
|
{
|
2012-12-06 07:40:26 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (GstGLDownloadPrivate));
|
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
G_OBJECT_CLASS (klass)->finalize = gst_gl_download_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_download_init (GstGLDownload * download)
|
|
|
|
{
|
2012-12-06 07:40:26 +00:00
|
|
|
|
|
|
|
download->priv = GST_GL_DOWNLOAD_GET_PRIVATE (download);
|
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
g_mutex_init (&download->lock);
|
|
|
|
|
|
|
|
gst_video_info_init (&download->info);
|
|
|
|
}
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_download_new:
|
2013-09-15 04:23:43 +00:00
|
|
|
* @context: a #GstGLContext
|
2012-09-27 05:53:46 +00:00
|
|
|
*
|
|
|
|
* Returns: a new #GstGLDownload object
|
|
|
|
*/
|
2012-09-16 11:23:09 +00:00
|
|
|
GstGLDownload *
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_download_new (GstGLContext * context)
|
2012-09-16 11:23:09 +00:00
|
|
|
{
|
|
|
|
GstGLDownload *download;
|
|
|
|
|
|
|
|
download = g_object_new (GST_TYPE_GL_DOWNLOAD, NULL);
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
download->context = gst_object_ref (context);
|
2014-04-01 02:30:51 +00:00
|
|
|
download->convert = gst_gl_color_convert_new (context);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
return download;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_download_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstGLDownload *download;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
download = GST_GL_DOWNLOAD (object);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
|
|
|
|
if (download->out_texture[i]) {
|
2014-04-02 06:43:52 +00:00
|
|
|
gst_memory_unref ((GstMemory *) download->out_texture[i]);
|
|
|
|
download->out_texture[i] = NULL;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-15 04:23:43 +00:00
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
if (download->in_texture) {
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_del_texture (download->context, &download->in_texture);
|
2012-09-16 11:23:09 +00:00
|
|
|
download->in_texture = 0;
|
|
|
|
}
|
2014-04-01 02:30:51 +00:00
|
|
|
if (download->convert) {
|
|
|
|
gst_object_unref (download->convert);
|
|
|
|
download->convert = NULL;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
if (download->context) {
|
|
|
|
gst_object_unref (download->context);
|
|
|
|
download->context = NULL;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_clear (&download->lock);
|
2013-06-13 04:36:41 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_gl_download_parent_class)->finalize (object);
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_download_init_format:
|
|
|
|
* @download: a #GstGLDownload
|
|
|
|
* @v_format: a #GstVideoFormat
|
|
|
|
* @out_width: the width to download to
|
|
|
|
* @out_height: the height to download to
|
|
|
|
*
|
|
|
|
* Initializes @download with the information required for download.
|
|
|
|
*
|
|
|
|
* Returns: whether the initialization was successful
|
|
|
|
*/
|
2012-09-18 11:39:21 +00:00
|
|
|
gboolean
|
|
|
|
gst_gl_download_init_format (GstGLDownload * download, GstVideoFormat v_format,
|
2012-09-25 05:20:21 +00:00
|
|
|
guint out_width, guint out_height)
|
2012-09-18 11:39:21 +00:00
|
|
|
{
|
|
|
|
GstVideoInfo info;
|
2013-07-19 06:48:03 +00:00
|
|
|
gboolean ret;
|
2012-09-18 11:39:21 +00:00
|
|
|
|
2013-07-18 09:37:32 +00:00
|
|
|
g_return_val_if_fail (download != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (v_format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
|
|
|
|
g_return_val_if_fail (v_format != GST_VIDEO_FORMAT_ENCODED, FALSE);
|
|
|
|
g_return_val_if_fail (out_width > 0 && out_height > 0, FALSE);
|
2012-09-18 11:39:21 +00:00
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
g_mutex_lock (&download->lock);
|
|
|
|
|
2012-09-18 11:39:21 +00:00
|
|
|
if (download->initted) {
|
|
|
|
g_mutex_unlock (&download->lock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-25 05:20:21 +00:00
|
|
|
gst_video_info_set_format (&info, v_format, out_width, out_height);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
download->info = info;
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_thread_add (download->context,
|
|
|
|
(GstGLContextThreadFunc) _init_download, download);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-07-19 06:48:03 +00:00
|
|
|
ret = download->initted = download->priv->result;
|
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
g_mutex_unlock (&download->lock);
|
|
|
|
|
2013-07-19 06:48:03 +00:00
|
|
|
return ret;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_download_perform_with_memory:
|
|
|
|
* @download: a #GstGLDownload
|
|
|
|
* @gl_mem: a #GstGLMemory
|
|
|
|
*
|
|
|
|
* Downloads the texture in @gl_mem
|
|
|
|
*
|
|
|
|
* Returns: whether the download was successful
|
|
|
|
*/
|
2012-09-18 11:39:21 +00:00
|
|
|
gboolean
|
|
|
|
gst_gl_download_perform_with_memory (GstGLDownload * download,
|
|
|
|
GstGLMemory * gl_mem)
|
|
|
|
{
|
|
|
|
gpointer data[GST_VIDEO_MAX_PLANES];
|
|
|
|
guint i;
|
|
|
|
gboolean ret;
|
|
|
|
|
2013-07-18 09:37:32 +00:00
|
|
|
if (!GST_GL_MEMORY_FLAG_IS_SET (gl_mem, GST_GL_MEMORY_FLAG_DOWNLOAD_INITTED))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!GST_GL_MEMORY_FLAG_IS_SET (gl_mem, GST_GL_MEMORY_FLAG_NEED_DOWNLOAD)) {
|
2012-09-18 11:39:21 +00:00
|
|
|
return FALSE;
|
2013-07-18 09:37:32 +00:00
|
|
|
}
|
2012-09-18 11:39:21 +00:00
|
|
|
|
2012-09-16 11:23:09 +00:00
|
|
|
g_mutex_lock (&download->lock);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
|
|
|
|
data[i] = (guint8 *) gl_mem->data +
|
|
|
|
GST_VIDEO_INFO_PLANE_OFFSET (&download->info, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret =
|
2013-07-18 09:37:32 +00:00
|
|
|
_gst_gl_download_perform_with_data_unlocked (download, gl_mem->tex_id,
|
2012-09-16 11:23:09 +00:00
|
|
|
data);
|
|
|
|
|
2013-07-18 09:37:32 +00:00
|
|
|
if (ret)
|
|
|
|
GST_GL_MEMORY_FLAG_UNSET (gl_mem, GST_GL_MEMORY_FLAG_NEED_DOWNLOAD);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
g_mutex_unlock (&download->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_download_perform_with_data:
|
|
|
|
* @download: a #GstGLDownload
|
|
|
|
* @texture_id: the texture id to download
|
|
|
|
* @data: (out): where the downloaded data should go
|
|
|
|
*
|
|
|
|
* Downloads @texture_id into @data. @data size and format is specified by
|
|
|
|
* the #GstVideoFormat passed to gst_gl_download_init_format()
|
|
|
|
*
|
|
|
|
* Returns: whether the download was successful
|
|
|
|
*/
|
2012-09-16 11:23:09 +00:00
|
|
|
gboolean
|
|
|
|
gst_gl_download_perform_with_data (GstGLDownload * download, GLuint texture_id,
|
|
|
|
gpointer data[GST_VIDEO_MAX_PLANES])
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (download != NULL, FALSE);
|
|
|
|
|
|
|
|
g_mutex_lock (&download->lock);
|
|
|
|
|
2013-07-18 09:37:32 +00:00
|
|
|
ret =
|
|
|
|
_gst_gl_download_perform_with_data_unlocked (download, texture_id, data);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
g_mutex_unlock (&download->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:37:32 +00:00
|
|
|
static gboolean
|
|
|
|
_gst_gl_download_perform_with_data_unlocked (GstGLDownload * download,
|
|
|
|
GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES])
|
2012-09-16 11:23:09 +00:00
|
|
|
{
|
2014-04-02 06:43:52 +00:00
|
|
|
gboolean realloc = FALSE;
|
|
|
|
gpointer temp_data;
|
2012-09-16 11:23:09 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (download != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (texture_id > 0, FALSE);
|
|
|
|
g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&download->info) !=
|
|
|
|
GST_VIDEO_FORMAT_UNKNOWN
|
|
|
|
&& GST_VIDEO_INFO_FORMAT (&download->info) != GST_VIDEO_FORMAT_ENCODED,
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
|
|
|
|
g_return_val_if_fail (data[i] != NULL, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
download->in_texture = texture_id;
|
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
|
2014-04-02 06:43:52 +00:00
|
|
|
if (download->data[i] != data[i])
|
|
|
|
realloc = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (realloc) {
|
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
|
|
|
|
if (download->out_texture[i])
|
|
|
|
gst_memory_unref ((GstMemory *) download->out_texture[i]);
|
|
|
|
download->data[i] = data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_VIDEO_INFO_FORMAT (&download->info) == GST_VIDEO_FORMAT_YV12) {
|
|
|
|
/* YV12 same as I420 except planes 1+2 swapped */
|
|
|
|
temp_data = download->data[1];
|
|
|
|
download->data[1] = download->data[2];
|
|
|
|
download->data[2] = temp_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_gl_memory_setup_wrapped (download->context, &download->info,
|
|
|
|
download->data, download->out_texture);
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_thread_add (download->context,
|
|
|
|
(GstGLContextThreadFunc) _do_download, download);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-07-19 06:48:03 +00:00
|
|
|
return download->priv->result;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-09-15 04:23:43 +00:00
|
|
|
_init_download (GstGLContext * context, GstGLDownload * download)
|
2012-09-16 11:23:09 +00:00
|
|
|
{
|
|
|
|
GstVideoFormat v_format;
|
2012-09-25 05:20:21 +00:00
|
|
|
guint out_width, out_height;
|
2014-04-01 02:30:51 +00:00
|
|
|
GstVideoInfo in_info;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
v_format = GST_VIDEO_INFO_FORMAT (&download->info);
|
2012-09-25 05:20:21 +00:00
|
|
|
out_width = GST_VIDEO_INFO_WIDTH (&download->info);
|
|
|
|
out_height = GST_VIDEO_INFO_HEIGHT (&download->info);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2013-07-15 15:08:42 +00:00
|
|
|
GST_TRACE ("initializing texture download for format %s",
|
|
|
|
gst_video_format_to_string (v_format));
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-04-01 02:30:51 +00:00
|
|
|
if (USING_GLES2 (context) && !USING_GLES3 (context)) {
|
|
|
|
/* GL_RGBA is the only officially supported texture format in GLES2 */
|
|
|
|
if (v_format == GST_VIDEO_FORMAT_RGB || v_format == GST_VIDEO_FORMAT_BGR) {
|
2014-04-02 06:43:52 +00:00
|
|
|
gst_gl_context_set_error (context, "Cannot download RGB textures in "
|
|
|
|
"GLES2");
|
2014-04-01 02:30:51 +00:00
|
|
|
download->priv->result = FALSE;
|
|
|
|
return;
|
2013-07-15 15:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
gst_video_info_set_format (&in_info, GST_VIDEO_FORMAT_RGBA, out_width,
|
|
|
|
out_height);
|
|
|
|
|
|
|
|
download->priv->result =
|
2014-04-12 08:16:29 +00:00
|
|
|
gst_gl_color_convert_init_format (download->convert, &in_info,
|
|
|
|
&download->info);
|
2014-04-02 06:43:52 +00:00
|
|
|
if (!download->priv->result)
|
|
|
|
return;
|
2013-07-19 06:48:03 +00:00
|
|
|
|
|
|
|
download->priv->result = TRUE;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-04-01 02:30:51 +00:00
|
|
|
_do_download (GstGLContext * context, GstGLDownload * download)
|
2012-12-06 07:40:26 +00:00
|
|
|
{
|
2012-09-25 05:20:21 +00:00
|
|
|
guint out_width, out_height;
|
2014-04-02 06:43:52 +00:00
|
|
|
GstMapInfo map_info;
|
|
|
|
GstGLMemory *in_tex[GST_VIDEO_MAX_PLANES] = { 0, };
|
|
|
|
gint i;
|
2013-01-16 04:21:44 +00:00
|
|
|
|
2012-09-26 07:18:53 +00:00
|
|
|
out_width = GST_VIDEO_INFO_WIDTH (&download->info);
|
|
|
|
out_height = GST_VIDEO_INFO_HEIGHT (&download->info);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-04-01 02:30:51 +00:00
|
|
|
GST_TRACE ("doing YUV download of texture:%u (%ux%u)",
|
|
|
|
download->in_texture, out_width, out_height);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
in_tex[0] = gst_gl_memory_wrapped_texture (context, download->in_texture,
|
|
|
|
GST_VIDEO_GL_TEXTURE_TYPE_RGBA, out_width, out_height, NULL, NULL);
|
|
|
|
|
2014-04-01 02:30:51 +00:00
|
|
|
download->priv->result =
|
|
|
|
gst_gl_color_convert_perform (download->convert, in_tex,
|
|
|
|
download->out_texture);
|
|
|
|
if (!download->priv->result)
|
|
|
|
return;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
|
|
|
|
gst_memory_map ((GstMemory *) download->out_texture[i], &map_info,
|
|
|
|
GST_MAP_READ);
|
|
|
|
gst_memory_unmap ((GstMemory *) download->out_texture[i], &map_info);
|
2012-12-06 07:40:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 06:43:52 +00:00
|
|
|
gst_memory_unref ((GstMemory *) in_tex[0]);
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|