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_flush (GstV4l2Decoder * self)
205 /* We ignore streamoff failure as it's not relevant, if we manage to
206 * streamon again, we are good. */
207 gst_v4l2_decoder_streamoff (self, GST_PAD_SINK);
208 gst_v4l2_decoder_streamoff (self, GST_PAD_SRC);
210 return gst_v4l2_decoder_streamon (self, GST_PAD_SINK) &&
211 gst_v4l2_decoder_streamon (self, GST_PAD_SRC);
215 gst_v4l2_decoder_enum_sink_fmt (GstV4l2Decoder * self, gint i,
218 struct v4l2_fmtdesc fmtdesc = { i, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, };
221 g_return_val_if_fail (self->opened, FALSE);
223 ret = ioctl (self->video_fd, VIDIOC_ENUM_FMT, &fmtdesc);
226 GST_ERROR_OBJECT (self, "VIDIOC_ENUM_FMT failed: %s", g_strerror (errno));
230 GST_DEBUG_OBJECT (self, "Found format %" GST_FOURCC_FORMAT " (%s)",
231 GST_FOURCC_ARGS (fmtdesc.pixelformat), fmtdesc.description);
232 *out_fmt = fmtdesc.pixelformat;
238 gst_v4l2_decoder_set_sink_fmt (GstV4l2Decoder * self, guint32 pix_fmt,
239 gint width, gint height)
241 struct v4l2_format format = (struct v4l2_format) {
242 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
243 .fmt.pix_mp = (struct v4l2_pix_format_mplane) {
244 .pixelformat = pix_fmt,
251 ret = ioctl (self->video_fd, VIDIOC_S_FMT, &format);
253 GST_ERROR_OBJECT (self, "VIDIOC_S_FMT failed: %s", g_strerror (errno));
257 if (format.fmt.pix_mp.pixelformat != pix_fmt
258 || format.fmt.pix_mp.width != width
259 || format.fmt.pix_mp.height != height) {
260 GST_WARNING_OBJECT (self, "Failed to set sink format to %"
261 GST_FOURCC_FORMAT " %ix%i", GST_FOURCC_ARGS (pix_fmt), width, height);
270 gst_v4l2_decoder_enum_src_formats (GstV4l2Decoder * self)
273 struct v4l2_format fmt = {
274 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
276 GstVideoFormat format;
278 GValue list = G_VALUE_INIT;
279 GValue value = G_VALUE_INIT;
282 g_return_val_if_fail (self->opened, FALSE);
284 ret = ioctl (self->video_fd, VIDIOC_G_FMT, &fmt);
286 GST_ERROR_OBJECT (self, "VIDIOC_G_FMT failed: %s", g_strerror (errno));
290 /* We first place a structure with the default pixel format */
291 if (gst_v4l2_format_to_video_format (fmt.fmt.pix_mp.pixelformat, &format))
292 caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING,
293 gst_video_format_to_string (format), NULL);
295 caps = gst_caps_new_empty ();
297 /* And then enumerate other possible formats and place that as a second
298 * structure in the caps */
299 g_value_init (&list, GST_TYPE_LIST);
300 g_value_init (&value, G_TYPE_STRING);
302 for (i = 0; ret >= 0; i++) {
303 struct v4l2_fmtdesc fmtdesc = { i, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, };
305 ret = ioctl (self->video_fd, VIDIOC_ENUM_FMT, &fmtdesc);
308 GST_ERROR_OBJECT (self, "VIDIOC_ENUM_FMT failed: %s",
313 if (gst_v4l2_format_to_video_format (fmtdesc.pixelformat, &format)) {
314 g_value_set_static_string (&value, gst_video_format_to_string (format));
315 gst_value_list_append_value (&list, &value);
318 g_value_reset (&value);
320 if (gst_value_list_get_size (&list) > 0) {
321 GstStructure *str = gst_structure_new_empty ("video/x-raw");
322 gst_structure_take_value (str, "format", &list);
323 gst_caps_append_structure (caps, str);
325 g_value_reset (&list);
332 gst_v4l2_decoder_select_src_format (GstV4l2Decoder * self, GstCaps * caps,
336 struct v4l2_format fmt = {
337 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
340 const gchar *format_str;
341 GstVideoFormat format;
344 if (gst_caps_is_empty (caps))
347 ret = ioctl (self->video_fd, VIDIOC_G_FMT, &fmt);
349 GST_ERROR_OBJECT (self, "VIDIOC_G_FMT failed: %s", g_strerror (errno));
353 caps = gst_caps_make_writable (caps);
354 str = gst_caps_get_structure (caps, 0);
355 gst_structure_fixate_field (str, "format");
357 format_str = gst_structure_get_string (str, "format");
358 format = gst_video_format_from_string (format_str);
360 if (gst_v4l2_format_from_video_format (format, &pix_fmt) &&
361 pix_fmt != fmt.fmt.pix_mp.pixelformat) {
362 GST_DEBUG_OBJECT (self, "Trying to use peer format: %s ", format_str);
363 fmt.fmt.pix_mp.pixelformat = pix_fmt;
365 ret = ioctl (self->video_fd, VIDIOC_S_FMT, &fmt);
367 GST_ERROR_OBJECT (self, "VIDIOC_S_FMT failed: %s", g_strerror (errno));
372 if (!gst_v4l2_format_to_video_info (&fmt, info)) {
373 GST_ERROR_OBJECT (self, "Unsupported V4L2 pixelformat %" GST_FOURCC_FORMAT,
374 GST_FOURCC_ARGS (fmt.fmt.pix_mp.pixelformat));
378 GST_INFO_OBJECT (self, "Selected format %s %ix%i",
379 gst_video_format_to_string (info->finfo->format),
380 info->width, info->height);
386 gst_v4l2_decoder_request_buffers (GstV4l2Decoder * self,
387 GstPadDirection direction, guint num_buffers)
390 struct v4l2_requestbuffers reqbufs = {
391 .count = num_buffers,
392 .memory = V4L2_MEMORY_MMAP,
393 .type = direction_to_buffer_type (direction),
396 GST_DEBUG_OBJECT (self, "Requesting %u buffers", num_buffers);
398 ret = ioctl (self->video_fd, VIDIOC_REQBUFS, &reqbufs);
400 GST_ERROR_OBJECT (self, "VIDIOC_REQBUFS failed: %s", g_strerror (errno));
404 return reqbufs.count;
408 gst_v4l2_decoder_export_buffer (GstV4l2Decoder * self,
409 GstPadDirection direction, gint index, gint * fds, gsize * sizes,
410 gsize * offsets, guint * num_fds)
413 struct v4l2_plane planes[GST_VIDEO_MAX_PLANES] = { {0} };
414 struct v4l2_buffer v4l2_buf = {
416 .type = direction_to_buffer_type (direction),
417 .length = GST_VIDEO_MAX_PLANES,
421 ret = ioctl (self->video_fd, VIDIOC_QUERYBUF, &v4l2_buf);
423 GST_ERROR_OBJECT (self, "VIDIOC_QUERYBUF failed: %s", g_strerror (errno));
427 *num_fds = v4l2_buf.length;
428 for (i = 0; i < v4l2_buf.length; i++) {
429 struct v4l2_plane *plane = v4l2_buf.m.planes + i;
430 struct v4l2_exportbuffer expbuf = {
431 .type = direction_to_buffer_type (direction),
434 .flags = O_CLOEXEC | O_RDWR,
437 ret = ioctl (self->video_fd, VIDIOC_EXPBUF, &expbuf);
440 GST_ERROR_OBJECT (self, "VIDIOC_EXPBUF failed: %s", g_strerror (errno));
442 for (j = i - 1; j >= 0; j--)
449 sizes[i] = plane->length;
450 offsets[i] = plane->data_offset;
457 gst_v4l2_decoder_queue_sink_mem (GstV4l2Decoder * self,
458 GstV4l2Request * request, GstMemory * mem, guint32 frame_num,
462 struct v4l2_plane plane = {
463 .bytesused = bytesused,
465 struct v4l2_buffer buf = {
466 .type = direction_to_buffer_type (GST_PAD_SINK),
467 .memory = V4L2_MEMORY_MMAP,
468 .index = gst_v4l2_codec_memory_get_index (mem),
469 .timestamp.tv_usec = frame_num,
470 .request_fd = request->fd,
471 .flags = V4L2_BUF_FLAG_REQUEST_FD,
476 ret = ioctl (self->video_fd, VIDIOC_QBUF, &buf);
478 GST_ERROR_OBJECT (self, "VIDIOC_QBUF failed: %s", g_strerror (errno));
482 request->bitstream = gst_memory_ref (mem);
488 gst_v4l2_decoder_queue_src_buffer (GstV4l2Decoder * self, GstBuffer * buffer,
492 struct v4l2_plane plane[GST_VIDEO_MAX_PLANES];
493 struct v4l2_buffer buf = {
494 .type = direction_to_buffer_type (GST_PAD_SRC),
495 .memory = V4L2_MEMORY_MMAP,
496 .index = gst_v4l2_codec_buffer_get_index (buffer),
497 .length = gst_buffer_n_memory (buffer),
501 for (i = 0; i < buf.length; i++) {
502 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
504 plane[i] = (struct v4l2_plane) {
505 .bytesused = gst_memory_get_sizes (mem, NULL, NULL),
510 ret = ioctl (self->video_fd, VIDIOC_QBUF, &buf);
512 GST_ERROR_OBJECT (self, "VIDIOC_QBUF failed: %s", g_strerror (errno));
520 gst_v4l2_decoder_dequeue_sink (GstV4l2Decoder * self)
523 struct v4l2_plane plane[GST_VIDEO_MAX_PLANES] = { {0} };
524 struct v4l2_buffer buf = {
525 .type = direction_to_buffer_type (GST_PAD_SINK),
526 .memory = V4L2_MEMORY_MMAP,
527 .length = GST_VIDEO_MAX_PLANES,
531 ret = ioctl (self->video_fd, VIDIOC_DQBUF, &buf);
533 GST_ERROR_OBJECT (self, "VIDIOC_DQBUF failed: %s", g_strerror (errno));
541 gst_v4l2_decoder_dequeue_src (GstV4l2Decoder * self, guint32 * out_frame_num)
544 struct v4l2_plane plane[GST_VIDEO_MAX_PLANES] = { {0} };
545 struct v4l2_buffer buf = {
546 .type = direction_to_buffer_type (GST_PAD_SRC),
547 .memory = V4L2_MEMORY_MMAP,
548 .length = GST_VIDEO_MAX_PLANES,
552 ret = ioctl (self->video_fd, VIDIOC_DQBUF, &buf);
554 GST_ERROR_OBJECT (self, "VIDIOC_DQBUF failed: %s", g_strerror (errno));
558 *out_frame_num = buf.timestamp.tv_usec;
563 gst_v4l2_decoder_set_controls (GstV4l2Decoder * self, GstV4l2Request * request,
564 struct v4l2_ext_control * control, guint count)
567 struct v4l2_ext_controls controls = {
570 .request_fd = request ? request->fd : 0,
571 .which = request ? V4L2_CTRL_WHICH_REQUEST_VAL : 0,
574 ret = ioctl (self->video_fd, VIDIOC_S_EXT_CTRLS, &controls);
576 GST_ERROR_OBJECT (self, "VIDIOC_S_EXT_CTRLS failed: %s",
585 gst_v4l2_decoder_install_properties (GObjectClass * gobject_class,
586 gint prop_offset, GstV4l2CodecDevice * device)
588 const gchar *media_device_path = NULL;
589 const gchar *video_device_path = NULL;
592 media_device_path = device->media_device_path;
593 video_device_path = device->video_device_path;
596 g_object_class_install_property (gobject_class, PROP_MEDIA_DEVICE,
597 g_param_spec_string ("media-device", "Media Device Path",
598 "Path to the media device node", media_device_path,
599 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
601 g_object_class_install_property (gobject_class, PROP_VIDEO_DEVICE,
602 g_param_spec_string ("video-device", "Video Device Path",
603 "Path to the video device node", video_device_path,
604 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
608 gst_v4l2_decoder_set_property (GObject * object, guint prop_id,
609 const GValue * value, GParamSpec * pspec)
611 GstV4l2Decoder *self = GST_V4L2_DECODER (object);
614 case PROP_MEDIA_DEVICE:
615 g_free (self->media_device);
616 self->media_device = g_value_dup_string (value);
618 case PROP_VIDEO_DEVICE:
619 g_free (self->video_device);
620 self->video_device = g_value_dup_string (value);
623 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
629 gst_v4l2_decoder_get_property (GObject * object, guint prop_id,
630 GValue * value, GParamSpec * pspec)
632 GstV4l2Decoder *self = GST_V4L2_DECODER (object);
635 case PROP_MEDIA_DEVICE:
636 g_value_set_string (value, self->media_device);
638 case PROP_VIDEO_DEVICE:
639 g_value_set_string (value, self->video_device);
642 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
648 gst_v4l2_decoder_alloc_request (GstV4l2Decoder * self)
650 GstV4l2Request *request = gst_atomic_queue_pop (self->request_pool);
654 request = g_new0 (GstV4l2Request, 1);
656 ret = ioctl (self->media_fd, MEDIA_IOC_REQUEST_ALLOC, &request->fd);
658 GST_ERROR_OBJECT (self, "MEDIA_IOC_REQUEST_ALLOC failed: %s",
663 request->poll = gst_poll_new (FALSE);
664 gst_poll_fd_init (&request->pollfd);
665 request->pollfd.fd = request->fd;
666 gst_poll_add_fd (request->poll, &request->pollfd);
667 gst_poll_fd_ctl_pri (request->poll, &request->pollfd, TRUE);
670 request->decoder = g_object_ref (self);
675 gst_v4l2_request_free (GstV4l2Request * request)
677 GstV4l2Decoder *decoder = request->decoder;
682 gst_poll_free (request->poll);
687 g_clear_pointer (&request->bitstream, gst_memory_unref);
688 request->decoder = NULL;
690 if (request->pending) {
691 gst_v4l2_request_free (request);
692 g_object_unref (decoder);
696 ret = ioctl (request->fd, MEDIA_REQUEST_IOC_REINIT, NULL);
698 GST_ERROR_OBJECT (request->decoder, "MEDIA_REQUEST_IOC_REINIT failed: %s",
700 gst_v4l2_request_free (request);
701 g_object_unref (decoder);
705 gst_atomic_queue_push (decoder->request_pool, request);
706 g_object_unref (decoder);
710 gst_v4l2_request_queue (GstV4l2Request * request)
714 ret = ioctl (request->fd, MEDIA_REQUEST_IOC_QUEUE, NULL);
716 GST_ERROR_OBJECT (request->decoder, "MEDIA_REQUEST_IOC_QUEUE, failed: %s",
721 request->pending = TRUE;
727 gst_v4l2_request_poll (GstV4l2Request * request, GstClockTime timeout)
729 return gst_poll_wait (request->poll, timeout);
733 gst_v4l2_request_set_done (GstV4l2Request * request)
735 if (request->bitstream) {
736 gst_v4l2_decoder_dequeue_sink (request->decoder);
737 g_clear_pointer (&request->bitstream, gst_memory_unref);
740 request->pending = FALSE;
744 gst_v4l2_request_is_done (GstV4l2Request * request)
746 return !request->pending;