2002-03-20 21:45:03 +00:00
|
|
|
/* GStreamer
|
2001-12-22 23:26:48 +00:00
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*#define GST_DEBUG_ENABLED*/
|
|
|
|
#include "gstmpegparse.h"
|
|
|
|
|
|
|
|
/* elementfactory information */
|
|
|
|
static GstElementDetails mpeg_parse_details = {
|
|
|
|
"MPEG System Parser",
|
2002-04-20 21:42:51 +00:00
|
|
|
"Codec/Parser",
|
|
|
|
"Parses MPEG1 and MPEG2 System Streams",
|
2001-12-22 23:26:48 +00:00
|
|
|
VERSION,
|
|
|
|
"Erik Walthinsen <omega@cse.ogi.edu>\n"
|
|
|
|
"Wim Taymans <wim.taymans@chello.be>",
|
|
|
|
"(C) 1999",
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CLASS(o) GST_MPEG_PARSE_CLASS (G_OBJECT_GET_CLASS (o))
|
|
|
|
|
|
|
|
/* GstMPEGParse signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
ARG_BIT_RATE,
|
|
|
|
ARG_MPEG2,
|
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
2002-04-11 20:42:25 +00:00
|
|
|
GST_PAD_TEMPLATE_FACTORY (sink_factory,
|
2001-12-22 23:26:48 +00:00
|
|
|
"sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_CAPS_NEW (
|
|
|
|
"mpeg_parse_sink",
|
|
|
|
"video/mpeg",
|
|
|
|
"mpegversion", GST_PROPS_INT_RANGE (1, 2),
|
|
|
|
"systemstream", GST_PROPS_BOOLEAN (TRUE)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2002-04-11 20:42:25 +00:00
|
|
|
GST_PAD_TEMPLATE_FACTORY (src_factory,
|
2001-12-22 23:26:48 +00:00
|
|
|
"src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_CAPS_NEW (
|
|
|
|
"mpeg_parse_src",
|
|
|
|
"video/mpeg",
|
|
|
|
"mpegversion", GST_PROPS_INT_RANGE (1, 2),
|
|
|
|
"systemstream", GST_PROPS_BOOLEAN (TRUE),
|
|
|
|
"parsed", GST_PROPS_BOOLEAN (TRUE)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
static void gst_mpeg_parse_class_init (GstMPEGParseClass *klass);
|
|
|
|
static void gst_mpeg_parse_init (GstMPEGParse *mpeg_parse);
|
2001-12-23 22:37:07 +00:00
|
|
|
static GstElementStateReturn
|
|
|
|
gst_mpeg_parse_change_state (GstElement *element);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
static gboolean gst_mpeg_parse_parse_packhead (GstMPEGParse *mpeg_parse, GstBuffer *buffer);
|
|
|
|
static void gst_mpeg_parse_send_data (GstMPEGParse *mpeg_parse, GstData *data);
|
|
|
|
|
|
|
|
static void gst_mpeg_parse_loop (GstElement *element);
|
|
|
|
|
|
|
|
static void gst_mpeg_parse_get_property (GObject *object, guint prop_id,
|
|
|
|
GValue *value, GParamSpec *pspec);
|
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
/*static guint gst_mpeg_parse_signals[LAST_SIGNAL] = { 0 };*/
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_mpeg_parse_get_type (void)
|
|
|
|
{
|
|
|
|
static GType mpeg_parse_type = 0;
|
|
|
|
|
|
|
|
if (!mpeg_parse_type) {
|
|
|
|
static const GTypeInfo mpeg_parse_info = {
|
|
|
|
sizeof(GstMPEGParseClass), NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc)gst_mpeg_parse_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof(GstMPEGParse),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc)gst_mpeg_parse_init,
|
|
|
|
};
|
|
|
|
mpeg_parse_type = g_type_register_static(GST_TYPE_ELEMENT, "GstMPEGParse", &mpeg_parse_info, 0);
|
|
|
|
}
|
|
|
|
return mpeg_parse_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mpeg_parse_class_init (GstMPEGParseClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass*)klass;
|
|
|
|
gstelement_class = (GstElementClass*)klass;
|
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
|
|
|
|
|
2001-12-22 23:26:48 +00:00
|
|
|
g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BIT_RATE,
|
2002-02-06 23:00:15 +00:00
|
|
|
g_param_spec_uint("bitrate","bitrate","bitrate",
|
2001-12-22 23:26:48 +00:00
|
|
|
0, G_MAXUINT, 0, G_PARAM_READABLE));
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MPEG2,
|
|
|
|
g_param_spec_boolean ("mpeg2", "mpeg2", "is this an mpeg2 stream",
|
|
|
|
FALSE, G_PARAM_READABLE));
|
|
|
|
|
|
|
|
gobject_class->get_property = gst_mpeg_parse_get_property;
|
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
gstelement_class->change_state = gst_mpeg_parse_change_state;
|
|
|
|
|
2001-12-22 23:26:48 +00:00
|
|
|
klass->parse_packhead = gst_mpeg_parse_parse_packhead;
|
|
|
|
klass->parse_syshead = NULL;
|
|
|
|
klass->parse_packet = NULL;
|
|
|
|
klass->parse_pes = NULL;
|
|
|
|
klass->send_data = gst_mpeg_parse_send_data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mpeg_parse_init (GstMPEGParse *mpeg_parse)
|
|
|
|
{
|
|
|
|
mpeg_parse->sinkpad = gst_pad_new_from_template(
|
2002-04-11 20:42:25 +00:00
|
|
|
GST_PAD_TEMPLATE_GET (sink_factory), "sink");
|
2001-12-22 23:26:48 +00:00
|
|
|
gst_element_add_pad(GST_ELEMENT(mpeg_parse),mpeg_parse->sinkpad);
|
|
|
|
gst_element_set_loop_function (GST_ELEMENT (mpeg_parse), gst_mpeg_parse_loop);
|
|
|
|
mpeg_parse->srcpad = gst_pad_new_from_template(
|
2002-04-11 20:42:25 +00:00
|
|
|
GST_PAD_TEMPLATE_GET (src_factory), "src");
|
2001-12-22 23:26:48 +00:00
|
|
|
gst_element_add_pad(GST_ELEMENT(mpeg_parse),mpeg_parse->srcpad);
|
|
|
|
|
|
|
|
/* initialize parser state */
|
|
|
|
mpeg_parse->packetize = NULL;
|
|
|
|
mpeg_parse->next_ts = 0;
|
|
|
|
|
|
|
|
/* zero counters (should be done at RUNNING?) */
|
|
|
|
mpeg_parse->bit_rate = 0;
|
|
|
|
|
|
|
|
GST_FLAG_SET (mpeg_parse, GST_ELEMENT_EVENT_AWARE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mpeg_parse_send_data (GstMPEGParse *mpeg_parse, GstData *data)
|
|
|
|
{
|
|
|
|
if (GST_IS_EVENT (data)) {
|
2001-12-26 20:31:50 +00:00
|
|
|
gst_pad_event_default (mpeg_parse->sinkpad, GST_EVENT (data));
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
guint64 size = GST_BUFFER_SIZE (data);
|
|
|
|
|
2001-12-26 20:14:07 +00:00
|
|
|
if (!GST_PAD_CAPS (mpeg_parse->srcpad)) {
|
|
|
|
gboolean mpeg2 = GST_MPEG_PACKETIZE_IS_MPEG2 (mpeg_parse->packetize);
|
|
|
|
|
2002-01-13 22:27:24 +00:00
|
|
|
gst_pad_try_set_caps (mpeg_parse->srcpad,
|
2001-12-26 20:14:07 +00:00
|
|
|
GST_CAPS_NEW (
|
|
|
|
"mpeg_parse_src",
|
|
|
|
"video/mpeg",
|
|
|
|
"mpegversion", GST_PROPS_INT (mpeg2 ? 2 : 1),
|
|
|
|
"systemstream", GST_PROPS_BOOLEAN (TRUE),
|
|
|
|
"parsed", GST_PROPS_BOOLEAN (TRUE)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2001-12-22 23:26:48 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (data) = mpeg_parse->next_ts;
|
|
|
|
gst_pad_push (mpeg_parse->srcpad, GST_BUFFER (data));
|
|
|
|
mpeg_parse->next_ts += ((size * 1000000.0) / (mpeg_parse->bit_rate));
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "mpeg_parse: next_ts %lld", mpeg_parse->next_ts);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mpeg_parse_parse_packhead (GstMPEGParse *mpeg_parse, GstBuffer *buffer)
|
|
|
|
{
|
|
|
|
guint8 *buf;
|
|
|
|
guint64 scr;
|
|
|
|
guint32 scr1, scr2;
|
2001-12-23 22:37:07 +00:00
|
|
|
guint32 new_rate;
|
2001-12-22 23:26:48 +00:00
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "mpeg_parse: in parse_packhead");
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
buf = GST_BUFFER_DATA (buffer);
|
|
|
|
buf += 4;
|
|
|
|
|
|
|
|
scr1 = GUINT32_FROM_BE (*(guint32*) buf);
|
|
|
|
scr2 = GUINT32_FROM_BE (*(guint32*) (buf+4));
|
|
|
|
|
|
|
|
if (GST_MPEG_PACKETIZE_IS_MPEG2 (mpeg_parse->packetize)) {
|
|
|
|
|
|
|
|
/* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
|
|
|
|
scr = (scr1 & 0x38000000) << 3;
|
|
|
|
scr |= (scr1 & 0x03fff800) << 4;
|
|
|
|
scr |= (scr1 & 0x000003ff) << 5;
|
|
|
|
scr |= (scr2 & 0xf8000000) >> 27;
|
|
|
|
|
|
|
|
buf += 6;
|
2001-12-23 22:37:07 +00:00
|
|
|
new_rate = (GUINT32_FROM_BE ((*(guint32 *) buf)) & 0xfffffc00) >> 10;
|
|
|
|
new_rate *= 133; /* FIXME trial and error */
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
scr = (scr1 & 0x0e000000) << 5;
|
|
|
|
scr |= (scr1 & 0x00fffe00) << 6;
|
|
|
|
scr |= (scr1 & 0x000000ff) << 7;
|
|
|
|
scr |= (scr2 & 0xfe000000) >> 25;
|
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
buf += 5;
|
|
|
|
new_rate = (GUINT32_FROM_BE ((*(guint32 *) buf)) & 0x7ffffe00) >> 9;
|
|
|
|
new_rate *= 400;
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "mpeg_parse: SCR is %llu", scr);
|
2001-12-22 23:26:48 +00:00
|
|
|
mpeg_parse->next_ts = (scr*100)/9;
|
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
if (mpeg_parse->bit_rate != new_rate) {
|
|
|
|
mpeg_parse->bit_rate = new_rate;
|
2002-02-06 23:00:15 +00:00
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (mpeg_parse), "bitrate");
|
2001-12-23 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "mpeg_parse: stream is %1.3fMbs",
|
2001-12-22 23:26:48 +00:00
|
|
|
(mpeg_parse->bit_rate) / 1000000.0);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mpeg_parse_loop (GstElement *element)
|
|
|
|
{
|
|
|
|
GstMPEGParse *mpeg_parse = GST_MPEG_PARSE (element);
|
2001-12-23 22:37:07 +00:00
|
|
|
GstData *data;
|
|
|
|
guint id;
|
|
|
|
gboolean mpeg2;
|
2001-12-22 23:26:48 +00:00
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
data = gst_mpeg_packetize_read (mpeg_parse->packetize);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
id = GST_MPEG_PACKETIZE_ID (mpeg_parse->packetize);
|
|
|
|
mpeg2 = GST_MPEG_PACKETIZE_IS_MPEG2 (mpeg_parse->packetize);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
if (GST_IS_BUFFER (data)) {
|
|
|
|
GstBuffer *buffer = GST_BUFFER (data);
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "mpeg2demux: have chunk 0x%02X", id);
|
2001-12-23 22:37:07 +00:00
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case 0xba:
|
|
|
|
if (CLASS (mpeg_parse)->parse_packhead) {
|
|
|
|
CLASS (mpeg_parse)->parse_packhead (mpeg_parse, buffer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xbb:
|
|
|
|
if (CLASS (mpeg_parse)->parse_syshead) {
|
|
|
|
CLASS (mpeg_parse)->parse_syshead (mpeg_parse, buffer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (mpeg2 && ((id < 0xBD) || (id > 0xFE))) {
|
|
|
|
g_warning ("mpeg2demux: ******** unknown id 0x%02X", id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mpeg2) {
|
|
|
|
if (CLASS (mpeg_parse)->parse_pes) {
|
|
|
|
CLASS (mpeg_parse)->parse_pes (mpeg_parse, buffer);
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
2001-12-23 22:37:07 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (CLASS (mpeg_parse)->parse_packet) {
|
|
|
|
CLASS (mpeg_parse)->parse_packet (mpeg_parse, buffer);
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
2001-12-23 22:37:07 +00:00
|
|
|
}
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
2001-12-23 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2001-12-26 20:31:50 +00:00
|
|
|
if (CLASS (mpeg_parse)->send_data)
|
2001-12-23 22:37:07 +00:00
|
|
|
CLASS (mpeg_parse)->send_data (mpeg_parse, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
|
|
|
gst_mpeg_parse_change_state (GstElement *element)
|
|
|
|
{
|
|
|
|
GstMPEGParse *mpeg_parse = GST_MPEG_PARSE (element);
|
|
|
|
|
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
2002-02-02 13:57:30 +00:00
|
|
|
case GST_STATE_READY_TO_PAUSED:
|
2001-12-23 22:37:07 +00:00
|
|
|
if (!mpeg_parse->packetize) {
|
2001-12-31 03:03:05 +00:00
|
|
|
mpeg_parse->packetize = gst_mpeg_packetize_new (mpeg_parse->sinkpad, GST_MPEG_PACKETIZE_SYSTEM);
|
2001-12-23 22:37:07 +00:00
|
|
|
}
|
|
|
|
break;
|
2002-02-02 13:57:30 +00:00
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
2001-12-23 22:37:07 +00:00
|
|
|
if (mpeg_parse->packetize) {
|
|
|
|
gst_mpeg_packetize_destroy (mpeg_parse->packetize);
|
|
|
|
mpeg_parse->packetize = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2001-12-22 23:26:48 +00:00
|
|
|
|
2002-02-02 13:57:30 +00:00
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mpeg_parse_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstMPEGParse *mpeg_parse;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
mpeg_parse = GST_MPEG_PARSE(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_BIT_RATE:
|
|
|
|
g_value_set_uint (value, mpeg_parse->bit_rate);
|
|
|
|
break;
|
|
|
|
case ARG_MPEG2:
|
|
|
|
if (mpeg_parse->packetize)
|
|
|
|
g_value_set_boolean (value, GST_MPEG_PACKETIZE_IS_MPEG2 (mpeg_parse->packetize));
|
|
|
|
else
|
|
|
|
g_value_set_boolean (value, FALSE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_mpeg_parse_plugin_init (GModule *module, GstPlugin *plugin)
|
|
|
|
{
|
|
|
|
GstElementFactory *factory;
|
|
|
|
|
|
|
|
/* this filter needs the bytestream package */
|
|
|
|
if (!gst_library_load("gstbytestream")) {
|
|
|
|
gst_info("mpeg_parse:: could not load support library: 'gstbytestream'\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create an elementfactory for the mpeg_parse element */
|
2002-04-11 20:42:25 +00:00
|
|
|
factory = gst_element_factory_new("mpegparse",GST_TYPE_MPEG_PARSE,
|
2001-12-22 23:26:48 +00:00
|
|
|
&mpeg_parse_details);
|
|
|
|
g_return_val_if_fail(factory != NULL, FALSE);
|
|
|
|
|
2002-04-11 20:42:25 +00:00
|
|
|
gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_factory));
|
|
|
|
gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_factory));
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|