Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / tests / check / gst / typefindfunctions.c
1 /* GStreamer unit tests for the -base typefind functions
2  *
3  * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <gst/check/gstcheck.h>
26 #include <gst/base/gsttypefindhelper.h>
27
28 GST_START_TEST (test_quicktime_mpeg4video)
29 {
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,
57     0x00, 0x00
58   };
59   GstTypeFindProbability prob;
60   const gchar *type;
61   GstBuffer *buf;
62   GstCaps *caps = NULL;
63
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)));
69
70   GST_BUFFER_OFFSET (buf) = 0;
71
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);
75
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);
79
80   gst_buffer_unref (buf);
81   gst_caps_unref (caps);
82 }
83
84 GST_END_TEST;
85
86 GST_START_TEST (test_broken_flac_in_ogg)
87 {
88   const guint8 flac_id_packet[4] = { 'f', 'L', 'a', 'C' };
89   GstTypeFindProbability prob;
90   const gchar *type;
91   GstBuffer *buf;
92   GstCaps *caps = NULL;
93
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)));
99
100   GST_BUFFER_OFFSET (buf) = 0;
101
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);
105
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);
109
110   gst_buffer_unref (buf);
111   gst_caps_unref (caps);
112 }
113
114 GST_END_TEST;
115
116 static GstCaps *
117 typefind_test_file (const gchar * filename)
118 {
119   GstBuffer *buf;
120   GError *err = NULL;
121   GstCaps *caps = NULL;
122   gchar *path, *data = NULL;
123   gsize data_len;
124
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);
129   }
130
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));
135
136   GST_BUFFER_OFFSET (buf) = 0;
137
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);
141
142   gst_buffer_unref (buf);
143   g_free (data);
144   g_free (path);
145
146   return caps;
147 }
148
149 GST_START_TEST (test_jpeg_not_ac3)
150 {
151   const gchar *type;
152   GstCaps *caps = NULL;
153
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");
157
158   gst_caps_unref (caps);
159 }
160
161 GST_END_TEST;
162
163 GST_START_TEST (test_mpegts)
164 {
165   GstStructure *s;
166   gboolean systemstream = FALSE;
167   GstCaps *caps = NULL;
168   gint packetsize = -1;
169
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);
179
180   gst_caps_unref (caps);
181 }
182
183 GST_END_TEST;
184
185 struct ac3_frmsize
186 {
187   unsigned frmsizecod;
188   unsigned frmsize;
189 };
190
191 static void
192 make_ac3_packet (guint8 * data, guint bytesize, guint bsid)
193 {
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 */
201   };
202   const struct ac3_frmsize frmsize[] = {
203     {17, 256}, {26, 640}        /* small subset of supported sizes */
204   };
205   guint wordsize = bytesize >> 1, frmsizecod = 0;
206   int i;
207
208   fail_unless ((bytesize & 0x01) == 0);
209   fail_unless (bytesize >= sizeof (ac3_header));
210
211   for (i = 0; i < G_N_ELEMENTS (frmsize); i++) {
212     if (frmsize[i].frmsize == wordsize) {
213       frmsizecod = frmsize[i].frmsizecod;
214       break;
215     }
216   }
217
218   fail_unless (frmsizecod);
219
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);
224 }
225
226 GST_START_TEST (test_ac3)
227 {
228   GstTypeFindProbability prob;
229   const gchar *type;
230   GstBuffer *buf;
231   GstCaps *caps = NULL;
232   guint bsid;
233   guint8 *data;
234
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);
241
242     caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
243     if (bsid <= 8) {
244       fail_unless (caps != NULL);
245       GST_LOG ("Found type for BSID %u: %" GST_PTR_FORMAT, bsid, caps);
246
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);
252     } else {
253       fail_unless (caps == NULL);
254     }
255
256     gst_buffer_unref (buf);
257   }
258 }
259
260 GST_END_TEST;
261
262 static void
263 make_eac3_packet (guint8 * data, guint bytesize, guint bsid)
264 {
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) */
275   };
276   guint wordsize = bytesize >> 1;
277
278   fail_unless ((bytesize & 0x01) == 0);
279   fail_unless (bytesize >= sizeof (eac3_header));
280
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);
286 }
287
288 GST_START_TEST (test_eac3)
289 {
290   GstTypeFindProbability prob;
291   const gchar *type;
292   GstBuffer *buf;
293   GstCaps *caps = NULL;
294   guint bsid;
295   guint8 *data;
296
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);
303
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);
309
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);
315     } else {
316       /* Invalid E-AC-3 BSID, must not be detected as anything: */
317       fail_unless (caps == NULL);
318     }
319
320     gst_buffer_unref (buf);
321   }
322 }
323
324 GST_END_TEST;
325
326 #define TEST_RANDOM_DATA_SIZE (4*1024)
327
328 /* typefind random data, to make sure all typefinders are called */
329 GST_START_TEST (test_random_data)
330 {
331   GstTypeFindProbability prob;
332   const gchar *seed_env;
333   GstBuffer *buf;
334   GstCaps *caps;
335   guint32 seed;
336   guint8 *data;
337   gint i;
338
339   seed_env = g_getenv ("GST_TYPEFIND_TEST_SEED");
340   if (seed_env != NULL)
341     seed = atoi (seed_env);
342   else
343     seed = (guint32) time (NULL);
344
345   g_random_set_seed (seed);
346
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;
350
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));
355
356   GST_BUFFER_OFFSET (buf) = 0;
357
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);
365   }
366
367   gst_buffer_unref (buf);
368   g_free (data);
369 }
370
371 GST_END_TEST;
372
373 GST_START_TEST (test_hls_m3u8)
374 {
375   const gchar *type;
376   GstCaps *caps = NULL;
377
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");
381
382   gst_caps_unref (caps);
383 }
384
385 GST_END_TEST;
386
387 static Suite *
388 typefindfunctions_suite (void)
389 {
390   Suite *s = suite_create ("typefindfunctions");
391   TCase *tc_chain = tcase_create ("general");
392
393   suite_add_tcase (s, tc_chain);
394
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);
403
404   return s;
405 }
406
407 GST_CHECK_MAIN (typefindfunctions);