From 87c262d1c31d0eecbb10ccb5923718ad157fbd77 Mon Sep 17 00:00:00 2001 From: David Schleef Date: Thu, 27 Dec 2007 04:15:26 +0000 Subject: [PATCH] [047/906] * sys/glsink/gltestsrc.c: * sys/glsink/gstglbuffer.c: * sys/glsink/gstglbuffer.h: * sys/glsink/gstgldownload.c: * sys/glsink/gstglfilter.c: * sys/glsink/gstglfilterexample.c: * sys/glsink/gstgltestsrc.c: * sys/glsink/gstglupload.c: Convert gldownload to BaseTransform. Make glfilterexample visually interesting. Add support for various formats to downloading. Fix a few places where we leak GL state to other elements (bad, but hard to prevent). --- gst-libs/gst/gl/gstglbuffer.c | 32 ++++- gst-libs/gst/gl/gstglbuffer.h | 13 +- gst/gl/gltestsrc.c | 3 + gst/gl/gstgldownload.c | 223 +++++++++++++++++++--------------- gst/gl/gstglfilter.c | 2 - gst/gl/gstglfilterexample.c | 76 ++++++++++-- gst/gl/gstgltestsrc.c | 2 - gst/gl/gstglupload.c | 2 - 8 files changed, 231 insertions(+), 122 deletions(-) diff --git a/gst-libs/gst/gl/gstglbuffer.c b/gst-libs/gst/gl/gstglbuffer.c index e3e9df6ba3..7a217a36a8 100644 --- a/gst-libs/gst/gl/gstglbuffer.c +++ b/gst-libs/gst/gl/gstglbuffer.c @@ -90,6 +90,9 @@ gst_gl_buffer_new (GstGLDisplay * display, GstGLBufferFormat format, buffer->display = g_object_ref (display); buffer->width = width; buffer->height = height; + /* this is not strictly true, but it's used for compatibility with + * queue and BaseTransform */ + GST_BUFFER_SIZE (buffer) = width * height * 4; gst_gl_display_lock (buffer->display); glGenTextures (1, &buffer->texture); @@ -129,6 +132,9 @@ gst_gl_buffer_new_from_data (GstGLDisplay * display, GstVideoFormat format, buffer->display = g_object_ref (display); buffer->width = width; buffer->height = height; + /* this is not strictly true, but it's used for compatibility with + * queue and BaseTransform */ + GST_BUFFER_SIZE (buffer) = width * height * 4; gst_gl_display_lock (buffer->display); glGenTextures (1, &buffer->texture); @@ -234,7 +240,7 @@ gst_gl_buffer_new_from_data (GstGLDisplay * display, GstVideoFormat format, void -gst_gl_buffer_download (GstGLBuffer * buffer, void *data) +gst_gl_buffer_download (GstGLBuffer * buffer, GstVideoFormat format, void *data) { GLuint fbo; @@ -254,15 +260,33 @@ gst_gl_buffer_download (GstGLBuffer * buffer, void *data) g_assert (glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT); - /* needs a reset function */ + /* we need a reset function */ glMatrixMode (GL_COLOR); glLoadIdentity (); glPixelTransferf (GL_POST_COLOR_MATRIX_RED_BIAS, 0); glPixelTransferf (GL_POST_COLOR_MATRIX_GREEN_BIAS, 0); glPixelTransferf (GL_POST_COLOR_MATRIX_BLUE_BIAS, 0); - glReadPixels (0, 0, buffer->width, buffer->height, GL_RGBA, - GL_UNSIGNED_BYTE, data); + switch (format) { + case GST_VIDEO_FORMAT_RGBx: + glReadPixels (0, 0, buffer->width, buffer->height, GL_RGBA, + GL_UNSIGNED_BYTE, data); + break; + case GST_VIDEO_FORMAT_BGRx: + glReadPixels (0, 0, buffer->width, buffer->height, GL_BGRA, + GL_UNSIGNED_BYTE, data); + break; + case GST_VIDEO_FORMAT_xBGR: + glReadPixels (0, 0, buffer->width, buffer->height, GL_RGBA, + GL_UNSIGNED_INT_8_8_8_8, data); + break; + case GST_VIDEO_FORMAT_xRGB: + glReadPixels (0, 0, buffer->width, buffer->height, GL_BGRA, + GL_UNSIGNED_INT_8_8_8_8, data); + break; + default: + g_assert_not_reached (); + } glDeleteFramebuffersEXT (1, &fbo); diff --git a/gst-libs/gst/gl/gstglbuffer.h b/gst-libs/gst/gl/gstglbuffer.h index 1d29aebf36..e5a39441f4 100644 --- a/gst-libs/gst/gl/gstglbuffer.h +++ b/gst-libs/gst/gl/gstglbuffer.h @@ -44,7 +44,18 @@ GstGLBuffer * gst_gl_buffer_new (GstGLDisplay *display, GstGLBufferFormat format, int width, int height); GstGLBuffer * gst_gl_buffer_new_from_data (GstGLDisplay *display, GstVideoFormat format, int width, int height, void *data); -void gst_gl_buffer_download (GstGLBuffer *buffer, void *data); +void gst_gl_buffer_download (GstGLBuffer *buffer, GstVideoFormat format, + void *data); + + +#define GST_GL_VIDEO_CAPS \ + "video/x-raw-gl," \ + "format=(int)[0,10]," \ + "width=(int)[1,2048]," \ + "height=(int)[1,2048]," \ + "pixel-aspect-ratio=(fraction)1/1," \ + "framerate=(fraction)[0/1,100/1]" + #endif diff --git a/gst/gl/gltestsrc.c b/gst/gl/gltestsrc.c index c6e7961706..05750f73b6 100644 --- a/gst/gl/gltestsrc.c +++ b/gst/gl/gltestsrc.c @@ -91,6 +91,9 @@ gst_gl_test_src_smpte (GstGLTestSrc * v, GstGLBuffer * buffer, int w, int h) glClearColor (0.0, 0.0, 0.0, 1.0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDisable (GL_CULL_FACE); + glDisable (GL_TEXTURE_RECTANGLE_ARB); + glMatrixMode (GL_PROJECTION); glLoadIdentity (); diff --git a/gst/gl/gstgldownload.c b/gst/gl/gstgldownload.c index bc50f45179..b5a3f1b2dd 100644 --- a/gst/gl/gstgldownload.c +++ b/gst/gl/gstgldownload.c @@ -23,6 +23,7 @@ #endif #include +#include #include #include @@ -42,10 +43,7 @@ typedef void (*GstGLDownloadProcessFunc) (GstGLDownload *, guint8 *, guint); struct _GstGLDownload { - GstElement element; - - GstPad *srcpad; - GstPad *sinkpad; + GstBaseTransform base_transform; /* < private > */ @@ -57,7 +55,7 @@ struct _GstGLDownload struct _GstGLDownloadClass { - GstElementClass element_class; + GstBaseTransformClass base_transform_class; }; static const GstElementDetails element_details = GST_ELEMENT_DETAILS ("FIXME", @@ -65,13 +63,12 @@ static const GstElementDetails element_details = GST_ELEMENT_DETAILS ("FIXME", "FIXME example filter", "FIXME "); -#define GST_GL_VIDEO_CAPS "video/x-raw-gl" - static GstStaticPadTemplate gst_gl_download_src_pad_template = -GST_STATIC_PAD_TEMPLATE ("src", + GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx) + GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB ";" + GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR) ); static GstStaticPadTemplate gst_gl_download_sink_pad_template = @@ -89,19 +86,26 @@ enum #define DEBUG_INIT(bla) \ GST_DEBUG_CATEGORY_INIT (gst_gl_download_debug, "gldownload", 0, "gldownload element"); -GST_BOILERPLATE_FULL (GstGLDownload, gst_gl_download, GstElement, - GST_TYPE_ELEMENT, DEBUG_INIT); +GST_BOILERPLATE_FULL (GstGLDownload, gst_gl_download, GstBaseTransform, + GST_TYPE_BASE_TRANSFORM, DEBUG_INIT); static void gst_gl_download_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); static void gst_gl_download_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); -static GstFlowReturn gst_gl_download_chain (GstPad * pad, GstBuffer * buf); static void gst_gl_download_reset (GstGLDownload * download); -static GstStateChangeReturn -gst_gl_download_change_state (GstElement * element, GstStateChange transition); -static gboolean gst_gl_download_sink_setcaps (GstPad * pad, GstCaps * caps); +static gboolean gst_gl_download_set_caps (GstBaseTransform * bt, + GstCaps * incaps, GstCaps * outcaps); +static GstCaps *gst_gl_download_transform_caps (GstBaseTransform * bt, + GstPadDirection direction, GstCaps * caps); +static gboolean gst_gl_download_start (GstBaseTransform * bt); +static gboolean gst_gl_download_stop (GstBaseTransform * bt); +static GstFlowReturn gst_gl_download_transform (GstBaseTransform * trans, + GstBuffer * inbuf, GstBuffer * outbuf); +static gboolean +gst_gl_download_get_unit_size (GstBaseTransform * trans, GstCaps * caps, + guint * size); static void @@ -126,22 +130,19 @@ gst_gl_download_class_init (GstGLDownloadClass * klass) gobject_class->set_property = gst_gl_download_set_property; gobject_class->get_property = gst_gl_download_get_property; - GST_ELEMENT_CLASS (klass)->change_state = gst_gl_download_change_state; + GST_BASE_TRANSFORM_CLASS (klass)->transform_caps = + gst_gl_download_transform_caps; + GST_BASE_TRANSFORM_CLASS (klass)->transform = gst_gl_download_transform; + GST_BASE_TRANSFORM_CLASS (klass)->start = gst_gl_download_start; + GST_BASE_TRANSFORM_CLASS (klass)->stop = gst_gl_download_stop; + GST_BASE_TRANSFORM_CLASS (klass)->set_caps = gst_gl_download_set_caps; + GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size = + gst_gl_download_get_unit_size; } static void gst_gl_download_init (GstGLDownload * download, GstGLDownloadClass * klass) { - gst_element_create_all_pads (GST_ELEMENT (download)); - - download->sinkpad = - gst_element_get_static_pad (GST_ELEMENT (download), "sink"); - download->srcpad = gst_element_get_static_pad (GST_ELEMENT (download), "src"); - - gst_pad_set_setcaps_function (download->sinkpad, - gst_gl_download_sink_setcaps); - gst_pad_set_chain_function (download->sinkpad, gst_gl_download_chain); - gst_gl_download_reset (download); } @@ -182,116 +183,138 @@ gst_gl_download_reset (GstGLDownload * download) } static gboolean -gst_gl_download_start (GstGLDownload * download) +gst_gl_download_start (GstBaseTransform * bt) { + GstGLDownload *download = GST_GL_DOWNLOAD (bt); + download->format = GST_VIDEO_FORMAT_RGBx; return TRUE; } static gboolean -gst_gl_download_stop (GstGLDownload * download) +gst_gl_download_stop (GstBaseTransform * bt) { + GstGLDownload *download = GST_GL_DOWNLOAD (bt); + gst_gl_download_reset (download); return TRUE; } -static gboolean -gst_gl_download_sink_setcaps (GstPad * pad, GstCaps * caps) +static GstCaps * +gst_gl_download_transform_caps (GstBaseTransform * bt, + GstPadDirection direction, GstCaps * caps) { GstGLDownload *download; - gboolean ret; GstStructure *structure; - GstCaps *srccaps; + GstCaps *newcaps; + GstStructure *newstruct; + const GValue *width_value; + const GValue *height_value; + const GValue *framerate_value; + const GValue *par_value; - download = GST_GL_DOWNLOAD (gst_pad_get_parent (pad)); - - GST_DEBUG ("called with %" GST_PTR_FORMAT, caps); + download = GST_GL_DOWNLOAD (bt); structure = gst_caps_get_structure (caps, 0); - ret = gst_structure_get_int (structure, "width", &download->width); - ret &= gst_structure_get_int (structure, "height", &download->height); + width_value = gst_structure_get_value (structure, "width"); + height_value = gst_structure_get_value (structure, "height"); + framerate_value = gst_structure_get_value (structure, "framerate"); + par_value = gst_structure_get_value (structure, "pixel-aspect-ratio"); + + if (direction == GST_PAD_SINK) { + newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL); + } else { + newcaps = gst_caps_new_simple ("video/x-raw-gl", NULL); + } + newstruct = gst_caps_get_structure (newcaps, 0); + gst_structure_set_value (newstruct, "width", width_value); + gst_structure_set_value (newstruct, "height", height_value); + gst_structure_set_value (newstruct, "framerate", framerate_value); + if (par_value) { + gst_structure_set_value (newstruct, "pixel-aspect-ratio", par_value); + } else { + gst_structure_set (newstruct, "pixel-aspect-ratio", GST_TYPE_FRACTION, + 1, 1, NULL); + } + + return newcaps; +} + +static gboolean +gst_gl_download_set_caps (GstBaseTransform * bt, GstCaps * incaps, + GstCaps * outcaps) +{ + GstGLDownload *download; + gboolean ret; + + download = GST_GL_DOWNLOAD (bt); + + GST_DEBUG ("called with %" GST_PTR_FORMAT, incaps); + + ret = gst_video_format_parse_caps (outcaps, &download->format, + &download->width, &download->height); + if (!ret) { GST_DEBUG ("bad caps"); return FALSE; } - srccaps = gst_video_format_new_caps (download->format, - download->width, download->height, 30, 1, 1, 1); - GST_DEBUG ("srccaps %" GST_PTR_FORMAT, srccaps); - ret = gst_pad_set_caps (download->srcpad, srccaps); - gst_caps_unref (srccaps); - return ret; } +static gboolean +gst_gl_download_get_unit_size (GstBaseTransform * trans, GstCaps * caps, + guint * size) +{ + gboolean ret; + GstStructure *structure; + + structure = gst_caps_get_structure (caps, 0); + if (gst_structure_has_name (structure, "video/x-raw-gl")) { + int width; + int height; + + ret = gst_structure_get_int (structure, "width", &width); + ret &= gst_structure_get_int (structure, "height", &height); + + /* FIXME */ + *size = width * height * 4; + } else { + int width; + int height; + + ret = gst_structure_get_int (structure, "width", &width); + ret &= gst_structure_get_int (structure, "height", &height); + + /* FIXME */ + *size = width * height * 4; + } + + + return TRUE; +} + static GstFlowReturn -gst_gl_download_chain (GstPad * pad, GstBuffer * buf) +gst_gl_download_transform (GstBaseTransform * trans, GstBuffer * inbuf, + GstBuffer * outbuf) { GstGLDownload *download; - GstGLBuffer *inbuf = GST_GL_BUFFER (buf); - GstBuffer *outbuf; + GstGLBuffer *gl_inbuf = GST_GL_BUFFER (inbuf); - download = GST_GL_DOWNLOAD (gst_pad_get_parent (pad)); + download = GST_GL_DOWNLOAD (trans); - outbuf = - gst_buffer_new_and_alloc (gst_video_format_get_size (download->format, - inbuf->width, inbuf->height)); - - gst_buffer_copy_metadata (GST_BUFFER (outbuf), buf, - GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS); - gst_buffer_set_caps (GST_BUFFER (outbuf), GST_PAD_CAPS (download->srcpad)); + if (download->display == NULL) { + download->display = g_object_ref (gl_inbuf->display); + } else { + g_assert (download->display == gl_inbuf->display); + } GST_DEBUG ("downloading %p size %d", GST_BUFFER_DATA (outbuf), GST_BUFFER_SIZE (outbuf)); - gst_gl_buffer_download (inbuf, GST_BUFFER_DATA (outbuf)); + gst_gl_buffer_download (gl_inbuf, download->format, GST_BUFFER_DATA (outbuf)); - gst_pad_push (download->srcpad, GST_BUFFER (outbuf)); - - gst_buffer_unref (buf); - gst_object_unref (download); return GST_FLOW_OK; } - -static GstStateChangeReturn -gst_gl_download_change_state (GstElement * element, GstStateChange transition) -{ - GstGLDownload *download; - GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; - - GST_DEBUG ("change state"); - - download = GST_GL_DOWNLOAD (element); - - switch (transition) { - case GST_STATE_CHANGE_NULL_TO_READY: - break; - case GST_STATE_CHANGE_READY_TO_PAUSED: - gst_gl_download_start (download); - break; - case GST_STATE_CHANGE_PAUSED_TO_PLAYING: - break; - default: - break; - } - - ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); - if (ret == GST_STATE_CHANGE_FAILURE) - return ret; - - switch (transition) { - case GST_STATE_CHANGE_PLAYING_TO_PAUSED: - break; - case GST_STATE_CHANGE_PAUSED_TO_READY: - gst_gl_download_stop (download); - break; - case GST_STATE_CHANGE_READY_TO_NULL: - break; - default: - break; - } - - return ret; -} diff --git a/gst/gl/gstglfilter.c b/gst/gl/gstglfilter.c index 3efa396b7f..1ff05adf9d 100644 --- a/gst/gl/gstglfilter.c +++ b/gst/gl/gstglfilter.c @@ -32,8 +32,6 @@ GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); -#define GST_GL_VIDEO_CAPS "video/x-raw-gl" - static GstStaticPadTemplate gst_gl_filter_src_pad_template = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, diff --git a/gst/gl/gstglfilterexample.c b/gst/gl/gstglfilterexample.c index 6a513b33d6..aa709abcfa 100644 --- a/gst/gl/gstglfilterexample.c +++ b/gst/gl/gstglfilterexample.c @@ -190,6 +190,8 @@ gst_gl_filter_example_transform (GstGLFilter * filter, GstGLBuffer * outbuf, GstGLBuffer * inbuf) { //GstGLFilterExample *example = GST_GL_FILTER_EXAMPLE(filter); + int i, j; + double *vertex_x, *vertex_y; glDisable (GL_CULL_FACE); glEnableClientState (GL_TEXTURE_COORD_ARRAY); @@ -203,6 +205,8 @@ gst_gl_filter_example_transform (GstGLFilter * filter, GstGLBuffer * outbuf, glColor4f (1, 0, 1, 1); #define GAIN 0.5 + /* just for fun. swap red and blue components. Doesn't work on my + * driver. */ { const double matrix[16] = { 0, 0, 1.0, 0, @@ -218,20 +222,70 @@ gst_gl_filter_example_transform (GstGLFilter * filter, GstGLBuffer * outbuf, glPixelTransferf (GL_POST_COLOR_MATRIX_BLUE_BIAS, (1 - GAIN) / 2); } - glBegin (GL_QUADS); - glNormal3f (0, 0, -1); - glTexCoord2f (inbuf->width, 0); - glVertex3f (0.9, -0.9, 0); - glTexCoord2f (0, 0); - glVertex3f (-1.0, -1.0, 0); - glTexCoord2f (0, inbuf->height); - glVertex3f (-1.0, 1.0, 0); - glTexCoord2f (inbuf->width, inbuf->height); - glVertex3f (1.0, 1.0, 0); - glEnd (); + /* load raster-scanning matrix */ + { + const double matrix[16] = { + 2.0, 0, 0, 0, + 0, 2.0, 0, 0, + 0, 0, 1, 0, + -1, -1, 0, 1 + }; + glMatrixMode (GL_MODELVIEW); + glLoadMatrixd (matrix); + } + /* load texture raster-scanning matrix */ + { + double matrix[16] = { + 1.0, 0, 0, 0, + 0, 1.0, 0, 0, + 0, 0, 1, 0, + -1, -1, 0, 1 + }; + matrix[0] = inbuf->width; + matrix[5] = inbuf->height; + glMatrixMode (GL_TEXTURE); + glLoadMatrixd (matrix); + } + +#define N 10 +#define SCALE (1.0/N) +#define NOISE() (0.1*SCALE*g_random_double_range(-1,1)) + vertex_x = malloc (sizeof (double) * (N + 1) * (N + 1)); + vertex_y = malloc (sizeof (double) * (N + 1) * (N + 1)); + for (j = 0; j < N + 1; j++) { + for (i = 0; i < N + 1; i++) { + vertex_x[j * (N + 1) + i] = i * SCALE + NOISE (); + vertex_y[j * (N + 1) + i] = j * SCALE + NOISE (); + } + } + for (j = 0; j < N; j++) { + for (i = 0; i < N; i++) { + glBegin (GL_QUADS); + glNormal3f (0, 0, -1); + glTexCoord2f (i * SCALE, j * SCALE); + glVertex3f (vertex_x[j * (N + 1) + i], vertex_y[j * (N + 1) + i], 0); + glTexCoord2f ((i + 1) * SCALE, j * SCALE); + glVertex3f (vertex_x[j * (N + 1) + (i + 1)], + vertex_y[j * (N + 1) + (i + 1)], 0); + glTexCoord2f ((i + 1) * SCALE, (j + 1) * SCALE); + glVertex3f (vertex_x[(j + 1) * (N + 1) + (i + 1)], + vertex_y[(j + 1) * (N + 1) + (i + 1)], 0); + glTexCoord2f (i * SCALE, (j + 1) * SCALE); + glVertex3f (vertex_x[(j + 1) * (N + 1) + i], + vertex_y[(j + 1) * (N + 1) + i], 0); + glEnd (); + } + } + free (vertex_x); + free (vertex_y); + glFlush (); + glMatrixMode (GL_MODELVIEW); + glLoadIdentity (); + glMatrixMode (GL_TEXTURE); + glLoadIdentity (); glMatrixMode (GL_COLOR); glLoadIdentity (); glPixelTransferf (GL_POST_COLOR_MATRIX_RED_SCALE, 1.0); diff --git a/gst/gl/gstgltestsrc.c b/gst/gl/gstgltestsrc.c index a5e544a6c6..563f3c5f13 100644 --- a/gst/gl/gstgltestsrc.c +++ b/gst/gl/gstgltestsrc.c @@ -53,8 +53,6 @@ GST_DEBUG_CATEGORY_STATIC (gl_test_src_debug); #define GST_CAT_DEFAULT gl_test_src_debug -#define GST_GL_VIDEO_CAPS "video/x-raw-gl,width=(int)[1,2048],height=(int)[1,2048],framerate=(fraction)[0/1,100/1],pixel_aspect_ratio=(fraction)[0/1,100/1]" - static const GstElementDetails gl_test_src_details = GST_ELEMENT_DETAILS ("Video test source", "Source/Video", diff --git a/gst/gl/gstglupload.c b/gst/gl/gstglupload.c index 3b5cbc5fa5..cab9935951 100644 --- a/gst/gl/gstglupload.c +++ b/gst/gl/gstglupload.c @@ -68,8 +68,6 @@ static const GstElementDetails element_details = GST_ELEMENT_DETAILS ("FIXME", "FIXME example filter", "FIXME "); -#define GST_GL_VIDEO_CAPS "video/x-raw-gl" - static GstStaticPadTemplate gst_gl_upload_src_pad_template = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,