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);
60 static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder);
62 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
63 static void gst_v4l2_video_dec_flush_buffer_event (GstVideoDecoder * decoder)
68 GST_ERROR("no decoder");
72 ret = gst_pad_push_event (decoder->srcpad,
73 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM | GST_EVENT_TYPE_SERIALIZED,
74 gst_structure_new_empty("tizen/flush-buffer")));
76 GST_WARNING_OBJECT(decoder, "event push ret[%d] for flush-buffer", ret);
78 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
80 gst_v4l2_video_dec_set_property (GObject * object,
81 guint prop_id, const GValue * value, GParamSpec * pspec)
83 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
86 case PROP_CAPTURE_IO_MODE:
87 if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
88 prop_id, value, pspec)) {
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 /* By default, only set on output */
95 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
96 prop_id, value, pspec)) {
97 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 gst_v4l2_video_dec_get_property (GObject * object,
105 guint prop_id, GValue * value, GParamSpec * pspec)
107 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
110 case PROP_CAPTURE_IO_MODE:
111 if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
112 prop_id, value, pspec)) {
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 /* By default read from output */
119 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
120 prop_id, value, pspec)) {
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
130 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
133 GST_DEBUG_OBJECT (self, "Opening");
135 if (!gst_v4l2_object_open (self->v4l2output))
138 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
141 codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
142 self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
144 gst_caps_unref (codec_caps);
146 if (gst_caps_is_empty (self->probed_sinkcaps))
147 goto no_encoded_format;
152 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
153 (_("Decoder on device %s has no supported input format"),
154 self->v4l2output->videodev), (NULL));
158 if (GST_V4L2_IS_OPEN (self->v4l2output))
159 gst_v4l2_object_close (self->v4l2output);
161 if (GST_V4L2_IS_OPEN (self->v4l2capture))
162 gst_v4l2_object_close (self->v4l2capture);
164 gst_caps_replace (&self->probed_srccaps, NULL);
165 gst_caps_replace (&self->probed_sinkcaps, NULL);
171 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
173 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
175 GST_DEBUG_OBJECT (self, "Closing");
177 gst_v4l2_object_close (self->v4l2output);
178 gst_v4l2_object_close (self->v4l2capture);
179 gst_caps_replace (&self->probed_srccaps, NULL);
180 gst_caps_replace (&self->probed_sinkcaps, NULL);
186 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
188 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
190 GST_DEBUG_OBJECT (self, "Starting");
192 gst_v4l2_object_unlock (self->v4l2output);
193 g_atomic_int_set (&self->active, TRUE);
194 self->output_flow = GST_FLOW_OK;
200 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
202 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
204 GST_DEBUG_OBJECT (self, "Stopping");
206 gst_v4l2_object_unlock (self->v4l2output);
207 gst_v4l2_object_unlock (self->v4l2capture);
209 /* Wait for capture thread to stop */
210 gst_pad_stop_task (decoder->srcpad);
212 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
213 self->output_flow = GST_FLOW_OK;
214 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
216 /* Should have been flushed already */
217 g_assert (g_atomic_int_get (&self->active) == FALSE);
218 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
219 gst_v4l2_video_dec_flush_buffer_event (decoder);
220 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
222 gst_v4l2_object_stop (self->v4l2output);
223 gst_v4l2_object_stop (self->v4l2capture);
225 if (self->input_state) {
226 gst_video_codec_state_unref (self->input_state);
227 self->input_state = NULL;
230 GST_DEBUG_OBJECT (self, "Stopped");
236 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
237 GstVideoCodecState * state)
239 GstV4l2Error error = GST_V4L2_ERROR_INIT;
241 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
243 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
245 if (self->input_state) {
246 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
247 GST_DEBUG_OBJECT (self, "Compatible caps");
250 gst_video_codec_state_unref (self->input_state);
251 self->input_state = NULL;
253 gst_v4l2_video_dec_finish (decoder);
254 gst_v4l2_object_stop (self->v4l2output);
256 /* The renegotiation flow don't blend with the base class flow. To properly
257 * stop the capture pool, if the buffers can't be orphaned, we need to
258 * reclaim our buffers, which will happend through the allocation query.
259 * The allocation query is triggered by gst_video_decoder_negotiate() which
260 * requires the output caps to be set, but we can't know this information
261 * as we rely on the decoder, which requires the capture queue to be
264 * To workaround this issue, we simply run an allocation query with the
265 * old negotiated caps in order to drain/reclaim our buffers. That breaks
266 * the complexity and should not have much impact in performance since the
267 * following allocation query will happen on a drained pipeline and won't
269 if (self->v4l2capture->pool &&
270 !gst_v4l2_buffer_pool_orphan (&self->v4l2capture->pool)) {
271 GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
273 GstQuery *query = gst_query_new_allocation (caps, FALSE);
274 gst_pad_peer_query (decoder->srcpad, query);
275 gst_query_unref (query);
276 gst_caps_unref (caps);
280 gst_v4l2_object_stop (self->v4l2capture);
281 self->output_flow = GST_FLOW_OK;
284 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
286 gst_caps_replace (&self->probed_srccaps, NULL);
287 self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
288 gst_v4l2_object_get_raw_caps ());
290 if (gst_caps_is_empty (self->probed_srccaps))
294 self->input_state = gst_video_codec_state_ref (state);
296 gst_v4l2_error (self, &error);
302 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
303 (_("Decoder on device %s has no supported output format"),
304 self->v4l2output->videodev), (NULL));
305 return GST_FLOW_ERROR;
309 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
311 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
313 GST_DEBUG_OBJECT (self, "Flushed");
315 /* Ensure the processing thread has stopped for the reverse playback
317 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
318 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
320 gst_v4l2_object_unlock (self->v4l2output);
321 gst_v4l2_object_unlock (self->v4l2capture);
322 gst_pad_stop_task (decoder->srcpad);
323 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
326 self->output_flow = GST_FLOW_OK;
328 gst_v4l2_object_unlock_stop (self->v4l2output);
329 gst_v4l2_object_unlock_stop (self->v4l2capture);
331 if (self->v4l2output->pool)
332 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
334 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
335 gst_v4l2_video_dec_flush_buffer_event (decoder);
336 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
337 /* gst_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
338 * called after gst_v4l2_object_unlock_stop() stopped flushing the buffer
340 if (self->v4l2capture->pool)
341 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
347 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
349 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
351 /* We don't allow renegotiation without carefull disabling the pool */
352 if (self->v4l2capture->pool &&
353 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
356 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
360 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
362 struct v4l2_decoder_cmd dcmd = { 0, };
364 GST_DEBUG_OBJECT (v4l2object->element,
365 "sending v4l2 decoder command %u with flags %u", cmd, flags);
367 if (!GST_V4L2_IS_OPEN (v4l2object))
372 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
378 if (errno == ENOTTY) {
379 GST_INFO_OBJECT (v4l2object->element,
380 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
381 cmd, flags, v4l2object->videodev, g_strerror (errno));
383 GST_ERROR_OBJECT (v4l2object->element,
384 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
385 cmd, flags, v4l2object->videodev, g_strerror (errno));
391 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
393 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
394 GstFlowReturn ret = GST_FLOW_OK;
397 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
400 GST_DEBUG_OBJECT (self, "Finishing decoding");
402 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
404 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
405 GstTask *task = decoder->srcpad->task;
407 /* If the decoder stop command succeeded, just wait until processing is
409 GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
410 GST_OBJECT_LOCK (task);
411 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
412 GST_TASK_WAIT (task);
413 GST_OBJECT_UNLOCK (task);
414 ret = GST_FLOW_FLUSHING;
416 /* otherwise keep queuing empty buffers until the processing thread has
417 * stopped, _pool_process() will return FLUSHING when that happened */
418 while (ret == GST_FLOW_OK) {
419 buffer = gst_buffer_new ();
421 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
422 v4l2output->pool), &buffer);
423 gst_buffer_unref (buffer);
427 /* and ensure the processing thread has stopped in case another error
429 gst_v4l2_object_unlock (self->v4l2capture);
430 gst_pad_stop_task (decoder->srcpad);
431 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
433 if (ret == GST_FLOW_FLUSHING)
434 ret = self->output_flow;
436 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
438 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
445 gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
447 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
449 GST_DEBUG_OBJECT (self, "Draining...");
450 gst_v4l2_video_dec_finish (decoder);
451 gst_v4l2_video_dec_flush (decoder);
456 static GstVideoCodecFrame *
457 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
459 GstVideoCodecFrame *frame = NULL;
463 frames = gst_video_decoder_get_frames (decoder);
465 for (l = frames; l != NULL; l = l->next) {
466 GstVideoCodecFrame *f = l->data;
468 if (!frame || frame->pts > f->pts)
475 GST_LOG_OBJECT (decoder,
476 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
477 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
478 gst_video_codec_frame_ref (frame);
481 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
487 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
489 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
490 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
492 GstVideoCodecFrame *frame;
493 GstBuffer *buffer = NULL;
496 GST_LOG_OBJECT (decoder, "Allocate output buffer");
498 self->output_flow = GST_FLOW_OK;
500 /* We cannot use the base class allotate helper since it taking the internal
501 * stream lock. we know that the acquire may need to poll until more frames
502 * comes in and holding this lock would prevent that.
504 pool = gst_video_decoder_get_buffer_pool (decoder);
506 /* Pool may be NULL if we started going to READY state */
508 ret = GST_FLOW_FLUSHING;
512 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
513 g_object_unref (pool);
515 if (ret != GST_FLOW_OK)
518 GST_LOG_OBJECT (decoder, "Process output buffer");
519 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
521 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
523 if (ret != GST_FLOW_OK)
526 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
529 frame->output_buffer = buffer;
531 ret = gst_video_decoder_finish_frame (decoder, frame);
533 if (ret != GST_FLOW_OK)
536 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
537 gst_buffer_unref (buffer);
543 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
544 gst_flow_get_name (ret));
546 gst_buffer_replace (&buffer, NULL);
547 self->output_flow = ret;
548 gst_v4l2_object_unlock (self->v4l2output);
549 gst_pad_pause_task (decoder->srcpad);
553 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
554 GstStructure * structure, gpointer user_data)
556 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
557 GstVideoAlignment *align = &self->v4l2capture->align;
558 GstVideoInfo *info = &self->v4l2capture->info;
561 if (!gst_structure_get_int (structure, "width", &width))
564 if (!gst_structure_get_int (structure, "height", &height))
567 if (align->padding_left != 0 || align->padding_top != 0 ||
568 height != info->height + align->padding_bottom)
571 if (height == info->height + align->padding_bottom) {
572 /* Some drivers may round up width to the padded with */
573 if (width == info->width + align->padding_right)
574 gst_structure_set (structure,
575 "width", G_TYPE_INT, width - align->padding_right,
576 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
577 /* Some drivers may keep visible width and only round up bytesperline */
578 else if (width == info->width)
579 gst_structure_set (structure,
580 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
587 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
588 GstVideoCodecFrame * frame)
590 GstV4l2Error error = GST_V4L2_ERROR_INIT;
591 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
592 GstFlowReturn ret = GST_FLOW_OK;
593 gboolean processed = FALSE;
595 GstTaskState task_state;
596 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
597 GstStructure *structure = NULL;
598 const gchar *caps_format = NULL;
599 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
601 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
603 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
606 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
607 if (!self->input_state)
609 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
614 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
615 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
617 GstVideoCodecState *output_state;
618 GstBuffer *codec_data;
619 GstCaps *acquired_caps, *available_caps, *caps, *filter;
622 GST_DEBUG_OBJECT (self, "Sending header");
624 codec_data = self->input_state->codec_data;
626 /* We are running in byte-stream mode, so we don't know the headers, but
627 * we need to send something, otherwise the decoder will refuse to
631 gst_buffer_ref (codec_data);
633 codec_data = gst_buffer_ref (frame->input_buffer);
637 /* Ensure input internal pool is active */
638 if (!gst_buffer_pool_is_active (pool)) {
639 GstStructure *config = gst_buffer_pool_get_config (pool);
640 gst_buffer_pool_config_set_params (config, self->input_state->caps,
641 self->v4l2output->info.size, 2, 2);
643 /* There is no reason to refuse this config */
644 if (!gst_buffer_pool_set_config (pool, config))
645 goto activate_failed;
647 if (!gst_buffer_pool_set_active (pool, TRUE))
648 goto activate_failed;
651 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
653 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
654 v4l2output->pool), &codec_data);
655 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
657 gst_buffer_unref (codec_data);
659 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
660 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
661 * and returns the visible size as with/height and the coded size as
663 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
666 /* Create caps from the acquired format, remove the format field */
667 acquired_caps = gst_video_info_to_caps (&info);
668 GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
669 st = gst_caps_get_structure (acquired_caps, 0);
670 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
673 /* Probe currently available pixel formats */
674 available_caps = gst_caps_copy (self->probed_srccaps);
675 GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
677 /* Replace coded size with visible size, we want to negotiate visible size
678 * with downstream, not coded size. */
679 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
681 filter = gst_caps_intersect_full (available_caps, acquired_caps,
682 GST_CAPS_INTERSECT_FIRST);
683 GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
684 gst_caps_unref (acquired_caps);
685 gst_caps_unref (available_caps);
686 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
687 gst_caps_unref (filter);
689 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
690 if (gst_caps_is_empty (caps)) {
691 gst_caps_unref (caps);
695 /* Fixate pixel format */
696 caps = gst_caps_fixate (caps);
698 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
699 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
700 structure = gst_caps_get_structure (caps, 0);
701 caps_format = gst_structure_get_string (structure, "format");
703 if (!strcmp (caps_format, "I420")) {
704 GST_INFO_OBJECT (self, "I420 -> S420");
705 gst_caps_set_simple (caps, "format", G_TYPE_STRING, "S420", NULL);
706 } else if (!strcmp (caps_format, "NV12")) {
707 GST_INFO_OBJECT (self, "NV12 -> SN12");
708 gst_caps_set_simple (caps, "format", G_TYPE_STRING, "SN12", NULL);
710 GST_INFO_OBJECT (self, "Updated decoded caps: %" GST_PTR_FORMAT, caps);
711 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
713 /* Try to set negotiated format, on success replace acquired format */
714 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
715 gst_video_info_from_caps (&info, caps);
717 gst_v4l2_clear_error (&error);
718 gst_caps_unref (caps);
720 output_state = gst_video_decoder_set_output_state (decoder,
721 info.finfo->format, info.width, info.height, self->input_state);
723 /* Copy the rest of the information, there might be more in the future */
724 output_state->info.interlace_mode = info.interlace_mode;
725 gst_video_codec_state_unref (output_state);
727 if (!gst_video_decoder_negotiate (decoder)) {
728 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
734 /* Ensure our internal pool is activated */
735 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
737 goto activate_failed;
740 task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
741 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
742 /* It's possible that the processing thread stopped due to an error */
743 if (self->output_flow != GST_FLOW_OK &&
744 self->output_flow != GST_FLOW_FLUSHING) {
745 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
746 ret = self->output_flow;
750 GST_DEBUG_OBJECT (self, "Starting decoding thread");
752 /* Start the processing task, when it quits, the task will disable input
753 * processing to unlock input if draining, or prevent potential block */
754 self->output_flow = GST_FLOW_FLUSHING;
755 if (!gst_pad_start_task (decoder->srcpad,
756 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
757 goto start_task_failed;
761 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
763 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
764 pool), &frame->input_buffer);
765 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
767 if (ret == GST_FLOW_FLUSHING) {
768 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
770 ret = self->output_flow;
772 } else if (ret != GST_FLOW_OK) {
777 /* No need to keep input arround */
778 tmp = frame->input_buffer;
779 frame->input_buffer = gst_buffer_new ();
780 gst_buffer_copy_into (frame->input_buffer, tmp,
781 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
782 GST_BUFFER_COPY_META, 0, 0);
783 gst_buffer_unref (tmp);
785 gst_video_codec_frame_unref (frame);
791 GST_ERROR_OBJECT (self, "not negotiated");
792 ret = GST_FLOW_NOT_NEGOTIATED;
793 gst_v4l2_error (self, &error);
798 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
799 (_("Failed to allocate required memory.")),
800 ("Buffer pool activation failed"));
801 ret = GST_FLOW_ERROR;
806 ret = GST_FLOW_FLUSHING;
812 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
813 (_("Failed to start decoding thread.")), (NULL));
814 ret = GST_FLOW_ERROR;
819 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
820 (_("Failed to process frame.")),
821 ("Maybe be due to not enough memory or failing driver"));
822 ret = GST_FLOW_ERROR;
827 gst_video_decoder_drop_frame (decoder, frame);
833 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
836 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
837 GstClockTime latency;
838 gboolean ret = FALSE;
840 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
841 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
844 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
845 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
846 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
847 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
848 self->v4l2capture->min_buffers, self->v4l2capture->duration);
849 gst_video_decoder_set_latency (decoder, latency, latency);
851 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
858 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
861 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
863 switch (GST_QUERY_TYPE (query)) {
864 case GST_QUERY_CAPS:{
865 GstCaps *filter, *result = NULL;
866 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
868 gst_query_parse_caps (query, &filter);
870 if (self->probed_srccaps)
871 result = gst_caps_ref (self->probed_srccaps);
873 result = gst_pad_get_pad_template_caps (pad);
876 GstCaps *tmp = result;
878 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
879 gst_caps_unref (tmp);
882 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
884 gst_query_set_caps_result (query, result);
885 gst_caps_unref (result);
890 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
898 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
900 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
903 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
906 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
912 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
914 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
916 GstEventType type = GST_EVENT_TYPE (event);
919 case GST_EVENT_FLUSH_START:
920 GST_DEBUG_OBJECT (self, "flush start");
921 gst_v4l2_object_unlock (self->v4l2output);
922 gst_v4l2_object_unlock (self->v4l2capture);
928 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
931 case GST_EVENT_FLUSH_START:
932 /* The processing thread should stop now, wait for it */
933 gst_pad_stop_task (decoder->srcpad);
934 GST_DEBUG_OBJECT (self, "flush start done");
943 static GstStateChangeReturn
944 gst_v4l2_video_dec_change_state (GstElement * element,
945 GstStateChange transition)
947 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
948 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
950 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
951 g_atomic_int_set (&self->active, FALSE);
952 gst_v4l2_object_unlock (self->v4l2output);
953 gst_v4l2_object_unlock (self->v4l2capture);
954 gst_pad_stop_task (decoder->srcpad);
957 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
961 gst_v4l2_video_dec_dispose (GObject * object)
963 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
965 gst_caps_replace (&self->probed_sinkcaps, NULL);
966 gst_caps_replace (&self->probed_srccaps, NULL);
968 G_OBJECT_CLASS (parent_class)->dispose (object);
972 gst_v4l2_video_dec_finalize (GObject * object)
974 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
976 gst_v4l2_object_destroy (self->v4l2capture);
977 gst_v4l2_object_destroy (self->v4l2output);
979 G_OBJECT_CLASS (parent_class)->finalize (object);
983 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
985 /* V4L2 object are created in subinstance_init */
989 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
991 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
992 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
993 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
995 gst_video_decoder_set_packetized (decoder, TRUE);
997 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
998 GST_OBJECT (GST_VIDEO_DECODER_SINK_PAD (self)),
999 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1000 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1001 self->v4l2output->no_initial_format = TRUE;
1002 self->v4l2output->keep_aspect = FALSE;
1004 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1005 GST_OBJECT (GST_VIDEO_DECODER_SRC_PAD (self)),
1006 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1007 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1011 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
1013 GstElementClass *element_class;
1014 GObjectClass *gobject_class;
1015 GstVideoDecoderClass *video_decoder_class;
1017 parent_class = g_type_class_peek_parent (klass);
1019 element_class = (GstElementClass *) klass;
1020 gobject_class = (GObjectClass *) klass;
1021 video_decoder_class = (GstVideoDecoderClass *) klass;
1023 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
1024 "V4L2 Video Decoder");
1026 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
1027 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
1028 gobject_class->set_property =
1029 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
1030 gobject_class->get_property =
1031 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
1033 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
1034 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
1035 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
1036 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
1037 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
1038 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
1039 video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_drain);
1040 video_decoder_class->set_format =
1041 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
1042 video_decoder_class->negotiate =
1043 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
1044 video_decoder_class->decide_allocation =
1045 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
1046 /* FIXME propose_allocation or not ? */
1047 video_decoder_class->handle_frame =
1048 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
1049 video_decoder_class->getcaps =
1050 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
1051 video_decoder_class->src_query =
1052 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
1053 video_decoder_class->sink_event =
1054 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
1056 element_class->change_state =
1057 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
1059 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1063 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
1065 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1066 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1067 GstV4l2VideoDecCData *cdata = data;
1069 klass->default_device = cdata->device;
1071 /* Note: gst_pad_template_new() take the floating ref from the caps */
1072 gst_element_class_add_pad_template (element_class,
1073 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1075 gst_element_class_add_pad_template (element_class,
1076 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1079 gst_element_class_set_static_metadata (element_class, cdata->longname,
1080 "Codec/Decoder/Video/Hardware", cdata->description,
1081 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1083 gst_caps_unref (cdata->sink_caps);
1084 gst_caps_unref (cdata->src_caps);
1088 /* Probing functions */
1090 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
1092 gboolean ret = FALSE;
1094 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
1095 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1102 gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
1103 const gchar * basename)
1105 gchar *codec_name = NULL;
1106 gchar *type_name = NULL;
1108 #define SET_META(codec) \
1110 cdata->longname = "V4L2 " codec " Decoder"; \
1111 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1112 codec_name = g_ascii_strdown (codec, -1); \
1115 if (gst_structure_has_name (s, "image/jpeg")) {
1117 } else if (gst_structure_has_name (s, "video/mpeg")) {
1118 gint mpegversion = 0;
1119 gst_structure_get_int (s, "mpegversion", &mpegversion);
1121 if (mpegversion == 2) {
1126 } else if (gst_structure_has_name (s, "video/x-h263")) {
1128 } else if (gst_structure_has_name (s, "video/x-fwht")) {
1130 } else if (gst_structure_has_name (s, "video/x-h264")) {
1132 } else if (gst_structure_has_name (s, "video/x-h265")) {
1134 } else if (gst_structure_has_name (s, "video/x-wmv")) {
1136 } else if (gst_structure_has_name (s, "video/x-vp8")) {
1138 } else if (gst_structure_has_name (s, "video/x-vp9")) {
1140 } else if (gst_structure_has_name (s, "video/x-bayer")) {
1142 } else if (gst_structure_has_name (s, "video/x-sonix")) {
1144 } else if (gst_structure_has_name (s, "video/x-pwc1")) {
1146 } else if (gst_structure_has_name (s, "video/x-pwc2")) {
1149 /* This code should be kept on sync with the exposed CODEC type of format
1150 * from gstv4l2object.c. This warning will only occure in case we forget
1151 * to also add a format here. */
1152 gchar *s_str = gst_structure_to_string (s);
1153 g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
1154 "bug, please report at https://bugs.gnome.org", s_str);
1159 type_name = g_strdup_printf ("v4l2%sdec", codec_name);
1160 if (g_type_from_name (type_name) != 0) {
1162 type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
1165 g_free (codec_name);
1173 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1174 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1178 for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
1179 GstV4l2VideoDecCData *cdata;
1181 GTypeQuery type_query;
1182 GTypeInfo type_info = { 0, };
1183 GType type, subtype;
1186 s = gst_caps_get_structure (sink_caps, i);
1188 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1189 cdata->device = g_strdup (device_path);
1190 cdata->sink_caps = gst_caps_new_empty ();
1191 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
1192 cdata->src_caps = gst_caps_ref (src_caps);
1193 type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
1195 /* Skip over if we hit an unmapped type */
1201 type = gst_v4l2_video_dec_get_type ();
1202 g_type_query (type, &type_query);
1203 memset (&type_info, 0, sizeof (type_info));
1204 type_info.class_size = type_query.class_size;
1205 type_info.instance_size = type_query.instance_size;
1206 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1207 type_info.class_data = cdata;
1208 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1210 subtype = g_type_register_static (type, type_name, &type_info, 0);
1211 #ifdef TIZEN_FEATURE_V4L2VIDEO_ADJ_RANK
1212 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY,
1214 GST_WARNING ("Failed to register plugin '%s'", type_name);
1216 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
1218 GST_WARNING ("Failed to register plugin '%s'", type_name);