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"
33 #include <sys/types.h>
36 GST_DEBUG_CATEGORY (v4l2_decoder_debug);
37 #define GST_CAT_DEFAULT v4l2_decoder_debug
46 struct _GstV4l2Request
48 GstV4l2Decoder *decoder;
56 struct _GstV4l2Decoder
63 GstAtomicQueue *request_pool;
70 G_DEFINE_TYPE_WITH_CODE (GstV4l2Decoder, gst_v4l2_decoder, GST_TYPE_OBJECT,
71 GST_DEBUG_CATEGORY_INIT (v4l2_decoder_debug, "v4l2codecs-decoder", 0,
72 "V4L2 stateless decoder helper"));
75 direction_to_buffer_type (GstPadDirection direction)
77 if (direction == GST_PAD_SRC)
78 return V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
80 return V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
84 gst_v4l2_decoder_finalize (GObject * obj)
86 GstV4l2Decoder *self = GST_V4L2_DECODER (obj);
88 gst_v4l2_decoder_close (self);
90 g_free (self->media_device);
91 g_free (self->video_device);
92 gst_atomic_queue_unref (self->request_pool);
94 G_OBJECT_CLASS (gst_v4l2_decoder_parent_class)->finalize (obj);
98 gst_v4l2_decoder_init (GstV4l2Decoder * self)
100 self->request_pool = gst_atomic_queue_new (16);
104 gst_v4l2_decoder_class_init (GstV4l2DecoderClass * klass)
106 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
108 gobject_class->finalize = gst_v4l2_decoder_finalize;
109 gobject_class->get_property = gst_v4l2_decoder_get_property;
110 gobject_class->set_property = gst_v4l2_decoder_set_property;
112 gst_v4l2_decoder_install_properties (gobject_class, 0, NULL);
116 gst_v4l2_decoder_new (GstV4l2CodecDevice * device)
118 GstV4l2Decoder *decoder;
120 g_return_val_if_fail (device->function == MEDIA_ENT_F_PROC_VIDEO_DECODER,
123 decoder = g_object_new (GST_TYPE_V4L2_DECODER,
124 "media-device", device->media_device_path,
125 "video-device", device->video_device_path, NULL);
127 return gst_object_ref_sink (decoder);
131 gst_v4l2_decoder_open (GstV4l2Decoder * self)
133 self->media_fd = open (self->media_device, 0);
134 if (self->media_fd < 0) {
135 GST_ERROR_OBJECT (self, "Failed to open '%s': %s",
136 self->media_device, g_strerror (errno));
140 self->video_fd = open (self->video_device, O_NONBLOCK);
141 if (self->video_fd < 0) {
142 GST_ERROR_OBJECT (self, "Failed to open '%s': %s",
143 self->video_device, g_strerror (errno));
153 gst_v4l2_decoder_close (GstV4l2Decoder * self)
155 GstV4l2Request *request;
157 while ((request = gst_atomic_queue_pop (self->request_pool)))
158 gst_v4l2_request_free (request);
161 close (self->media_fd);
163 close (self->media_fd);
167 self->opened = FALSE;
173 gst_v4l2_decoder_streamon (GstV4l2Decoder * self, GstPadDirection direction)
176 guint32 type = direction_to_buffer_type (direction);
178 ret = ioctl (self->video_fd, VIDIOC_STREAMON, &type);
180 GST_ERROR_OBJECT (self, "VIDIOC_STREAMON failed: %s", g_strerror (errno));
188 gst_v4l2_decoder_streamoff (GstV4l2Decoder * self, GstPadDirection direction)
191 guint32 type = direction_to_buffer_type (direction);
193 ret = ioctl (self->video_fd, VIDIOC_STREAMOFF, &type);
195 GST_ERROR_OBJECT (self, "VIDIOC_STREAMOFF failed: %s", g_strerror (errno));
203 gst_v4l2_decoder_enum_sink_fmt (GstV4l2Decoder * self, gint i,
206 struct v4l2_fmtdesc fmtdesc = { i, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, };
209 g_return_val_if_fail (self->opened, FALSE);
211 ret = ioctl (self->video_fd, VIDIOC_ENUM_FMT, &fmtdesc);
214 GST_ERROR_OBJECT (self, "VIDIOC_ENUM_FMT failed: %s", g_strerror (errno));
218 GST_DEBUG_OBJECT (self, "Found format %" GST_FOURCC_FORMAT " (%s)",
219 GST_FOURCC_ARGS (fmtdesc.pixelformat), fmtdesc.description);
220 *out_fmt = fmtdesc.pixelformat;
226 gst_v4l2_decoder_set_sink_fmt (GstV4l2Decoder * self, guint32 pix_fmt,
227 gint width, gint height)
229 struct v4l2_format format = (struct v4l2_format) {
230 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
231 .fmt.pix_mp = (struct v4l2_pix_format_mplane) {
232 .pixelformat = pix_fmt,
239 ret = ioctl (self->video_fd, VIDIOC_S_FMT, &format);
241 GST_ERROR_OBJECT (self, "VIDIOC_S_FMT failed: %s", g_strerror (errno));
245 if (format.fmt.pix_mp.pixelformat != pix_fmt
246 || format.fmt.pix_mp.width != width
247 || format.fmt.pix_mp.height != height) {
248 GST_WARNING_OBJECT (self, "Failed to set sink format to %"
249 GST_FOURCC_FORMAT " %ix%i", GST_FOURCC_ARGS (pix_fmt), width, height);
258 gst_v4l2_decoder_enum_src_formats (GstV4l2Decoder * self)
261 struct v4l2_format fmt = {
262 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
264 GstVideoFormat format;
266 GValue list = G_VALUE_INIT;
267 GValue value = G_VALUE_INIT;
270 g_return_val_if_fail (self->opened, FALSE);
272 ret = ioctl (self->video_fd, VIDIOC_G_FMT, &fmt);
274 GST_ERROR_OBJECT (self, "VIDIOC_G_FMT failed: %s", g_strerror (errno));
278 /* We first place a structure with the default pixel format */
279 if (gst_v4l2_format_to_video_format (fmt.fmt.pix_mp.pixelformat, &format))
280 caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING,
281 gst_video_format_to_string (format), NULL);
283 caps = gst_caps_new_empty ();
285 /* And then enumerate other possible formats and place that as a second
286 * structure in the caps */
287 g_value_init (&list, GST_TYPE_LIST);
288 g_value_init (&value, G_TYPE_STRING);
290 for (i = 0; ret >= 0; i++) {
291 struct v4l2_fmtdesc fmtdesc = { i, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, };
293 ret = ioctl (self->video_fd, VIDIOC_ENUM_FMT, &fmtdesc);
296 GST_ERROR_OBJECT (self, "VIDIOC_ENUM_FMT failed: %s",
301 if (gst_v4l2_format_to_video_format (fmtdesc.pixelformat, &format)) {
302 g_value_set_static_string (&value, gst_video_format_to_string (format));
303 gst_value_list_append_value (&list, &value);
306 g_value_reset (&value);
308 if (gst_value_list_get_size (&list) > 0) {
309 GstStructure *str = gst_structure_new_empty ("video/x-raw");
310 gst_structure_take_value (str, "format", &list);
311 gst_caps_append_structure (caps, str);
313 g_value_reset (&list);
320 gst_v4l2_decoder_select_src_format (GstV4l2Decoder * self, GstCaps * caps,
324 struct v4l2_format fmt = {
325 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
328 const gchar *format_str;
329 GstVideoFormat format;
332 if (gst_caps_is_empty (caps))
335 ret = ioctl (self->video_fd, VIDIOC_G_FMT, &fmt);
337 GST_ERROR_OBJECT (self, "VIDIOC_G_FMT failed: %s", g_strerror (errno));
341 caps = gst_caps_make_writable (caps);
342 str = gst_caps_get_structure (caps, 0);
343 gst_structure_fixate_field (str, "format");
345 format_str = gst_structure_get_string (str, "format");
346 format = gst_video_format_from_string (format_str);
348 if (gst_v4l2_format_from_video_format (format, &pix_fmt) &&
349 pix_fmt != fmt.fmt.pix_mp.pixelformat) {
350 GST_DEBUG_OBJECT (self, "Trying to use peer format: %s ", format_str);
351 fmt.fmt.pix_mp.pixelformat = pix_fmt;
353 ret = ioctl (self->video_fd, VIDIOC_S_FMT, &fmt);
355 GST_ERROR_OBJECT (self, "VIDIOC_S_FMT failed: %s", g_strerror (errno));
360 if (!gst_v4l2_format_to_video_info (&fmt, info)) {
361 GST_ERROR_OBJECT (self, "Unsupported V4L2 pixelformat %" GST_FOURCC_FORMAT,
362 GST_FOURCC_ARGS (fmt.fmt.pix_mp.pixelformat));
366 GST_INFO_OBJECT (self, "Selected format %s %ix%i",
367 gst_video_format_to_string (info->finfo->format),
368 info->width, info->height);
374 gst_v4l2_decoder_request_buffers (GstV4l2Decoder * self,
375 GstPadDirection direction, guint num_buffers)
378 struct v4l2_requestbuffers reqbufs = {
379 .count = num_buffers,
380 .memory = V4L2_MEMORY_MMAP,
381 .type = direction_to_buffer_type (direction),
384 GST_DEBUG_OBJECT (self, "Requesting %u buffers", num_buffers);
386 ret = ioctl (self->video_fd, VIDIOC_REQBUFS, &reqbufs);
388 GST_ERROR_OBJECT (self, "VIDIOC_REQBUFS failed: %s", g_strerror (errno));
392 return reqbufs.count;
396 gst_v4l2_decoder_export_buffer (GstV4l2Decoder * self,
397 GstPadDirection direction, gint index, gint * fds, gsize * sizes,
398 gsize * offsets, guint * num_fds)
401 struct v4l2_plane planes[GST_VIDEO_MAX_PLANES] = { {0} };
402 struct v4l2_buffer v4l2_buf = {
404 .type = direction_to_buffer_type (direction),
405 .length = GST_VIDEO_MAX_PLANES,
409 ret = ioctl (self->video_fd, VIDIOC_QUERYBUF, &v4l2_buf);
411 GST_ERROR_OBJECT (self, "VIDIOC_QUERYBUF failed: %s", g_strerror (errno));
415 *num_fds = v4l2_buf.length;
416 for (i = 0; i < v4l2_buf.length; i++) {
417 struct v4l2_plane *plane = v4l2_buf.m.planes + i;
418 struct v4l2_exportbuffer expbuf = {
419 .type = direction_to_buffer_type (direction),
422 .flags = O_CLOEXEC | O_RDWR,
425 ret = ioctl (self->video_fd, VIDIOC_EXPBUF, &expbuf);
428 GST_ERROR_OBJECT (self, "VIDIOC_EXPBUF failed: %s", g_strerror (errno));
430 for (j = i - 1; j >= 0; j--)
437 sizes[i] = plane->length;
438 offsets[i] = plane->data_offset;
445 gst_v4l2_decoder_queue_sink_mem (GstV4l2Decoder * self,
446 GstV4l2Request * request, GstMemory * mem, guint32 frame_num,
450 struct v4l2_plane plane = {
451 .bytesused = bytesused,
453 struct v4l2_buffer buf = {
454 .type = direction_to_buffer_type (GST_PAD_SINK),
455 .memory = V4L2_MEMORY_MMAP,
456 .index = gst_v4l2_codec_memory_get_index (mem),
457 .timestamp.tv_usec = frame_num,
458 .request_fd = request->fd,
459 .flags = V4L2_BUF_FLAG_REQUEST_FD,
464 ret = ioctl (self->video_fd, VIDIOC_QBUF, &buf);
466 GST_ERROR_OBJECT (self, "VIDIOC_QBUF failed: %s", g_strerror (errno));
470 request->bitstream = gst_memory_ref (mem);
476 gst_v4l2_decoder_queue_src_buffer (GstV4l2Decoder * self, GstBuffer * buffer,
480 struct v4l2_plane plane[GST_VIDEO_MAX_PLANES];
481 struct v4l2_buffer buf = {
482 .type = direction_to_buffer_type (GST_PAD_SRC),
483 .memory = V4L2_MEMORY_MMAP,
484 .index = gst_v4l2_codec_buffer_get_index (buffer),
485 .length = gst_buffer_n_memory (buffer),
489 for (i = 0; i < buf.length; i++) {
490 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
492 plane[i] = (struct v4l2_plane) {
493 .bytesused = gst_memory_get_sizes (mem, NULL, NULL),
498 ret = ioctl (self->video_fd, VIDIOC_QBUF, &buf);
500 GST_ERROR_OBJECT (self, "VIDIOC_QBUF failed: %s", g_strerror (errno));
508 gst_v4l2_decoder_dequeue_sink (GstV4l2Decoder * self)
511 struct v4l2_plane plane[GST_VIDEO_MAX_PLANES] = { {0} };
512 struct v4l2_buffer buf = {
513 .type = direction_to_buffer_type (GST_PAD_SINK),
514 .memory = V4L2_MEMORY_MMAP,
515 .length = GST_VIDEO_MAX_PLANES,
519 ret = ioctl (self->video_fd, VIDIOC_DQBUF, &buf);
521 GST_ERROR_OBJECT (self, "VIDIOC_DQBUF failed: %s", g_strerror (errno));
529 gst_v4l2_decoder_dequeue_src (GstV4l2Decoder * self, guint32 * out_frame_num)
532 struct v4l2_plane plane[GST_VIDEO_MAX_PLANES] = { {0} };
533 struct v4l2_buffer buf = {
534 .type = direction_to_buffer_type (GST_PAD_SRC),
535 .memory = V4L2_MEMORY_MMAP,
536 .length = GST_VIDEO_MAX_PLANES,
540 ret = ioctl (self->video_fd, VIDIOC_DQBUF, &buf);
542 GST_ERROR_OBJECT (self, "VIDIOC_DQBUF failed: %s", g_strerror (errno));
546 *out_frame_num = buf.timestamp.tv_usec;
551 gst_v4l2_decoder_set_controls (GstV4l2Decoder * self, GstV4l2Request * request,
552 struct v4l2_ext_control * control, guint count)
555 struct v4l2_ext_controls controls = {
558 .request_fd = request ? request->fd : 0,
559 .which = request ? V4L2_CTRL_WHICH_REQUEST_VAL : 0,
562 ret = ioctl (self->video_fd, VIDIOC_S_EXT_CTRLS, &controls);
564 GST_ERROR_OBJECT (self, "VIDIOC_S_EXT_CTRLS failed: %s",
573 gst_v4l2_decoder_install_properties (GObjectClass * gobject_class,
574 gint prop_offset, GstV4l2CodecDevice * device)
576 const gchar *media_device_path = NULL;
577 const gchar *video_device_path = NULL;
580 media_device_path = device->media_device_path;
581 video_device_path = device->video_device_path;
584 g_object_class_install_property (gobject_class, PROP_MEDIA_DEVICE,
585 g_param_spec_string ("media-device", "Media Device Path",
586 "Path to the media device node", media_device_path,
587 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
589 g_object_class_install_property (gobject_class, PROP_VIDEO_DEVICE,
590 g_param_spec_string ("video-device", "Video Device Path",
591 "Path to the video device node", video_device_path,
592 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
596 gst_v4l2_decoder_set_property (GObject * object, guint prop_id,
597 const GValue * value, GParamSpec * pspec)
599 GstV4l2Decoder *self = GST_V4L2_DECODER (object);
602 case PROP_MEDIA_DEVICE:
603 g_free (self->media_device);
604 self->media_device = g_value_dup_string (value);
606 case PROP_VIDEO_DEVICE:
607 g_free (self->video_device);
608 self->video_device = g_value_dup_string (value);
611 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
617 gst_v4l2_decoder_get_property (GObject * object, guint prop_id,
618 GValue * value, GParamSpec * pspec)
620 GstV4l2Decoder *self = GST_V4L2_DECODER (object);
623 case PROP_MEDIA_DEVICE:
624 g_value_set_string (value, self->media_device);
626 case PROP_VIDEO_DEVICE:
627 g_value_set_string (value, self->video_device);
630 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
636 gst_v4l2_decoder_alloc_request (GstV4l2Decoder * self)
638 GstV4l2Request *request = gst_atomic_queue_pop (self->request_pool);
642 request = g_new0 (GstV4l2Request, 1);
644 ret = ioctl (self->media_fd, MEDIA_IOC_REQUEST_ALLOC, &request->fd);
646 GST_ERROR_OBJECT (self, "MEDIA_IOC_REQUEST_ALLOC failed: %s",
651 request->poll = gst_poll_new (FALSE);
652 gst_poll_fd_init (&request->pollfd);
653 request->pollfd.fd = request->fd;
654 gst_poll_add_fd (request->poll, &request->pollfd);
655 gst_poll_fd_ctl_pri (request->poll, &request->pollfd, TRUE);
658 request->decoder = g_object_ref (self);
663 gst_v4l2_request_free (GstV4l2Request * request)
665 GstV4l2Decoder *decoder = request->decoder;
670 gst_poll_free (request->poll);
675 g_clear_pointer (&request->bitstream, gst_memory_unref);
676 request->decoder = NULL;
678 if (request->pending) {
679 gst_v4l2_request_free (request);
680 g_object_unref (decoder);
684 ret = ioctl (request->fd, MEDIA_REQUEST_IOC_REINIT, NULL);
686 GST_ERROR_OBJECT (request->decoder, "MEDIA_REQUEST_IOC_REINIT failed: %s",
688 gst_v4l2_request_free (request);
689 g_object_unref (decoder);
693 gst_atomic_queue_push (decoder->request_pool, request);
694 g_object_unref (decoder);
698 gst_v4l2_request_queue (GstV4l2Request * request)
702 ret = ioctl (request->fd, MEDIA_REQUEST_IOC_QUEUE, NULL);
704 GST_ERROR_OBJECT (request->decoder, "MEDIA_REQUEST_IOC_QUEUE, failed: %s",
709 request->pending = TRUE;
715 gst_v4l2_request_poll (GstV4l2Request * request, GstClockTime timeout)
717 return gst_poll_wait (request->poll, timeout);
721 gst_v4l2_request_set_done (GstV4l2Request * request)
723 if (request->bitstream) {
724 gst_v4l2_decoder_dequeue_sink (request->decoder);
725 g_clear_pointer (&request->bitstream, gst_memory_unref);
728 request->pending = FALSE;
732 gst_v4l2_request_is_done (GstV4l2Request * request)
734 return !request->pending;