1 /* GStreamer unit tests for the -base typefind functions
3 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
25 #include <gst/check/gstcheck.h>
26 #include <gst/base/gsttypefindhelper.h>
28 GST_START_TEST (test_quicktime_mpeg4video)
30 /* quicktime redirect file which starts with what could also be interpreted
31 * as an MPEG-4 video object layer start code */
32 const guint8 qt_redirect_396042[] =
33 { 0x00, 0x00, 0x01, 0x22, 0x6d, 0x6f, 0x6f, 0x76, 0x00, 0x00, 0x01, 0x1a,
34 0x72, 0x6d, 0x72, 0x61, 0x00, 0x00, 0x00, 0x86, 0x72, 0x6d, 0x64, 0x61,
35 0x00, 0x00, 0x00, 0x54, 0x72, 0x64, 0x72, 0x66, 0x00, 0x00, 0x00, 0x00,
36 0x75, 0x72, 0x6c, 0x20, 0x00, 0x00, 0x00, 0x40, 0x68, 0x74, 0x74, 0x70,
37 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x2e, 0x71, 0x74,
38 0x76, 0x2e, 0x61, 0x70, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
39 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x2f, 0x6a, 0x61, 0x6e, 0x2f, 0x6a,
40 0x34, 0x37, 0x64, 0x35, 0x32, 0x6f, 0x6f, 0x2f, 0x71, 0x74, 0x37, 0x72,
41 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x2e, 0x6d, 0x6f, 0x76, 0x00,
42 0x00, 0x00, 0x00, 0x10, 0x72, 0x6d, 0x64, 0x72, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x0a, 0xf0, 0x00, 0x00, 0x00, 0x1a, 0x72, 0x6d, 0x76, 0x63,
44 0x00, 0x00, 0x00, 0x00, 0x71, 0x74, 0x69, 0x6d, 0x06, 0x50, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x72, 0x6d,
46 0x64, 0x61, 0x00, 0x00, 0x00, 0x5a, 0x72, 0x64, 0x72, 0x66, 0x00, 0x00,
47 0x00, 0x00, 0x75, 0x72, 0x6c, 0x20, 0x00, 0x00, 0x00, 0x46, 0x68, 0x74,
48 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x2e,
49 0x71, 0x74, 0x76, 0x2e, 0x61, 0x70, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f,
50 0x6d, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x73, 0x2f, 0x6a, 0x61, 0x6e,
51 0x2f, 0x6a, 0x34, 0x37, 0x64, 0x35, 0x32, 0x6f, 0x6f, 0x2f, 0x38, 0x38,
52 0x34, 0x38, 0x31, 0x32, 0x35, 0x5f, 0x32, 0x5f, 0x33, 0x35, 0x30, 0x5f,
53 0x72, 0x65, 0x66, 0x2e, 0x6d, 0x6f, 0x76, 0x00, 0x00, 0x00, 0x00, 0x10,
54 0x72, 0x6d, 0x64, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xf0,
55 0x00, 0x00, 0x00, 0x1a, 0x72, 0x6d, 0x76, 0x63, 0x00, 0x00, 0x00, 0x00,
56 0x71, 0x74, 0x69, 0x6d, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 GstTypeFindProbability prob;
64 buf = gst_buffer_new ();
65 GST_BUFFER_DATA (buf) = (guint8 *) qt_redirect_396042;
66 GST_BUFFER_SIZE (buf) = sizeof (qt_redirect_396042);
67 GST_BUFFER_OFFSET (buf) = 0;
69 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
70 fail_unless (caps != NULL);
71 GST_LOG ("Found type: %" GST_PTR_FORMAT, caps);
73 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
74 fail_unless_equals_string (type, "video/quicktime");
75 fail_unless (prob > GST_TYPE_FIND_MINIMUM && prob <= GST_TYPE_FIND_MAXIMUM);
77 gst_buffer_unref (buf);
78 gst_caps_unref (caps);
83 GST_START_TEST (test_broken_flac_in_ogg)
85 const guint8 flac_id_packet[4] = { 'f', 'L', 'a', 'C' };
86 GstTypeFindProbability prob;
91 buf = gst_buffer_new ();
92 GST_BUFFER_DATA (buf) = (guint8 *) flac_id_packet;
93 GST_BUFFER_SIZE (buf) = sizeof (flac_id_packet);
94 GST_BUFFER_OFFSET (buf) = 0;
96 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
97 fail_unless (caps != NULL);
98 GST_LOG ("Found type: %" GST_PTR_FORMAT, caps);
100 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
101 fail_unless_equals_string (type, "audio/x-flac");
102 fail_unless (prob > GST_TYPE_FIND_MINIMUM && prob <= GST_TYPE_FIND_MAXIMUM);
104 gst_buffer_unref (buf);
105 gst_caps_unref (caps);
111 typefind_test_file (const gchar * filename)
115 GstCaps *caps = NULL;
116 gchar *path, *data = NULL;
119 path = g_build_filename (GST_TEST_FILES_PATH, filename, NULL);
120 GST_LOG ("reading file '%s'", path);
121 if (!g_file_get_contents (path, &data, &data_len, &err)) {
122 g_error ("error loading test file: %s", err->message);
125 buf = gst_buffer_new ();
126 GST_BUFFER_DATA (buf) = (guint8 *) data;
127 GST_BUFFER_SIZE (buf) = data_len;
128 GST_BUFFER_OFFSET (buf) = 0;
130 caps = gst_type_find_helper_for_buffer (NULL, buf, NULL);
131 fail_unless (caps != NULL);
132 GST_LOG ("Found type: %" GST_PTR_FORMAT, caps);
134 gst_buffer_unref (buf);
141 GST_START_TEST (test_jpeg_not_ac3)
144 GstCaps *caps = NULL;
146 caps = typefind_test_file ("partialframe.mjpeg");
147 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
148 fail_unless_equals_string (type, "image/jpeg");
150 gst_caps_unref (caps);
155 GST_START_TEST (test_mpegts)
158 gboolean systemstream = FALSE;
159 GstCaps *caps = NULL;
160 gint packetsize = -1;
162 caps = typefind_test_file ("623663.mts");
163 s = gst_caps_get_structure (caps, 0);
164 fail_unless (gst_structure_has_name (s, "video/mpegts"));
165 fail_unless (gst_structure_has_field (s, "systemstream"));
166 fail_unless (gst_structure_get_boolean (s, "systemstream", &systemstream));
167 fail_unless_equals_int (systemstream, TRUE);
168 fail_unless (gst_structure_has_field (s, "packetsize"));
169 fail_unless (gst_structure_get_int (s, "packetsize", &packetsize));
170 fail_unless_equals_int (packetsize, 192);
172 gst_caps_unref (caps);
184 make_ac3_packet (guint8 * data, guint bytesize, guint bsid)
186 /* Actually not a fully valid packet; if the typefinder starts to
187 * check e.g. the CRCs, this test needs to be improved as well. */
188 const guint8 ac3_header[] = {
189 0x0b, 0x77, /* syncword */
190 0x00, 0x00, /* crc1 */
191 0x00, /* fscod 0xc0, frmsizecod 0x3f */
192 0x00 /* bsid 0xf8, bsmod 0x07 */
194 const struct ac3_frmsize frmsize[] = {
195 {17, 256}, {26, 640} /* small subset of supported sizes */
197 guint wordsize = bytesize >> 1, frmsizecod = 0;
200 fail_unless ((bytesize & 0x01) == 0);
201 fail_unless (bytesize >= sizeof (ac3_header));
203 for (i = 0; i < G_N_ELEMENTS (frmsize); i++) {
204 if (frmsize[i].frmsize == wordsize) {
205 frmsizecod = frmsize[i].frmsizecod;
210 fail_unless (frmsizecod);
212 memcpy (data, ac3_header, sizeof (ac3_header));
213 data[4] = (data[4] & ~0x3f) | (frmsizecod & 0x3f);
214 data[5] = (bsid & 0x1f) << 3;
215 memset (data + 6, 0, bytesize - 6);
218 GST_START_TEST (test_ac3)
220 GstTypeFindProbability prob;
223 GstCaps *caps = NULL;
226 for (bsid = 0; bsid < 32; bsid++) {
227 buf = gst_buffer_new_and_alloc ((256 + 640) * 2);
228 make_ac3_packet (GST_BUFFER_DATA (buf), 256 * 2, bsid);
229 make_ac3_packet (GST_BUFFER_DATA (buf) + 256 * 2, 640 * 2, bsid);
231 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
233 fail_unless (caps != NULL);
234 GST_LOG ("Found type for BSID %u: %" GST_PTR_FORMAT, bsid, caps);
236 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
237 fail_unless_equals_string (type, "audio/x-ac3");
238 fail_unless (prob > GST_TYPE_FIND_MINIMUM
239 && prob <= GST_TYPE_FIND_MAXIMUM);
240 gst_caps_unref (caps);
242 fail_unless (caps == NULL);
245 gst_buffer_unref (buf);
252 make_eac3_packet (guint8 * data, guint bytesize, guint bsid)
254 /* Actually not a fully valid packet; if the typefinder starts to
255 * check e.g. the CRCs, this test needs to be improved as well. */
256 const guint8 eac3_header[] = {
257 0x0b, 0x77, /* syncword */
258 0x00, /* strmtyp 0xc0, substreamid 0x38,
259 * frmsize 0x07 (3 high bits) */
260 0x00, /* frmsize (low bits -> 11 total) */
261 0x00, /* fscod 0xc0, fscod2/numblocks 0x30,
262 * acmod 0x0e, lfeon 0x01 */
263 0x00 /* bsid 0xf8, dialnorm 0x07 (3 high bits) */
265 guint wordsize = bytesize >> 1;
267 fail_unless ((bytesize & 0x01) == 0);
268 fail_unless (bytesize >= sizeof (eac3_header));
270 memcpy (data, eac3_header, sizeof (eac3_header));
271 data[2] = (data[2] & ~0x07) | ((((wordsize - 1) & 0x700) >> 8) & 0xff);
272 data[3] = (wordsize - 1) & 0xff;
273 data[5] = (bsid & 0x1f) << 3;
274 memset (data + 6, 0, bytesize - 6);
277 GST_START_TEST (test_eac3)
279 GstTypeFindProbability prob;
282 GstCaps *caps = NULL;
285 for (bsid = 0; bsid <= 32; bsid++) {
286 buf = gst_buffer_new_and_alloc (558 + 384);
287 make_eac3_packet (GST_BUFFER_DATA (buf), 558, bsid);
288 make_eac3_packet (GST_BUFFER_DATA (buf) + 558, 384, bsid);
290 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
291 if (bsid > 10 && bsid <= 16) {
292 /* Only BSIs 11..16 are valid for Annex E */
293 fail_unless (caps != NULL);
294 GST_LOG ("Found type for BSID %u: %" GST_PTR_FORMAT, bsid, caps);
296 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
297 fail_unless_equals_string (type, "audio/x-eac3");
298 fail_unless (prob > GST_TYPE_FIND_MINIMUM
299 && prob <= GST_TYPE_FIND_MAXIMUM);
300 gst_caps_unref (caps);
302 /* Invalid E-AC-3 BSID, must not be detected as anything: */
303 fail_unless (caps == NULL);
306 gst_buffer_unref (buf);
312 #define TEST_RANDOM_DATA_SIZE (4*1024)
314 /* typefind random data, to make sure all typefinders are called */
315 GST_START_TEST (test_random_data)
317 GstTypeFindProbability prob;
318 const gchar *seed_env;
325 seed_env = g_getenv ("GST_TYPEFIND_TEST_SEED");
326 if (seed_env != NULL)
327 seed = atoi (seed_env);
329 seed = (guint32) time (NULL);
331 g_random_set_seed (seed);
333 data = g_malloc (TEST_RANDOM_DATA_SIZE);
334 for (i = 0; i < TEST_RANDOM_DATA_SIZE; ++i)
335 data[i] = g_random_int () & 0xff;
337 buf = gst_buffer_new ();
338 GST_BUFFER_DATA (buf) = (guint8 *) data;
339 GST_BUFFER_SIZE (buf) = TEST_RANDOM_DATA_SIZE;
340 GST_BUFFER_OFFSET (buf) = 0;
342 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
343 GST_INFO ("caps: %" GST_PTR_FORMAT ", probability=%u", caps, prob);
344 /* for now we just print an error log message */
345 if (caps != NULL /* && prob >= GST_TYPE_FIND_LIKELY */ ) {
346 GST_ERROR ("typefinder thinks random data is %" GST_PTR_FORMAT ", with a "
347 "probability of %u (seed was %u)", caps, prob, seed);
348 gst_caps_unref (caps);
351 gst_buffer_unref (buf);
357 GST_START_TEST (test_hls_m3u8)
360 GstCaps *caps = NULL;
362 caps = typefind_test_file ("hls.m3u8");
363 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
364 fail_unless_equals_string (type, "application/x-hls");
366 gst_caps_unref (caps);
372 typefindfunctions_suite (void)
374 Suite *s = suite_create ("typefindfunctions");
375 TCase *tc_chain = tcase_create ("general");
377 suite_add_tcase (s, tc_chain);
379 tcase_add_test (tc_chain, test_quicktime_mpeg4video);
380 tcase_add_test (tc_chain, test_broken_flac_in_ogg);
381 tcase_add_test (tc_chain, test_jpeg_not_ac3);
382 tcase_add_test (tc_chain, test_mpegts);
383 tcase_add_test (tc_chain, test_ac3);
384 tcase_add_test (tc_chain, test_eac3);
385 tcase_add_test (tc_chain, test_random_data);
386 tcase_add_test (tc_chain, test_hls_m3u8);
391 GST_CHECK_MAIN (typefindfunctions);