2010-12-14 07:32:30 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 David Schleef <ds@schleef.org>
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-12-14 07:32:30 +00:00
|
|
|
*/
|
|
|
|
/**
|
2020-07-10 14:26:27 +00:00
|
|
|
* SECTION:element-y4mdec
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: gsty4mdec
|
2010-12-14 07:32:30 +00:00
|
|
|
*
|
|
|
|
* The gsty4mdec element decodes uncompressed video in YUV4MPEG format.
|
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Example launch line
|
2010-12-14 07:32:30 +00:00
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 -v filesrc location=file.y4m ! y4mdec ! xvimagesink
|
2010-12-14 07:32:30 +00:00
|
|
|
* ]|
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2010-12-14 07:32:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <gst/video/video.h>
|
|
|
|
#include "gsty4mdec.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define MAX_SIZE 32768
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY (y4mdec_debug);
|
|
|
|
#define GST_CAT_DEFAULT y4mdec_debug
|
|
|
|
|
|
|
|
/* prototypes */
|
|
|
|
|
|
|
|
|
|
|
|
static void gst_y4m_dec_set_property (GObject * object,
|
|
|
|
guint property_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_y4m_dec_get_property (GObject * object,
|
|
|
|
guint property_id, GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_y4m_dec_dispose (GObject * object);
|
|
|
|
static void gst_y4m_dec_finalize (GObject * object);
|
|
|
|
|
2011-11-17 14:53:42 +00:00
|
|
|
static GstFlowReturn gst_y4m_dec_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buffer);
|
|
|
|
static gboolean gst_y4m_dec_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2011-11-17 14:53:42 +00:00
|
|
|
static gboolean gst_y4m_dec_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
2011-11-16 16:28:10 +00:00
|
|
|
static gboolean gst_y4m_dec_src_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_y4m_dec_change_state (GstElement * element, GstStateChange transition);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* pad templates */
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_y4m_dec_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("application/x-yuv4mpeg, y4mversion=2")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_y4m_dec_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2024-01-25 22:56:07 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ \
|
|
|
|
I420,Y41B,Y42B,Y444, \
|
|
|
|
I420_10LE,I422_10LE,Y444_10LE, \
|
|
|
|
I420_12LE,I422_12LE,Y444_12LE, \
|
|
|
|
Y444_16LE,GRAY8,GRAY16_LE \
|
|
|
|
}")));
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
/* class initialization */
|
2011-10-16 15:41:45 +00:00
|
|
|
#define gst_y4m_dec_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstY4mDec, gst_y4m_dec, GST_TYPE_ELEMENT);
|
2021-02-25 14:22:15 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (y4mdec, "y4mdec", GST_RANK_SECONDARY,
|
|
|
|
gst_y4m_dec_get_type (), GST_DEBUG_CATEGORY_INIT (y4mdec_debug, "y4mdec", 0,
|
|
|
|
"y4mdec element"));
|
2010-12-14 07:32:30 +00:00
|
|
|
static void
|
|
|
|
gst_y4m_dec_class_init (GstY4mDecClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->set_property = gst_y4m_dec_set_property;
|
|
|
|
gobject_class->get_property = gst_y4m_dec_get_property;
|
|
|
|
gobject_class->dispose = gst_y4m_dec_dispose;
|
|
|
|
gobject_class->finalize = gst_y4m_dec_finalize;
|
2011-10-16 15:41:45 +00:00
|
|
|
|
2010-12-14 07:32:30 +00:00
|
|
|
element_class->change_state = GST_DEBUG_FUNCPTR (gst_y4m_dec_change_state);
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_y4m_dec_src_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_y4m_dec_sink_template);
|
2011-10-16 15:41:45 +00:00
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2011-10-16 15:41:45 +00:00
|
|
|
"YUV4MPEG demuxer/decoder", "Codec/Demuxer",
|
|
|
|
"Demuxes/decodes YUV4MPEG streams", "David Schleef <ds@schleef.org>");
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-16 15:41:45 +00:00
|
|
|
gst_y4m_dec_init (GstY4mDec * y4mdec)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
|
|
|
y4mdec->adapter = gst_adapter_new ();
|
|
|
|
|
|
|
|
y4mdec->sinkpad =
|
|
|
|
gst_pad_new_from_static_template (&gst_y4m_dec_sink_template, "sink");
|
|
|
|
gst_pad_set_event_function (y4mdec->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_y4m_dec_sink_event));
|
|
|
|
gst_pad_set_chain_function (y4mdec->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_y4m_dec_chain));
|
|
|
|
gst_element_add_pad (GST_ELEMENT (y4mdec), y4mdec->sinkpad);
|
|
|
|
|
|
|
|
y4mdec->srcpad = gst_pad_new_from_static_template (&gst_y4m_dec_src_template,
|
|
|
|
"src");
|
|
|
|
gst_pad_set_event_function (y4mdec->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_y4m_dec_src_event));
|
|
|
|
gst_pad_set_query_function (y4mdec->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_y4m_dec_src_query));
|
|
|
|
gst_pad_use_fixed_caps (y4mdec->srcpad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (y4mdec), y4mdec->srcpad);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_y4m_dec_set_property (GObject * object, guint property_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_Y4M_DEC (object));
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_y4m_dec_get_property (GObject * object, guint property_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_Y4M_DEC (object));
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_y4m_dec_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstY4mDec *y4mdec;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_Y4M_DEC (object));
|
|
|
|
y4mdec = GST_Y4M_DEC (object);
|
|
|
|
|
|
|
|
/* clean up as possible. may be called multiple times */
|
|
|
|
if (y4mdec->adapter) {
|
|
|
|
g_object_unref (y4mdec->adapter);
|
|
|
|
y4mdec->adapter = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_y4m_dec_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GST_IS_Y4M_DEC (object));
|
|
|
|
|
|
|
|
/* clean up object here */
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_y4m_dec_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
2013-03-22 14:49:18 +00:00
|
|
|
GstY4mDec *y4mdec;
|
2010-12-14 07:32:30 +00:00
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_Y4M_DEC (element), GST_STATE_CHANGE_FAILURE);
|
|
|
|
|
2013-03-22 14:49:18 +00:00
|
|
|
y4mdec = GST_Y4M_DEC (element);
|
|
|
|
|
2010-12-14 07:32:30 +00:00
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2013-03-22 14:49:18 +00:00
|
|
|
if (y4mdec->pool) {
|
|
|
|
gst_buffer_pool_set_active (y4mdec->pool, FALSE);
|
|
|
|
gst_object_unref (y4mdec->pool);
|
|
|
|
}
|
|
|
|
y4mdec->pool = NULL;
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
2013-03-22 13:37:37 +00:00
|
|
|
gst_y4m_dec_frames_to_timestamp (GstY4mDec * y4mdec, gint64 frame_index)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
2013-03-22 13:37:37 +00:00
|
|
|
if (frame_index == -1)
|
|
|
|
return -1;
|
|
|
|
|
2011-10-16 15:41:45 +00:00
|
|
|
return gst_util_uint64_scale (frame_index, GST_SECOND * y4mdec->info.fps_d,
|
|
|
|
y4mdec->info.fps_n);
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:37:37 +00:00
|
|
|
static gint64
|
2010-12-14 07:32:30 +00:00
|
|
|
gst_y4m_dec_timestamp_to_frames (GstY4mDec * y4mdec, GstClockTime timestamp)
|
|
|
|
{
|
2013-03-22 13:37:37 +00:00
|
|
|
if (timestamp == -1)
|
|
|
|
return -1;
|
|
|
|
|
2011-10-16 15:41:45 +00:00
|
|
|
return gst_util_uint64_scale (timestamp, y4mdec->info.fps_n,
|
|
|
|
GST_SECOND * y4mdec->info.fps_d);
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:37:37 +00:00
|
|
|
static gint64
|
2010-12-14 07:32:30 +00:00
|
|
|
gst_y4m_dec_bytes_to_frames (GstY4mDec * y4mdec, gint64 bytes)
|
|
|
|
{
|
2013-03-22 13:37:37 +00:00
|
|
|
if (bytes == -1)
|
|
|
|
return -1;
|
|
|
|
|
2010-12-14 07:32:30 +00:00
|
|
|
if (bytes < y4mdec->header_size)
|
|
|
|
return 0;
|
2011-10-16 15:41:45 +00:00
|
|
|
return (bytes - y4mdec->header_size) / (y4mdec->info.size + 6);
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:37:37 +00:00
|
|
|
static guint64
|
|
|
|
gst_y4m_dec_frames_to_bytes (GstY4mDec * y4mdec, gint64 frame_index)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
2013-03-22 13:37:37 +00:00
|
|
|
if (frame_index == -1)
|
|
|
|
return -1;
|
|
|
|
|
2011-10-16 15:41:45 +00:00
|
|
|
return y4mdec->header_size + (y4mdec->info.size + 6) * frame_index;
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstClockTime
|
|
|
|
gst_y4m_dec_bytes_to_timestamp (GstY4mDec * y4mdec, gint64 bytes)
|
|
|
|
{
|
2013-03-22 13:37:37 +00:00
|
|
|
if (bytes == -1)
|
|
|
|
return -1;
|
|
|
|
|
2010-12-14 07:32:30 +00:00
|
|
|
return gst_y4m_dec_frames_to_timestamp (y4mdec,
|
|
|
|
gst_y4m_dec_bytes_to_frames (y4mdec, bytes));
|
|
|
|
}
|
|
|
|
|
2024-01-25 22:56:07 +00:00
|
|
|
static GstVideoFormat
|
|
|
|
parse_colorspace (const char *param)
|
2024-01-25 22:14:44 +00:00
|
|
|
{
|
2024-01-25 22:56:07 +00:00
|
|
|
char *end;
|
|
|
|
guint iformat = g_ascii_strtoull (param, &end, 10);
|
|
|
|
|
|
|
|
if (*end == '\0') {
|
|
|
|
switch (iformat) {
|
|
|
|
case 420:
|
|
|
|
return GST_VIDEO_FORMAT_I420;
|
|
|
|
case 411:
|
|
|
|
return GST_VIDEO_FORMAT_Y41B;
|
|
|
|
case 422:
|
|
|
|
return GST_VIDEO_FORMAT_Y42B;
|
|
|
|
case 444:
|
|
|
|
return GST_VIDEO_FORMAT_Y444;
|
|
|
|
}
|
2024-01-25 22:14:44 +00:00
|
|
|
}
|
2024-01-25 22:56:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse non-standard (i.e., unknown to mjpegtools) streams that are
|
|
|
|
* generated by FFmpeg:
|
|
|
|
* https://wiki.multimedia.cx/index.php/YUV4MPEG2
|
|
|
|
* https://github.com/FFmpeg/FFmpeg/blob/eee3b7e2/libavformat/yuv4mpegenc.c#L74-L166
|
|
|
|
* Will assume little-endian because this is an on-disk serialization format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TODO: Differentiate between:
|
|
|
|
// * C420jpeg: biaxially-displaced chroma planes
|
|
|
|
// * C420paldv: coincident R and vertically-displaced B
|
|
|
|
// * C420mpeg2: vertically-displaced chroma planes
|
|
|
|
if (iformat == 420 && (g_strcmp0 (end, "jpeg") == 0 ||
|
|
|
|
g_strcmp0 (end, "paldv") == 0 || g_strcmp0 (end, "mpeg2") == 0))
|
|
|
|
return GST_VIDEO_FORMAT_I420;
|
|
|
|
|
|
|
|
if (iformat == 0 && strncmp (end, "mono", 4) == 0) {
|
|
|
|
char *type = end + 4;
|
|
|
|
if (*type == '\0')
|
|
|
|
return GST_VIDEO_FORMAT_GRAY8;
|
|
|
|
if (g_strcmp0 (type, "16") == 0)
|
|
|
|
return GST_VIDEO_FORMAT_GRAY16_LE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*end == 'p') {
|
|
|
|
guint depth = g_ascii_strtoull (end + 1, NULL, 10);
|
|
|
|
if (depth == 10) {
|
|
|
|
switch (iformat) {
|
|
|
|
case 420:
|
|
|
|
return GST_VIDEO_FORMAT_I420_10LE;
|
|
|
|
case 422:
|
|
|
|
return GST_VIDEO_FORMAT_I422_10LE;
|
|
|
|
case 444:
|
|
|
|
return GST_VIDEO_FORMAT_Y444_10LE;
|
|
|
|
}
|
|
|
|
} else if (depth == 12) {
|
|
|
|
switch (iformat) {
|
|
|
|
case 420:
|
|
|
|
return GST_VIDEO_FORMAT_I420_12LE;
|
|
|
|
case 422:
|
|
|
|
return GST_VIDEO_FORMAT_I422_12LE;
|
|
|
|
case 444:
|
|
|
|
return GST_VIDEO_FORMAT_Y444_12LE;
|
|
|
|
}
|
|
|
|
} else if (depth == 16 && iformat == 444) {
|
|
|
|
return GST_VIDEO_FORMAT_Y444_16LE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_WARNING ("%s is not a supported format", param);
|
|
|
|
return GST_VIDEO_FORMAT_UNKNOWN;
|
2024-01-25 22:14:44 +00:00
|
|
|
}
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
static gboolean
|
2024-01-25 22:14:44 +00:00
|
|
|
parse_ratio (const char *param, gulong * n, gulong * d)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
|
|
|
char *end;
|
2024-01-25 22:56:07 +00:00
|
|
|
*n = g_ascii_strtoull (param, &end, 10);
|
2024-01-25 22:14:44 +00:00
|
|
|
if (end == param)
|
|
|
|
return FALSE;
|
|
|
|
param = end;
|
|
|
|
if (param[0] != ':')
|
|
|
|
return FALSE;
|
|
|
|
param++;
|
2024-01-25 22:56:07 +00:00
|
|
|
*d = g_ascii_strtoull (param, &end, 10);
|
2024-01-25 22:14:44 +00:00
|
|
|
if (end == param)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_y4m_dec_parse_header (GstY4mDec * y4mdec, char *header)
|
|
|
|
{
|
|
|
|
guint len;
|
|
|
|
char **params;
|
|
|
|
guint interlaced_char = 0;
|
|
|
|
gulong fps_n = 0, fps_d = 0;
|
|
|
|
gulong par_n = 0, par_d = 0;
|
|
|
|
gulong width = 0, height = 0;
|
|
|
|
GstVideoFormat format = GST_VIDEO_FORMAT_I420;
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
if (memcmp (header, "YUV4MPEG2 ", 10) != 0) {
|
2024-01-25 22:14:44 +00:00
|
|
|
GST_ERROR_OBJECT (y4mdec, "y4m start code not found");
|
2010-12-14 07:32:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
header += 10;
|
2024-01-25 22:56:07 +00:00
|
|
|
if (!g_str_is_ascii (header)) {
|
|
|
|
GST_ERROR_OBJECT (y4mdec, "Invalid non-ASCII y4m header: %s", header);
|
2024-01-25 22:14:44 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2024-01-25 22:56:07 +00:00
|
|
|
GST_INFO_OBJECT (y4mdec, "Found header: %s", header);
|
|
|
|
params = g_strsplit (header, " ", -1);
|
2024-01-25 22:14:44 +00:00
|
|
|
len = g_strv_length (params);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
const char *param = params[i];
|
|
|
|
char param_type = *param;
|
|
|
|
const char *param_value = param + 1;
|
|
|
|
switch (param_type) {
|
2010-12-14 07:32:30 +00:00
|
|
|
case 'C':
|
2024-01-25 22:56:07 +00:00
|
|
|
format = parse_colorspace (param_value);
|
|
|
|
if (format == GST_VIDEO_FORMAT_UNKNOWN) {
|
2024-01-25 22:14:44 +00:00
|
|
|
GST_ERROR_OBJECT (y4mdec, "Failed to parse colorspace: %s", param);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2024-01-25 22:56:07 +00:00
|
|
|
GST_INFO_OBJECT (y4mdec, "Parsed format as %s",
|
|
|
|
gst_video_format_to_string (format));
|
2024-01-25 22:14:44 +00:00
|
|
|
continue;
|
2010-12-14 07:32:30 +00:00
|
|
|
case 'W':
|
2024-01-25 22:56:07 +00:00
|
|
|
if ((width = g_ascii_strtoull (param_value, NULL, 10)) == 0) {
|
2024-01-25 22:14:44 +00:00
|
|
|
GST_ERROR_OBJECT (y4mdec, "Failed to parse width: %s", param);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
continue;
|
2010-12-14 07:32:30 +00:00
|
|
|
case 'H':
|
2024-01-25 22:56:07 +00:00
|
|
|
if ((height = g_ascii_strtoull (param_value, NULL, 10)) == 0) {
|
2024-01-25 22:14:44 +00:00
|
|
|
GST_ERROR_OBJECT (y4mdec, "Failed to parse height: %s", param);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
continue;
|
2010-12-14 07:32:30 +00:00
|
|
|
case 'I':
|
2024-01-25 22:14:44 +00:00
|
|
|
if ((interlaced_char = param_value[0]) == 0) {
|
|
|
|
GST_ERROR_OBJECT (y4mdec, "Expecting interlaced flag: %s", param);
|
2010-12-14 07:32:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2024-01-25 22:14:44 +00:00
|
|
|
continue;
|
2010-12-14 07:32:30 +00:00
|
|
|
case 'F':
|
2024-01-25 22:14:44 +00:00
|
|
|
if (!parse_ratio (param_value, &fps_n, &fps_d)) {
|
|
|
|
GST_ERROR_OBJECT (y4mdec, "Failed to parse framerate: %s", param);
|
2010-12-14 07:32:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2024-01-25 22:14:44 +00:00
|
|
|
continue;
|
2010-12-14 07:32:30 +00:00
|
|
|
case 'A':
|
2024-01-25 22:14:44 +00:00
|
|
|
if (!parse_ratio (param_value, &par_n, &par_d)) {
|
|
|
|
GST_ERROR_OBJECT (y4mdec, "Failed to parse PAR: %s", param);
|
2010-12-14 07:32:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2024-01-25 22:14:44 +00:00
|
|
|
continue;
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
2024-01-25 22:14:44 +00:00
|
|
|
GST_WARNING_OBJECT (y4mdec, "Unknown y4m param field '%s', ignoring",
|
|
|
|
param);
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
2024-01-25 22:14:44 +00:00
|
|
|
g_strfreev (params);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2024-01-25 22:14:44 +00:00
|
|
|
if (width > MAX_SIZE || height > MAX_SIZE) {
|
2024-01-25 23:27:21 +00:00
|
|
|
GST_ERROR_OBJECT (y4mdec, "Dimensions %lux%lu out of range", width, height);
|
2010-12-14 07:32:30 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-18 11:17:27 +00:00
|
|
|
gst_video_info_init (&y4mdec->info);
|
2013-03-22 14:49:18 +00:00
|
|
|
gst_video_info_set_format (&y4mdec->out_info, format, width, height);
|
|
|
|
y4mdec->info = y4mdec->out_info;
|
|
|
|
|
|
|
|
switch (y4mdec->info.finfo->format) {
|
|
|
|
case GST_VIDEO_FORMAT_I420:
|
|
|
|
y4mdec->info.offset[0] = 0;
|
|
|
|
y4mdec->info.stride[0] = width;
|
|
|
|
y4mdec->info.offset[1] = y4mdec->info.stride[0] * height;
|
|
|
|
y4mdec->info.stride[1] = GST_ROUND_UP_2 (width) / 2;
|
|
|
|
y4mdec->info.offset[2] =
|
|
|
|
y4mdec->info.offset[1] +
|
|
|
|
y4mdec->info.stride[1] * (GST_ROUND_UP_2 (height) / 2);
|
|
|
|
y4mdec->info.stride[2] = GST_ROUND_UP_2 (width) / 2;
|
|
|
|
y4mdec->info.size =
|
|
|
|
y4mdec->info.offset[2] +
|
|
|
|
y4mdec->info.stride[2] * (GST_ROUND_UP_2 (height) / 2);
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_Y42B:
|
|
|
|
y4mdec->info.offset[0] = 0;
|
|
|
|
y4mdec->info.stride[0] = width;
|
|
|
|
y4mdec->info.offset[1] = y4mdec->info.stride[0] * height;
|
|
|
|
y4mdec->info.stride[1] = GST_ROUND_UP_2 (width) / 2;
|
|
|
|
y4mdec->info.offset[2] =
|
|
|
|
y4mdec->info.offset[1] + y4mdec->info.stride[1] * height;
|
|
|
|
y4mdec->info.stride[2] = GST_ROUND_UP_2 (width) / 2;
|
|
|
|
y4mdec->info.size =
|
|
|
|
y4mdec->info.offset[2] + y4mdec->info.stride[2] * height;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_Y444:
|
|
|
|
y4mdec->info.offset[0] = 0;
|
|
|
|
y4mdec->info.stride[0] = width;
|
|
|
|
y4mdec->info.offset[1] = y4mdec->info.stride[0] * height;
|
|
|
|
y4mdec->info.stride[1] = width;
|
|
|
|
y4mdec->info.offset[2] =
|
|
|
|
y4mdec->info.offset[1] + y4mdec->info.stride[1] * height;
|
|
|
|
y4mdec->info.stride[2] = width;
|
|
|
|
y4mdec->info.size =
|
|
|
|
y4mdec->info.offset[2] + y4mdec->info.stride[2] * height;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
switch (interlaced_char) {
|
|
|
|
case 0:
|
|
|
|
case '?':
|
|
|
|
case 'p':
|
2011-12-19 17:44:33 +00:00
|
|
|
y4mdec->info.interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
case 'b':
|
2011-12-19 17:44:33 +00:00
|
|
|
y4mdec->info.interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-01-25 23:27:21 +00:00
|
|
|
GST_ERROR_OBJECT (y4mdec, "Unknown interlaced char '%c'",
|
2010-12-14 07:32:30 +00:00
|
|
|
interlaced_char);
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-16 15:41:45 +00:00
|
|
|
if (fps_n == 0)
|
|
|
|
fps_n = 1;
|
|
|
|
if (fps_d == 0)
|
|
|
|
fps_d = 1;
|
|
|
|
if (par_n == 0)
|
|
|
|
par_n = 1;
|
|
|
|
if (par_d == 0)
|
|
|
|
par_d = 1;
|
|
|
|
|
|
|
|
y4mdec->info.fps_n = fps_n;
|
|
|
|
y4mdec->info.fps_d = fps_d;
|
|
|
|
y4mdec->info.par_n = par_n;
|
|
|
|
y4mdec->info.par_d = par_d;
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-17 14:53:42 +00:00
|
|
|
gst_y4m_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
|
|
|
GstY4mDec *y4mdec;
|
|
|
|
int n_avail;
|
|
|
|
GstFlowReturn flow_ret = GST_FLOW_OK;
|
|
|
|
#define MAX_HEADER_LENGTH 80
|
|
|
|
char header[MAX_HEADER_LENGTH];
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
|
2011-11-17 14:53:42 +00:00
|
|
|
y4mdec = GST_Y4M_DEC (parent);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (y4mdec, "chain");
|
|
|
|
|
|
|
|
if (GST_BUFFER_IS_DISCONT (buffer)) {
|
|
|
|
GST_DEBUG ("got discont");
|
|
|
|
gst_adapter_clear (y4mdec->adapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_adapter_push (y4mdec->adapter, buffer);
|
|
|
|
n_avail = gst_adapter_available (y4mdec->adapter);
|
|
|
|
|
|
|
|
if (!y4mdec->have_header) {
|
|
|
|
gboolean ret;
|
|
|
|
GstCaps *caps;
|
2013-03-22 14:49:18 +00:00
|
|
|
GstQuery *query;
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
if (n_avail < MAX_HEADER_LENGTH)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
gst_adapter_copy (y4mdec->adapter, (guint8 *) header, 0, MAX_HEADER_LENGTH);
|
|
|
|
|
|
|
|
header[MAX_HEADER_LENGTH - 1] = 0;
|
|
|
|
for (i = 0; i < MAX_HEADER_LENGTH; i++) {
|
|
|
|
if (header[i] == 0x0a)
|
|
|
|
header[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_y4m_dec_parse_header (y4mdec, header);
|
|
|
|
if (!ret) {
|
|
|
|
GST_ELEMENT_ERROR (y4mdec, STREAM, DECODE,
|
|
|
|
("Failed to parse YUV4MPEG header"), (NULL));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
y4mdec->header_size = strlen (header) + 1;
|
|
|
|
gst_adapter_flush (y4mdec->adapter, y4mdec->header_size);
|
|
|
|
|
2011-10-16 15:41:45 +00:00
|
|
|
caps = gst_video_info_to_caps (&y4mdec->info);
|
2010-12-14 07:32:30 +00:00
|
|
|
ret = gst_pad_set_caps (y4mdec->srcpad, caps);
|
2013-03-22 14:49:18 +00:00
|
|
|
|
|
|
|
query = gst_query_new_allocation (caps, FALSE);
|
|
|
|
y4mdec->video_meta = FALSE;
|
|
|
|
|
|
|
|
if (y4mdec->pool) {
|
|
|
|
gst_buffer_pool_set_active (y4mdec->pool, FALSE);
|
|
|
|
gst_object_unref (y4mdec->pool);
|
|
|
|
}
|
|
|
|
y4mdec->pool = NULL;
|
|
|
|
|
|
|
|
if (gst_pad_peer_query (y4mdec->srcpad, query)) {
|
|
|
|
y4mdec->video_meta =
|
2014-03-15 14:17:43 +00:00
|
|
|
gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
|
2013-03-22 14:49:18 +00:00
|
|
|
|
|
|
|
/* We only need a pool if we need to do stride conversion for downstream */
|
|
|
|
if (!y4mdec->video_meta && memcmp (&y4mdec->info, &y4mdec->out_info,
|
|
|
|
sizeof (y4mdec->info)) != 0) {
|
|
|
|
GstBufferPool *pool = NULL;
|
|
|
|
GstAllocator *allocator = NULL;
|
|
|
|
GstAllocationParams params;
|
|
|
|
GstStructure *config;
|
|
|
|
guint size, min, max;
|
|
|
|
|
|
|
|
if (gst_query_get_n_allocation_params (query) > 0) {
|
|
|
|
gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
|
|
|
|
} else {
|
|
|
|
allocator = NULL;
|
|
|
|
gst_allocation_params_init (¶ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gst_query_get_n_allocation_pools (query) > 0) {
|
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min,
|
|
|
|
&max);
|
|
|
|
size = MAX (size, y4mdec->out_info.size);
|
|
|
|
} else {
|
|
|
|
pool = NULL;
|
|
|
|
size = y4mdec->out_info.size;
|
|
|
|
min = max = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool == NULL) {
|
|
|
|
pool = gst_video_buffer_pool_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, min, max);
|
|
|
|
gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
|
|
|
|
gst_buffer_pool_set_config (pool, config);
|
|
|
|
|
|
|
|
if (allocator)
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
|
|
|
|
y4mdec->pool = pool;
|
|
|
|
}
|
|
|
|
} else if (memcmp (&y4mdec->info, &y4mdec->out_info,
|
|
|
|
sizeof (y4mdec->info)) != 0) {
|
|
|
|
GstBufferPool *pool;
|
|
|
|
GstStructure *config;
|
|
|
|
|
|
|
|
/* No pool, create our own if we need to do stride conversion */
|
|
|
|
pool = gst_video_buffer_pool_new ();
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, y4mdec->out_info.size, 0,
|
|
|
|
0);
|
|
|
|
gst_buffer_pool_set_config (pool, config);
|
|
|
|
y4mdec->pool = pool;
|
|
|
|
}
|
|
|
|
if (y4mdec->pool) {
|
|
|
|
gst_buffer_pool_set_active (y4mdec->pool, TRUE);
|
|
|
|
}
|
|
|
|
gst_query_unref (query);
|
2010-12-14 07:32:30 +00:00
|
|
|
gst_caps_unref (caps);
|
|
|
|
if (!ret) {
|
|
|
|
GST_DEBUG_OBJECT (y4mdec, "Couldn't set caps on src pad");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
y4mdec->have_header = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y4mdec->have_new_segment) {
|
|
|
|
GstEvent *event;
|
|
|
|
GstClockTime start = gst_y4m_dec_bytes_to_timestamp (y4mdec,
|
2011-10-16 15:41:45 +00:00
|
|
|
y4mdec->segment.start);
|
2010-12-14 07:32:30 +00:00
|
|
|
GstClockTime stop = gst_y4m_dec_bytes_to_timestamp (y4mdec,
|
2011-10-16 15:41:45 +00:00
|
|
|
y4mdec->segment.stop);
|
|
|
|
GstClockTime time = gst_y4m_dec_bytes_to_timestamp (y4mdec,
|
|
|
|
y4mdec->segment.time);
|
|
|
|
GstSegment seg;
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2011-10-16 15:41:45 +00:00
|
|
|
gst_segment_init (&seg, GST_FORMAT_TIME);
|
|
|
|
seg.start = start;
|
|
|
|
seg.stop = stop;
|
|
|
|
seg.time = time;
|
|
|
|
event = gst_event_new_segment (&seg);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
gst_pad_push_event (y4mdec->srcpad, event);
|
|
|
|
//gst_event_unref (event);
|
|
|
|
|
|
|
|
y4mdec->have_new_segment = FALSE;
|
|
|
|
y4mdec->frame_index = gst_y4m_dec_bytes_to_frames (y4mdec,
|
2011-10-16 15:41:45 +00:00
|
|
|
y4mdec->segment.time);
|
2010-12-14 07:32:30 +00:00
|
|
|
GST_DEBUG ("new frame_index %d", y4mdec->frame_index);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n_avail = gst_adapter_available (y4mdec->adapter);
|
|
|
|
if (n_avail < MAX_HEADER_LENGTH)
|
|
|
|
break;
|
|
|
|
|
|
|
|
gst_adapter_copy (y4mdec->adapter, (guint8 *) header, 0, MAX_HEADER_LENGTH);
|
|
|
|
header[MAX_HEADER_LENGTH - 1] = 0;
|
|
|
|
for (i = 0; i < MAX_HEADER_LENGTH; i++) {
|
|
|
|
if (header[i] == 0x0a)
|
|
|
|
header[i] = 0;
|
|
|
|
}
|
|
|
|
if (memcmp (header, "FRAME", 5) != 0) {
|
|
|
|
GST_ELEMENT_ERROR (y4mdec, STREAM, DECODE,
|
|
|
|
("Failed to parse YUV4MPEG frame"), (NULL));
|
|
|
|
flow_ret = GST_FLOW_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen (header);
|
2011-10-16 15:41:45 +00:00
|
|
|
if (n_avail < y4mdec->info.size + len + 1) {
|
2010-12-14 07:32:30 +00:00
|
|
|
/* not enough data */
|
2024-01-25 23:27:21 +00:00
|
|
|
GST_TRACE ("not enough data for frame %d < %" G_GSIZE_FORMAT,
|
2011-10-16 15:41:45 +00:00
|
|
|
n_avail, y4mdec->info.size + len + 1);
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_adapter_flush (y4mdec->adapter, len + 1);
|
|
|
|
|
2011-10-16 15:41:45 +00:00
|
|
|
buffer = gst_adapter_take_buffer (y4mdec->adapter, y4mdec->info.size);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) =
|
|
|
|
gst_y4m_dec_frames_to_timestamp (y4mdec, y4mdec->frame_index);
|
|
|
|
GST_BUFFER_DURATION (buffer) =
|
2011-06-27 02:29:24 +00:00
|
|
|
gst_y4m_dec_frames_to_timestamp (y4mdec, y4mdec->frame_index + 1) -
|
2010-12-14 07:32:30 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer);
|
|
|
|
|
|
|
|
y4mdec->frame_index++;
|
|
|
|
|
2013-03-22 14:49:18 +00:00
|
|
|
if (y4mdec->video_meta) {
|
|
|
|
gst_buffer_add_video_meta_full (buffer, 0, y4mdec->info.finfo->format,
|
|
|
|
y4mdec->info.width, y4mdec->info.height, y4mdec->info.finfo->n_planes,
|
|
|
|
y4mdec->info.offset, y4mdec->info.stride);
|
|
|
|
} else if (memcmp (&y4mdec->info, &y4mdec->out_info,
|
|
|
|
sizeof (y4mdec->info)) != 0) {
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
GstVideoFrame iframe, oframe;
|
|
|
|
gint i, j;
|
|
|
|
gint w, h, istride, ostride;
|
|
|
|
guint8 *src, *dest;
|
|
|
|
|
|
|
|
/* Allocate a new buffer and do stride conversion */
|
|
|
|
g_assert (y4mdec->pool != NULL);
|
|
|
|
|
|
|
|
flow_ret = gst_buffer_pool_acquire_buffer (y4mdec->pool, &outbuf, NULL);
|
|
|
|
if (flow_ret != GST_FLOW_OK) {
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_frame_map (&iframe, &y4mdec->info, buffer, GST_MAP_READ);
|
|
|
|
gst_video_frame_map (&oframe, &y4mdec->out_info, outbuf, GST_MAP_WRITE);
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
2015-03-09 03:51:21 +00:00
|
|
|
w = GST_VIDEO_FRAME_COMP_WIDTH (&iframe, i);
|
|
|
|
h = GST_VIDEO_FRAME_COMP_HEIGHT (&iframe, i);
|
|
|
|
istride = GST_VIDEO_FRAME_COMP_STRIDE (&iframe, i);
|
|
|
|
ostride = GST_VIDEO_FRAME_COMP_STRIDE (&oframe, i);
|
2013-03-22 14:49:18 +00:00
|
|
|
src = GST_VIDEO_FRAME_COMP_DATA (&iframe, i);
|
|
|
|
dest = GST_VIDEO_FRAME_COMP_DATA (&oframe, i);
|
|
|
|
|
|
|
|
for (j = 0; j < h; j++) {
|
|
|
|
memcpy (dest, src, w);
|
|
|
|
|
|
|
|
dest += ostride;
|
|
|
|
src += istride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_frame_unmap (&iframe);
|
|
|
|
gst_video_frame_unmap (&oframe);
|
2013-04-27 18:56:48 +00:00
|
|
|
gst_buffer_copy_into (outbuf, buffer, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
|
2013-03-22 14:49:18 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
buffer = outbuf;
|
|
|
|
}
|
|
|
|
|
2010-12-14 07:32:30 +00:00
|
|
|
flow_ret = gst_pad_push (y4mdec->srcpad, buffer);
|
|
|
|
if (flow_ret != GST_FLOW_OK)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG ("returning %d", flow_ret);
|
2011-11-17 14:53:42 +00:00
|
|
|
|
2010-12-14 07:32:30 +00:00
|
|
|
return flow_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-17 14:53:42 +00:00
|
|
|
gst_y4m_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
GstY4mDec *y4mdec;
|
|
|
|
|
2011-11-17 14:53:42 +00:00
|
|
|
y4mdec = GST_Y4M_DEC (parent);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (y4mdec, "event");
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_FLUSH_START:
|
|
|
|
res = gst_pad_push_event (y4mdec->srcpad, event);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
res = gst_pad_push_event (y4mdec->srcpad, event);
|
|
|
|
break;
|
2011-10-16 15:41:45 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
2011-10-16 15:41:45 +00:00
|
|
|
GstSegment seg;
|
|
|
|
|
|
|
|
gst_event_copy_segment (event, &seg);
|
|
|
|
|
|
|
|
GST_DEBUG ("segment: %" GST_SEGMENT_FORMAT, &seg);
|
|
|
|
|
|
|
|
if (seg.format == GST_FORMAT_BYTES) {
|
|
|
|
y4mdec->segment = seg;
|
2010-12-14 07:32:30 +00:00
|
|
|
y4mdec->have_new_segment = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = TRUE;
|
2012-01-12 15:19:01 +00:00
|
|
|
/* not sure why it's not forwarded, but let's unref it so it
|
|
|
|
doesn't leak, remove the unref if it gets forwarded again */
|
|
|
|
gst_event_unref (event);
|
2010-12-14 07:32:30 +00:00
|
|
|
//res = gst_pad_push_event (y4mdec->srcpad, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
default:
|
2013-03-22 13:52:54 +00:00
|
|
|
res = gst_pad_event_default (pad, parent, event);
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-17 14:53:42 +00:00
|
|
|
gst_y4m_dec_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
GstY4mDec *y4mdec;
|
|
|
|
|
2011-11-17 14:53:42 +00:00
|
|
|
y4mdec = GST_Y4M_DEC (parent);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (y4mdec, "event");
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_SEEK:
|
|
|
|
{
|
|
|
|
gdouble rate;
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
2013-03-22 13:37:37 +00:00
|
|
|
gint64 framenum;
|
2010-12-14 07:32:30 +00:00
|
|
|
guint64 byte;
|
|
|
|
|
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags, &start_type,
|
|
|
|
&start, &stop_type, &stop);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME) {
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
framenum = gst_y4m_dec_timestamp_to_frames (y4mdec, start);
|
2013-03-22 13:37:37 +00:00
|
|
|
GST_DEBUG ("seeking to frame %" G_GINT64_FORMAT, framenum);
|
|
|
|
if (framenum == -1) {
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
byte = gst_y4m_dec_frames_to_bytes (y4mdec, framenum);
|
2013-03-22 13:37:37 +00:00
|
|
|
GST_DEBUG ("offset %" G_GUINT64_FORMAT, (guint64) byte);
|
|
|
|
if (byte == -1) {
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
|
|
|
|
start_type, byte, stop_type, -1);
|
|
|
|
|
|
|
|
res = gst_pad_push_event (y4mdec->sinkpad, event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-22 13:52:54 +00:00
|
|
|
res = gst_pad_event_default (pad, parent, event);
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-16 16:28:10 +00:00
|
|
|
gst_y4m_dec_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
|
2010-12-14 07:32:30 +00:00
|
|
|
{
|
2011-11-16 16:28:10 +00:00
|
|
|
GstY4mDec *y4mdec = GST_Y4M_DEC (parent);
|
2010-12-14 07:32:30 +00:00
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_DURATION:
|
|
|
|
{
|
|
|
|
GstFormat format;
|
2011-11-16 16:28:10 +00:00
|
|
|
GstQuery *peer_query;
|
2010-12-14 07:32:30 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("duration query");
|
|
|
|
|
|
|
|
gst_query_parse_duration (query, &format, NULL);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME) {
|
|
|
|
res = FALSE;
|
|
|
|
GST_DEBUG_OBJECT (y4mdec, "not handling duration query in format %d",
|
|
|
|
format);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-16 16:28:10 +00:00
|
|
|
peer_query = gst_query_new_duration (GST_FORMAT_BYTES);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2011-11-16 16:28:10 +00:00
|
|
|
res = gst_pad_peer_query (y4mdec->sinkpad, peer_query);
|
|
|
|
if (res) {
|
|
|
|
gint64 duration;
|
|
|
|
int n_frames;
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2011-11-16 16:28:10 +00:00
|
|
|
gst_query_parse_duration (peer_query, &format, &duration);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2011-11-16 16:28:10 +00:00
|
|
|
n_frames = gst_y4m_dec_bytes_to_frames (y4mdec, duration);
|
|
|
|
GST_DEBUG ("duration in frames %d", n_frames);
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2011-11-16 16:28:10 +00:00
|
|
|
duration = gst_y4m_dec_frames_to_timestamp (y4mdec, n_frames);
|
|
|
|
GST_DEBUG ("duration in time %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (duration));
|
2010-12-14 07:32:30 +00:00
|
|
|
|
2011-11-16 16:28:10 +00:00
|
|
|
gst_query_set_duration (query, GST_FORMAT_TIME, duration);
|
|
|
|
res = TRUE;
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
2011-11-16 16:28:10 +00:00
|
|
|
gst_query_unref (peer_query);
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2011-11-16 16:28:10 +00:00
|
|
|
res = gst_pad_query_default (pad, parent, query);
|
2010-12-14 07:32:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
2021-02-25 14:22:15 +00:00
|
|
|
return GST_ELEMENT_REGISTER (y4mdec, plugin);
|
2010-12-14 07:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
2012-04-05 16:02:56 +00:00
|
|
|
y4mdec,
|
2011-04-30 09:28:03 +00:00
|
|
|
"Demuxes/decodes YUV4MPEG streams",
|
|
|
|
plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)
|