Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / tests / check / elements / assrender.c
1 /* GStreamer
2  * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <string.h>
21
22 #include <gst/check/gstcheck.h>
23 #include <gst/video/video.h>
24 #include <gst/app/gstappsrc.h>
25
26 static gboolean
27 bus_handler (GstBus * bus, GstMessage * message, gpointer data)
28 {
29   GMainLoop *loop = (GMainLoop *) data;
30
31   switch (message->type) {
32     case GST_MESSAGE_EOS:
33       g_main_loop_quit (loop);
34       break;
35     case GST_MESSAGE_WARNING:
36     case GST_MESSAGE_ERROR:{
37       GError *gerror;
38       gchar *debug;
39
40       if (message->type == GST_MESSAGE_WARNING)
41         gst_message_parse_warning (message, &gerror, &debug);
42       else
43         gst_message_parse_error (message, &gerror, &debug);
44       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
45       gst_message_unref (message);
46       g_error_free (gerror);
47       g_free (debug);
48       g_main_loop_quit (loop);
49       break;
50     }
51     default:
52       break;
53   }
54
55   return TRUE;
56 }
57
58 typedef struct
59 {
60   GstClockTime ts;
61   GstClockTime duration;
62   const gchar buf[];
63 } TestBuffer;
64
65 static const TestBuffer buf0 = {
66   0,
67   0,
68   {"[Script Info]\n"
69         "; This is a Sub Station Alpha v4 script.\n"
70         "; For Sub Station Alpha info and downloads,\n"
71         "; go to http://www.eswat.demon.co.uk/\n"
72         "Title: Some Test\n"
73         "Script Updated By: version 2.8.01\n"
74         "ScriptType: v4.00\n"
75         "Collisions: Normal\n"
76         "PlayResY: 200\n"
77         "PlayDepth: 0\n"
78         "Timer: 100,0000\n"
79         " \n"
80         "[V4 Styles]\n"
81         "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, \n"
82         "   Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\n"
83         "Style: DefaultVCD, Arial,28,11861244,11861244,11861244,-2147483640,-1,0,1,1,2,2,30,30,30,0,0\n"
84         " \n"
85         "[Events]\n"
86         "Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"}
87 };
88
89 static const TestBuffer buf1 = {
90   40 * GST_MSECOND,
91   60 * GST_MSECOND,
92   {"1,,DefaultVCD, NTP,0000,0000,0000,,Some Test Blabla"}
93 };
94
95 static void
96 sink_handoff_cb_xRGB (GstElement * object, GstBuffer * buffer, GstPad * pad,
97     gpointer user_data)
98 {
99   guint *sink_pos = (guint *) user_data;
100   gboolean contains_text = (*sink_pos == 1 || *sink_pos == 2);
101   guint i, j;
102   GstMapInfo map;
103   gboolean all_red = TRUE;
104
105   gst_buffer_map (buffer, &map, GST_MAP_READ);
106
107   fail_unless_equals_int (map.size, 640 * 480 * 4);
108
109   for (i = 0; i < 640; i++) {
110     for (j = 0; j < 480; j++) {
111       all_red = all_red && (map.data[i * 480 * 4 + j * 4 + 1] == 255 &&
112           map.data[i * 480 * 4 + j * 4 + 2] == 0 &&
113           map.data[i * 480 * 4 + j * 4 + 3] == 0);
114     }
115   }
116   gst_buffer_unmap (buffer, &map);
117
118   fail_unless (contains_text != all_red,
119       "Frame %d is incorrect (all red %d, contains text %d)", *sink_pos,
120       all_red, contains_text);
121   *sink_pos = *sink_pos + 1;
122 }
123
124 static void
125 sink_handoff_cb_I420 (GstElement * object, GstBuffer * buffer, GstPad * pad,
126     gpointer user_data)
127 {
128   guint *sink_pos = (guint *) user_data;
129   gboolean contains_text = (*sink_pos == 1 || *sink_pos == 2);
130   guint c, i, j;
131   gboolean all_red = TRUE;
132   guint8 *comp;
133   gint comp_stride, comp_width, comp_height;
134   const guint8 color[] = { 81, 90, 240 };
135   GstVideoInfo info;
136   GstVideoFrame frame;
137
138   gst_video_info_init (&info);
139   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_I420, 640, 480);
140
141   gst_video_frame_map (&frame, &info, buffer, GST_MAP_READ);
142
143   for (c = 0; c < 3; c++) {
144     comp = GST_VIDEO_FRAME_COMP_DATA (&frame, c);
145     comp_stride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, c);
146     comp_width = GST_VIDEO_FRAME_COMP_WIDTH (&frame, c);
147     comp_height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, c);
148
149     for (i = 0; i < comp_height; i++) {
150       for (j = 0; j < comp_width; j++) {
151         all_red = all_red && (comp[i * comp_stride + j] == color[c]);
152       }
153     }
154   }
155   gst_video_frame_unmap (&frame);
156
157   fail_unless (contains_text != all_red,
158       "Frame %d is incorrect (all red %d, contains text %d)", *sink_pos,
159       all_red, contains_text);
160   *sink_pos = *sink_pos + 1;
161 }
162
163 static gulong probe_id = 0;
164
165 static GstPadProbeReturn
166 src_buffer_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
167 {
168   GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
169   GstPad *otherpad = GST_PAD (user_data);
170
171   if (GST_BUFFER_TIMESTAMP (buffer) == buf1.ts)
172     gst_pad_remove_probe (otherpad, probe_id);
173
174   return GST_PAD_PROBE_OK;
175 }
176
177 #define CREATE_BASIC_TEST(format) \
178 GST_START_TEST (test_assrender_basic_##format) \
179 { \
180   GstElement *pipeline; \
181   GstElement *appsrc, *videotestsrc, *capsfilter, *assrender, *fakesink; \
182   guint sink_pos = 0; \
183   GstCaps *video_caps; \
184   GstCaps *text_caps; \
185   GstBuffer *buf; \
186   GstBus *bus; \
187   GMainLoop *loop; \
188   GstPad *pad, *blocked_pad; \
189   guint bus_watch = 0; \
190   GstVideoInfo info; \
191   \
192   pipeline = gst_pipeline_new ("pipeline"); \
193   fail_unless (pipeline != NULL); \
194   \
195   capsfilter = gst_element_factory_make ("capsfilter", NULL); \
196   fail_unless (capsfilter != NULL); \
197   gst_video_info_init (&info); \
198   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_##format, 640, 480); \
199   info.fps_n = 25; \
200   info.fps_d = 1; \
201   video_caps = gst_video_info_to_caps (&info); \
202   g_object_set (capsfilter, "caps", video_caps, NULL); \
203   gst_caps_unref (video_caps); \
204   blocked_pad = gst_element_get_static_pad (capsfilter, "src"); \
205   gst_pad_add_probe (blocked_pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, NULL, NULL, NULL); \
206   \
207   appsrc = gst_element_factory_make ("appsrc", NULL); \
208   fail_unless (appsrc != NULL); \
209   buf = gst_buffer_new_and_alloc (strlen (buf0.buf) + 1); \
210   gst_buffer_fill (buf, 0, buf0.buf, strlen (buf0.buf) + 1); \
211   GST_BUFFER_TIMESTAMP (buf) = buf0.ts; \
212   GST_BUFFER_DURATION (buf) = buf0.duration; \
213   text_caps = \
214       gst_caps_new_simple ("application/x-ssa", "codec_data", GST_TYPE_BUFFER, \
215       buf, NULL); \
216   gst_buffer_unref (buf); \
217   gst_app_src_set_caps (GST_APP_SRC (appsrc), text_caps); \
218   g_object_set (appsrc, "format", GST_FORMAT_TIME, NULL); \
219   pad = gst_element_get_static_pad (appsrc, "src"); \
220   probe_id = gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, src_buffer_probe_cb, \
221       gst_object_ref (blocked_pad), (GDestroyNotify) gst_object_unref); \
222   gst_object_unref (blocked_pad); \
223   gst_object_unref (pad); \
224   \
225   videotestsrc = gst_element_factory_make ("videotestsrc", NULL); \
226   fail_unless (videotestsrc != NULL); \
227   g_object_set (videotestsrc, "num-buffers", 5, "pattern", 4, NULL); \
228   \
229   assrender = gst_element_factory_make ("assrender", NULL); \
230   fail_unless (assrender != NULL); \
231   \
232   fakesink = gst_element_factory_make ("fakesink", NULL); \
233   fail_unless (fakesink != NULL); \
234   g_object_set (fakesink, "signal-handoffs", TRUE, "async", FALSE, NULL); \
235   g_signal_connect (fakesink, "handoff", G_CALLBACK (sink_handoff_cb_##format), \
236       &sink_pos); \
237   \
238   gst_bin_add_many (GST_BIN (pipeline), appsrc, videotestsrc, capsfilter, \
239       assrender, fakesink, NULL); \
240   \
241   fail_unless (gst_element_link_pads (appsrc, "src", assrender, "text_sink")); \
242   fail_unless (gst_element_link_pads (videotestsrc, "src", capsfilter, "sink")); \
243   fail_unless (gst_element_link_pads (capsfilter, "src", assrender, \
244           "video_sink")); \
245   fail_unless (gst_element_link_pads (assrender, "src", fakesink, "sink")); \
246   \
247   loop = g_main_loop_new (NULL, TRUE); \
248   fail_unless (loop != NULL); \
249   \
250   bus = gst_element_get_bus (pipeline); \
251   fail_unless (bus != NULL); \
252   bus_watch = gst_bus_add_watch (bus, bus_handler, loop); \
253   gst_object_unref (bus); \
254   \
255   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING), \
256       GST_STATE_CHANGE_SUCCESS); \
257   \
258   buf = gst_buffer_new_and_alloc (strlen (buf1.buf) + 1); \
259   gst_buffer_fill (buf, 0, buf1.buf, strlen (buf1.buf) + 1); \
260   GST_BUFFER_TIMESTAMP (buf) = buf1.ts; \
261   GST_BUFFER_DURATION (buf) = buf1.duration; \
262   gst_app_src_push_buffer (GST_APP_SRC (appsrc), buf); \
263   gst_caps_unref (text_caps); \
264   gst_app_src_end_of_stream (GST_APP_SRC (appsrc)); \
265   \
266   g_main_loop_run (loop); \
267   \
268   gst_element_set_state (pipeline, GST_STATE_NULL); \
269   \
270   fail_unless_equals_int (sink_pos, 5); \
271   \
272   g_object_unref (pipeline); \
273   g_main_loop_unref (loop); \
274   g_source_remove (bus_watch); \
275 } \
276 \
277 GST_END_TEST;
278
279 CREATE_BASIC_TEST (xRGB);
280 CREATE_BASIC_TEST (I420);
281
282 static Suite *
283 assrender_suite (void)
284 {
285   Suite *s = suite_create ("assrender");
286   TCase *tc_chain = tcase_create ("linear");
287
288   /* time out after 120s, not the default 3 */
289   tcase_set_timeout (tc_chain, 120);
290
291   suite_add_tcase (s, tc_chain);
292   tcase_add_test (tc_chain, test_assrender_basic_xRGB);
293   tcase_add_test (tc_chain, test_assrender_basic_I420);
294
295   return s;
296 }
297
298 GST_CHECK_MAIN (assrender);