1 /*
2 * GStreamer
3 *
4 * unit test for mpegvideoparse
5 *
6 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
7 * Contact: Stefan Kost <stefan.kost@nokia.com>
8 * Copyright (C) 2018 Tim-Philipp Müller <tim centricular com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include <gst/check/check.h>
27 #include <gst/video/video.h>
28 #include "parser.h"
29
30 #define SRC_CAPS_TMPL "video/mpeg, mpegversion=(int)2, systemstream=(boolean)false, parsed=(boolean)false"
31 #define SINK_CAPS_TMPL "video/mpeg, mpegversion=(int){1, 2}, systemstream=(boolean)false, parsed=(boolean)true"
32
33 GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
34 GST_PAD_SINK,
35 GST_PAD_ALWAYS,
36 GST_STATIC_CAPS (SINK_CAPS_TMPL)
37 );
38
39 GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
40 GST_PAD_SRC,
41 GST_PAD_ALWAYS,
42 GST_STATIC_CAPS (SRC_CAPS_TMPL)
43 );
44
45 /* some data */
46
47 /* actually seq + gop */
48 static guint8 mpeg2_seq[] = {
49 0x00, 0x00, 0x01, 0xb3, 0x02, 0x00, 0x18, 0x15,
50 0xff, 0xff, 0xe0, 0x28, 0x00, 0x00, 0x01, 0xb5,
51 0x14, 0x8a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
52 0x01, 0xb8, 0x00, 0x08, 0x00, 0x00
53 };
54
55 /* actually seq + gop */
56 static guint8 mpeg1_seq[] = {
57 0x00, 0x00, 0x01, 0xb3, 0x02, 0x00, 0x18, 0x15,
58 0xff, 0xff, 0xe0, 0x28, 0x00, 0x00, 0x01, 0xb8,
59 0x00, 0x08, 00, 00
60 };
61
62 /* keyframes all around */
63 static guint8 mpeg2_iframe[] = {
64 0x00, 0x00, 0x01, 0x00, 0x00, 0x0f, 0xff, 0xf8,
65 0x00, 0x00, 0x01, 0xb5, 0x8f, 0xff, 0xf3, 0x41,
66 0x80, 0x00, 0x00, 0x01, 0x01, 0x23, 0xf8, 0x7d,
67 0x29, 0x48, 0x8b, 0x94, 0xa5, 0x22, 0x20, 0x00,
68 0x00, 0x01, 0x02, 0x23, 0xf8, 0x7d, 0x29, 0x48,
69 0x8b, 0x94, 0xa5, 0x22, 0x20
70 };
71
72 static guint8 mpeg1_iframe[] = {
73 0x00, 0x00, 0x01, 0x00, 0x00, 0x0f, 0xff, 0xf8,
74 0x00, 0x00, 0x01, 0x01, 0x23, 0xf8, 0x7d,
75 0x29, 0x48, 0x8b, 0x94, 0xa5, 0x22, 0x20, 0x00,
76 0x00, 0x01, 0x02, 0x23, 0xf8, 0x7d, 0x29, 0x48,
77 0x8b, 0x94, 0xa5, 0x22, 0x20
78 };
79
80 static gboolean
verify_buffer(buffer_verify_data_s * vdata,GstBuffer * buffer)81 verify_buffer (buffer_verify_data_s * vdata, GstBuffer * buffer)
82 {
83 GstMapInfo map;
84
85 gst_buffer_map (buffer, &map, GST_MAP_READ);
86
87 /* check initial header special case, otherwise delegate to default */
88 if (vdata->discard) {
89 /* header is separate */
90 fail_unless (map.size == ctx_headers[0].size - 8);
91 fail_unless (memcmp (map.data, ctx_headers[0].data, map.size) == 0);
92 } else {
93 /* header is merged in initial frame */
94 if (vdata->buffer_counter == 0) {
95 fail_unless (map.size > 4);
96 if (GST_READ_UINT32_BE (map.data) == 0x1b3) {
97 /* the whole sequence header is included */
98 fail_unless (map.size ==
99 ctx_headers[0].size + vdata->data_to_verify_size);
100 fail_unless (memcmp (map.data, ctx_headers[0].data,
101 ctx_headers[0].size) == 0);
102 fail_unless (memcmp (map.data + ctx_headers[0].size,
103 vdata->data_to_verify, vdata->data_to_verify_size) == 0);
104 } else {
105 /* sequence was separate, only gop here */
106 fail_unless (map.size == 8 + vdata->data_to_verify_size);
107 fail_unless (memcmp (map.data,
108 ctx_headers[0].data + ctx_headers[0].size - 8, 8) == 0);
109 fail_unless (memcmp (map.data + 8,
110 vdata->data_to_verify, vdata->data_to_verify_size) == 0);
111 }
112 gst_buffer_unmap (buffer, &map);
113 return TRUE;
114 }
115 }
116 gst_buffer_unmap (buffer, &map);
117
118 return FALSE;
119 }
120
121 #define GOP_SPLIT "gop-split"
122
123 static GstElement *
setup_element(const gchar * desc)124 setup_element (const gchar * desc)
125 {
126 GstElement *element;
127
128 if (strcmp (desc, GOP_SPLIT) == 0) {
129 element = gst_check_setup_element ("mpegvideoparse");
130 g_object_set (G_OBJECT (element), "gop-split", TRUE, NULL);
131 } else {
132 element = gst_check_setup_element ("mpegvideoparse");
133 }
134
135 return element;
136 }
137
GST_START_TEST(test_parse_normal)138 GST_START_TEST (test_parse_normal)
139 {
140 gst_parser_test_normal (mpeg2_iframe, sizeof (mpeg2_iframe));
141 }
142
143 GST_END_TEST;
144
145
GST_START_TEST(test_parse_drain_single)146 GST_START_TEST (test_parse_drain_single)
147 {
148 gst_parser_test_drain_single (mpeg2_iframe, sizeof (mpeg2_iframe));
149 }
150
151 GST_END_TEST;
152
153
GST_START_TEST(test_parse_split)154 GST_START_TEST (test_parse_split)
155 {
156 gst_parser_test_split (mpeg2_iframe, sizeof (mpeg2_iframe));
157 }
158
159 GST_END_TEST;
160
161
162 #define structure_get_int(s,f) \
163 (g_value_get_int(gst_structure_get_value(s,f)))
164 #define fail_unless_structure_field_int_equals(s,field,num) \
165 fail_unless_equals_int (structure_get_int(s,field), num)
166
167 static void
mpeg_video_parse_check_caps(guint version,guint8 * seq,gint size)168 mpeg_video_parse_check_caps (guint version, guint8 * seq, gint size)
169 {
170 GstCaps *caps;
171 GstStructure *s;
172 GstBuffer *buf;
173 const GValue *val;
174 GstMapInfo map;
175
176 ctx_headers[0].data = seq;
177 ctx_headers[0].size = size;
178 if (version == 1)
179 caps = gst_parser_test_get_output_caps (mpeg1_iframe, sizeof (mpeg1_iframe),
180 NULL);
181 else
182 caps = gst_parser_test_get_output_caps (mpeg2_iframe, sizeof (mpeg2_iframe),
183 NULL);
184 fail_unless (caps != NULL);
185
186 /* Check that the negotiated caps are as expected */
187 /* When codec_data is present, parser assumes that data is version 4 */
188 GST_LOG ("mpegvideo output caps: %" GST_PTR_FORMAT, caps);
189 s = gst_caps_get_structure (caps, 0);
190 fail_unless (gst_structure_has_name (s, "video/mpeg"));
191 fail_unless_structure_field_int_equals (s, "mpegversion", version);
192 fail_unless_structure_field_int_equals (s, "width", 32);
193 fail_unless_structure_field_int_equals (s, "height", 24);
194 fail_unless (gst_structure_has_field (s, "codec_data"));
195
196 /* check codec-data in more detail */
197 val = gst_structure_get_value (s, "codec_data");
198 fail_unless (val != NULL);
199 buf = gst_value_get_buffer (val);
200 fail_unless (buf != NULL);
201 gst_buffer_map (buf, &map, GST_MAP_READ);
202 /* codec-data = header - GOP */
203 assert_equals_int (map.size, size - 8);
204 fail_unless (memcmp (map.data, seq, map.size) == 0);
205 gst_buffer_unmap (buf, &map);
206
207 gst_caps_unref (caps);
208 }
209
GST_START_TEST(test_parse_detect_stream_mpeg2)210 GST_START_TEST (test_parse_detect_stream_mpeg2)
211 {
212 mpeg_video_parse_check_caps (2, mpeg2_seq, sizeof (mpeg2_seq));
213 }
214
215 GST_END_TEST;
216
217
GST_START_TEST(test_parse_detect_stream_mpeg1)218 GST_START_TEST (test_parse_detect_stream_mpeg1)
219 {
220 mpeg_video_parse_check_caps (1, mpeg1_seq, sizeof (mpeg1_seq));
221 }
222
223 GST_END_TEST;
224
225
GST_START_TEST(test_parse_gop_split)226 GST_START_TEST (test_parse_gop_split)
227 {
228 ctx_factory = GOP_SPLIT;
229 ctx_discard = 1;
230 gst_parser_test_normal (mpeg2_iframe, sizeof (mpeg2_iframe));
231 ctx_factory = "mpegvideoparse";
232 ctx_discard = 0;
233 }
234
235 GST_END_TEST;
236
GST_START_TEST(test_parse_cea708_captions)237 GST_START_TEST (test_parse_cea708_captions)
238 {
239 GstClockTime last_ts = 0;
240 GstHarness *h;
241 GstBuffer *buf;
242 gchar *fn;
243 gint i, j;
244
245 h = gst_harness_new_parse ("filesrc name=filesrc ! mpegvideoparse");
246
247 /* Minimal stripped down MPEG-2 video elementary stream with truncated slices.
248 * Created via mpegvideoparse ! multifilesink and then truncating all files
249 * to 80-100 bytes or such and concatenating them back together. */
250 fn = g_build_filename (GST_TEST_FILES_PATH, "mpeg2-es-with-cea708-cc.dat",
251 NULL);
252 gst_harness_set (h, "filesrc", "location", fn, NULL);
253 g_free (fn);
254
255 gst_harness_play (h);
256
257 for (i = 0; i < 50; ++i) {
258 GstVideoCaptionMeta *caption_meta;
259
260 buf = gst_harness_pull (h);
261 GST_LOG ("pulled buffer %" GST_PTR_FORMAT, buf);
262 caption_meta = gst_buffer_get_video_caption_meta (buf);
263 fail_unless (caption_meta != NULL);
264 fail_unless_equals_int (caption_meta->caption_type,
265 GST_VIDEO_CAPTION_TYPE_CEA708_RAW);
266 fail_unless_equals_int (caption_meta->size, 60);
267
268 /* iterate over triplets */
269 for (j = 0; j < 20; ++j) {
270 guint8 cc_type = caption_meta->data[3 * j] & 0x03;
271
272 /* first triplet always CEA-608 line 21 field 2 in our sample */
273 if (j == 0)
274 fail_unless_equals_int (cc_type, 1);
275 /* second triplet always CEA-608 line 21 field 1 in our sample */
276 if (j == 1)
277 fail_unless_equals_int (cc_type, 0);
278 if (j > 2)
279 fail_unless (cc_type == 2 || cc_type == 3);
280 /* first packet starts with a CCP header */
281 if (i == 0 && j == 2)
282 fail_unless (cc_type == 3);
283 }
284 /* buffer sanity check */
285 if (i > 0)
286 fail_unless (GST_BUFFER_DTS (buf) > last_ts);
287
288 last_ts = GST_BUFFER_DTS (buf);
289 gst_buffer_unref (buf);
290 }
291
292 gst_harness_teardown (h);
293 }
294
295 GST_END_TEST;
296
297 static Suite *
mpegvideoparse_suite(void)298 mpegvideoparse_suite (void)
299 {
300 Suite *s = suite_create ("mpegvideoparse");
301 TCase *tc_chain = tcase_create ("general");
302
303 /* init test context */
304 ctx_factory = "mpegvideoparse";
305 ctx_sink_template = &sinktemplate;
306 ctx_src_template = &srctemplate;
307 ctx_headers[0].data = mpeg2_seq;
308 ctx_headers[0].size = sizeof (mpeg2_seq);
309 ctx_verify_buffer = verify_buffer;
310 ctx_setup = setup_element;
311
312
313 suite_add_tcase (s, tc_chain);
314 tcase_add_test (tc_chain, test_parse_normal);
315 tcase_add_test (tc_chain, test_parse_drain_single);
316 tcase_add_test (tc_chain, test_parse_split);
317 tcase_add_test (tc_chain, test_parse_detect_stream_mpeg1);
318 tcase_add_test (tc_chain, test_parse_detect_stream_mpeg2);
319 tcase_add_test (tc_chain, test_parse_gop_split);
320 tcase_add_test (tc_chain, test_parse_cea708_captions);
321
322 return s;
323 }
324
325
326 /*
327 * TODO:
328 * - Both push- and pull-modes need to be tested
329 * * Pull-mode & EOS
330 */
331 GST_CHECK_MAIN (mpegvideoparse);
332