0ebd70ac296a898f42af742c83fc0cb28736f907
[platform/upstream/gstreamer.git] / tests / check / elements / vp8dec.c
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
23 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
24     GST_PAD_SINK,
25     GST_PAD_ALWAYS,
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]"));
30
31 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
32     GST_PAD_SRC,
33     GST_PAD_ALWAYS,
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]"));
38
39 static GstPad *sinkpad, *srcpad;
40
41 static GstElement *
42 setup_vp8dec (const gchar * src_caps_str)
43 {
44   GstElement *bin;
45   GstElement *vp8enc, *vp8dec;
46   GstCaps *srccaps = NULL;
47   GstBus *bus;
48   GstPad *ghostpad, *targetpad;
49
50   if (src_caps_str) {
51     srccaps = gst_caps_from_string (src_caps_str);
52     fail_unless (srccaps != NULL);
53   }
54
55   bin = gst_bin_new ("bin");
56
57   vp8enc = gst_check_setup_element ("vp8enc");
58   fail_unless (vp8enc != NULL);
59   vp8dec = gst_check_setup_element ("vp8dec");
60   fail_unless (vp8dec != NULL);
61
62   g_object_set (vp8enc, "name", "encoder", NULL);
63   g_object_set (vp8dec, "name", "decoder", NULL);
64
65   gst_bin_add_many (GST_BIN (bin), vp8enc, vp8dec, NULL);
66   fail_unless (gst_element_link_pads (vp8enc, "src", vp8dec, "sink"));
67
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);
74
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);
81
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);
87
88   bus = gst_bus_new ();
89   gst_element_set_bus (bin, bus);
90
91   fail_unless (gst_element_set_state (bin,
92           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
93       "could not set to playing");
94
95   if (srccaps)
96     gst_caps_unref (srccaps);
97
98   buffers = NULL;
99   return bin;
100 }
101
102 static void
103 cleanup_vp8dec (GstElement * bin)
104 {
105   GstBus *bus;
106
107   /* Free parsed buffers */
108   gst_check_drop_buffers ();
109
110   bus = GST_ELEMENT_BUS (bin);
111   gst_bus_set_flushing (bus, TRUE);
112   gst_object_unref (bus);
113
114   gst_pad_set_active (srcpad, FALSE);
115   gst_pad_set_active (sinkpad, FALSE);
116
117   gst_check_teardown_src_pad (bin);
118   gst_check_teardown_sink_pad (bin);
119   gst_check_teardown_element (bin);
120 }
121
122 static void
123 _gst_vp8_test_check_output_caps (gint width, gint height, gint fps_n,
124     gint fps_d)
125 {
126   GstCaps *caps;
127   GstStructure *structure;
128   gint caps_w, caps_h, caps_fpsn, caps_fpsd;
129
130   caps = gst_pad_get_current_caps (sinkpad);
131   fail_unless (caps != NULL);
132   structure = gst_caps_get_structure (caps, 0);
133
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,
137           &caps_fpsd));
138
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);
143
144   gst_caps_unref (caps);
145 }
146
147 GST_START_TEST (test_decode_simple)
148 {
149   GstElement *bin;
150   GstBuffer *buffer;
151   gint i;
152   GList *l;
153   GstSegment seg;
154
155   bin =
156       setup_vp8dec
157       ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
158
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)));
162
163   buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
164   gst_buffer_memset (buffer, 0, 0, -1);
165
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);
170   }
171
172   gst_buffer_unref (buffer);
173
174   fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
175
176   /* All buffers must be there now */
177   fail_unless_equals_int (g_list_length (buffers), 20);
178
179   for (l = buffers, i = 0; l; l = l->next, i++) {
180     buffer = l->data;
181
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));
186   }
187
188   cleanup_vp8dec (bin);
189 }
190
191 GST_END_TEST;
192
193
194 GST_START_TEST (test_decode_caps_change)
195 {
196   GstElement *bin;
197   GstBuffer *buffer;
198   GstSegment seg;
199   GstElement *encoder;
200   GstCaps *caps;
201
202   bin =
203       setup_vp8dec
204       ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
205
206   gst_segment_init (&seg, GST_FORMAT_TIME);
207   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg)));
208
209   buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
210   gst_buffer_memset (buffer, 0, 0, -1);
211
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);
215
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);
220   buffers = NULL;
221
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);
235
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);
239
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);
244   buffers = NULL;
245
246   cleanup_vp8dec (bin);
247 }
248
249 GST_END_TEST;
250
251
252 static Suite *
253 vp8dec_suite (void)
254 {
255   Suite *s = suite_create ("vp8dec");
256   TCase *tc_chain = tcase_create ("general");
257
258   suite_add_tcase (s, tc_chain);
259
260   tcase_add_test (tc_chain, test_decode_simple);
261   tcase_add_test (tc_chain, test_decode_caps_change);
262
263   return s;
264 }
265
266 GST_CHECK_MAIN (vp8dec);