1 /* GStreamer
2 *
3 * Copyright (c) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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 #include <gst/check/gstcheck.h>
22 #include <gst/check/gstharness.h>
23 #include <gst/video/video.h>
24
25 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
26 GST_PAD_SINK,
27 GST_PAD_ALWAYS,
28 GST_STATIC_CAPS ("video/x-raw, "
29 "format = (string) I420, "
30 "width = (int) [1, MAX], "
31 "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
32
33 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
34 GST_PAD_SRC,
35 GST_PAD_ALWAYS,
36 GST_STATIC_CAPS ("video/x-raw, "
37 "format = (string) I420, "
38 "width = (int) [1, MAX], "
39 "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
40
41 static GstPad *sinkpad, *srcpad;
42
43 static GstElement *
setup_vp8dec(const gchar * src_caps_str)44 setup_vp8dec (const gchar * src_caps_str)
45 {
46 GstElement *bin;
47 GstElement *vp8enc, *vp8dec;
48 GstCaps *srccaps = NULL;
49 GstBus *bus;
50 GstPad *ghostpad, *targetpad;
51
52 if (src_caps_str) {
53 srccaps = gst_caps_from_string (src_caps_str);
54 fail_unless (srccaps != NULL);
55 }
56
57 bin = gst_bin_new ("bin");
58
59 vp8enc = gst_check_setup_element ("vp8enc");
60 fail_unless (vp8enc != NULL);
61 vp8dec = gst_check_setup_element ("vp8dec");
62 fail_unless (vp8dec != NULL);
63
64 g_object_set (vp8enc, "name", "encoder", NULL);
65 g_object_set (vp8dec, "name", "decoder", NULL);
66
67 gst_bin_add_many (GST_BIN (bin), vp8enc, vp8dec, NULL);
68 fail_unless (gst_element_link_pads (vp8enc, "src", vp8dec, "sink"));
69
70 targetpad = gst_element_get_static_pad (vp8enc, "sink");
71 fail_unless (targetpad != NULL);
72 ghostpad = gst_ghost_pad_new ("sink", targetpad);
73 fail_unless (ghostpad != NULL);
74 gst_element_add_pad (bin, ghostpad);
75 gst_object_unref (targetpad);
76
77 targetpad = gst_element_get_static_pad (vp8dec, "src");
78 fail_unless (targetpad != NULL);
79 ghostpad = gst_ghost_pad_new ("src", targetpad);
80 fail_unless (ghostpad != NULL);
81 gst_element_add_pad (bin, ghostpad);
82 gst_object_unref (targetpad);
83
84 srcpad = gst_check_setup_src_pad (bin, &srctemplate);
85 sinkpad = gst_check_setup_sink_pad (bin, &sinktemplate);
86 gst_pad_set_active (srcpad, TRUE);
87 gst_pad_set_active (sinkpad, TRUE);
88 gst_check_setup_events (srcpad, bin, srccaps, GST_FORMAT_TIME);
89
90 bus = gst_bus_new ();
91 gst_element_set_bus (bin, bus);
92
93 fail_unless (gst_element_set_state (bin,
94 GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
95 "could not set to playing");
96
97 if (srccaps)
98 gst_caps_unref (srccaps);
99
100 buffers = NULL;
101 return bin;
102 }
103
104 static void
cleanup_vp8dec(GstElement * bin)105 cleanup_vp8dec (GstElement * bin)
106 {
107 GstBus *bus;
108
109 /* Free parsed buffers */
110 gst_check_drop_buffers ();
111
112 bus = GST_ELEMENT_BUS (bin);
113 gst_bus_set_flushing (bus, TRUE);
114 gst_object_unref (bus);
115
116 gst_pad_set_active (srcpad, FALSE);
117 gst_pad_set_active (sinkpad, FALSE);
118
119 gst_check_teardown_src_pad (bin);
120 gst_check_teardown_sink_pad (bin);
121 gst_check_teardown_element (bin);
122 }
123
124 static void
_gst_vp8_test_check_output_caps(gint width,gint height,gint fps_n,gint fps_d)125 _gst_vp8_test_check_output_caps (gint width, gint height, gint fps_n,
126 gint fps_d)
127 {
128 GstCaps *caps;
129 GstStructure *structure;
130 gint caps_w, caps_h, caps_fpsn, caps_fpsd;
131
132 caps = gst_pad_get_current_caps (sinkpad);
133 fail_unless (caps != NULL);
134 structure = gst_caps_get_structure (caps, 0);
135
136 fail_unless (gst_structure_get_int (structure, "width", &caps_w));
137 fail_unless (gst_structure_get_int (structure, "height", &caps_h));
138 fail_unless (gst_structure_get_fraction (structure, "framerate", &caps_fpsn,
139 &caps_fpsd));
140
141 fail_unless (width == caps_w);
142 fail_unless (height == caps_h);
143 fail_unless (fps_n == caps_fpsn);
144 fail_unless (fps_d == caps_fpsd);
145
146 gst_caps_unref (caps);
147 }
148
GST_START_TEST(test_decode_simple)149 GST_START_TEST (test_decode_simple)
150 {
151 GstElement *bin;
152 GstBuffer *buffer;
153 gint i;
154 GList *l;
155 GstSegment seg;
156
157 bin =
158 setup_vp8dec
159 ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
160
161 gst_segment_init (&seg, GST_FORMAT_TIME);
162 seg.stop = gst_util_uint64_scale (20, GST_SECOND, 25);
163 fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
164
165 buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
166 gst_buffer_memset (buffer, 0, 0, -1);
167
168 for (i = 0; i < 20; i++) {
169 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
170 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
171 fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
172 }
173
174 gst_buffer_unref (buffer);
175
176 fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
177
178 /* All buffers must be there now */
179 fail_unless_equals_int (g_list_length (buffers), 20);
180
181 for (l = buffers, i = 0; l; l = l->next, i++) {
182 buffer = l->data;
183
184 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (buffer),
185 gst_util_uint64_scale (i, GST_SECOND, 25));
186 fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
187 gst_util_uint64_scale (1, GST_SECOND, 25));
188 }
189
190 cleanup_vp8dec (bin);
191 }
192
193 GST_END_TEST;
194
195
GST_START_TEST(test_decode_caps_change)196 GST_START_TEST (test_decode_caps_change)
197 {
198 GstElement *bin;
199 GstBuffer *buffer;
200 GstSegment seg;
201 GstElement *encoder;
202 GstCaps *caps;
203
204 bin =
205 setup_vp8dec
206 ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
207
208 gst_segment_init (&seg, GST_FORMAT_TIME);
209 fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
210
211 buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
212 gst_buffer_memset (buffer, 0, 0, -1);
213
214 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (0, GST_SECOND, 25);
215 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
216 fail_unless (gst_pad_push (srcpad, buffer) == GST_FLOW_OK);
217
218 /* at this point, the output caps should be the same as the input */
219 _gst_vp8_test_check_output_caps (320, 240, 25, 1);
220 fail_unless_equals_int (g_list_length (buffers), 1);
221 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
222 buffers = NULL;
223
224 /* now change the caps */
225 encoder = gst_bin_get_by_name (GST_BIN (bin), "encoder");
226 gst_element_set_state (encoder, GST_STATE_NULL);
227 gst_element_sync_state_with_parent (encoder);
228 gst_object_unref (encoder);
229 caps = gst_caps_from_string
230 ("video/x-raw,format=(string)I420,width=(int)64,"
231 "height=(int)32,framerate=(fraction)30/1");
232 fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
233 fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
234 buffer = gst_buffer_new_and_alloc (64 * 32 + 2 * 32 * 16);
235 gst_buffer_memset (buffer, 0, 0, -1);
236 gst_caps_unref (caps);
237
238 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (0, GST_SECOND, 30);
239 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 30);
240 fail_unless (gst_pad_push (srcpad, buffer) == GST_FLOW_OK);
241
242 /* at this point, the output caps should be the same as the input */
243 _gst_vp8_test_check_output_caps (64, 32, 30, 1);
244 fail_unless_equals_int (g_list_length (buffers), 1);
245 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
246 buffers = NULL;
247
248 cleanup_vp8dec (bin);
249 }
250
251 GST_END_TEST;
252
GST_START_TEST(test_decode_invalid_resolution)253 GST_START_TEST (test_decode_invalid_resolution)
254 {
255 GstHarness *h;
256
257 guint8 invalid_width[] = {
258 0x50, 0x48, 0x00, 0x9d, 0x01, 0x2a, 0x00, 0x00, 0x11, 0x44, 0x39, 0x63,
259 };
260 guint8 invalid_height[] = {
261 0x50, 0x48, 0x00, 0x9d, 0x01, 0x2a, 0x11, 0x44, 0x00, 0x00, 0x39, 0x63,
262 };
263
264 h = gst_harness_new_parse ("vp8dec");
265 gst_harness_set_src_caps_str (h, "video/x-vp8");
266
267 fail_unless_equals_int (GST_FLOW_OK,
268 gst_harness_push (h,
269 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, invalid_width,
270 sizeof (invalid_width), 0, sizeof (invalid_width), NULL, NULL)));
271 fail_unless (gst_harness_try_pull (h) == NULL);
272
273 fail_unless_equals_int (GST_FLOW_OK,
274 gst_harness_push (h,
275 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, invalid_height,
276 sizeof (invalid_height), 0, sizeof (invalid_height), NULL,
277 NULL)));
278 fail_unless (gst_harness_try_pull (h) == NULL);
279
280 gst_harness_teardown (h);
281 }
282
283 GST_END_TEST;
284
285
286 static Suite *
vp8dec_suite(void)287 vp8dec_suite (void)
288 {
289 Suite *s = suite_create ("vp8dec");
290 TCase *tc_chain = tcase_create ("general");
291
292 suite_add_tcase (s, tc_chain);
293
294 tcase_add_test (tc_chain, test_decode_simple);
295 tcase_add_test (tc_chain, test_decode_caps_change);
296 tcase_add_test (tc_chain, test_decode_invalid_resolution);
297
298 return s;
299 }
300
301 GST_CHECK_MAIN (vp8dec);
302