PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/GStreamer/Source/gst-plugins-bad/tests/check/elements/assrender.c

http://ossbuild.googlecode.com/
C | 294 lines | 248 code | 27 blank | 19 comment | 32 complexity | 32f15b37fb4d805945f83daef73fface MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, AGPL-1.0, LGPL-2.1, LGPL-2.0, GPL-3.0, LGPL-3.0, CC-BY-SA-3.0
  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., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. #include <string.h>
  20. #include <gst/check/gstcheck.h>
  21. #include <gst/video/video.h>
  22. #include <gst/app/gstappsrc.h>
  23. static gboolean
  24. bus_handler (GstBus * bus, GstMessage * message, gpointer data)
  25. {
  26. GMainLoop *loop = (GMainLoop *) data;
  27. switch (message->type) {
  28. case GST_MESSAGE_EOS:
  29. g_main_loop_quit (loop);
  30. break;
  31. case GST_MESSAGE_WARNING:
  32. case GST_MESSAGE_ERROR:{
  33. GError *gerror;
  34. gchar *debug;
  35. if (message->type == GST_MESSAGE_WARNING)
  36. gst_message_parse_warning (message, &gerror, &debug);
  37. else
  38. gst_message_parse_error (message, &gerror, &debug);
  39. gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
  40. gst_message_unref (message);
  41. g_error_free (gerror);
  42. g_free (debug);
  43. g_main_loop_quit (loop);
  44. break;
  45. }
  46. default:
  47. break;
  48. }
  49. return TRUE;
  50. }
  51. typedef struct
  52. {
  53. GstClockTime ts;
  54. GstClockTime duration;
  55. const gchar buf[];
  56. } TestBuffer;
  57. static const TestBuffer buf0 = {
  58. 0,
  59. 0,
  60. "[Script Info]\n"
  61. "; This is a Sub Station Alpha v4 script.\n"
  62. "; For Sub Station Alpha info and downloads,\n"
  63. "; go to http://www.eswat.demon.co.uk/\n"
  64. "Title: Some Test\n"
  65. "Script Updated By: version 2.8.01\n"
  66. "ScriptType: v4.00\n"
  67. "Collisions: Normal\n"
  68. "PlayResY: 200\n"
  69. "PlayDepth: 0\n"
  70. "Timer: 100,0000\n"
  71. " \n"
  72. "[V4 Styles]\n"
  73. "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, \n"
  74. " Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\n"
  75. "Style: DefaultVCD, Arial,28,11861244,11861244,11861244,-2147483640,-1,0,1,1,2,2,30,30,30,0,0\n"
  76. " \n"
  77. "[Events]\n"
  78. "Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"
  79. };
  80. static const TestBuffer buf1 = {
  81. 40 * GST_MSECOND,
  82. 60 * GST_MSECOND,
  83. "1,,DefaultVCD, NTP,0000,0000,0000,,Some Test Blabla"
  84. };
  85. static void
  86. sink_handoff_cb_xRGB (GstElement * object, GstBuffer * buffer, GstPad * pad,
  87. gpointer user_data)
  88. {
  89. guint *sink_pos = (guint *) user_data;
  90. gboolean contains_text = (*sink_pos == 1 || *sink_pos == 2);
  91. guint i, j;
  92. guint8 *data = GST_BUFFER_DATA (buffer);
  93. gboolean all_red = TRUE;
  94. fail_unless_equals_int (GST_BUFFER_SIZE (buffer), 640 * 480 * 4);
  95. for (i = 0; i < 640; i++) {
  96. for (j = 0; j < 480; j++) {
  97. all_red = all_red && (data[i * 480 * 4 + j * 4 + 1] == 255 &&
  98. data[i * 480 * 4 + j * 4 + 2] == 0 &&
  99. data[i * 480 * 4 + j * 4 + 3] == 0);
  100. }
  101. }
  102. fail_unless (contains_text != all_red,
  103. "Frame %d is incorrect (all red %d, contains text %d)", *sink_pos,
  104. all_red, contains_text);
  105. *sink_pos = *sink_pos + 1;
  106. }
  107. static void
  108. sink_handoff_cb_I420 (GstElement * object, GstBuffer * buffer, GstPad * pad,
  109. gpointer user_data)
  110. {
  111. guint *sink_pos = (guint *) user_data;
  112. gboolean contains_text = (*sink_pos == 1 || *sink_pos == 2);
  113. guint c, i, j;
  114. guint8 *data = GST_BUFFER_DATA (buffer);
  115. gboolean all_red = TRUE;
  116. guint8 *comp;
  117. gint comp_stride, comp_width, comp_height;
  118. const guint8 color[] = { 81, 90, 240 };
  119. fail_unless_equals_int (GST_BUFFER_SIZE (buffer),
  120. gst_video_format_get_size (GST_VIDEO_FORMAT_I420, 640, 480));
  121. for (c = 0; c < 3; c++) {
  122. comp =
  123. data + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, c,
  124. 640, 480);
  125. comp_stride =
  126. gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, c, 640);
  127. comp_width =
  128. gst_video_format_get_component_width (GST_VIDEO_FORMAT_I420, c, 640);
  129. comp_height =
  130. gst_video_format_get_component_height (GST_VIDEO_FORMAT_I420, c, 480);
  131. for (i = 0; i < comp_height; i++) {
  132. for (j = 0; j < comp_width; j++) {
  133. all_red = all_red && (comp[i * comp_stride + j] == color[c]);
  134. }
  135. }
  136. }
  137. fail_unless (contains_text != all_red,
  138. "Frame %d is incorrect (all red %d, contains text %d)", *sink_pos,
  139. all_red, contains_text);
  140. *sink_pos = *sink_pos + 1;
  141. }
  142. static void
  143. _dummy_blocked_cb (GstPad * pad, gboolean blocked, gpointer user_data)
  144. {
  145. GST_LOG_OBJECT (pad, "pad blocked: %d", blocked);
  146. }
  147. static gboolean
  148. src_buffer_probe_cb (GstPad * pad, GstBuffer * buffer, gpointer user_data)
  149. {
  150. GstPad *otherpad = GST_PAD (user_data);
  151. if (GST_BUFFER_TIMESTAMP (buffer) == buf1.ts)
  152. gst_pad_set_blocked_async (otherpad, FALSE, _dummy_blocked_cb, NULL);
  153. return TRUE;
  154. }
  155. #define CREATE_BASIC_TEST(format) \
  156. GST_START_TEST (test_assrender_basic_##format) \
  157. { \
  158. GstElement *pipeline; \
  159. GstElement *appsrc, *videotestsrc, *capsfilter, *assrender, *fakesink; \
  160. guint sink_pos = 0; \
  161. GstCaps *video_caps; \
  162. GstCaps *text_caps; \
  163. GstBuffer *buf; \
  164. GstBus *bus; \
  165. GMainLoop *loop; \
  166. GstPad *pad, *blocked_pad; \
  167. guint bus_watch = 0; \
  168. \
  169. pipeline = gst_pipeline_new ("pipeline"); \
  170. fail_unless (pipeline != NULL); \
  171. \
  172. capsfilter = gst_element_factory_make ("capsfilter", NULL); \
  173. fail_unless (capsfilter != NULL); \
  174. video_caps = \
  175. gst_video_format_new_caps (GST_VIDEO_FORMAT_##format, 640, 480, 25, 1, 1, 1); \
  176. g_object_set (capsfilter, "caps", video_caps, NULL); \
  177. gst_caps_unref (video_caps); \
  178. blocked_pad = gst_element_get_static_pad (capsfilter, "src"); \
  179. gst_pad_set_blocked_async (blocked_pad, TRUE, _dummy_blocked_cb, NULL); \
  180. \
  181. appsrc = gst_element_factory_make ("appsrc", NULL); \
  182. fail_unless (appsrc != NULL); \
  183. buf = gst_buffer_new_and_alloc (strlen (buf0.buf) + 1); \
  184. memcpy (GST_BUFFER_DATA (buf), buf0.buf, GST_BUFFER_SIZE (buf)); \
  185. GST_BUFFER_TIMESTAMP (buf) = buf0.ts; \
  186. GST_BUFFER_DURATION (buf) = buf0.duration; \
  187. text_caps = \
  188. gst_caps_new_simple ("application/x-ssa", "codec_data", GST_TYPE_BUFFER, \
  189. buf, NULL); \
  190. gst_buffer_unref (buf); \
  191. gst_app_src_set_caps (GST_APP_SRC (appsrc), text_caps); \
  192. g_object_set (appsrc, "format", GST_FORMAT_TIME, NULL); \
  193. pad = gst_element_get_static_pad (appsrc, "src"); \
  194. gst_pad_add_buffer_probe_full (pad, G_CALLBACK (src_buffer_probe_cb), \
  195. gst_object_ref (blocked_pad), (GDestroyNotify) gst_object_unref); \
  196. gst_object_unref (blocked_pad); \
  197. gst_object_unref (pad); \
  198. \
  199. videotestsrc = gst_element_factory_make ("videotestsrc", NULL); \
  200. fail_unless (videotestsrc != NULL); \
  201. g_object_set (videotestsrc, "num-buffers", 5, "pattern", 4, NULL); \
  202. \
  203. assrender = gst_element_factory_make ("assrender", NULL); \
  204. fail_unless (assrender != NULL); \
  205. \
  206. fakesink = gst_element_factory_make ("fakesink", NULL); \
  207. fail_unless (fakesink != NULL); \
  208. g_object_set (fakesink, "signal-handoffs", TRUE, "async", FALSE, NULL); \
  209. g_signal_connect (fakesink, "handoff", G_CALLBACK (sink_handoff_cb_##format), \
  210. &sink_pos); \
  211. \
  212. gst_bin_add_many (GST_BIN (pipeline), appsrc, videotestsrc, capsfilter, \
  213. assrender, fakesink, NULL); \
  214. \
  215. fail_unless (gst_element_link_pads (appsrc, "src", assrender, "text_sink")); \
  216. fail_unless (gst_element_link_pads (videotestsrc, "src", capsfilter, "sink")); \
  217. fail_unless (gst_element_link_pads (capsfilter, "src", assrender, \
  218. "video_sink")); \
  219. fail_unless (gst_element_link_pads (assrender, "src", fakesink, "sink")); \
  220. \
  221. loop = g_main_loop_new (NULL, TRUE); \
  222. fail_unless (loop != NULL); \
  223. \
  224. bus = gst_element_get_bus (pipeline); \
  225. fail_unless (bus != NULL); \
  226. bus_watch = gst_bus_add_watch (bus, bus_handler, loop); \
  227. gst_object_unref (bus); \
  228. \
  229. fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING), \
  230. GST_STATE_CHANGE_SUCCESS); \
  231. \
  232. buf = gst_buffer_new_and_alloc (strlen (buf1.buf) + 1); \
  233. memcpy (GST_BUFFER_DATA (buf), buf1.buf, GST_BUFFER_SIZE (buf)); \
  234. gst_buffer_set_caps (buf, text_caps); \
  235. GST_BUFFER_TIMESTAMP (buf) = buf1.ts; \
  236. GST_BUFFER_DURATION (buf) = buf1.duration; \
  237. gst_app_src_push_buffer (GST_APP_SRC (appsrc), buf); \
  238. gst_caps_unref (text_caps); \
  239. gst_app_src_end_of_stream (GST_APP_SRC (appsrc)); \
  240. \
  241. g_main_loop_run (loop); \
  242. \
  243. gst_element_set_state (pipeline, GST_STATE_NULL); \
  244. \
  245. fail_unless_equals_int (sink_pos, 5); \
  246. \
  247. g_object_unref (pipeline); \
  248. g_main_loop_unref (loop); \
  249. g_source_remove (bus_watch); \
  250. } \
  251. \
  252. GST_END_TEST;
  253. CREATE_BASIC_TEST (xRGB);
  254. CREATE_BASIC_TEST (I420);
  255. static Suite *
  256. assrender_suite (void)
  257. {
  258. Suite *s = suite_create ("assrender");
  259. TCase *tc_chain = tcase_create ("linear");
  260. /* time out after 120s, not the default 3 */
  261. tcase_set_timeout (tc_chain, 120);
  262. suite_add_tcase (s, tc_chain);
  263. tcase_add_test (tc_chain, test_assrender_basic_xRGB);
  264. tcase_add_test (tc_chain, test_assrender_basic_I420);
  265. return s;
  266. }
  267. GST_CHECK_MAIN (assrender);