2003-04-22 14:55:12 +00:00
|
|
|
/* GStreamer xvid decoder plugin
|
|
|
|
* Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "gstxviddec.h"
|
|
|
|
#include <gst/video/video.h>
|
|
|
|
|
|
|
|
/* elementfactory information */
|
|
|
|
GstElementDetails gst_xviddec_details = {
|
|
|
|
"Xvid decoder",
|
|
|
|
"Codec/Video/Decoder",
|
|
|
|
"Xvid decoder based on xviddecore",
|
|
|
|
"Ronald Bultje <rbultje@ronald.bitfreak.net>",
|
|
|
|
};
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE (
|
2003-04-22 14:55:12 +00:00
|
|
|
"sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_STATIC_CAPS (
|
|
|
|
"video/x-xvid, "
|
|
|
|
"width = (int) [ 0, MAX ], "
|
|
|
|
"height = (int) [ 0, MAX ], "
|
|
|
|
"framerate = (double) [ 0, MAX ]"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE (
|
2003-04-22 14:55:12 +00:00
|
|
|
"src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_STATIC_CAPS (
|
|
|
|
GST_VIDEO_YUV_PAD_TEMPLATE_CAPS ("{ I420, YUY2, YV12, YVYU, UYVY }") "; "
|
|
|
|
GST_VIDEO_RGB_PAD_TEMPLATE_CAPS_24_32 "; "
|
|
|
|
GST_VIDEO_RGB_PAD_TEMPLATE_CAPS_15_16
|
2003-07-06 20:49:52 +00:00
|
|
|
)
|
2003-12-22 01:47:09 +00:00
|
|
|
);
|
2003-04-22 14:55:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* XvidDec signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0
|
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
2003-11-02 02:31:04 +00:00
|
|
|
static void gst_xviddec_base_init (gpointer g_class);
|
2003-04-22 14:55:12 +00:00
|
|
|
static void gst_xviddec_class_init (GstXvidDecClass *klass);
|
|
|
|
static void gst_xviddec_init (GstXvidDec *xviddec);
|
|
|
|
static void gst_xviddec_dispose (GObject *object);
|
|
|
|
static void gst_xviddec_chain (GstPad *pad,
|
2003-10-09 19:52:36 +00:00
|
|
|
GstData *data);
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstPadLinkReturn gst_xviddec_link (GstPad *pad,
|
|
|
|
const GstCaps *vscapslist);
|
|
|
|
static GstPadLinkReturn gst_xviddec_negotiate (GstXvidDec *xviddec);
|
2003-04-22 14:55:12 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
/* static guint gst_xviddec_signals[LAST_SIGNAL] = { 0 }; */
|
|
|
|
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_xviddec_get_type(void)
|
|
|
|
{
|
|
|
|
static GType xviddec_type = 0;
|
|
|
|
|
|
|
|
if (!xviddec_type)
|
|
|
|
{
|
|
|
|
static const GTypeInfo xviddec_info = {
|
|
|
|
sizeof(GstXvidDecClass),
|
2003-11-02 02:31:04 +00:00
|
|
|
gst_xviddec_base_init,
|
2003-04-22 14:55:12 +00:00
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_xviddec_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof(GstXvidDec),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_xviddec_init,
|
|
|
|
};
|
|
|
|
xviddec_type = g_type_register_static(GST_TYPE_ELEMENT,
|
|
|
|
"GstXvidDec",
|
|
|
|
&xviddec_info, 0);
|
|
|
|
}
|
|
|
|
return xviddec_type;
|
|
|
|
}
|
|
|
|
|
2003-11-02 02:31:04 +00:00
|
|
|
static void
|
|
|
|
gst_xviddec_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sink_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&src_template));
|
2003-11-02 02:31:04 +00:00
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &gst_xviddec_details);
|
|
|
|
}
|
2003-04-22 14:55:12 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xviddec_class_init (GstXvidDecClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
2003-07-29 11:59:09 +00:00
|
|
|
|
|
|
|
gst_xvid_init();
|
2003-04-22 14:55:12 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
gobject_class->dispose = gst_xviddec_dispose;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xviddec_init (GstXvidDec *xviddec)
|
|
|
|
{
|
|
|
|
/* create the sink pad */
|
|
|
|
xviddec->sinkpad = gst_pad_new_from_template(
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&sink_template),
|
2003-04-22 14:55:12 +00:00
|
|
|
"sink");
|
|
|
|
gst_element_add_pad(GST_ELEMENT(xviddec), xviddec->sinkpad);
|
|
|
|
|
|
|
|
gst_pad_set_chain_function(xviddec->sinkpad, gst_xviddec_chain);
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_pad_set_link_function(xviddec->sinkpad, gst_xviddec_link);
|
2003-04-22 14:55:12 +00:00
|
|
|
|
|
|
|
/* create the src pad */
|
|
|
|
xviddec->srcpad = gst_pad_new_from_template(
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&src_template),
|
2003-04-22 14:55:12 +00:00
|
|
|
"src");
|
|
|
|
gst_element_add_pad(GST_ELEMENT(xviddec), xviddec->srcpad);
|
|
|
|
|
|
|
|
/* bitrate, etc. */
|
|
|
|
xviddec->width = xviddec->height = xviddec->csp = -1;
|
|
|
|
|
|
|
|
/* set xvid handle to NULL */
|
|
|
|
xviddec->handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xviddec_unset (GstXvidDec *xviddec)
|
|
|
|
{
|
|
|
|
/* unref this instance */
|
|
|
|
xvid_decore(xviddec->handle, XVID_DEC_DESTROY, NULL, NULL);
|
|
|
|
xviddec->handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_xviddec_setup (GstXvidDec *xviddec)
|
|
|
|
{
|
|
|
|
XVID_DEC_PARAM xdec;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* initialise parameters, see xvid documentation */
|
|
|
|
memset(&xdec, 0, sizeof(XVID_DEC_PARAM));
|
|
|
|
xdec.width = xviddec->width;
|
|
|
|
xdec.height = xviddec->height;
|
|
|
|
|
|
|
|
if ((ret = xvid_decore(NULL, XVID_DEC_CREATE,
|
|
|
|
&xdec, NULL)) != XVID_ERR_OK) {
|
2003-09-16 10:00:00 +00:00
|
|
|
gst_element_error(GST_ELEMENT(xviddec),
|
|
|
|
"Setting parameters %dx%d@%d failed: %s (%d)",
|
2003-06-01 15:13:56 +00:00
|
|
|
xviddec->width, xviddec->height, xviddec->csp,
|
2003-09-16 10:00:00 +00:00
|
|
|
gst_xvid_error(ret), ret);
|
2003-04-22 14:55:12 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
xviddec->handle = xdec.handle;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xviddec_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GstXvidDec *xviddec = GST_XVIDDEC(object);
|
|
|
|
|
|
|
|
gst_xviddec_unset(xviddec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_xviddec_chain (GstPad *pad,
|
2003-10-08 16:08:19 +00:00
|
|
|
GstData *_data)
|
2003-04-22 14:55:12 +00:00
|
|
|
{
|
2003-10-08 16:08:19 +00:00
|
|
|
GstBuffer *buf = GST_BUFFER (_data);
|
2003-04-22 14:55:12 +00:00
|
|
|
GstXvidDec *xviddec;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
XVID_DEC_FRAME xframe;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_return_if_fail(pad != NULL);
|
|
|
|
g_return_if_fail(GST_IS_PAD(pad));
|
|
|
|
g_return_if_fail(buf != NULL);
|
|
|
|
|
|
|
|
xviddec = GST_XVIDDEC(GST_OBJECT_PARENT(pad));
|
|
|
|
|
|
|
|
if (!xviddec->handle) {
|
2003-07-06 20:49:52 +00:00
|
|
|
if (!gst_xviddec_negotiate(xviddec)) {
|
2003-09-16 10:00:00 +00:00
|
|
|
gst_element_error(GST_ELEMENT(xviddec),
|
|
|
|
"No format set - aborting");
|
2003-07-06 20:49:52 +00:00
|
|
|
gst_buffer_unref(buf);
|
|
|
|
return;
|
|
|
|
}
|
2003-04-22 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outbuf = gst_buffer_new_and_alloc(xviddec->width *
|
|
|
|
xviddec->height *
|
|
|
|
xviddec->bpp / 8);
|
|
|
|
GST_BUFFER_TIMESTAMP(outbuf) = GST_BUFFER_TIMESTAMP(buf);
|
|
|
|
GST_BUFFER_SIZE(outbuf) = xviddec->width *
|
|
|
|
xviddec->height *
|
|
|
|
xviddec->bpp / 8;
|
|
|
|
|
|
|
|
/* encode and so ... */
|
|
|
|
xframe.bitstream = (void *) GST_BUFFER_DATA(buf);
|
|
|
|
xframe.image = (void *) GST_BUFFER_DATA(outbuf);
|
|
|
|
xframe.length = GST_BUFFER_SIZE(buf);
|
2003-07-14 06:31:26 +00:00
|
|
|
xframe.stride = 0; /*xviddec->width * xviddec->bpp / 8;*/
|
2003-04-22 14:55:12 +00:00
|
|
|
xframe.colorspace = xviddec->csp;
|
|
|
|
|
|
|
|
if ((ret = xvid_decore(xviddec->handle, XVID_DEC_DECODE,
|
|
|
|
&xframe, NULL))) {
|
2003-09-16 10:00:00 +00:00
|
|
|
gst_element_error(GST_ELEMENT(xviddec),
|
|
|
|
"Error decoding xvid frame: %s (%d)\n",
|
|
|
|
gst_xvid_error(ret), ret);
|
2003-04-22 14:55:12 +00:00
|
|
|
gst_buffer_unref(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-10-08 16:08:19 +00:00
|
|
|
gst_pad_push(xviddec->srcpad, GST_DATA (outbuf));
|
2003-04-22 14:55:12 +00:00
|
|
|
gst_buffer_unref(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstPadLinkReturn
|
2003-07-06 20:49:52 +00:00
|
|
|
gst_xviddec_negotiate (GstXvidDec *xviddec)
|
2003-04-22 14:55:12 +00:00
|
|
|
{
|
2003-07-06 20:49:52 +00:00
|
|
|
GstPadLinkReturn ret;
|
2003-04-22 14:55:12 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
struct {
|
|
|
|
guint32 fourcc;
|
|
|
|
gint depth, bpp;
|
|
|
|
gint csp;
|
|
|
|
} fmt_list[] = {
|
|
|
|
{ GST_MAKE_FOURCC('Y','U','Y','V'), 16, 16, XVID_CSP_YUY2 },
|
|
|
|
{ GST_MAKE_FOURCC('U','Y','V','Y'), 16, 16, XVID_CSP_UYVY },
|
|
|
|
{ GST_MAKE_FOURCC('Y','V','Y','U'), 16, 16, XVID_CSP_YVYU },
|
|
|
|
{ GST_MAKE_FOURCC('Y','V','1','2'), 12, 12, XVID_CSP_YV12 },
|
2003-07-14 06:31:26 +00:00
|
|
|
{ GST_MAKE_FOURCC('I','4','2','0'), 12, 12, XVID_CSP_I420 },
|
2003-04-22 14:55:12 +00:00
|
|
|
{ GST_MAKE_FOURCC('R','G','B',' '), 32, 32, XVID_CSP_RGB32 },
|
|
|
|
{ GST_MAKE_FOURCC('R','G','B',' '), 24, 24, XVID_CSP_RGB24 },
|
|
|
|
{ GST_MAKE_FOURCC('R','G','B',' '), 16, 16, XVID_CSP_RGB555 },
|
|
|
|
{ GST_MAKE_FOURCC('R','G','B',' '), 15, 16, XVID_CSP_RGB565 },
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
};
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; fmt_list[i].fourcc != 0; i++) {
|
|
|
|
xviddec->csp = fmt_list[i].csp;
|
|
|
|
|
|
|
|
/* try making a caps to set on the other side */
|
|
|
|
if (fmt_list[i].fourcc == GST_MAKE_FOURCC('R','G','B',' ')) {
|
|
|
|
guint32 r_mask = 0, b_mask = 0, g_mask = 0;
|
2003-07-06 20:49:52 +00:00
|
|
|
gint endianness = 0;
|
2003-04-22 14:55:12 +00:00
|
|
|
switch (fmt_list[i].depth) {
|
|
|
|
case 15:
|
2003-07-06 20:49:52 +00:00
|
|
|
endianness = G_BYTE_ORDER;
|
2003-04-22 14:55:12 +00:00
|
|
|
r_mask = 0xf800; g_mask = 0x07c0; b_mask = 0x003e;
|
|
|
|
break;
|
|
|
|
case 16:
|
2003-07-06 20:49:52 +00:00
|
|
|
endianness = G_BYTE_ORDER;
|
2003-12-22 01:47:09 +00:00
|
|
|
r_mask = R_MASK_16_INT; g_mask = G_MASK_16_INT; b_mask = B_MASK_16_INT;
|
2003-04-22 14:55:12 +00:00
|
|
|
break;
|
|
|
|
case 24:
|
2003-07-06 20:49:52 +00:00
|
|
|
endianness = G_BIG_ENDIAN;
|
2003-12-22 01:47:09 +00:00
|
|
|
r_mask = R_MASK_24_INT; g_mask = G_MASK_24_INT; b_mask = B_MASK_24_INT;
|
2003-04-22 14:55:12 +00:00
|
|
|
break;
|
|
|
|
case 32:
|
2003-07-06 20:49:52 +00:00
|
|
|
endianness = G_BIG_ENDIAN;
|
2003-12-22 01:47:09 +00:00
|
|
|
r_mask = R_MASK_32_INT; g_mask = G_MASK_32_INT; b_mask = B_MASK_32_INT;
|
2003-04-22 14:55:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-12-22 01:47:09 +00:00
|
|
|
caps = gst_caps_new_simple (
|
2003-07-06 20:49:52 +00:00
|
|
|
"video/x-raw-rgb",
|
2003-12-22 01:47:09 +00:00
|
|
|
"width", G_TYPE_INT, xviddec->width,
|
|
|
|
"height", G_TYPE_INT, xviddec->height,
|
|
|
|
"depth", G_TYPE_INT, fmt_list[i].depth,
|
|
|
|
"bpp", G_TYPE_INT, fmt_list[i].bpp,
|
|
|
|
"endianness", G_TYPE_INT, endianness,
|
|
|
|
"red_mask", G_TYPE_INT, r_mask,
|
|
|
|
"green_mask", G_TYPE_INT, g_mask,
|
|
|
|
"blue_mask", G_TYPE_INT, b_mask,
|
|
|
|
"framerate", G_TYPE_DOUBLE, xviddec->fps,
|
2003-04-22 14:55:12 +00:00
|
|
|
NULL);
|
|
|
|
} else {
|
2003-12-22 01:47:09 +00:00
|
|
|
caps = gst_caps_new_simple (
|
2003-07-06 20:49:52 +00:00
|
|
|
"video/x-raw-yuv",
|
2003-12-22 01:47:09 +00:00
|
|
|
"width", G_TYPE_INT, xviddec->width,
|
|
|
|
"height", G_TYPE_INT, xviddec->height,
|
|
|
|
"format", GST_TYPE_FOURCC, fmt_list[i].fourcc,
|
|
|
|
"framerate", G_TYPE_DOUBLE, xviddec->fps,
|
2003-04-22 14:55:12 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2003-07-06 20:49:52 +00:00
|
|
|
if ((ret = gst_pad_try_set_caps(xviddec->srcpad, caps)) > 0) {
|
2003-04-22 14:55:12 +00:00
|
|
|
xviddec->csp = fmt_list[i].csp;
|
|
|
|
xviddec->bpp = fmt_list[i].bpp;
|
|
|
|
if (gst_xviddec_setup(xviddec))
|
|
|
|
return GST_PAD_LINK_OK;
|
2003-07-06 20:49:52 +00:00
|
|
|
} else if (ret == GST_PAD_LINK_DELAYED) {
|
|
|
|
return ret; /* don't try further (yet) */
|
2003-04-22 14:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we got here - it's not good */
|
|
|
|
return GST_PAD_LINK_REFUSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-06 20:49:52 +00:00
|
|
|
static GstPadLinkReturn
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_xviddec_link (GstPad *pad,
|
|
|
|
const GstCaps *vscaps)
|
2003-07-06 20:49:52 +00:00
|
|
|
{
|
|
|
|
GstXvidDec *xviddec;
|
2003-12-22 01:47:09 +00:00
|
|
|
GstStructure *structure;
|
2003-07-06 20:49:52 +00:00
|
|
|
|
|
|
|
xviddec = GST_XVIDDEC(gst_pad_get_parent (pad));
|
|
|
|
|
|
|
|
/* if there's something old around, remove it */
|
|
|
|
if (xviddec->handle) {
|
|
|
|
gst_xviddec_unset(xviddec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we get here, we know the input is xvid. we
|
|
|
|
* only need to bother with the output colorspace */
|
2003-12-22 01:47:09 +00:00
|
|
|
structure = gst_caps_get_structure (vscaps, 0);
|
|
|
|
gst_structure_get_int(structure, "width", &xviddec->width);
|
|
|
|
gst_structure_get_int(structure, "height", &xviddec->height);
|
|
|
|
gst_structure_get_double(structure, "framerate", &xviddec->fps);
|
2003-07-06 20:49:52 +00:00
|
|
|
|
|
|
|
return gst_xviddec_negotiate(xviddec);
|
|
|
|
}
|