2 * Copyright (C) 2020 Nicolas Dufresne <nicolas.dufresne@collabora.com>
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.
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.
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.
24 #include "gstv4l2codecallocator.h"
25 #include "gstv4l2codech264dec.h"
26 #include "gstv4l2codecpool.h"
27 #include "linux/h264-ctrls.h"
29 GST_DEBUG_CATEGORY_STATIC (v4l2_h264dec_debug);
30 #define GST_CAT_DEFAULT v4l2_h264dec_debug
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")
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 }")));
50 struct _GstV4l2CodecH264Dec
52 GstH264Decoder parent;
53 GstV4l2Decoder *decoder;
54 GstVideoCodecState *output_state;
61 guint chroma_format_idc;
63 GstV4l2CodecAllocator *sink_allocator;
64 GstV4l2CodecAllocator *src_allocator;
65 GstV4l2CodecPool *src_pool;
67 gboolean has_videometa;
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
77 gst_v4l2_codec_h264_dec_open (GstVideoDecoder * decoder)
79 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
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)));
92 gst_v4l2_codec_h264_dec_close (GstVideoDecoder * decoder)
94 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
95 gst_v4l2_decoder_close (self->decoder);
100 gst_v4l2_codec_h264_dec_stop (GstVideoDecoder * decoder)
102 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
104 if (self->sink_allocator) {
105 gst_v4l2_codec_allocator_detach (self->sink_allocator);
106 g_clear_object (&self->sink_allocator);
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);
115 if (self->output_state)
116 gst_video_codec_state_unref (self->output_state);
117 self->output_state = NULL;
119 return GST_VIDEO_DECODER_CLASS (parent_class)->stop (decoder);
123 gst_v4l2_codec_h264_dec_negotiate (GstVideoDecoder * decoder)
125 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
126 GstH264Decoder *h264dec = GST_H264_DECODER (decoder);
128 GST_DEBUG_OBJECT (self, "Negotiate");
130 if (self->sink_allocator)
131 gst_v4l2_codec_allocator_detach (self->sink_allocator);
133 if (self->src_allocator)
134 gst_v4l2_codec_allocator_detach (self->src_allocator);
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);
145 /* TODO set sequence parameter control, this is needed to negotiate a
146 * format with the help of the driver */
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));
156 /* TODO some decoders supports color convertion and scaling */
158 if (self->output_state)
159 gst_video_codec_state_unref (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);
166 self->output_state->caps = gst_video_info_to_caps (&self->output_state->info);
168 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
172 gst_v4l2_codec_h264_dec_decide_allocation (GstVideoDecoder * decoder,
175 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (decoder);
178 self->has_videometa = gst_query_find_allocation_meta (query,
179 GST_VIDEO_META_API_TYPE, NULL);
181 g_clear_object (&self->src_pool);
182 g_clear_object (&self->src_allocator);
184 if (gst_query_get_n_allocation_pools (query) > 0)
185 gst_query_parse_nth_allocation_pool (query, 0, NULL, NULL, &min, NULL);
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);
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
203 gst_v4l2_codec_h264_dec_new_sequence (GstH264Decoder * decoder,
204 const GstH264SPS * sps, gint max_dpb_size)
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;
211 if (self->vinfo.finfo->format == GST_VIDEO_FORMAT_UNKNOWN)
212 negotiation_needed = TRUE;
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;
220 if (sps->frame_cropping_flag) {
221 crop_width = sps->crop_rect_width;
222 crop_height = sps->crop_rect_height;
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);
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);
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);
251 if (negotiation_needed) {
252 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
253 GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
262 gst_v4l2_codec_h264_dec_start_picture (GstH264Decoder * decoder,
263 GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb)
265 /* Fill v4l2_ctrl_h264_decode_params */
270 gst_v4l2_codec_h264_dec_output_picture (GstH264Decoder * decoder,
271 GstH264Picture * picture)
273 /* Fill vl2_ctrl_h264_slice_params */
274 return GST_FLOW_ERROR;
278 gst_v4l2_codec_h264_dec_end_picture (GstH264Decoder * decoder,
279 GstH264Picture * picture)
285 gst_v4l2_codec_h264_dec_decode_slice (GstH264Decoder * decoder,
286 GstH264Picture * picture, GstH264Slice * slice)
292 gst_v4l2_codec_h264_dec_set_property (GObject * object, guint prop_id,
293 const GValue * value, GParamSpec * pspec)
295 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (object);
296 GObject *dec = G_OBJECT (self->decoder);
300 gst_v4l2_decoder_set_property (dec, prop_id - PROP_LAST, value, pspec);
306 gst_v4l2_codec_h264_dec_get_property (GObject * object, guint prop_id,
307 GValue * value, GParamSpec * pspec)
309 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (object);
310 GObject *dec = G_OBJECT (self->decoder);
314 gst_v4l2_decoder_get_property (dec, prop_id - PROP_LAST, value, pspec);
320 gst_v4l2_codec_h264_dec_init (GstV4l2CodecH264Dec * self)
325 gst_v4l2_codec_h264_dec_subinit (GstV4l2CodecH264Dec * self,
326 GstV4l2CodecH264DecClass * klass)
328 self->decoder = gst_v4l2_decoder_new (klass->device);
329 gst_video_info_init (&self->vinfo);
333 gst_v4l2_codec_h264_dec_dispose (GObject * object)
335 GstV4l2CodecH264Dec *self = GST_V4L2_CODEC_H264_DEC (object);
337 g_clear_object (&self->decoder);
339 G_OBJECT_CLASS (parent_class)->dispose (object);
343 gst_v4l2_codec_h264_dec_class_init (GstV4l2CodecH264DecClass * klass)
348 gst_v4l2_codec_h264_dec_subclass_init (GstV4l2CodecH264DecClass * klass,
349 GstV4l2CodecDevice * device)
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);
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;
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>");
366 gst_element_class_add_static_pad_template (element_class, &sink_template);
367 gst_element_class_add_static_pad_template (element_class, &src_template);
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);
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);
388 klass->device = device;
389 gst_v4l2_decoder_install_properties (gobject_class, PROP_LAST, device);
393 gst_v4l2_codec_h264_dec_register (GstPlugin * plugin,
394 GstV4l2CodecDevice * device, guint rank)
396 GTypeQuery type_query;
397 GTypeInfo type_info = { 0, };
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);
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");
417 if (g_type_from_name (type_name) != 0) {
418 gchar *basename = g_path_get_basename (device->video_device_path);
420 type_name = g_strdup_printf ("v4l2sl%sh264dec", basename);
424 subtype = g_type_register_static (GST_TYPE_V4L2_CODEC_H264_DEC, type_name,
427 if (!gst_element_register (plugin, type_name, rank, subtype))
428 GST_WARNING ("Failed to register plugin '%s'", type_name);