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"
35 #include "gstv4l2h264codec.h"
36 #include "gstv4l2h265codec.h"
37 #include "gstv4l2mpeg2codec.h"
38 #include "gstv4l2mpeg4codec.h"
39 #include "gstv4l2vp8codec.h"
40 #include "gstv4l2vp9codec.h"
43 #include <gst/gst-i18n-plugin.h>
45 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
46 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
53 const gchar *longname;
54 const gchar *description;
55 const GstV4l2Codec *codec;
56 } GstV4l2VideoDecCData;
62 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
64 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
67 #define gst_v4l2_video_dec_parent_class parent_class
68 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
69 GST_TYPE_VIDEO_DECODER);
71 static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder);
73 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
74 static void gst_v4l2_video_dec_flush_buffer_event (GstVideoDecoder * decoder)
79 GST_ERROR("no decoder");
83 ret = gst_pad_push_event (decoder->srcpad,
84 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM | GST_EVENT_TYPE_SERIALIZED,
85 gst_structure_new_empty("tizen/flush-buffer")));
87 GST_WARNING_OBJECT(decoder, "event push ret[%d] for flush-buffer", ret);
89 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
91 gst_v4l2_video_dec_set_property (GObject * object,
92 guint prop_id, const GValue * value, GParamSpec * pspec)
94 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
97 case PROP_CAPTURE_IO_MODE:
98 if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
99 prop_id, value, pspec)) {
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
104 case PROP_TBM_OUTPUT:
105 self->v4l2capture->tbm_output = g_value_get_boolean (value);
106 GST_INFO_OBJECT (self, "tbm output [%d]", self->v4l2capture->tbm_output);
108 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
111 /* By default, only set on output */
113 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
114 prop_id, value, pspec)) {
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
122 gst_v4l2_video_dec_get_property (GObject * object,
123 guint prop_id, GValue * value, GParamSpec * pspec)
125 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
128 case PROP_CAPTURE_IO_MODE:
129 if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
130 prop_id, value, pspec)) {
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
135 case PROP_TBM_OUTPUT:
136 GST_INFO_OBJECT (self, "tbm output [%d]", self->v4l2capture->tbm_output);
137 g_value_set_boolean (value, self->v4l2capture->tbm_output);
139 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
141 /* By default read from output */
143 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
144 prop_id, value, pspec)) {
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
154 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
155 GstV4l2Error error = GST_V4L2_ERROR_INIT;
158 GST_DEBUG_OBJECT (self, "Opening");
160 if (!gst_v4l2_object_open (self->v4l2output, &error))
163 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
166 codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
167 self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
169 gst_caps_unref (codec_caps);
171 if (gst_caps_is_empty (self->probed_sinkcaps))
172 goto no_encoded_format;
177 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
178 (_("Decoder on device %s has no supported input format"),
179 self->v4l2output->videodev), (NULL));
183 if (GST_V4L2_IS_OPEN (self->v4l2output))
184 gst_v4l2_object_close (self->v4l2output);
186 if (GST_V4L2_IS_OPEN (self->v4l2capture))
187 gst_v4l2_object_close (self->v4l2capture);
189 gst_caps_replace (&self->probed_srccaps, NULL);
190 gst_caps_replace (&self->probed_sinkcaps, NULL);
192 gst_v4l2_error (self, &error);
198 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
200 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
202 GST_DEBUG_OBJECT (self, "Closing");
204 gst_v4l2_object_close (self->v4l2output);
205 gst_v4l2_object_close (self->v4l2capture);
206 gst_caps_replace (&self->probed_srccaps, NULL);
207 gst_caps_replace (&self->probed_sinkcaps, NULL);
213 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
215 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
217 GST_DEBUG_OBJECT (self, "Starting");
219 gst_v4l2_object_unlock (self->v4l2output);
220 g_atomic_int_set (&self->active, TRUE);
221 self->output_flow = GST_FLOW_OK;
227 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
229 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
231 GST_DEBUG_OBJECT (self, "Stopping");
233 gst_v4l2_object_unlock (self->v4l2output);
234 gst_v4l2_object_unlock (self->v4l2capture);
236 /* Wait for capture thread to stop */
237 gst_pad_stop_task (decoder->srcpad);
239 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
240 self->output_flow = GST_FLOW_OK;
241 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
243 /* Should have been flushed already */
244 g_assert (g_atomic_int_get (&self->active) == FALSE);
245 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
246 gst_v4l2_video_dec_flush_buffer_event (decoder);
247 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
249 gst_v4l2_object_stop (self->v4l2output);
250 gst_v4l2_object_stop (self->v4l2capture);
252 if (self->input_state) {
253 gst_video_codec_state_unref (self->input_state);
254 self->input_state = NULL;
257 GST_DEBUG_OBJECT (self, "Stopped");
263 compatible_caps (GstV4l2VideoDec * self, GstCaps * new_caps)
265 GstCaps *current_caps, *caps1, *caps2;
269 current_caps = gst_v4l2_object_get_current_caps (self->v4l2output);
273 caps1 = gst_caps_copy (current_caps);
274 s = gst_caps_get_structure (caps1, 0);
275 gst_structure_remove_field (s, "framerate");
277 caps2 = gst_caps_copy (new_caps);
278 s = gst_caps_get_structure (caps2, 0);
279 gst_structure_remove_field (s, "framerate");
281 ret = gst_caps_is_equal (caps1, caps2);
283 gst_caps_unref (caps1);
284 gst_caps_unref (caps2);
285 gst_caps_unref (current_caps);
291 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
292 GstVideoCodecState * state)
294 GstV4l2Error error = GST_V4L2_ERROR_INIT;
296 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
298 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
300 if (self->input_state) {
301 if (compatible_caps (self, state->caps)) {
302 GST_DEBUG_OBJECT (self, "Compatible caps");
305 gst_video_codec_state_unref (self->input_state);
306 self->input_state = NULL;
308 gst_v4l2_video_dec_finish (decoder);
309 gst_v4l2_object_stop (self->v4l2output);
311 /* The renegotiation flow don't blend with the base class flow. To properly
312 * stop the capture pool, if the buffers can't be orphaned, we need to
313 * reclaim our buffers, which will happend through the allocation query.
314 * The allocation query is triggered by gst_video_decoder_negotiate() which
315 * requires the output caps to be set, but we can't know this information
316 * as we rely on the decoder, which requires the capture queue to be
319 * To workaround this issue, we simply run an allocation query with the
320 * old negotiated caps in order to drain/reclaim our buffers. That breaks
321 * the complexity and should not have much impact in performance since the
322 * following allocation query will happen on a drained pipeline and won't
324 if (self->v4l2capture->pool &&
325 !gst_v4l2_buffer_pool_orphan (&self->v4l2capture->pool)) {
326 GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
328 GstQuery *query = gst_query_new_allocation (caps, FALSE);
329 gst_pad_peer_query (decoder->srcpad, query);
330 gst_query_unref (query);
331 gst_caps_unref (caps);
335 gst_v4l2_object_stop (self->v4l2capture);
336 self->output_flow = GST_FLOW_OK;
339 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
341 gst_caps_replace (&self->probed_srccaps, NULL);
342 self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
343 gst_v4l2_object_get_raw_caps ());
345 if (gst_caps_is_empty (self->probed_srccaps))
349 self->input_state = gst_video_codec_state_ref (state);
351 gst_v4l2_error (self, &error);
357 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
358 (_("Decoder on device %s has no supported output format"),
359 self->v4l2output->videodev), (NULL));
360 return GST_FLOW_ERROR;
364 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
366 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
368 GST_DEBUG_OBJECT (self, "Flushed");
370 /* Ensure the processing thread has stopped for the reverse playback
372 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
373 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
375 gst_v4l2_object_unlock (self->v4l2output);
376 gst_v4l2_object_unlock (self->v4l2capture);
377 gst_pad_stop_task (decoder->srcpad);
378 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
381 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
384 self->output_flow = GST_FLOW_OK;
386 gst_v4l2_object_unlock_stop (self->v4l2output);
387 gst_v4l2_object_unlock_stop (self->v4l2capture);
389 if (self->v4l2output->pool)
390 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
392 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
393 gst_v4l2_video_dec_flush_buffer_event (decoder);
394 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
395 /* gst_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
396 * called after gst_v4l2_object_unlock_stop() stopped flushing the buffer
398 if (self->v4l2capture->pool)
399 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
405 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
407 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
409 /* We don't allow renegotiation without careful disabling the pool */
410 if (self->v4l2capture->pool &&
411 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
414 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
418 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
420 struct v4l2_decoder_cmd dcmd = { 0, };
422 GST_DEBUG_OBJECT (v4l2object->element,
423 "sending v4l2 decoder command %u with flags %u", cmd, flags);
425 if (!GST_V4L2_IS_OPEN (v4l2object))
430 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
436 if (errno == ENOTTY) {
437 GST_INFO_OBJECT (v4l2object->element,
438 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
439 cmd, flags, v4l2object->videodev, g_strerror (errno));
441 GST_ERROR_OBJECT (v4l2object->element,
442 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
443 cmd, flags, v4l2object->videodev, g_strerror (errno));
449 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
451 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
452 GstFlowReturn ret = GST_FLOW_OK;
455 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
458 GST_DEBUG_OBJECT (self, "Finishing decoding");
460 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
462 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
465 GST_OBJECT_LOCK (decoder->srcpad);
466 task = GST_PAD_TASK (decoder->srcpad);
468 gst_object_ref (task);
469 GST_OBJECT_UNLOCK (decoder->srcpad);
472 /* If the decoder stop command succeeded, just wait until processing is
474 GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
475 GST_OBJECT_LOCK (task);
476 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
477 GST_TASK_WAIT (task);
478 GST_OBJECT_UNLOCK (task);
480 ret = GST_FLOW_FLUSHING;
481 gst_object_unref (task);
484 /* otherwise keep queuing empty buffers until the processing thread has
485 * stopped, _pool_process() will return FLUSHING when that happened */
486 while (ret == GST_FLOW_OK) {
487 buffer = gst_buffer_new ();
489 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
490 v4l2output->pool), &buffer, NULL);
491 gst_buffer_unref (buffer);
495 /* and ensure the processing thread has stopped in case another error
497 gst_v4l2_object_unlock (self->v4l2capture);
498 gst_pad_stop_task (decoder->srcpad);
499 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
501 if (ret == GST_FLOW_FLUSHING)
502 ret = self->output_flow;
504 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
506 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
513 gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
515 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
517 GST_DEBUG_OBJECT (self, "Draining...");
518 gst_v4l2_video_dec_finish (decoder);
519 gst_v4l2_video_dec_flush (decoder);
525 check_system_frame_number_too_old (guint32 current, guint32 old)
527 guint32 absdiff = current > old ? current - old : old - current;
529 /* More than 100 frames in the past, or current wrapped around */
531 /* Wraparound and difference is actually smaller than 100 */
532 if (absdiff > G_MAXUINT32 - 100)
541 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
543 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
544 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
546 GstVideoCodecFrame *frame;
547 GstBuffer *buffer = NULL;
550 GST_LOG_OBJECT (decoder, "Allocate output buffer");
552 self->output_flow = GST_FLOW_OK;
554 /* We cannot use the base class allotate helper since it taking the internal
555 * stream lock. we know that the acquire may need to poll until more frames
556 * comes in and holding this lock would prevent that.
558 pool = gst_video_decoder_get_buffer_pool (decoder);
560 /* Pool may be NULL if we started going to READY state */
562 ret = GST_FLOW_FLUSHING;
566 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
567 g_object_unref (pool);
569 if (ret != GST_FLOW_OK)
572 GST_LOG_OBJECT (decoder, "Process output buffer");
573 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer, NULL);
574 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
576 if (ret != GST_FLOW_OK)
579 if (GST_BUFFER_TIMESTAMP (buffer) % GST_SECOND != 0)
580 GST_ERROR_OBJECT (decoder,
581 "Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git");
582 GST_LOG_OBJECT (decoder, "Got buffer for frame number %u",
583 (guint32) (GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND));
586 gst_video_decoder_get_frame (decoder,
587 GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND);
589 GstVideoCodecFrame *oldest_frame;
590 gboolean warned = FALSE;
592 /* Garbage collect old frames in case of codec bugs */
593 while ((oldest_frame = gst_video_decoder_get_oldest_frame (decoder)) &&
594 check_system_frame_number_too_old (frame->system_frame_number,
595 oldest_frame->system_frame_number)) {
596 gst_video_decoder_drop_frame (decoder, oldest_frame);
600 g_warning ("%s: Too old frames, bug in decoder -- please file a bug",
601 GST_ELEMENT_NAME (decoder));
606 gst_video_codec_frame_unref (oldest_frame);
608 frame->output_buffer = buffer;
610 ret = gst_video_decoder_finish_frame (decoder, frame);
612 if (ret != GST_FLOW_OK)
615 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
616 gst_buffer_unref (buffer);
622 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
623 gst_flow_get_name (ret));
624 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
625 if (ret == GST_FLOW_EOS)
626 gst_v4l2_video_dec_flush_buffer_event (decoder);
629 gst_buffer_replace (&buffer, NULL);
630 self->output_flow = ret;
631 gst_v4l2_object_unlock (self->v4l2output);
632 gst_pad_pause_task (decoder->srcpad);
636 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
637 GstStructure * structure, gpointer user_data)
639 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
640 GstVideoAlignment *align = &self->v4l2capture->align;
641 GstVideoInfo *info = &self->v4l2capture->info;
644 if (!gst_structure_get_int (structure, "width", &width))
647 if (!gst_structure_get_int (structure, "height", &height))
650 if (align->padding_left != 0 || align->padding_top != 0 ||
651 height != info->height + align->padding_bottom)
654 if (height == info->height + align->padding_bottom) {
655 /* Some drivers may round up width to the padded with */
656 if (width == info->width + align->padding_right)
657 gst_structure_set (structure,
658 "width", G_TYPE_INT, width - align->padding_right,
659 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
660 /* Some drivers may keep visible width and only round up bytesperline */
661 else if (width == info->width)
662 gst_structure_set (structure,
663 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
670 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
671 GstVideoCodecFrame * frame)
673 GstV4l2Error error = GST_V4L2_ERROR_INIT;
674 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
675 GstFlowReturn ret = GST_FLOW_OK;
676 gboolean processed = FALSE;
678 GstTaskState task_state;
679 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
680 GstStructure *structure = NULL;
681 const gchar *caps_format = NULL;
682 GstMessage *msg = NULL;
683 GstV4l2BufferPool *capture_pool = NULL;
684 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
686 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
688 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
691 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
692 if (!self->input_state)
694 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
699 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
700 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
702 GstVideoCodecState *output_state;
703 GstBuffer *codec_data;
704 GstCaps *acquired_caps, *available_caps, *caps, *filter;
706 guint32 dummy_frame_number = 0;
708 GST_DEBUG_OBJECT (self, "Sending header");
710 codec_data = self->input_state->codec_data;
712 /* We are running in byte-stream mode, so we don't know the headers, but
713 * we need to send something, otherwise the decoder will refuse to
717 gst_buffer_ref (codec_data);
719 codec_data = gst_buffer_ref (frame->input_buffer);
723 /* Ensure input internal pool is active */
724 if (!gst_buffer_pool_is_active (pool)) {
725 GstStructure *config = gst_buffer_pool_get_config (pool);
726 guint min = MAX (self->v4l2output->min_buffers,
727 GST_V4L2_MIN_BUFFERS (self->v4l2output));
728 guint max = VIDEO_MAX_FRAME;
730 gst_buffer_pool_config_set_params (config, self->input_state->caps,
731 self->v4l2output->info.size, min, max);
733 /* There is no reason to refuse this config */
734 if (!gst_buffer_pool_set_config (pool, config))
735 goto activate_failed;
737 if (!gst_buffer_pool_set_active (pool, TRUE))
738 goto activate_failed;
741 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
742 GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
743 processed ? frame->system_frame_number : 0);
745 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
746 v4l2output->pool), &codec_data,
747 processed ? &frame->system_frame_number : &dummy_frame_number);
748 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
750 gst_buffer_unref (codec_data);
752 /* init capture fps according to output */
753 self->v4l2capture->info.fps_d = self->v4l2output->info.fps_d;
754 self->v4l2capture->info.fps_n = self->v4l2output->info.fps_n;
756 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
757 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
758 * and returns the visible size as with/height and the coded size as
760 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
763 /* gst_v4l2_object_acquire_format() does not set fps, copy from sink */
764 info.fps_n = self->v4l2output->info.fps_n;
765 info.fps_d = self->v4l2output->info.fps_d;
767 /* Create caps from the acquired format, remove the format field */
768 acquired_caps = gst_video_info_to_caps (&info);
769 GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
770 st = gst_caps_get_structure (acquired_caps, 0);
771 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
774 /* Probe currently available pixel formats */
775 available_caps = gst_caps_copy (self->probed_srccaps);
776 GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
778 /* Replace coded size with visible size, we want to negotiate visible size
779 * with downstream, not coded size. */
780 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
782 filter = gst_caps_intersect_full (available_caps, acquired_caps,
783 GST_CAPS_INTERSECT_FIRST);
784 GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
785 gst_caps_unref (acquired_caps);
786 gst_caps_unref (available_caps);
787 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
788 gst_caps_unref (filter);
790 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
791 if (gst_caps_is_empty (caps)) {
792 gst_caps_unref (caps);
796 /* Fixate pixel format */
797 caps = gst_caps_fixate (caps);
799 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
800 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
801 structure = gst_caps_get_structure (caps, 0);
802 caps_format = gst_structure_get_string (structure, "format");
804 if (!strcmp (caps_format, "I420")) {
805 GST_INFO_OBJECT (self, "I420 -> S420");
806 gst_caps_set_simple (caps, "format", G_TYPE_STRING, "S420", NULL);
807 } else if (!strcmp (caps_format, "NV12")) {
808 GST_INFO_OBJECT (self, "NV12 -> SN12");
809 gst_caps_set_simple (caps, "format", G_TYPE_STRING, "SN12", NULL);
811 GST_INFO_OBJECT (self, "Updated decoded caps: %" GST_PTR_FORMAT, caps);
812 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
814 /* Try to set negotiated format, on success replace acquired format */
815 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
816 gst_video_info_from_caps (&info, caps);
818 gst_v4l2_clear_error (&error);
819 gst_caps_unref (caps);
821 output_state = gst_video_decoder_set_output_state (decoder,
822 info.finfo->format, info.width, info.height, self->input_state);
824 /* Copy the rest of the information, there might be more in the future */
825 output_state->info.interlace_mode = info.interlace_mode;
826 gst_video_codec_state_unref (output_state);
828 if (!gst_video_decoder_negotiate (decoder)) {
829 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
835 /* Ensure our internal pool is activated */
836 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
838 goto activate_failed;
839 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
840 capture_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
842 msg = gst_message_new_element (GST_OBJECT_CAST (decoder),
843 gst_structure_new ("prepare-decode-buffers",
844 "num_buffers", G_TYPE_INT, capture_pool->num_allocated,
845 "extra_num_buffers", G_TYPE_INT, capture_pool->num_allocated - 2, NULL));
847 gst_element_post_message (GST_ELEMENT_CAST (decoder), msg);
849 GST_WARNING_OBJECT (self, "output buffer[%d]", capture_pool->num_allocated);
850 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
853 task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
854 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
855 /* It's possible that the processing thread stopped due to an error */
856 if (self->output_flow != GST_FLOW_OK &&
857 self->output_flow != GST_FLOW_FLUSHING) {
858 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
859 ret = self->output_flow;
863 GST_DEBUG_OBJECT (self, "Starting decoding thread");
865 /* Start the processing task, when it quits, the task will disable input
866 * processing to unlock input if draining, or prevent potential block */
867 self->output_flow = GST_FLOW_FLUSHING;
868 if (!gst_pad_start_task (decoder->srcpad,
869 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
870 goto start_task_failed;
874 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
875 GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
876 frame->system_frame_number);
878 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
879 pool), &frame->input_buffer, &frame->system_frame_number);
880 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
882 if (ret == GST_FLOW_FLUSHING) {
883 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
885 ret = self->output_flow;
887 } else if (ret != GST_FLOW_OK) {
892 /* No need to keep input around */
893 tmp = frame->input_buffer;
894 frame->input_buffer = gst_buffer_new ();
895 gst_buffer_copy_into (frame->input_buffer, tmp,
896 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
897 GST_BUFFER_COPY_META, 0, 0);
898 gst_buffer_unref (tmp);
900 gst_video_codec_frame_unref (frame);
906 GST_ERROR_OBJECT (self, "not negotiated");
907 ret = GST_FLOW_NOT_NEGOTIATED;
908 gst_v4l2_error (self, &error);
913 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
914 (_("Failed to allocate required memory.")),
915 ("Buffer pool activation failed"));
916 ret = GST_FLOW_ERROR;
921 ret = GST_FLOW_FLUSHING;
927 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
928 (_("Failed to start decoding thread.")), (NULL));
929 ret = GST_FLOW_ERROR;
934 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
935 (_("Failed to process frame.")),
936 ("Maybe be due to not enough memory or failing driver"));
937 ret = GST_FLOW_ERROR;
942 gst_video_decoder_drop_frame (decoder, frame);
948 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
951 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
952 GstClockTime latency;
953 gboolean ret = FALSE;
955 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
956 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
959 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
960 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
961 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
962 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
963 self->v4l2capture->min_buffers, self->v4l2capture->duration);
964 gst_video_decoder_set_latency (decoder, latency, latency);
966 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
973 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
976 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
978 switch (GST_QUERY_TYPE (query)) {
979 case GST_QUERY_CAPS:{
980 GstCaps *filter, *result = NULL;
981 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
983 gst_query_parse_caps (query, &filter);
985 if (self->probed_srccaps)
986 result = gst_caps_ref (self->probed_srccaps);
988 result = gst_pad_get_pad_template_caps (pad);
991 GstCaps *tmp = result;
993 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
994 gst_caps_unref (tmp);
997 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
999 gst_query_set_caps_result (query, result);
1000 gst_caps_unref (result);
1005 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
1013 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
1015 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
1018 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
1021 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
1027 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
1029 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
1031 GstEventType type = GST_EVENT_TYPE (event);
1034 case GST_EVENT_FLUSH_START:
1035 GST_DEBUG_OBJECT (self, "flush start");
1036 gst_v4l2_object_unlock (self->v4l2output);
1037 gst_v4l2_object_unlock (self->v4l2capture);
1043 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
1046 case GST_EVENT_FLUSH_START:
1047 /* The processing thread should stop now, wait for it */
1048 gst_pad_stop_task (decoder->srcpad);
1049 GST_DEBUG_OBJECT (self, "flush start done");
1058 static GstStateChangeReturn
1059 gst_v4l2_video_dec_change_state (GstElement * element,
1060 GstStateChange transition)
1062 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
1063 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
1065 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
1066 g_atomic_int_set (&self->active, FALSE);
1067 gst_v4l2_object_unlock (self->v4l2output);
1068 gst_v4l2_object_unlock (self->v4l2capture);
1069 gst_pad_stop_task (decoder->srcpad);
1072 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1076 gst_v4l2_video_dec_dispose (GObject * object)
1078 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
1080 gst_caps_replace (&self->probed_sinkcaps, NULL);
1081 gst_caps_replace (&self->probed_srccaps, NULL);
1083 G_OBJECT_CLASS (parent_class)->dispose (object);
1087 gst_v4l2_video_dec_finalize (GObject * object)
1089 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
1091 gst_v4l2_object_destroy (self->v4l2capture);
1092 gst_v4l2_object_destroy (self->v4l2output);
1094 G_OBJECT_CLASS (parent_class)->finalize (object);
1098 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
1100 /* V4L2 object are created in subinstance_init */
1104 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
1106 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1107 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
1108 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
1110 gst_video_decoder_set_packetized (decoder, TRUE);
1112 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1113 GST_OBJECT (GST_VIDEO_DECODER_SINK_PAD (self)),
1114 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1115 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1116 self->v4l2output->no_initial_format = TRUE;
1117 self->v4l2output->keep_aspect = FALSE;
1119 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1120 GST_OBJECT (GST_VIDEO_DECODER_SRC_PAD (self)),
1121 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1122 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1123 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
1124 self->v4l2capture->tbm_output = TRUE;
1125 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
1129 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
1131 GstElementClass *element_class;
1132 GObjectClass *gobject_class;
1133 GstVideoDecoderClass *video_decoder_class;
1135 parent_class = g_type_class_peek_parent (klass);
1137 element_class = (GstElementClass *) klass;
1138 gobject_class = (GObjectClass *) klass;
1139 video_decoder_class = (GstVideoDecoderClass *) klass;
1141 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
1142 "V4L2 Video Decoder");
1144 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
1145 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
1146 gobject_class->set_property =
1147 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
1148 gobject_class->get_property =
1149 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
1151 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
1152 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
1153 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
1154 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
1155 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
1156 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
1157 video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_drain);
1158 video_decoder_class->set_format =
1159 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
1160 video_decoder_class->negotiate =
1161 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
1162 video_decoder_class->decide_allocation =
1163 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
1164 /* FIXME propose_allocation or not ? */
1165 video_decoder_class->handle_frame =
1166 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
1167 video_decoder_class->getcaps =
1168 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
1169 video_decoder_class->src_query =
1170 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
1171 video_decoder_class->sink_event =
1172 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
1174 element_class->change_state =
1175 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
1177 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1178 #ifdef TIZEN_FEATURE_V4L2_TBM_SUPPORT
1179 g_object_class_install_property (gobject_class, PROP_TBM_OUTPUT,
1180 g_param_spec_boolean ("tbm-output", "Enable TBM for output buffer",
1181 "It works for only DMABUF mode.",
1182 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1183 #endif /* TIZEN_FEATURE_V4L2_TBM_SUPPORT */
1187 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
1189 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1190 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1191 GstV4l2VideoDecCData *cdata = data;
1193 klass->default_device = cdata->device;
1195 gst_element_class_add_pad_template (element_class,
1196 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1198 gst_element_class_add_pad_template (element_class,
1199 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1202 gst_element_class_set_metadata (element_class, cdata->longname,
1203 "Codec/Decoder/Video/Hardware", cdata->description,
1204 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1206 gst_caps_unref (cdata->sink_caps);
1207 gst_caps_unref (cdata->src_caps);
1211 /* Probing functions */
1213 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
1215 gboolean ret = FALSE;
1217 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
1218 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1225 gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
1226 const gchar * basename)
1228 gchar *codec_name = NULL;
1229 gchar *type_name = NULL;
1231 #define SET_META(codec) \
1233 cdata->longname = "V4L2 " codec " Decoder"; \
1234 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1235 codec_name = g_ascii_strdown (codec, -1); \
1238 if (gst_structure_has_name (s, "image/jpeg")) {
1240 } else if (gst_structure_has_name (s, "video/mpeg")) {
1241 gint mpegversion = 0;
1242 gst_structure_get_int (s, "mpegversion", &mpegversion);
1244 if (mpegversion == 2) {
1246 cdata->codec = gst_v4l2_mpeg2_get_codec ();
1249 cdata->codec = gst_v4l2_mpeg4_get_codec ();
1251 } else if (gst_structure_has_name (s, "video/x-h263")) {
1253 } else if (gst_structure_has_name (s, "video/x-fwht")) {
1255 } else if (gst_structure_has_name (s, "video/x-h264")) {
1257 cdata->codec = gst_v4l2_h264_get_codec ();
1258 } else if (gst_structure_has_name (s, "video/x-h265")) {
1260 cdata->codec = gst_v4l2_h265_get_codec ();
1261 } else if (gst_structure_has_name (s, "video/x-wmv")) {
1263 } else if (gst_structure_has_name (s, "video/x-vp8")) {
1265 cdata->codec = gst_v4l2_vp8_get_codec ();
1266 } else if (gst_structure_has_name (s, "video/x-vp9")) {
1268 cdata->codec = gst_v4l2_vp9_get_codec ();
1269 } else if (gst_structure_has_name (s, "video/x-bayer")) {
1271 } else if (gst_structure_has_name (s, "video/x-sonix")) {
1273 } else if (gst_structure_has_name (s, "video/x-pwc1")) {
1275 } else if (gst_structure_has_name (s, "video/x-pwc2")) {
1278 /* This code should be kept on sync with the exposed CODEC type of format
1279 * from gstv4l2object.c. This warning will only occur in case we forget
1280 * to also add a format here. */
1281 gchar *s_str = gst_structure_to_string (s);
1282 g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
1283 "bug, please report at https://bugs.gnome.org", s_str);
1288 type_name = g_strdup_printf ("v4l2%sdec", codec_name);
1289 if (g_type_from_name (type_name) != 0) {
1291 type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
1294 g_free (codec_name);
1302 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1303 const gchar * device_path, gint video_fd, GstCaps * sink_caps,
1308 for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
1309 GstV4l2VideoDecCData *cdata;
1311 GTypeQuery type_query;
1312 GTypeInfo type_info = { 0, };
1313 GType type, subtype;
1316 s = gst_caps_get_structure (sink_caps, i);
1318 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1319 cdata->device = g_strdup (device_path);
1320 cdata->sink_caps = gst_caps_new_empty ();
1321 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
1322 cdata->src_caps = gst_caps_ref (src_caps);
1323 type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
1325 /* Skip over if we hit an unmapped type */
1331 if (cdata->codec != NULL && cdata->codec != gst_v4l2_vp8_get_codec ()
1332 && cdata->codec != gst_v4l2_vp9_get_codec ()) {
1333 GValue value = G_VALUE_INIT;
1335 if (gst_v4l2_codec_probe_levels (cdata->codec, video_fd, &value)) {
1336 gst_caps_set_value (cdata->sink_caps, "level", &value);
1337 g_value_unset (&value);
1340 if (gst_v4l2_codec_probe_profiles (cdata->codec, video_fd, &value)) {
1341 gst_caps_set_value (cdata->sink_caps, "profile", &value);
1342 g_value_unset (&value);
1346 type = gst_v4l2_video_dec_get_type ();
1347 g_type_query (type, &type_query);
1348 memset (&type_info, 0, sizeof (type_info));
1349 type_info.class_size = type_query.class_size;
1350 type_info.instance_size = type_query.instance_size;
1351 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1352 type_info.class_data = cdata;
1353 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1355 subtype = g_type_register_static (type, type_name, &type_info, 0);
1357 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
1359 GST_WARNING ("Failed to register plugin '%s'", type_name);