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)
232 GstV4l2Error error = GST_V4L2_ERROR_INIT;
234 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
236 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
238 if (self->input_state) {
239 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
240 GST_DEBUG_OBJECT (self, "Compatible caps");
243 gst_video_codec_state_unref (self->input_state);
244 self->input_state = NULL;
246 /* FIXME we probably need to do more work if pools are active */
249 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
252 self->input_state = gst_video_codec_state_ref (state);
254 gst_v4l2_error (self, &error);
261 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
263 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
265 GST_DEBUG_OBJECT (self, "Flushed");
267 /* Ensure the processing thread has stopped for the reverse playback
269 if (g_atomic_int_get (&self->processing)) {
270 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
272 gst_v4l2_object_unlock (self->v4l2output);
273 gst_v4l2_object_unlock (self->v4l2capture);
274 gst_pad_stop_task (decoder->srcpad);
275 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
278 self->output_flow = GST_FLOW_OK;
280 gst_v4l2_object_unlock_stop (self->v4l2output);
281 gst_v4l2_object_unlock_stop (self->v4l2capture);
287 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
289 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
291 /* We don't allow renegotiation without carefull disabling the pool */
292 if (self->v4l2capture->pool &&
293 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
296 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
300 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
302 struct v4l2_decoder_cmd dcmd = { 0, };
304 GST_DEBUG_OBJECT (v4l2object->element,
305 "sending v4l2 decoder command %u with flags %u", cmd, flags);
307 if (!GST_V4L2_IS_OPEN (v4l2object))
312 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
318 if (errno == ENOTTY) {
319 GST_INFO_OBJECT (v4l2object->element,
320 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
321 cmd, flags, v4l2object->videodev, g_strerror (errno));
323 GST_ERROR_OBJECT (v4l2object->element,
324 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
325 cmd, flags, v4l2object->videodev, g_strerror (errno));
331 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
333 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
334 GstFlowReturn ret = GST_FLOW_OK;
337 if (!g_atomic_int_get (&self->processing))
340 GST_DEBUG_OBJECT (self, "Finishing decoding");
342 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
344 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
345 GstTask *task = decoder->srcpad->task;
347 /* If the decoder stop command succeeded, just wait until processing is
349 GST_OBJECT_LOCK (task);
350 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
351 GST_TASK_WAIT (task);
352 GST_OBJECT_UNLOCK (task);
353 ret = GST_FLOW_FLUSHING;
355 /* otherwise keep queuing empty buffers until the processing thread has
356 * stopped, _pool_process() will return FLUSHING when that happened */
357 while (ret == GST_FLOW_OK) {
358 buffer = gst_buffer_new ();
360 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
361 v4l2output->pool), &buffer);
362 gst_buffer_unref (buffer);
366 /* and ensure the processing thread has stopped in case another error
368 gst_v4l2_object_unlock (self->v4l2capture);
369 gst_pad_stop_task (decoder->srcpad);
370 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
372 if (ret == GST_FLOW_FLUSHING)
373 ret = self->output_flow;
375 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
381 static GstVideoCodecFrame *
382 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
384 GstVideoCodecFrame *frame = NULL;
388 frames = gst_video_decoder_get_frames (decoder);
390 for (l = frames; l != NULL; l = l->next) {
391 GstVideoCodecFrame *f = l->data;
393 if (!frame || frame->pts > f->pts)
400 GST_LOG_OBJECT (decoder,
401 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
402 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
403 gst_video_codec_frame_ref (frame);
406 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
412 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
414 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
415 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
417 GstVideoCodecFrame *frame;
418 GstBuffer *buffer = NULL;
421 GST_LOG_OBJECT (decoder, "Allocate output buffer");
424 /* We cannot use the base class allotate helper since it taking the internal
425 * stream lock. we know that the acquire may need to poll until more frames
426 * comes in and holding this lock would prevent that.
428 pool = gst_video_decoder_get_buffer_pool (decoder);
430 /* Pool may be NULL if we started going to READY state */
432 ret = GST_FLOW_FLUSHING;
436 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
437 g_object_unref (pool);
439 if (ret != GST_FLOW_OK)
442 GST_LOG_OBJECT (decoder, "Process output buffer");
443 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
445 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
447 if (ret != GST_FLOW_OK)
450 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
453 frame->output_buffer = buffer;
455 ret = gst_video_decoder_finish_frame (decoder, frame);
457 if (ret != GST_FLOW_OK)
460 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
461 gst_buffer_unref (buffer);
467 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
468 gst_flow_get_name (ret));
470 gst_buffer_replace (&buffer, NULL);
471 self->output_flow = ret;
472 g_atomic_int_set (&self->processing, FALSE);
473 gst_v4l2_object_unlock (self->v4l2output);
474 gst_pad_pause_task (decoder->srcpad);
478 gst_v4l2_video_dec_loop_stopped (GstV4l2VideoDec * self)
480 /* When flushing, decoding thread may never run */
481 if (g_atomic_int_get (&self->processing)) {
482 GST_DEBUG_OBJECT (self, "Early stop of decoding thread");
483 self->output_flow = GST_FLOW_FLUSHING;
484 g_atomic_int_set (&self->processing, FALSE);
487 GST_DEBUG_OBJECT (self, "Decoding task destroyed: %s",
488 gst_flow_get_name (self->output_flow));
492 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
493 GstStructure * structure, gpointer user_data)
495 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
496 GstVideoAlignment *align = &self->v4l2capture->align;
497 GstVideoInfo *info = &self->v4l2capture->info;
500 if (!gst_structure_get_int (structure, "width", &width))
503 if (!gst_structure_get_int (structure, "height", &height))
506 if (align->padding_left != 0 || align->padding_top != 0 ||
507 width != info->width + align->padding_right ||
508 height != info->height + align->padding_bottom)
511 gst_structure_set (structure,
512 "width", G_TYPE_INT, width - align->padding_right,
513 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
519 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
520 GstVideoCodecFrame * frame)
522 GstV4l2Error error = GST_V4L2_ERROR_INIT;
523 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
524 GstFlowReturn ret = GST_FLOW_OK;
525 gboolean processed = FALSE;
528 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
530 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
533 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
534 if (!self->input_state)
536 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
541 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
542 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
544 GstVideoCodecState *output_state;
545 GstBuffer *codec_data;
546 GstCaps *acquired_caps, *available_caps, *caps, *filter;
549 GST_DEBUG_OBJECT (self, "Sending header");
551 codec_data = self->input_state->codec_data;
553 /* We are running in byte-stream mode, so we don't know the headers, but
554 * we need to send something, otherwise the decoder will refuse to
558 gst_buffer_ref (codec_data);
560 codec_data = gst_buffer_ref (frame->input_buffer);
564 /* Ensure input internal pool is active */
565 if (!gst_buffer_pool_is_active (pool)) {
566 GstStructure *config = gst_buffer_pool_get_config (pool);
567 gst_buffer_pool_config_set_params (config, self->input_state->caps,
568 self->v4l2output->info.size, 2, 2);
570 /* There is no reason to refuse this config */
571 if (!gst_buffer_pool_set_config (pool, config))
572 goto activate_failed;
574 if (!gst_buffer_pool_set_active (pool, TRUE))
575 goto activate_failed;
578 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
580 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
581 v4l2output->pool), &codec_data);
582 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
584 gst_buffer_unref (codec_data);
586 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
587 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
588 * and returns the visible size as with/height and the coded size as
590 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
593 /* Create caps from the acquired format, remove the format field */
594 acquired_caps = gst_video_info_to_caps (&info);
595 st = gst_caps_get_structure (acquired_caps, 0);
596 gst_structure_remove_field (st, "format");
598 /* Probe currently available pixel formats */
599 available_caps = gst_v4l2_object_probe_caps (self->v4l2capture, NULL);
600 available_caps = gst_caps_make_writable (available_caps);
602 /* Replace coded size with visible size, we want to negotiate visible size
603 * with downstream, not coded size. */
604 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
606 filter = gst_caps_intersect_full (available_caps, acquired_caps,
607 GST_CAPS_INTERSECT_FIRST);
608 gst_caps_unref (acquired_caps);
609 gst_caps_unref (available_caps);
610 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
611 gst_caps_unref (filter);
613 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
614 if (gst_caps_is_empty (caps)) {
615 gst_caps_unref (caps);
619 /* Fixate pixel format */
620 caps = gst_caps_fixate (caps);
622 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
624 /* Try to set negotiated format, on success replace acquired format */
625 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
626 gst_video_info_from_caps (&info, caps);
628 gst_v4l2_clear_error (&error);
629 gst_caps_unref (caps);
631 output_state = gst_video_decoder_set_output_state (decoder,
632 info.finfo->format, info.width, info.height, self->input_state);
634 /* Copy the rest of the information, there might be more in the future */
635 output_state->info.interlace_mode = info.interlace_mode;
636 gst_video_codec_state_unref (output_state);
638 if (!gst_video_decoder_negotiate (decoder)) {
639 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
645 /* Ensure our internal pool is activated */
646 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
648 goto activate_failed;
651 if (g_atomic_int_get (&self->processing) == FALSE) {
652 /* It's possible that the processing thread stopped due to an error */
653 if (self->output_flow != GST_FLOW_OK &&
654 self->output_flow != GST_FLOW_FLUSHING) {
655 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
656 ret = self->output_flow;
660 GST_DEBUG_OBJECT (self, "Starting decoding thread");
662 /* Start the processing task, when it quits, the task will disable input
663 * processing to unlock input if draining, or prevent potential block */
664 g_atomic_int_set (&self->processing, TRUE);
665 if (!gst_pad_start_task (decoder->srcpad,
666 (GstTaskFunction) gst_v4l2_video_dec_loop, self,
667 (GDestroyNotify) gst_v4l2_video_dec_loop_stopped))
668 goto start_task_failed;
672 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
674 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
675 pool), &frame->input_buffer);
676 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
678 if (ret == GST_FLOW_FLUSHING) {
679 if (g_atomic_int_get (&self->processing) == FALSE)
680 ret = self->output_flow;
682 } else if (ret != GST_FLOW_OK) {
687 /* No need to keep input arround */
688 tmp = frame->input_buffer;
689 frame->input_buffer = gst_buffer_new ();
690 gst_buffer_copy_into (frame->input_buffer, tmp,
691 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
692 GST_BUFFER_COPY_META, 0, 0);
693 gst_buffer_unref (tmp);
695 gst_video_codec_frame_unref (frame);
701 GST_ERROR_OBJECT (self, "not negotiated");
702 ret = GST_FLOW_NOT_NEGOTIATED;
703 gst_v4l2_error (self, &error);
708 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
709 (_("Failed to allocate required memory.")),
710 ("Buffer pool activation failed"));
711 ret = GST_FLOW_ERROR;
716 ret = GST_FLOW_FLUSHING;
722 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
723 (_("Failed to start decoding thread.")), (NULL));
724 g_atomic_int_set (&self->processing, FALSE);
725 ret = GST_FLOW_ERROR;
730 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
731 (_("Failed to process frame.")),
732 ("Maybe be due to not enough memory or failing driver"));
733 ret = GST_FLOW_ERROR;
738 gst_video_decoder_drop_frame (decoder, frame);
744 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
747 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
748 GstClockTime latency;
749 gboolean ret = FALSE;
751 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
752 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
755 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
756 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
757 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
758 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
759 self->v4l2capture->min_buffers, self->v4l2capture->duration);
760 gst_video_decoder_set_latency (decoder, latency, latency);
762 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
769 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
772 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
774 switch (GST_QUERY_TYPE (query)) {
775 case GST_QUERY_CAPS:{
776 GstCaps *filter, *result = NULL;
777 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
779 gst_query_parse_caps (query, &filter);
781 if (self->probed_srccaps)
782 result = gst_caps_ref (self->probed_srccaps);
784 result = gst_pad_get_pad_template_caps (pad);
787 GstCaps *tmp = result;
789 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
790 gst_caps_unref (tmp);
793 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
795 gst_query_set_caps_result (query, result);
796 gst_caps_unref (result);
801 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
809 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
811 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
814 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
817 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
823 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
825 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
828 switch (GST_EVENT_TYPE (event)) {
829 case GST_EVENT_FLUSH_START:
830 GST_DEBUG_OBJECT (self, "flush start");
831 gst_v4l2_object_unlock (self->v4l2output);
832 gst_v4l2_object_unlock (self->v4l2capture);
838 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
840 switch (GST_EVENT_TYPE (event)) {
841 case GST_EVENT_FLUSH_START:
842 /* The processing thread should stop now, wait for it */
843 gst_pad_stop_task (decoder->srcpad);
844 GST_DEBUG_OBJECT (self, "flush start done");
853 static GstStateChangeReturn
854 gst_v4l2_video_dec_change_state (GstElement * element,
855 GstStateChange transition)
857 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
858 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
860 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
861 g_atomic_int_set (&self->active, FALSE);
862 gst_v4l2_object_unlock (self->v4l2output);
863 gst_v4l2_object_unlock (self->v4l2capture);
864 gst_pad_stop_task (decoder->srcpad);
867 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
871 gst_v4l2_video_dec_dispose (GObject * object)
873 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
875 gst_caps_replace (&self->probed_sinkcaps, NULL);
876 gst_caps_replace (&self->probed_srccaps, NULL);
878 G_OBJECT_CLASS (parent_class)->dispose (object);
882 gst_v4l2_video_dec_finalize (GObject * object)
884 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
886 gst_v4l2_object_destroy (self->v4l2capture);
887 gst_v4l2_object_destroy (self->v4l2output);
889 G_OBJECT_CLASS (parent_class)->finalize (object);
893 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
895 /* V4L2 object are created in subinstance_init */
899 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
901 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
902 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
903 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
905 gst_video_decoder_set_packetized (decoder, TRUE);
907 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
908 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
909 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
910 self->v4l2output->no_initial_format = TRUE;
911 self->v4l2output->keep_aspect = FALSE;
913 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
914 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
915 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
916 self->v4l2capture->no_initial_format = TRUE;
917 self->v4l2output->keep_aspect = FALSE;
921 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
923 GstElementClass *element_class;
924 GObjectClass *gobject_class;
925 GstVideoDecoderClass *video_decoder_class;
927 parent_class = g_type_class_peek_parent (klass);
929 element_class = (GstElementClass *) klass;
930 gobject_class = (GObjectClass *) klass;
931 video_decoder_class = (GstVideoDecoderClass *) klass;
933 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
934 "V4L2 Video Decoder");
936 gst_element_class_set_static_metadata (element_class,
937 "V4L2 Video Decoder",
938 "Codec/Decoder/Video",
939 "Decode video streams via V4L2 API",
940 "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
942 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
943 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
944 gobject_class->set_property =
945 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
946 gobject_class->get_property =
947 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
949 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
950 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
951 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
952 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
953 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
954 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
955 video_decoder_class->set_format =
956 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
957 video_decoder_class->negotiate =
958 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
959 video_decoder_class->decide_allocation =
960 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
961 /* FIXME propose_allocation or not ? */
962 video_decoder_class->handle_frame =
963 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
964 video_decoder_class->getcaps =
965 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
966 video_decoder_class->src_query =
967 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
968 video_decoder_class->sink_event =
969 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
971 element_class->change_state =
972 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
974 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
978 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
980 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
981 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
982 GstV4l2VideoDecCData *cdata = data;
984 klass->default_device = cdata->device;
986 /* Note: gst_pad_template_new() take the floating ref from the caps */
987 gst_element_class_add_pad_template (element_class,
988 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
990 gst_element_class_add_pad_template (element_class,
991 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
997 /* Probing functions */
999 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
1001 gboolean ret = FALSE;
1003 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
1004 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1011 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1012 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1014 GTypeQuery type_query;
1015 GTypeInfo type_info = { 0, };
1016 GType type, subtype;
1018 GstV4l2VideoDecCData *cdata;
1020 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1021 cdata->device = g_strdup (device_path);
1022 cdata->sink_caps = gst_caps_ref (sink_caps);
1023 cdata->src_caps = gst_caps_ref (src_caps);
1025 type = gst_v4l2_video_dec_get_type ();
1026 g_type_query (type, &type_query);
1027 memset (&type_info, 0, sizeof (type_info));
1028 type_info.class_size = type_query.class_size;
1029 type_info.instance_size = type_query.instance_size;
1030 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1031 type_info.class_data = cdata;
1032 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1034 type_name = g_strdup_printf ("v4l2%sdec", basename);
1035 subtype = g_type_register_static (type, type_name, &type_info, 0);
1037 gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);