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_take_memory (buf, -1,
66 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
67 (gpointer) qt_redirect_396042, NULL,
68 sizeof (qt_redirect_396042), 0, sizeof (qt_redirect_396042)));
70 GST_BUFFER_OFFSET (buf) = 0;
72 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
73 fail_unless (caps != NULL);
74 GST_LOG ("Found type: %" GST_PTR_FORMAT, caps);
76 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
77 fail_unless_equals_string (type, "video/quicktime");
78 fail_unless (prob > GST_TYPE_FIND_MINIMUM && prob <= GST_TYPE_FIND_MAXIMUM);
80 gst_buffer_unref (buf);
81 gst_caps_unref (caps);
86 GST_START_TEST (test_broken_flac_in_ogg)
88 const guint8 flac_id_packet[4] = { 'f', 'L', 'a', 'C' };
89 GstTypeFindProbability prob;
94 buf = gst_buffer_new ();
95 gst_buffer_take_memory (buf, -1,
96 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
97 (gpointer) flac_id_packet, NULL,
98 sizeof (flac_id_packet), 0, sizeof (flac_id_packet)));
100 GST_BUFFER_OFFSET (buf) = 0;
102 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
103 fail_unless (caps != NULL);
104 GST_LOG ("Found type: %" GST_PTR_FORMAT, caps);
106 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
107 fail_unless_equals_string (type, "audio/x-flac");
108 fail_unless (prob > GST_TYPE_FIND_MINIMUM && prob <= GST_TYPE_FIND_MAXIMUM);
110 gst_buffer_unref (buf);
111 gst_caps_unref (caps);
117 typefind_test_file (const gchar * filename)
121 GstCaps *caps = NULL;
122 gchar *path, *data = NULL;
125 path = g_build_filename (GST_TEST_FILES_PATH, filename, NULL);
126 GST_LOG ("reading file '%s'", path);
127 if (!g_file_get_contents (path, &data, &data_len, &err)) {
128 g_error ("error loading test file: %s", err->message);
131 buf = gst_buffer_new ();
132 gst_buffer_take_memory (buf, -1,
133 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
134 (gpointer) data, NULL, data_len, 0, data_len));
136 GST_BUFFER_OFFSET (buf) = 0;
138 caps = gst_type_find_helper_for_buffer (NULL, buf, NULL);
139 fail_unless (caps != NULL);
140 GST_LOG ("Found type: %" GST_PTR_FORMAT, caps);
142 gst_buffer_unref (buf);
149 GST_START_TEST (test_jpeg_not_ac3)
152 GstCaps *caps = NULL;
154 caps = typefind_test_file ("partialframe.mjpeg");
155 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
156 fail_unless_equals_string (type, "image/jpeg");
158 gst_caps_unref (caps);
163 GST_START_TEST (test_mpegts)
166 gboolean systemstream = FALSE;
167 GstCaps *caps = NULL;
168 gint packetsize = -1;
170 caps = typefind_test_file ("623663.mts");
171 s = gst_caps_get_structure (caps, 0);
172 fail_unless (gst_structure_has_name (s, "video/mpegts"));
173 fail_unless (gst_structure_has_field (s, "systemstream"));
174 fail_unless (gst_structure_get_boolean (s, "systemstream", &systemstream));
175 fail_unless_equals_int (systemstream, TRUE);
176 fail_unless (gst_structure_has_field (s, "packetsize"));
177 fail_unless (gst_structure_get_int (s, "packetsize", &packetsize));
178 fail_unless_equals_int (packetsize, 192);
180 gst_caps_unref (caps);
192 make_ac3_packet (guint8 * data, guint bytesize, guint bsid)
194 /* Actually not a fully valid packet; if the typefinder starts to
195 * check e.g. the CRCs, this test needs to be improved as well. */
196 const guint8 ac3_header[] = {
197 0x0b, 0x77, /* syncword */
198 0x00, 0x00, /* crc1 */
199 0x00, /* fscod 0xc0, frmsizecod 0x3f */
200 0x00 /* bsid 0xf8, bsmod 0x07 */
202 const struct ac3_frmsize frmsize[] = {
203 {17, 256}, {26, 640} /* small subset of supported sizes */
205 guint wordsize = bytesize >> 1, frmsizecod = 0;
208 fail_unless ((bytesize & 0x01) == 0);
209 fail_unless (bytesize >= sizeof (ac3_header));
211 for (i = 0; i < G_N_ELEMENTS (frmsize); i++) {
212 if (frmsize[i].frmsize == wordsize) {
213 frmsizecod = frmsize[i].frmsizecod;
218 fail_unless (frmsizecod);
220 memcpy (data, ac3_header, sizeof (ac3_header));
221 data[4] = (data[4] & ~0x3f) | (frmsizecod & 0x3f);
222 data[5] = (bsid & 0x1f) << 3;
223 memset (data + 6, 0, bytesize - 6);
226 GST_START_TEST (test_ac3)
228 GstTypeFindProbability prob;
231 GstCaps *caps = NULL;
235 for (bsid = 0; bsid < 32; bsid++) {
236 buf = gst_buffer_new_and_alloc ((256 + 640) * 2);
237 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
238 make_ac3_packet (data, 256 * 2, bsid);
239 make_ac3_packet (data + 256 * 2, 640 * 2, bsid);
240 gst_buffer_unmap (buf, data, (256 + 640) * 2);
242 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
244 fail_unless (caps != NULL);
245 GST_LOG ("Found type for BSID %u: %" GST_PTR_FORMAT, bsid, caps);
247 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
248 fail_unless_equals_string (type, "audio/x-ac3");
249 fail_unless (prob > GST_TYPE_FIND_MINIMUM
250 && prob <= GST_TYPE_FIND_MAXIMUM);
251 gst_caps_unref (caps);
253 fail_unless (caps == NULL);
256 gst_buffer_unref (buf);
263 make_eac3_packet (guint8 * data, guint bytesize, guint bsid)
265 /* Actually not a fully valid packet; if the typefinder starts to
266 * check e.g. the CRCs, this test needs to be improved as well. */
267 const guint8 eac3_header[] = {
268 0x0b, 0x77, /* syncword */
269 0x00, /* strmtyp 0xc0, substreamid 0x38,
270 * frmsize 0x07 (3 high bits) */
271 0x00, /* frmsize (low bits -> 11 total) */
272 0x00, /* fscod 0xc0, fscod2/numblocks 0x30,
273 * acmod 0x0e, lfeon 0x01 */
274 0x00 /* bsid 0xf8, dialnorm 0x07 (3 high bits) */
276 guint wordsize = bytesize >> 1;
278 fail_unless ((bytesize & 0x01) == 0);
279 fail_unless (bytesize >= sizeof (eac3_header));
281 memcpy (data, eac3_header, sizeof (eac3_header));
282 data[2] = (data[2] & ~0x07) | ((((wordsize - 1) & 0x700) >> 8) & 0xff);
283 data[3] = (wordsize - 1) & 0xff;
284 data[5] = (bsid & 0x1f) << 3;
285 memset (data + 6, 0, bytesize - 6);
288 GST_START_TEST (test_eac3)
290 GstTypeFindProbability prob;
293 GstCaps *caps = NULL;
297 for (bsid = 0; bsid <= 32; bsid++) {
298 buf = gst_buffer_new_and_alloc (558 + 384);
299 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
300 make_eac3_packet (data, 558, bsid);
301 make_eac3_packet (data + 558, 384, bsid);
302 gst_buffer_unmap (buf, data, 558 + 384);
304 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
305 if (bsid > 10 && bsid <= 16) {
306 /* Only BSIs 11..16 are valid for Annex E */
307 fail_unless (caps != NULL);
308 GST_LOG ("Found type for BSID %u: %" GST_PTR_FORMAT, bsid, caps);
310 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
311 fail_unless_equals_string (type, "audio/x-eac3");
312 fail_unless (prob > GST_TYPE_FIND_MINIMUM
313 && prob <= GST_TYPE_FIND_MAXIMUM);
314 gst_caps_unref (caps);
316 /* Invalid E-AC-3 BSID, must not be detected as anything: */
317 fail_unless (caps == NULL);
320 gst_buffer_unref (buf);
326 #define TEST_RANDOM_DATA_SIZE (4*1024)
328 /* typefind random data, to make sure all typefinders are called */
329 GST_START_TEST (test_random_data)
331 GstTypeFindProbability prob;
332 const gchar *seed_env;
339 seed_env = g_getenv ("GST_TYPEFIND_TEST_SEED");
340 if (seed_env != NULL)
341 seed = atoi (seed_env);
343 seed = (guint32) time (NULL);
345 g_random_set_seed (seed);
347 data = g_malloc (TEST_RANDOM_DATA_SIZE);
348 for (i = 0; i < TEST_RANDOM_DATA_SIZE; ++i)
349 data[i] = g_random_int () & 0xff;
351 buf = gst_buffer_new ();
352 gst_buffer_take_memory (buf, -1,
353 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
354 data, NULL, TEST_RANDOM_DATA_SIZE, 0, TEST_RANDOM_DATA_SIZE));
356 GST_BUFFER_OFFSET (buf) = 0;
358 caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
359 GST_INFO ("caps: %" GST_PTR_FORMAT ", probability=%u", caps, prob);
360 /* for now we just print an error log message */
361 if (caps != NULL /* && prob >= GST_TYPE_FIND_LIKELY */ ) {
362 GST_ERROR ("typefinder thinks random data is %" GST_PTR_FORMAT ", with a "
363 "probability of %u (seed was %u)", caps, prob, seed);
364 gst_caps_unref (caps);
367 gst_buffer_unref (buf);
373 GST_START_TEST (test_hls_m3u8)
376 GstCaps *caps = NULL;
378 caps = typefind_test_file ("hls.m3u8");
379 type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
380 fail_unless_equals_string (type, "application/x-hls");
382 gst_caps_unref (caps);
388 typefindfunctions_suite (void)
390 Suite *s = suite_create ("typefindfunctions");
391 TCase *tc_chain = tcase_create ("general");
393 suite_add_tcase (s, tc_chain);
395 tcase_add_test (tc_chain, test_quicktime_mpeg4video);
396 tcase_add_test (tc_chain, test_broken_flac_in_ogg);
397 tcase_add_test (tc_chain, test_jpeg_not_ac3);
398 tcase_add_test (tc_chain, test_mpegts);
399 tcase_add_test (tc_chain, test_ac3);
400 tcase_add_test (tc_chain, test_eac3);
401 tcase_add_test (tc_chain, test_random_data);
402 tcase_add_test (tc_chain, test_hls_m3u8);
407 GST_CHECK_MAIN (typefindfunctions);