3 * Copyright (c) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
21 #include <gst/check/gstcheck.h>
23 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
26 GST_STATIC_CAPS ("video/x-raw, "
27 "format = (string) I420, "
28 "width = (int) [1, MAX], "
29 "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
31 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
34 GST_STATIC_CAPS ("video/x-raw, "
35 "format = (string) I420, "
36 "width = (int) [1, MAX], "
37 "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
39 static GstPad *sinkpad, *srcpad;
42 setup_vp8dec (const gchar * src_caps_str)
45 GstElement *vp8enc, *vp8dec;
46 GstCaps *srccaps = NULL;
48 GstPad *ghostpad, *targetpad;
51 srccaps = gst_caps_from_string (src_caps_str);
52 fail_unless (srccaps != NULL);
55 bin = gst_bin_new ("bin");
57 vp8enc = gst_check_setup_element ("vp8enc");
58 fail_unless (vp8enc != NULL);
59 vp8dec = gst_check_setup_element ("vp8dec");
60 fail_unless (vp8dec != NULL);
62 g_object_set (vp8enc, "name", "encoder", NULL);
63 g_object_set (vp8dec, "name", "decoder", NULL);
65 gst_bin_add_many (GST_BIN (bin), vp8enc, vp8dec, NULL);
66 fail_unless (gst_element_link_pads (vp8enc, "src", vp8dec, "sink"));
68 targetpad = gst_element_get_static_pad (vp8enc, "sink");
69 fail_unless (targetpad != NULL);
70 ghostpad = gst_ghost_pad_new ("sink", targetpad);
71 fail_unless (ghostpad != NULL);
72 gst_element_add_pad (bin, ghostpad);
73 gst_object_unref (targetpad);
75 targetpad = gst_element_get_static_pad (vp8dec, "src");
76 fail_unless (targetpad != NULL);
77 ghostpad = gst_ghost_pad_new ("src", targetpad);
78 fail_unless (ghostpad != NULL);
79 gst_element_add_pad (bin, ghostpad);
80 gst_object_unref (targetpad);
82 srcpad = gst_check_setup_src_pad (bin, &srctemplate);
83 sinkpad = gst_check_setup_sink_pad (bin, &sinktemplate);
84 gst_pad_set_active (srcpad, TRUE);
85 gst_pad_set_active (sinkpad, TRUE);
86 gst_check_setup_events (srcpad, bin, srccaps, GST_FORMAT_TIME);
89 gst_element_set_bus (bin, bus);
91 fail_unless (gst_element_set_state (bin,
92 GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
93 "could not set to playing");
96 gst_caps_unref (srccaps);
103 cleanup_vp8dec (GstElement * bin)
107 /* Free parsed buffers */
108 gst_check_drop_buffers ();
110 bus = GST_ELEMENT_BUS (bin);
111 gst_bus_set_flushing (bus, TRUE);
112 gst_object_unref (bus);
114 gst_pad_set_active (srcpad, FALSE);
115 gst_pad_set_active (sinkpad, FALSE);
117 gst_check_teardown_src_pad (bin);
118 gst_check_teardown_sink_pad (bin);
119 gst_check_teardown_element (bin);
123 _gst_vp8_test_check_output_caps (gint width, gint height, gint fps_n,
127 GstStructure *structure;
128 gint caps_w, caps_h, caps_fpsn, caps_fpsd;
130 caps = gst_pad_get_current_caps (sinkpad);
131 fail_unless (caps != NULL);
132 structure = gst_caps_get_structure (caps, 0);
134 fail_unless (gst_structure_get_int (structure, "width", &caps_w));
135 fail_unless (gst_structure_get_int (structure, "height", &caps_h));
136 fail_unless (gst_structure_get_fraction (structure, "framerate", &caps_fpsn,
139 fail_unless (width == caps_w);
140 fail_unless (height == caps_h);
141 fail_unless (fps_n == caps_fpsn);
142 fail_unless (fps_d == caps_fpsd);
144 gst_caps_unref (caps);
147 GST_START_TEST (test_decode_simple)
157 ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
159 gst_segment_init (&seg, GST_FORMAT_TIME);
160 seg.stop = gst_util_uint64_scale (20, GST_SECOND, 25);
161 fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
163 buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
164 gst_buffer_memset (buffer, 0, 0, -1);
166 for (i = 0; i < 20; i++) {
167 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
168 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
169 fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
172 gst_buffer_unref (buffer);
174 fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
176 /* All buffers must be there now */
177 fail_unless_equals_int (g_list_length (buffers), 20);
179 for (l = buffers, i = 0; l; l = l->next, i++) {
182 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (buffer),
183 gst_util_uint64_scale (i, GST_SECOND, 25));
184 fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
185 gst_util_uint64_scale (1, GST_SECOND, 25));
188 cleanup_vp8dec (bin);
194 GST_START_TEST (test_decode_caps_change)
204 ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
206 gst_segment_init (&seg, GST_FORMAT_TIME);
207 fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
209 buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
210 gst_buffer_memset (buffer, 0, 0, -1);
212 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (0, GST_SECOND, 25);
213 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
214 fail_unless (gst_pad_push (srcpad, buffer) == GST_FLOW_OK);
216 /* at this point, the output caps should be the same as the input */
217 _gst_vp8_test_check_output_caps (320, 240, 25, 1);
218 fail_unless_equals_int (g_list_length (buffers), 1);
219 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
222 /* now change the caps */
223 encoder = gst_bin_get_by_name (GST_BIN (bin), "encoder");
224 gst_element_set_state (encoder, GST_STATE_NULL);
225 gst_element_sync_state_with_parent (encoder);
226 gst_object_unref (encoder);
227 caps = gst_caps_from_string
228 ("video/x-raw,format=(string)I420,width=(int)64,"
229 "height=(int)32,framerate=(fraction)30/1");
230 fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
231 fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
232 buffer = gst_buffer_new_and_alloc (64 * 32 + 2 * 32 * 16);
233 gst_buffer_memset (buffer, 0, 0, -1);
234 gst_caps_unref (caps);
236 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (0, GST_SECOND, 30);
237 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 30);
238 fail_unless (gst_pad_push (srcpad, buffer) == GST_FLOW_OK);
240 /* at this point, the output caps should be the same as the input */
241 _gst_vp8_test_check_output_caps (64, 32, 30, 1);
242 fail_unless_equals_int (g_list_length (buffers), 1);
243 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
246 cleanup_vp8dec (bin);
255 Suite *s = suite_create ("vp8dec");
256 TCase *tc_chain = tcase_create ("general");
258 suite_add_tcase (s, tc_chain);
260 tcase_add_test (tc_chain, test_decode_simple);
261 tcase_add_test (tc_chain, test_decode_caps_change);
266 GST_CHECK_MAIN (vp8dec);