1 /* GStreamer
2  *
3  * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24 
25 #include <gst/check/gstcheck.h>
26 
27 #include <gst/gl/gl.h>
28 #include <gst/gl/gstglfuncs.h>
29 
30 #include <stdio.h>
31 
32 static GstGLDisplay *display;
33 static GstGLContext *context;
34 static GstGLWindow *window;
35 static GstGLColorConvert *convert;
36 
37 typedef struct _TestFrame
38 {
39   gint width;
40   gint height;
41   GstVideoFormat v_format;
42   gchar *data[GST_VIDEO_MAX_PLANES];
43 } TestFrame;
44 
45 #define IGNORE_MAGIC 0x05
46 
47 static const gchar rgba_reorder_data[] = { 0x49, 0x24, 0x72, 0xff };
48 static const gchar rgbx_reorder_data[] = { 0x49, 0x24, 0x72, IGNORE_MAGIC };
49 static const gchar argb_reorder_data[] = { 0xff, 0x49, 0x24, 0x72 };
50 static const gchar xrgb_reorder_data[] = { IGNORE_MAGIC, 0x49, 0x24, 0x72 };
51 static const gchar rgb_reorder_data[] = { 0x49, 0x24, 0x72, IGNORE_MAGIC };
52 static const gchar bgr_reorder_data[] = { 0x72, 0x24, 0x49, IGNORE_MAGIC };
53 static const gchar bgra_reorder_data[] = { 0x72, 0x24, 0x49, 0xff };
54 static const gchar bgrx_reorder_data[] = { 0x72, 0x24, 0x49, IGNORE_MAGIC };
55 static const gchar abgr_reorder_data[] = { 0xff, 0x72, 0x24, 0x49 };
56 static const gchar xbgr_reorder_data[] = { IGNORE_MAGIC, 0x72, 0x24, 0x49 };
57 
58 static TestFrame test_rgba_reorder[] = {
59   {1, 1, GST_VIDEO_FORMAT_RGBA, {(gchar *) & rgba_reorder_data}},
60   {1, 1, GST_VIDEO_FORMAT_RGBx, {(gchar *) & rgbx_reorder_data}},
61   {1, 1, GST_VIDEO_FORMAT_ARGB, {(gchar *) & argb_reorder_data}},
62   {1, 1, GST_VIDEO_FORMAT_xRGB, {(gchar *) & xrgb_reorder_data}},
63   {1, 1, GST_VIDEO_FORMAT_BGRA, {(gchar *) & bgra_reorder_data}},
64   {1, 1, GST_VIDEO_FORMAT_BGRx, {(gchar *) & bgrx_reorder_data}},
65   {1, 1, GST_VIDEO_FORMAT_ABGR, {(gchar *) & abgr_reorder_data}},
66   {1, 1, GST_VIDEO_FORMAT_xBGR, {(gchar *) & xbgr_reorder_data}},
67   {1, 1, GST_VIDEO_FORMAT_RGB, {(gchar *) & rgb_reorder_data}},
68   {1, 1, GST_VIDEO_FORMAT_BGR, {(gchar *) & bgr_reorder_data}},
69 };
70 
71 static void
setup(void)72 setup (void)
73 {
74   GError *error = NULL;
75 
76   display = gst_gl_display_new ();
77   context = gst_gl_context_new (display);
78 
79   gst_gl_context_create (context, 0, &error);
80   window = gst_gl_context_get_window (context);
81 
82   fail_if (error != NULL, "Error creating context: %s\n",
83       error ? error->message : "Unknown Error");
84 
85   convert = gst_gl_color_convert_new (context);
86 }
87 
88 static void
_check_gl_error(GstGLContext * context,gpointer data)89 _check_gl_error (GstGLContext * context, gpointer data)
90 {
91   GLuint error = context->gl_vtable->GetError ();
92   fail_if (error != GL_NONE, "GL error 0x%x encountered during processing\n",
93       error);
94 }
95 
96 static void
teardown(void)97 teardown (void)
98 {
99   gst_object_unref (convert);
100   gst_object_unref (window);
101 
102   gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _check_gl_error,
103       NULL);
104   gst_object_unref (context);
105   gst_object_unref (display);
106 }
107 
108 static gsize
_video_info_plane_size(GstVideoInfo * info,guint plane)109 _video_info_plane_size (GstVideoInfo * info, guint plane)
110 {
111   if (GST_VIDEO_INFO_N_PLANES (info) == plane + 1)
112     return info->offset[0] + info->size - info->offset[plane];
113 
114   return info->offset[plane + 1] - info->offset[plane];
115 }
116 
117 static void
_frame_unref(gpointer user_data)118 _frame_unref (gpointer user_data)
119 {
120   gint *ref_count = user_data;
121   g_atomic_int_add (ref_count, -1);
122 }
123 
124 static void
check_conversion(TestFrame * frames,guint size)125 check_conversion (TestFrame * frames, guint size)
126 {
127   GstGLBaseMemoryAllocator *base_mem_alloc;
128   gint i, j, k, l;
129   gint ref_count = 0;
130 
131   base_mem_alloc =
132       GST_GL_BASE_MEMORY_ALLOCATOR (gst_allocator_find
133       (GST_GL_MEMORY_ALLOCATOR_NAME));
134 
135   for (i = 0; i < size; i++) {
136     GstBuffer *inbuf;
137     GstVideoInfo in_info;
138     gint in_width = frames[i].width;
139     gint in_height = frames[i].height;
140     GstVideoFormat in_v_format = frames[i].v_format;
141     GstVideoFrame in_frame;
142     GstCaps *in_caps;
143 
144     gst_video_info_set_format (&in_info, in_v_format, in_width, in_height);
145     in_caps = gst_video_info_to_caps (&in_info);
146     gst_caps_set_features (in_caps, 0,
147         gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
148 
149     /* create GL buffer */
150     inbuf = gst_buffer_new ();
151     for (j = 0; j < GST_VIDEO_INFO_N_PLANES (&in_info); j++) {
152       GstGLFormat tex_format = gst_gl_format_from_video_info (context,
153           &in_info, j);
154       GstGLVideoAllocationParams *params;
155       GstGLBaseMemory *mem;
156 
157       ref_count++;
158       params = gst_gl_video_allocation_params_new_wrapped_data (context, NULL,
159           &in_info, j, NULL, GST_GL_TEXTURE_TARGET_2D, tex_format,
160           frames[i].data[j], &ref_count, _frame_unref);
161 
162       mem = gst_gl_base_memory_alloc (base_mem_alloc,
163           (GstGLAllocationParams *) params);
164       gst_buffer_append_memory (inbuf, GST_MEMORY_CAST (mem));
165 
166       gst_gl_allocation_params_free ((GstGLAllocationParams *) params);
167     }
168 
169     fail_unless (gst_video_frame_map (&in_frame, &in_info, inbuf,
170             GST_MAP_READ));
171 
172     /* sanity check that the correct values were wrapped */
173     for (j = 0; j < GST_VIDEO_INFO_N_PLANES (&in_info); j++) {
174       for (k = 0; k < _video_info_plane_size (&in_info, j); k++) {
175         if (frames[i].data[j][k] != IGNORE_MAGIC)
176           fail_unless (((gchar *) in_frame.data[j])[k] == frames[i].data[j][k]);
177       }
178     }
179 
180     for (j = 0; j < size; j++) {
181       GstBuffer *outbuf;
182       GstVideoInfo out_info;
183       gint out_width = frames[j].width;
184       gint out_height = frames[j].height;
185       GstVideoFormat out_v_format = frames[j].v_format;
186       gchar *out_data[GST_VIDEO_MAX_PLANES] = { 0 };
187       GstVideoFrame out_frame;
188       GstCaps *out_caps;
189 
190       gst_video_info_set_format (&out_info, out_v_format, out_width,
191           out_height);
192       out_caps = gst_video_info_to_caps (&out_info);
193       gst_caps_set_features (out_caps, 0,
194           gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
195 
196       for (k = 0; k < GST_VIDEO_INFO_N_PLANES (&out_info); k++) {
197         out_data[k] = frames[j].data[k];
198       }
199 
200       gst_gl_color_convert_set_caps (convert, in_caps, out_caps);
201 
202       /* convert the data */
203       outbuf = gst_gl_color_convert_perform (convert, inbuf);
204       if (outbuf == NULL) {
205         const gchar *in_str = gst_video_format_to_string (in_v_format);
206         const gchar *out_str = gst_video_format_to_string (out_v_format);
207         GST_WARNING ("failed to convert from %s to %s", in_str, out_str);
208       }
209 
210       fail_unless (gst_video_frame_map (&out_frame, &out_info, outbuf,
211               GST_MAP_READ));
212 
213       /* check that the converted values are correct */
214       for (k = 0; k < GST_VIDEO_INFO_N_PLANES (&out_info); k++) {
215         for (l = 0; l < _video_info_plane_size (&out_info, k); l++) {
216           gchar out_pixel = ((gchar *) out_frame.data[k])[l];
217           if (out_data[k][l] != IGNORE_MAGIC && out_pixel != IGNORE_MAGIC)
218             fail_unless (out_pixel == out_data[k][l]);
219           /* FIXME: check alpha clobbering */
220         }
221       }
222 
223       gst_caps_unref (out_caps);
224       gst_video_frame_unmap (&out_frame);
225       gst_buffer_unref (outbuf);
226     }
227 
228     gst_caps_unref (in_caps);
229     gst_video_frame_unmap (&in_frame);
230     gst_buffer_unref (inbuf);
231 
232     fail_unless_equals_int (ref_count, 0);
233   }
234 
235   gst_object_unref (base_mem_alloc);
236 }
237 
GST_START_TEST(test_reorder_buffer)238 GST_START_TEST (test_reorder_buffer)
239 {
240   guint size = G_N_ELEMENTS (test_rgba_reorder);
241 
242   /* gles can't download rgb24 textures */
243   if (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES2)
244     size -= 2;
245 
246   check_conversion (test_rgba_reorder, size);
247 }
248 
249 GST_END_TEST;
250 
251 static Suite *
gst_gl_color_convert_suite(void)252 gst_gl_color_convert_suite (void)
253 {
254   Suite *s = suite_create ("GstGLColorConvert");
255   TCase *tc_chain = tcase_create ("upload");
256 
257   suite_add_tcase (s, tc_chain);
258   tcase_add_checked_fixture (tc_chain, setup, teardown);
259   tcase_add_test (tc_chain, test_reorder_buffer);
260   /* FIXME add YUV <--> RGB conversion tests */
261 
262   return s;
263 }
264 
265 GST_CHECK_MAIN (gst_gl_color_convert);
266