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 "gstv4l2codecpool.h"
26 #include "gstv4l2decoder.h"
27 #include "gstv4l2format.h"
28 #include "linux/media.h"
29 #include "linux/videodev2.h"
32 #include <sys/ioctl.h>
34 #include <sys/types.h>
37 #include <gst/base/base.h>
39 GST_DEBUG_CATEGORY (v4l2_decoder_debug);
40 #define GST_CAT_DEFAULT v4l2_decoder_debug
49 struct _GstV4l2Request
54 GstV4l2Decoder *decoder;
65 gboolean hold_pic_buf;
69 struct _GstV4l2Decoder
76 GstQueueArray *request_pool;
77 GstQueueArray *pending_requests;
80 enum v4l2_buf_type src_buf_type;
81 enum v4l2_buf_type sink_buf_type;
90 G_DEFINE_TYPE_WITH_CODE (GstV4l2Decoder, gst_v4l2_decoder, GST_TYPE_OBJECT,
91 GST_DEBUG_CATEGORY_INIT (v4l2_decoder_debug, "v4l2codecs-decoder", 0,
92 "V4L2 stateless decoder helper"));
94 static void gst_v4l2_request_free (GstV4l2Request * request);
97 direction_to_buffer_type (GstV4l2Decoder * self, GstPadDirection direction)
99 if (direction == GST_PAD_SRC)
100 return self->src_buf_type;
102 return self->sink_buf_type;
106 gst_v4l2_decoder_finalize (GObject * obj)
108 GstV4l2Decoder *self = GST_V4L2_DECODER (obj);
110 gst_v4l2_decoder_close (self);
112 g_free (self->media_device);
113 g_free (self->video_device);
114 gst_queue_array_free (self->request_pool);
116 G_OBJECT_CLASS (gst_v4l2_decoder_parent_class)->finalize (obj);
120 gst_v4l2_decoder_init (GstV4l2Decoder * self)
122 self->request_pool = gst_queue_array_new (16);
123 self->pending_requests = gst_queue_array_new (16);
127 gst_v4l2_decoder_class_init (GstV4l2DecoderClass * klass)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 gobject_class->finalize = gst_v4l2_decoder_finalize;
132 gobject_class->get_property = gst_v4l2_decoder_get_property;
133 gobject_class->set_property = gst_v4l2_decoder_set_property;
135 gst_v4l2_decoder_install_properties (gobject_class, 0, NULL);
139 gst_v4l2_decoder_new (GstV4l2CodecDevice * device)
141 GstV4l2Decoder *decoder;
143 g_return_val_if_fail (device->function == MEDIA_ENT_F_PROC_VIDEO_DECODER,
146 decoder = g_object_new (GST_TYPE_V4L2_DECODER,
147 "media-device", device->media_device_path,
148 "video-device", device->video_device_path, NULL);
150 return gst_object_ref_sink (decoder);
154 gst_v4l2_decoder_get_version (GstV4l2Decoder * self)
156 return self->version;
160 gst_v4l2_decoder_open (GstV4l2Decoder * self)
163 struct v4l2_capability querycap;
164 guint32 capabilities;
166 self->media_fd = open (self->media_device, 0);
167 if (self->media_fd < 0) {
168 GST_ERROR_OBJECT (self, "Failed to open '%s': %s",
169 self->media_device, g_strerror (errno));
173 self->video_fd = open (self->video_device, O_NONBLOCK);
174 if (self->video_fd < 0) {
175 GST_ERROR_OBJECT (self, "Failed to open '%s': %s",
176 self->video_device, g_strerror (errno));
180 ret = ioctl (self->video_fd, VIDIOC_QUERYCAP, &querycap);
182 GST_ERROR_OBJECT (self, "VIDIOC_QUERYCAP failed: %s", g_strerror (errno));
183 gst_v4l2_decoder_close (self);
187 self->version = querycap.version;
189 if (querycap.capabilities & V4L2_CAP_DEVICE_CAPS)
190 capabilities = querycap.device_caps;
192 capabilities = querycap.capabilities;
194 if (capabilities & V4L2_CAP_VIDEO_M2M_MPLANE) {
195 self->sink_buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
196 self->src_buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
198 } else if (capabilities & V4L2_CAP_VIDEO_M2M) {
199 self->sink_buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
200 self->src_buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
201 self->mplane = FALSE;
203 GST_ERROR_OBJECT (self, "Unsupported memory-2-memory device.");
204 gst_v4l2_decoder_close (self);
214 gst_v4l2_decoder_close (GstV4l2Decoder * self)
216 GstV4l2Request *request;
218 while ((request = gst_queue_array_pop_head (self->pending_requests)))
219 gst_v4l2_request_unref (request);
221 while ((request = gst_queue_array_pop_head (self->request_pool)))
222 gst_v4l2_request_free (request);
225 close (self->media_fd);
227 close (self->video_fd);
231 self->opened = FALSE;
237 gst_v4l2_decoder_streamon (GstV4l2Decoder * self, GstPadDirection direction)
240 guint32 type = direction_to_buffer_type (self, direction);
242 ret = ioctl (self->video_fd, VIDIOC_STREAMON, &type);
244 GST_ERROR_OBJECT (self, "VIDIOC_STREAMON failed: %s", g_strerror (errno));
252 gst_v4l2_decoder_streamoff (GstV4l2Decoder * self, GstPadDirection direction)
254 guint32 type = direction_to_buffer_type (self, direction);
257 if (direction == GST_PAD_SRC) {
258 GstV4l2Request *pending_req;
260 /* STREAMOFF have the effect of cancelling all requests and unqueuing all
261 * buffers, so clear the pending request list */
262 while ((pending_req = gst_queue_array_pop_head (self->pending_requests))) {
263 g_clear_pointer (&pending_req->bitstream, gst_memory_unref);
264 pending_req->pending = FALSE;
265 gst_v4l2_request_unref (pending_req);
269 ret = ioctl (self->video_fd, VIDIOC_STREAMOFF, &type);
271 GST_ERROR_OBJECT (self, "VIDIOC_STREAMOFF failed: %s", g_strerror (errno));
279 gst_v4l2_decoder_flush (GstV4l2Decoder * self)
281 /* We ignore streamoff failure as it's not relevant, if we manage to
282 * streamon again, we are good. */
283 gst_v4l2_decoder_streamoff (self, GST_PAD_SINK);
284 gst_v4l2_decoder_streamoff (self, GST_PAD_SRC);
286 return gst_v4l2_decoder_streamon (self, GST_PAD_SINK) &&
287 gst_v4l2_decoder_streamon (self, GST_PAD_SRC);
291 gst_v4l2_decoder_enum_sink_fmt (GstV4l2Decoder * self, gint i,
294 struct v4l2_fmtdesc fmtdesc = { i, self->sink_buf_type, };
297 g_return_val_if_fail (self->opened, FALSE);
299 ret = ioctl (self->video_fd, VIDIOC_ENUM_FMT, &fmtdesc);
302 GST_ERROR_OBJECT (self, "VIDIOC_ENUM_FMT failed: %s", g_strerror (errno));
306 GST_DEBUG_OBJECT (self, "Found format %" GST_FOURCC_FORMAT " (%s)",
307 GST_FOURCC_ARGS (fmtdesc.pixelformat), fmtdesc.description);
308 *out_fmt = fmtdesc.pixelformat;
314 gst_v4l2_decoder_set_sink_fmt (GstV4l2Decoder * self, guint32 pix_fmt,
315 gint width, gint height, gint pixel_bitdepth)
317 struct v4l2_format format = (struct v4l2_format) {
318 .type = self->sink_buf_type,
319 /* Compatible with .fmt.pix for these field */
320 .fmt.pix_mp = (struct v4l2_pix_format_mplane) {
321 .pixelformat = pix_fmt,
327 /* Using raw image size for now, it is guarantied to be large enough */
328 gsize sizeimage = (width * height * pixel_bitdepth) / 8;
331 format.fmt.pix_mp.plane_fmt[0].sizeimage = sizeimage;
333 format.fmt.pix.sizeimage = sizeimage;
335 ret = ioctl (self->video_fd, VIDIOC_S_FMT, &format);
337 GST_ERROR_OBJECT (self, "VIDIOC_S_FMT failed: %s", g_strerror (errno));
341 if (format.fmt.pix_mp.pixelformat != pix_fmt
342 || format.fmt.pix_mp.width < width || format.fmt.pix_mp.height < height) {
343 GST_WARNING_OBJECT (self, "Failed to set sink format to %"
344 GST_FOURCC_FORMAT " %ix%i", GST_FOURCC_ARGS (pix_fmt), width, height);
353 gst_v4l2_decoder_enum_src_formats (GstV4l2Decoder * self)
356 struct v4l2_format fmt = {
357 .type = self->src_buf_type,
359 GstVideoFormat format;
361 GValue list = G_VALUE_INIT;
362 GValue value = G_VALUE_INIT;
365 g_return_val_if_fail (self->opened, FALSE);
367 ret = ioctl (self->video_fd, VIDIOC_G_FMT, &fmt);
369 GST_ERROR_OBJECT (self, "VIDIOC_G_FMT failed: %s", g_strerror (errno));
373 /* We first place a structure with the default pixel format */
374 if (gst_v4l2_format_to_video_format (fmt.fmt.pix_mp.pixelformat, &format))
375 caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING,
376 gst_video_format_to_string (format), NULL);
378 caps = gst_caps_new_empty ();
380 /* And then enumerate other possible formats and place that as a second
381 * structure in the caps */
382 g_value_init (&list, GST_TYPE_LIST);
383 g_value_init (&value, G_TYPE_STRING);
385 for (i = 0; ret >= 0; i++) {
386 struct v4l2_fmtdesc fmtdesc = { i, self->src_buf_type, };
388 ret = ioctl (self->video_fd, VIDIOC_ENUM_FMT, &fmtdesc);
391 GST_ERROR_OBJECT (self, "VIDIOC_ENUM_FMT failed: %s",
396 if (gst_v4l2_format_to_video_format (fmtdesc.pixelformat, &format)) {
397 g_value_set_static_string (&value, gst_video_format_to_string (format));
398 gst_value_list_append_value (&list, &value);
401 g_value_reset (&value);
403 if (gst_value_list_get_size (&list) > 0) {
404 GstStructure *str = gst_structure_new_empty ("video/x-raw");
405 gst_structure_take_value (str, "format", &list);
406 gst_caps_append_structure (caps, str);
408 g_value_reset (&list);
415 gst_v4l2_decoder_select_src_format (GstV4l2Decoder * self, GstCaps * caps,
419 struct v4l2_format fmt = {
420 .type = self->src_buf_type,
423 const gchar *format_str;
424 GstVideoFormat format;
427 if (gst_caps_is_empty (caps))
430 ret = ioctl (self->video_fd, VIDIOC_G_FMT, &fmt);
432 GST_ERROR_OBJECT (self, "VIDIOC_G_FMT failed: %s", g_strerror (errno));
436 caps = gst_caps_make_writable (caps);
437 str = gst_caps_get_structure (caps, 0);
438 gst_structure_fixate_field (str, "format");
440 format_str = gst_structure_get_string (str, "format");
441 format = gst_video_format_from_string (format_str);
443 if (gst_v4l2_format_from_video_format (format, &pix_fmt) &&
444 pix_fmt != fmt.fmt.pix_mp.pixelformat) {
445 GST_DEBUG_OBJECT (self, "Trying to use peer format: %s ", format_str);
446 fmt.fmt.pix_mp.pixelformat = pix_fmt;
448 ret = ioctl (self->video_fd, VIDIOC_S_FMT, &fmt);
450 GST_ERROR_OBJECT (self, "VIDIOC_S_FMT failed: %s", g_strerror (errno));
455 if (!gst_v4l2_format_to_video_info (&fmt, info)) {
456 GST_ERROR_OBJECT (self, "Unsupported V4L2 pixelformat %" GST_FOURCC_FORMAT,
457 GST_FOURCC_ARGS (fmt.fmt.pix_mp.pixelformat));
461 GST_INFO_OBJECT (self, "Selected format %s %ix%i",
462 gst_video_format_to_string (info->finfo->format),
463 info->width, info->height);
469 gst_v4l2_decoder_request_buffers (GstV4l2Decoder * self,
470 GstPadDirection direction, guint num_buffers)
473 struct v4l2_requestbuffers reqbufs = {
474 .count = num_buffers,
475 .memory = V4L2_MEMORY_MMAP,
476 .type = direction_to_buffer_type (self, direction),
479 GST_DEBUG_OBJECT (self, "Requesting %u buffers", num_buffers);
481 ret = ioctl (self->video_fd, VIDIOC_REQBUFS, &reqbufs);
483 GST_ERROR_OBJECT (self, "VIDIOC_REQBUFS failed: %s", g_strerror (errno));
487 return reqbufs.count;
491 gst_v4l2_decoder_export_buffer (GstV4l2Decoder * self,
492 GstPadDirection direction, gint index, gint * fds, gsize * sizes,
493 gsize * offsets, guint * num_fds)
496 struct v4l2_plane planes[GST_VIDEO_MAX_PLANES] = { {0} };
497 struct v4l2_buffer v4l2_buf = {
499 .type = direction_to_buffer_type (self, direction),
503 v4l2_buf.length = GST_VIDEO_MAX_PLANES;
504 v4l2_buf.m.planes = planes;
507 ret = ioctl (self->video_fd, VIDIOC_QUERYBUF, &v4l2_buf);
509 GST_ERROR_OBJECT (self, "VIDIOC_QUERYBUF failed: %s", g_strerror (errno));
514 for (i = 0; i < v4l2_buf.length; i++) {
515 struct v4l2_plane *plane = v4l2_buf.m.planes + i;
516 struct v4l2_exportbuffer expbuf = {
517 .type = direction_to_buffer_type (self, direction),
520 .flags = O_CLOEXEC | O_RDWR,
523 ret = ioctl (self->video_fd, VIDIOC_EXPBUF, &expbuf);
526 GST_ERROR_OBJECT (self, "VIDIOC_EXPBUF failed: %s", g_strerror (errno));
528 for (j = i - 1; j >= 0; j--)
534 *num_fds = v4l2_buf.length;
536 sizes[i] = plane->length;
537 offsets[i] = plane->data_offset;
540 struct v4l2_exportbuffer expbuf = {
541 .type = direction_to_buffer_type (self, direction),
543 .flags = O_CLOEXEC | O_RDWR,
546 ret = ioctl (self->video_fd, VIDIOC_EXPBUF, &expbuf);
548 GST_ERROR_OBJECT (self, "VIDIOC_EXPBUF failed: %s", g_strerror (errno));
554 sizes[0] = v4l2_buf.length;
562 gst_v4l2_decoder_queue_sink_mem (GstV4l2Decoder * self,
563 GstV4l2Request * request, GstMemory * mem, guint32 frame_num, guint flags)
566 gsize bytesused = gst_memory_get_sizes (mem, NULL, NULL);
567 struct v4l2_plane plane = {
568 .bytesused = bytesused,
570 struct v4l2_buffer buf = {
571 .type = self->sink_buf_type,
572 .memory = V4L2_MEMORY_MMAP,
573 .index = gst_v4l2_codec_memory_get_index (mem),
574 .timestamp.tv_usec = frame_num,
575 .request_fd = request->fd,
576 .flags = V4L2_BUF_FLAG_REQUEST_FD | flags,
579 GST_TRACE_OBJECT (self, "Queueing bitstream buffer %i", buf.index);
583 buf.m.planes = &plane;
585 buf.bytesused = bytesused;
588 ret = ioctl (self->video_fd, VIDIOC_QBUF, &buf);
590 GST_ERROR_OBJECT (self, "VIDIOC_QBUF failed: %s", g_strerror (errno));
598 gst_v4l2_decoder_queue_src_buffer (GstV4l2Decoder * self, GstBuffer * buffer)
601 struct v4l2_plane planes[GST_VIDEO_MAX_PLANES];
602 struct v4l2_buffer buf = {
603 .type = self->src_buf_type,
604 .memory = V4L2_MEMORY_MMAP,
605 .index = gst_v4l2_codec_buffer_get_index (buffer),
608 GST_TRACE_OBJECT (self, "Queuing picture buffer %i", buf.index);
611 buf.length = gst_buffer_n_memory (buffer);
612 buf.m.planes = planes;
613 for (i = 0; i < buf.length; i++) {
614 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
616 planes[i] = (struct v4l2_plane) {
617 .bytesused = gst_memory_get_sizes (mem, NULL, NULL),
622 buf.bytesused = gst_buffer_get_size (buffer);
625 ret = ioctl (self->video_fd, VIDIOC_QBUF, &buf);
627 GST_ERROR_OBJECT (self, "VIDIOC_QBUF failed: %s", g_strerror (errno));
635 gst_v4l2_decoder_dequeue_sink (GstV4l2Decoder * self)
638 struct v4l2_plane planes[GST_VIDEO_MAX_PLANES] = { {0} };
639 struct v4l2_buffer buf = {
640 .type = self->sink_buf_type,
641 .memory = V4L2_MEMORY_MMAP,
645 buf.length = GST_VIDEO_MAX_PLANES;
646 buf.m.planes = planes;
649 ret = ioctl (self->video_fd, VIDIOC_DQBUF, &buf);
651 GST_ERROR_OBJECT (self, "VIDIOC_DQBUF failed: %s", g_strerror (errno));
655 GST_TRACE_OBJECT (self, "Dequeued bitstream buffer %i", buf.index);
661 gst_v4l2_decoder_dequeue_src (GstV4l2Decoder * self, guint32 * out_frame_num)
664 struct v4l2_plane planes[GST_VIDEO_MAX_PLANES] = { {0} };
665 struct v4l2_buffer buf = {
666 .type = self->src_buf_type,
667 .memory = V4L2_MEMORY_MMAP,
671 buf.length = GST_VIDEO_MAX_PLANES;
672 buf.m.planes = planes;
675 ret = ioctl (self->video_fd, VIDIOC_DQBUF, &buf);
677 GST_ERROR_OBJECT (self, "VIDIOC_DQBUF failed: %s", g_strerror (errno));
681 *out_frame_num = buf.timestamp.tv_usec;
683 GST_TRACE_OBJECT (self, "Dequeued picture buffer %i", buf.index);
689 gst_v4l2_decoder_set_controls (GstV4l2Decoder * self, GstV4l2Request * request,
690 struct v4l2_ext_control * control, guint count)
693 struct v4l2_ext_controls controls = {
696 .request_fd = request ? request->fd : 0,
697 .which = request ? V4L2_CTRL_WHICH_REQUEST_VAL : 0,
700 ret = ioctl (self->video_fd, VIDIOC_S_EXT_CTRLS, &controls);
702 GST_ERROR_OBJECT (self, "VIDIOC_S_EXT_CTRLS failed: %s",
711 gst_v4l2_decoder_get_controls (GstV4l2Decoder * self,
712 struct v4l2_ext_control * control, guint count)
715 struct v4l2_ext_controls controls = {
720 ret = ioctl (self->video_fd, VIDIOC_G_EXT_CTRLS, &controls);
722 GST_ERROR_OBJECT (self, "VIDIOC_G_EXT_CTRLS failed: %s",
731 gst_v4l2_decoder_query_control_size (GstV4l2Decoder * self,
732 unsigned int control_id, unsigned int *control_size)
735 struct v4l2_query_ext_ctrl control = {
741 ret = ioctl (self->video_fd, VIDIOC_QUERY_EXT_CTRL, &control);
744 * It's not an error if a control is not supported by this driver.
745 * Return false but don't print any error.
749 *control_size = control.elem_size;
754 gst_v4l2_decoder_install_properties (GObjectClass * gobject_class,
755 gint prop_offset, GstV4l2CodecDevice * device)
757 const gchar *media_device_path = NULL;
758 const gchar *video_device_path = NULL;
761 media_device_path = device->media_device_path;
762 video_device_path = device->video_device_path;
765 g_object_class_install_property (gobject_class, PROP_MEDIA_DEVICE,
766 g_param_spec_string ("media-device", "Media Device Path",
767 "Path to the media device node", media_device_path,
768 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
770 g_object_class_install_property (gobject_class, PROP_VIDEO_DEVICE,
771 g_param_spec_string ("video-device", "Video Device Path",
772 "Path to the video device node", video_device_path,
773 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
777 gst_v4l2_decoder_set_property (GObject * object, guint prop_id,
778 const GValue * value, GParamSpec * pspec)
780 GstV4l2Decoder *self = GST_V4L2_DECODER (object);
783 case PROP_MEDIA_DEVICE:
784 g_free (self->media_device);
785 self->media_device = g_value_dup_string (value);
787 case PROP_VIDEO_DEVICE:
788 g_free (self->video_device);
789 self->video_device = g_value_dup_string (value);
792 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
798 gst_v4l2_decoder_get_property (GObject * object, guint prop_id,
799 GValue * value, GParamSpec * pspec)
801 GstV4l2Decoder *self = GST_V4L2_DECODER (object);
804 case PROP_MEDIA_DEVICE:
805 g_value_set_string (value, self->media_device);
807 case PROP_VIDEO_DEVICE:
808 g_value_set_string (value, self->video_device);
811 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
817 * gst_v4l2_decoder_register:
818 * @plugin: a #GstPlugin
819 * @dec_type: A #GType for the codec
820 * @class_init: The #GClassInitFunc for #dec_type
821 * @instance_init: The #GInstanceInitFunc for #dec_type
822 * @element_name_tmpl: A string to use for the first codec found and as a template for the next ones.
823 * @device: (transfer full) A #GstV4l2CodecDevice
824 * @rank: The rank to use for the element
825 * @class_data: (nullable) (transfer full) A #gpointer to pass as class_data, set to @device if null
826 * @element_name (nullable) (out) Sets the pointer to the new element name
828 * Registers a decoder element as a subtype of @dec_type for @plugin.
829 * Will create a different sub_types for each subsequent @decoder of the
833 gst_v4l2_decoder_register (GstPlugin * plugin,
834 GType dec_type, GClassInitFunc class_init, gconstpointer class_data,
835 GInstanceInitFunc instance_init, const gchar * element_name_tmpl,
836 GstV4l2CodecDevice * device, guint rank, gchar ** element_name)
838 GTypeQuery type_query;
839 GTypeInfo type_info = { 0, };
843 g_type_query (dec_type, &type_query);
844 memset (&type_info, 0, sizeof (type_info));
845 type_info.class_size = type_query.class_size;
846 type_info.instance_size = type_query.instance_size;
847 type_info.class_init = class_init;
848 type_info.class_data = class_data;
849 type_info.instance_init = instance_init;
851 if (class_data == device)
852 GST_MINI_OBJECT_FLAG_SET (device, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
854 /* The first decoder to be registered should use a constant name, like
855 * v4l2slvp8dec, for any additional decoders, we create unique names. Decoder
856 * names may change between boots, so this should help gain stable names for
857 * the most common use cases. SL stands for state-less, we differentiate
858 * with v4l2vp8dec as this element may not have the same properties */
859 type_name = g_strdup_printf (element_name_tmpl, "");
861 if (g_type_from_name (type_name) != 0) {
862 gchar *basename = g_path_get_basename (device->video_device_path);
864 type_name = g_strdup_printf (element_name_tmpl, basename);
868 subtype = g_type_register_static (dec_type, type_name, &type_info, 0);
870 if (!gst_element_register (plugin, type_name, rank, subtype)) {
871 GST_WARNING ("Failed to register plugin '%s'", type_name);
877 *element_name = type_name;
883 * gst_v4l2_decoder_alloc_request:
884 * @self a #GstV4l2Decoder pointer
885 * @frame_num: Used as a timestamp to identify references
886 * @bitstream the #GstMemory that holds the bitstream data
887 * @pic_buf the #GstBuffer holding the decoded picture
889 * Allocate a Linux media request file descriptor. This request wrapper will
890 * hold a reference to the requested bitstream memory to decoded and the
891 * picture buffer this request will decode to. This will be used for
892 * transparent management of the V4L2 queues.
894 * Returns: a new #GstV4l2Request
897 gst_v4l2_decoder_alloc_request (GstV4l2Decoder * self, guint32 frame_num,
898 GstMemory * bitstream, GstBuffer * pic_buf)
900 GstV4l2Request *request = gst_queue_array_pop_head (self->request_pool);
904 request = g_new0 (GstV4l2Request, 1);
906 ret = ioctl (self->media_fd, MEDIA_IOC_REQUEST_ALLOC, &request->fd);
908 GST_ERROR_OBJECT (self, "MEDIA_IOC_REQUEST_ALLOC failed: %s",
913 request->poll = gst_poll_new (FALSE);
914 gst_poll_fd_init (&request->pollfd);
915 request->pollfd.fd = request->fd;
916 gst_poll_add_fd (request->poll, &request->pollfd);
917 gst_poll_fd_ctl_pri (request->poll, &request->pollfd, TRUE);
920 request->decoder = g_object_ref (self);
921 request->bitstream = gst_memory_ref (bitstream);
922 request->pic_buf = gst_buffer_ref (pic_buf);
923 request->frame_num = frame_num;
924 request->ref_count = 1;
930 * gst_v4l2_decoder_alloc_sub_request:
931 * @self a #GstV4l2Decoder pointer
932 * @prev_request the #GstV4l2Request this request continue
933 * @bitstream the #GstMemory that holds the bitstream data
935 * Allocate a Linux media request file descriptor. Similar to
936 * gst_v4l2_decoder_alloc_request(), but used when a request is the
937 * continuation of the decoding of the same picture. This is notably the case
938 * for subsequent slices or for second field of a frame.
940 * Returns: a new #GstV4l2Request
943 gst_v4l2_decoder_alloc_sub_request (GstV4l2Decoder * self,
944 GstV4l2Request * prev_request, GstMemory * bitstream)
946 GstV4l2Request *request = gst_queue_array_pop_head (self->request_pool);
950 request = g_new0 (GstV4l2Request, 1);
952 ret = ioctl (self->media_fd, MEDIA_IOC_REQUEST_ALLOC, &request->fd);
954 GST_ERROR_OBJECT (self, "MEDIA_IOC_REQUEST_ALLOC failed: %s",
959 request->poll = gst_poll_new (FALSE);
960 gst_poll_fd_init (&request->pollfd);
961 request->pollfd.fd = request->fd;
962 gst_poll_add_fd (request->poll, &request->pollfd);
963 gst_poll_fd_ctl_pri (request->poll, &request->pollfd, TRUE);
966 request->decoder = g_object_ref (self);
967 request->bitstream = gst_memory_ref (bitstream);
968 request->pic_buf = gst_buffer_ref (prev_request->pic_buf);
969 request->frame_num = prev_request->frame_num;
970 request->sub_request = TRUE;
971 request->ref_count = 1;
977 * gst_v4l2_decoder_set_render_delay:
978 * @self: a #GstV4l2Decoder pointer
979 * @delay: The expected render delay
981 * The decoder will adjust the number of allowed concurrent request in order
982 * to allow this delay. The same number of concurrent bitstream buffer will be
983 * used, so make sure to adjust the number of bitstream buffer.
985 * For per-slice decoder, this is the maximum number of pending slice, so the
986 * render backlog in frame may be less then the render delay.
989 gst_v4l2_decoder_set_render_delay (GstV4l2Decoder * self, guint delay)
991 self->render_delay = delay;
995 * gst_v4l2_decoder_get_render_delay:
996 * @self: a #GstV4l2Decoder pointer
998 * This function is used to avoid storing the render delay in multiple places.
1000 * Returns: The currently configured render delay.
1003 gst_v4l2_decoder_get_render_delay (GstV4l2Decoder * self)
1005 return self->render_delay;
1009 gst_v4l2_request_ref (GstV4l2Request * request)
1011 request->ref_count++;
1016 gst_v4l2_request_free (GstV4l2Request * request)
1018 GstV4l2Decoder *decoder = request->decoder;
1020 request->decoder = NULL;
1021 close (request->fd);
1022 gst_poll_free (request->poll);
1026 g_object_unref (decoder);
1030 gst_v4l2_request_unref (GstV4l2Request * request)
1032 GstV4l2Decoder *decoder = request->decoder;
1035 g_return_if_fail (request->ref_count > 0);
1037 if (--request->ref_count > 0)
1040 g_clear_pointer (&request->bitstream, gst_memory_unref);
1041 g_clear_pointer (&request->pic_buf, gst_buffer_unref);
1042 request->frame_num = G_MAXUINT32;
1043 request->failed = FALSE;
1044 request->hold_pic_buf = FALSE;
1045 request->sub_request = FALSE;
1047 if (request->pending) {
1050 GST_DEBUG_OBJECT (decoder, "Freeing pending request %p.", request);
1052 idx = gst_queue_array_find (decoder->pending_requests, NULL, request);
1054 gst_queue_array_drop_element (decoder->pending_requests, idx);
1056 gst_v4l2_request_free (request);
1060 GST_TRACE_OBJECT (decoder, "Recycling request %p.", request);
1062 ret = ioctl (request->fd, MEDIA_REQUEST_IOC_REINIT, NULL);
1064 GST_ERROR_OBJECT (request->decoder, "MEDIA_REQUEST_IOC_REINIT failed: %s",
1065 g_strerror (errno));
1066 gst_v4l2_request_free (request);
1070 gst_queue_array_push_tail (decoder->request_pool, request);
1071 g_clear_object (&request->decoder);
1075 gst_v4l2_request_queue (GstV4l2Request * request, guint flags)
1077 GstV4l2Decoder *decoder = request->decoder;
1081 GST_TRACE_OBJECT (decoder, "Queuing request %p.", request);
1083 if (!gst_v4l2_decoder_queue_sink_mem (decoder, request,
1084 request->bitstream, request->frame_num, flags)) {
1085 GST_ERROR_OBJECT (decoder, "Driver did not accept the bitstream data.");
1089 if (!request->sub_request &&
1090 !gst_v4l2_decoder_queue_src_buffer (decoder, request->pic_buf)) {
1091 GST_ERROR_OBJECT (decoder, "Driver did not accept the picture buffer.");
1095 ret = ioctl (request->fd, MEDIA_REQUEST_IOC_QUEUE, NULL);
1097 GST_ERROR_OBJECT (decoder, "MEDIA_REQUEST_IOC_QUEUE, failed: %s",
1098 g_strerror (errno));
1102 if (flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
1103 request->hold_pic_buf = TRUE;
1105 request->pending = TRUE;
1106 gst_queue_array_push_tail (decoder->pending_requests,
1107 gst_v4l2_request_ref (request));
1109 max_pending = MAX (1, decoder->render_delay);
1111 if (gst_queue_array_get_length (decoder->pending_requests) > max_pending) {
1112 GstV4l2Request *pending_req;
1114 pending_req = gst_queue_array_peek_head (decoder->pending_requests);
1115 gst_v4l2_request_set_done (pending_req);
1122 gst_v4l2_request_set_done (GstV4l2Request * request)
1124 GstV4l2Decoder *decoder = request->decoder;
1125 GstV4l2Request *pending_req = NULL;
1128 if (!request->pending)
1131 ret = gst_poll_wait (request->poll, GST_SECOND);
1135 while ((pending_req = gst_queue_array_pop_head (decoder->pending_requests))) {
1136 gst_v4l2_decoder_dequeue_sink (decoder);
1137 g_clear_pointer (&pending_req->bitstream, gst_memory_unref);
1139 if (!pending_req->hold_pic_buf) {
1140 guint32 frame_num = G_MAXUINT32;
1142 if (!gst_v4l2_decoder_dequeue_src (decoder, &frame_num)) {
1143 pending_req->failed = TRUE;
1144 } else if (frame_num != pending_req->frame_num) {
1145 GST_WARNING_OBJECT (decoder,
1146 "Requested frame %u, but driver returned frame %u.",
1147 pending_req->frame_num, frame_num);
1148 pending_req->failed = TRUE;
1152 pending_req->pending = FALSE;
1153 gst_v4l2_request_unref (pending_req);
1155 if (pending_req == request)
1159 /* Pending request must be in the pending request list */
1160 g_assert (pending_req == request);
1166 gst_v4l2_request_failed (GstV4l2Request * request)
1168 return request->failed;