codecparsers: mpeg: Add an mpeg video bitstream parsing library
[platform/upstream/gstreamer.git] / tests / check / libs / mpegvideoparser.c
1 /* Gstreamer
2  * Copyright (C) <2011> Intel Corporation
3  * Copyright (C) <2011> Collabora Ltd.
4  * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23 #include <gst/codecparsers/gstmpegvideoparser.h>
24
25 /* actually seq + gop */
26 static guint8 mpeg2_seq[] = {
27   0x00, 0x00, 0x01, 0xb3, 0x02, 0x00, 0x18, 0x15, 0xff, 0xff, 0xe0, 0x28,
28   0x00, 0x00, 0x01, 0xb3, 0x78, 0x04, 0x38, 0x37, 0xff, 0xff, 0xf0, 0x00,
29   0x00, 0x00, 0x01, 0xb5, 0x14, 0x8a, 0x00, 0x11, 0x03, 0x71,
30   0x00, 0x00, 0x01, 0xb8, 0x00, 0x08, 0x00, 0x00,
31   0x00, 0x00, 0x01, 0x03, 0x00, 0x08, 0x00, 0x00
32 };
33
34 static guint8 mis_identified_datas[] = {
35   0x00, 0x00, 0x01, 0x1f, 0x4a, 0xf4, 0xd4, 0xd8, 0x08, 0x23, 0xdd,
36   0x7c, 0xd3, 0x75, 0x21, 0x43, 0x85, 0x31, 0x43, 0x04, 0x24, 0x30,
37   0x18, 0x43, 0xba, 0x1a, 0x50, 0x60, 0xbb, 0x53, 0x56, 0x80, 0x41,
38   0xb9, 0xd4, 0x25, 0x42, 0xea, 0x71, 0xb7, 0x49, 0x84, 0x0b, 0x14,
39   0x24, 0xc2, 0xaa, 0xba, 0xf9, 0xf7, 0x5b, 0x78, 0xa2, 0xba, 0xd3,
40   0xc7, 0x12, 0xee, 0xbe, 0xba, 0xfa, 0xeb, 0xeb, 0xaf, 0xbe, 0x6f,
41   0xce, 0x92, 0x05, 0x15, 0x22, 0x44, 0xf4, 0xc9, 0x1b, 0xcd, 0x84,
42   0x80, 0x87, 0x35, 0x6c, 0x07, 0x82, 0xaf, 0x3c, 0x3a, 0x89, 0x48,
43   0x3a, 0x26, 0x00, 0x64, 0x03, 0x12, 0x60, 0x03, 0xf4, 0x8c, 0x21,
44   0x16, 0xbe, 0x3c, 0x7c, 0x18, 0x03, 0x10, 0x0c, 0x80, 0xa0, 0x05,
45   0xe1, 0x85, 0x94, 0x90, 0xc4, 0x74, 0x05, 0x72, 0x80, 0x7a, 0x8e,
46   0x3e, 0x00, 0x30,
47   /* The accelerated version of scan_for_start_codes()
48    * mis-identifies the following as a start code */
49   0x01, 0x00, 0x01, 0x80, 0x68, 0x14,
50   0x26, 0xe4, 0x80, 0x98, 0x0a, 0xba, 0x77, 0x01, 0xc2, 0x42, 0x12,
51   0xc4, 0x59, 0x2a, 0xbb, 0x49, 0xf2, 0xc5, 0xa8, 0xd9, 0x30, 0x33,
52   0x16, 0x50, 0x60, 0x61, 0x41, 0xaa, 0x0d, 0x41, 0x5b, 0x17, 0x77,
53   0x76, 0x1a, 0x14, 0x3a, 0x08, 0x19, 0x3d, 0x6c, 0x94, 0x55, 0xd0,
54   0x94, 0x5a, 0xeb, 0x61, 0x22, 0xa7, 0xa6, 0x83, 0x47, 0x6d, 0x4d,
55   0x84, 0xc4, 0x6f, 0x78, 0xd8, 0x3a, 0xb4, 0x02, 0x0c, 0x36, 0xa6,
56   0x0b, 0x18, 0x49, 0xf7, 0xad, 0x00, 0x82, 0x09, 0xba, 0x12, 0xba,
57   0x1d, 0x44, 0x94, 0x0a, 0x1b, 0x03, 0xbb, 0xa2, 0x53, 0x02, 0xc0,
58   0x41, 0xac, 0x22,
59   /* the real start code is here */
60   0x00, 0x00, 0x01, 0x20, 0x4a, 0xfd, 0xf5, 0x50
61 };
62
63 static GstMpegVideoPacketTypeCode ordercode[] = {
64   GST_MPEG_VIDEO_PACKET_SEQUENCE,
65   GST_MPEG_VIDEO_PACKET_EXTENSION,
66   GST_MPEG_VIDEO_PACKET_GOP,
67 };
68
69 GST_START_TEST (test_mpeg_parse)
70 {
71   gint i;
72   GList *list, *tmp;
73   GstMpegVideoTypeOffsetSize *typeoffsz;
74
75   list = gst_mpeg_video_parse (mpeg2_seq, sizeof (mpeg2_seq), 12);
76
77   assert_equals_int (g_list_length (list), 4);
78   for (tmp = list, i = 0; tmp; tmp = g_list_next (tmp), i++) {
79     typeoffsz = tmp->data;
80     if (i == 3) {
81       fail_unless (GST_MPEG_VIDEO_PACKET_SLICE_MIN <= typeoffsz->type &&
82           typeoffsz->type <= GST_MPEG_VIDEO_PACKET_SLICE_MAX);
83       fail_unless (typeoffsz->size < 0);
84     } else
85       assert_equals_int (ordercode[i], typeoffsz->type);
86   }
87
88   g_list_free_full (list, (GDestroyNotify) g_free);
89 }
90
91 GST_END_TEST;
92
93 GST_START_TEST (test_mpeg_parse_sequence_header)
94 {
95   GList *list;
96   GstMpegVideoTypeOffsetSize *typeoffsz;
97   GstMpegVideoSequenceHdr seqhdr;
98
99   list = gst_mpeg_video_parse (mpeg2_seq, sizeof (mpeg2_seq), 12);
100
101   typeoffsz = list->data;
102   fail_unless (typeoffsz->type == GST_MPEG_VIDEO_PACKET_SEQUENCE);
103   fail_unless (gst_mpeg_video_parse_sequence_header (&seqhdr, mpeg2_seq,
104           sizeof (mpeg2_seq), typeoffsz->offset));
105   assert_equals_int (seqhdr.width, 1920);
106   assert_equals_int (seqhdr.height, 1080);
107   assert_equals_int (seqhdr.aspect_ratio_info, 3);
108   assert_equals_int (seqhdr.par_w, 17280);
109   assert_equals_int (seqhdr.par_h, 17280);
110   assert_equals_int (seqhdr.frame_rate_code, 7);
111   assert_equals_int (seqhdr.fps_n, 60000);
112   assert_equals_int (seqhdr.fps_d, 1001);
113   assert_equals_int (seqhdr.bitrate_value, 262143);
114   assert_equals_int (seqhdr.bitrate, 0);
115   assert_equals_int (seqhdr.vbv_buffer_size_value, 512);
116   fail_unless (seqhdr.constrained_parameters_flag == FALSE);
117
118   g_list_free_full (list, (GDestroyNotify) g_free);
119 }
120
121 GST_END_TEST;
122
123 GST_START_TEST (test_mpeg_parse_sequence_extension)
124 {
125   GList *list;
126   GstMpegVideoTypeOffsetSize *typeoffsz;
127   GstMpegVideoSequenceExt seqext;
128
129   list = gst_mpeg_video_parse (mpeg2_seq, sizeof (mpeg2_seq), 12);
130
131   typeoffsz = list->next->data;
132   fail_unless (typeoffsz->type == GST_MPEG_VIDEO_PACKET_EXTENSION);
133   fail_unless (gst_mpeg_video_parse_sequence_extension (&seqext,
134           mpeg2_seq, sizeof (mpeg2_seq), typeoffsz->offset));
135   assert_equals_int (seqext.profile, 4);
136   assert_equals_int (seqext.level, 8);
137   assert_equals_int (seqext.progressive, 1);
138   assert_equals_int (seqext.chroma_format, 1);
139   assert_equals_int (seqext.horiz_size_ext, 0);
140   assert_equals_int (seqext.vert_size_ext, 0);
141   assert_equals_int (seqext.vert_size_ext, 0);
142   assert_equals_int (seqext.bitrate_ext, 8);
143   assert_equals_int (seqext.vbv_buffer_size_extension, 3);
144   assert_equals_int (seqext.low_delay, 0);
145   assert_equals_int (seqext.fps_n_ext, 3);
146   assert_equals_int (seqext.fps_d_ext, 2);
147
148   g_list_free_full (list, (GDestroyNotify) g_free);
149 }
150
151 GST_END_TEST;
152
153 GST_START_TEST (test_mis_identified_datas)
154 {
155   GList *list, *tmp;
156   GstMpegVideoTypeOffsetSize *typeoffsz;
157   guint8 *data = mis_identified_datas;
158
159   list = gst_mpeg_video_parse (mis_identified_datas,
160       sizeof (mis_identified_datas), 0);
161
162   assert_equals_int (g_list_length (list), 2);
163   for (tmp = list; tmp; tmp = g_list_next (tmp)) {
164     typeoffsz = tmp->data;
165
166     assert_equals_int (data[typeoffsz->offset - 4], 0);
167     assert_equals_int (data[typeoffsz->offset - 3], 0);
168     assert_equals_int (data[typeoffsz->offset - 2], 1);
169   }
170
171   g_list_free_full (list, (GDestroyNotify) g_free);
172 }
173
174 GST_END_TEST;
175
176 static Suite *
177 videoparsers_suite (void)
178 {
179   Suite *s = suite_create ("Video Parsers library");
180
181   TCase *tc_chain = tcase_create ("general");
182
183   suite_add_tcase (s, tc_chain);
184   tcase_add_test (tc_chain, test_mpeg_parse);
185   tcase_add_test (tc_chain, test_mpeg_parse_sequence_header);
186   tcase_add_test (tc_chain, test_mpeg_parse_sequence_extension);
187   tcase_add_test (tc_chain, test_mis_identified_datas);
188
189   return s;
190 }
191
192 int
193 main (int argc, char **argv)
194 {
195   int nf;
196
197   Suite *s = videoparsers_suite ();
198
199   SRunner *sr = srunner_create (s);
200
201   gst_check_init (&argc, &argv);
202
203   srunner_run_all (sr, CK_NORMAL);
204   nf = srunner_ntests_failed (sr);
205   srunner_free (sr);
206
207   return nf;
208 }