2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "gstv4l2videodec.h"
27 #include "v4l2_calls.h"
30 #include <gst/gst-i18n-plugin.h>
32 #define DEFAULT_PROP_DEVICE "/dev/video8"
34 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
35 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
37 #define parent_class gst_v4l2_video_dec_parent_class
38 G_DEFINE_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec, GST_TYPE_VIDEO_DECODER);
40 static gboolean gst_v4l2_video_dec_flush (GstVideoDecoder * decoder);
45 V4L2_STD_OBJECT_PROPS,
50 gst_v4l2_video_dec_set_property (GObject * object,
51 guint prop_id, const GValue * value, GParamSpec * pspec)
53 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
56 /* Split IO mode so output is configure through 'io-mode' and capture
57 * through 'capture-io-mode' */
59 gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
62 case PROP_CAPTURE_IO_MODE:
63 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
68 gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
70 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
74 /* By default, only set on output */
76 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
77 prop_id, value, pspec)) {
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 gst_v4l2_video_dec_get_property (GObject * object,
86 guint prop_id, GValue * value, GParamSpec * pspec)
88 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
92 gst_v4l2_object_get_property_helper (self->v4l2output, prop_id, value,
95 case PROP_CAPTURE_IO_MODE:
96 gst_v4l2_object_get_property_helper (self->v4l2output, PROP_IO_MODE,
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);
115 GST_DEBUG_OBJECT (self, "Opening");
117 if (!gst_v4l2_object_open (self->v4l2output))
120 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
123 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
124 gst_v4l2_object_get_codec_caps ());
126 if (gst_caps_is_empty (self->probed_sinkcaps))
127 goto no_encoded_format;
129 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
130 gst_v4l2_object_get_raw_caps ());
132 if (gst_caps_is_empty (self->probed_srccaps))
138 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
139 (_("Encoder on device %s has no supported input format"),
140 self->v4l2output->videodev), (NULL));
145 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
146 (_("Encoder on device %s has no supported output format"),
147 self->v4l2output->videodev), (NULL));
151 if (GST_V4L2_IS_OPEN (self->v4l2output))
152 gst_v4l2_object_close (self->v4l2output);
154 if (GST_V4L2_IS_OPEN (self->v4l2capture))
155 gst_v4l2_object_close (self->v4l2capture);
157 gst_caps_replace (&self->probed_srccaps, NULL);
158 gst_caps_replace (&self->probed_sinkcaps, NULL);
164 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
166 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
168 GST_DEBUG_OBJECT (self, "Closing");
170 gst_v4l2_object_close (self->v4l2output);
171 gst_v4l2_object_close (self->v4l2capture);
172 gst_caps_replace (&self->probed_srccaps, NULL);
173 gst_caps_replace (&self->probed_sinkcaps, NULL);
179 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
181 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
183 GST_DEBUG_OBJECT (self, "Starting");
185 gst_v4l2_object_unlock (self->v4l2output);
186 g_atomic_int_set (&self->active, TRUE);
187 self->output_flow = GST_FLOW_OK;
193 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
195 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
197 GST_DEBUG_OBJECT (self, "Stopping");
199 /* Should have been flushed already */
200 g_assert (g_atomic_int_get (&self->active) == FALSE);
201 g_assert (g_atomic_int_get (&self->processing) == FALSE);
203 gst_v4l2_object_stop (self->v4l2output);
204 gst_v4l2_object_stop (self->v4l2capture);
206 if (self->input_state) {
207 gst_video_codec_state_unref (self->input_state);
208 self->input_state = NULL;
211 GST_DEBUG_OBJECT (self, "Stopped");
217 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
218 GstVideoCodecState * state)
221 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
223 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
225 if (self->input_state) {
226 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
227 GST_DEBUG_OBJECT (self, "Compatible caps");
230 gst_video_codec_state_unref (self->input_state);
232 /* FIXME we probably need to do more work if pools are active */
235 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps);
238 self->input_state = gst_video_codec_state_ref (state);
245 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
247 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
249 GST_DEBUG_OBJECT (self, "Flushing");
251 /* Wait for capture thread to stop */
252 gst_pad_stop_task (decoder->srcpad);
253 self->output_flow = GST_FLOW_OK;
255 gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2output->pool));
256 gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2capture->pool));
258 /* Output will remain flushing until new frame comes in */
259 gst_v4l2_object_unlock_stop (self->v4l2capture);
265 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
267 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
271 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
273 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
274 GstFlowReturn ret = GST_FLOW_OK;
277 if (!self->input_state)
280 GST_DEBUG_OBJECT (self, "Finishing decoding");
282 /* Keep queuing empty buffers until the processing thread has stopped,
283 * _pool_process() will return FLUSHING when that happened */
284 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
285 while (ret == GST_FLOW_OK) {
286 buffer = gst_buffer_new ();
288 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
289 v4l2output->pool), buffer);
290 gst_buffer_unref (buffer);
292 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
294 g_assert (g_atomic_int_get (&self->processing) == FALSE);
296 if (ret == GST_FLOW_FLUSHING)
297 ret = self->output_flow;
299 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
305 static GstVideoCodecFrame *
306 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
308 GstVideoCodecFrame *frame = NULL;
312 frames = gst_video_decoder_get_frames (decoder);
314 for (l = frames; l != NULL; l = l->next) {
315 GstVideoCodecFrame *f = l->data;
317 if (!frame || frame->pts > f->pts)
324 GST_LOG_OBJECT (decoder,
325 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
326 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
327 gst_video_codec_frame_ref (frame);
330 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
336 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
338 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
340 GstVideoCodecFrame *frame;
341 GstBuffer *buffer = NULL;
344 GST_LOG_OBJECT (decoder, "Allocate output buffer");
346 /* We cannot use the base class allotate helper since it taking the internal
347 * stream lock. we know that the acquire may need to poll until more frames
348 * comes in and holding this lock would prevent that.
350 pool = gst_video_decoder_get_buffer_pool (decoder);
351 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
352 g_object_unref (pool);
354 if (ret != GST_FLOW_OK)
357 /* Check if buffer isn't the last one */
358 if (gst_buffer_get_size (buffer) == 0)
361 GST_LOG_OBJECT (decoder, "Process output buffer");
363 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
364 v4l2capture->pool), buffer);
366 if (ret != GST_FLOW_OK)
369 frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
372 frame->output_buffer = buffer;
374 ret = gst_video_decoder_finish_frame (decoder, frame);
376 if (ret != GST_FLOW_OK)
379 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
380 gst_buffer_unref (buffer);
386 GST_DEBUG_OBJECT (decoder, "Leaving output thread");
388 gst_buffer_replace (&buffer, NULL);
389 self->output_flow = ret;
390 g_atomic_int_set (&self->processing, FALSE);
391 gst_v4l2_object_unlock (self->v4l2output);
392 gst_pad_pause_task (decoder->srcpad);
396 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
397 GstVideoCodecFrame * frame)
399 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
400 GstFlowReturn ret = GST_FLOW_OK;
402 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
404 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
407 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
408 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps))
412 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
414 GstVideoCodecState *output_state;
415 GstBuffer *codec_data;
417 GST_DEBUG_OBJECT (self, "Sending header");
419 codec_data = self->input_state->codec_data;
421 /* We are running in byte-stream mode, so we don't know the headers, but
422 * we need to send something, otherwise the decoder will refuse to
426 gst_buffer_ref (codec_data);
428 codec_data = frame->input_buffer;
429 frame->input_buffer = NULL;
432 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
433 gst_v4l2_object_unlock_stop (self->v4l2output);
435 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
437 gst_v4l2_object_unlock (self->v4l2output);
438 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
440 gst_buffer_unref (codec_data);
442 if (!gst_v4l2_object_setup_format (self->v4l2capture, &info, &self->align))
445 output_state = gst_video_decoder_set_output_state (decoder,
446 info.finfo->format, info.width, info.height, self->input_state);
448 /* Copy the rest of the information, there might be more in the future */
449 output_state->info.interlace_mode = info.interlace_mode;
450 gst_video_codec_state_unref (output_state);
452 if (!gst_video_decoder_negotiate (decoder)) {
453 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
460 if (g_atomic_int_get (&self->processing) == FALSE) {
461 /* It possible that the processing thread stopped due to an error */
462 if (self->output_flow != GST_FLOW_OK) {
463 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
464 ret = self->output_flow;
468 GST_DEBUG_OBJECT (self, "Starting decoding thread");
470 /* Enable processing input */
471 gst_v4l2_object_unlock_stop (self->v4l2output);
473 /* Start the processing task, when it quits, the task will disable input
474 * processing to unlock input if draining, or prevent potential block */
475 g_atomic_int_set (&self->processing, TRUE);
476 gst_pad_start_task (decoder->srcpad,
477 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL);
480 if (frame->input_buffer) {
481 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
483 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
484 pool), frame->input_buffer);
485 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
487 if (ret == GST_FLOW_FLUSHING) {
488 if (g_atomic_int_get (&self->processing) == FALSE)
489 ret = self->output_flow;
492 /* No need to keep input arround */
493 gst_buffer_replace (&frame->input_buffer, NULL);
496 gst_video_codec_frame_unref (frame);
502 GST_ERROR_OBJECT (self, "not negotiated");
503 ret = GST_FLOW_NOT_NEGOTIATED;
508 ret = GST_FLOW_FLUSHING;
513 gst_video_decoder_drop_frame (decoder, frame);
519 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
522 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
523 gboolean ret = FALSE;
525 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
526 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
533 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
536 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
538 switch (GST_QUERY_TYPE (query)) {
539 case GST_QUERY_CAPS:{
540 GstCaps *filter, *result = NULL;
541 gst_query_parse_caps (query, &filter);
543 if (self->probed_srccaps)
544 result = gst_caps_ref (self->probed_srccaps);
546 result = gst_v4l2_object_get_raw_caps ();
549 GstCaps *tmp = result;
551 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
552 gst_caps_unref (tmp);
555 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
557 gst_query_set_caps_result (query, result);
558 gst_caps_unref (result);
563 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
571 gst_v4l2_video_dec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
574 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
576 switch (GST_QUERY_TYPE (query)) {
577 case GST_QUERY_CAPS:{
578 GstCaps *filter, *result = NULL;
579 gst_query_parse_caps (query, &filter);
581 if (self->probed_sinkcaps)
582 result = gst_caps_ref (self->probed_sinkcaps);
584 result = gst_v4l2_object_get_codec_caps ();
587 GstCaps *tmp = result;
589 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
590 gst_caps_unref (tmp);
593 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
595 gst_query_set_caps_result (query, result);
596 gst_caps_unref (result);
601 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_query (decoder, query);
609 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
611 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
613 switch (GST_EVENT_TYPE (event)) {
614 case GST_EVENT_FLUSH_START:
615 gst_v4l2_object_unlock (self->v4l2output);
616 gst_v4l2_object_unlock (self->v4l2capture);
621 return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
624 static GstStateChangeReturn
625 gst_v4l2_video_dec_change_state (GstElement * element,
626 GstStateChange transition)
628 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
630 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
631 g_atomic_int_set (&self->active, FALSE);
632 gst_v4l2_object_unlock (self->v4l2output);
633 gst_v4l2_object_unlock (self->v4l2capture);
636 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
640 gst_v4l2_video_dec_dispose (GObject * object)
642 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
644 gst_caps_replace (&self->probed_sinkcaps, NULL);
645 gst_caps_replace (&self->probed_srccaps, NULL);
647 G_OBJECT_CLASS (parent_class)->dispose (object);
651 gst_v4l2_video_dec_finalize (GObject * object)
653 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
655 gst_v4l2_object_destroy (self->v4l2capture);
656 gst_v4l2_object_destroy (self->v4l2output);
658 G_OBJECT_CLASS (parent_class)->finalize (object);
662 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
664 GstVideoDecoder *decoder = (GstVideoDecoder *) self;
666 gst_video_decoder_set_packetized (decoder, TRUE);
668 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
669 V4L2_BUF_TYPE_VIDEO_OUTPUT, DEFAULT_PROP_DEVICE,
670 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
671 self->v4l2output->no_initial_format = TRUE;
672 self->v4l2output->keep_aspect = FALSE;
674 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
675 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
676 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
677 self->v4l2capture->no_initial_format = TRUE;
678 self->v4l2output->keep_aspect = FALSE;
680 g_object_set (self, "device", DEFAULT_PROP_DEVICE, NULL);
684 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
686 GstElementClass *element_class;
687 GObjectClass *gobject_class;
688 GstVideoDecoderClass *video_decoder_class;
690 element_class = (GstElementClass *) klass;
691 gobject_class = (GObjectClass *) klass;
692 video_decoder_class = (GstVideoDecoderClass *) klass;
694 gst_element_class_add_pad_template (element_class,
695 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
696 gst_v4l2_object_get_codec_caps ()));
698 gst_element_class_add_pad_template (element_class,
699 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
700 gst_v4l2_object_get_raw_caps ()));
702 gst_element_class_set_static_metadata (element_class,
703 "V4L2 Video Decoder",
704 "Codec/Decoder/Video",
705 "Decode video streams via V4L2 API",
706 "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
708 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
709 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
710 gobject_class->set_property =
711 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
712 gobject_class->get_property =
713 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
715 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
716 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
717 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
718 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
719 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
720 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
721 video_decoder_class->set_format =
722 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
723 video_decoder_class->negotiate =
724 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
725 video_decoder_class->decide_allocation =
726 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
727 /* FIXME propose_allocation or not ? */
728 video_decoder_class->handle_frame =
729 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
730 video_decoder_class->sink_query =
731 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_query);
732 video_decoder_class->src_query =
733 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
734 video_decoder_class->sink_event =
735 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
737 element_class->change_state =
738 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
741 gst_v4l2_object_install_properties_helper (gobject_class,
742 DEFAULT_PROP_DEVICE);
745 * GstV4l2VideoDec:capture-io-mode
749 g_object_class_install_property (gobject_class, PROP_IO_MODE,
750 g_param_spec_enum ("capture-io-mode", "Capture IO mode",
752 GST_TYPE_V4L2_IO_MODE, GST_V4L2_IO_AUTO,
753 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
755 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
756 "V4L2 Video Decoder");