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
|
|
|
*/
|
|
|
|
|
2014-05-30 00:27:14 +00:00
|
|
|
#define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
|
|
|
|
#define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
|
|
|
|
#define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
|
|
|
|
#define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
|
|
|
|
#define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
|
2013-09-15 04:23:43 +00:00
|
|
|
|
2014-05-29 05:50:56 +00:00
|
|
|
static gboolean _do_download (GstGLDownload * download, guint texture_id,
|
|
|
|
gpointer data[GST_VIDEO_MAX_PLANES]);
|
2014-05-13 04:13:57 +00:00
|
|
|
static gboolean _init_download (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]);
|
2014-05-13 04:13:57 +00:00
|
|
|
static void gst_gl_download_reset (GstGLDownload * download);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
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;
|
|
|
|
|
2014-05-27 08:20:29 +00:00
|
|
|
GstGLMemory *in_tex[GST_VIDEO_MAX_PLANES];
|
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");
|
|
|
|
|
2014-05-08 05:30:49 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLDownload, gst_gl_download, GST_TYPE_OBJECT,
|
2012-09-16 11:23:09 +00:00
|
|
|
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
|
|
|
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;
|
|
|
|
|
|
|
|
download = GST_GL_DOWNLOAD (object);
|
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
gst_gl_download_reset (download);
|
2013-09-15 04:23:43 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
static void
|
|
|
|
gst_gl_download_reset (GstGLDownload * download)
|
2012-09-18 11:39:21 +00:00
|
|
|
{
|
2014-05-13 04:13:57 +00:00
|
|
|
guint i;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
|
2014-05-27 08:20:29 +00:00
|
|
|
if (download->priv->in_tex[i]) {
|
|
|
|
gst_memory_unref ((GstMemory *) download->priv->in_tex[i]);
|
|
|
|
download->priv->in_tex[i] = NULL;
|
2014-05-13 04:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
2014-05-13 04:13:57 +00:00
|
|
|
* gst_gl_download_set_format:
|
2012-09-27 05:53:46 +00:00
|
|
|
* @download: a #GstGLDownload
|
2014-05-13 04:13:57 +00:00
|
|
|
* @v_format: a #GstVideoFormat
|
|
|
|
* @out_width: the width to download to
|
|
|
|
* @out_height: the height to download to
|
2012-09-27 05:53:46 +00:00
|
|
|
*
|
2014-05-13 04:13:57 +00:00
|
|
|
* Initializes @download with the information required for download.
|
2012-09-27 05:53:46 +00:00
|
|
|
*/
|
2014-05-13 04:13:57 +00:00
|
|
|
void
|
|
|
|
gst_gl_download_set_format (GstGLDownload * download, GstVideoInfo * out_info)
|
2012-09-18 11:39:21 +00:00
|
|
|
{
|
2014-05-13 04:13:57 +00:00
|
|
|
g_return_if_fail (download != NULL);
|
|
|
|
g_return_if_fail (GST_VIDEO_INFO_FORMAT (out_info) !=
|
|
|
|
GST_VIDEO_FORMAT_UNKNOWN);
|
|
|
|
g_return_if_fail (GST_VIDEO_INFO_FORMAT (out_info) !=
|
|
|
|
GST_VIDEO_FORMAT_ENCODED);
|
2012-09-18 11:39:21 +00:00
|
|
|
|
2014-05-29 06:22:52 +00:00
|
|
|
GST_OBJECT_LOCK (download);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
if (gst_video_info_is_equal (&download->info, out_info)) {
|
2014-05-29 06:22:52 +00:00
|
|
|
GST_OBJECT_UNLOCK (download);
|
2014-05-13 04:13:57 +00:00
|
|
|
return;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
gst_gl_download_reset (download);
|
|
|
|
download->initted = FALSE;
|
|
|
|
download->info = *out_info;
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-29 06:22:52 +00:00
|
|
|
GST_OBJECT_UNLOCK (download);
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
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
|
2014-05-13 04:13:57 +00:00
|
|
|
* the #GstVideoFormat passed to gst_gl_download_set_format()
|
2012-09-27 05:53:46 +00:00
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
|
2014-05-29 06:22:52 +00:00
|
|
|
GST_OBJECT_LOCK (download);
|
2013-07-18 09:37:32 +00:00
|
|
|
ret =
|
|
|
|
_gst_gl_download_perform_with_data_unlocked (download, texture_id, data);
|
2014-05-29 06:22:52 +00:00
|
|
|
GST_OBJECT_UNLOCK (download);
|
2012-09-16 11:23:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-27 08:20:29 +00:00
|
|
|
if (!download->priv->in_tex[0])
|
|
|
|
download->priv->in_tex[0] =
|
|
|
|
gst_gl_memory_wrapped_texture (download->context, texture_id,
|
|
|
|
GST_VIDEO_GL_TEXTURE_TYPE_RGBA, GST_VIDEO_INFO_WIDTH (&download->info),
|
2014-05-13 04:13:57 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&download->info), NULL, NULL);
|
2014-04-02 06:43:52 +00:00
|
|
|
|
2014-05-27 08:20:29 +00:00
|
|
|
download->priv->in_tex[0]->tex_id = texture_id;
|
2014-04-02 06:43:52 +00:00
|
|
|
|
2014-05-29 05:50:56 +00:00
|
|
|
return _do_download (download, texture_id, data);
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
static gboolean
|
|
|
|
_init_download (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
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
if (download->initted)
|
|
|
|
return TRUE;
|
|
|
|
|
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-05-13 04:13:57 +00:00
|
|
|
if (USING_GLES2 (download->context) && !USING_GLES3 (download->context)) {
|
2014-04-01 02:30:51 +00:00
|
|
|
/* 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-05-13 04:13:57 +00:00
|
|
|
gst_gl_context_set_error (download->context, "Cannot download RGB "
|
|
|
|
"textures in GLES2");
|
|
|
|
return FALSE;
|
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);
|
|
|
|
|
2014-05-09 08:07:03 +00:00
|
|
|
gst_gl_color_convert_set_format (download->convert, &in_info,
|
2014-04-12 08:16:29 +00:00
|
|
|
&download->info);
|
2013-07-19 06:48:03 +00:00
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
return TRUE;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 05:50:56 +00:00
|
|
|
static gboolean
|
|
|
|
_do_download (GstGLDownload * download, guint texture_id,
|
|
|
|
gpointer data[GST_VIDEO_MAX_PLANES])
|
2012-12-06 07:40:26 +00:00
|
|
|
{
|
2012-09-25 05:20:21 +00:00
|
|
|
guint out_width, out_height;
|
2014-05-27 08:20:29 +00:00
|
|
|
GstBuffer *inbuf, *outbuf;
|
2014-04-02 06:43:52 +00:00
|
|
|
GstMapInfo map_info;
|
2014-05-29 05:50:56 +00:00
|
|
|
gboolean ret = TRUE;
|
2014-04-02 06:43:52 +00:00
|
|
|
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-05-13 04:13:57 +00:00
|
|
|
if (!download->initted) {
|
2014-05-29 05:50:56 +00:00
|
|
|
if (!_init_download (download))
|
|
|
|
return FALSE;
|
2014-05-13 04:13:57 +00:00
|
|
|
}
|
2012-09-16 11:23:09 +00:00
|
|
|
|
2014-05-13 04:13:57 +00:00
|
|
|
GST_TRACE ("doing download of texture:%u (%ux%u)",
|
2014-05-27 08:20:29 +00:00
|
|
|
download->priv->in_tex[0]->tex_id, out_width, out_height);
|
|
|
|
|
|
|
|
inbuf = gst_buffer_new ();
|
|
|
|
gst_buffer_append_memory (inbuf,
|
|
|
|
gst_memory_ref ((GstMemory *) download->priv->in_tex[0]));
|
|
|
|
|
2014-05-29 05:50:56 +00:00
|
|
|
outbuf = gst_gl_color_convert_perform (download->convert, inbuf);
|
|
|
|
if (!outbuf)
|
|
|
|
return FALSE;
|
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++) {
|
2014-05-29 05:50:56 +00:00
|
|
|
GstMemory *out_mem = gst_buffer_peek_memory (outbuf, i);
|
|
|
|
gpointer temp_data = ((GstGLMemory *) out_mem)->data;
|
|
|
|
((GstGLMemory *) out_mem)->data = data[i];
|
|
|
|
|
|
|
|
if (!gst_memory_map (out_mem, &map_info, GST_MAP_READ)) {
|
|
|
|
GST_ERROR_OBJECT (download, "Failed to map memory");
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
gst_memory_unmap (out_mem, &map_info);
|
|
|
|
((GstGLMemory *) out_mem)->data = temp_data;
|
2012-12-06 07:40:26 +00:00
|
|
|
}
|
2014-05-27 08:20:29 +00:00
|
|
|
|
|
|
|
gst_buffer_unref (inbuf);
|
2014-05-29 05:50:56 +00:00
|
|
|
|
|
|
|
return ret;
|
2012-09-16 11:23:09 +00:00
|
|
|
}
|