h264/5timestamper: provide a workaround for h264/5parse producing pts=NONE buffers
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / tests / check / elements / h264timestamper.c
1 /*
2  * GStreamer
3  *
4  * unit test for h264timestamper
5  *
6  * Copyright (C) 2022 Matthew Waters <matthew@centricular.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/check/check.h>
29 #include <gst/video/video.h>
30 /* SPS */
31 static guint8 h264_sps[] = {
32   0x00, 0x00, 0x00, 0x01, 0x67, 0x4d, 0x40, 0x15,
33   0xec, 0xa4, 0xbf, 0x2e, 0x02, 0x20, 0x00, 0x00,
34   0x03, 0x00, 0x2e, 0xe6, 0xb2, 0x80, 0x01, 0xe2,
35   0xc5, 0xb2, 0xc0
36 };
37
38 /* PPS */
39 static guint8 h264_pps[] = {
40   0x00, 0x00, 0x00, 0x01, 0x68, 0xeb, 0xec, 0xb2
41 };
42
43 /* keyframes all around */
44 static guint8 h264_idrframe[] = {
45   0x00, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x00,
46   0x10, 0xff, 0xfe, 0xf6, 0xf0, 0xfe, 0x05, 0x36,
47   0x56, 0x04, 0x50, 0x96, 0x7b, 0x3f, 0x53, 0xe1
48 };
49
50 static GstBuffer *
51 create_keyframe_with_sps_pps (void)
52 {
53   gsize size =
54       G_N_ELEMENTS (h264_sps) + G_N_ELEMENTS (h264_pps) +
55       G_N_ELEMENTS (h264_idrframe);
56   GstBuffer *buffer = gst_buffer_new_allocate (NULL, size, NULL);
57   GstMapInfo map_info;
58   gsize offset = 0;
59
60   g_assert (gst_buffer_map (buffer, &map_info, GST_MAP_WRITE));
61   memcpy (&map_info.data[offset], h264_sps, G_N_ELEMENTS (h264_sps));
62   offset += G_N_ELEMENTS (h264_sps);
63   memcpy (&map_info.data[offset], h264_pps, G_N_ELEMENTS (h264_pps));
64   offset += G_N_ELEMENTS (h264_pps);
65   memcpy (&map_info.data[offset], h264_idrframe, G_N_ELEMENTS (h264_idrframe));
66   offset += G_N_ELEMENTS (h264_idrframe);
67
68   gst_buffer_unmap (buffer, &map_info);
69
70   return buffer;
71 }
72
73 GST_START_TEST (test_input_dts_none)
74 {
75   GstHarness *h = gst_harness_new ("h264timestamper");
76   GstBuffer *buffer;
77   int i;
78
79   gst_harness_set_src_caps_str (h,
80       "video/x-h264,stream-format=byte-stream,alignment=au");
81   gst_harness_set_sink_caps_str (h,
82       "video/x-h264,stream-format=byte-stream,alignment=au");
83
84   buffer = create_keyframe_with_sps_pps ();
85   GST_BUFFER_PTS (buffer) = 0;
86   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
87   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
88   GST_BUFFER_PTS (buffer) = 1 * GST_MSECOND;
89   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
90   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
91   GST_BUFFER_PTS (buffer) = 2 * GST_MSECOND;
92   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
93   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
94   GST_BUFFER_PTS (buffer) = 3 * GST_MSECOND;
95   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
96   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
97   GST_BUFFER_PTS (buffer) = 4 * GST_MSECOND;
98   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
99
100   gst_harness_push_event (h, gst_event_new_eos ());
101
102   for (i = 0; i < 5; i++) {
103     buffer = gst_harness_pull (h);
104     fail_unless (buffer != NULL);
105     fail_unless (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)));
106     fail_unless (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buffer)));
107     fail_unless (GST_BUFFER_PTS (buffer) >= GST_BUFFER_DTS (buffer));
108     gst_buffer_unref (buffer);
109   }
110
111   gst_harness_teardown (h);
112 }
113
114 GST_END_TEST;
115
116 GST_START_TEST (test_input_pts_none)
117 {
118   GstHarness *h = gst_harness_new ("h264timestamper");
119   GstBuffer *buffer;
120   int i;
121
122   gst_harness_set_src_caps_str (h,
123       "video/x-h264,stream-format=byte-stream,alignment=au");
124   gst_harness_set_sink_caps_str (h,
125       "video/x-h264,stream-format=byte-stream,alignment=au");
126
127   buffer = create_keyframe_with_sps_pps ();
128   GST_BUFFER_PTS (buffer) = 0;
129   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
130   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
131   GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
132   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
133   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
134   GST_BUFFER_PTS (buffer) = 2 * GST_MSECOND;
135   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
136   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
137   GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
138   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
139   buffer = gst_buffer_new_memdup (h264_idrframe, G_N_ELEMENTS (h264_idrframe));
140   GST_BUFFER_PTS (buffer) = 4 * GST_MSECOND;
141   fail_unless_equals_int (gst_harness_push (h, buffer), GST_FLOW_OK);;
142
143   gst_harness_push_event (h, gst_event_new_eos ());
144
145   for (i = 0; i < 5; i++) {
146     buffer = gst_harness_pull (h);
147     fail_unless (buffer != NULL);
148     fail_unless (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)));
149     fail_unless (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buffer)));
150     fail_unless (GST_BUFFER_PTS (buffer) >= GST_BUFFER_DTS (buffer));
151     gst_buffer_unref (buffer);
152   }
153
154   gst_harness_teardown (h);
155 }
156
157 GST_END_TEST;
158 static Suite *
159 h264timestamper_suite (void)
160 {
161   Suite *s = suite_create ("h264timestamper");
162   TCase *tc = tcase_create ("general");
163
164   tcase_add_test (tc, test_input_dts_none);
165   tcase_add_test (tc, test_input_pts_none);
166
167   suite_add_tcase (s, tc);
168
169   return s;
170 }
171
172 GST_CHECK_MAIN (h264timestamper);