78662a2103f438ae9f3d9d1efc5361fc2b1584f0
[platform/upstream/gst-plugins-good.git] / tests / check / elements / aacparse.c
1 /*
2  * GStreamer
3  *
4  * unit test for aacparse
5  *
6  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
7  *
8  * Contact: Stefan Kost <stefan.kost@nokia.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., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #include <gst/check/gstcheck.h>
27 #include "aacparse_data.h"
28
29 #define SRC_CAPS_CDATA "audio/mpeg, framed=(boolean)false, codec_data=(buffer)1190"
30 #define SRC_CAPS_TMPL  "audio/mpeg, framed=(boolean)false, mpegversion=(int){2,4}"
31
32 #define SINK_CAPS \
33     "audio/mpeg, framed=(boolean)true"
34 #define SINK_CAPS_MPEG2 \
35     "audio/mpeg, framed=(boolean)true, mpegversion=2, rate=48000, channels=2"
36 #define SINK_CAPS_MPEG4 \
37     "audio/mpeg, framed=(boolean)true, mpegversion=4, rate=96000, channels=2"
38 #define SINK_CAPS_TMPL  "audio/mpeg, framed=(boolean)true, mpegversion=(int){2,4}"
39
40 GList *buffers;
41 GList *current_buf = NULL;
42
43 GstPad *srcpad, *sinkpad;
44 guint dataoffset = 0;
45 GstClockTime ts_counter = 0;
46 gint64 offset_counter = 0;
47 guint buffer_counter = 0;
48
49 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (SINK_CAPS_TMPL)
53     );
54
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS (SRC_CAPS_TMPL)
59     );
60
61 typedef struct
62 {
63   guint buffers_before_offset_skip;
64   guint offset_skip_amount;
65   const unsigned char *data_to_verify;
66   GstCaps *caps;
67 } buffer_verify_data_s;
68
69 /* takes a copy of the passed buffer data */
70 static GstBuffer *
71 buffer_new (const unsigned char *buffer_data, guint size)
72 {
73   GstBuffer *buffer;
74
75   buffer = gst_buffer_new_and_alloc (size);
76   if (buffer_data) {
77     memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
78   } else {
79     guint i;
80     /* Create a recognizable pattern (loop 0x00 -> 0xff) in the data block */
81     for (i = 0; i < size; i++) {
82       GST_BUFFER_DATA (buffer)[i] = i % 0x100;
83     }
84   }
85
86   gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad));
87   GST_BUFFER_OFFSET (buffer) = dataoffset;
88   dataoffset += size;
89   return buffer;
90 }
91
92
93 /*
94  * Count buffer sizes together.
95  */
96 static void
97 buffer_count_size (void *buffer, void *user_data)
98 {
99   guint *sum = (guint *) user_data;
100   *sum += GST_BUFFER_SIZE (buffer);
101 }
102
103
104 /*
105  * Verify that given buffer contains predefined ADTS frame.
106  */
107 static void
108 buffer_verify_adts (void *buffer, void *user_data)
109 {
110   buffer_verify_data_s *vdata;
111
112   if (!user_data) {
113     return;
114   }
115
116   vdata = (buffer_verify_data_s *) user_data;
117
118   fail_unless (memcmp (GST_BUFFER_DATA (buffer), vdata->data_to_verify,
119           ADTS_FRAME_LEN) == 0);
120
121   fail_unless (GST_BUFFER_TIMESTAMP (buffer) == ts_counter);
122   fail_unless (GST_BUFFER_DURATION (buffer) != 0);
123
124   if (vdata->buffers_before_offset_skip) {
125     /* This is for skipping the garbage in some test cases */
126     if (buffer_counter == vdata->buffers_before_offset_skip) {
127       offset_counter += vdata->offset_skip_amount;
128     }
129   }
130   fail_unless (GST_BUFFER_OFFSET (buffer) == offset_counter);
131
132   if (vdata->caps) {
133     gchar *bcaps = gst_caps_to_string (GST_BUFFER_CAPS (buffer));
134     g_free (bcaps);
135
136     GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT " ?",
137         GST_BUFFER_CAPS (buffer), vdata->caps);
138     fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (buffer), vdata->caps));
139   }
140
141   ts_counter += GST_BUFFER_DURATION (buffer);
142   offset_counter += ADTS_FRAME_LEN;
143   buffer_counter++;
144 }
145
146 static GstElement *
147 setup_aacparse (const gchar * src_caps_str)
148 {
149   GstElement *aacparse;
150   GstCaps *srccaps = NULL;
151   GstBus *bus;
152
153   if (src_caps_str) {
154     srccaps = gst_caps_from_string (src_caps_str);
155     fail_unless (srccaps != NULL);
156   }
157
158   aacparse = gst_check_setup_element ("aacparse");
159   srcpad = gst_check_setup_src_pad (aacparse, &srctemplate, srccaps);
160   sinkpad = gst_check_setup_sink_pad (aacparse, &sinktemplate, NULL);
161   gst_pad_set_active (srcpad, TRUE);
162   gst_pad_set_active (sinkpad, TRUE);
163
164   bus = gst_bus_new ();
165   gst_element_set_bus (aacparse, bus);
166
167   fail_unless (gst_element_set_state (aacparse,
168           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
169       "could not set to playing");
170
171   if (srccaps) {
172     gst_caps_unref (srccaps);
173   }
174   ts_counter = offset_counter = buffer_counter = 0;
175   buffers = NULL;
176   return aacparse;
177 }
178
179 static void
180 cleanup_aacparse (GstElement * aacparse)
181 {
182   GstBus *bus;
183
184   /* Free parsed buffers */
185   gst_check_drop_buffers ();
186
187   bus = GST_ELEMENT_BUS (aacparse);
188   gst_bus_set_flushing (bus, TRUE);
189   gst_object_unref (bus);
190
191   gst_pad_set_active (srcpad, FALSE);
192   gst_pad_set_active (sinkpad, FALSE);
193   gst_check_teardown_src_pad (aacparse);
194   gst_check_teardown_sink_pad (aacparse);
195   gst_check_teardown_element (aacparse);
196 }
197
198
199 /*
200  * Test if the parser pushes data with ADIF header properly and detects the
201  * stream to MPEG4 properly.
202  */
203 GST_START_TEST (test_parse_adif_normal)
204 {
205   GstElement *aacparse;
206   GstBuffer *buffer;
207   GstCaps *scaps, *sinkcaps;
208   guint datasum = 0;
209   guint i;
210
211   aacparse = setup_aacparse (NULL);
212
213   buffer = buffer_new (adif_header, ADIF_HEADER_LEN);
214   fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
215
216   for (i = 0; i < 3; i++) {
217     buffer = buffer_new (NULL, 100);
218     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
219   }
220   gst_pad_push_event (srcpad, gst_event_new_eos ());
221
222   /* Calculate the outputted buffer sizes */
223   g_list_foreach (buffers, buffer_count_size, &datasum);
224
225   /* ADIF is not a framed format, and therefore we cannot expect the
226      same amount of output buffers as we pushed. However, all data should
227      still come through, including the header bytes */
228   fail_unless_equals_int (datasum, 3 * 100 + ADIF_HEADER_LEN);
229
230   /* Check that the negotiated caps are as expected */
231   /* For ADIF parser assumes that data is always version 4 */
232   scaps = gst_caps_from_string (SINK_CAPS_MPEG4 ", stream-format=(string)adif");
233   sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
234
235   GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT " ?", sinkcaps, scaps);
236   fail_unless (gst_caps_is_equal (sinkcaps, scaps));
237   gst_caps_unref (sinkcaps);
238   gst_caps_unref (scaps);
239
240   cleanup_aacparse (aacparse);
241 }
242
243 GST_END_TEST;
244
245
246 /*
247  * Test if the parser pushes data with ADTS frames properly.
248  */
249 GST_START_TEST (test_parse_adts_normal)
250 {
251   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
252   GstElement *aacparse;
253   GstBuffer *buffer;
254   guint i;
255
256   aacparse = setup_aacparse (NULL);
257
258   for (i = 0; i < 10; i++) {
259     buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
260     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
261   }
262   gst_pad_push_event (srcpad, gst_event_new_eos ());
263
264   fail_unless_equals_int (g_list_length (buffers), 10);
265   g_list_foreach (buffers, buffer_verify_adts, &vdata);
266
267   cleanup_aacparse (aacparse);
268 }
269
270 GST_END_TEST;
271
272
273 /*
274  * Test if ADTS parser drains its buffers properly. Even one single frame
275  * should be drained and pushed forward when EOS occurs. This single frame
276  * case is special, since normally the parser needs more data to be sure
277  * about stream format. But it should still push the frame forward in EOS.
278  */
279 GST_START_TEST (test_parse_adts_drain_single)
280 {
281   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
282   GstElement *aacparse;
283   GstBuffer *buffer;
284
285   aacparse = setup_aacparse (NULL);
286
287   buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
288   fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
289   gst_pad_push_event (srcpad, gst_event_new_eos ());
290
291   fail_unless_equals_int (g_list_length (buffers), 1);
292   g_list_foreach (buffers, buffer_verify_adts, &vdata);
293
294   cleanup_aacparse (aacparse);
295 }
296
297 GST_END_TEST;
298
299
300 /*
301  * Make sure that parser does not drain garbage when EOS occurs.
302  */
303 GST_START_TEST (test_parse_adts_drain_garbage)
304 {
305   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
306   GstElement *aacparse;
307   GstBuffer *buffer;
308   guint i;
309
310   aacparse = setup_aacparse (NULL);
311
312   for (i = 0; i < 10; i++) {
313     buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
314     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
315   }
316
317   /* Push one garbage frame and then EOS */
318   buffer = buffer_new (garbage_frame, GARBAGE_FRAME_LEN);
319   fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
320   gst_pad_push_event (srcpad, gst_event_new_eos ());
321
322   fail_unless_equals_int (g_list_length (buffers), 10);
323   g_list_foreach (buffers, buffer_verify_adts, &vdata);
324
325   cleanup_aacparse (aacparse);
326 }
327
328 GST_END_TEST;
329
330
331 /*
332  * Test if ADTS parser splits a buffer that contains two frames into two
333  * separate buffers properly.
334  */
335 GST_START_TEST (test_parse_adts_split)
336 {
337   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
338   GstElement *aacparse;
339   GstBuffer *buffer;
340   guint i;
341
342   aacparse = setup_aacparse (NULL);
343
344   for (i = 0; i < 5; i++) {
345     buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN * 2);
346     memcpy (GST_BUFFER_DATA (buffer) + ADTS_FRAME_LEN,
347         adts_frame_mpeg4, ADTS_FRAME_LEN);
348     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
349   }
350   gst_pad_push_event (srcpad, gst_event_new_eos ());
351
352   fail_unless_equals_int (g_list_length (buffers), 10);
353   g_list_foreach (buffers, buffer_verify_adts, &vdata);
354
355   cleanup_aacparse (aacparse);
356 }
357
358 GST_END_TEST;
359
360
361 /*
362  * Test if the ADTS parser skips garbage between frames properly.
363  */
364 GST_START_TEST (test_parse_adts_skip_garbage)
365 {
366   buffer_verify_data_s vdata =
367       { 10, GARBAGE_FRAME_LEN, adts_frame_mpeg4, NULL };
368   GstElement *aacparse;
369   GstBuffer *buffer;
370   guint i;
371
372   aacparse = setup_aacparse (NULL);
373
374   for (i = 0; i < 10; i++) {
375     buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
376     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
377   }
378
379   /* push garbage */
380   buffer = buffer_new (garbage_frame, GARBAGE_FRAME_LEN);
381   fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
382
383   for (i = 0; i < 10; i++) {
384     buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
385     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
386   }
387   gst_pad_push_event (srcpad, gst_event_new_eos ());
388
389   fail_unless_equals_int (g_list_length (buffers), 20);
390   g_list_foreach (buffers, buffer_verify_adts, &vdata);
391
392   cleanup_aacparse (aacparse);
393 }
394
395 GST_END_TEST;
396
397
398 /*
399  * Test if the src caps are set according to stream format (MPEG version).
400  */
401 GST_START_TEST (test_parse_adts_detect_mpeg_version)
402 {
403   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg2, NULL };
404   GstElement *aacparse;
405   GstBuffer *buffer;
406   GstCaps *sinkcaps;
407   guint i;
408
409   aacparse = setup_aacparse (NULL);
410
411   /* buffer_verify_adts will check if the caps are equal */
412   vdata.caps = gst_caps_from_string (SINK_CAPS_MPEG2
413       ", stream-format=(string)adts");
414
415   for (i = 0; i < 10; i++) {
416     /* Push MPEG version 2 frames. */
417     buffer = buffer_new (adts_frame_mpeg2, ADTS_FRAME_LEN);
418     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
419   }
420   gst_pad_push_event (srcpad, gst_event_new_eos ());
421
422   /* Check that the negotiated caps are as expected */
423   sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
424   GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT "?", sinkcaps, vdata.caps);
425   fail_unless (gst_caps_is_equal (sinkcaps, vdata.caps));
426   gst_caps_unref (sinkcaps);
427
428   fail_unless_equals_int (g_list_length (buffers), 10);
429   g_list_foreach (buffers, buffer_verify_adts, &vdata);
430
431   gst_caps_unref (vdata.caps);
432   cleanup_aacparse (aacparse);
433 }
434
435 GST_END_TEST;
436
437 #define structure_get_int(s,f) \
438     (g_value_get_int(gst_structure_get_value(s,f)))
439 #define fail_unless_structure_field_int_equals(s,field,num) \
440     fail_unless_equals_int (structure_get_int(s,field), num)
441 /*
442  * Test if the parser handles raw stream and codec_data info properly.
443  */
444 GST_START_TEST (test_parse_handle_codec_data)
445 {
446   GstElement *aacparse;
447   GstBuffer *buffer;
448   GstCaps *sinkcaps;
449   GstStructure *s;
450   guint datasum = 0;
451   guint i;
452   const gchar *stream_format;
453
454   aacparse = setup_aacparse (SRC_CAPS_CDATA);
455
456   for (i = 0; i < 10; i++) {
457     /* Push random data. It should get through since the parser should be
458        initialized because it got codec_data in the caps */
459     buffer = buffer_new (NULL, 100);
460     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
461   }
462   gst_pad_push_event (srcpad, gst_event_new_eos ());
463
464   /* Check that the negotiated caps are as expected */
465   /* When codec_data is present, parser assumes that data is version 4 */
466   sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
467   GST_LOG ("aac output caps: %" GST_PTR_FORMAT, sinkcaps);
468   s = gst_caps_get_structure (sinkcaps, 0);
469   fail_unless (gst_structure_has_name (s, "audio/mpeg"));
470   fail_unless_structure_field_int_equals (s, "mpegversion", 4);
471   fail_unless_structure_field_int_equals (s, "channels", 2);
472   fail_unless_structure_field_int_equals (s, "rate", 48000);
473   fail_unless (gst_structure_has_field (s, "codec_data"));
474   fail_unless (gst_structure_has_field (s, "stream-format"));
475   stream_format = gst_structure_get_string (s, "stream-format");
476   fail_unless (strcmp (stream_format, "raw") == 0);
477
478   gst_caps_unref (sinkcaps);
479
480   g_list_foreach (buffers, buffer_count_size, &datasum);
481   fail_unless_equals_int (datasum, 10 * 100);
482
483   cleanup_aacparse (aacparse);
484 }
485
486 GST_END_TEST;
487
488
489 static Suite *
490 aacparse_suite (void)
491 {
492   Suite *s = suite_create ("aacparse");
493   TCase *tc_chain = tcase_create ("general");
494
495   suite_add_tcase (s, tc_chain);
496   /* ADIF tests */
497   tcase_add_test (tc_chain, test_parse_adif_normal);
498
499   /* ADTS tests */
500   tcase_add_test (tc_chain, test_parse_adts_normal);
501   tcase_add_test (tc_chain, test_parse_adts_drain_single);
502   tcase_add_test (tc_chain, test_parse_adts_drain_garbage);
503   tcase_add_test (tc_chain, test_parse_adts_split);
504   tcase_add_test (tc_chain, test_parse_adts_skip_garbage);
505   tcase_add_test (tc_chain, test_parse_adts_detect_mpeg_version);
506
507   /* Other tests */
508   tcase_add_test (tc_chain, test_parse_handle_codec_data);
509
510   return s;
511 }
512
513
514 /*
515  * TODO:
516  *   - Both push- and pull-modes need to be tested
517  *      * Pull-mode & EOS
518  */
519
520 GST_CHECK_MAIN (aacparse);