Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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_DATA (buf) = (guint8 *) qt_redirect_396042;
66   GST_BUFFER_SIZE (buf) = sizeof (qt_redirect_396042);
67   GST_BUFFER_OFFSET (buf) = 0;
68
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);
72
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);
76
77   gst_buffer_unref (buf);
78   gst_caps_unref (caps);
79 }
80
81 GST_END_TEST;
82
83 GST_START_TEST (test_broken_flac_in_ogg)
84 {
85   const guint8 flac_id_packet[4] = { 'f', 'L', 'a', 'C' };
86   GstTypeFindProbability prob;
87   const gchar *type;
88   GstBuffer *buf;
89   GstCaps *caps = NULL;
90
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;
95
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);
99
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);
103
104   gst_buffer_unref (buf);
105   gst_caps_unref (caps);
106 }
107
108 GST_END_TEST;
109
110 static GstCaps *
111 typefind_test_file (const gchar * filename)
112 {
113   GstBuffer *buf;
114   GError *err = NULL;
115   GstCaps *caps = NULL;
116   gchar *path, *data = NULL;
117   gsize data_len;
118
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);
123   }
124
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;
129
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);
133
134   gst_buffer_unref (buf);
135   g_free (data);
136   g_free (path);
137
138   return caps;
139 }
140
141 GST_START_TEST (test_jpeg_not_ac3)
142 {
143   const gchar *type;
144   GstCaps *caps = NULL;
145
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");
149
150   gst_caps_unref (caps);
151 }
152
153 GST_END_TEST;
154
155 GST_START_TEST (test_mpegts)
156 {
157   GstStructure *s;
158   gboolean systemstream = FALSE;
159   GstCaps *caps = NULL;
160   gint packetsize = -1;
161
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);
171
172   gst_caps_unref (caps);
173 }
174
175 GST_END_TEST;
176
177 struct ac3_frmsize
178 {
179   unsigned frmsizecod;
180   unsigned frmsize;
181 };
182
183 static void
184 make_ac3_packet (guint8 * data, guint bytesize, guint bsid)
185 {
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 */
193   };
194   const struct ac3_frmsize frmsize[] = {
195     {17, 256}, {26, 640}        /* small subset of supported sizes */
196   };
197   guint wordsize = bytesize >> 1, frmsizecod = 0;
198   int i;
199
200   fail_unless ((bytesize & 0x01) == 0);
201   fail_unless (bytesize >= sizeof (ac3_header));
202
203   for (i = 0; i < G_N_ELEMENTS (frmsize); i++) {
204     if (frmsize[i].frmsize == wordsize) {
205       frmsizecod = frmsize[i].frmsizecod;
206       break;
207     }
208   }
209
210   fail_unless (frmsizecod);
211
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);
216 }
217
218 GST_START_TEST (test_ac3)
219 {
220   GstTypeFindProbability prob;
221   const gchar *type;
222   GstBuffer *buf;
223   GstCaps *caps = NULL;
224   guint bsid;
225
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);
230
231     caps = gst_type_find_helper_for_buffer (NULL, buf, &prob);
232     if (bsid <= 8) {
233       fail_unless (caps != NULL);
234       GST_LOG ("Found type for BSID %u: %" GST_PTR_FORMAT, bsid, caps);
235
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);
241     } else {
242       fail_unless (caps == NULL);
243     }
244
245     gst_buffer_unref (buf);
246   }
247 }
248
249 GST_END_TEST;
250
251 static void
252 make_eac3_packet (guint8 * data, guint bytesize, guint bsid)
253 {
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) */
264   };
265   guint wordsize = bytesize >> 1;
266
267   fail_unless ((bytesize & 0x01) == 0);
268   fail_unless (bytesize >= sizeof (eac3_header));
269
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);
275 }
276
277 GST_START_TEST (test_eac3)
278 {
279   GstTypeFindProbability prob;
280   const gchar *type;
281   GstBuffer *buf;
282   GstCaps *caps = NULL;
283   guint bsid;
284
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);
289
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);
295
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);
301     } else {
302       /* Invalid E-AC-3 BSID, must not be detected as anything: */
303       fail_unless (caps == NULL);
304     }
305
306     gst_buffer_unref (buf);
307   }
308 }
309
310 GST_END_TEST;
311
312 #define TEST_RANDOM_DATA_SIZE (4*1024)
313
314 /* typefind random data, to make sure all typefinders are called */
315 GST_START_TEST (test_random_data)
316 {
317   GstTypeFindProbability prob;
318   const gchar *seed_env;
319   GstBuffer *buf;
320   GstCaps *caps;
321   guint32 seed;
322   guint8 *data;
323   gint i;
324
325   seed_env = g_getenv ("GST_TYPEFIND_TEST_SEED");
326   if (seed_env != NULL)
327     seed = atoi (seed_env);
328   else
329     seed = (guint32) time (NULL);
330
331   g_random_set_seed (seed);
332
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;
336
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;
341
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);
349   }
350
351   gst_buffer_unref (buf);
352   g_free (data);
353 }
354
355 GST_END_TEST;
356
357 GST_START_TEST (test_hls_m3u8)
358 {
359   const gchar *type;
360   GstCaps *caps = NULL;
361
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");
365
366   gst_caps_unref (caps);
367 }
368
369 GST_END_TEST;
370
371 static Suite *
372 typefindfunctions_suite (void)
373 {
374   Suite *s = suite_create ("typefindfunctions");
375   TCase *tc_chain = tcase_create ("general");
376
377   suite_add_tcase (s, tc_chain);
378
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);
387
388   return s;
389 }
390
391 GST_CHECK_MAIN (typefindfunctions);