2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
32 #include "gstv4l2object.h"
33 #include "gstv4l2videodec.h"
36 #include <gst/gst-i18n-plugin.h>
38 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
39 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
46 const gchar *longname;
47 const gchar *description;
48 } GstV4l2VideoDecCData;
56 #define gst_v4l2_video_dec_parent_class parent_class
57 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
58 GST_TYPE_VIDEO_DECODER);
61 gst_v4l2_video_dec_set_property (GObject * object,
62 guint prop_id, const GValue * value, GParamSpec * pspec)
64 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
67 case PROP_OUTPUT_IO_MODE:
68 gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
71 case PROP_CAPTURE_IO_MODE:
72 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
76 /* By default, only set on output */
78 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
79 prop_id, value, pspec)) {
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 gst_v4l2_video_dec_get_property (GObject * object,
88 guint prop_id, GValue * value, GParamSpec * pspec)
90 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
93 case PROP_OUTPUT_IO_MODE:
94 gst_v4l2_object_get_property_helper (self->v4l2output, prop_id, value,
97 case PROP_CAPTURE_IO_MODE:
98 gst_v4l2_object_get_property_helper (self->v4l2capture, prop_id, value,
102 /* By default read from output */
104 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
105 prop_id, value, pspec)) {
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
115 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
118 GST_DEBUG_OBJECT (self, "Opening");
120 if (!gst_v4l2_object_open (self->v4l2output))
123 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
126 codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
127 self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
129 gst_caps_unref (codec_caps);
131 if (gst_caps_is_empty (self->probed_sinkcaps))
132 goto no_encoded_format;
134 self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
135 gst_v4l2_object_get_raw_caps ());
137 if (gst_caps_is_empty (self->probed_srccaps))
143 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
144 (_("Encoder on device %s has no supported input format"),
145 self->v4l2output->videodev), (NULL));
150 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
151 (_("Encoder on device %s has no supported output format"),
152 self->v4l2output->videodev), (NULL));
156 if (GST_V4L2_IS_OPEN (self->v4l2output))
157 gst_v4l2_object_close (self->v4l2output);
159 if (GST_V4L2_IS_OPEN (self->v4l2capture))
160 gst_v4l2_object_close (self->v4l2capture);
162 gst_caps_replace (&self->probed_srccaps, NULL);
163 gst_caps_replace (&self->probed_sinkcaps, NULL);
169 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
171 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
173 GST_DEBUG_OBJECT (self, "Closing");
175 gst_v4l2_object_close (self->v4l2output);
176 gst_v4l2_object_close (self->v4l2capture);
177 gst_caps_replace (&self->probed_srccaps, NULL);
178 gst_caps_replace (&self->probed_sinkcaps, NULL);
184 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
186 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
188 GST_DEBUG_OBJECT (self, "Starting");
190 gst_v4l2_object_unlock (self->v4l2output);
191 g_atomic_int_set (&self->active, TRUE);
192 self->output_flow = GST_FLOW_OK;
198 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
200 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
202 GST_DEBUG_OBJECT (self, "Stopping");
204 gst_v4l2_object_unlock (self->v4l2output);
205 gst_v4l2_object_unlock (self->v4l2capture);
207 /* Wait for capture thread to stop */
208 gst_pad_stop_task (decoder->srcpad);
210 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
211 self->output_flow = GST_FLOW_OK;
212 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
214 /* Should have been flushed already */
215 g_assert (g_atomic_int_get (&self->active) == FALSE);
217 gst_v4l2_object_stop (self->v4l2output);
218 gst_v4l2_object_stop (self->v4l2capture);
220 if (self->input_state) {
221 gst_video_codec_state_unref (self->input_state);
222 self->input_state = NULL;
225 GST_DEBUG_OBJECT (self, "Stopped");
231 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
232 GstVideoCodecState * state)
234 GstV4l2Error error = GST_V4L2_ERROR_INIT;
236 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
238 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
240 if (self->input_state) {
241 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
242 GST_DEBUG_OBJECT (self, "Compatible caps");
245 gst_video_codec_state_unref (self->input_state);
246 self->input_state = NULL;
248 /* FIXME we probably need to do more work if pools are active */
251 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
254 self->input_state = gst_video_codec_state_ref (state);
256 gst_v4l2_error (self, &error);
263 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
265 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
267 GST_DEBUG_OBJECT (self, "Flushed");
269 /* Ensure the processing thread has stopped for the reverse playback
271 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
272 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
274 gst_v4l2_object_unlock (self->v4l2output);
275 gst_v4l2_object_unlock (self->v4l2capture);
276 gst_pad_stop_task (decoder->srcpad);
277 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
280 self->output_flow = GST_FLOW_OK;
282 gst_v4l2_object_unlock_stop (self->v4l2output);
283 gst_v4l2_object_unlock_stop (self->v4l2capture);
289 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
291 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
293 /* We don't allow renegotiation without carefull disabling the pool */
294 if (self->v4l2capture->pool &&
295 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
298 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
302 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
304 struct v4l2_decoder_cmd dcmd = { 0, };
306 GST_DEBUG_OBJECT (v4l2object->element,
307 "sending v4l2 decoder command %u with flags %u", cmd, flags);
309 if (!GST_V4L2_IS_OPEN (v4l2object))
314 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
320 if (errno == ENOTTY) {
321 GST_INFO_OBJECT (v4l2object->element,
322 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
323 cmd, flags, v4l2object->videodev, g_strerror (errno));
325 GST_ERROR_OBJECT (v4l2object->element,
326 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
327 cmd, flags, v4l2object->videodev, g_strerror (errno));
333 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
335 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
336 GstFlowReturn ret = GST_FLOW_OK;
339 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
342 GST_DEBUG_OBJECT (self, "Finishing decoding");
344 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
346 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
347 GstTask *task = decoder->srcpad->task;
349 /* If the decoder stop command succeeded, just wait until processing is
351 GST_OBJECT_LOCK (task);
352 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
353 GST_TASK_WAIT (task);
354 GST_OBJECT_UNLOCK (task);
355 ret = GST_FLOW_FLUSHING;
357 /* otherwise keep queuing empty buffers until the processing thread has
358 * stopped, _pool_process() will return FLUSHING when that happened */
359 while (ret == GST_FLOW_OK) {
360 buffer = gst_buffer_new ();
362 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
363 v4l2output->pool), &buffer);
364 gst_buffer_unref (buffer);
368 /* and ensure the processing thread has stopped in case another error
370 gst_v4l2_object_unlock (self->v4l2capture);
371 gst_pad_stop_task (decoder->srcpad);
372 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
374 if (ret == GST_FLOW_FLUSHING)
375 ret = self->output_flow;
377 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
383 static GstVideoCodecFrame *
384 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
386 GstVideoCodecFrame *frame = NULL;
390 frames = gst_video_decoder_get_frames (decoder);
392 for (l = frames; l != NULL; l = l->next) {
393 GstVideoCodecFrame *f = l->data;
395 if (!frame || frame->pts > f->pts)
402 GST_LOG_OBJECT (decoder,
403 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
404 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
405 gst_video_codec_frame_ref (frame);
408 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
414 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
416 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
417 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
419 GstVideoCodecFrame *frame;
420 GstBuffer *buffer = NULL;
423 GST_LOG_OBJECT (decoder, "Allocate output buffer");
425 self->output_flow = GST_FLOW_OK;
427 /* We cannot use the base class allotate helper since it taking the internal
428 * stream lock. we know that the acquire may need to poll until more frames
429 * comes in and holding this lock would prevent that.
431 pool = gst_video_decoder_get_buffer_pool (decoder);
433 /* Pool may be NULL if we started going to READY state */
435 ret = GST_FLOW_FLUSHING;
439 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
440 g_object_unref (pool);
442 if (ret != GST_FLOW_OK)
445 GST_LOG_OBJECT (decoder, "Process output buffer");
446 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
448 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
450 if (ret != GST_FLOW_OK)
453 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
456 frame->output_buffer = buffer;
458 ret = gst_video_decoder_finish_frame (decoder, frame);
460 if (ret != GST_FLOW_OK)
463 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
464 gst_buffer_unref (buffer);
470 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
471 gst_flow_get_name (ret));
473 gst_buffer_replace (&buffer, NULL);
474 self->output_flow = ret;
475 gst_v4l2_object_unlock (self->v4l2output);
476 gst_pad_pause_task (decoder->srcpad);
480 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
481 GstStructure * structure, gpointer user_data)
483 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
484 GstVideoAlignment *align = &self->v4l2capture->align;
485 GstVideoInfo *info = &self->v4l2capture->info;
488 if (!gst_structure_get_int (structure, "width", &width))
491 if (!gst_structure_get_int (structure, "height", &height))
494 if (align->padding_left != 0 || align->padding_top != 0 ||
495 width != info->width + align->padding_right ||
496 height != info->height + align->padding_bottom)
499 gst_structure_set (structure,
500 "width", G_TYPE_INT, width - align->padding_right,
501 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
507 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
508 GstVideoCodecFrame * frame)
510 GstV4l2Error error = GST_V4L2_ERROR_INIT;
511 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
512 GstFlowReturn ret = GST_FLOW_OK;
513 gboolean processed = FALSE;
516 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
518 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
521 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
522 if (!self->input_state)
524 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
529 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
530 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
532 GstVideoCodecState *output_state;
533 GstBuffer *codec_data;
534 GstCaps *acquired_caps, *available_caps, *caps, *filter;
537 GST_DEBUG_OBJECT (self, "Sending header");
539 codec_data = self->input_state->codec_data;
541 /* We are running in byte-stream mode, so we don't know the headers, but
542 * we need to send something, otherwise the decoder will refuse to
546 gst_buffer_ref (codec_data);
548 codec_data = gst_buffer_ref (frame->input_buffer);
552 /* Ensure input internal pool is active */
553 if (!gst_buffer_pool_is_active (pool)) {
554 GstStructure *config = gst_buffer_pool_get_config (pool);
555 gst_buffer_pool_config_set_params (config, self->input_state->caps,
556 self->v4l2output->info.size, 2, 2);
558 /* There is no reason to refuse this config */
559 if (!gst_buffer_pool_set_config (pool, config))
560 goto activate_failed;
562 if (!gst_buffer_pool_set_active (pool, TRUE))
563 goto activate_failed;
566 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
568 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
569 v4l2output->pool), &codec_data);
570 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
572 gst_buffer_unref (codec_data);
574 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
575 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
576 * and returns the visible size as with/height and the coded size as
578 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
581 /* Create caps from the acquired format, remove the format field */
582 acquired_caps = gst_video_info_to_caps (&info);
583 GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
584 st = gst_caps_get_structure (acquired_caps, 0);
585 gst_structure_remove_field (st, "format");
587 /* Probe currently available pixel formats */
588 available_caps = gst_v4l2_object_probe_caps (self->v4l2capture, NULL);
589 available_caps = gst_caps_make_writable (available_caps);
590 GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
592 /* Replace coded size with visible size, we want to negotiate visible size
593 * with downstream, not coded size. */
594 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
596 filter = gst_caps_intersect_full (available_caps, acquired_caps,
597 GST_CAPS_INTERSECT_FIRST);
598 GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
599 gst_caps_unref (acquired_caps);
600 gst_caps_unref (available_caps);
601 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
602 gst_caps_unref (filter);
604 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
605 if (gst_caps_is_empty (caps)) {
606 gst_caps_unref (caps);
610 /* Fixate pixel format */
611 caps = gst_caps_fixate (caps);
613 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
615 /* Try to set negotiated format, on success replace acquired format */
616 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
617 gst_video_info_from_caps (&info, caps);
619 gst_v4l2_clear_error (&error);
620 gst_caps_unref (caps);
622 output_state = gst_video_decoder_set_output_state (decoder,
623 info.finfo->format, info.width, info.height, self->input_state);
625 /* Copy the rest of the information, there might be more in the future */
626 output_state->info.interlace_mode = info.interlace_mode;
627 gst_video_codec_state_unref (output_state);
629 if (!gst_video_decoder_negotiate (decoder)) {
630 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
636 /* Ensure our internal pool is activated */
637 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
639 goto activate_failed;
642 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) ==
644 /* It's possible that the processing thread stopped due to an error */
645 if (self->output_flow != GST_FLOW_OK &&
646 self->output_flow != GST_FLOW_FLUSHING) {
647 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
648 ret = self->output_flow;
652 GST_DEBUG_OBJECT (self, "Starting decoding thread");
654 /* Start the processing task, when it quits, the task will disable input
655 * processing to unlock input if draining, or prevent potential block */
656 self->output_flow = GST_FLOW_FLUSHING;
657 if (!gst_pad_start_task (decoder->srcpad,
658 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
659 goto start_task_failed;
663 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
665 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
666 pool), &frame->input_buffer);
667 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
669 if (ret == GST_FLOW_FLUSHING) {
670 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
672 ret = self->output_flow;
674 } else if (ret != GST_FLOW_OK) {
679 /* No need to keep input arround */
680 tmp = frame->input_buffer;
681 frame->input_buffer = gst_buffer_new ();
682 gst_buffer_copy_into (frame->input_buffer, tmp,
683 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
684 GST_BUFFER_COPY_META, 0, 0);
685 gst_buffer_unref (tmp);
687 gst_video_codec_frame_unref (frame);
693 GST_ERROR_OBJECT (self, "not negotiated");
694 ret = GST_FLOW_NOT_NEGOTIATED;
695 gst_v4l2_error (self, &error);
700 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
701 (_("Failed to allocate required memory.")),
702 ("Buffer pool activation failed"));
703 ret = GST_FLOW_ERROR;
708 ret = GST_FLOW_FLUSHING;
714 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
715 (_("Failed to start decoding thread.")), (NULL));
716 ret = GST_FLOW_ERROR;
721 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
722 (_("Failed to process frame.")),
723 ("Maybe be due to not enough memory or failing driver"));
724 ret = GST_FLOW_ERROR;
729 gst_video_decoder_drop_frame (decoder, frame);
735 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
738 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
739 GstClockTime latency;
740 gboolean ret = FALSE;
742 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
743 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
746 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
747 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
748 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
749 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
750 self->v4l2capture->min_buffers, self->v4l2capture->duration);
751 gst_video_decoder_set_latency (decoder, latency, latency);
753 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
760 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
763 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
765 switch (GST_QUERY_TYPE (query)) {
766 case GST_QUERY_CAPS:{
767 GstCaps *filter, *result = NULL;
768 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
770 gst_query_parse_caps (query, &filter);
772 if (self->probed_srccaps)
773 result = gst_caps_ref (self->probed_srccaps);
775 result = gst_pad_get_pad_template_caps (pad);
778 GstCaps *tmp = result;
780 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
781 gst_caps_unref (tmp);
784 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
786 gst_query_set_caps_result (query, result);
787 gst_caps_unref (result);
792 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
800 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
802 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
805 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
808 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
814 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
816 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
819 switch (GST_EVENT_TYPE (event)) {
820 case GST_EVENT_FLUSH_START:
821 GST_DEBUG_OBJECT (self, "flush start");
822 gst_v4l2_object_unlock (self->v4l2output);
823 gst_v4l2_object_unlock (self->v4l2capture);
829 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
831 switch (GST_EVENT_TYPE (event)) {
832 case GST_EVENT_FLUSH_START:
833 /* The processing thread should stop now, wait for it */
834 gst_pad_stop_task (decoder->srcpad);
835 GST_DEBUG_OBJECT (self, "flush start done");
844 static GstStateChangeReturn
845 gst_v4l2_video_dec_change_state (GstElement * element,
846 GstStateChange transition)
848 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
849 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
851 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
852 g_atomic_int_set (&self->active, FALSE);
853 gst_v4l2_object_unlock (self->v4l2output);
854 gst_v4l2_object_unlock (self->v4l2capture);
855 gst_pad_stop_task (decoder->srcpad);
858 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
862 gst_v4l2_video_dec_dispose (GObject * object)
864 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
866 gst_caps_replace (&self->probed_sinkcaps, NULL);
867 gst_caps_replace (&self->probed_srccaps, NULL);
869 G_OBJECT_CLASS (parent_class)->dispose (object);
873 gst_v4l2_video_dec_finalize (GObject * object)
875 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
877 gst_v4l2_object_destroy (self->v4l2capture);
878 gst_v4l2_object_destroy (self->v4l2output);
880 G_OBJECT_CLASS (parent_class)->finalize (object);
884 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
886 /* V4L2 object are created in subinstance_init */
890 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
892 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
893 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
894 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
896 gst_video_decoder_set_packetized (decoder, TRUE);
898 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
899 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
900 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
901 self->v4l2output->no_initial_format = TRUE;
902 self->v4l2output->keep_aspect = FALSE;
904 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
905 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
906 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
907 self->v4l2capture->no_initial_format = TRUE;
908 self->v4l2output->keep_aspect = FALSE;
912 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
914 GstElementClass *element_class;
915 GObjectClass *gobject_class;
916 GstVideoDecoderClass *video_decoder_class;
918 parent_class = g_type_class_peek_parent (klass);
920 element_class = (GstElementClass *) klass;
921 gobject_class = (GObjectClass *) klass;
922 video_decoder_class = (GstVideoDecoderClass *) klass;
924 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
925 "V4L2 Video Decoder");
927 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
928 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
929 gobject_class->set_property =
930 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
931 gobject_class->get_property =
932 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
934 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
935 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
936 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
937 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
938 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
939 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
940 video_decoder_class->set_format =
941 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
942 video_decoder_class->negotiate =
943 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
944 video_decoder_class->decide_allocation =
945 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
946 /* FIXME propose_allocation or not ? */
947 video_decoder_class->handle_frame =
948 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
949 video_decoder_class->getcaps =
950 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
951 video_decoder_class->src_query =
952 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
953 video_decoder_class->sink_event =
954 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
956 element_class->change_state =
957 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
959 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
963 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
965 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
966 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
967 GstV4l2VideoDecCData *cdata = data;
969 klass->default_device = cdata->device;
971 /* Note: gst_pad_template_new() take the floating ref from the caps */
972 gst_element_class_add_pad_template (element_class,
973 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
975 gst_element_class_add_pad_template (element_class,
976 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
979 gst_element_class_set_static_metadata (element_class, cdata->longname,
980 "Codec/Decoder/Video", cdata->description,
981 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
986 /* Probing functions */
988 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
990 gboolean ret = FALSE;
992 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
993 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1000 gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
1001 const gchar * basename)
1003 gchar *codec_name = NULL;
1004 gchar *type_name = NULL;
1006 #define SET_META(codec) \
1008 cdata->longname = "V4L2 " codec " Decoder"; \
1009 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1010 codec_name = g_ascii_strdown (codec, -1); \
1013 if (gst_structure_has_name (s, "image/jpeg")) {
1015 } else if (gst_structure_has_name (s, "video/mpeg")) {
1016 gint mpegversion = 0;
1017 gst_structure_get_int (s, "mpegversion", &mpegversion);
1019 if (mpegversion == 2) {
1024 } else if (gst_structure_has_name (s, "video/x-h263")) {
1026 } else if (gst_structure_has_name (s, "video/x-h264")) {
1028 } else if (gst_structure_has_name (s, "video/x-wmv")) {
1030 } else if (gst_structure_has_name (s, "video/x-vp8")) {
1032 } else if (gst_structure_has_name (s, "video/x-bayer")) {
1034 } else if (gst_structure_has_name (s, "video/x-sonix")) {
1036 } else if (gst_structure_has_name (s, "video/x-pwc1")) {
1038 } else if (gst_structure_has_name (s, "video/x-pwc2")) {
1041 /* This code should be kept on sync with the exposed CODEC type of format
1042 * from gstv4l2object.c. This warning will only occure in case we forget
1043 * to also add a format here. */
1044 gchar *s_str = gst_structure_to_string (s);
1045 g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
1046 "bug, please report at https://bugs.gnome.org", s_str);
1051 type_name = g_strdup_printf ("v4l2%sdec", codec_name);
1052 if (g_type_from_name (type_name) != 0) {
1054 type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
1063 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1064 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1068 for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
1069 GstV4l2VideoDecCData *cdata;
1071 GTypeQuery type_query;
1072 GTypeInfo type_info = { 0, };
1073 GType type, subtype;
1076 s = gst_caps_get_structure (sink_caps, i);
1078 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1079 cdata->device = g_strdup (device_path);
1080 cdata->sink_caps = gst_caps_new_empty ();
1081 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
1082 cdata->src_caps = gst_caps_ref (src_caps);
1083 type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
1085 /* Skip over if we hit an unmapped type */
1091 type = gst_v4l2_video_dec_get_type ();
1092 g_type_query (type, &type_query);
1093 memset (&type_info, 0, sizeof (type_info));
1094 type_info.class_size = type_query.class_size;
1095 type_info.instance_size = type_query.instance_size;
1096 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1097 type_info.class_data = cdata;
1098 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1100 subtype = g_type_register_static (type, type_name, &type_info, 0);
1101 gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);