2013-11-28 22:14:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Collabora Ltd.
|
2017-07-13 16:32:00 +00:00
|
|
|
* Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
|
2013-11-28 22:14:18 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-12-18 08:56:35 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-07-13 00:49:47 +00:00
|
|
|
#include "gstv4l2object.h"
|
2013-11-28 22:14:18 +00:00
|
|
|
#include "gstv4l2videodec.h"
|
|
|
|
|
2019-03-04 11:05:29 +00:00
|
|
|
#include "gstv4l2h264codec.h"
|
|
|
|
#include "gstv4l2h265codec.h"
|
2019-09-27 06:46:22 +00:00
|
|
|
#include "gstv4l2mpeg2codec.h"
|
2019-03-04 11:05:29 +00:00
|
|
|
#include "gstv4l2mpeg4codec.h"
|
|
|
|
#include "gstv4l2vp8codec.h"
|
|
|
|
#include "gstv4l2vp9codec.h"
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
#include <string.h>
|
2022-03-25 13:59:23 +00:00
|
|
|
#include <glib/gi18n-lib.h>
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
|
|
|
|
|
2013-12-18 08:56:35 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gchar *device;
|
|
|
|
GstCaps *sink_caps;
|
|
|
|
GstCaps *src_caps;
|
2017-07-13 16:32:00 +00:00
|
|
|
const gchar *longname;
|
|
|
|
const gchar *description;
|
2019-03-04 11:05:29 +00:00
|
|
|
const GstV4l2Codec *codec;
|
2014-03-16 10:38:07 +00:00
|
|
|
} GstV4l2VideoDecCData;
|
2013-12-18 08:56:35 +00:00
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2014-05-15 22:08:53 +00:00
|
|
|
V4L2_STD_OBJECT_PROPS
|
2013-11-28 22:14:18 +00:00
|
|
|
};
|
|
|
|
|
2014-03-16 10:38:07 +00:00
|
|
|
#define gst_v4l2_video_dec_parent_class parent_class
|
|
|
|
G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
|
|
|
|
GST_TYPE_VIDEO_DECODER);
|
2013-12-18 08:56:35 +00:00
|
|
|
|
2017-12-13 20:23:46 +00:00
|
|
|
static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_video_dec_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_CAPTURE_IO_MODE:
|
2018-01-12 08:53:37 +00:00
|
|
|
if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
|
|
|
|
prop_id, value, pspec)) {
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
2013-11-28 22:14:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* By default, only set on output */
|
|
|
|
default:
|
|
|
|
if (!gst_v4l2_object_set_property_helper (self->v4l2output,
|
|
|
|
prop_id, value, pspec)) {
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_video_dec_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_CAPTURE_IO_MODE:
|
2018-01-12 08:53:37 +00:00
|
|
|
if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
|
|
|
|
prop_id, value, pspec)) {
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
2013-11-28 22:14:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* By default read from output */
|
|
|
|
default:
|
|
|
|
if (!gst_v4l2_object_get_property_helper (self->v4l2output,
|
|
|
|
prop_id, value, pspec)) {
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
2020-08-06 11:15:10 +00:00
|
|
|
GstV4l2Error error = GST_V4L2_ERROR_INIT;
|
2017-07-14 15:54:57 +00:00
|
|
|
GstCaps *codec_caps;
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Opening");
|
|
|
|
|
2020-08-06 11:15:10 +00:00
|
|
|
if (!gst_v4l2_object_open (self->v4l2output, &error))
|
2013-11-28 22:14:18 +00:00
|
|
|
goto failure;
|
|
|
|
|
|
|
|
if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
|
|
|
|
goto failure;
|
|
|
|
|
2017-07-14 15:54:57 +00:00
|
|
|
codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
|
|
|
|
self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
|
|
|
|
codec_caps);
|
|
|
|
gst_caps_unref (codec_caps);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
if (gst_caps_is_empty (self->probed_sinkcaps))
|
|
|
|
goto no_encoded_format;
|
|
|
|
|
2023-04-14 22:54:22 +00:00
|
|
|
self->supports_source_change =
|
|
|
|
gst_v4l2_object_subscribe_event (self->v4l2capture,
|
|
|
|
V4L2_EVENT_SOURCE_CHANGE);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
no_encoded_format:
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
|
2018-05-12 06:03:28 +00:00
|
|
|
(_("Decoder on device %s has no supported input format"),
|
2013-11-28 22:14:18 +00:00
|
|
|
self->v4l2output->videodev), (NULL));
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
if (GST_V4L2_IS_OPEN (self->v4l2output))
|
|
|
|
gst_v4l2_object_close (self->v4l2output);
|
|
|
|
|
|
|
|
if (GST_V4L2_IS_OPEN (self->v4l2capture))
|
|
|
|
gst_v4l2_object_close (self->v4l2capture);
|
|
|
|
|
|
|
|
gst_caps_replace (&self->probed_srccaps, NULL);
|
|
|
|
gst_caps_replace (&self->probed_sinkcaps, NULL);
|
|
|
|
|
2020-08-06 11:15:10 +00:00
|
|
|
gst_v4l2_error (self, &error);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Closing");
|
|
|
|
|
|
|
|
gst_v4l2_object_close (self->v4l2output);
|
|
|
|
gst_v4l2_object_close (self->v4l2capture);
|
|
|
|
gst_caps_replace (&self->probed_srccaps, NULL);
|
|
|
|
gst_caps_replace (&self->probed_sinkcaps, NULL);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Starting");
|
|
|
|
|
|
|
|
gst_v4l2_object_unlock (self->v4l2output);
|
|
|
|
g_atomic_int_set (&self->active, TRUE);
|
|
|
|
self->output_flow = GST_FLOW_OK;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Stopping");
|
|
|
|
|
2014-03-12 16:11:16 +00:00
|
|
|
gst_v4l2_object_unlock (self->v4l2output);
|
|
|
|
gst_v4l2_object_unlock (self->v4l2capture);
|
|
|
|
|
|
|
|
/* Wait for capture thread to stop */
|
|
|
|
gst_pad_stop_task (decoder->srcpad);
|
2014-05-15 14:31:40 +00:00
|
|
|
|
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
2014-03-12 16:11:16 +00:00
|
|
|
self->output_flow = GST_FLOW_OK;
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
/* Should have been flushed already */
|
|
|
|
g_assert (g_atomic_int_get (&self->active) == FALSE);
|
|
|
|
|
|
|
|
gst_v4l2_object_stop (self->v4l2output);
|
|
|
|
gst_v4l2_object_stop (self->v4l2capture);
|
|
|
|
|
|
|
|
if (self->input_state) {
|
|
|
|
gst_video_codec_state_unref (self->input_state);
|
|
|
|
self->input_state = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Stopped");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:27:35 +00:00
|
|
|
static gboolean
|
|
|
|
compatible_caps (GstV4l2VideoDec * self, GstCaps * new_caps)
|
|
|
|
{
|
|
|
|
GstCaps *current_caps, *caps1, *caps2;
|
|
|
|
GstStructure *s;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
current_caps = gst_v4l2_object_get_current_caps (self->v4l2output);
|
|
|
|
if (!current_caps)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
caps1 = gst_caps_copy (current_caps);
|
|
|
|
s = gst_caps_get_structure (caps1, 0);
|
|
|
|
gst_structure_remove_field (s, "framerate");
|
|
|
|
|
|
|
|
caps2 = gst_caps_copy (new_caps);
|
|
|
|
s = gst_caps_get_structure (caps2, 0);
|
|
|
|
gst_structure_remove_field (s, "framerate");
|
|
|
|
|
|
|
|
ret = gst_caps_is_equal (caps1, caps2);
|
|
|
|
|
|
|
|
gst_caps_unref (caps1);
|
|
|
|
gst_caps_unref (caps2);
|
|
|
|
gst_caps_unref (current_caps);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecState * state)
|
|
|
|
{
|
2023-04-19 18:19:13 +00:00
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
2016-06-07 20:04:52 +00:00
|
|
|
GstV4l2Error error = GST_V4L2_ERROR_INIT;
|
2013-11-28 22:14:18 +00:00
|
|
|
gboolean ret = TRUE;
|
2023-04-19 18:19:13 +00:00
|
|
|
gboolean dyn_resolution = self->v4l2output->fmtdesc &&
|
|
|
|
(self->v4l2output->fmtdesc->flags & V4L2_FMT_FLAG_DYN_RESOLUTION);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
|
|
|
|
|
2023-04-19 18:19:13 +00:00
|
|
|
if (self->input_state && !dyn_resolution) {
|
2020-05-26 14:27:35 +00:00
|
|
|
if (compatible_caps (self, state->caps)) {
|
2013-11-28 22:14:18 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Compatible caps");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
gst_video_codec_state_unref (self->input_state);
|
2014-03-12 16:56:18 +00:00
|
|
|
self->input_state = NULL;
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2017-12-13 20:23:46 +00:00
|
|
|
gst_v4l2_video_dec_finish (decoder);
|
|
|
|
gst_v4l2_object_stop (self->v4l2output);
|
|
|
|
|
2019-01-24 15:12:13 +00:00
|
|
|
/* The renegotiation flow don't blend with the base class flow. To properly
|
|
|
|
* stop the capture pool, if the buffers can't be orphaned, we need to
|
|
|
|
* reclaim our buffers, which will happend through the allocation query.
|
|
|
|
* The allocation query is triggered by gst_video_decoder_negotiate() which
|
|
|
|
* requires the output caps to be set, but we can't know this information
|
|
|
|
* as we rely on the decoder, which requires the capture queue to be
|
|
|
|
* stopped.
|
2017-12-13 20:23:46 +00:00
|
|
|
*
|
|
|
|
* To workaround this issue, we simply run an allocation query with the
|
|
|
|
* old negotiated caps in order to drain/reclaim our buffers. That breaks
|
|
|
|
* the complexity and should not have much impact in performance since the
|
|
|
|
* following allocation query will happen on a drained pipeline and won't
|
|
|
|
* block. */
|
2022-11-28 22:26:50 +00:00
|
|
|
if (!gst_v4l2_buffer_pool_orphan (self->v4l2capture)) {
|
2017-12-13 20:23:46 +00:00
|
|
|
GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
|
2018-07-09 17:59:02 +00:00
|
|
|
if (caps) {
|
|
|
|
GstQuery *query = gst_query_new_allocation (caps, FALSE);
|
|
|
|
gst_pad_peer_query (decoder->srcpad, query);
|
|
|
|
gst_query_unref (query);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
2017-12-13 20:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_v4l2_object_stop (self->v4l2capture);
|
|
|
|
self->output_flow = GST_FLOW_OK;
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 18:19:13 +00:00
|
|
|
/* No V4L2_FMT_FLAG_DYN_RESOLUTION or no fmtdesc set yet */
|
|
|
|
if (!dyn_resolution)
|
|
|
|
ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
if (ret)
|
|
|
|
self->input_state = gst_video_codec_state_ref (state);
|
2016-06-07 20:04:52 +00:00
|
|
|
else
|
|
|
|
gst_v4l2_error (self, &error);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Flushed");
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
/* Ensure the processing thread has stopped for the reverse playback
|
|
|
|
* discount case */
|
2017-02-17 13:01:08 +00:00
|
|
|
if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
|
2014-05-25 00:20:07 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_v4l2_object_unlock (self->v4l2output);
|
|
|
|
gst_v4l2_object_unlock (self->v4l2capture);
|
|
|
|
gst_pad_stop_task (decoder->srcpad);
|
2015-01-06 18:54:25 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
2014-05-25 00:20:07 +00:00
|
|
|
}
|
2014-03-16 16:01:10 +00:00
|
|
|
|
2019-10-11 12:27:12 +00:00
|
|
|
if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
|
|
|
|
return TRUE;
|
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
self->output_flow = GST_FLOW_OK;
|
2014-05-25 00:00:14 +00:00
|
|
|
|
2018-06-26 13:04:39 +00:00
|
|
|
gst_v4l2_object_unlock_stop (self->v4l2output);
|
|
|
|
gst_v4l2_object_unlock_stop (self->v4l2capture);
|
|
|
|
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_v4l2_buffer_pool_flush (self->v4l2output);
|
2017-12-13 20:23:46 +00:00
|
|
|
|
2018-06-26 13:04:39 +00:00
|
|
|
/* gst_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
|
|
|
|
* called after gst_v4l2_object_unlock_stop() stopped flushing the buffer
|
2023-04-26 19:58:23 +00:00
|
|
|
* pool. If the resolution has changed before we stopped the driver we must
|
|
|
|
* reallocate the capture pool. We simply discard the pool, and let the
|
|
|
|
* capture thread handle re-allocation.*/
|
|
|
|
if (gst_v4l2_buffer_pool_flush (self->v4l2capture) ==
|
|
|
|
GST_V4L2_FLOW_RESOLUTION_CHANGE || self->draining)
|
|
|
|
gst_v4l2_object_stop (self->v4l2capture);
|
2017-12-13 20:23:46 +00:00
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-06-26 21:08:57 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_remove_padding (GstCapsFeatures * features,
|
|
|
|
GstStructure * structure, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
|
|
|
|
GstVideoAlignment *align = &self->v4l2capture->align;
|
|
|
|
GstVideoInfo *info = &self->v4l2capture->info;
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, "width", &width))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, "height", &height))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (align->padding_left != 0 || align->padding_top != 0 ||
|
|
|
|
height != info->height + align->padding_bottom)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (height == info->height + align->padding_bottom) {
|
|
|
|
/* Some drivers may round up width to the padded with */
|
|
|
|
if (width == info->width + align->padding_right)
|
|
|
|
gst_structure_set (structure,
|
|
|
|
"width", G_TYPE_INT, width - align->padding_right,
|
|
|
|
"height", G_TYPE_INT, height - align->padding_bottom, NULL);
|
|
|
|
/* Some drivers may keep visible width and only round up bytesperline */
|
|
|
|
else if (width == info->width)
|
|
|
|
gst_structure_set (structure,
|
|
|
|
"height", G_TYPE_INT, height - align->padding_bottom, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
|
|
|
|
{
|
2015-01-09 21:59:53 +00:00
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
2023-06-26 21:08:57 +00:00
|
|
|
GstV4l2Error error = GST_V4L2_ERROR_INIT;
|
|
|
|
GstVideoInfo info;
|
|
|
|
GstVideoCodecState *output_state;
|
|
|
|
GstCaps *acquired_caps, *fixation_caps, *available_caps, *caps, *filter;
|
|
|
|
GstStructure *st;
|
|
|
|
gboolean active;
|
|
|
|
GstBufferPool *cpool;
|
|
|
|
gboolean ret;
|
2015-01-09 21:59:53 +00:00
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
/* We don't allow renegotiation without careful disabling the pool */
|
2023-06-26 21:08:57 +00:00
|
|
|
cpool = gst_v4l2_object_get_buffer_pool (self->v4l2capture);
|
|
|
|
if (cpool) {
|
|
|
|
gboolean is_active = gst_buffer_pool_is_active (cpool);
|
|
|
|
gst_object_unref (cpool);
|
|
|
|
if (is_active)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init capture fps according to output */
|
|
|
|
self->v4l2capture->info.fps_d = self->v4l2output->info.fps_d;
|
|
|
|
self->v4l2capture->info.fps_n = self->v4l2output->info.fps_n;
|
|
|
|
|
|
|
|
/* For decoders G_FMT returns coded size, G_SELECTION returns visible size
|
|
|
|
* in the compose rectangle. gst_v4l2_object_acquire_format() checks both
|
|
|
|
* and returns the visible size as with/height and the coded size as
|
|
|
|
* padding. */
|
|
|
|
if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
|
|
|
|
goto not_negotiated;
|
|
|
|
|
|
|
|
/* gst_v4l2_object_acquire_format() does not set fps, copy from sink */
|
|
|
|
info.fps_n = self->v4l2output->info.fps_n;
|
|
|
|
info.fps_d = self->v4l2output->info.fps_d;
|
|
|
|
|
|
|
|
gst_caps_replace (&self->probed_srccaps, NULL);
|
|
|
|
self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
|
|
|
|
gst_v4l2_object_get_raw_caps ());
|
|
|
|
/* Create caps from the acquired format, remove the format field */
|
|
|
|
acquired_caps = gst_video_info_to_caps (&info);
|
|
|
|
GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
|
|
|
|
fixation_caps = gst_caps_copy (acquired_caps);
|
|
|
|
st = gst_caps_get_structure (fixation_caps, 0);
|
|
|
|
gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Probe currently available pixel formats */
|
|
|
|
available_caps = gst_caps_copy (self->probed_srccaps);
|
|
|
|
GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
|
|
|
|
|
|
|
|
/* Replace coded size with visible size, we want to negotiate visible size
|
|
|
|
* with downstream, not coded size. */
|
|
|
|
gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
|
|
|
|
|
|
|
|
filter = gst_caps_intersect_full (available_caps, fixation_caps,
|
|
|
|
GST_CAPS_INTERSECT_FIRST);
|
|
|
|
GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
|
|
|
|
gst_caps_unref (fixation_caps);
|
|
|
|
gst_caps_unref (available_caps);
|
|
|
|
caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
|
|
|
|
gst_caps_unref (filter);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
|
|
|
|
if (gst_caps_is_empty (caps)) {
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
goto not_negotiated;
|
2022-11-28 22:26:50 +00:00
|
|
|
}
|
2015-01-09 21:59:53 +00:00
|
|
|
|
2023-06-26 21:08:57 +00:00
|
|
|
/* Prefer the acquired caps over anything suggested downstream, this ensure
|
|
|
|
* that we preserves the bit depth, as we don't have any fancy fixation
|
|
|
|
* process */
|
|
|
|
if (gst_caps_is_subset (acquired_caps, caps)) {
|
|
|
|
gst_caps_unref (acquired_caps);
|
|
|
|
goto use_acquired_caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fixate pixel format */
|
|
|
|
caps = gst_caps_fixate (caps);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
/* Try to set negotiated format, on success replace acquired format */
|
|
|
|
if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
|
|
|
|
gst_video_info_from_caps (&info, caps);
|
|
|
|
else
|
|
|
|
gst_v4l2_clear_error (&error);
|
|
|
|
|
|
|
|
use_acquired_caps:
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
/* catch possible bogus driver that don't enumerate the format it actually
|
|
|
|
* returned from G_FMT */
|
|
|
|
if (!self->v4l2capture->fmtdesc)
|
|
|
|
goto not_negotiated;
|
|
|
|
|
|
|
|
output_state = gst_video_decoder_set_output_state (decoder,
|
|
|
|
info.finfo->format, info.width, info.height, self->input_state);
|
|
|
|
|
|
|
|
/* Copy the rest of the information, there might be more in the future */
|
|
|
|
output_state->info.interlace_mode = info.interlace_mode;
|
|
|
|
output_state->info.colorimetry = info.colorimetry;
|
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
|
|
|
|
ret = GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
|
|
|
|
if (!ret)
|
|
|
|
goto not_negotiated;
|
|
|
|
|
|
|
|
/* The pool may be created through gst_video_decoder_negotiate(), so must
|
|
|
|
* be kept after */
|
|
|
|
cpool = gst_v4l2_object_get_buffer_pool (self->v4l2capture);
|
|
|
|
gst_v4l2_buffer_pool_enable_resolution_change (GST_V4L2_BUFFER_POOL (cpool));
|
|
|
|
|
|
|
|
/* Ensure our internal pool is activated */
|
|
|
|
active = gst_buffer_pool_set_active (cpool, TRUE);
|
|
|
|
if (cpool)
|
|
|
|
gst_object_unref (cpool);
|
|
|
|
if (!active)
|
|
|
|
goto activate_failed;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
not_negotiated:
|
|
|
|
GST_ERROR_OBJECT (self, "not negotiated");
|
|
|
|
gst_v4l2_error (self, &error);
|
|
|
|
gst_v4l2_object_stop (self->v4l2capture);
|
|
|
|
return FALSE;
|
|
|
|
activate_failed:
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
|
|
|
|
(_("Failed to allocate required memory.")),
|
|
|
|
("Buffer pool activation failed"));
|
|
|
|
gst_v4l2_object_stop (self->v4l2capture);
|
|
|
|
return FALSE;
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 13:31:36 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
|
|
|
|
{
|
|
|
|
struct v4l2_decoder_cmd dcmd = { 0, };
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (v4l2object->element,
|
|
|
|
"sending v4l2 decoder command %u with flags %u", cmd, flags);
|
|
|
|
|
|
|
|
if (!GST_V4L2_IS_OPEN (v4l2object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dcmd.cmd = cmd;
|
|
|
|
dcmd.flags = flags;
|
2017-07-24 18:19:02 +00:00
|
|
|
if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
|
2014-12-12 13:31:36 +00:00
|
|
|
goto dcmd_failed;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
dcmd_failed:
|
|
|
|
if (errno == ENOTTY) {
|
|
|
|
GST_INFO_OBJECT (v4l2object->element,
|
|
|
|
"Failed to send decoder command %u with flags %u for '%s'. (%s)",
|
|
|
|
cmd, flags, v4l2object->videodev, g_strerror (errno));
|
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (v4l2object->element,
|
|
|
|
"Failed to send decoder command %u with flags %u for '%s'. (%s)",
|
|
|
|
cmd, flags, v4l2object->videodev, g_strerror (errno));
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstBuffer *buffer;
|
2022-05-28 19:04:10 +00:00
|
|
|
GList *pending_frames = NULL;
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2017-02-17 13:01:08 +00:00
|
|
|
if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
|
2013-11-28 22:14:18 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Finishing decoding");
|
|
|
|
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
2015-09-02 09:48:29 +00:00
|
|
|
|
2023-04-19 18:19:13 +00:00
|
|
|
/* If we are in the middle of a source change, cancel it */
|
|
|
|
self->draining = FALSE;
|
|
|
|
|
2015-09-02 09:48:29 +00:00
|
|
|
if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
|
2022-01-17 16:44:47 +00:00
|
|
|
GstTask *task;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (decoder->srcpad);
|
|
|
|
task = GST_PAD_TASK (decoder->srcpad);
|
|
|
|
if (task)
|
|
|
|
gst_object_ref (task);
|
|
|
|
GST_OBJECT_UNLOCK (decoder->srcpad);
|
|
|
|
|
|
|
|
if (task) {
|
|
|
|
/* If the decoder stop command succeeded, just wait until processing is
|
|
|
|
* finished */
|
|
|
|
GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
|
|
|
|
GST_OBJECT_LOCK (task);
|
|
|
|
while (GST_TASK_STATE (task) == GST_TASK_STARTED)
|
|
|
|
GST_TASK_WAIT (task);
|
|
|
|
GST_OBJECT_UNLOCK (task);
|
|
|
|
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
gst_object_unref (task);
|
|
|
|
}
|
2015-09-02 09:48:29 +00:00
|
|
|
} else {
|
2022-11-28 22:26:50 +00:00
|
|
|
GstBufferPool *opool = gst_v4l2_object_get_buffer_pool (self->v4l2output);
|
2015-09-02 09:48:29 +00:00
|
|
|
/* otherwise keep queuing empty buffers until the processing thread has
|
|
|
|
* stopped, _pool_process() will return FLUSHING when that happened */
|
|
|
|
while (ret == GST_FLOW_OK) {
|
|
|
|
buffer = gst_buffer_new ();
|
|
|
|
ret =
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (opool), &buffer,
|
|
|
|
NULL);
|
2015-09-02 09:48:29 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
}
|
2022-11-28 22:26:50 +00:00
|
|
|
if (opool)
|
|
|
|
gst_object_unref (opool);
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
/* and ensure the processing thread has stopped in case another error
|
2019-09-02 12:27:35 +00:00
|
|
|
* occurred. */
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_v4l2_object_unlock (self->v4l2capture);
|
|
|
|
gst_pad_stop_task (decoder->srcpad);
|
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
2014-05-25 00:00:14 +00:00
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
if (ret == GST_FLOW_FLUSHING)
|
|
|
|
ret = self->output_flow;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (decoder, "Done draining buffers");
|
|
|
|
|
2023-03-31 14:31:07 +00:00
|
|
|
/* Draining of the capture buffer has completed.
|
2022-05-28 19:04:10 +00:00
|
|
|
* If any pending frames remain at this point there is a decoder error.
|
2023-03-31 14:31:07 +00:00
|
|
|
* This has been observed as a driver bug, where eos is sent too early.
|
2022-05-28 19:04:10 +00:00
|
|
|
* These frames will never be rendered, so drop them now with a warning */
|
|
|
|
|
|
|
|
pending_frames = gst_video_decoder_get_frames (decoder);
|
|
|
|
if (pending_frames) {
|
|
|
|
int counter = 0;
|
|
|
|
guint32 first, last;
|
|
|
|
for (GList * g = pending_frames; g; g = g->next) {
|
|
|
|
GstVideoCodecFrame *frame = g->data;
|
|
|
|
g->data = NULL;
|
|
|
|
last = frame->system_frame_number;
|
|
|
|
if (!counter)
|
|
|
|
first = last;
|
|
|
|
counter++;
|
|
|
|
gst_video_decoder_drop_frame (decoder, frame);
|
|
|
|
}
|
2023-04-17 20:07:21 +00:00
|
|
|
if (self->output_flow == GST_FLOW_OK) {
|
|
|
|
g_warning ("%s: %i frames %u-%u left undrained after CMD_STOP, "
|
|
|
|
"eos sent too early: bug in decoder -- please file a bug",
|
|
|
|
GST_ELEMENT_NAME (decoder), counter, first, last);
|
|
|
|
}
|
2022-05-28 19:04:10 +00:00
|
|
|
if (pending_frames)
|
|
|
|
g_list_free (pending_frames);
|
|
|
|
}
|
2017-12-13 20:23:46 +00:00
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-13 21:55:29 +00:00
|
|
|
static GstFlowReturn
|
2017-12-13 20:23:46 +00:00
|
|
|
gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Draining...");
|
|
|
|
gst_v4l2_video_dec_finish (decoder);
|
|
|
|
gst_v4l2_video_dec_flush (decoder);
|
|
|
|
|
2018-06-13 21:55:29 +00:00
|
|
|
return GST_FLOW_OK;
|
2017-12-13 20:23:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:23:25 +00:00
|
|
|
static gboolean
|
|
|
|
check_system_frame_number_too_old (guint32 current, guint32 old)
|
|
|
|
{
|
|
|
|
guint32 absdiff = current > old ? current - old : old - current;
|
|
|
|
|
|
|
|
/* More than 100 frames in the past, or current wrapped around */
|
|
|
|
if (absdiff > 100) {
|
|
|
|
/* Wraparound and difference is actually smaller than 100 */
|
|
|
|
if (absdiff > G_MAXUINT32 - 100)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-04-14 22:54:22 +00:00
|
|
|
/* Only used initially to wait for a SRC_CH event
|
|
|
|
* called with decoder stream lock */
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_video_dec_wait_for_src_ch (GstV4l2VideoDec * self)
|
|
|
|
{
|
|
|
|
GstFlowReturn flowret;
|
|
|
|
|
|
|
|
if (!self->wait_for_source_change)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
2023-04-17 20:10:34 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Waiting for source change event");
|
|
|
|
|
2023-04-14 22:54:22 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (GST_VIDEO_DECODER (self));
|
|
|
|
flowret = gst_v4l2_object_poll (self->v4l2capture, GST_CLOCK_TIME_NONE);
|
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (GST_VIDEO_DECODER (self));
|
|
|
|
|
|
|
|
/* Fix the flow return value, as the poll is watching for buffer, but we are
|
|
|
|
* looking for the source change event */
|
|
|
|
if (flowret == GST_V4L2_FLOW_RESOLUTION_CHANGE) {
|
|
|
|
self->wait_for_source_change = FALSE;
|
|
|
|
flowret = GST_FLOW_OK;
|
|
|
|
} else if (flowret == GST_FLOW_OK) {
|
|
|
|
/* A buffer would be unexpected, in this case just terminate */
|
|
|
|
flowret = GST_V4L2_FLOW_LAST_BUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return flowret;
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
GstBufferPool *pool;
|
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
GstBuffer *buffer = NULL;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
2023-04-17 20:10:34 +00:00
|
|
|
GST_LOG_OBJECT (self, "Looping.");
|
|
|
|
|
2021-10-19 08:10:06 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
2023-04-14 13:53:15 +00:00
|
|
|
if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
|
2023-04-14 22:54:22 +00:00
|
|
|
ret = gst_v4l2_video_dec_wait_for_src_ch (self);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_INFO_OBJECT (decoder, "Polling for source change was interrupted");
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2023-04-21 17:33:11 +00:00
|
|
|
GST_DEBUG_OBJECT (decoder, "Setup the capture queue");
|
2023-06-26 21:08:57 +00:00
|
|
|
if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
|
|
|
|
if (!gst_video_decoder_negotiate (decoder)) {
|
|
|
|
/* FIXME not super nice ? */
|
|
|
|
if (GST_PAD_IS_FLUSHING (decoder->sinkpad)
|
|
|
|
|| GST_PAD_IS_FLUSHING (decoder->srcpad)) {
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
} else {
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
GST_ERROR_OBJECT (decoder, "Failed to setup capture queue");
|
|
|
|
}
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
|
|
|
goto beach;
|
|
|
|
}
|
2022-03-15 01:49:59 +00:00
|
|
|
}
|
2023-04-14 13:53:15 +00:00
|
|
|
|
2023-06-26 21:08:57 +00:00
|
|
|
/* just a safety, as introducing mistakes in negotiation seems rather
|
2023-04-14 13:53:15 +00:00
|
|
|
* easy.*/
|
|
|
|
g_return_if_fail (GST_V4L2_IS_ACTIVE (self->v4l2capture));
|
2021-10-19 08:10:06 +00:00
|
|
|
}
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
|
|
|
|
2023-04-17 20:10:34 +00:00
|
|
|
GST_LOG_OBJECT (decoder, "Acquire output buffer");
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2017-02-17 13:01:08 +00:00
|
|
|
self->output_flow = GST_FLOW_OK;
|
2021-10-19 08:10:06 +00:00
|
|
|
|
2014-11-16 17:34:17 +00:00
|
|
|
do {
|
|
|
|
/* We cannot use the base class allotate helper since it taking the internal
|
|
|
|
* stream lock. we know that the acquire may need to poll until more frames
|
|
|
|
* comes in and holding this lock would prevent that.
|
|
|
|
*/
|
|
|
|
pool = gst_video_decoder_get_buffer_pool (decoder);
|
2014-03-12 17:07:38 +00:00
|
|
|
|
2014-11-16 17:34:17 +00:00
|
|
|
/* Pool may be NULL if we started going to READY state */
|
|
|
|
if (pool == NULL) {
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
goto beach;
|
|
|
|
}
|
2014-03-12 17:07:38 +00:00
|
|
|
|
2014-11-16 17:34:17 +00:00
|
|
|
ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
|
|
|
|
g_object_unref (pool);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2014-11-16 17:34:17 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto beach;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (decoder, "Process output buffer");
|
2022-11-28 22:26:50 +00:00
|
|
|
{
|
|
|
|
GstV4l2BufferPool *cpool =
|
|
|
|
GST_V4L2_BUFFER_POOL (gst_v4l2_object_get_buffer_pool
|
|
|
|
(self->v4l2capture));
|
|
|
|
ret = gst_v4l2_buffer_pool_process (cpool, &buffer, NULL);
|
|
|
|
if (cpool)
|
|
|
|
gst_object_unref (cpool);
|
|
|
|
}
|
2014-11-16 17:34:17 +00:00
|
|
|
} while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto beach;
|
|
|
|
|
2020-10-19 14:56:04 +00:00
|
|
|
if (GST_BUFFER_TIMESTAMP (buffer) % GST_SECOND != 0)
|
|
|
|
GST_ERROR_OBJECT (decoder,
|
|
|
|
"Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git");
|
|
|
|
GST_LOG_OBJECT (decoder, "Got buffer for frame number %u",
|
|
|
|
(guint32) (GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND));
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2020-10-19 14:56:04 +00:00
|
|
|
frame =
|
|
|
|
gst_video_decoder_get_frame (decoder,
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND);
|
2013-11-28 22:14:18 +00:00
|
|
|
if (frame) {
|
2020-10-19 15:23:25 +00:00
|
|
|
GstVideoCodecFrame *oldest_frame;
|
|
|
|
gboolean warned = FALSE;
|
|
|
|
|
|
|
|
/* Garbage collect old frames in case of codec bugs */
|
|
|
|
while ((oldest_frame = gst_video_decoder_get_oldest_frame (decoder)) &&
|
|
|
|
check_system_frame_number_too_old (frame->system_frame_number,
|
|
|
|
oldest_frame->system_frame_number)) {
|
2022-11-14 00:19:41 +00:00
|
|
|
if (oldest_frame->system_frame_number > 0) {
|
2022-05-28 20:47:42 +00:00
|
|
|
gst_video_decoder_drop_frame (decoder, oldest_frame);
|
2022-11-14 00:19:41 +00:00
|
|
|
oldest_frame = NULL;
|
|
|
|
|
|
|
|
if (!warned) {
|
|
|
|
g_warning ("%s: Too old frames, bug in decoder -- please file a bug",
|
|
|
|
GST_ELEMENT_NAME (decoder));
|
|
|
|
warned = TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* special treatment when oldest_frame->system_frame_number = 0:
|
|
|
|
* if a consecutive sequence 0, 1, 2,..., n < frame->system_frame_number
|
|
|
|
* is pending, drop them all at this time. (This has been seen to occur
|
|
|
|
* as a driver bug when the initial frame triggered a Caps re-negotiation,
|
|
|
|
* and the driver dropped these frames) */
|
|
|
|
guint32 counter = 0;
|
|
|
|
while (oldest_frame) {
|
|
|
|
gst_video_decoder_drop_frame (decoder, oldest_frame);
|
|
|
|
counter++;
|
|
|
|
oldest_frame = gst_video_decoder_get_oldest_frame (decoder);
|
|
|
|
if (oldest_frame &&
|
|
|
|
(oldest_frame->system_frame_number > counter ||
|
|
|
|
oldest_frame->system_frame_number >=
|
|
|
|
frame->system_frame_number)) {
|
|
|
|
gst_video_codec_frame_unref (oldest_frame);
|
|
|
|
oldest_frame = NULL;
|
|
|
|
}
|
2022-05-28 20:47:42 +00:00
|
|
|
}
|
2022-11-14 00:19:41 +00:00
|
|
|
g_warning
|
|
|
|
("%s: %i initial frames were not dequeued: bug in decoder -- please file a bug",
|
|
|
|
GST_ELEMENT_NAME (decoder), counter);
|
2022-05-28 20:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 15:23:25 +00:00
|
|
|
if (oldest_frame)
|
|
|
|
gst_video_codec_frame_unref (oldest_frame);
|
|
|
|
|
2022-03-14 08:19:33 +00:00
|
|
|
frame->duration = self->v4l2capture->duration;
|
2013-11-28 22:14:18 +00:00
|
|
|
frame->output_buffer = buffer;
|
|
|
|
buffer = NULL;
|
|
|
|
ret = gst_video_decoder_finish_frame (decoder, frame);
|
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto beach;
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
beach:
|
2023-04-19 18:19:13 +00:00
|
|
|
if (ret == GST_V4L2_FLOW_RESOLUTION_CHANGE) {
|
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
|
|
|
self->draining = TRUE;
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
|
|
|
GST_INFO_OBJECT (decoder, "Received resolution change");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-18 15:58:51 +00:00
|
|
|
if (ret == GST_V4L2_FLOW_LAST_BUFFER) {
|
2023-04-19 18:19:13 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
|
|
|
if (self->draining) {
|
|
|
|
self->draining = FALSE;
|
|
|
|
gst_v4l2_object_stop (self->v4l2capture);
|
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
|
|
|
return;
|
|
|
|
}
|
2023-04-26 19:58:23 +00:00
|
|
|
|
2023-04-19 18:19:13 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:39:40 +00:00
|
|
|
GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
|
|
|
|
gst_flow_get_name (ret));
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
gst_buffer_replace (&buffer, NULL);
|
|
|
|
self->output_flow = ret;
|
|
|
|
gst_v4l2_object_unlock (self->v4l2output);
|
|
|
|
gst_pad_pause_task (decoder->srcpad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
2016-06-07 20:04:52 +00:00
|
|
|
GstV4l2Error error = GST_V4L2_ERROR_INIT;
|
2013-11-28 22:14:18 +00:00
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
2023-04-14 13:53:15 +00:00
|
|
|
GstBufferPool *pool = NULL;
|
2013-11-28 22:14:18 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2016-06-07 20:42:09 +00:00
|
|
|
gboolean processed = FALSE;
|
|
|
|
GstBuffer *tmp;
|
2017-12-13 20:23:46 +00:00
|
|
|
GstTaskState task_state;
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
|
|
|
|
|
|
|
|
if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
|
|
|
|
goto flushing;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
|
2014-03-19 21:52:08 +00:00
|
|
|
if (!self->input_state)
|
|
|
|
goto not_negotiated;
|
2016-06-07 20:04:52 +00:00
|
|
|
if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
|
2016-06-07 20:41:58 +00:00
|
|
|
&error))
|
2013-11-28 22:14:18 +00:00
|
|
|
goto not_negotiated;
|
|
|
|
}
|
|
|
|
|
2023-04-14 13:53:15 +00:00
|
|
|
pool = gst_v4l2_object_get_buffer_pool (self->v4l2output);
|
2021-10-26 02:03:42 +00:00
|
|
|
if (G_UNLIKELY (!gst_buffer_pool_is_active (pool))) {
|
2013-11-28 22:14:18 +00:00
|
|
|
GstBuffer *codec_data;
|
2021-10-26 02:03:42 +00:00
|
|
|
GstStructure *config = gst_buffer_pool_get_config (pool);
|
|
|
|
guint min = MAX (self->v4l2output->min_buffers,
|
|
|
|
GST_V4L2_MIN_BUFFERS (self->v4l2output));
|
|
|
|
guint max = VIDEO_MAX_FRAME;
|
2020-10-19 14:56:04 +00:00
|
|
|
guint32 dummy_frame_number = 0;
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Sending header");
|
|
|
|
codec_data = self->input_state->codec_data;
|
|
|
|
|
|
|
|
/* We are running in byte-stream mode, so we don't know the headers, but
|
|
|
|
* we need to send something, otherwise the decoder will refuse to
|
2019-09-02 12:27:35 +00:00
|
|
|
* initialize.
|
2013-11-28 22:14:18 +00:00
|
|
|
*/
|
|
|
|
if (codec_data) {
|
|
|
|
gst_buffer_ref (codec_data);
|
|
|
|
} else {
|
2016-06-07 20:42:09 +00:00
|
|
|
codec_data = gst_buffer_ref (frame->input_buffer);
|
|
|
|
processed = TRUE;
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 20:41:46 +00:00
|
|
|
/* Ensure input internal pool is active */
|
2020-04-15 16:36:29 +00:00
|
|
|
|
2021-10-26 02:03:42 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, self->input_state->caps,
|
|
|
|
self->v4l2output->info.size, min, max);
|
2014-04-15 20:41:46 +00:00
|
|
|
|
2021-10-26 02:03:42 +00:00
|
|
|
/* There is no reason to refuse this config */
|
2022-05-19 19:51:03 +00:00
|
|
|
if (!gst_buffer_pool_set_config (pool, config)) {
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
|
|
|
|
if (!gst_buffer_pool_config_validate_params (config,
|
|
|
|
self->input_state->caps, self->v4l2output->info.size, min, max)) {
|
|
|
|
gst_structure_free (config);
|
|
|
|
goto activate_failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_buffer_pool_set_config (pool, config))
|
|
|
|
goto activate_failed;
|
|
|
|
}
|
2014-04-15 20:41:46 +00:00
|
|
|
|
2023-04-14 22:54:22 +00:00
|
|
|
/* Ensure to unlock capture, as it may be flushing due to previous
|
|
|
|
* unlock/stop calls */
|
|
|
|
gst_v4l2_object_unlock_stop (self->v4l2output);
|
|
|
|
gst_v4l2_object_unlock_stop (self->v4l2capture);
|
|
|
|
|
2021-10-26 02:03:42 +00:00
|
|
|
if (!gst_buffer_pool_set_active (pool, TRUE))
|
|
|
|
goto activate_failed;
|
2014-03-16 17:39:32 +00:00
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
2020-10-19 14:56:04 +00:00
|
|
|
GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
|
|
|
|
processed ? frame->system_frame_number : 0);
|
2013-11-28 22:14:18 +00:00
|
|
|
ret =
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), &codec_data,
|
2020-10-19 14:56:04 +00:00
|
|
|
processed ? &frame->system_frame_number : &dummy_frame_number);
|
2013-11-28 22:14:18 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
|
|
|
|
|
|
|
gst_buffer_unref (codec_data);
|
2023-04-14 22:54:22 +00:00
|
|
|
|
|
|
|
/* Only wait for source change if the formats supports it */
|
|
|
|
if (self->v4l2output->fmtdesc->flags & V4L2_FMT_FLAG_DYN_RESOLUTION) {
|
|
|
|
gst_v4l2_object_unlock_stop (self->v4l2capture);
|
|
|
|
self->wait_for_source_change = TRUE;
|
|
|
|
}
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 20:23:46 +00:00
|
|
|
task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
|
|
|
|
if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
|
2014-05-23 22:25:49 +00:00
|
|
|
/* It's possible that the processing thread stopped due to an error */
|
|
|
|
if (self->output_flow != GST_FLOW_OK &&
|
|
|
|
self->output_flow != GST_FLOW_FLUSHING) {
|
2013-11-28 22:14:18 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
|
|
|
|
ret = self->output_flow;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Starting decoding thread");
|
|
|
|
|
|
|
|
/* Start the processing task, when it quits, the task will disable input
|
|
|
|
* processing to unlock input if draining, or prevent potential block */
|
2017-02-17 13:01:08 +00:00
|
|
|
self->output_flow = GST_FLOW_FLUSHING;
|
2023-04-19 18:19:13 +00:00
|
|
|
self->draining = FALSE;
|
2014-05-23 21:28:13 +00:00
|
|
|
if (!gst_pad_start_task (decoder->srcpad,
|
2017-02-17 13:01:08 +00:00
|
|
|
(GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
|
2014-05-23 21:28:13 +00:00
|
|
|
goto start_task_failed;
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 20:42:09 +00:00
|
|
|
if (!processed) {
|
2013-11-28 22:14:18 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
|
2020-10-19 14:56:04 +00:00
|
|
|
GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
|
|
|
|
frame->system_frame_number);
|
2013-11-28 22:14:18 +00:00
|
|
|
ret =
|
2022-11-28 22:26:50 +00:00
|
|
|
gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool),
|
|
|
|
&frame->input_buffer, &frame->system_frame_number);
|
2013-11-28 22:14:18 +00:00
|
|
|
GST_VIDEO_DECODER_STREAM_LOCK (decoder);
|
|
|
|
|
|
|
|
if (ret == GST_FLOW_FLUSHING) {
|
2017-02-17 13:01:08 +00:00
|
|
|
if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
|
|
|
|
GST_TASK_STARTED)
|
2013-11-28 22:14:18 +00:00
|
|
|
ret = self->output_flow;
|
2014-05-23 21:01:02 +00:00
|
|
|
goto drop;
|
|
|
|
} else if (ret != GST_FLOW_OK) {
|
|
|
|
goto process_failed;
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
/* No need to keep input around */
|
2016-06-07 20:42:09 +00:00
|
|
|
tmp = frame->input_buffer;
|
|
|
|
frame->input_buffer = gst_buffer_new ();
|
|
|
|
gst_buffer_copy_into (frame->input_buffer, tmp,
|
|
|
|
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
|
|
|
|
GST_BUFFER_COPY_META, 0, 0);
|
|
|
|
gst_buffer_unref (tmp);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
gst_video_codec_frame_unref (frame);
|
2022-11-28 22:26:50 +00:00
|
|
|
if (pool)
|
|
|
|
gst_object_unref (pool);
|
2013-11-28 22:14:18 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
not_negotiated:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (self, "not negotiated");
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
2016-06-07 20:04:52 +00:00
|
|
|
gst_v4l2_error (self, &error);
|
2013-11-28 22:14:18 +00:00
|
|
|
goto drop;
|
|
|
|
}
|
2014-03-16 17:39:32 +00:00
|
|
|
activate_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
|
2014-04-15 20:41:46 +00:00
|
|
|
(_("Failed to allocate required memory.")),
|
2014-03-16 17:39:32 +00:00
|
|
|
("Buffer pool activation failed"));
|
2014-05-23 21:01:02 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto drop;
|
2014-03-16 17:39:32 +00:00
|
|
|
}
|
2013-11-28 22:14:18 +00:00
|
|
|
flushing:
|
|
|
|
{
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
goto drop;
|
|
|
|
}
|
2014-05-23 21:28:13 +00:00
|
|
|
|
|
|
|
start_task_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
|
|
|
|
(_("Failed to start decoding thread.")), (NULL));
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto drop;
|
|
|
|
}
|
2014-05-23 21:01:02 +00:00
|
|
|
process_failed:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
|
|
|
|
(_("Failed to process frame.")),
|
|
|
|
("Maybe be due to not enough memory or failing driver"));
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto drop;
|
|
|
|
}
|
2013-11-28 22:14:18 +00:00
|
|
|
drop:
|
|
|
|
{
|
2022-11-28 22:26:50 +00:00
|
|
|
if (pool)
|
|
|
|
gst_object_unref (pool);
|
2013-11-28 22:14:18 +00:00
|
|
|
gst_video_decoder_drop_frame (decoder, frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
2013-12-19 20:26:52 +00:00
|
|
|
GstClockTime latency;
|
2013-11-28 22:14:18 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
|
|
|
|
ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
|
|
|
|
query);
|
|
|
|
|
2016-03-16 15:24:55 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
|
|
|
|
latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
|
2017-04-05 13:46:31 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
|
|
|
|
G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
|
|
|
|
self->v4l2capture->min_buffers, self->v4l2capture->duration);
|
2016-03-16 15:24:55 +00:00
|
|
|
gst_video_decoder_set_latency (decoder, latency, latency);
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
|
|
|
|
}
|
2013-12-19 20:26:52 +00:00
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
|
|
|
|
{
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CAPS:{
|
|
|
|
GstCaps *filter, *result = NULL;
|
2014-03-06 23:13:14 +00:00
|
|
|
GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
gst_query_parse_caps (query, &filter);
|
|
|
|
|
|
|
|
if (self->probed_srccaps)
|
|
|
|
result = gst_caps_ref (self->probed_srccaps);
|
|
|
|
else
|
2014-03-06 23:13:14 +00:00
|
|
|
result = gst_pad_get_pad_template_caps (pad);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
GstCaps *tmp = result;
|
|
|
|
result =
|
|
|
|
gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
|
|
|
|
gst_caps_unref (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
|
|
|
|
|
|
|
|
gst_query_set_caps_result (query, result);
|
|
|
|
gst_caps_unref (result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:49:34 +00:00
|
|
|
static GstCaps *
|
|
|
|
gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
|
2013-11-28 22:14:18 +00:00
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
2015-01-13 21:49:34 +00:00
|
|
|
GstCaps *result;
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2015-01-13 21:49:34 +00:00
|
|
|
result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
|
|
|
|
filter);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2015-01-13 21:49:34 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2015-01-13 21:49:34 +00:00
|
|
|
return result;
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
|
2014-05-25 00:20:07 +00:00
|
|
|
gboolean ret;
|
2019-04-16 09:05:06 +00:00
|
|
|
GstEventType type = GST_EVENT_TYPE (event);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
2019-04-16 09:05:06 +00:00
|
|
|
switch (type) {
|
2013-11-28 22:14:18 +00:00
|
|
|
case GST_EVENT_FLUSH_START:
|
2014-05-23 21:19:07 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "flush start");
|
2013-11-28 22:14:18 +00:00
|
|
|
gst_v4l2_object_unlock (self->v4l2output);
|
|
|
|
gst_v4l2_object_unlock (self->v4l2capture);
|
2014-05-25 00:20:07 +00:00
|
|
|
break;
|
2013-11-28 22:14:18 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-25 00:20:07 +00:00
|
|
|
ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
|
|
|
|
|
2019-04-16 09:05:06 +00:00
|
|
|
switch (type) {
|
2014-05-25 00:20:07 +00:00
|
|
|
case GST_EVENT_FLUSH_START:
|
|
|
|
/* The processing thread should stop now, wait for it */
|
|
|
|
gst_pad_stop_task (decoder->srcpad);
|
|
|
|
GST_DEBUG_OBJECT (self, "flush start done");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_v4l2_video_dec_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
|
2014-05-25 00:20:07 +00:00
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
|
|
|
|
g_atomic_int_set (&self->active, FALSE);
|
|
|
|
gst_v4l2_object_unlock (self->v4l2output);
|
|
|
|
gst_v4l2_object_unlock (self->v4l2capture);
|
2014-05-25 00:20:07 +00:00
|
|
|
gst_pad_stop_task (decoder->srcpad);
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_video_dec_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
|
|
|
|
|
|
|
|
gst_caps_replace (&self->probed_sinkcaps, NULL);
|
|
|
|
gst_caps_replace (&self->probed_srccaps, NULL);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_video_dec_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
|
|
|
|
|
|
|
|
gst_v4l2_object_destroy (self->v4l2capture);
|
|
|
|
gst_v4l2_object_destroy (self->v4l2output);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-03-16 10:38:07 +00:00
|
|
|
gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
|
2013-12-18 08:56:35 +00:00
|
|
|
{
|
2014-03-16 10:38:07 +00:00
|
|
|
/* V4L2 object are created in subinstance_init */
|
2013-12-18 08:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-03-16 10:38:07 +00:00
|
|
|
gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
|
2013-11-28 22:14:18 +00:00
|
|
|
{
|
2014-03-16 10:38:07 +00:00
|
|
|
GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
|
|
|
|
GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
|
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
|
2013-11-28 22:14:18 +00:00
|
|
|
|
|
|
|
gst_video_decoder_set_packetized (decoder, TRUE);
|
|
|
|
|
|
|
|
self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
|
2017-12-13 19:39:47 +00:00
|
|
|
GST_OBJECT (GST_VIDEO_DECODER_SINK_PAD (self)),
|
2014-03-16 10:38:07 +00:00
|
|
|
V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
|
2013-11-28 22:14:18 +00:00
|
|
|
gst_v4l2_get_output, gst_v4l2_set_output, NULL);
|
|
|
|
self->v4l2output->no_initial_format = TRUE;
|
|
|
|
self->v4l2output->keep_aspect = FALSE;
|
|
|
|
|
|
|
|
self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
|
2017-12-13 19:39:47 +00:00
|
|
|
GST_OBJECT (GST_VIDEO_DECODER_SRC_PAD (self)),
|
2014-03-16 10:38:07 +00:00
|
|
|
V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
|
2013-11-28 22:14:18 +00:00
|
|
|
gst_v4l2_get_input, gst_v4l2_set_input, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class;
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstVideoDecoderClass *video_decoder_class;
|
|
|
|
|
2013-12-18 08:56:35 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
element_class = (GstElementClass *) klass;
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
video_decoder_class = (GstVideoDecoderClass *) klass;
|
|
|
|
|
2014-05-03 01:38:30 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
|
|
|
|
"V4L2 Video Decoder");
|
|
|
|
|
2013-11-28 22:14:18 +00:00
|
|
|
gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
|
|
|
|
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
|
|
|
|
gobject_class->set_property =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
|
|
|
|
gobject_class->get_property =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
|
|
|
|
|
|
|
|
video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
|
|
|
|
video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
|
|
|
|
video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
|
|
|
|
video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
|
|
|
|
video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
|
|
|
|
video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
|
2017-12-13 20:23:46 +00:00
|
|
|
video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_drain);
|
2013-11-28 22:14:18 +00:00
|
|
|
video_decoder_class->set_format =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
|
|
|
|
video_decoder_class->negotiate =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
|
|
|
|
video_decoder_class->decide_allocation =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
|
|
|
|
/* FIXME propose_allocation or not ? */
|
|
|
|
video_decoder_class->handle_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
|
2015-01-13 21:49:34 +00:00
|
|
|
video_decoder_class->getcaps =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
|
2013-11-28 22:14:18 +00:00
|
|
|
video_decoder_class->src_query =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
|
|
|
|
video_decoder_class->sink_event =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
|
|
|
|
|
|
|
|
element_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
|
|
|
|
|
2014-05-15 22:08:53 +00:00
|
|
|
gst_v4l2_object_install_m2m_properties_helper (gobject_class);
|
2013-12-18 08:56:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 10:38:07 +00:00
|
|
|
static void
|
|
|
|
gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
|
|
|
|
{
|
|
|
|
GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
GstV4l2VideoDecCData *cdata = data;
|
|
|
|
|
|
|
|
klass->default_device = cdata->device;
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
|
|
|
cdata->sink_caps));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
cdata->src_caps));
|
|
|
|
|
2020-06-11 17:39:33 +00:00
|
|
|
gst_element_class_set_metadata (element_class, cdata->longname,
|
2019-02-27 11:56:20 +00:00
|
|
|
"Codec/Decoder/Video/Hardware", cdata->description,
|
2017-07-13 16:32:00 +00:00
|
|
|
"Nicolas Dufresne <nicolas.dufresne@collabora.com>");
|
|
|
|
|
2017-09-20 01:46:15 +00:00
|
|
|
gst_caps_unref (cdata->sink_caps);
|
|
|
|
gst_caps_unref (cdata->src_caps);
|
2014-03-16 10:38:07 +00:00
|
|
|
g_free (cdata);
|
|
|
|
}
|
|
|
|
|
2013-12-18 08:56:35 +00:00
|
|
|
/* Probing functions */
|
2014-05-03 01:38:30 +00:00
|
|
|
gboolean
|
|
|
|
gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
|
2013-12-18 08:56:35 +00:00
|
|
|
{
|
2014-05-03 01:38:30 +00:00
|
|
|
gboolean ret = FALSE;
|
2013-12-18 08:56:35 +00:00
|
|
|
|
2014-05-03 01:38:30 +00:00
|
|
|
if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
|
|
|
|
&& gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
|
|
|
|
ret = TRUE;
|
2013-12-18 08:56:35 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-13 16:32:00 +00:00
|
|
|
static gchar *
|
|
|
|
gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
|
|
|
|
const gchar * basename)
|
|
|
|
{
|
|
|
|
gchar *codec_name = NULL;
|
|
|
|
gchar *type_name = NULL;
|
|
|
|
|
|
|
|
#define SET_META(codec) \
|
|
|
|
G_STMT_START { \
|
|
|
|
cdata->longname = "V4L2 " codec " Decoder"; \
|
|
|
|
cdata->description = "Decodes " codec " streams via V4L2 API"; \
|
|
|
|
codec_name = g_ascii_strdown (codec, -1); \
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
|
|
if (gst_structure_has_name (s, "image/jpeg")) {
|
|
|
|
SET_META ("JPEG");
|
|
|
|
} else if (gst_structure_has_name (s, "video/mpeg")) {
|
|
|
|
gint mpegversion = 0;
|
|
|
|
gst_structure_get_int (s, "mpegversion", &mpegversion);
|
|
|
|
|
2023-07-06 09:44:48 +00:00
|
|
|
if (mpegversion == 4) {
|
|
|
|
SET_META ("MPEG4");
|
|
|
|
cdata->codec = gst_v4l2_mpeg4_get_codec ();
|
|
|
|
} else {
|
|
|
|
/* MPEG 2 decoders supports MPEG 1 format */
|
|
|
|
SET_META ("MPEG2");
|
|
|
|
cdata->codec = gst_v4l2_mpeg2_get_codec ();
|
2017-07-13 16:32:00 +00:00
|
|
|
}
|
|
|
|
} else if (gst_structure_has_name (s, "video/x-h263")) {
|
|
|
|
SET_META ("H263");
|
2018-09-10 19:20:52 +00:00
|
|
|
} else if (gst_structure_has_name (s, "video/x-fwht")) {
|
|
|
|
SET_META ("FWHT");
|
2017-07-13 16:32:00 +00:00
|
|
|
} else if (gst_structure_has_name (s, "video/x-h264")) {
|
|
|
|
SET_META ("H264");
|
2019-03-04 11:05:29 +00:00
|
|
|
cdata->codec = gst_v4l2_h264_get_codec ();
|
2018-09-13 03:16:32 +00:00
|
|
|
} else if (gst_structure_has_name (s, "video/x-h265")) {
|
|
|
|
SET_META ("H265");
|
2019-03-04 11:05:29 +00:00
|
|
|
cdata->codec = gst_v4l2_h265_get_codec ();
|
2017-07-13 16:32:00 +00:00
|
|
|
} else if (gst_structure_has_name (s, "video/x-wmv")) {
|
|
|
|
SET_META ("VC1");
|
|
|
|
} else if (gst_structure_has_name (s, "video/x-vp8")) {
|
|
|
|
SET_META ("VP8");
|
2019-03-04 11:05:29 +00:00
|
|
|
cdata->codec = gst_v4l2_vp8_get_codec ();
|
2017-08-02 21:13:55 +00:00
|
|
|
} else if (gst_structure_has_name (s, "video/x-vp9")) {
|
|
|
|
SET_META ("VP9");
|
2019-03-04 11:05:29 +00:00
|
|
|
cdata->codec = gst_v4l2_vp9_get_codec ();
|
2017-07-13 16:32:00 +00:00
|
|
|
} else if (gst_structure_has_name (s, "video/x-bayer")) {
|
|
|
|
SET_META ("BAYER");
|
|
|
|
} else if (gst_structure_has_name (s, "video/x-sonix")) {
|
|
|
|
SET_META ("SONIX");
|
|
|
|
} else if (gst_structure_has_name (s, "video/x-pwc1")) {
|
|
|
|
SET_META ("PWC1");
|
|
|
|
} else if (gst_structure_has_name (s, "video/x-pwc2")) {
|
|
|
|
SET_META ("PWC2");
|
|
|
|
} else {
|
|
|
|
/* This code should be kept on sync with the exposed CODEC type of format
|
2019-09-02 12:27:35 +00:00
|
|
|
* from gstv4l2object.c. This warning will only occur in case we forget
|
2017-07-13 16:32:00 +00:00
|
|
|
* to also add a format here. */
|
|
|
|
gchar *s_str = gst_structure_to_string (s);
|
|
|
|
g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
|
|
|
|
"bug, please report at https://bugs.gnome.org", s_str);
|
|
|
|
g_free (s_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codec_name) {
|
|
|
|
type_name = g_strdup_printf ("v4l2%sdec", codec_name);
|
|
|
|
if (g_type_from_name (type_name) != 0) {
|
|
|
|
g_free (type_name);
|
|
|
|
type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
|
|
|
|
}
|
2017-09-19 21:55:58 +00:00
|
|
|
|
|
|
|
g_free (codec_name);
|
2017-07-13 16:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return type_name;
|
|
|
|
#undef SET_META
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:51:07 +00:00
|
|
|
void
|
2014-05-03 01:38:30 +00:00
|
|
|
gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
|
2019-03-04 11:05:29 +00:00
|
|
|
const gchar * device_path, gint video_fd, GstCaps * sink_caps,
|
|
|
|
GstCaps * src_caps)
|
2013-12-18 08:56:35 +00:00
|
|
|
{
|
2017-07-13 16:32:00 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
|
|
|
|
GstV4l2VideoDecCData *cdata;
|
|
|
|
GstStructure *s;
|
|
|
|
GTypeQuery type_query;
|
|
|
|
GTypeInfo type_info = { 0, };
|
|
|
|
GType type, subtype;
|
|
|
|
gchar *type_name;
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (sink_caps, i);
|
|
|
|
|
|
|
|
cdata = g_new0 (GstV4l2VideoDecCData, 1);
|
|
|
|
cdata->device = g_strdup (device_path);
|
|
|
|
cdata->sink_caps = gst_caps_new_empty ();
|
|
|
|
gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
|
|
|
|
cdata->src_caps = gst_caps_ref (src_caps);
|
|
|
|
type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
|
|
|
|
|
|
|
|
/* Skip over if we hit an unmapped type */
|
|
|
|
if (!type_name) {
|
|
|
|
g_free (cdata);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:32:40 +00:00
|
|
|
if (cdata->codec != NULL && cdata->codec != gst_v4l2_vp8_get_codec ()
|
|
|
|
&& cdata->codec != gst_v4l2_vp9_get_codec ()) {
|
2020-05-21 21:09:39 +00:00
|
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
|
|
|
|
if (gst_v4l2_codec_probe_levels (cdata->codec, video_fd, &value)) {
|
|
|
|
gst_caps_set_value (cdata->sink_caps, "level", &value);
|
|
|
|
g_value_unset (&value);
|
2019-03-04 11:05:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 21:09:39 +00:00
|
|
|
if (gst_v4l2_codec_probe_profiles (cdata->codec, video_fd, &value)) {
|
|
|
|
gst_caps_set_value (cdata->sink_caps, "profile", &value);
|
|
|
|
g_value_unset (&value);
|
2019-03-04 11:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 16:32:00 +00:00
|
|
|
type = gst_v4l2_video_dec_get_type ();
|
|
|
|
g_type_query (type, &type_query);
|
|
|
|
memset (&type_info, 0, sizeof (type_info));
|
|
|
|
type_info.class_size = type_query.class_size;
|
|
|
|
type_info.instance_size = type_query.instance_size;
|
|
|
|
type_info.class_init = gst_v4l2_video_dec_subclass_init;
|
|
|
|
type_info.class_data = cdata;
|
|
|
|
type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
|
|
|
|
|
|
|
|
subtype = g_type_register_static (type, type_name, &type_info, 0);
|
2017-07-27 14:51:07 +00:00
|
|
|
if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
|
|
|
|
subtype))
|
|
|
|
GST_WARNING ("Failed to register plugin '%s'", type_name);
|
2017-07-13 16:32:00 +00:00
|
|
|
|
|
|
|
g_free (type_name);
|
|
|
|
}
|
2013-11-28 22:14:18 +00:00
|
|
|
}
|