2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
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 "gstv4l2videodec.h"
33 #include "v4l2_calls.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 } GstV4l2VideoDecCData;
54 #define gst_v4l2_video_dec_parent_class parent_class
55 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
56 GST_TYPE_VIDEO_DECODER);
59 gst_v4l2_video_dec_set_property (GObject * object,
60 guint prop_id, const GValue * value, GParamSpec * pspec)
62 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
65 case PROP_OUTPUT_IO_MODE:
66 gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
69 case PROP_CAPTURE_IO_MODE:
70 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
74 /* By default, only set on output */
76 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
77 prop_id, value, pspec)) {
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 gst_v4l2_video_dec_get_property (GObject * object,
86 guint prop_id, GValue * value, GParamSpec * pspec)
88 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
91 case PROP_OUTPUT_IO_MODE:
92 gst_v4l2_object_get_property_helper (self->v4l2output, prop_id, value,
95 case PROP_CAPTURE_IO_MODE:
96 gst_v4l2_object_get_property_helper (self->v4l2capture, prop_id, value,
100 /* By default read from output */
102 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
103 prop_id, value, pspec)) {
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
113 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
115 GST_DEBUG_OBJECT (self, "Opening");
117 if (!gst_v4l2_object_open (self->v4l2output))
120 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
123 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
124 gst_v4l2_object_get_codec_caps ());
126 if (gst_caps_is_empty (self->probed_sinkcaps))
127 goto no_encoded_format;
129 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
130 gst_v4l2_object_get_raw_caps ());
132 if (gst_caps_is_empty (self->probed_srccaps))
138 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
139 (_("Encoder on device %s has no supported input format"),
140 self->v4l2output->videodev), (NULL));
145 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
146 (_("Encoder on device %s has no supported output format"),
147 self->v4l2output->videodev), (NULL));
151 if (GST_V4L2_IS_OPEN (self->v4l2output))
152 gst_v4l2_object_close (self->v4l2output);
154 if (GST_V4L2_IS_OPEN (self->v4l2capture))
155 gst_v4l2_object_close (self->v4l2capture);
157 gst_caps_replace (&self->probed_srccaps, NULL);
158 gst_caps_replace (&self->probed_sinkcaps, NULL);
164 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
166 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
168 GST_DEBUG_OBJECT (self, "Closing");
170 gst_v4l2_object_close (self->v4l2output);
171 gst_v4l2_object_close (self->v4l2capture);
172 gst_caps_replace (&self->probed_srccaps, NULL);
173 gst_caps_replace (&self->probed_sinkcaps, NULL);
179 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
181 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
183 GST_DEBUG_OBJECT (self, "Starting");
185 gst_v4l2_object_unlock (self->v4l2output);
186 g_atomic_int_set (&self->active, TRUE);
187 self->output_flow = GST_FLOW_OK;
193 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
195 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
197 GST_DEBUG_OBJECT (self, "Stopping");
199 gst_v4l2_object_unlock (self->v4l2output);
200 gst_v4l2_object_unlock (self->v4l2capture);
202 /* Wait for capture thread to stop */
203 gst_pad_stop_task (decoder->srcpad);
205 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
206 self->output_flow = GST_FLOW_OK;
207 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
209 /* Should have been flushed already */
210 g_assert (g_atomic_int_get (&self->active) == FALSE);
212 gst_v4l2_object_stop (self->v4l2output);
213 gst_v4l2_object_stop (self->v4l2capture);
215 if (self->input_state) {
216 gst_video_codec_state_unref (self->input_state);
217 self->input_state = NULL;
220 GST_DEBUG_OBJECT (self, "Stopped");
226 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
227 GstVideoCodecState * state)
229 GstV4l2Error error = GST_V4L2_ERROR_INIT;
231 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
233 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
235 if (self->input_state) {
236 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
237 GST_DEBUG_OBJECT (self, "Compatible caps");
240 gst_video_codec_state_unref (self->input_state);
241 self->input_state = NULL;
243 /* FIXME we probably need to do more work if pools are active */
246 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
249 self->input_state = gst_video_codec_state_ref (state);
251 gst_v4l2_error (self, &error);
258 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
260 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
262 GST_DEBUG_OBJECT (self, "Flushed");
264 /* Ensure the processing thread has stopped for the reverse playback
266 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
267 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
269 gst_v4l2_object_unlock (self->v4l2output);
270 gst_v4l2_object_unlock (self->v4l2capture);
271 gst_pad_stop_task (decoder->srcpad);
272 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
275 self->output_flow = GST_FLOW_OK;
277 gst_v4l2_object_unlock_stop (self->v4l2output);
278 gst_v4l2_object_unlock_stop (self->v4l2capture);
284 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
286 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
288 /* We don't allow renegotiation without carefull disabling the pool */
289 if (self->v4l2capture->pool &&
290 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
293 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
297 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
299 struct v4l2_decoder_cmd dcmd = { 0, };
301 GST_DEBUG_OBJECT (v4l2object->element,
302 "sending v4l2 decoder command %u with flags %u", cmd, flags);
304 if (!GST_V4L2_IS_OPEN (v4l2object))
309 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
315 if (errno == ENOTTY) {
316 GST_INFO_OBJECT (v4l2object->element,
317 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
318 cmd, flags, v4l2object->videodev, g_strerror (errno));
320 GST_ERROR_OBJECT (v4l2object->element,
321 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
322 cmd, flags, v4l2object->videodev, g_strerror (errno));
328 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
330 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
331 GstFlowReturn ret = GST_FLOW_OK;
334 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
337 GST_DEBUG_OBJECT (self, "Finishing decoding");
339 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
341 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
342 GstTask *task = decoder->srcpad->task;
344 /* If the decoder stop command succeeded, just wait until processing is
346 GST_OBJECT_LOCK (task);
347 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
348 GST_TASK_WAIT (task);
349 GST_OBJECT_UNLOCK (task);
350 ret = GST_FLOW_FLUSHING;
352 /* otherwise keep queuing empty buffers until the processing thread has
353 * stopped, _pool_process() will return FLUSHING when that happened */
354 while (ret == GST_FLOW_OK) {
355 buffer = gst_buffer_new ();
357 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
358 v4l2output->pool), &buffer);
359 gst_buffer_unref (buffer);
363 /* and ensure the processing thread has stopped in case another error
365 gst_v4l2_object_unlock (self->v4l2capture);
366 gst_pad_stop_task (decoder->srcpad);
367 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
369 if (ret == GST_FLOW_FLUSHING)
370 ret = self->output_flow;
372 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
378 static GstVideoCodecFrame *
379 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
381 GstVideoCodecFrame *frame = NULL;
385 frames = gst_video_decoder_get_frames (decoder);
387 for (l = frames; l != NULL; l = l->next) {
388 GstVideoCodecFrame *f = l->data;
390 if (!frame || frame->pts > f->pts)
397 GST_LOG_OBJECT (decoder,
398 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
399 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
400 gst_video_codec_frame_ref (frame);
403 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
409 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
411 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
412 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
414 GstVideoCodecFrame *frame;
415 GstBuffer *buffer = NULL;
418 GST_LOG_OBJECT (decoder, "Allocate output buffer");
420 self->output_flow = GST_FLOW_OK;
422 /* We cannot use the base class allotate helper since it taking the internal
423 * stream lock. we know that the acquire may need to poll until more frames
424 * comes in and holding this lock would prevent that.
426 pool = gst_video_decoder_get_buffer_pool (decoder);
428 /* Pool may be NULL if we started going to READY state */
430 ret = GST_FLOW_FLUSHING;
434 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
435 g_object_unref (pool);
437 if (ret != GST_FLOW_OK)
440 GST_LOG_OBJECT (decoder, "Process output buffer");
441 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
443 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
445 if (ret != GST_FLOW_OK)
448 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
451 frame->output_buffer = buffer;
453 ret = gst_video_decoder_finish_frame (decoder, frame);
455 if (ret != GST_FLOW_OK)
458 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
459 gst_buffer_unref (buffer);
465 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
466 gst_flow_get_name (ret));
468 gst_buffer_replace (&buffer, NULL);
469 self->output_flow = ret;
470 gst_v4l2_object_unlock (self->v4l2output);
471 gst_pad_pause_task (decoder->srcpad);
475 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
476 GstStructure * structure, gpointer user_data)
478 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
479 GstVideoAlignment *align = &self->v4l2capture->align;
480 GstVideoInfo *info = &self->v4l2capture->info;
483 if (!gst_structure_get_int (structure, "width", &width))
486 if (!gst_structure_get_int (structure, "height", &height))
489 if (align->padding_left != 0 || align->padding_top != 0 ||
490 width != info->width + align->padding_right ||
491 height != info->height + align->padding_bottom)
494 gst_structure_set (structure,
495 "width", G_TYPE_INT, width - align->padding_right,
496 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
502 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
503 GstVideoCodecFrame * frame)
505 GstV4l2Error error = GST_V4L2_ERROR_INIT;
506 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
507 GstFlowReturn ret = GST_FLOW_OK;
508 gboolean processed = FALSE;
511 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
513 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
516 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
517 if (!self->input_state)
519 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
524 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
525 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
527 GstVideoCodecState *output_state;
528 GstBuffer *codec_data;
529 GstCaps *acquired_caps, *available_caps, *caps, *filter;
532 GST_DEBUG_OBJECT (self, "Sending header");
534 codec_data = self->input_state->codec_data;
536 /* We are running in byte-stream mode, so we don't know the headers, but
537 * we need to send something, otherwise the decoder will refuse to
541 gst_buffer_ref (codec_data);
543 codec_data = gst_buffer_ref (frame->input_buffer);
547 /* Ensure input internal pool is active */
548 if (!gst_buffer_pool_is_active (pool)) {
549 GstStructure *config = gst_buffer_pool_get_config (pool);
550 gst_buffer_pool_config_set_params (config, self->input_state->caps,
551 self->v4l2output->info.size, 2, 2);
553 /* There is no reason to refuse this config */
554 if (!gst_buffer_pool_set_config (pool, config))
555 goto activate_failed;
557 if (!gst_buffer_pool_set_active (pool, TRUE))
558 goto activate_failed;
561 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
563 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
564 v4l2output->pool), &codec_data);
565 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
567 gst_buffer_unref (codec_data);
569 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
570 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
571 * and returns the visible size as with/height and the coded size as
573 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
576 /* Create caps from the acquired format, remove the format field */
577 acquired_caps = gst_video_info_to_caps (&info);
578 st = gst_caps_get_structure (acquired_caps, 0);
579 gst_structure_remove_field (st, "format");
581 /* Probe currently available pixel formats */
582 available_caps = gst_v4l2_object_probe_caps (self->v4l2capture, NULL);
583 available_caps = gst_caps_make_writable (available_caps);
585 /* Replace coded size with visible size, we want to negotiate visible size
586 * with downstream, not coded size. */
587 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
589 filter = gst_caps_intersect_full (available_caps, acquired_caps,
590 GST_CAPS_INTERSECT_FIRST);
591 gst_caps_unref (acquired_caps);
592 gst_caps_unref (available_caps);
593 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
594 gst_caps_unref (filter);
596 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
597 if (gst_caps_is_empty (caps)) {
598 gst_caps_unref (caps);
602 /* Fixate pixel format */
603 caps = gst_caps_fixate (caps);
605 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
607 /* Try to set negotiated format, on success replace acquired format */
608 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
609 gst_video_info_from_caps (&info, caps);
611 gst_v4l2_clear_error (&error);
612 gst_caps_unref (caps);
614 output_state = gst_video_decoder_set_output_state (decoder,
615 info.finfo->format, info.width, info.height, self->input_state);
617 /* Copy the rest of the information, there might be more in the future */
618 output_state->info.interlace_mode = info.interlace_mode;
619 gst_video_codec_state_unref (output_state);
621 if (!gst_video_decoder_negotiate (decoder)) {
622 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
628 /* Ensure our internal pool is activated */
629 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
631 goto activate_failed;
634 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) ==
636 /* It's possible that the processing thread stopped due to an error */
637 if (self->output_flow != GST_FLOW_OK &&
638 self->output_flow != GST_FLOW_FLUSHING) {
639 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
640 ret = self->output_flow;
644 GST_DEBUG_OBJECT (self, "Starting decoding thread");
646 /* Start the processing task, when it quits, the task will disable input
647 * processing to unlock input if draining, or prevent potential block */
648 self->output_flow = GST_FLOW_FLUSHING;
649 if (!gst_pad_start_task (decoder->srcpad,
650 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
651 goto start_task_failed;
655 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
657 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
658 pool), &frame->input_buffer);
659 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
661 if (ret == GST_FLOW_FLUSHING) {
662 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
664 ret = self->output_flow;
666 } else if (ret != GST_FLOW_OK) {
671 /* No need to keep input arround */
672 tmp = frame->input_buffer;
673 frame->input_buffer = gst_buffer_new ();
674 gst_buffer_copy_into (frame->input_buffer, tmp,
675 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
676 GST_BUFFER_COPY_META, 0, 0);
677 gst_buffer_unref (tmp);
679 gst_video_codec_frame_unref (frame);
685 GST_ERROR_OBJECT (self, "not negotiated");
686 ret = GST_FLOW_NOT_NEGOTIATED;
687 gst_v4l2_error (self, &error);
692 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
693 (_("Failed to allocate required memory.")),
694 ("Buffer pool activation failed"));
695 ret = GST_FLOW_ERROR;
700 ret = GST_FLOW_FLUSHING;
706 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
707 (_("Failed to start decoding thread.")), (NULL));
708 ret = GST_FLOW_ERROR;
713 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
714 (_("Failed to process frame.")),
715 ("Maybe be due to not enough memory or failing driver"));
716 ret = GST_FLOW_ERROR;
721 gst_video_decoder_drop_frame (decoder, frame);
727 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
730 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
731 GstClockTime latency;
732 gboolean ret = FALSE;
734 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
735 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
738 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
739 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
740 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
741 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
742 self->v4l2capture->min_buffers, self->v4l2capture->duration);
743 gst_video_decoder_set_latency (decoder, latency, latency);
745 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
752 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
755 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
757 switch (GST_QUERY_TYPE (query)) {
758 case GST_QUERY_CAPS:{
759 GstCaps *filter, *result = NULL;
760 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
762 gst_query_parse_caps (query, &filter);
764 if (self->probed_srccaps)
765 result = gst_caps_ref (self->probed_srccaps);
767 result = gst_pad_get_pad_template_caps (pad);
770 GstCaps *tmp = result;
772 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
773 gst_caps_unref (tmp);
776 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
778 gst_query_set_caps_result (query, result);
779 gst_caps_unref (result);
784 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
792 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
794 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
797 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
800 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
806 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
808 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
811 switch (GST_EVENT_TYPE (event)) {
812 case GST_EVENT_FLUSH_START:
813 GST_DEBUG_OBJECT (self, "flush start");
814 gst_v4l2_object_unlock (self->v4l2output);
815 gst_v4l2_object_unlock (self->v4l2capture);
821 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
823 switch (GST_EVENT_TYPE (event)) {
824 case GST_EVENT_FLUSH_START:
825 /* The processing thread should stop now, wait for it */
826 gst_pad_stop_task (decoder->srcpad);
827 GST_DEBUG_OBJECT (self, "flush start done");
836 static GstStateChangeReturn
837 gst_v4l2_video_dec_change_state (GstElement * element,
838 GstStateChange transition)
840 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
841 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
843 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
844 g_atomic_int_set (&self->active, FALSE);
845 gst_v4l2_object_unlock (self->v4l2output);
846 gst_v4l2_object_unlock (self->v4l2capture);
847 gst_pad_stop_task (decoder->srcpad);
850 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
854 gst_v4l2_video_dec_dispose (GObject * object)
856 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
858 gst_caps_replace (&self->probed_sinkcaps, NULL);
859 gst_caps_replace (&self->probed_srccaps, NULL);
861 G_OBJECT_CLASS (parent_class)->dispose (object);
865 gst_v4l2_video_dec_finalize (GObject * object)
867 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
869 gst_v4l2_object_destroy (self->v4l2capture);
870 gst_v4l2_object_destroy (self->v4l2output);
872 G_OBJECT_CLASS (parent_class)->finalize (object);
876 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
878 /* V4L2 object are created in subinstance_init */
882 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
884 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
885 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
886 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
888 gst_video_decoder_set_packetized (decoder, TRUE);
890 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
891 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
892 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
893 self->v4l2output->no_initial_format = TRUE;
894 self->v4l2output->keep_aspect = FALSE;
896 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
897 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
898 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
899 self->v4l2capture->no_initial_format = TRUE;
900 self->v4l2output->keep_aspect = FALSE;
904 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
906 GstElementClass *element_class;
907 GObjectClass *gobject_class;
908 GstVideoDecoderClass *video_decoder_class;
910 parent_class = g_type_class_peek_parent (klass);
912 element_class = (GstElementClass *) klass;
913 gobject_class = (GObjectClass *) klass;
914 video_decoder_class = (GstVideoDecoderClass *) klass;
916 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
917 "V4L2 Video Decoder");
919 gst_element_class_set_static_metadata (element_class,
920 "V4L2 Video Decoder",
921 "Codec/Decoder/Video",
922 "Decode video streams via V4L2 API",
923 "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
925 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
926 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
927 gobject_class->set_property =
928 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
929 gobject_class->get_property =
930 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
932 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
933 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
934 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
935 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
936 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
937 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
938 video_decoder_class->set_format =
939 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
940 video_decoder_class->negotiate =
941 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
942 video_decoder_class->decide_allocation =
943 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
944 /* FIXME propose_allocation or not ? */
945 video_decoder_class->handle_frame =
946 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
947 video_decoder_class->getcaps =
948 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
949 video_decoder_class->src_query =
950 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
951 video_decoder_class->sink_event =
952 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
954 element_class->change_state =
955 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
957 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
961 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
963 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
964 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
965 GstV4l2VideoDecCData *cdata = data;
967 klass->default_device = cdata->device;
969 /* Note: gst_pad_template_new() take the floating ref from the caps */
970 gst_element_class_add_pad_template (element_class,
971 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
973 gst_element_class_add_pad_template (element_class,
974 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
980 /* Probing functions */
982 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
984 gboolean ret = FALSE;
986 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
987 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
994 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
995 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
997 GTypeQuery type_query;
998 GTypeInfo type_info = { 0, };
1001 GstV4l2VideoDecCData *cdata;
1003 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1004 cdata->device = g_strdup (device_path);
1005 cdata->sink_caps = gst_caps_ref (sink_caps);
1006 cdata->src_caps = gst_caps_ref (src_caps);
1008 type = gst_v4l2_video_dec_get_type ();
1009 g_type_query (type, &type_query);
1010 memset (&type_info, 0, sizeof (type_info));
1011 type_info.class_size = type_query.class_size;
1012 type_info.instance_size = type_query.instance_size;
1013 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1014 type_info.class_data = cdata;
1015 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1017 type_name = g_strdup_printf ("v4l2%sdec", basename);
1018 subtype = g_type_register_static (type, type_name, &type_info, 0);
1020 gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);