Couple more g_memdup() -> g_memdup2() fixes
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / tests / check / elements / rtphdrext-colorspace.c
1 /* GStreamer
2  *
3  * unit test for rtphdrext-colorspace elements
4  *
5  * Copyright (C) 2020-2021 Collabora Ltd.
6  *   @author: Jakub Adam <jakub.adam@collabora.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include <gst/check/gstcheck.h>
25 #include <gst/check/gstharness.h>
26 #include <gst/video/video-color.h>
27 #include <gst/video/video-hdr.h>
28 #include <gst/rtp/gstrtphdrext-colorspace.h>
29
30 #define EXTMAP_ID 9
31
32 const GstVideoColorimetry expected_colorimetry = {
33   GST_VIDEO_COLOR_RANGE_0_255,
34   GST_VIDEO_COLOR_MATRIX_BT601,
35   GST_VIDEO_TRANSFER_BT2020_10,
36   GST_VIDEO_COLOR_PRIMARIES_BT2020
37 };
38
39 const GstVideoChromaSite expected_chroma_site = GST_VIDEO_CHROMA_SITE_MPEG2;
40
41 const GstVideoMasteringDisplayInfo expected_display_info = {
42   {{1, 2}, {3, 4}, {5, 6}},
43   {7, 8},
44   10000,
45   42
46 };
47
48 const GstVideoContentLightLevel expected_content_light_level = {
49   35987, 28543
50 };
51
52 const guint8 vp8_payload[] = {
53   0x30, 0x00, 0x00, 0x9d, 0x01, 0x2a, 0xb0, 0x00, 0x90, 0x00, 0x06, 0x47,
54   0x08, 0x85, 0x85, 0x88, 0x99, 0x84, 0x88, 0x21, 0x00
55 };
56
57 /* validate that upstream colorspace information get embedded into RTP packets
58  * as Color Space header extension and correctly reconstructed in depayloader's
59  * srccaps. This variant of the test case creates the one-byte form of the
60  * header (without HDR metadata).
61  */
62 GST_START_TEST (test_rtphdrext_colorspace_onebyte)
63 {
64   GstHarness *h;
65   GstElement *pay, *depay;
66   GstVideoColorimetry colorimetry;
67   GstVideoChromaSite chroma_site;
68   gchar *colorimetry_str;
69   const gchar *str;
70   GstCaps *src_caps, *caps, *expected_caps;
71   GstPad *pad;
72   GstStructure *s;
73   GstRTPHeaderExtension *pay_ext, *depay_ext;
74
75   h = gst_harness_new_parse ("rtpvp8pay ! rtpvp8depay");
76
77   pay = gst_harness_find_element (h, "rtpvp8pay");
78   depay = gst_harness_find_element (h, "rtpvp8depay");
79
80   pay_ext =
81       GST_RTP_HEADER_EXTENSION (gst_element_factory_make ("rtphdrextcolorspace",
82           NULL));
83   depay_ext =
84       GST_RTP_HEADER_EXTENSION (gst_element_factory_make ("rtphdrextcolorspace",
85           NULL));
86
87   gst_rtp_header_extension_set_id (pay_ext, EXTMAP_ID);
88   gst_rtp_header_extension_set_id (depay_ext, EXTMAP_ID);
89
90   g_signal_emit_by_name (pay, "add-extension", pay_ext);
91   g_signal_emit_by_name (depay, "add-extension", depay_ext);
92
93   colorimetry_str = gst_video_colorimetry_to_string (&expected_colorimetry);
94   src_caps = gst_caps_new_simple ("video/x-vp8",
95       "colorimetry", G_TYPE_STRING, colorimetry_str,
96       "chroma-site", G_TYPE_STRING,
97       gst_video_chroma_to_string (expected_chroma_site), NULL);
98
99   gst_harness_set_src_caps (h, src_caps);
100
101   gst_harness_push (h,
102       gst_buffer_new_memdup (vp8_payload, sizeof (vp8_payload)));
103
104   /* verify depayloader correctly reconstructs colorspace information in
105    * its srccaps. */
106   pad = gst_element_get_static_pad (depay, "src");
107   caps = gst_pad_get_current_caps (pad);
108   s = gst_caps_get_structure (caps, 0);
109   gst_object_unref (pad);
110
111   str = gst_structure_get_string (s, "colorimetry");
112   fail_unless (str != NULL);
113   gst_video_colorimetry_from_string (&colorimetry, str);
114   fail_unless (gst_video_colorimetry_is_equal (&colorimetry,
115           &expected_colorimetry));
116
117   str = gst_structure_get_string (s, "chroma-site");
118   fail_unless (str != NULL);
119   chroma_site = gst_video_chroma_from_string (str);
120   fail_unless_equals_int (chroma_site, expected_chroma_site);
121
122   gst_caps_unref (caps);
123
124   /* verify the presence of Color Space extmap in caps */
125   pad = gst_element_get_static_pad (pay, "src");
126   caps = gst_pad_get_current_caps (pad);
127   expected_caps = gst_caps_from_string ("application/x-rtp, "
128       "extmap-" G_STRINGIFY (EXTMAP_ID) "=" GST_RTP_HDREXT_COLORSPACE_URI);
129   fail_unless (gst_caps_is_subset (caps, expected_caps));
130   gst_object_unref (pad);
131   gst_caps_unref (caps);
132   gst_caps_unref (expected_caps);
133
134   g_free (colorimetry_str);
135
136   gst_object_unref (pay_ext);
137   gst_object_unref (depay_ext);
138   gst_object_unref (pay);
139   gst_object_unref (depay);
140   gst_harness_teardown (h);
141 }
142
143 GST_END_TEST;
144
145 /* validate that upstream colorspace information get embedded into RTP packets
146  * as Color Space header extension and correctly reconstructed in depayloader's
147  * srccaps. This variant of the test case creates the two-byte form of the
148  * header (including HDR metadata).
149  */
150 GST_START_TEST (test_rtphdrext_colorspace_twobyte)
151 {
152   GstHarness *h;
153   GstElement *pay, *depay;
154   GstVideoColorimetry colorimetry;
155   GstVideoChromaSite chroma_site;
156   GstVideoMasteringDisplayInfo display_info;
157   GstVideoContentLightLevel content_light_level;
158   gchar *colorimetry_str;
159   const gchar *str;
160   GstCaps *src_caps, *caps, *expected_caps;
161   GstPad *pad;
162   GstStructure *s;
163   GstRTPHeaderExtension *pay_ext, *depay_ext;
164
165   h = gst_harness_new_parse ("rtpvp8pay ! rtpvp8depay");
166
167   pay = gst_harness_find_element (h, "rtpvp8pay");
168   depay = gst_harness_find_element (h, "rtpvp8depay");
169
170   pay_ext =
171       GST_RTP_HEADER_EXTENSION (gst_element_factory_make ("rtphdrextcolorspace",
172           NULL));
173   depay_ext =
174       GST_RTP_HEADER_EXTENSION (gst_element_factory_make ("rtphdrextcolorspace",
175           NULL));
176
177   gst_rtp_header_extension_set_id (pay_ext, EXTMAP_ID);
178   gst_rtp_header_extension_set_id (depay_ext, EXTMAP_ID);
179
180   g_signal_emit_by_name (pay, "add-extension", pay_ext);
181   g_signal_emit_by_name (depay, "add-extension", depay_ext);
182
183   colorimetry_str = gst_video_colorimetry_to_string (&expected_colorimetry);
184   src_caps = gst_caps_new_simple ("video/x-vp8",
185       "colorimetry", G_TYPE_STRING, colorimetry_str,
186       "chroma-site", G_TYPE_STRING,
187       gst_video_chroma_to_string (expected_chroma_site), NULL);
188   gst_video_mastering_display_info_add_to_caps (&expected_display_info,
189       src_caps);
190   gst_video_content_light_level_add_to_caps (&expected_content_light_level,
191       src_caps);
192
193   gst_harness_set_src_caps (h, src_caps);
194
195   gst_harness_push (h,
196       gst_buffer_new_memdup (vp8_payload, sizeof (vp8_payload)));
197
198   /* verify depayloader correctly reconstructs colorspace information in
199    * its srccaps. */
200   pad = gst_element_get_static_pad (depay, "src");
201   caps = gst_pad_get_current_caps (pad);
202   s = gst_caps_get_structure (caps, 0);
203   gst_object_unref (pad);
204
205   str = gst_structure_get_string (s, "colorimetry");
206   fail_unless (str != NULL);
207   gst_video_colorimetry_from_string (&colorimetry, str);
208   fail_unless (gst_video_colorimetry_is_equal (&colorimetry,
209           &expected_colorimetry));
210
211   str = gst_structure_get_string (s, "chroma-site");
212   fail_unless (str != NULL);
213   chroma_site = gst_video_chroma_from_string (str);
214   fail_unless_equals_int (chroma_site, expected_chroma_site);
215
216   gst_video_mastering_display_info_from_caps (&display_info, caps);
217   fail_unless (gst_video_mastering_display_info_is_equal (&display_info,
218           &expected_display_info));
219
220   gst_video_content_light_level_from_caps (&content_light_level, caps);
221   fail_unless (gst_video_content_light_level_is_equal (&content_light_level,
222           &expected_content_light_level));
223
224   gst_caps_unref (caps);
225
226   /* verify the presence of Color Space extmap in caps */
227   pad = gst_element_get_static_pad (pay, "src");
228   caps = gst_pad_get_current_caps (pad);
229   expected_caps = gst_caps_from_string ("application/x-rtp, "
230       "extmap-" G_STRINGIFY (EXTMAP_ID) "=" GST_RTP_HDREXT_COLORSPACE_URI);
231   fail_unless (gst_caps_is_subset (caps, expected_caps));
232   gst_object_unref (pad);
233   gst_caps_unref (caps);
234   gst_caps_unref (expected_caps);
235
236   g_free (colorimetry_str);
237
238   gst_object_unref (pay_ext);
239   gst_object_unref (depay_ext);
240   gst_object_unref (pay);
241   gst_object_unref (depay);
242   gst_harness_teardown (h);
243 }
244
245 GST_END_TEST;
246
247 static Suite *
248 rtphdrext_colorspace_suite (void)
249 {
250   Suite *s = suite_create ("rtphdrext_colorspace");
251   TCase *tc_chain = tcase_create ("general");
252
253   suite_add_tcase (s, tc_chain);
254
255   tcase_add_test (tc_chain, test_rtphdrext_colorspace_onebyte);
256   tcase_add_test (tc_chain, test_rtphdrext_colorspace_twobyte);
257
258   return s;
259 }
260
261 GST_CHECK_MAIN (rtphdrext_colorspace)