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;
54 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
56 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
59 #define gst_v4l2_video_dec_parent_class parent_class
60 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
61 GST_TYPE_VIDEO_DECODER);
63 static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder);
65 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
66 static void gst_v4l2_video_dec_flush_buffer_event (GstVideoDecoder * decoder)
71 GST_ERROR("no decoder");
75 ret = gst_pad_push_event (decoder->srcpad,
76 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM | GST_EVENT_TYPE_SERIALIZED,
77 gst_structure_new_empty("tizen/flush-buffer")));
79 GST_WARNING_OBJECT(decoder, "event push ret[%d] for flush-buffer", ret);
81 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
83 gst_v4l2_video_dec_set_property (GObject * object,
84 guint prop_id, const GValue * value, GParamSpec * pspec)
86 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
89 case PROP_CAPTURE_IO_MODE:
90 if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
91 prop_id, value, pspec)) {
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
97 self->v4l2capture->tbm_output = g_value_get_boolean (value);
98 GST_INFO_OBJECT (self, "tbm output [%d]", self->v4l2capture->tbm_output);
100 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
103 /* By default, only set on output */
105 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
106 prop_id, value, pspec)) {
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 gst_v4l2_video_dec_get_property (GObject * object,
115 guint prop_id, GValue * value, GParamSpec * pspec)
117 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
120 case PROP_CAPTURE_IO_MODE:
121 if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
122 prop_id, value, pspec)) {
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
127 case PROP_TBM_OUTPUT:
128 GST_INFO_OBJECT (self, "tbm output [%d]", self->v4l2capture->tbm_output);
129 g_value_set_boolean (value, self->v4l2capture->tbm_output);
131 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
133 /* By default read from output */
135 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
136 prop_id, value, pspec)) {
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
146 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
149 GST_DEBUG_OBJECT (self, "Opening");
151 if (!gst_v4l2_object_open (self->v4l2output))
154 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
157 codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
158 self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
160 gst_caps_unref (codec_caps);
162 if (gst_caps_is_empty (self->probed_sinkcaps))
163 goto no_encoded_format;
168 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
169 (_("Decoder on device %s has no supported input format"),
170 self->v4l2output->videodev), (NULL));
174 if (GST_V4L2_IS_OPEN (self->v4l2output))
175 gst_v4l2_object_close (self->v4l2output);
177 if (GST_V4L2_IS_OPEN (self->v4l2capture))
178 gst_v4l2_object_close (self->v4l2capture);
180 gst_caps_replace (&self->probed_srccaps, NULL);
181 gst_caps_replace (&self->probed_sinkcaps, NULL);
187 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
189 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
191 GST_DEBUG_OBJECT (self, "Closing");
193 gst_v4l2_object_close (self->v4l2output);
194 gst_v4l2_object_close (self->v4l2capture);
195 gst_caps_replace (&self->probed_srccaps, NULL);
196 gst_caps_replace (&self->probed_sinkcaps, NULL);
202 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
204 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
206 GST_DEBUG_OBJECT (self, "Starting");
208 gst_v4l2_object_unlock (self->v4l2output);
209 g_atomic_int_set (&self->active, TRUE);
210 self->output_flow = GST_FLOW_OK;
216 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
218 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
220 GST_DEBUG_OBJECT (self, "Stopping");
222 gst_v4l2_object_unlock (self->v4l2output);
223 gst_v4l2_object_unlock (self->v4l2capture);
225 /* Wait for capture thread to stop */
226 gst_pad_stop_task (decoder->srcpad);
228 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
229 self->output_flow = GST_FLOW_OK;
230 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
232 /* Should have been flushed already */
233 g_assert (g_atomic_int_get (&self->active) == FALSE);
234 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
235 gst_v4l2_video_dec_flush_buffer_event (decoder);
236 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
238 gst_v4l2_object_stop (self->v4l2output);
239 gst_v4l2_object_stop (self->v4l2capture);
241 if (self->input_state) {
242 gst_video_codec_state_unref (self->input_state);
243 self->input_state = NULL;
246 GST_DEBUG_OBJECT (self, "Stopped");
252 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
253 GstVideoCodecState * state)
255 GstV4l2Error error = GST_V4L2_ERROR_INIT;
257 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
259 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
261 if (self->input_state) {
262 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
263 GST_DEBUG_OBJECT (self, "Compatible caps");
266 gst_video_codec_state_unref (self->input_state);
267 self->input_state = NULL;
269 gst_v4l2_video_dec_finish (decoder);
270 gst_v4l2_object_stop (self->v4l2output);
272 /* The renegotiation flow don't blend with the base class flow. To properly
273 * stop the capture pool, if the buffers can't be orphaned, we need to
274 * reclaim our buffers, which will happend through the allocation query.
275 * The allocation query is triggered by gst_video_decoder_negotiate() which
276 * requires the output caps to be set, but we can't know this information
277 * as we rely on the decoder, which requires the capture queue to be
280 * To workaround this issue, we simply run an allocation query with the
281 * old negotiated caps in order to drain/reclaim our buffers. That breaks
282 * the complexity and should not have much impact in performance since the
283 * following allocation query will happen on a drained pipeline and won't
285 if (self->v4l2capture->pool &&
286 !gst_v4l2_buffer_pool_orphan (&self->v4l2capture->pool)) {
287 GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
289 GstQuery *query = gst_query_new_allocation (caps, FALSE);
290 gst_pad_peer_query (decoder->srcpad, query);
291 gst_query_unref (query);
292 gst_caps_unref (caps);
296 gst_v4l2_object_stop (self->v4l2capture);
297 self->output_flow = GST_FLOW_OK;
300 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
302 gst_caps_replace (&self->probed_srccaps, NULL);
303 self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
304 gst_v4l2_object_get_raw_caps ());
306 if (gst_caps_is_empty (self->probed_srccaps))
310 self->input_state = gst_video_codec_state_ref (state);
312 gst_v4l2_error (self, &error);
318 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
319 (_("Decoder on device %s has no supported output format"),
320 self->v4l2output->videodev), (NULL));
321 return GST_FLOW_ERROR;
325 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
327 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
329 GST_DEBUG_OBJECT (self, "Flushed");
331 /* Ensure the processing thread has stopped for the reverse playback
333 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
334 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
336 gst_v4l2_object_unlock (self->v4l2output);
337 gst_v4l2_object_unlock (self->v4l2capture);
338 gst_pad_stop_task (decoder->srcpad);
339 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
342 self->output_flow = GST_FLOW_OK;
344 gst_v4l2_object_unlock_stop (self->v4l2output);
345 gst_v4l2_object_unlock_stop (self->v4l2capture);
347 if (self->v4l2output->pool)
348 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
350 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
351 gst_v4l2_video_dec_flush_buffer_event (decoder);
352 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
353 /* gst_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
354 * called after gst_v4l2_object_unlock_stop() stopped flushing the buffer
356 if (self->v4l2capture->pool)
357 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
363 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
365 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
367 /* We don't allow renegotiation without carefull disabling the pool */
368 if (self->v4l2capture->pool &&
369 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
372 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
376 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
378 struct v4l2_decoder_cmd dcmd = { 0, };
380 GST_DEBUG_OBJECT (v4l2object->element,
381 "sending v4l2 decoder command %u with flags %u", cmd, flags);
383 if (!GST_V4L2_IS_OPEN (v4l2object))
388 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
394 if (errno == ENOTTY) {
395 GST_INFO_OBJECT (v4l2object->element,
396 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
397 cmd, flags, v4l2object->videodev, g_strerror (errno));
399 GST_ERROR_OBJECT (v4l2object->element,
400 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
401 cmd, flags, v4l2object->videodev, g_strerror (errno));
407 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
409 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
410 GstFlowReturn ret = GST_FLOW_OK;
413 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
416 GST_DEBUG_OBJECT (self, "Finishing decoding");
418 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
420 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
421 GstTask *task = decoder->srcpad->task;
423 /* If the decoder stop command succeeded, just wait until processing is
425 GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
426 GST_OBJECT_LOCK (task);
427 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
428 GST_TASK_WAIT (task);
429 GST_OBJECT_UNLOCK (task);
430 ret = GST_FLOW_FLUSHING;
432 /* otherwise keep queuing empty buffers until the processing thread has
433 * stopped, _pool_process() will return FLUSHING when that happened */
434 while (ret == GST_FLOW_OK) {
435 buffer = gst_buffer_new ();
437 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
438 v4l2output->pool), &buffer);
439 gst_buffer_unref (buffer);
443 /* and ensure the processing thread has stopped in case another error
445 gst_v4l2_object_unlock (self->v4l2capture);
446 gst_pad_stop_task (decoder->srcpad);
447 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
449 if (ret == GST_FLOW_FLUSHING)
450 ret = self->output_flow;
452 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
454 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
461 gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
463 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
465 GST_DEBUG_OBJECT (self, "Draining...");
466 gst_v4l2_video_dec_finish (decoder);
467 gst_v4l2_video_dec_flush (decoder);
472 static GstVideoCodecFrame *
473 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
475 GstVideoCodecFrame *frame = NULL;
479 frames = gst_video_decoder_get_frames (decoder);
481 for (l = frames; l != NULL; l = l->next) {
482 GstVideoCodecFrame *f = l->data;
484 if (!frame || frame->pts > f->pts)
491 GST_LOG_OBJECT (decoder,
492 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
493 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
494 gst_video_codec_frame_ref (frame);
497 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
503 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
505 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
506 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
508 GstVideoCodecFrame *frame;
509 GstBuffer *buffer = NULL;
512 GST_LOG_OBJECT (decoder, "Allocate output buffer");
514 self->output_flow = GST_FLOW_OK;
516 /* We cannot use the base class allotate helper since it taking the internal
517 * stream lock. we know that the acquire may need to poll until more frames
518 * comes in and holding this lock would prevent that.
520 pool = gst_video_decoder_get_buffer_pool (decoder);
522 /* Pool may be NULL if we started going to READY state */
524 ret = GST_FLOW_FLUSHING;
528 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
529 g_object_unref (pool);
531 if (ret != GST_FLOW_OK)
534 GST_LOG_OBJECT (decoder, "Process output buffer");
535 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
537 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
539 if (ret != GST_FLOW_OK)
542 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
545 frame->output_buffer = buffer;
547 ret = gst_video_decoder_finish_frame (decoder, frame);
549 if (ret != GST_FLOW_OK)
552 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
553 gst_buffer_unref (buffer);
559 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
560 gst_flow_get_name (ret));
562 gst_buffer_replace (&buffer, NULL);
563 self->output_flow = ret;
564 gst_v4l2_object_unlock (self->v4l2output);
565 gst_pad_pause_task (decoder->srcpad);
569 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
570 GstStructure * structure, gpointer user_data)
572 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
573 GstVideoAlignment *align = &self->v4l2capture->align;
574 GstVideoInfo *info = &self->v4l2capture->info;
577 if (!gst_structure_get_int (structure, "width", &width))
580 if (!gst_structure_get_int (structure, "height", &height))
583 if (align->padding_left != 0 || align->padding_top != 0 ||
584 height != info->height + align->padding_bottom)
587 if (height == info->height + align->padding_bottom) {
588 /* Some drivers may round up width to the padded with */
589 if (width == info->width + align->padding_right)
590 gst_structure_set (structure,
591 "width", G_TYPE_INT, width - align->padding_right,
592 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
593 /* Some drivers may keep visible width and only round up bytesperline */
594 else if (width == info->width)
595 gst_structure_set (structure,
596 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
603 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
604 GstVideoCodecFrame * frame)
606 GstV4l2Error error = GST_V4L2_ERROR_INIT;
607 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
608 GstFlowReturn ret = GST_FLOW_OK;
609 gboolean processed = FALSE;
611 GstTaskState task_state;
612 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
613 GstStructure *structure = NULL;
614 const gchar *caps_format = NULL;
615 GstMessage *msg = NULL;
616 GstV4l2BufferPool *capture_pool = NULL;
617 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
619 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
621 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
624 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
625 if (!self->input_state)
627 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
632 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
633 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
635 GstVideoCodecState *output_state;
636 GstBuffer *codec_data;
637 GstCaps *acquired_caps, *available_caps, *caps, *filter;
640 GST_DEBUG_OBJECT (self, "Sending header");
642 codec_data = self->input_state->codec_data;
644 /* We are running in byte-stream mode, so we don't know the headers, but
645 * we need to send something, otherwise the decoder will refuse to
649 gst_buffer_ref (codec_data);
651 codec_data = gst_buffer_ref (frame->input_buffer);
655 /* Ensure input internal pool is active */
656 if (!gst_buffer_pool_is_active (pool)) {
657 GstStructure *config = gst_buffer_pool_get_config (pool);
658 gst_buffer_pool_config_set_params (config, self->input_state->caps,
659 self->v4l2output->info.size, 2, 2);
661 /* There is no reason to refuse this config */
662 if (!gst_buffer_pool_set_config (pool, config))
663 goto activate_failed;
665 if (!gst_buffer_pool_set_active (pool, TRUE))
666 goto activate_failed;
669 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
671 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
672 v4l2output->pool), &codec_data);
673 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
675 gst_buffer_unref (codec_data);
677 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
678 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
679 * and returns the visible size as with/height and the coded size as
681 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
684 /* Create caps from the acquired format, remove the format field */
685 acquired_caps = gst_video_info_to_caps (&info);
686 GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
687 st = gst_caps_get_structure (acquired_caps, 0);
688 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
691 /* Probe currently available pixel formats */
692 available_caps = gst_caps_copy (self->probed_srccaps);
693 GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
695 /* Replace coded size with visible size, we want to negotiate visible size
696 * with downstream, not coded size. */
697 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
699 filter = gst_caps_intersect_full (available_caps, acquired_caps,
700 GST_CAPS_INTERSECT_FIRST);
701 GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
702 gst_caps_unref (acquired_caps);
703 gst_caps_unref (available_caps);
704 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
705 gst_caps_unref (filter);
707 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
708 if (gst_caps_is_empty (caps)) {
709 gst_caps_unref (caps);
713 /* Fixate pixel format */
714 caps = gst_caps_fixate (caps);
716 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
717 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
718 structure = gst_caps_get_structure (caps, 0);
719 caps_format = gst_structure_get_string (structure, "format");
721 if (!strcmp (caps_format, "I420")) {
722 GST_INFO_OBJECT (self, "I420 -> S420");
723 gst_caps_set_simple (caps, "format", G_TYPE_STRING, "S420", NULL);
724 } else if (!strcmp (caps_format, "NV12")) {
725 GST_INFO_OBJECT (self, "NV12 -> SN12");
726 gst_caps_set_simple (caps, "format", G_TYPE_STRING, "SN12", NULL);
728 GST_INFO_OBJECT (self, "Updated decoded caps: %" GST_PTR_FORMAT, caps);
729 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
731 /* Try to set negotiated format, on success replace acquired format */
732 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
733 gst_video_info_from_caps (&info, caps);
735 gst_v4l2_clear_error (&error);
736 gst_caps_unref (caps);
738 output_state = gst_video_decoder_set_output_state (decoder,
739 info.finfo->format, info.width, info.height, self->input_state);
741 /* Copy the rest of the information, there might be more in the future */
742 output_state->info.interlace_mode = info.interlace_mode;
743 gst_video_codec_state_unref (output_state);
745 if (!gst_video_decoder_negotiate (decoder)) {
746 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
752 /* Ensure our internal pool is activated */
753 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
755 goto activate_failed;
756 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
757 capture_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
759 msg = gst_message_new_element (GST_OBJECT_CAST (decoder),
760 gst_structure_new ("prepare-decode-buffers",
761 "num_buffers", G_TYPE_INT, capture_pool->num_allocated,
762 "extra_num_buffers", G_TYPE_INT, capture_pool->num_allocated - 2, NULL));
764 gst_element_post_message (GST_ELEMENT_CAST (decoder), msg);
766 GST_WARNING_OBJECT (self, "output buffer[%d]", capture_pool->num_allocated);
767 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
770 task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
771 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
772 /* It's possible that the processing thread stopped due to an error */
773 if (self->output_flow != GST_FLOW_OK &&
774 self->output_flow != GST_FLOW_FLUSHING) {
775 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
776 ret = self->output_flow;
780 GST_DEBUG_OBJECT (self, "Starting decoding thread");
782 /* Start the processing task, when it quits, the task will disable input
783 * processing to unlock input if draining, or prevent potential block */
784 self->output_flow = GST_FLOW_FLUSHING;
785 if (!gst_pad_start_task (decoder->srcpad,
786 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
787 goto start_task_failed;
791 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
793 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
794 pool), &frame->input_buffer);
795 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
797 if (ret == GST_FLOW_FLUSHING) {
798 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
800 ret = self->output_flow;
802 } else if (ret != GST_FLOW_OK) {
807 /* No need to keep input arround */
808 tmp = frame->input_buffer;
809 frame->input_buffer = gst_buffer_new ();
810 gst_buffer_copy_into (frame->input_buffer, tmp,
811 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
812 GST_BUFFER_COPY_META, 0, 0);
813 gst_buffer_unref (tmp);
815 gst_video_codec_frame_unref (frame);
821 GST_ERROR_OBJECT (self, "not negotiated");
822 ret = GST_FLOW_NOT_NEGOTIATED;
823 gst_v4l2_error (self, &error);
828 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
829 (_("Failed to allocate required memory.")),
830 ("Buffer pool activation failed"));
831 ret = GST_FLOW_ERROR;
836 ret = GST_FLOW_FLUSHING;
842 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
843 (_("Failed to start decoding thread.")), (NULL));
844 ret = GST_FLOW_ERROR;
849 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
850 (_("Failed to process frame.")),
851 ("Maybe be due to not enough memory or failing driver"));
852 ret = GST_FLOW_ERROR;
857 gst_video_decoder_drop_frame (decoder, frame);
863 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
866 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
867 GstClockTime latency;
868 gboolean ret = FALSE;
870 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
871 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
874 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
875 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
876 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
877 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
878 self->v4l2capture->min_buffers, self->v4l2capture->duration);
879 gst_video_decoder_set_latency (decoder, latency, latency);
881 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
888 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
891 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
893 switch (GST_QUERY_TYPE (query)) {
894 case GST_QUERY_CAPS:{
895 GstCaps *filter, *result = NULL;
896 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
898 gst_query_parse_caps (query, &filter);
900 if (self->probed_srccaps)
901 result = gst_caps_ref (self->probed_srccaps);
903 result = gst_pad_get_pad_template_caps (pad);
906 GstCaps *tmp = result;
908 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
909 gst_caps_unref (tmp);
912 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
914 gst_query_set_caps_result (query, result);
915 gst_caps_unref (result);
920 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
928 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
930 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
933 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
936 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
942 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
944 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
946 GstEventType type = GST_EVENT_TYPE (event);
949 case GST_EVENT_FLUSH_START:
950 GST_DEBUG_OBJECT (self, "flush start");
951 gst_v4l2_object_unlock (self->v4l2output);
952 gst_v4l2_object_unlock (self->v4l2capture);
958 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
961 case GST_EVENT_FLUSH_START:
962 /* The processing thread should stop now, wait for it */
963 gst_pad_stop_task (decoder->srcpad);
964 GST_DEBUG_OBJECT (self, "flush start done");
973 static GstStateChangeReturn
974 gst_v4l2_video_dec_change_state (GstElement * element,
975 GstStateChange transition)
977 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
978 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
980 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
981 g_atomic_int_set (&self->active, FALSE);
982 gst_v4l2_object_unlock (self->v4l2output);
983 gst_v4l2_object_unlock (self->v4l2capture);
984 gst_pad_stop_task (decoder->srcpad);
987 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
991 gst_v4l2_video_dec_dispose (GObject * object)
993 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
995 gst_caps_replace (&self->probed_sinkcaps, NULL);
996 gst_caps_replace (&self->probed_srccaps, NULL);
998 G_OBJECT_CLASS (parent_class)->dispose (object);
1002 gst_v4l2_video_dec_finalize (GObject * object)
1004 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
1006 gst_v4l2_object_destroy (self->v4l2capture);
1007 gst_v4l2_object_destroy (self->v4l2output);
1009 G_OBJECT_CLASS (parent_class)->finalize (object);
1013 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
1015 /* V4L2 object are created in subinstance_init */
1019 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
1021 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1022 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
1023 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
1025 gst_video_decoder_set_packetized (decoder, TRUE);
1027 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1028 GST_OBJECT (GST_VIDEO_DECODER_SINK_PAD (self)),
1029 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1030 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1031 self->v4l2output->no_initial_format = TRUE;
1032 self->v4l2output->keep_aspect = FALSE;
1034 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1035 GST_OBJECT (GST_VIDEO_DECODER_SRC_PAD (self)),
1036 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1037 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1038 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
1039 self->v4l2capture->tbm_output = TRUE;
1040 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
1044 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
1046 GstElementClass *element_class;
1047 GObjectClass *gobject_class;
1048 GstVideoDecoderClass *video_decoder_class;
1050 parent_class = g_type_class_peek_parent (klass);
1052 element_class = (GstElementClass *) klass;
1053 gobject_class = (GObjectClass *) klass;
1054 video_decoder_class = (GstVideoDecoderClass *) klass;
1056 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
1057 "V4L2 Video Decoder");
1059 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
1060 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
1061 gobject_class->set_property =
1062 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
1063 gobject_class->get_property =
1064 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
1066 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
1067 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
1068 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
1069 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
1070 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
1071 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
1072 video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_drain);
1073 video_decoder_class->set_format =
1074 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
1075 video_decoder_class->negotiate =
1076 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
1077 video_decoder_class->decide_allocation =
1078 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
1079 /* FIXME propose_allocation or not ? */
1080 video_decoder_class->handle_frame =
1081 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
1082 video_decoder_class->getcaps =
1083 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
1084 video_decoder_class->src_query =
1085 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
1086 video_decoder_class->sink_event =
1087 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
1089 element_class->change_state =
1090 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
1092 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1093 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
1094 g_object_class_install_property (gobject_class, PROP_TBM_OUTPUT,
1095 g_param_spec_boolean ("tbm-output", "Enable TBM for output buffer",
1096 "It works for only DMABUF mode.",
1097 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1098 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
1102 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
1104 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1105 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1106 GstV4l2VideoDecCData *cdata = data;
1108 klass->default_device = cdata->device;
1110 /* Note: gst_pad_template_new() take the floating ref from the caps */
1111 gst_element_class_add_pad_template (element_class,
1112 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1114 gst_element_class_add_pad_template (element_class,
1115 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1118 gst_element_class_set_static_metadata (element_class, cdata->longname,
1119 "Codec/Decoder/Video/Hardware", cdata->description,
1120 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1122 gst_caps_unref (cdata->sink_caps);
1123 gst_caps_unref (cdata->src_caps);
1127 /* Probing functions */
1129 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
1131 gboolean ret = FALSE;
1133 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
1134 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1141 gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
1142 const gchar * basename)
1144 gchar *codec_name = NULL;
1145 gchar *type_name = NULL;
1147 #define SET_META(codec) \
1149 cdata->longname = "V4L2 " codec " Decoder"; \
1150 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1151 codec_name = g_ascii_strdown (codec, -1); \
1154 if (gst_structure_has_name (s, "image/jpeg")) {
1156 } else if (gst_structure_has_name (s, "video/mpeg")) {
1157 gint mpegversion = 0;
1158 gst_structure_get_int (s, "mpegversion", &mpegversion);
1160 if (mpegversion == 2) {
1165 } else if (gst_structure_has_name (s, "video/x-h263")) {
1167 } else if (gst_structure_has_name (s, "video/x-fwht")) {
1169 } else if (gst_structure_has_name (s, "video/x-h264")) {
1171 } else if (gst_structure_has_name (s, "video/x-h265")) {
1173 } else if (gst_structure_has_name (s, "video/x-wmv")) {
1175 } else if (gst_structure_has_name (s, "video/x-vp8")) {
1177 } else if (gst_structure_has_name (s, "video/x-vp9")) {
1179 } else if (gst_structure_has_name (s, "video/x-bayer")) {
1181 } else if (gst_structure_has_name (s, "video/x-sonix")) {
1183 } else if (gst_structure_has_name (s, "video/x-pwc1")) {
1185 } else if (gst_structure_has_name (s, "video/x-pwc2")) {
1188 /* This code should be kept on sync with the exposed CODEC type of format
1189 * from gstv4l2object.c. This warning will only occure in case we forget
1190 * to also add a format here. */
1191 gchar *s_str = gst_structure_to_string (s);
1192 g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
1193 "bug, please report at https://bugs.gnome.org", s_str);
1198 type_name = g_strdup_printf ("v4l2%sdec", codec_name);
1199 if (g_type_from_name (type_name) != 0) {
1201 type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
1204 g_free (codec_name);
1212 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1213 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1217 for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
1218 GstV4l2VideoDecCData *cdata;
1220 GTypeQuery type_query;
1221 GTypeInfo type_info = { 0, };
1222 GType type, subtype;
1225 s = gst_caps_get_structure (sink_caps, i);
1227 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1228 cdata->device = g_strdup (device_path);
1229 cdata->sink_caps = gst_caps_new_empty ();
1230 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
1231 cdata->src_caps = gst_caps_ref (src_caps);
1232 type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
1234 /* Skip over if we hit an unmapped type */
1240 type = gst_v4l2_video_dec_get_type ();
1241 g_type_query (type, &type_query);
1242 memset (&type_info, 0, sizeof (type_info));
1243 type_info.class_size = type_query.class_size;
1244 type_info.instance_size = type_query.instance_size;
1245 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1246 type_info.class_data = cdata;
1247 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1249 subtype = g_type_register_static (type, type_name, &type_info, 0);
1250 #ifdef TIZEN_FEATURE_V4L2VIDEO_ADJ_RANK
1251 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY,
1253 GST_WARNING ("Failed to register plugin '%s'", type_name);
1255 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
1257 GST_WARNING ("Failed to register plugin '%s'", type_name);