Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / tests / check / elements / vp8enc.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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, 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-vp8, "
27         "width = (int) [1, MAX], "
28         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
29
30 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
31     GST_PAD_SRC,
32     GST_PAD_ALWAYS,
33     GST_STATIC_CAPS ("video/x-raw-yuv, "
34         "format = (fourcc) I420, "
35         "width = (int) [1, MAX], "
36         "height = (int) [1, MAX], " "framerate = (fraction) [0, MAX]"));
37
38 static GstPad *sinkpad, *srcpad;
39
40 static GstElement *
41 setup_vp8enc (const gchar * src_caps_str)
42 {
43   GstElement *vp8enc;
44   GstCaps *srccaps = NULL;
45   GstBus *bus;
46
47   if (src_caps_str) {
48     srccaps = gst_caps_from_string (src_caps_str);
49     fail_unless (srccaps != NULL);
50   }
51
52   vp8enc = gst_check_setup_element ("vp8enc");
53   fail_unless (vp8enc != NULL);
54   srcpad = gst_check_setup_src_pad (vp8enc, &srctemplate, srccaps);
55   sinkpad = gst_check_setup_sink_pad (vp8enc, &sinktemplate, NULL);
56   gst_pad_set_active (srcpad, TRUE);
57   gst_pad_set_active (sinkpad, TRUE);
58
59   bus = gst_bus_new ();
60   gst_element_set_bus (vp8enc, bus);
61
62   fail_unless (gst_element_set_state (vp8enc,
63           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
64       "could not set to playing");
65
66   if (srccaps)
67     gst_caps_unref (srccaps);
68
69   buffers = NULL;
70   return vp8enc;
71 }
72
73 static void
74 cleanup_vp8enc (GstElement * vp8enc)
75 {
76   GstBus *bus;
77
78   /* Free parsed buffers */
79   gst_check_drop_buffers ();
80
81   bus = GST_ELEMENT_BUS (vp8enc);
82   gst_bus_set_flushing (bus, TRUE);
83   gst_object_unref (bus);
84
85   gst_pad_set_active (srcpad, FALSE);
86   gst_pad_set_active (sinkpad, FALSE);
87   gst_check_teardown_src_pad (vp8enc);
88   gst_check_teardown_sink_pad (vp8enc);
89   gst_check_teardown_element (vp8enc);
90 }
91
92 GST_START_TEST (test_encode_simple)
93 {
94   GstElement *vp8enc;
95   GstBuffer *buffer;
96   gint i;
97   GList *l;
98   GstCaps *outcaps;
99
100   vp8enc =
101       setup_vp8enc
102       ("video/x-raw-yuv,format=(fourcc)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1");
103
104   g_object_set (vp8enc, "max-latency", 5, NULL);
105
106   fail_unless (gst_pad_push_event (srcpad, gst_event_new_new_segment (FALSE,
107               1.0, GST_FORMAT_TIME, 0, gst_util_uint64_scale (20, GST_SECOND,
108                   25), 0)));
109
110   buffer = gst_buffer_new_and_alloc (320 * 240 + 2 * 160 * 120);
111   memset (GST_BUFFER_DATA (buffer), 0, GST_BUFFER_SIZE (buffer));
112   gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad));
113
114   for (i = 0; i < 20; i++) {
115     GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
116     GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
117     fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
118   }
119
120   gst_buffer_unref (buffer);
121
122   /* Only 5 buffers are allowed to be queued now */
123   fail_unless (g_list_length (buffers) > 15);
124
125   fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ()));
126
127
128   /* All buffers must be there now */
129   fail_unless_equals_int (g_list_length (buffers), 20);
130
131   outcaps =
132       gst_caps_from_string
133       ("video/x-vp8,width=(int)320,height=(int)240,framerate=(fraction)25/1");
134
135   for (l = buffers, i = 0; l; l = l->next, i++) {
136     buffer = l->data;
137
138     if (i == 0)
139       fail_if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT));
140
141     fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (buffer),
142         gst_util_uint64_scale (i, GST_SECOND, 25));
143     fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
144         gst_util_uint64_scale (1, GST_SECOND, 25));
145
146     fail_unless (gst_caps_can_intersect (GST_BUFFER_CAPS (buffer), outcaps));
147   }
148
149   gst_caps_unref (outcaps);
150
151   cleanup_vp8enc (vp8enc);
152 }
153
154 GST_END_TEST;
155
156 static Suite *
157 vp8enc_suite (void)
158 {
159   Suite *s = suite_create ("vp8enc");
160   TCase *tc_chain = tcase_create ("general");
161
162   suite_add_tcase (s, tc_chain);
163
164   tcase_add_test (tc_chain, test_encode_simple);
165
166   return s;
167 }
168
169 GST_CHECK_MAIN (vp8enc);