videocodectestsink: Add YUV422 support
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / debugutils / gstvideocodectestsink.c
1 /* GStreamer
2  * Copyright (C) 2021 Collabora Ltd.
3  *   Author: Nicolas Dufresne <nicolas.dufresne@collabora.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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <gst/video/video.h>
27 #include <gio/gio.h>
28
29 #include "gstdebugutilsbadelements.h"
30 #include "gstvideocodectestsink.h"
31
32 /**
33  * SECTION:videocodectestsink
34  *
35  * An element that computes the checksum of a video stream and/or writes back its
36  * raw I420 data ignoring the padding introduced by GStreamer. This element is
37  * meant to be used for CODEC conformance testing. It also supports producing an I420
38  * checksum and and can write out a file in I420 layout directly from NV12 input
39  * data.
40  *
41  * The checksum is communicated back to the application just before EOS
42  * message with an element message of type `conformance/checksum` with the
43  * following fields:
44  *
45  * * "checksum-type"  G_TYPE_STRING The checksum type (only MD5 is supported)
46  * * "checksum"       G_TYPE_STRING The checksum as a string
47  *
48  * ## Example launch lines
49  * |[
50  * gst-launch-1.0 videotestsrc num-buffers=2 ! videocodectestsink location=true-raw.yuv -m
51  * ]|
52  *
53  * Since: 1.20
54  */
55
56 enum
57 {
58   PROP_0,
59   PROP_LOCATION,
60 };
61
62 struct _GstVideoCodecTestSink
63 {
64   GstBaseSink parent;
65   GChecksumType hash;
66
67   /* protect with stream lock */
68   GstVideoInfo vinfo;
69     GstFlowReturn (*process) (GstVideoCodecTestSink * self,
70       GstVideoFrame * frame);
71   GOutputStream *ostream;
72   GChecksum *checksum;
73
74   /* protect with object lock */
75   gchar *location;
76 };
77
78 static GstStaticPadTemplate gst_video_codec_test_sink_template =
79 GST_STATIC_PAD_TEMPLATE ("sink",
80     GST_PAD_SINK,
81     GST_PAD_ALWAYS,
82     GST_STATIC_CAPS ("video/x-raw, format = { "
83         "I422_10LE, I420_10LE, Y42B, I420, NV12 }"));
84
85 #define gst_video_codec_test_sink_parent_class parent_class
86 G_DEFINE_TYPE (GstVideoCodecTestSink, gst_video_codec_test_sink,
87     GST_TYPE_BASE_SINK);
88 GST_ELEMENT_REGISTER_DEFINE (videocodectestsink, "videocodectestsink",
89     GST_RANK_NONE, gst_video_codec_test_sink_get_type ());
90
91 static void
92 gst_video_codec_test_sink_set_property (GObject * object, guint prop_id,
93     const GValue * value, GParamSpec * pspec)
94 {
95   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (object);
96
97   GST_OBJECT_LOCK (self);
98
99   switch (prop_id) {
100     case PROP_LOCATION:
101       g_free (self->location);
102       self->location = g_value_dup_string (value);
103       break;
104     default:
105       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106       break;
107   }
108
109   GST_OBJECT_UNLOCK (self);
110 }
111
112 static void
113 gst_video_codec_test_sink_get_property (GObject * object, guint prop_id,
114     GValue * value, GParamSpec * pspec)
115 {
116   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (object);
117
118   GST_OBJECT_LOCK (self);
119
120   switch (prop_id) {
121     case PROP_LOCATION:
122       g_value_set_string (value, self->location);
123       break;
124     default:
125       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126       break;
127   }
128
129   GST_OBJECT_UNLOCK (self);
130 }
131
132 static gboolean
133 gst_video_codec_test_sink_start (GstBaseSink * sink)
134 {
135   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (sink);
136   GError *error = NULL;
137   GFile *file = NULL;
138   gboolean ret = TRUE;
139
140   GST_OBJECT_LOCK (self);
141
142   self->checksum = g_checksum_new (self->hash);
143   if (self->location)
144     file = g_file_new_for_path (self->location);
145
146   GST_OBJECT_UNLOCK (self);
147
148   if (file) {
149     self->ostream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
150             G_FILE_CREATE_REPLACE_DESTINATION, NULL, &error));
151     if (!self->ostream) {
152       GST_ELEMENT_ERROR (self, RESOURCE, WRITE,
153           ("Failed to open '%s' for writing.", self->location),
154           ("Open failed failed: %s", error->message));
155       g_error_free (error);
156       ret = FALSE;
157     }
158
159     g_object_unref (file);
160   }
161
162   return ret;
163 }
164
165 static gboolean
166 gst_video_codec_test_sink_stop (GstBaseSink * sink)
167 {
168   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (sink);
169
170   g_checksum_free (self->checksum);
171   self->checksum = NULL;
172
173   if (self->ostream) {
174     GError *error = NULL;
175
176     if (!g_output_stream_close (self->ostream, NULL, &error)) {
177       GST_ELEMENT_WARNING (self, RESOURCE, CLOSE,
178           ("Did not close '%s' properly", self->location),
179           ("Failed to close stream: %s", error->message));
180     }
181
182     g_clear_object (&self->ostream);
183   }
184
185   return TRUE;
186 }
187
188 static GstFlowReturn
189 gst_video_codec_test_sink_process_data (GstVideoCodecTestSink * self,
190     const guchar * data, gssize length)
191 {
192   GError *error = NULL;
193
194   g_checksum_update (self->checksum, data, length);
195
196   if (!self->ostream)
197     return GST_FLOW_OK;
198
199   if (!g_output_stream_write_all (self->ostream, data, length, NULL, NULL,
200           &error)) {
201     GST_ELEMENT_ERROR (self, RESOURCE, WRITE,
202         ("Failed to write video data into '%s'", self->location),
203         ("Writing %" G_GSIZE_FORMAT " bytes failed: %s", length,
204             error->message));
205     g_error_free (error);
206     return GST_FLOW_ERROR;
207   }
208
209   return GST_FLOW_OK;
210 }
211
212 static GstFlowReturn
213 gst_video_codec_test_sink_process_i42x (GstVideoCodecTestSink * self,
214     GstVideoFrame * frame)
215 {
216   guint plane;
217
218   for (plane = 0; plane < 3; plane++) {
219     gint y;
220     guint stride;
221     const guchar *data;
222
223     stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, plane);
224     data = GST_VIDEO_FRAME_PLANE_DATA (frame, plane);
225
226     for (y = 0; y < GST_VIDEO_INFO_COMP_HEIGHT (&self->vinfo, plane); y++) {
227       gsize length = GST_VIDEO_INFO_COMP_WIDTH (&self->vinfo, plane) *
228           GST_VIDEO_INFO_COMP_PSTRIDE (&self->vinfo, plane);
229       GstFlowReturn ret;
230
231       ret = gst_video_codec_test_sink_process_data (self, data, length);
232       if (ret != GST_FLOW_OK)
233         return ret;
234
235       data += stride;
236     }
237   }
238
239   return GST_FLOW_OK;
240 }
241
242 static GstFlowReturn
243 gst_video_codec_test_sink_process_nv12 (GstVideoCodecTestSink * self,
244     GstVideoFrame * frame)
245 {
246   gint x, y, comp;
247   guint stride;
248   const guchar *data;
249
250   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
251   data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
252
253   for (y = 0; y < GST_VIDEO_INFO_HEIGHT (&self->vinfo); y++) {
254     gsize length = GST_VIDEO_INFO_WIDTH (&self->vinfo);
255     GstFlowReturn ret;
256
257     ret = gst_video_codec_test_sink_process_data (self, data, length);
258     if (ret != GST_FLOW_OK)
259       return ret;
260
261     data += stride;
262   }
263
264   /* Deinterleave the UV plane */
265   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 1);
266
267   for (comp = 0; comp < 2; comp++) {
268     data = GST_VIDEO_FRAME_PLANE_DATA (frame, 1);
269
270     for (y = 0; y < GST_VIDEO_INFO_COMP_HEIGHT (&self->vinfo, 1); y++) {
271       guint width = GST_ROUND_UP_2 (GST_VIDEO_INFO_WIDTH (&self->vinfo)) / 2;
272
273       for (x = 0; x < width; x++) {
274         GstFlowReturn ret;
275
276         ret = gst_video_codec_test_sink_process_data (self,
277             data + 2 * x + comp, 1);
278
279         if (ret != GST_FLOW_OK)
280           return ret;
281       }
282
283       data += stride;
284     }
285   }
286
287   return GST_FLOW_OK;
288 }
289
290 static GstFlowReturn
291 gst_video_codec_test_sink_render (GstBaseSink * sink, GstBuffer * buffer)
292 {
293   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (sink);
294   GstVideoFrame frame;
295
296   if (!gst_video_frame_map (&frame, &self->vinfo, buffer, GST_MAP_READ))
297     return GST_FLOW_ERROR;
298
299   self->process (self, &frame);
300
301   gst_video_frame_unmap (&frame);
302   return GST_FLOW_OK;
303 }
304
305 static gboolean
306 gst_video_codec_test_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
307 {
308   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (sink);
309
310   if (!gst_video_info_from_caps (&self->vinfo, caps))
311     return FALSE;
312
313   switch (GST_VIDEO_INFO_FORMAT (&self->vinfo)) {
314     case GST_VIDEO_FORMAT_I420:
315     case GST_VIDEO_FORMAT_I420_10LE:
316     case GST_VIDEO_FORMAT_Y42B:
317     case GST_VIDEO_FORMAT_I422_10LE:
318       self->process = gst_video_codec_test_sink_process_i42x;
319       break;
320     case GST_VIDEO_FORMAT_NV12:
321       self->process = gst_video_codec_test_sink_process_nv12;
322       break;
323     default:
324       g_assert_not_reached ();
325       break;
326   }
327
328   return TRUE;
329 }
330
331 static gboolean
332 gst_video_codec_test_sink_propose_allocation (GstBaseSink * sink,
333     GstQuery * query)
334 {
335   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
336   return TRUE;
337 }
338
339 static gboolean
340 gst_video_codec_test_sink_event (GstBaseSink * sink, GstEvent * event)
341 {
342   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (sink);
343
344   if (event->type == GST_EVENT_EOS) {
345     const gchar *checksum_type = "UNKNOWN";
346
347     switch (self->hash) {
348       case G_CHECKSUM_MD5:
349         checksum_type = "MD5";
350         break;
351       case G_CHECKSUM_SHA1:
352         checksum_type = "SHA1";
353         break;
354       case G_CHECKSUM_SHA256:
355         checksum_type = "SHA256";
356         break;
357       case G_CHECKSUM_SHA512:
358         checksum_type = "SHA512";
359         break;
360       case G_CHECKSUM_SHA384:
361         checksum_type = "SHA384";
362         break;
363       default:
364         g_assert_not_reached ();
365         break;
366     }
367
368     gst_element_post_message (GST_ELEMENT (self),
369         gst_message_new_element (GST_OBJECT (self),
370             gst_structure_new ("conformance/checksum", "checksum-type",
371                 G_TYPE_STRING, checksum_type, "checksum", G_TYPE_STRING,
372                 g_checksum_get_string (self->checksum), NULL)));
373     g_checksum_reset (self->checksum);
374   }
375
376   return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
377 }
378
379 static void
380 gst_video_codec_test_sink_init (GstVideoCodecTestSink * sink)
381 {
382   gst_base_sink_set_sync (GST_BASE_SINK (sink), FALSE);
383   sink->hash = G_CHECKSUM_MD5;
384 }
385
386 static void
387 gst_video_codec_test_sink_finalize (GObject * object)
388 {
389   GstVideoCodecTestSink *self = GST_VIDEO_CODEC_TEST_SINK (object);
390
391   g_free (self->location);
392
393   G_OBJECT_CLASS (parent_class)->finalize (object);
394 }
395
396 static void
397 gst_video_codec_test_sink_class_init (GstVideoCodecTestSinkClass * klass)
398 {
399   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
400   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
401   GstBaseSinkClass *base_sink_class = GST_BASE_SINK_CLASS (klass);
402
403   gobject_class->set_property = gst_video_codec_test_sink_set_property;
404   gobject_class->get_property = gst_video_codec_test_sink_get_property;
405   gobject_class->finalize = gst_video_codec_test_sink_finalize;
406
407   base_sink_class->start = GST_DEBUG_FUNCPTR (gst_video_codec_test_sink_start);
408   base_sink_class->stop = GST_DEBUG_FUNCPTR (gst_video_codec_test_sink_stop);
409   base_sink_class->render =
410       GST_DEBUG_FUNCPTR (gst_video_codec_test_sink_render);
411   base_sink_class->set_caps =
412       GST_DEBUG_FUNCPTR (gst_video_codec_test_sink_set_caps);
413   base_sink_class->propose_allocation =
414       GST_DEBUG_FUNCPTR (gst_video_codec_test_sink_propose_allocation);
415   base_sink_class->event = GST_DEBUG_FUNCPTR (gst_video_codec_test_sink_event);
416
417   gst_element_class_add_static_pad_template (element_class,
418       &gst_video_codec_test_sink_template);
419
420   g_object_class_install_property (gobject_class, PROP_LOCATION,
421       g_param_spec_string ("location", "Location",
422           "File path to store non-padded I420 stream (optional).", NULL,
423           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424
425   gst_element_class_set_static_metadata (element_class,
426       "Video CODEC Test Sink", "Debug/video/Sink",
427       "Sink to test video CODEC conformance",
428       "Nicolas Dufresne <nicolas.dufresne@collabora.com");
429 }