82797fa9d7ce694b5b5679494da779711f537a4d
[platform/upstream/gstreamer.git] / sys / v4l2codecs / gstv4l2codech264dec.c
1 /* GStreamer
2  * Copyright (C) 2020 Nicolas Dufresne <nicolas.dufresne@collabora.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "gstv4l2codecallocator.h"
25 #include "gstv4l2codech264dec.h"
26 #include "gstv4l2codecpool.h"
27 #include "linux/h264-ctrls.h"
28
29 GST_DEBUG_CATEGORY_STATIC (v4l2_h264dec_debug);
30 #define GST_CAT_DEFAULT v4l2_h264dec_debug
31
32 enum
33 {
34   PROP_0,
35   PROP_LAST = PROP_0
36 };
37
38 static GstStaticPadTemplate sink_template =
39 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SINK_NAME,
40     GST_PAD_SINK, GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("video/x-h264, "
42         "stream-format=(string) byte-stream, alignment=(string) au")
43     );
44
45 static GstStaticPadTemplate src_template =
46 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SRC_NAME,
47     GST_PAD_SRC, GST_PAD_ALWAYS,
48     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ NV12, YUY2 }")));
49
50 struct _GstV4l2CodecH264Dec
51 {
52   GstH264Decoder parent;
53   GstV4l2Decoder *decoder;
54   GstVideoCodecState *output_state;
55   GstVideoInfo vinfo;
56   gint display_width;
57   gint display_height;
58   gint coded_width;
59   gint coded_height;
60   guint bitdepth;
61   guint chroma_format_idc;
62
63   GstV4l2CodecAllocator *sink_allocator;
64   GstV4l2CodecAllocator *src_allocator;
65   GstV4l2CodecPool *src_pool;
66   gint min_pool_size;
67   gboolean has_videometa;
68 };
69
70 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstV4l2CodecH264Dec,
71     gst_v4l2_codec_h264_dec, GST_TYPE_H264_DECODER,
72     GST_DEBUG_CATEGORY_INIT (v4l2_h264dec_debug, "v4l2codecs-h264dec", 0,
73         "V4L2 stateless h264 decoder"));
74 #define parent_class gst_v4l2_codec_h264_dec_parent_class
75
76 static gboolean
77 gst_v4l2_codec_h264_dec_open (GstVideoDecoder * decoder)
78 {
79   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
80
81   if (!gst_v4l2_decoder_open (self->decoder)) {
82     GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
83         ("Failed to open H264 decoder"),
84         ("gst_v4l2_decoder_open() failed: %s", g_strerror (errno)));
85     return FALSE;
86   }
87
88   return TRUE;
89 }
90
91 static gboolean
92 gst_v4l2_codec_h264_dec_close (GstVideoDecoder * decoder)
93 {
94   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
95   gst_v4l2_decoder_close (self->decoder);
96   return TRUE;
97 }
98
99 static gboolean
100 gst_v4l2_codec_h264_dec_stop (GstVideoDecoder * decoder)
101 {
102   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
103
104   if (self->sink_allocator) {
105     gst_v4l2_codec_allocator_detach (self->sink_allocator);
106     g_clear_object (&self->sink_allocator);
107   }
108
109   if (self->src_allocator) {
110     gst_v4l2_codec_allocator_detach (self->src_allocator);
111     g_clear_object (&self->src_allocator);
112     g_clear_object (&self->src_pool);
113   }
114
115   if (self->output_state)
116     gst_video_codec_state_unref (self->output_state);
117   self->output_state = NULL;
118
119   return GST_VIDEO_DECODER_CLASS (parent_class)->stop (decoder);
120 }
121
122 static gboolean
123 gst_v4l2_codec_h264_dec_negotiate (GstVideoDecoder * decoder)
124 {
125   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
126   GstH264Decoder *h264dec = GST_H264_DECODER (decoder);
127
128   GST_DEBUG_OBJECT (self, "Negotiate");
129
130   if (self->sink_allocator)
131     gst_v4l2_codec_allocator_detach (self->sink_allocator);
132
133   if (self->src_allocator)
134     gst_v4l2_codec_allocator_detach (self->src_allocator);
135
136   if (!gst_v4l2_decoder_set_sink_fmt (self->decoder, V4L2_PIX_FMT_H264_SLICE,
137           self->coded_width, self->coded_height)) {
138     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
139         ("Failed to configure H264 decoder"),
140         ("gst_v4l2_decoder_set_sink_fmt() failed: %s", g_strerror (errno)));
141     gst_v4l2_decoder_close (self->decoder);
142     return FALSE;
143   }
144
145   /* TODO set sequence parameter control, this is needed to negotiate a
146    * format with the help of the driver */
147
148   if (!gst_v4l2_decoder_select_src_format (self->decoder, &self->vinfo)) {
149     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
150         ("Unsupported bitdepth/chroma format"),
151         ("No support for %ux%u %ubit chroma IDC %i", self->coded_width,
152             self->coded_height, self->bitdepth, self->chroma_format_idc));
153     return FALSE;
154   }
155
156   /* TODO some decoders supports color convertion and scaling */
157
158   if (self->output_state)
159     gst_video_codec_state_unref (self->output_state);
160
161   self->output_state =
162       gst_video_decoder_set_output_state (GST_VIDEO_DECODER (self),
163       self->vinfo.finfo->format, self->display_width,
164       self->display_height, h264dec->input_state);
165
166   self->output_state->caps = gst_video_info_to_caps (&self->output_state->info);
167
168   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
169 }
170
171 static gboolean
172 gst_v4l2_codec_h264_dec_decide_allocation (GstVideoDecoder * decoder,
173     GstQuery * query)
174 {
175   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
176   guint min = 0;
177
178   self->has_videometa = gst_query_find_allocation_meta (query,
179       GST_VIDEO_META_API_TYPE, NULL);
180
181   g_clear_object (&self->src_pool);
182   g_clear_object (&self->src_allocator);
183
184   if (gst_query_get_n_allocation_pools (query) > 0)
185     gst_query_parse_nth_allocation_pool (query, 0, NULL, NULL, &min, NULL);
186
187   min = MAX (2, min);
188
189   self->sink_allocator = gst_v4l2_codec_allocator_new (self->decoder,
190       GST_PAD_SINK, self->min_pool_size + 2);
191   self->src_allocator = gst_v4l2_codec_allocator_new (self->decoder,
192       GST_PAD_SRC, self->min_pool_size + min + 4);
193   self->src_pool = gst_v4l2_codec_pool_new (self->src_allocator, &self->vinfo);
194
195   /* Our buffer pool is internal, we will let the base class create a video
196    * pool, and use it if we are running out of buffers or if downstream does
197    * not support GstVideoMeta */
198   return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
199       (decoder, query);
200 }
201
202 static gboolean
203 gst_v4l2_codec_h264_dec_new_sequence (GstH264Decoder * decoder,
204     const GstH264SPS * sps, gint max_dpb_size)
205 {
206   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
207   gint crop_width = sps->width;
208   gint crop_height = sps->height;
209   gboolean negotiation_needed = FALSE;
210
211   if (self->vinfo.finfo->format == GST_VIDEO_FORMAT_UNKNOWN)
212     negotiation_needed = TRUE;
213
214   /* TODO check if CREATE_BUFS is supported, and simply grow the pool */
215   if (self->min_pool_size < max_dpb_size) {
216     self->min_pool_size = max_dpb_size;
217     negotiation_needed = TRUE;
218   }
219
220   if (sps->frame_cropping_flag) {
221     crop_width = sps->crop_rect_width;
222     crop_height = sps->crop_rect_height;
223   }
224
225   /* TODO Check if current buffers are large enough, and reuse them */
226   if (self->display_width != crop_width || self->display_height != crop_height
227       || self->coded_width != sps->width || self->coded_height != sps->height) {
228     self->display_width = crop_width;
229     self->display_height = crop_height;
230     self->coded_width = sps->width;
231     self->coded_height = sps->height;
232     negotiation_needed = TRUE;
233     GST_INFO_OBJECT (self, "Resolution changed to %dx%d (%ix%i)",
234         self->display_width, self->display_height,
235         self->coded_width, self->coded_height);
236   }
237
238   if (self->bitdepth != sps->bit_depth_luma_minus8 + 8) {
239     self->bitdepth = sps->bit_depth_luma_minus8 + 8;
240     negotiation_needed = TRUE;
241     GST_INFO_OBJECT (self, "Bitdepth changed to %u", self->bitdepth);
242   }
243
244   if (self->chroma_format_idc != sps->chroma_format_idc) {
245     self->chroma_format_idc = sps->chroma_format_idc;
246     negotiation_needed = TRUE;
247     GST_INFO_OBJECT (self, "Chroma format changed to %i",
248         self->chroma_format_idc);
249   }
250
251   if (negotiation_needed) {
252     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
253       GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
254       return FALSE;
255     }
256   }
257
258   return TRUE;
259 }
260
261 static gboolean
262 gst_v4l2_codec_h264_dec_start_picture (GstH264Decoder * decoder,
263     GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb)
264 {
265   /* Fill v4l2_ctrl_h264_decode_params */
266   return FALSE;
267 }
268
269 static GstFlowReturn
270 gst_v4l2_codec_h264_dec_output_picture (GstH264Decoder * decoder,
271     GstH264Picture * picture)
272 {
273   /* Fill vl2_ctrl_h264_slice_params */
274   return GST_FLOW_ERROR;
275 }
276
277 static gboolean
278 gst_v4l2_codec_h264_dec_end_picture (GstH264Decoder * decoder,
279     GstH264Picture * picture)
280 {
281   return TRUE;
282 }
283
284 static gboolean
285 gst_v4l2_codec_h264_dec_decode_slice (GstH264Decoder * decoder,
286     GstH264Picture * picture, GstH264Slice * slice)
287 {
288   return TRUE;
289 }
290
291 static void
292 gst_v4l2_codec_h264_dec_set_property (GObject * object, guint prop_id,
293     const GValue * value, GParamSpec * pspec)
294 {
295   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (object);
296   GObject *dec = G_OBJECT (self->decoder);
297
298   switch (prop_id) {
299     default:
300       gst_v4l2_decoder_set_property (dec, prop_id - PROP_LAST, value, pspec);
301       break;
302   }
303 }
304
305 static void
306 gst_v4l2_codec_h264_dec_get_property (GObject * object, guint prop_id,
307     GValue * value, GParamSpec * pspec)
308 {
309   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (object);
310   GObject *dec = G_OBJECT (self->decoder);
311
312   switch (prop_id) {
313     default:
314       gst_v4l2_decoder_get_property (dec, prop_id - PROP_LAST, value, pspec);
315       break;
316   }
317 }
318
319 static void
320 gst_v4l2_codec_h264_dec_init (GstV4l2CodecH264Dec * self)
321 {
322 }
323
324 static void
325 gst_v4l2_codec_h264_dec_subinit (GstV4l2CodecH264Dec * self,
326     GstV4l2CodecH264DecClass * klass)
327 {
328   self->decoder = gst_v4l2_decoder_new (klass->device);
329   gst_video_info_init (&self->vinfo);
330 }
331
332 static void
333 gst_v4l2_codec_h264_dec_dispose (GObject * object)
334 {
335   GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (object);
336
337   g_clear_object (&self->decoder);
338
339   G_OBJECT_CLASS (parent_class)->dispose (object);
340 }
341
342 static void
343 gst_v4l2_codec_h264_dec_class_init (GstV4l2CodecH264DecClass * klass)
344 {
345 }
346
347 static void
348 gst_v4l2_codec_h264_dec_subclass_init (GstV4l2CodecH264DecClass * klass,
349     GstV4l2CodecDevice * device)
350 {
351   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
352   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
353   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
354   GstH264DecoderClass *h264decoder_class = GST_H264_DECODER_CLASS (klass);
355
356   gobject_class->set_property = gst_v4l2_codec_h264_dec_set_property;
357   gobject_class->get_property = gst_v4l2_codec_h264_dec_get_property;
358   gobject_class->dispose = gst_v4l2_codec_h264_dec_dispose;
359
360   gst_element_class_set_static_metadata (element_class,
361       "V4L2 Stateless H.264 Video Decoder",
362       "Codec/Decoder/Video/Hardware",
363       "A V4L2 based H.264 video decoder",
364       "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
365
366   gst_element_class_add_static_pad_template (element_class, &sink_template);
367   gst_element_class_add_static_pad_template (element_class, &src_template);
368
369   decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_open);
370   decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_close);
371   decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_stop);
372   decoder_class->negotiate =
373       GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_negotiate);
374   decoder_class->decide_allocation =
375       GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_decide_allocation);
376
377   h264decoder_class->new_sequence =
378       GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_new_sequence);
379   h264decoder_class->output_picture =
380       GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_output_picture);
381   h264decoder_class->start_picture =
382       GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_start_picture);
383   h264decoder_class->decode_slice =
384       GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_decode_slice);
385   h264decoder_class->end_picture =
386       GST_DEBUG_FUNCPTR (gst_v4l2_codec_h264_dec_end_picture);
387
388   klass->device = device;
389   gst_v4l2_decoder_install_properties (gobject_class, PROP_LAST, device);
390 }
391
392 void
393 gst_v4l2_codec_h264_dec_register (GstPlugin * plugin,
394     GstV4l2CodecDevice * device, guint rank)
395 {
396   GTypeQuery type_query;
397   GTypeInfo type_info = { 0, };
398   GType subtype;
399   gchar *type_name;
400
401   g_type_query (GST_TYPE_V4L2_CODEC_H264_DEC, &type_query);
402   memset (&type_info, 0, sizeof (type_info));
403   type_info.class_size = type_query.class_size;
404   type_info.instance_size = type_query.instance_size;
405   type_info.class_init = (GClassInitFunc) gst_v4l2_codec_h264_dec_subclass_init;
406   type_info.class_data = gst_mini_object_ref (GST_MINI_OBJECT (device));
407   type_info.instance_init = (GInstanceInitFunc) gst_v4l2_codec_h264_dec_subinit;
408   GST_MINI_OBJECT_FLAG_SET (device, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
409
410   /* The first decoder to be registered should use a constant name, like
411    * v4l2slh264dec, for any additional decoders, we create unique names. Decoder
412    * names may change between boots, so this should help gain stable names for
413    * the most common use cases. SL stands for state-less, we differentiate
414    * with v4l2h264dec as this element may not have the same properties */
415   type_name = g_strdup ("v4l2slh264dec");
416
417   if (g_type_from_name (type_name) != 0) {
418     gchar *basename = g_path_get_basename (device->video_device_path);
419     g_free (type_name);
420     type_name = g_strdup_printf ("v4l2sl%sh264dec", basename);
421     g_free (basename);
422   }
423
424   subtype = g_type_register_static (GST_TYPE_V4L2_CODEC_H264_DEC, type_name,
425       &type_info, 0);
426
427   if (!gst_element_register (plugin, type_name, rank, subtype))
428     GST_WARNING ("Failed to register plugin '%s'", type_name);
429
430   g_free (type_name);
431 }