gstreamer/gst-libs/gst/gl/gstgldownload.c

360 lines
10 KiB
C
Raw Normal View History

/*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "gl.h"
#include "gstgldownload.h"
/**
* 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.
*
* A #GstGLDownload can be created with gst_gl_download_new()
*/
#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 gboolean _init_download (GstGLDownload * download);
static gboolean _gst_gl_download_perform_with_data_unlocked (GstGLDownload *
download, GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES]);
static void gst_gl_download_reset (GstGLDownload * download);
/* *INDENT-ON* */
struct _GstGLDownloadPrivate
{
const gchar *YUY2_UYVY;
const gchar *I420_YV12;
const gchar *AYUV;
const gchar *ARGB;
const gchar *vert_shader;
gboolean result;
GstGLMemory *in_tex[GST_VIDEO_MAX_PLANES];
GstGLMemory *out_tex[GST_VIDEO_MAX_PLANES];
};
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, GST_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)
{
g_type_class_add_private (klass, sizeof (GstGLDownloadPrivate));
G_OBJECT_CLASS (klass)->finalize = gst_gl_download_finalize;
}
static void
gst_gl_download_init (GstGLDownload * download)
{
download->priv = GST_GL_DOWNLOAD_GET_PRIVATE (download);
g_mutex_init (&download->lock);
gst_video_info_init (&download->info);
}
/**
* gst_gl_download_new:
* @context: a #GstGLContext
*
* Returns: a new #GstGLDownload object
*/
GstGLDownload *
gst_gl_download_new (GstGLContext * context)
{
GstGLDownload *download;
download = g_object_new (GST_TYPE_GL_DOWNLOAD, NULL);
download->context = gst_object_ref (context);
download->convert = gst_gl_color_convert_new (context);
return download;
}
static void
gst_gl_download_finalize (GObject * object)
{
GstGLDownload *download;
download = GST_GL_DOWNLOAD (object);
gst_gl_download_reset (download);
if (download->convert) {
gst_object_unref (download->convert);
download->convert = NULL;
}
if (download->context) {
gst_object_unref (download->context);
download->context = NULL;
}
g_mutex_clear (&download->lock);
G_OBJECT_CLASS (gst_gl_download_parent_class)->finalize (object);
}
static void
gst_gl_download_reset (GstGLDownload * download)
{
guint i;
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
if (download->priv->out_tex[i]) {
gst_memory_unref ((GstMemory *) download->priv->out_tex[i]);
download->priv->out_tex[i] = NULL;
}
}
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
if (download->priv->in_tex[i]) {
gst_memory_unref ((GstMemory *) download->priv->in_tex[i]);
download->priv->in_tex[i] = NULL;
}
}
}
/**
* gst_gl_download_set_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.
*/
void
gst_gl_download_set_format (GstGLDownload * download, GstVideoInfo * out_info)
{
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);
g_mutex_lock (&download->lock);
if (gst_video_info_is_equal (&download->info, out_info)) {
g_mutex_unlock (&download->lock);
return;
}
gst_gl_download_reset (download);
download->initted = FALSE;
download->info = *out_info;
g_mutex_unlock (&download->lock);
}
/**
* 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_set_format()
*
* Returns: whether the download was successful
*/
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);
ret =
_gst_gl_download_perform_with_data_unlocked (download, texture_id, data);
g_mutex_unlock (&download->lock);
return ret;
}
static gboolean
_gst_gl_download_perform_with_data_unlocked (GstGLDownload * download,
GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES])
{
gpointer temp_data;
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);
}
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),
GST_VIDEO_INFO_HEIGHT (&download->info), NULL, NULL);
download->priv->in_tex[0]->tex_id = texture_id;
if (!download->priv->out_tex[0]) {
if (GST_VIDEO_INFO_FORMAT (&download->info) == GST_VIDEO_FORMAT_YUY2
|| GST_VIDEO_INFO_FORMAT (&download->info) == GST_VIDEO_FORMAT_UYVY) {
download->priv->out_tex[0] = gst_gl_memory_wrapped (download->context,
GST_VIDEO_GL_TEXTURE_TYPE_RGBA,
GST_VIDEO_INFO_COMP_WIDTH (&download->info, 1),
GST_VIDEO_INFO_HEIGHT (&download->info),
GST_VIDEO_INFO_PLANE_STRIDE (&download->info, 0), data[0], NULL,
NULL);
} else {
gst_gl_memory_setup_wrapped (download->context, &download->info,
data, download->priv->out_tex);
}
}
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
download->priv->out_tex[i]->data = 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->priv->out_tex[1]->data;
download->priv->out_tex[1]->data = download->priv->out_tex[2]->data;
download->priv->out_tex[2]->data = temp_data;
}
gst_gl_context_thread_add (download->context,
(GstGLContextThreadFunc) _do_download, download);
return download->priv->result;
}
static gboolean
_init_download (GstGLDownload * download)
{
GstVideoFormat v_format;
guint out_width, out_height;
GstVideoInfo in_info;
v_format = GST_VIDEO_INFO_FORMAT (&download->info);
out_width = GST_VIDEO_INFO_WIDTH (&download->info);
out_height = GST_VIDEO_INFO_HEIGHT (&download->info);
if (download->initted)
return TRUE;
GST_TRACE ("initializing texture download for format %s",
gst_video_format_to_string (v_format));
if (USING_GLES2 (download->context) && !USING_GLES3 (download->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) {
gst_gl_context_set_error (download->context, "Cannot download RGB "
"textures in GLES2");
return FALSE;
}
}
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,
&download->info);
return TRUE;
}
static void
_do_download (GstGLContext * context, GstGLDownload * download)
{
guint out_width, out_height;
GstBuffer *inbuf, *outbuf;
GstMapInfo map_info;
gint i;
2013-01-16 04:21:44 +00:00
out_width = GST_VIDEO_INFO_WIDTH (&download->info);
out_height = GST_VIDEO_INFO_HEIGHT (&download->info);
if (!download->initted) {
if (!(download->priv->result = _init_download (download)))
return;
}
GST_TRACE ("doing download of texture:%u (%ux%u)",
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]));
outbuf = gst_buffer_new ();
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
gst_buffer_append_memory (outbuf,
gst_memory_ref ((GstMemory *) download->priv->out_tex[0]));
}
download->priv->result =
gst_gl_color_convert_perform (download->convert, inbuf, outbuf);
if (!download->priv->result)
return;
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&download->info); i++) {
gst_memory_map ((GstMemory *) download->priv->out_tex[i], &map_info,
GST_MAP_READ);
gst_memory_unmap ((GstMemory *) download->priv->out_tex[i], &map_info);
}
gst_buffer_unref (inbuf);
gst_buffer_unref (outbuf);
}