From 1e024387153b34c2dd5c14688ceafeb3ba01c6e9 Mon Sep 17 00:00:00 2001 From: Matthew Waters Date: Thu, 14 Nov 2013 15:27:52 +1100 Subject: [PATCH] [855/906] upload: add support for GstVideoGLTextureUploadMeta --- gst-libs/gst/gl/gstglbufferpool.c | 3 + gst-libs/gst/gl/gstglfilter.c | 121 +++------ gst-libs/gst/gl/gstglmixer.c | 151 +++-------- gst-libs/gst/gl/gstglmixer.h | 1 - gst-libs/gst/gl/gstglupload.c | 401 +++++++++++++++++++++++++++--- gst-libs/gst/gl/gstglupload.h | 6 + 6 files changed, 441 insertions(+), 242 deletions(-) diff --git a/gst-libs/gst/gl/gstglbufferpool.c b/gst-libs/gst/gl/gstglbufferpool.c index 74b40b79bc..43f6c06d1c 100644 --- a/gst-libs/gst/gl/gstglbufferpool.c +++ b/gst-libs/gst/gl/gstglbufferpool.c @@ -179,6 +179,9 @@ gst_gl_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer, gst_buffer_add_video_meta (buf, 0, GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info)); + + gst_gl_upload_add_video_gl_texture_upload_meta (((GstGLMemory *) + gl_mem)->upload, buf); } *buffer = buf; diff --git a/gst-libs/gst/gl/gstglfilter.c b/gst-libs/gst/gl/gstglfilter.c index 4f25982ad9..f9cd9fd06f 100644 --- a/gst-libs/gst/gl/gstglfilter.c +++ b/gst-libs/gst/gl/gstglfilter.c @@ -31,17 +31,23 @@ GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); static GstStaticPadTemplate gst_gl_filter_src_pad_template = -GST_STATIC_PAD_TEMPLATE ("src", + GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_GL_DOWNLOAD_VIDEO_CAPS) + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_DOWNLOAD_FORMATS) "; " + GST_VIDEO_CAPS_MAKE_WITH_FEATURES + (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, + "RGBA")) ); static GstStaticPadTemplate gst_gl_filter_sink_pad_template = -GST_STATIC_PAD_TEMPLATE ("sink", + GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_GL_UPLOAD_VIDEO_CAPS) + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_UPLOAD_FORMATS) "; " + GST_VIDEO_CAPS_MAKE_WITH_FEATURES + (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, + "RGBA")) ); /* Properties */ @@ -893,6 +899,12 @@ gst_gl_filter_decide_allocation (GstBaseTransform * trans, GstQuery * query) out_width = GST_VIDEO_INFO_WIDTH (&filter->out_info); out_height = GST_VIDEO_INFO_HEIGHT (&filter->out_info); + if (!filter->upload) { + filter->upload = gst_gl_upload_new (filter->context); + gst_gl_upload_init_format (filter->upload, + GST_VIDEO_INFO_FORMAT (&filter->in_info), in_width, in_height, + out_width, out_height); + } //blocking call, generate a FBO if (!gst_gl_context_gen_fbo (filter->context, out_width, out_height, &filter->fbo, &filter->depthbuffer)) @@ -960,52 +972,29 @@ gst_gl_filter_filter_texture (GstGLFilter * filter, GstBuffer * inbuf, GstBuffer * outbuf) { GstGLFilterClass *filter_class; - GstVideoFrame in_frame; - GstVideoFrame out_frame; guint in_tex, out_tex; - gboolean ret, in_gl_wrapped, out_gl_wrapped; + GstVideoFrame out_frame; + gboolean ret, out_gl_mem; + GstVideoGLTextureUploadMeta *out_tex_upload_meta; filter_class = GST_GL_FILTER_GET_CLASS (filter); - if (!gst_video_frame_map (&in_frame, &filter->in_info, inbuf, - GST_MAP_READ | GST_MAP_GL)) { + if (!gst_gl_upload_perform_with_buffer (filter->upload, inbuf, &in_tex)) return FALSE; - } + if (!gst_video_frame_map (&out_frame, &filter->out_info, outbuf, GST_MAP_WRITE | GST_MAP_GL)) { - gst_video_frame_unmap (&in_frame); - return FALSE; + ret = FALSE; + goto inbuf_error; } - in_gl_wrapped = !gst_is_gl_memory (in_frame.map[0].memory); - out_gl_wrapped = !gst_is_gl_memory (out_frame.map[0].memory); + out_gl_mem = gst_is_gl_memory (out_frame.map[0].memory); + out_tex_upload_meta = gst_buffer_get_video_gl_texture_upload_meta (outbuf); - if (!in_gl_wrapped && !out_gl_wrapped) { /* both GL */ - in_tex = *(guint *) in_frame.data[0]; + if (out_gl_mem) { out_tex = *(guint *) out_frame.data[0]; - } else if (in_gl_wrapped && !out_gl_wrapped) { /* input: non-GL, output: GL */ - GST_INFO ("Input Buffer does not contain correct meta, " - "attempting to wrap for upload"); - - if (!filter->upload) { - filter->upload = gst_gl_upload_new (filter->context); - - if (!gst_gl_upload_init_format (filter->upload, - GST_VIDEO_FRAME_FORMAT (&in_frame), - GST_VIDEO_FRAME_WIDTH (&in_frame), - GST_VIDEO_FRAME_HEIGHT (&in_frame), - GST_VIDEO_FRAME_WIDTH (&out_frame), - GST_VIDEO_FRAME_HEIGHT (&out_frame))) { - GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, - ("%s", "Failed to init upload format"), (NULL)); - return FALSE; - } - } - - in_tex = filter->in_tex_id; - out_tex = *(guint *) out_frame.data[0]; - } else if (!in_gl_wrapped && out_gl_wrapped) { /* input: GL, output: non-GL */ - GST_INFO ("Output Buffer does not contain correct memory, " + } else { + GST_LOG ("Output Buffer does not contain correct memory, " "attempting to wrap for download"); if (!filter->download) { @@ -1017,52 +1006,11 @@ gst_gl_filter_filter_texture (GstGLFilter * filter, GstBuffer * inbuf, GST_VIDEO_FRAME_HEIGHT (&out_frame))) { GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, ("%s", "Failed to init download format"), (NULL)); - return FALSE; + ret = FALSE; + goto error; } } - - in_tex = *(guint *) in_frame.data[0]; out_tex = filter->out_tex_id; - } else { /* both non-GL */ - if (!filter->upload) { - filter->upload = gst_gl_upload_new (filter->context); - - if (!gst_gl_upload_init_format (filter->upload, - GST_VIDEO_FRAME_FORMAT (&in_frame), - GST_VIDEO_FRAME_WIDTH (&in_frame), - GST_VIDEO_FRAME_HEIGHT (&in_frame), - GST_VIDEO_FRAME_WIDTH (&out_frame), - GST_VIDEO_FRAME_HEIGHT (&out_frame))) { - GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, - ("%s", "Failed to init upload format"), (NULL)); - return FALSE; - } - } - - if (!filter->download) { - filter->download = gst_gl_download_new (filter->context); - - if (!gst_gl_download_init_format (filter->download, - GST_VIDEO_FRAME_FORMAT (&out_frame), - GST_VIDEO_FRAME_WIDTH (&out_frame), - GST_VIDEO_FRAME_HEIGHT (&out_frame))) { - GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, - ("%s", "Failed to init download format"), (NULL)); - return FALSE; - } - } - - out_tex = filter->out_tex_id; - in_tex = filter->in_tex_id; - } - - if (in_gl_wrapped) { - if (!gst_gl_upload_perform_with_data (filter->upload, in_tex, - in_frame.data)) { - GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, ("%s", - "Failed to upload video frame"), (NULL)); - return FALSE; - } } GST_DEBUG ("calling filter_texture with textures in:%i out:%i", in_tex, @@ -1071,17 +1019,20 @@ gst_gl_filter_filter_texture (GstGLFilter * filter, GstBuffer * inbuf, g_assert (filter_class->filter_texture); ret = filter_class->filter_texture (filter, in_tex, out_tex); - if (out_gl_wrapped) { + if (!out_gl_mem && !out_tex_upload_meta) { if (!gst_gl_download_perform_with_data (filter->download, out_tex, out_frame.data)) { GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, ("%s", "Failed to download video frame"), (NULL)); - return FALSE; + ret = FALSE; + goto error; } } - gst_video_frame_unmap (&in_frame); +error: gst_video_frame_unmap (&out_frame); +inbuf_error: + gst_gl_upload_release_buffer (filter->upload); return ret; } diff --git a/gst-libs/gst/gl/gstglmixer.c b/gst-libs/gst/gl/gstglmixer.c index 31db30dc66..97c282535f 100644 --- a/gst-libs/gst/gl/gstglmixer.c +++ b/gst-libs/gst/gl/gstglmixer.c @@ -485,20 +485,6 @@ gst_gl_mixer_sink_query (GstCollectPads * pads, GstCollectData * data, &mix->display); break; } - case GST_QUERY_CUSTOM: - { - /* mix is a sink in terms of gl chain, so we are sharing the gldisplay that - * comes from src pad with every display of the sink pads */ - GstStructure *structure = gst_query_writable_structure (query); - - if (gst_structure_has_name (structure, "gstglcontext")) { - gst_structure_set (structure, "gstglcontext", G_TYPE_POINTER, - mix->context, NULL); - } else { - ret = gst_collect_pads_query_default (pads, data, query, FALSE); - } - break; - } default: ret = gst_collect_pads_query_default (pads, data, query, FALSE); break; @@ -527,22 +513,25 @@ enum static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_GL_UPLOAD_VIDEO_CAPS) + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_DOWNLOAD_FORMATS) "; " + GST_VIDEO_CAPS_MAKE_WITH_FEATURES + (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, + "RGBA")) ); static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d", GST_PAD_SINK, GST_PAD_REQUEST, - GST_STATIC_CAPS (GST_GL_DOWNLOAD_VIDEO_CAPS) + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_UPLOAD_FORMATS) "; " + GST_VIDEO_CAPS_MAKE_WITH_FEATURES + (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, + "RGBA")) ); static void gst_gl_mixer_finalize (GObject * object); static gboolean gst_gl_mixer_src_query (GstPad * pad, GstObject * object, GstQuery * query); -static gboolean gst_gl_mixer_src_activate_mode (GstPad * pad, - GstObject * parent, GstPadMode mode, gboolean active); -static gboolean gst_gl_mixer_activate (GstGLMixer * mix, gboolean activate); static GstFlowReturn gst_gl_mixer_sink_clip (GstCollectPads * pads, GstCollectData * data, GstBuffer * buf, GstBuffer ** outbuf, GstGLMixer * mix); @@ -714,8 +703,6 @@ gst_gl_mixer_init (GstGLMixer * mix) GST_DEBUG_FUNCPTR (gst_gl_mixer_src_query)); gst_pad_set_event_function (GST_PAD (mix->srcpad), GST_DEBUG_FUNCPTR (gst_gl_mixer_src_event)); - gst_pad_set_activatemode_function (GST_PAD (mix->srcpad), - GST_DEBUG_FUNCPTR (gst_gl_mixer_src_activate_mode)); gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad); mix->collect = gst_collect_pads_new (); @@ -1043,42 +1030,6 @@ gst_gl_mixer_src_query (GstPad * pad, GstObject * parent, GstQuery * query) return res; } -static gboolean -gst_gl_mixer_src_activate_mode (GstPad * pad, GstObject * parent, - GstPadMode mode, gboolean active) -{ - gboolean result = FALSE; - GstGLMixer *mix; - - mix = GST_GL_MIXER (parent); - - switch (mode) { - case GST_PAD_MODE_PULL: - case GST_PAD_MODE_PUSH: - { - result = gst_gl_mixer_activate (mix, active); - break; - } - default: - result = TRUE; - break; - } - - return result; -} - -static gboolean -gst_gl_mixer_activate (GstGLMixer * mix, gboolean activate) -{ - if (activate) { - if (!gst_gl_ensure_display (mix, &mix->display)) { - return FALSE; - } - } - - return TRUE; -} - static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query) { @@ -1624,8 +1575,10 @@ gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf) gint64 stream_time; GstSegment *seg; guint in_tex; - GstVideoFrame *in_frame; GstGLMixerFrameData *frame; + GstVideoFormat in_format; + guint in_width, in_height, out_width, out_height; + frame = g_ptr_array_index (mix->frames, array_index); frame->pad = pad; @@ -1642,58 +1595,32 @@ gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf) if (GST_CLOCK_TIME_IS_VALID (stream_time)) gst_object_sync_values (GST_OBJECT (pad), stream_time); - in_frame = g_ptr_array_index (mix->in_frames, array_index); + in_format = GST_VIDEO_INFO_FORMAT (&pad->in_info); + in_width = GST_VIDEO_INFO_WIDTH (&pad->in_info); + in_height = GST_VIDEO_INFO_HEIGHT (&pad->in_info); + out_width = GST_VIDEO_INFO_WIDTH (&mix->out_info); + out_height = GST_VIDEO_INFO_HEIGHT (&mix->out_info); - if (!gst_video_frame_map (in_frame, &pad->in_info, mixcol->buffer, - GST_MAP_READ | GST_MAP_GL)) { + if (!pad->upload) { + pad->upload = gst_gl_upload_new (mix->context); + + if (!gst_gl_upload_init_format (pad->upload, in_format, + in_width, in_height, out_width, out_height)) { + GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, + ("%s", "Failed to init upload format"), (NULL)); + res = FALSE; + goto out; + } + } + + if (!gst_gl_upload_perform_with_buffer (pad->upload, mixcol->buffer, + &in_tex)) { ++array_index; pad->mapped = FALSE; continue; } pad->mapped = TRUE; - if (gst_is_gl_memory (in_frame->map[0].memory)) { - in_tex = *(guint *) in_frame->data[0]; - } else { - GstVideoFormat in_format; - guint in_width, in_height, out_width, out_height; - - GST_DEBUG ("Input buffer:%p does not contain correct memory, " - "attempting to wrap for upload", mixcol->buffer); - - in_format = GST_VIDEO_INFO_FORMAT (&pad->in_info); - in_width = GST_VIDEO_INFO_WIDTH (&pad->in_info); - in_height = GST_VIDEO_INFO_HEIGHT (&pad->in_info); - out_width = GST_VIDEO_INFO_WIDTH (&mix->out_info); - out_height = GST_VIDEO_INFO_HEIGHT (&mix->out_info); - - if (!pad->upload) { - pad->upload = gst_gl_upload_new (mix->context); - - if (!gst_gl_upload_init_format (pad->upload, in_format, - in_width, in_height, in_width, in_height)) { - GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, - ("%s", "Failed to init upload format"), (NULL)); - res = FALSE; - goto out; - } - - if (!pad->in_tex_id) - gst_gl_context_gen_texture (mix->context, &pad->in_tex_id, - GST_VIDEO_FORMAT_RGBA, out_width, out_height); - } - - if (!gst_gl_upload_perform_with_data (pad->upload, pad->in_tex_id, - in_frame->data)) { - GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, - ("%s", "Failed to upload video frame"), (NULL)); - res = FALSE; - goto out; - } - - in_tex = pad->in_tex_id; - } - frame->texture = in_tex; } ++array_index; @@ -1716,10 +1643,9 @@ out: walk = mix->sinkpads; while (walk) { GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data); - GstVideoFrame *in_frame = g_ptr_array_index (mix->in_frames, i); - if (in_frame && pad->mapped) - gst_video_frame_unmap (in_frame); + if (pad->mapped) + gst_gl_upload_release_buffer (pad->upload); pad->mapped = FALSE; walk = g_slist_next (walk); @@ -2284,17 +2210,11 @@ gst_gl_mixer_change_state (GstElement * element, GstStateChange transition) guint i; mix->array_buffers = g_ptr_array_new_full (mix->numpads, NULL); - mix->in_frames = g_ptr_array_new_full (mix->numpads, NULL); mix->frames = g_ptr_array_new_full (mix->numpads, NULL); g_ptr_array_set_size (mix->array_buffers, mix->numpads); - g_ptr_array_set_size (mix->in_frames, mix->numpads); g_ptr_array_set_size (mix->frames, mix->numpads); - for (i = 0; i < mix->numpads; i++) { - mix->in_frames->pdata[i] = g_slice_new0 (GstVideoFrame); - } - for (i = 0; i < mix->numpads; i++) { mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData); } @@ -2312,15 +2232,10 @@ gst_gl_mixer_change_state (GstElement * element, GstStateChange transition) gst_collect_pads_stop (mix->collect); for (i = 0; i < mix->numpads; i++) { - g_slice_free1 (sizeof (GstVideoFrame), mix->in_frames->pdata[i]); - } - - for (i = 0; i < mix->numpads; i++) { - g_slice_free1 (sizeof (GstGLMixerFrameData), mix->in_frames->pdata[i]); + g_slice_free1 (sizeof (GstGLMixerFrameData), mix->frames->pdata[i]); } g_ptr_array_free (mix->array_buffers, TRUE); - g_ptr_array_free (mix->in_frames, TRUE); g_ptr_array_free (mix->frames, TRUE); if (mixer_class->reset) diff --git a/gst-libs/gst/gl/gstglmixer.h b/gst-libs/gst/gl/gstglmixer.h index 7a1435749d..41dd48202e 100644 --- a/gst-libs/gst/gl/gstglmixer.h +++ b/gst-libs/gst/gl/gstglmixer.h @@ -74,7 +74,6 @@ struct _GstGLMixer gint next_sinkpad; GPtrArray *array_buffers; - GPtrArray *in_frames; GPtrArray *frames; GstVideoInfo out_info; diff --git a/gst-libs/gst/gl/gstglupload.c b/gst-libs/gst/gl/gstglupload.c index 8d8f7cf858..cb64aa4b30 100644 --- a/gst-libs/gst/gl/gstglupload.c +++ b/gst-libs/gst/gl/gstglupload.c @@ -50,6 +50,7 @@ static void _init_upload (GstGLContext * context, GstGLUpload * upload); static gboolean _init_upload_fbo (GstGLContext * context, GstGLUpload * upload); static gboolean _gst_gl_upload_perform_with_data_unlocked (GstGLUpload * upload, GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES]); +static void _do_upload_with_meta (GstGLContext * context, GstGLUpload * upload); #if GST_GL_HAVE_OPENGL static gboolean _do_upload_draw_opengl (GstGLContext * context, @@ -324,6 +325,12 @@ struct _GstGLUploadPrivate const gchar *vert_shader; gboolean (*draw) (GstGLContext * context, GstGLUpload * download); + + GstBuffer *buffer; + GstVideoFrame frame; + GstVideoGLTextureUploadMeta *meta; + guint tex_id; + gboolean mapped; }; GST_DEBUG_CATEGORY_STATIC (gst_gl_upload_debug); @@ -431,6 +438,10 @@ gst_gl_upload_finalize (GObject * object) gst_gl_context_del_texture (upload->context, &upload->out_texture); upload->out_texture = 0; } + if (upload->priv->tex_id) { + gst_gl_context_del_texture (upload->context, &upload->priv->tex_id); + upload->priv->tex_id = 0; + } if (upload->fbo || upload->depth_buffer) { gst_gl_context_del_fbo (upload->context, upload->fbo, upload->depth_buffer); upload->fbo = 0; @@ -451,6 +462,38 @@ gst_gl_upload_finalize (GObject * object) G_OBJECT_CLASS (gst_gl_upload_parent_class)->finalize (object); } +static gboolean +_gst_gl_upload_init_format_unlocked (GstGLUpload * upload, + GstVideoFormat v_format, guint in_width, guint in_height, guint out_width, + guint out_height) +{ + GstVideoInfo info; + + g_return_val_if_fail (upload != 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 (in_width > 0 && in_height > 0, FALSE); + g_return_val_if_fail (out_width > 0 && out_height > 0, FALSE); + + if (upload->initted) { + return FALSE; + } else { + upload->initted = TRUE; + } + + gst_video_info_set_format (&info, v_format, out_width, out_height); + + upload->info = info; + upload->in_width = in_width; + upload->in_height = in_height; + + gst_gl_context_thread_add (upload->context, + (GstGLContextThreadFunc) _init_upload, upload); + + return upload->priv->result; +} + + /** * gst_gl_upload_init_format: * @upload: a #GstGLUpload @@ -468,35 +511,138 @@ gboolean gst_gl_upload_init_format (GstGLUpload * upload, GstVideoFormat v_format, guint in_width, guint in_height, guint out_width, guint out_height) { - GstVideoInfo info; - - g_return_val_if_fail (upload != 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 (in_width > 0 && in_height > 0, FALSE); - g_return_val_if_fail (out_width > 0 && out_height > 0, FALSE); + gboolean ret; g_mutex_lock (&upload->lock); - if (upload->initted) { - g_mutex_unlock (&upload->lock); - return FALSE; - } else { - upload->initted = TRUE; - } - - gst_video_info_set_format (&info, v_format, out_width, out_height); - - upload->info = info; - upload->in_width = in_width; - upload->in_height = in_height; - - gst_gl_context_thread_add (upload->context, - (GstGLContextThreadFunc) _init_upload, upload); + ret = _gst_gl_upload_init_format_unlocked (upload, v_format, in_width, + in_height, out_width, out_height); g_mutex_unlock (&upload->lock); - return upload->priv->result; + return ret; +} + +/** + * gst_gl_upload_perform_with_buffer: + * @upload: a #GstGLUpload + * @buffer: a #GstBuffer + * @tex_id: resulting texture + * + * Uploads @buffer to the texture given by @texture_id. @texture_id is valid + * until gst_gl_upload_release() is called. + * + * 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; + + g_return_val_if_fail (upload != NULL, FALSE); + 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)) { + GST_LOG_OBJECT (upload, "Attempting upload with GstGLMemory"); + /* Assuming only one memory */ + if (!gst_video_frame_map (&upload->priv->frame, &upload->info, buffer, + GST_MAP_READ | GST_MAP_GL)) { + GST_ERROR_OBJECT (upload, "Failed to map memory"); + return FALSE; + } + + *tex_id = *(guint *) upload->priv->frame.data[0]; + + upload->priv->mapped = TRUE; + return TRUE; + } + + if (!upload->priv->tex_id) + gst_gl_context_gen_texture (upload->context, &upload->priv->tex_id, + GST_VIDEO_INFO_FORMAT (&upload->info), + GST_VIDEO_INFO_WIDTH (&upload->info), + GST_VIDEO_INFO_HEIGHT (&upload->info)); + + /* GstVideoGLTextureUploadMeta */ + gl_tex_upload_meta = gst_buffer_get_video_gl_texture_upload_meta (buffer); + if (gl_tex_upload_meta) { + guint texture_ids[] = { 0, 0, 0, 0 }; + GST_LOG_OBJECT (upload, "Attempting upload with " + "GstVideoGLTextureUploadMeta"); + 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->info), + GST_VIDEO_INFO_HEIGHT (&upload->info)); + + 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 */ + if (!gst_video_frame_map (&upload->priv->frame, &upload->info, buffer, + GST_MAP_READ)) { + GST_ERROR_OBJECT (upload, "Failed to map memory"); + return FALSE; + } + + if (!gst_gl_upload_perform_with_data (upload, upload->priv->tex_id, + upload->priv->frame.data)) { + return FALSE; + } + + upload->priv->mapped = TRUE; + *tex_id = upload->priv->tex_id; + return TRUE; +} + +void +gst_gl_upload_release_buffer (GstGLUpload * upload) +{ + g_return_if_fail (upload != NULL); + + if (upload->priv->mapped) + gst_video_frame_unmap (&upload->priv->frame); +} + +static gboolean +_upload_memory_unlocked (GstGLUpload * upload, GstGLMemory * gl_mem, + guint tex_id) +{ + gpointer data[GST_VIDEO_MAX_PLANES]; + guint i; + gboolean ret; + + upload->in_width = GST_VIDEO_INFO_WIDTH (&upload->info); + upload->in_height = GST_VIDEO_INFO_HEIGHT (&upload->info); + + for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&upload->info); i++) { + data[i] = (guint8 *) gl_mem->data + + GST_VIDEO_INFO_PLANE_OFFSET (&upload->info, i); + } + + ret = _gst_gl_upload_perform_with_data_unlocked (upload, tex_id, data); + + if (ret && tex_id == gl_mem->tex_id) + GST_GL_MEMORY_FLAG_UNSET (gl_mem, GST_GL_MEMORY_FLAG_NEED_UPLOAD); + + return ret; } /** @@ -511,8 +657,6 @@ gst_gl_upload_init_format (GstGLUpload * upload, GstVideoFormat v_format, gboolean gst_gl_upload_perform_with_memory (GstGLUpload * upload, GstGLMemory * gl_mem) { - gpointer data[GST_VIDEO_MAX_PLANES]; - guint i; gboolean ret; g_return_val_if_fail (upload != NULL, FALSE); @@ -525,25 +669,204 @@ gst_gl_upload_perform_with_memory (GstGLUpload * upload, GstGLMemory * gl_mem) g_mutex_lock (&upload->lock); - upload->in_width = GST_VIDEO_INFO_WIDTH (&upload->info); - upload->in_height = GST_VIDEO_INFO_HEIGHT (&upload->info); - - for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&upload->info); i++) { - data[i] = (guint8 *) gl_mem->data + - GST_VIDEO_INFO_PLANE_OFFSET (&upload->info, i); - } - - ret = - _gst_gl_upload_perform_with_data_unlocked (upload, gl_mem->tex_id, data); - - if (ret) - GST_GL_MEMORY_FLAG_UNSET (gl_mem, GST_GL_MEMORY_FLAG_NEED_UPLOAD); + ret = _upload_memory_unlocked (upload, gl_mem, gl_mem->tex_id); g_mutex_unlock (&upload->lock); return ret; } +/* + * 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) +{ + GstVideoMeta *v_meta; + GstVideoInfo info; + GstVideoFrame frame; + GstMemory *mem; + gboolean ret; + + g_return_val_if_fail (upload != NULL, FALSE); + g_return_val_if_fail (meta != NULL, FALSE); + + v_meta = gst_buffer_get_video_meta (upload->priv->buffer); + + if (!upload->initted) { + GstVideoFormat v_format; + guint width, height; + + if (v_meta == NULL) + return FALSE; + + v_format = v_meta->format; + width = v_meta->width; + height = v_meta->height; + + if (!_gst_gl_upload_init_format_unlocked (upload, v_format, width, height, + width, height)) + return FALSE; + } + + /* GstGLMemory */ + mem = gst_buffer_peek_memory (upload->priv->buffer, 0); + + if (gst_is_gl_memory (mem)) { + if (GST_GL_MEMORY_FLAG_IS_SET (mem, GST_GL_MEMORY_FLAG_NEED_UPLOAD)) { + ret = _upload_memory_unlocked (upload, (GstGLMemory *) mem, + upload->out_texture); + } else { + ret = gst_gl_memory_copy_into_texture ((GstGLMemory *) mem, + upload->out_texture); + } + + if (ret) + return TRUE; + } + + gst_video_info_set_format (&info, v_meta->format, v_meta->width, + v_meta->height); + + if (!gst_video_frame_map (&frame, &info, upload->priv->buffer, GST_MAP_READ)) { + GST_ERROR ("failed to map video frame"); + return FALSE; + } + + ret = _gst_gl_upload_perform_with_data_unlocked (upload, upload->out_texture, + frame.data); + + gst_video_frame_unmap (&frame); + + if (ret) + return TRUE; + + 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; + upload->priv->tex_id = texture_id[0]; + + 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; + + g_mutex_unlock (&upload->lock); + + return ret; +} + +gboolean +_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); + + upload->out_texture = texture_id[0]; + + 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) +{ + GstVideoGLTextureType texture_types[] = + { GST_VIDEO_GL_TEXTURE_TYPE_RGBA, 0, 0, 0 }; + + g_return_val_if_fail (upload != NULL, FALSE); + g_return_val_if_fail (buffer != NULL, FALSE); + g_return_val_if_fail (gst_buffer_n_memory (buffer) == 1, FALSE); + + upload->priv->buffer = buffer; + + 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; +} + /** * gst_gl_upload_perform_with_data: * @upload: a #GstGLUpload @@ -590,6 +913,8 @@ _gst_gl_upload_perform_with_data_unlocked (GstGLUpload * upload, upload->data[i] = data[i]; } + GST_LOG ("Uploading data into texture %u", texture_id); + gst_gl_context_thread_add (upload->context, (GstGLContextThreadFunc) _do_upload, upload); diff --git a/gst-libs/gst/gl/gstglupload.h b/gst-libs/gst/gl/gstglupload.h index 4b5660cf35..6ab84441e1 100644 --- a/gst-libs/gst/gl/gstglupload.h +++ b/gst-libs/gst/gl/gstglupload.h @@ -109,10 +109,16 @@ gboolean gst_gl_upload_init_format (GstGLUpload * upload, GstVideoFormat guint in_width, guint in_height, guint out_width, guint out_height); +gboolean gst_gl_upload_add_video_gl_texture_upload_meta (GstGLUpload * upload, GstBuffer * buffer); + +gboolean gst_gl_upload_perform_with_buffer (GstGLUpload * upload, GstBuffer * buffer, guint * tex_id); +void gst_gl_upload_release_buffer (GstGLUpload * upload); gboolean gst_gl_upload_perform_with_memory (GstGLUpload * upload, GstGLMemory * gl_mem); gboolean gst_gl_upload_perform_with_data (GstGLUpload * upload, GLuint texture_id, gpointer data[GST_VIDEO_MAX_PLANES]); +gboolean gst_gl_upload_perform_with_gl_texture_upload_meta (GstGLUpload *upload, GstVideoGLTextureUploadMeta *meta, guint texture_id[4]); + G_END_DECLS #endif /* __GST_GL_UPLOAD_H__ */