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
41 static gboolean gst_v4l2_video_dec_flush (GstVideoDecoder * decoder);
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);
117 GST_DEBUG_OBJECT (self, "Opening");
119 if (!gst_v4l2_object_open (self->v4l2output))
122 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
125 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
126 gst_v4l2_object_get_codec_caps ());
128 if (gst_caps_is_empty (self->probed_sinkcaps))
129 goto no_encoded_format;
131 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
132 gst_v4l2_object_get_raw_caps ());
134 if (gst_caps_is_empty (self->probed_srccaps))
140 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
141 (_("Encoder on device %s has no supported input format"),
142 self->v4l2output->videodev), (NULL));
147 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
148 (_("Encoder on device %s has no supported output format"),
149 self->v4l2output->videodev), (NULL));
153 if (GST_V4L2_IS_OPEN (self->v4l2output))
154 gst_v4l2_object_close (self->v4l2output);
156 if (GST_V4L2_IS_OPEN (self->v4l2capture))
157 gst_v4l2_object_close (self->v4l2capture);
159 gst_caps_replace (&self->probed_srccaps, NULL);
160 gst_caps_replace (&self->probed_sinkcaps, NULL);
166 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
168 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
170 GST_DEBUG_OBJECT (self, "Closing");
172 gst_v4l2_object_close (self->v4l2output);
173 gst_v4l2_object_close (self->v4l2capture);
174 gst_caps_replace (&self->probed_srccaps, NULL);
175 gst_caps_replace (&self->probed_sinkcaps, NULL);
181 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
183 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
185 GST_DEBUG_OBJECT (self, "Starting");
187 gst_v4l2_object_unlock (self->v4l2output);
188 g_atomic_int_set (&self->active, TRUE);
189 self->output_flow = GST_FLOW_OK;
195 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
197 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
199 GST_DEBUG_OBJECT (self, "Stopping");
201 gst_v4l2_object_unlock (self->v4l2output);
202 gst_v4l2_object_unlock (self->v4l2capture);
204 /* Wait for capture thread to stop */
205 gst_pad_stop_task (decoder->srcpad);
207 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
208 self->output_flow = GST_FLOW_OK;
209 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
211 /* Should have been flushed already */
212 g_assert (g_atomic_int_get (&self->active) == FALSE);
213 g_assert (g_atomic_int_get (&self->processing) == FALSE);
215 gst_v4l2_object_stop (self->v4l2output);
216 gst_v4l2_object_stop (self->v4l2capture);
218 if (self->input_state) {
219 gst_video_codec_state_unref (self->input_state);
220 self->input_state = NULL;
223 GST_DEBUG_OBJECT (self, "Stopped");
229 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
230 GstVideoCodecState * state)
233 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
235 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
237 if (self->input_state) {
238 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
239 GST_DEBUG_OBJECT (self, "Compatible caps");
242 gst_video_codec_state_unref (self->input_state);
243 self->input_state = NULL;
245 /* FIXME we probably need to do more work if pools are active */
248 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps);
251 self->input_state = gst_video_codec_state_ref (state);
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 (g_atomic_int_get (&self->processing)) {
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 (!g_atomic_int_get (&self->processing))
337 GST_DEBUG_OBJECT (self, "Finishing decoding");
339 /* Keep queuing empty buffers until the processing thread has stopped,
340 * _pool_process() will return FLUSHING when that happened */
341 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
342 while (ret == GST_FLOW_OK) {
343 buffer = gst_buffer_new ();
345 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
346 v4l2output->pool), &buffer);
347 gst_buffer_unref (buffer);
350 /* and ensure the processing thread has stopped in case another error
352 gst_v4l2_object_unlock (self->v4l2capture);
353 gst_pad_stop_task (decoder->srcpad);
354 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
356 if (ret == GST_FLOW_FLUSHING)
357 ret = self->output_flow;
359 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
365 static GstVideoCodecFrame *
366 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
368 GstVideoCodecFrame *frame = NULL;
372 frames = gst_video_decoder_get_frames (decoder);
374 for (l = frames; l != NULL; l = l->next) {
375 GstVideoCodecFrame *f = l->data;
377 if (!frame || frame->pts > f->pts)
384 GST_LOG_OBJECT (decoder,
385 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
386 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
387 gst_video_codec_frame_ref (frame);
390 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
396 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
398 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
399 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
401 GstVideoCodecFrame *frame;
402 GstBuffer *buffer = NULL;
405 GST_LOG_OBJECT (decoder, "Allocate output buffer");
408 /* We cannot use the base class allotate helper since it taking the internal
409 * stream lock. we know that the acquire may need to poll until more frames
410 * comes in and holding this lock would prevent that.
412 pool = gst_video_decoder_get_buffer_pool (decoder);
414 /* Pool may be NULL if we started going to READY state */
416 ret = GST_FLOW_FLUSHING;
420 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
421 g_object_unref (pool);
423 if (ret != GST_FLOW_OK)
426 GST_LOG_OBJECT (decoder, "Process output buffer");
427 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
429 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
431 if (ret != GST_FLOW_OK)
434 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
437 frame->output_buffer = buffer;
439 ret = gst_video_decoder_finish_frame (decoder, frame);
441 if (ret != GST_FLOW_OK)
444 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
445 gst_buffer_unref (buffer);
451 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
452 gst_flow_get_name (ret));
454 gst_buffer_replace (&buffer, NULL);
455 self->output_flow = ret;
456 g_atomic_int_set (&self->processing, FALSE);
457 gst_v4l2_object_unlock (self->v4l2output);
458 gst_pad_pause_task (decoder->srcpad);
462 gst_v4l2_video_dec_loop_stopped (GstV4l2VideoDec * self)
464 /* When flushing, decoding thread may never run */
465 if (g_atomic_int_get (&self->processing)) {
466 GST_DEBUG_OBJECT (self, "Early stop of decoding thread");
467 self->output_flow = GST_FLOW_FLUSHING;
468 g_atomic_int_set (&self->processing, FALSE);
471 GST_DEBUG_OBJECT (self, "Decoding task destroyed: %s",
472 gst_flow_get_name (self->output_flow));
476 gst_v4l2_video_remove_padding(GstCapsFeatures * features,
477 GstStructure * structure, gpointer user_data)
479 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
480 GstVideoAlignment *align = &self->v4l2capture->align;
481 GstVideoInfo *info = &self->v4l2capture->info;
484 if (!gst_structure_get_int(structure, "width", &width))
487 if (!gst_structure_get_int(structure, "height", &height))
490 if (align->padding_left != 0 || align->padding_top != 0 ||
491 width != info->width + align->padding_right ||
492 height != info->height + align->padding_bottom)
495 gst_structure_set(structure,
496 "width", G_TYPE_INT, width - align->padding_right,
497 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
503 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
504 GstVideoCodecFrame * frame)
506 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
507 GstFlowReturn ret = GST_FLOW_OK;
509 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
511 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
514 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
515 if (!self->input_state)
517 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps))
521 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
522 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
524 GstVideoCodecState *output_state;
525 GstBuffer *codec_data;
526 GstCaps *acquired_caps, *available_caps, *caps, *filter;
529 GST_DEBUG_OBJECT (self, "Sending header");
531 codec_data = self->input_state->codec_data;
533 /* We are running in byte-stream mode, so we don't know the headers, but
534 * we need to send something, otherwise the decoder will refuse to
538 gst_buffer_ref (codec_data);
540 codec_data = frame->input_buffer;
541 frame->input_buffer = NULL;
544 /* Ensure input internal pool is active */
545 if (!gst_buffer_pool_is_active (pool)) {
546 GstStructure *config = gst_buffer_pool_get_config (pool);
547 gst_buffer_pool_config_set_params (config, self->input_state->caps,
548 self->v4l2output->info.size, 2, 2);
550 /* There is no reason to refuse this config */
551 if (!gst_buffer_pool_set_config (pool, config))
552 goto activate_failed;
554 if (!gst_buffer_pool_set_active (pool, TRUE))
555 goto activate_failed;
558 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
560 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
561 v4l2output->pool), &codec_data);
562 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
564 gst_buffer_unref (codec_data);
566 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
567 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
568 * and returns the visible size as with/height and the coded size as
570 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
573 /* Create caps from the acquired format, remove the format field */
574 acquired_caps = gst_video_info_to_caps (&info);
575 st = gst_caps_get_structure (acquired_caps, 0);
576 gst_structure_remove_field (st, "format");
578 /* Probe currently available pixel formats */
579 available_caps = gst_v4l2_object_probe_caps (self->v4l2capture, NULL);
580 available_caps = gst_caps_make_writable (available_caps);
582 /* Replace coded size with visible size, we want to negotiate visible size
583 * with downstream, not coded size. */
584 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
586 filter = gst_caps_intersect_full (available_caps, acquired_caps,
587 GST_CAPS_INTERSECT_FIRST);
588 gst_caps_unref (acquired_caps);
589 gst_caps_unref (available_caps);
590 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
591 gst_caps_unref (filter);
593 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
594 if (gst_caps_is_empty (caps)) {
595 gst_caps_unref (caps);
599 /* Fixate pixel format */
600 caps = gst_caps_fixate (caps);
602 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
604 /* Try to set negotiated format, on success replace acquired format */
605 if (gst_v4l2_object_set_format (self->v4l2capture, caps))
606 gst_video_info_from_caps (&info, caps);
607 gst_caps_unref (caps);
609 output_state = gst_video_decoder_set_output_state (decoder,
610 info.finfo->format, info.width, info.height, self->input_state);
612 /* Copy the rest of the information, there might be more in the future */
613 output_state->info.interlace_mode = info.interlace_mode;
614 gst_video_codec_state_unref (output_state);
616 if (!gst_video_decoder_negotiate (decoder)) {
617 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
623 /* Ensure our internal pool is activated */
624 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
626 goto activate_failed;
629 if (g_atomic_int_get (&self->processing) == FALSE) {
630 /* It's possible that the processing thread stopped due to an error */
631 if (self->output_flow != GST_FLOW_OK &&
632 self->output_flow != GST_FLOW_FLUSHING) {
633 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
634 ret = self->output_flow;
638 GST_DEBUG_OBJECT (self, "Starting decoding thread");
640 /* Start the processing task, when it quits, the task will disable input
641 * processing to unlock input if draining, or prevent potential block */
642 g_atomic_int_set (&self->processing, TRUE);
643 if (!gst_pad_start_task (decoder->srcpad,
644 (GstTaskFunction) gst_v4l2_video_dec_loop, self,
645 (GDestroyNotify) gst_v4l2_video_dec_loop_stopped))
646 goto start_task_failed;
649 if (frame->input_buffer) {
650 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
652 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
653 pool), &frame->input_buffer);
654 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
656 if (ret == GST_FLOW_FLUSHING) {
657 if (g_atomic_int_get (&self->processing) == FALSE)
658 ret = self->output_flow;
660 } else if (ret != GST_FLOW_OK) {
664 /* No need to keep input arround */
665 gst_buffer_replace (&frame->input_buffer, NULL);
668 gst_video_codec_frame_unref (frame);
674 GST_ERROR_OBJECT (self, "not negotiated");
675 ret = GST_FLOW_NOT_NEGOTIATED;
680 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
681 (_("Failed to allocate required memory.")),
682 ("Buffer pool activation failed"));
683 ret = GST_FLOW_ERROR;
688 ret = GST_FLOW_FLUSHING;
694 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
695 (_("Failed to start decoding thread.")), (NULL));
696 g_atomic_int_set (&self->processing, FALSE);
697 ret = GST_FLOW_ERROR;
702 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
703 (_("Failed to process frame.")),
704 ("Maybe be due to not enough memory or failing driver"));
705 ret = GST_FLOW_ERROR;
710 gst_video_decoder_drop_frame (decoder, frame);
716 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
719 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
720 GstClockTime latency;
721 gboolean ret = FALSE;
723 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
724 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
727 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
728 gst_video_decoder_set_latency (decoder, latency, latency);
734 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
737 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
739 switch (GST_QUERY_TYPE (query)) {
740 case GST_QUERY_CAPS:{
741 GstCaps *filter, *result = NULL;
742 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
744 gst_query_parse_caps (query, &filter);
746 if (self->probed_srccaps)
747 result = gst_caps_ref (self->probed_srccaps);
749 result = gst_pad_get_pad_template_caps (pad);
752 GstCaps *tmp = result;
754 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
755 gst_caps_unref (tmp);
758 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
760 gst_query_set_caps_result (query, result);
761 gst_caps_unref (result);
766 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
774 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
776 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
779 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
782 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
788 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
790 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
793 switch (GST_EVENT_TYPE (event)) {
794 case GST_EVENT_FLUSH_START:
795 GST_DEBUG_OBJECT (self, "flush start");
796 gst_v4l2_object_unlock (self->v4l2output);
797 gst_v4l2_object_unlock (self->v4l2capture);
803 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
805 switch (GST_EVENT_TYPE (event)) {
806 case GST_EVENT_FLUSH_START:
807 /* The processing thread should stop now, wait for it */
808 gst_pad_stop_task (decoder->srcpad);
809 GST_DEBUG_OBJECT (self, "flush start done");
818 static GstStateChangeReturn
819 gst_v4l2_video_dec_change_state (GstElement * element,
820 GstStateChange transition)
822 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
823 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
825 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
826 g_atomic_int_set (&self->active, FALSE);
827 gst_v4l2_object_unlock (self->v4l2output);
828 gst_v4l2_object_unlock (self->v4l2capture);
829 gst_pad_stop_task (decoder->srcpad);
832 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
836 gst_v4l2_video_dec_dispose (GObject * object)
838 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
840 gst_caps_replace (&self->probed_sinkcaps, NULL);
841 gst_caps_replace (&self->probed_srccaps, NULL);
843 G_OBJECT_CLASS (parent_class)->dispose (object);
847 gst_v4l2_video_dec_finalize (GObject * object)
849 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
851 gst_v4l2_object_destroy (self->v4l2capture);
852 gst_v4l2_object_destroy (self->v4l2output);
854 G_OBJECT_CLASS (parent_class)->finalize (object);
858 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
860 /* V4L2 object are created in subinstance_init */
864 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
866 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
867 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
868 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
870 gst_video_decoder_set_packetized (decoder, TRUE);
872 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
873 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
874 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
875 self->v4l2output->no_initial_format = TRUE;
876 self->v4l2output->keep_aspect = FALSE;
878 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
879 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
880 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
881 self->v4l2capture->no_initial_format = TRUE;
882 self->v4l2output->keep_aspect = FALSE;
886 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
888 GstElementClass *element_class;
889 GObjectClass *gobject_class;
890 GstVideoDecoderClass *video_decoder_class;
892 parent_class = g_type_class_peek_parent (klass);
894 element_class = (GstElementClass *) klass;
895 gobject_class = (GObjectClass *) klass;
896 video_decoder_class = (GstVideoDecoderClass *) klass;
898 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
899 "V4L2 Video Decoder");
901 gst_element_class_set_static_metadata (element_class,
902 "V4L2 Video Decoder",
903 "Codec/Decoder/Video",
904 "Decode video streams via V4L2 API",
905 "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
907 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
908 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
909 gobject_class->set_property =
910 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
911 gobject_class->get_property =
912 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
914 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
915 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
916 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
917 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
918 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
919 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
920 video_decoder_class->set_format =
921 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
922 video_decoder_class->negotiate =
923 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
924 video_decoder_class->decide_allocation =
925 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
926 /* FIXME propose_allocation or not ? */
927 video_decoder_class->handle_frame =
928 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
929 video_decoder_class->getcaps =
930 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
931 video_decoder_class->src_query =
932 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
933 video_decoder_class->sink_event =
934 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
936 element_class->change_state =
937 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
939 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
943 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
945 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
946 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
947 GstV4l2VideoDecCData *cdata = data;
949 klass->default_device = cdata->device;
951 /* Note: gst_pad_template_new() take the floating ref from the caps */
952 gst_element_class_add_pad_template (element_class,
953 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
955 gst_element_class_add_pad_template (element_class,
956 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
962 /* Probing functions */
964 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
966 gboolean ret = FALSE;
968 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
969 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
976 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
977 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
979 GTypeQuery type_query;
980 GTypeInfo type_info = { 0, };
983 GstV4l2VideoDecCData *cdata;
985 cdata = g_new0 (GstV4l2VideoDecCData, 1);
986 cdata->device = g_strdup (device_path);
987 cdata->sink_caps = gst_caps_ref (sink_caps);
988 cdata->src_caps = gst_caps_ref (src_caps);
990 type = gst_v4l2_video_dec_get_type ();
991 g_type_query (type, &type_query);
992 memset (&type_info, 0, sizeof (type_info));
993 type_info.class_size = type_query.class_size;
994 type_info.instance_size = type_query.instance_size;
995 type_info.class_init = gst_v4l2_video_dec_subclass_init;
996 type_info.class_data = cdata;
997 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
999 type_name = g_strdup_printf ("v4l2%sdec", basename);
1000 subtype = g_type_register_static (type, type_name, &type_info, 0);
1002 gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);