1 /* GStreamer
2 *
3 * Copyright (c) 2016 Stian Selnes <stian@pexip.com>
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 #include <gst/check/gstharness.h>
21 #include <gst/check/gstcheck.h>
22 #include <gst/video/video.h>
23
24 #define gst_caps_new_i420(w, h) gst_caps_new_i420_full (w, h, 30, 1, 1, 1)
25 static GstCaps *
gst_caps_new_i420_full(gint width,gint height,gint fps_n,gint fps_d,gint par_n,gint par_d)26 gst_caps_new_i420_full (gint width, gint height, gint fps_n, gint fps_d,
27 gint par_n, gint par_d)
28 {
29 GstVideoInfo info;
30 gst_video_info_init (&info);
31 gst_video_info_set_format (&info, GST_VIDEO_FORMAT_I420, width, height);
32 GST_VIDEO_INFO_FPS_N (&info) = fps_n;
33 GST_VIDEO_INFO_FPS_D (&info) = fps_d;
34 GST_VIDEO_INFO_PAR_N (&info) = par_n;
35 GST_VIDEO_INFO_PAR_D (&info) = par_d;
36 return gst_video_info_to_caps (&info);
37 }
38
GST_START_TEST(test_encode_lag_in_frames)39 GST_START_TEST (test_encode_lag_in_frames)
40 {
41 GstHarness *h = gst_harness_new_parse ("vp9enc lag-in-frames=5 cpu-used=8 "
42 "deadline=1");
43 gint i;
44
45 gst_harness_add_src_parse (h, "videotestsrc is-live=true pattern=black ! "
46 "capsfilter caps=\"video/x-raw,format=I420,width=320,height=240,framerate=25/1\"",
47 TRUE);
48
49 /* Push 20 buffers into the encoder */
50 fail_unless_equals_int (GST_FLOW_OK,
51 gst_harness_src_crank_and_push_many (h, 20, 20));
52
53 /* Only 5 buffers are allowed to be queued now */
54 fail_unless (gst_harness_buffers_received (h) > 15);
55
56 /* EOS will cause the remaining buffers to be drained */
57 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
58 fail_unless_equals_int (gst_harness_buffers_received (h), 20);
59
60 for (i = 0; i < 20; i++) {
61 GstBuffer *buffer = gst_harness_pull (h);
62
63 if (i == 0)
64 fail_if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT));
65
66 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (buffer),
67 gst_util_uint64_scale (i, GST_SECOND, 25));
68 fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer),
69 gst_util_uint64_scale (1, GST_SECOND, 25));
70
71 gst_buffer_unref (buffer);
72 }
73
74 gst_harness_teardown (h);
75 }
76
77 GST_END_TEST;
78
79
GST_START_TEST(test_autobitrate_changes_with_caps)80 GST_START_TEST (test_autobitrate_changes_with_caps)
81 {
82 gint bitrate = 0;
83 GstHarness *h = gst_harness_new ("vp9enc");
84 gst_harness_set_src_caps (h, gst_caps_new_i420_full (1280, 720, 30, 1, 1, 1));
85
86 /* Default settings for 720p @ 30fps ~0.8Mbps */
87 g_object_get (h->element, "target-bitrate", &bitrate, NULL);
88 fail_unless_equals_int (bitrate, 799000);
89
90 /* Change bits-per-pixel 0.036 to give us ~1Mbps */
91 g_object_set (h->element, "bits-per-pixel", 0.037, NULL);
92 g_object_get (h->element, "target-bitrate", &bitrate, NULL);
93 fail_unless_equals_int (bitrate, 1022000);
94
95 /* Halving the framerate should halve the auto bitrate */
96 gst_harness_set_src_caps (h, gst_caps_new_i420_full (1280, 720, 15, 1, 1, 1));
97 g_object_get (h->element, "target-bitrate", &bitrate, NULL);
98 fail_unless_equals_int (bitrate, 511000);
99
100 /* Halving the resolution should quarter the auto bitrate */
101 gst_harness_set_src_caps (h, gst_caps_new_i420_full (640, 360, 15, 1, 1, 1));
102 g_object_get (h->element, "target-bitrate", &bitrate, NULL);
103 fail_unless_equals_int (bitrate, 127000);
104
105 gst_harness_teardown (h);
106 }
107
108 GST_END_TEST;
109
110 static Suite *
vp9enc_suite(void)111 vp9enc_suite (void)
112 {
113 Suite *s = suite_create ("vp9enc");
114 TCase *tc_chain = tcase_create ("general");
115
116 suite_add_tcase (s, tc_chain);
117
118 tcase_add_test (tc_chain, test_encode_lag_in_frames);
119 tcase_add_test (tc_chain, test_autobitrate_changes_with_caps);
120
121 return s;
122 }
123
124 GST_CHECK_MAIN (vp9enc);
125