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);
63 gst_v4l2_video_dec_set_property (GObject * object,
64 guint prop_id, const GValue * value, GParamSpec * pspec)
66 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
69 case PROP_CAPTURE_IO_MODE:
70 if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
71 prop_id, value, pspec)) {
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
76 /* By default, only set on output */
78 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
79 prop_id, value, pspec)) {
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 gst_v4l2_video_dec_get_property (GObject * object,
88 guint prop_id, GValue * value, GParamSpec * pspec)
90 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
93 case PROP_CAPTURE_IO_MODE:
94 if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
95 prop_id, value, pspec)) {
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
100 /* By default read from output */
102 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
103 prop_id, value, pspec)) {
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
113 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
116 GST_DEBUG_OBJECT (self, "Opening");
118 if (!gst_v4l2_object_open (self->v4l2output))
121 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
124 codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
125 self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
127 gst_caps_unref (codec_caps);
129 if (gst_caps_is_empty (self->probed_sinkcaps))
130 goto no_encoded_format;
132 self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
133 gst_v4l2_object_get_raw_caps ());
135 if (gst_caps_is_empty (self->probed_srccaps))
141 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
142 (_("Encoder on device %s has no supported input format"),
143 self->v4l2output->videodev), (NULL));
148 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
149 (_("Encoder on device %s has no supported output format"),
150 self->v4l2output->videodev), (NULL));
154 if (GST_V4L2_IS_OPEN (self->v4l2output))
155 gst_v4l2_object_close (self->v4l2output);
157 if (GST_V4L2_IS_OPEN (self->v4l2capture))
158 gst_v4l2_object_close (self->v4l2capture);
160 gst_caps_replace (&self->probed_srccaps, NULL);
161 gst_caps_replace (&self->probed_sinkcaps, NULL);
167 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
169 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
171 GST_DEBUG_OBJECT (self, "Closing");
173 gst_v4l2_object_close (self->v4l2output);
174 gst_v4l2_object_close (self->v4l2capture);
175 gst_caps_replace (&self->probed_srccaps, NULL);
176 gst_caps_replace (&self->probed_sinkcaps, NULL);
182 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
184 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
186 GST_DEBUG_OBJECT (self, "Starting");
188 gst_v4l2_object_unlock (self->v4l2output);
189 g_atomic_int_set (&self->active, TRUE);
190 self->output_flow = GST_FLOW_OK;
196 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
198 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
200 GST_DEBUG_OBJECT (self, "Stopping");
202 gst_v4l2_object_unlock (self->v4l2output);
203 gst_v4l2_object_unlock (self->v4l2capture);
205 /* Wait for capture thread to stop */
206 gst_pad_stop_task (decoder->srcpad);
208 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
209 self->output_flow = GST_FLOW_OK;
210 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
212 /* Should have been flushed already */
213 g_assert (g_atomic_int_get (&self->active) == FALSE);
215 gst_v4l2_object_stop (self->v4l2output);
216 gst_v4l2_object_stop (self->v4l2capture);
218 if (self->input_state) {
219 gst_video_codec_state_unref (self->input_state);
220 self->input_state = NULL;
223 GST_DEBUG_OBJECT (self, "Stopped");
229 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
230 GstVideoCodecState * state)
232 GstV4l2Error error = GST_V4L2_ERROR_INIT;
234 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
236 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
238 if (self->input_state) {
239 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
240 GST_DEBUG_OBJECT (self, "Compatible caps");
243 gst_video_codec_state_unref (self->input_state);
244 self->input_state = NULL;
246 gst_v4l2_video_dec_finish (decoder);
247 gst_v4l2_object_stop (self->v4l2output);
249 /* The renegotiation flow don't blend with the base class flow. To
250 * properly stop the capture pool we need to reclaim our buffers, which
251 * will happend through the allocation query. The allocation query is
252 * triggered by gst_video_decoder_negotiate() which requires the output
253 * caps to be set, but we can't know this information as we rely on the
254 * decoder, which requires the capture queue to be stopped.
256 * To workaround this issue, we simply run an allocation query with the
257 * old negotiated caps in order to drain/reclaim our buffers. That breaks
258 * the complexity and should not have much impact in performance since the
259 * following allocation query will happen on a drained pipeline and won't
262 GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
263 GstQuery *query = gst_query_new_allocation (caps, FALSE);
264 gst_pad_peer_query (decoder->srcpad, query);
265 gst_query_unref (query);
266 gst_caps_unref (caps);
269 gst_v4l2_object_stop (self->v4l2capture);
270 self->output_flow = GST_FLOW_OK;
273 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
276 self->input_state = gst_video_codec_state_ref (state);
278 gst_v4l2_error (self, &error);
285 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
287 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
289 GST_DEBUG_OBJECT (self, "Flushed");
291 /* Ensure the processing thread has stopped for the reverse playback
293 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
294 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
296 gst_v4l2_object_unlock (self->v4l2output);
297 gst_v4l2_object_unlock (self->v4l2capture);
298 gst_pad_stop_task (decoder->srcpad);
299 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
302 self->output_flow = GST_FLOW_OK;
304 if (self->v4l2output->pool)
305 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
307 if (self->v4l2capture->pool)
308 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
310 gst_v4l2_object_unlock_stop (self->v4l2output);
311 gst_v4l2_object_unlock_stop (self->v4l2capture);
317 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
319 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
321 /* We don't allow renegotiation without carefull disabling the pool */
322 if (self->v4l2capture->pool &&
323 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
326 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
330 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
332 struct v4l2_decoder_cmd dcmd = { 0, };
334 GST_DEBUG_OBJECT (v4l2object->element,
335 "sending v4l2 decoder command %u with flags %u", cmd, flags);
337 if (!GST_V4L2_IS_OPEN (v4l2object))
342 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
348 if (errno == ENOTTY) {
349 GST_INFO_OBJECT (v4l2object->element,
350 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
351 cmd, flags, v4l2object->videodev, g_strerror (errno));
353 GST_ERROR_OBJECT (v4l2object->element,
354 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
355 cmd, flags, v4l2object->videodev, g_strerror (errno));
361 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
363 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
364 GstFlowReturn ret = GST_FLOW_OK;
367 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
370 GST_DEBUG_OBJECT (self, "Finishing decoding");
372 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
374 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
375 GstTask *task = decoder->srcpad->task;
377 /* If the decoder stop command succeeded, just wait until processing is
379 GST_OBJECT_LOCK (task);
380 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
381 GST_TASK_WAIT (task);
382 GST_OBJECT_UNLOCK (task);
383 ret = GST_FLOW_FLUSHING;
385 /* otherwise keep queuing empty buffers until the processing thread has
386 * stopped, _pool_process() will return FLUSHING when that happened */
387 while (ret == GST_FLOW_OK) {
388 buffer = gst_buffer_new ();
390 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
391 v4l2output->pool), &buffer);
392 gst_buffer_unref (buffer);
396 /* and ensure the processing thread has stopped in case another error
398 gst_v4l2_object_unlock (self->v4l2capture);
399 gst_pad_stop_task (decoder->srcpad);
400 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
402 if (ret == GST_FLOW_FLUSHING)
403 ret = self->output_flow;
405 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
407 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
414 gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
416 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
418 GST_DEBUG_OBJECT (self, "Draining...");
419 gst_v4l2_video_dec_finish (decoder);
420 gst_v4l2_video_dec_flush (decoder);
425 static GstVideoCodecFrame *
426 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
428 GstVideoCodecFrame *frame = NULL;
432 frames = gst_video_decoder_get_frames (decoder);
434 for (l = frames; l != NULL; l = l->next) {
435 GstVideoCodecFrame *f = l->data;
437 if (!frame || frame->pts > f->pts)
444 GST_LOG_OBJECT (decoder,
445 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
446 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
447 gst_video_codec_frame_ref (frame);
450 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
456 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
458 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
459 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
461 GstVideoCodecFrame *frame;
462 GstBuffer *buffer = NULL;
465 GST_LOG_OBJECT (decoder, "Allocate output buffer");
467 self->output_flow = GST_FLOW_OK;
469 /* We cannot use the base class allotate helper since it taking the internal
470 * stream lock. we know that the acquire may need to poll until more frames
471 * comes in and holding this lock would prevent that.
473 pool = gst_video_decoder_get_buffer_pool (decoder);
475 /* Pool may be NULL if we started going to READY state */
477 ret = GST_FLOW_FLUSHING;
481 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
482 g_object_unref (pool);
484 if (ret != GST_FLOW_OK)
487 GST_LOG_OBJECT (decoder, "Process output buffer");
488 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
490 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
492 if (ret != GST_FLOW_OK)
495 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
498 frame->output_buffer = buffer;
500 ret = gst_video_decoder_finish_frame (decoder, frame);
502 if (ret != GST_FLOW_OK)
505 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
506 gst_buffer_unref (buffer);
512 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
513 gst_flow_get_name (ret));
515 gst_buffer_replace (&buffer, NULL);
516 self->output_flow = ret;
517 gst_v4l2_object_unlock (self->v4l2output);
518 gst_pad_pause_task (decoder->srcpad);
522 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
523 GstStructure * structure, gpointer user_data)
525 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
526 GstVideoAlignment *align = &self->v4l2capture->align;
527 GstVideoInfo *info = &self->v4l2capture->info;
530 if (!gst_structure_get_int (structure, "width", &width))
533 if (!gst_structure_get_int (structure, "height", &height))
536 if (align->padding_left != 0 || align->padding_top != 0 ||
537 height != info->height + align->padding_bottom)
540 if (height == info->height + align->padding_bottom) {
541 /* Some drivers may round up width to the padded with */
542 if (width == info->width + align->padding_right)
543 gst_structure_set (structure,
544 "width", G_TYPE_INT, width - align->padding_right,
545 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
546 /* Some drivers may keep visible width and only round up bytesperline */
547 else if (width == info->width)
548 gst_structure_set (structure,
549 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
556 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
557 GstVideoCodecFrame * frame)
559 GstV4l2Error error = GST_V4L2_ERROR_INIT;
560 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
561 GstFlowReturn ret = GST_FLOW_OK;
562 gboolean processed = FALSE;
564 GstTaskState task_state;
566 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
568 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
571 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
572 if (!self->input_state)
574 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
579 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
580 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
582 GstVideoCodecState *output_state;
583 GstBuffer *codec_data;
584 GstCaps *acquired_caps, *available_caps, *caps, *filter;
587 GST_DEBUG_OBJECT (self, "Sending header");
589 codec_data = self->input_state->codec_data;
591 /* We are running in byte-stream mode, so we don't know the headers, but
592 * we need to send something, otherwise the decoder will refuse to
596 gst_buffer_ref (codec_data);
598 codec_data = gst_buffer_ref (frame->input_buffer);
602 /* Ensure input internal pool is active */
603 if (!gst_buffer_pool_is_active (pool)) {
604 GstStructure *config = gst_buffer_pool_get_config (pool);
605 gst_buffer_pool_config_set_params (config, self->input_state->caps,
606 self->v4l2output->info.size, 2, 2);
608 /* There is no reason to refuse this config */
609 if (!gst_buffer_pool_set_config (pool, config))
610 goto activate_failed;
612 if (!gst_buffer_pool_set_active (pool, TRUE))
613 goto activate_failed;
616 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
618 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
619 v4l2output->pool), &codec_data);
620 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
622 gst_buffer_unref (codec_data);
624 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
625 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
626 * and returns the visible size as with/height and the coded size as
628 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
631 /* Create caps from the acquired format, remove the format field */
632 acquired_caps = gst_video_info_to_caps (&info);
633 GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
634 st = gst_caps_get_structure (acquired_caps, 0);
635 gst_structure_remove_field (st, "format");
637 /* Probe currently available pixel formats */
638 available_caps = gst_v4l2_object_probe_caps (self->v4l2capture, NULL);
639 available_caps = gst_caps_make_writable (available_caps);
640 GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
642 /* Replace coded size with visible size, we want to negotiate visible size
643 * with downstream, not coded size. */
644 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
646 filter = gst_caps_intersect_full (available_caps, acquired_caps,
647 GST_CAPS_INTERSECT_FIRST);
648 GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
649 gst_caps_unref (acquired_caps);
650 gst_caps_unref (available_caps);
651 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
652 gst_caps_unref (filter);
654 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
655 if (gst_caps_is_empty (caps)) {
656 gst_caps_unref (caps);
660 /* Fixate pixel format */
661 caps = gst_caps_fixate (caps);
663 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
665 /* Try to set negotiated format, on success replace acquired format */
666 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
667 gst_video_info_from_caps (&info, caps);
669 gst_v4l2_clear_error (&error);
670 gst_caps_unref (caps);
672 output_state = gst_video_decoder_set_output_state (decoder,
673 info.finfo->format, info.width, info.height, self->input_state);
675 /* Copy the rest of the information, there might be more in the future */
676 output_state->info.interlace_mode = info.interlace_mode;
677 gst_video_codec_state_unref (output_state);
679 if (!gst_video_decoder_negotiate (decoder)) {
680 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
686 /* Ensure our internal pool is activated */
687 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
689 goto activate_failed;
692 task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
693 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
694 /* It's possible that the processing thread stopped due to an error */
695 if (self->output_flow != GST_FLOW_OK &&
696 self->output_flow != GST_FLOW_FLUSHING) {
697 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
698 ret = self->output_flow;
702 GST_DEBUG_OBJECT (self, "Starting decoding thread");
704 /* Start the processing task, when it quits, the task will disable input
705 * processing to unlock input if draining, or prevent potential block */
706 self->output_flow = GST_FLOW_FLUSHING;
707 if (!gst_pad_start_task (decoder->srcpad,
708 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
709 goto start_task_failed;
713 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
715 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
716 pool), &frame->input_buffer);
717 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
719 if (ret == GST_FLOW_FLUSHING) {
720 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
722 ret = self->output_flow;
724 } else if (ret != GST_FLOW_OK) {
729 /* No need to keep input arround */
730 tmp = frame->input_buffer;
731 frame->input_buffer = gst_buffer_new ();
732 gst_buffer_copy_into (frame->input_buffer, tmp,
733 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
734 GST_BUFFER_COPY_META, 0, 0);
735 gst_buffer_unref (tmp);
737 gst_video_codec_frame_unref (frame);
743 GST_ERROR_OBJECT (self, "not negotiated");
744 ret = GST_FLOW_NOT_NEGOTIATED;
745 gst_v4l2_error (self, &error);
750 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
751 (_("Failed to allocate required memory.")),
752 ("Buffer pool activation failed"));
753 ret = GST_FLOW_ERROR;
758 ret = GST_FLOW_FLUSHING;
764 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
765 (_("Failed to start decoding thread.")), (NULL));
766 ret = GST_FLOW_ERROR;
771 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
772 (_("Failed to process frame.")),
773 ("Maybe be due to not enough memory or failing driver"));
774 ret = GST_FLOW_ERROR;
779 gst_video_decoder_drop_frame (decoder, frame);
785 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
788 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
789 GstClockTime latency;
790 gboolean ret = FALSE;
792 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
793 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
796 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
797 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
798 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
799 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
800 self->v4l2capture->min_buffers, self->v4l2capture->duration);
801 gst_video_decoder_set_latency (decoder, latency, latency);
803 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
810 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
813 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
815 switch (GST_QUERY_TYPE (query)) {
816 case GST_QUERY_CAPS:{
817 GstCaps *filter, *result = NULL;
818 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
820 gst_query_parse_caps (query, &filter);
822 if (self->probed_srccaps)
823 result = gst_caps_ref (self->probed_srccaps);
825 result = gst_pad_get_pad_template_caps (pad);
828 GstCaps *tmp = result;
830 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
831 gst_caps_unref (tmp);
834 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
836 gst_query_set_caps_result (query, result);
837 gst_caps_unref (result);
842 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
850 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
852 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
855 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
858 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
864 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
866 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
869 switch (GST_EVENT_TYPE (event)) {
870 case GST_EVENT_FLUSH_START:
871 GST_DEBUG_OBJECT (self, "flush start");
872 gst_v4l2_object_unlock (self->v4l2output);
873 gst_v4l2_object_unlock (self->v4l2capture);
879 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
881 switch (GST_EVENT_TYPE (event)) {
882 case GST_EVENT_FLUSH_START:
883 /* The processing thread should stop now, wait for it */
884 gst_pad_stop_task (decoder->srcpad);
885 GST_DEBUG_OBJECT (self, "flush start done");
894 static GstStateChangeReturn
895 gst_v4l2_video_dec_change_state (GstElement * element,
896 GstStateChange transition)
898 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
899 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
901 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
902 g_atomic_int_set (&self->active, FALSE);
903 gst_v4l2_object_unlock (self->v4l2output);
904 gst_v4l2_object_unlock (self->v4l2capture);
905 gst_pad_stop_task (decoder->srcpad);
908 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
912 gst_v4l2_video_dec_dispose (GObject * object)
914 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
916 gst_caps_replace (&self->probed_sinkcaps, NULL);
917 gst_caps_replace (&self->probed_srccaps, NULL);
919 G_OBJECT_CLASS (parent_class)->dispose (object);
923 gst_v4l2_video_dec_finalize (GObject * object)
925 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
927 gst_v4l2_object_destroy (self->v4l2capture);
928 gst_v4l2_object_destroy (self->v4l2output);
930 G_OBJECT_CLASS (parent_class)->finalize (object);
934 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
936 /* V4L2 object are created in subinstance_init */
940 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
942 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
943 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
944 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
946 gst_video_decoder_set_packetized (decoder, TRUE);
948 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
949 GST_OBJECT (GST_VIDEO_DECODER_SINK_PAD (self)),
950 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
951 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
952 self->v4l2output->no_initial_format = TRUE;
953 self->v4l2output->keep_aspect = FALSE;
955 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
956 GST_OBJECT (GST_VIDEO_DECODER_SRC_PAD (self)),
957 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
958 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
959 self->v4l2capture->no_initial_format = TRUE;
960 self->v4l2output->keep_aspect = FALSE;
964 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
966 GstElementClass *element_class;
967 GObjectClass *gobject_class;
968 GstVideoDecoderClass *video_decoder_class;
970 parent_class = g_type_class_peek_parent (klass);
972 element_class = (GstElementClass *) klass;
973 gobject_class = (GObjectClass *) klass;
974 video_decoder_class = (GstVideoDecoderClass *) klass;
976 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
977 "V4L2 Video Decoder");
979 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
980 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
981 gobject_class->set_property =
982 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
983 gobject_class->get_property =
984 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
986 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
987 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
988 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
989 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
990 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
991 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
992 video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_drain);
993 video_decoder_class->set_format =
994 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
995 video_decoder_class->negotiate =
996 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
997 video_decoder_class->decide_allocation =
998 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
999 /* FIXME propose_allocation or not ? */
1000 video_decoder_class->handle_frame =
1001 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
1002 video_decoder_class->getcaps =
1003 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
1004 video_decoder_class->src_query =
1005 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
1006 video_decoder_class->sink_event =
1007 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
1009 element_class->change_state =
1010 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
1012 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1016 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
1018 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1019 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1020 GstV4l2VideoDecCData *cdata = data;
1022 klass->default_device = cdata->device;
1024 /* Note: gst_pad_template_new() take the floating ref from the caps */
1025 gst_element_class_add_pad_template (element_class,
1026 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1028 gst_element_class_add_pad_template (element_class,
1029 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1032 gst_element_class_set_static_metadata (element_class, cdata->longname,
1033 "Codec/Decoder/Video", cdata->description,
1034 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1036 gst_caps_unref (cdata->sink_caps);
1037 gst_caps_unref (cdata->src_caps);
1041 /* Probing functions */
1043 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
1045 gboolean ret = FALSE;
1047 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
1048 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1055 gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
1056 const gchar * basename)
1058 gchar *codec_name = NULL;
1059 gchar *type_name = NULL;
1061 #define SET_META(codec) \
1063 cdata->longname = "V4L2 " codec " Decoder"; \
1064 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1065 codec_name = g_ascii_strdown (codec, -1); \
1068 if (gst_structure_has_name (s, "image/jpeg")) {
1070 } else if (gst_structure_has_name (s, "video/mpeg")) {
1071 gint mpegversion = 0;
1072 gst_structure_get_int (s, "mpegversion", &mpegversion);
1074 if (mpegversion == 2) {
1079 } else if (gst_structure_has_name (s, "video/x-h263")) {
1081 } else if (gst_structure_has_name (s, "video/x-h264")) {
1083 } else if (gst_structure_has_name (s, "video/x-wmv")) {
1085 } else if (gst_structure_has_name (s, "video/x-vp8")) {
1087 } else if (gst_structure_has_name (s, "video/x-vp9")) {
1089 } else if (gst_structure_has_name (s, "video/x-bayer")) {
1091 } else if (gst_structure_has_name (s, "video/x-sonix")) {
1093 } else if (gst_structure_has_name (s, "video/x-pwc1")) {
1095 } else if (gst_structure_has_name (s, "video/x-pwc2")) {
1098 /* This code should be kept on sync with the exposed CODEC type of format
1099 * from gstv4l2object.c. This warning will only occure in case we forget
1100 * to also add a format here. */
1101 gchar *s_str = gst_structure_to_string (s);
1102 g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
1103 "bug, please report at https://bugs.gnome.org", s_str);
1108 type_name = g_strdup_printf ("v4l2%sdec", codec_name);
1109 if (g_type_from_name (type_name) != 0) {
1111 type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
1114 g_free (codec_name);
1122 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1123 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1127 for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
1128 GstV4l2VideoDecCData *cdata;
1130 GTypeQuery type_query;
1131 GTypeInfo type_info = { 0, };
1132 GType type, subtype;
1135 s = gst_caps_get_structure (sink_caps, i);
1137 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1138 cdata->device = g_strdup (device_path);
1139 cdata->sink_caps = gst_caps_new_empty ();
1140 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
1141 cdata->src_caps = gst_caps_ref (src_caps);
1142 type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
1144 /* Skip over if we hit an unmapped type */
1150 type = gst_v4l2_video_dec_get_type ();
1151 g_type_query (type, &type_query);
1152 memset (&type_info, 0, sizeof (type_info));
1153 type_info.class_size = type_query.class_size;
1154 type_info.instance_size = type_query.instance_size;
1155 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1156 type_info.class_data = cdata;
1157 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1159 subtype = g_type_register_static (type, type_name, &type_info, 0);
1160 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
1162 GST_WARNING ("Failed to register plugin '%s'", type_name);