• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
22 
23 #include <gst/check/gstharness.h>
24 #include <gst/check/gstcheck.h>
25 #include <gst/video/video.h>
26 
27 #define gst_caps_new_i420(w, h) gst_caps_new_i420_full (w, h, 30, 1, 1, 1)
28 static GstCaps *
gst_caps_new_i420_full(gint width,gint height,gint fps_n,gint fps_d,gint par_n,gint par_d)29 gst_caps_new_i420_full (gint width, gint height, gint fps_n, gint fps_d,
30     gint par_n, gint par_d)
31 {
32   GstVideoInfo info;
33   gst_video_info_init (&info);
34   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_I420, width, height);
35   GST_VIDEO_INFO_FPS_N (&info) = fps_n;
36   GST_VIDEO_INFO_FPS_D (&info) = fps_d;
37   GST_VIDEO_INFO_PAR_N (&info) = par_n;
38   GST_VIDEO_INFO_PAR_D (&info) = par_d;
39   return gst_video_info_to_caps (&info);
40 }
41 
42 static GstBuffer *
gst_harness_create_video_buffer_from_info(GstHarness * h,gint value,GstVideoInfo * info,GstClockTime timestamp,GstClockTime duration)43 gst_harness_create_video_buffer_from_info (GstHarness * h, gint value,
44     GstVideoInfo * info, GstClockTime timestamp, GstClockTime duration)
45 {
46   GstBuffer *buf;
47   gsize size;
48 
49   size = GST_VIDEO_INFO_SIZE (info);
50 
51   buf = gst_harness_create_buffer (h, size);
52   gst_buffer_memset (buf, 0, value, size);
53   g_assert (buf != NULL);
54 
55   gst_buffer_add_video_meta_full (buf,
56       GST_VIDEO_FRAME_FLAG_NONE,
57       GST_VIDEO_INFO_FORMAT (info),
58       GST_VIDEO_INFO_WIDTH (info),
59       GST_VIDEO_INFO_HEIGHT (info),
60       GST_VIDEO_INFO_N_PLANES (info), info->offset, info->stride);
61 
62   GST_BUFFER_PTS (buf) = timestamp;
63   GST_BUFFER_DURATION (buf) = duration;
64 
65   return buf;
66 }
67 
68 static GstBuffer *
gst_harness_create_video_buffer_full(GstHarness * h,gint value,guint width,guint height,GstClockTime timestamp,GstClockTime duration)69 gst_harness_create_video_buffer_full (GstHarness * h, gint value,
70     guint width, guint height, GstClockTime timestamp, GstClockTime duration)
71 {
72   GstVideoInfo info;
73 
74   gst_video_info_init (&info);
75   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_I420, width, height);
76 
77   return gst_harness_create_video_buffer_from_info (h, value, &info,
78       timestamp, duration);
79 }
80 
GST_START_TEST(test_encode_simple)81 GST_START_TEST (test_encode_simple)
82 {
83   gint i;
84   GstHarness *h = gst_harness_new ("vp8enc");
85   gst_harness_set_src_caps (h, gst_caps_new_i420_full (320, 240, 25, 1, 1, 1));
86 
87   for (i = 0; i < 20; i++) {
88     GstBuffer *buffer = gst_harness_create_video_buffer_full (h, 0x0,
89         320, 240, gst_util_uint64_scale (i, GST_SECOND, 25),
90         gst_util_uint64_scale (1, GST_SECOND, 25));
91     fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, buffer));
92   }
93 
94   for (i = 0; i < 20; i++) {
95     GstBuffer *buffer = gst_harness_pull (h);
96 
97     if (i == 0)
98       fail_if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT));
99 
100     fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (buffer),
101         gst_util_uint64_scale (i, GST_SECOND, 25));
102     fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
103         gst_util_uint64_scale (1, GST_SECOND, 25));
104 
105     gst_buffer_unref (buffer);
106   }
107 
108   gst_harness_teardown (h);
109 }
110 
111 GST_END_TEST;
112 
GST_START_TEST(test_encode_lag_in_frames)113 GST_START_TEST (test_encode_lag_in_frames)
114 {
115   GstFlowReturn ret;
116   GstBuffer *buffer;
117   GstSegment seg;
118   GstHarness *h = gst_harness_new ("vp8enc");
119   g_object_set (h->element, "lag-in-frames", 5, NULL);
120   gst_harness_set_src_caps (h, gst_caps_new_i420_full (320, 240, 25, 1, 1, 1));
121 
122   gst_segment_init (&seg, GST_FORMAT_TIME);
123   seg.stop = gst_util_uint64_scale (20, GST_SECOND, 25);
124   fail_unless (gst_harness_push_event (h, gst_event_new_segment (&seg)));
125 
126   buffer = gst_harness_create_video_buffer_full (h, 0x0,
127       320, 240, gst_util_uint64_scale (0, GST_SECOND, 25),
128       gst_util_uint64_scale (1, GST_SECOND, 25));
129 
130   ret = gst_harness_push (h, gst_buffer_ref (buffer));
131   /* If libvpx was built with CONFIG_REALTIME_ONLY, then we'll receive
132    * GST_FLOW_NOT_NEGOTIATED. Accept this, and skip the rest of this test
133    * in that case. */
134   fail_unless (ret == GST_FLOW_OK || ret == GST_FLOW_NOT_NEGOTIATED);
135 
136   if (ret == GST_FLOW_OK) {
137     gint i;
138 
139     for (i = 1; i < 20; i++) {
140       GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25);
141       GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25);
142       fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h,
143               gst_buffer_ref (buffer)));
144     }
145 
146     fail_unless_equals_int (20, gst_harness_buffers_received (h));
147 
148     for (i = 0; i < 20; i++) {
149       GstBuffer *outbuf = gst_harness_pull (h);
150 
151       if (i == 0)
152         fail_if (GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT));
153 
154       fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuf),
155           gst_util_uint64_scale (i, GST_SECOND, 25));
156       fail_unless_equals_uint64 (GST_BUFFER_DURATION (outbuf),
157           gst_util_uint64_scale (1, GST_SECOND, 25));
158 
159       gst_buffer_unref (outbuf);
160     }
161   }
162 
163   gst_buffer_unref (buffer);
164 
165   gst_harness_teardown (h);
166 }
167 
168 GST_END_TEST;
169 
GST_START_TEST(test_encode_simple_when_bitrate_set_to_zero)170 GST_START_TEST (test_encode_simple_when_bitrate_set_to_zero)
171 {
172   GstHarness *h = gst_harness_new_parse ("vp8enc target-bitrate=0");
173   GstBuffer *buf;
174 
175   gst_harness_set_src_caps (h, gst_caps_new_i420 (320, 240));
176 
177   buf = gst_harness_create_video_buffer_full (h, 0x42,
178       320, 240, 0, gst_util_uint64_scale (GST_SECOND, 1, 30));
179   gst_harness_push (h, buf);
180   gst_buffer_unref (gst_harness_pull (h));
181   gst_harness_teardown (h);
182 }
183 
184 GST_END_TEST;
185 
GST_START_TEST(test_autobitrate_changes_with_caps)186 GST_START_TEST (test_autobitrate_changes_with_caps)
187 {
188   gint bitrate = 0;
189   GstHarness *h = gst_harness_new ("vp8enc");
190   gst_harness_set_src_caps (h, gst_caps_new_i420_full (1280, 720, 30, 1, 1, 1));
191 
192   /* Default settings for 720p @ 30fps ~1.2Mbps */
193   g_object_get (h->element, "target-bitrate", &bitrate, NULL);
194   fail_unless_equals_int (bitrate, 1199000);
195 
196   /* Change bits-per-pixel 0.036 to give us ~1Mbps */
197   g_object_set (h->element, "bits-per-pixel", 0.037, NULL);
198   g_object_get (h->element, "target-bitrate", &bitrate, NULL);
199   fail_unless_equals_int (bitrate, 1022000);
200 
201   /* Halving the framerate should halve the auto bitrate */
202   gst_harness_set_src_caps (h, gst_caps_new_i420_full (1280, 720, 15, 1, 1, 1));
203   g_object_get (h->element, "target-bitrate", &bitrate, NULL);
204   fail_unless_equals_int (bitrate, 511000);
205 
206   /* Halving the resolution should quarter the auto bitrate */
207   gst_harness_set_src_caps (h, gst_caps_new_i420_full (640, 360, 15, 1, 1, 1));
208   g_object_get (h->element, "target-bitrate", &bitrate, NULL);
209   fail_unless_equals_int (bitrate, 127000);
210 
211   gst_harness_teardown (h);
212 }
213 
214 GST_END_TEST;
215 
216 #define verify_meta(buffer, usets, ybit, tid, tl0picindex)               \
217   G_STMT_START {                                                         \
218     gboolean use_temporal_scaling, layer_sync;                           \
219     guint temporal_layer_id, tl0picidx;                                  \
220     GstCustomMeta *meta = gst_buffer_get_custom_meta (buffer,            \
221         "GstVP8Meta");                                                   \
222     GstStructure *s;                                                     \
223     fail_unless (meta != NULL);                                          \
224     s = gst_custom_meta_get_structure (meta);                            \
225     fail_unless (gst_structure_get (s,                                   \
226           "use-temporal-scaling", G_TYPE_BOOLEAN, &use_temporal_scaling, \
227           "layer-sync", G_TYPE_BOOLEAN, &layer_sync,                     \
228           "layer-id", G_TYPE_UINT, &temporal_layer_id,                   \
229           "tl0picidx", G_TYPE_UINT, &tl0picidx, NULL));                  \
230     fail_unless_equals_int (usets, use_temporal_scaling);                \
231     fail_unless_equals_int (ybit, layer_sync);                           \
232     fail_unless_equals_int (tid, temporal_layer_id);                     \
233     fail_unless_equals_int (tl0picindex, tl0picidx);                     \
234   } G_STMT_END
235 
236 static void
configure_vp8ts(GstHarness * h)237 configure_vp8ts (GstHarness * h)
238 {
239   gint i;
240   GValue layer_sync_flags = G_VALUE_INIT;
241   GValueArray *decimators = g_value_array_new (3);
242   GValueArray *layer_ids = g_value_array_new (4);
243   GValueArray *bitrates = g_value_array_new (3);
244   GValue ival = { 0, }, bval = {
245   0,};
246 
247   gst_value_array_init (&layer_sync_flags, 8);
248 
249   g_value_init (&ival, G_TYPE_INT);
250   for (i = 0; i < 3; i++) {
251     /* 7.5, 15, 30fps */
252     static const gint d[] = { 4, 2, 1 };
253     g_value_set_int (&ival, d[i]);
254     g_value_array_append (decimators, &ival);
255   }
256 
257   for (i = 0; i < 4; i++) {
258     static const gint d[] = { 0, 2, 1, 2 };
259     g_value_set_int (&ival, d[i]);
260     g_value_array_append (layer_ids, &ival);
261   }
262 
263   for (i = 0; i < 3; i++) {
264     /* Split 512kbps 40%, 20%, 40% */
265     static const gint d[] = { 204800, 307200, 512000 };
266     g_value_set_int (&ival, d[i]);
267     g_value_array_append (bitrates, &ival);
268   }
269 
270   gst_util_set_object_arg (G_OBJECT (h->element),
271       "temporal-scalability-layer-flags",
272       /* layer 0 */
273       "<no-ref-golden+no-upd-golden+no-upd-alt,"
274       /* layer 2 (sync) */
275       "no-ref-golden+no-upd-last+no-upd-golden+no-upd-alt+no-upd-entropy,"
276       /* layer 1 (sync) */
277       "no-ref-golden+no-upd-last+no-upd-alt,"
278       /* layer 2 */
279       "no-upd-last+no-upd-golden+no-upd-alt+no-upd-entropy,"
280       /* layer 0 */
281       "no-ref-golden+no-upd-golden+no-upd-alt,"
282       /* layer 2 */
283       "no-upd-last+no-upd-golden+no-upd-alt+no-upd-entropy,"
284       /* layer 1 */
285       "no-upd-last+no-upd-alt,"
286       /* layer 2 */
287       "no-upd-last+no-upd-golden+no-upd-alt+no-upd-entropy>");
288 
289   g_value_init (&bval, G_TYPE_BOOLEAN);
290   for (i = 0; i < 8; i++) {
291     /* Reflect pattern above */
292     static const gboolean d[] = {
293       FALSE,
294       TRUE,
295       TRUE,
296       FALSE,
297       FALSE,
298       FALSE,
299       FALSE,
300       FALSE
301     };
302     g_value_set_boolean (&bval, d[i]);
303     gst_value_array_append_value (&layer_sync_flags, &bval);
304   }
305 
306   g_object_set_property (G_OBJECT (h->element),
307       "temporal-scalability-layer-sync-flags", &layer_sync_flags);
308 
309   g_object_set (h->element,
310       "temporal-scalability-number-layers", decimators->n_values,
311       "temporal-scalability-periodicity", layer_ids->n_values,
312       "temporal-scalability-rate-decimator", decimators,
313       "temporal-scalability-layer-id", layer_ids,
314       "temporal-scalability-target-bitrate", bitrates,
315       "error-resilient", 1, NULL);
316 
317   g_value_array_free (decimators);
318   g_value_array_free (layer_ids);
319   g_value_array_free (bitrates);
320   g_value_unset (&layer_sync_flags);
321 }
322 
GST_START_TEST(test_encode_temporally_scaled)323 GST_START_TEST (test_encode_temporally_scaled)
324 {
325   gint i;
326   struct
327   {
328     gboolean ybit;
329     gint tid;
330     gint tl0picidx;
331     gboolean droppable;
332   } expected[] = {
333     {
334     TRUE, 0, 1, FALSE},         /* This is an intra */
335     {
336     TRUE, 2, 1, TRUE}, {
337     TRUE, 1, 1, FALSE}, {
338     FALSE, 2, 1, TRUE}, {
339     FALSE, 0, 2, FALSE}, {
340     FALSE, 2, 2, TRUE}, {
341     FALSE, 1, 2, FALSE}, {
342     FALSE, 2, 2, TRUE}, {
343     FALSE, 0, 3, FALSE}, {
344     TRUE, 2, 3, TRUE}, {
345     TRUE, 1, 3, FALSE}, {
346     FALSE, 2, 3, TRUE}, {
347     FALSE, 0, 4, FALSE}, {
348     FALSE, 2, 4, TRUE}, {
349     FALSE, 1, 4, FALSE}, {
350   FALSE, 2, 4, TRUE},};
351   GstHarness *h = gst_harness_new ("vp8enc");
352   gst_harness_set_src_caps (h, gst_caps_new_i420 (320, 240));
353   configure_vp8ts (h);
354 
355   for (i = 0; i < 16; i++) {
356     GstBuffer *in, *out;
357 
358     in = gst_harness_create_video_buffer_full (h, 0x42,
359         320, 240, gst_util_uint64_scale (i, GST_SECOND, 30),
360         gst_util_uint64_scale (1, GST_SECOND, 30));
361     gst_harness_push (h, in);
362 
363     out = gst_harness_pull (h);
364     /* Ensure first frame is encoded as an intra */
365     if (i == 0)
366       fail_if (GST_BUFFER_FLAG_IS_SET (out, GST_BUFFER_FLAG_DELTA_UNIT));
367     else
368       fail_unless (GST_BUFFER_FLAG_IS_SET (out, GST_BUFFER_FLAG_DELTA_UNIT));
369     fail_unless_equals_int (expected[i].droppable,
370         GST_BUFFER_FLAG_IS_SET (out, GST_BUFFER_FLAG_DROPPABLE));
371     verify_meta (out, TRUE, expected[i].ybit, expected[i].tid,
372         expected[i].tl0picidx);
373     gst_buffer_unref (out);
374   }
375 
376   gst_harness_teardown (h);
377 }
378 
379 GST_END_TEST;
380 
GST_START_TEST(test_encode_fresh_meta)381 GST_START_TEST (test_encode_fresh_meta)
382 {
383   gint i;
384   GstBuffer *buffer;
385   GstHarness *h = gst_harness_new ("vp8enc");
386   GstCustomMeta *meta;
387   GstStructure *s;
388   gst_harness_set_src_caps (h, gst_caps_new_i420_full (320, 240, 25, 1, 1, 1));
389 
390   buffer = gst_harness_create_video_buffer_full (h, 0x0,
391       320, 240, gst_util_uint64_scale (0, GST_SECOND, 25),
392       gst_util_uint64_scale (1, GST_SECOND, 25));
393 
394   /* Attach bogus meta to input buffer */
395   meta = gst_buffer_add_custom_meta (buffer, "GstVP8Meta");
396   s = gst_custom_meta_get_structure (meta);
397   gst_structure_set (s,
398       "use-temporal-scaling", G_TYPE_BOOLEAN, FALSE,
399       "layer-sync", G_TYPE_BOOLEAN, FALSE,
400       "layer-id", G_TYPE_UINT, 0, "tl0picidx", G_TYPE_UINT, 0, NULL);
401 
402   for (i = 0; i < 2; i++) {
403     GstBuffer *out;
404 
405     fail_unless_equals_int (GST_FLOW_OK,
406         gst_harness_push (h, gst_buffer_ref (buffer)));
407 
408     out = gst_harness_pull (h);
409     /* Ensure that output buffer has fresh meta */
410     verify_meta (out, FALSE, (i == 0), 0, i + 1);
411     gst_buffer_unref (out);
412   }
413 
414   gst_buffer_unref (buffer);
415 
416   gst_harness_teardown (h);
417 }
418 
419 GST_END_TEST;
420 
421 static Suite *
vp8enc_suite(void)422 vp8enc_suite (void)
423 {
424   Suite *s = suite_create ("vp8enc");
425   TCase *tc_chain = tcase_create ("general");
426 
427   suite_add_tcase (s, tc_chain);
428 
429   tcase_add_test (tc_chain, test_encode_simple);
430   tcase_add_test (tc_chain, test_encode_lag_in_frames);
431   tcase_add_test (tc_chain, test_encode_simple_when_bitrate_set_to_zero);
432   tcase_add_test (tc_chain, test_autobitrate_changes_with_caps);
433   tcase_add_test (tc_chain, test_encode_temporally_scaled);
434   tcase_add_test (tc_chain, test_encode_fresh_meta);
435 
436   return s;
437 }
438 
439 GST_CHECK_MAIN (vp8enc);
440