1 /* GStreamer
2 *
3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include <gst/check/gstcheck.h>
22 #include <gst/codecparsers/nalutils.h>
23 #include <string.h>
24
GST_START_TEST(test_nal_writer_init)25 GST_START_TEST (test_nal_writer_init)
26 {
27 NalWriter nw;
28
29 /* init with invalid params */
30 ASSERT_CRITICAL (nal_writer_init (&nw, 0, TRUE));
31 ASSERT_CRITICAL (nal_writer_init (&nw, 0, FALSE));
32 ASSERT_CRITICAL (nal_writer_init (&nw, 5, TRUE));
33 ASSERT_CRITICAL (nal_writer_init (&nw, 5, FALSE));
34
35 nal_writer_init (&nw, 4, FALSE);
36 nal_writer_reset (&nw);
37 }
38
39 GST_END_TEST;
40
GST_START_TEST(test_nal_writer_emulation_preventation)41 GST_START_TEST (test_nal_writer_emulation_preventation)
42 {
43 NalWriter nw;
44 gint i, j;
45 static guint8 patterns[][3] = {
46 {0x00, 0x00, 0x00},
47 {0x00, 0x00, 0x01},
48 {0x00, 0x00, 0x02},
49 {0x00, 0x00, 0x03},
50 };
51 static guint8 expected_rst[][4] = {
52 {0x00, 0x00, 0x03, 0x00},
53 {0x00, 0x00, 0x03, 0x01},
54 {0x00, 0x00, 0x03, 0x02},
55 {0x00, 0x00, 0x03, 0x03},
56 };
57
58 /* Within the NAL unit, the following three-byte sequences shall not occur
59 * at any byte-aligned position:
60 * – 0x000000
61 * – 0x000001
62 * – 0x000002
63 * Within the NAL unit, any four-byte sequence that starts with 0x000003
64 * other than the following sequences shall not occur
65 * at any byte-aligned position:
66 * – 0x00000300
67 * – 0x00000301
68 * – 0x00000302
69 * – 0x00000303
70 */
71
72 for (i = 0; i < G_N_ELEMENTS (patterns); i++) {
73 GstMemory *mem;
74 GstMapInfo info;
75
76 nal_writer_init (&nw, 4, FALSE);
77
78 /* forbidden_zero_bit */
79 fail_unless (nal_writer_put_bits_uint8 (&nw, 0, 1));
80 /* nal_ref_idc, just set zero for test */
81 fail_unless (nal_writer_put_bits_uint8 (&nw, 0, 2));
82 /* nal_unit_type, unknown h264 nal type */
83 fail_unless (nal_writer_put_bits_uint8 (&nw, 0x1f, 5));
84
85 for (j = 0; j < 3; j++)
86 fail_unless (nal_writer_put_bits_uint8 (&nw, patterns[i][j], 8));
87
88 mem = nal_writer_reset_and_get_memory (&nw);
89
90 fail_unless (mem != NULL);
91 fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
92
93 /* start code prefix 4 + nalu header 1 + written bytes 3 +
94 * emulation prevention byte 1 */
95 assert_equals_int (info.size, (4 + 1 + 3 + 1));
96
97 fail_if (memcmp (info.data + 5, expected_rst[i], 4));
98 gst_memory_unmap (mem, &info);
99 gst_memory_unref (mem);
100 }
101 }
102
103 GST_END_TEST;
104
105 static Suite *
nalutils_suite(void)106 nalutils_suite (void)
107 {
108 Suite *s = suite_create ("H264/H264 Nal Utils");
109
110 TCase *tc_chain = tcase_create ("general");
111
112 suite_add_tcase (s, tc_chain);
113 tcase_add_test (tc_chain, test_nal_writer_init);
114 tcase_add_test (tc_chain, test_nal_writer_emulation_preventation);
115
116 return s;
117 }
118
119 GST_CHECK_MAIN (nalutils);
120