2002-03-20 21:45:03 +00:00
|
|
|
/* GStreamer
|
2001-12-31 03:03:05 +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*/
|
2003-06-29 19:46:09 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-31 03:03:05 +00:00
|
|
|
#include "gstrfc2250enc.h"
|
|
|
|
|
|
|
|
/* elementfactory information */
|
|
|
|
static GstElementDetails rfc2250_enc_details = {
|
|
|
|
"RFC 2250 packet encoder",
|
2002-04-20 21:42:51 +00:00
|
|
|
"Codec/Parser",
|
2001-12-31 03:03:05 +00:00
|
|
|
"transforms MPEG1/2 video to an RFC 2250 compliant format",
|
2003-11-02 21:22:09 +00:00
|
|
|
"Wim Taymans <wim.taymans@chello.be>"
|
2001-12-31 03:03:05 +00:00
|
|
|
};
|
|
|
|
|
2005-12-06 19:48:07 +00:00
|
|
|
#define CLASS(o) GST_RFC2250_ENC_CLASS (G_OBJECT_GET_CLASS (o))
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
/* GstRFC2250Enc signals and args */
|
2004-03-14 22:34:30 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-31 03:03:05 +00:00
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-31 03:03:05 +00:00
|
|
|
ARG_0,
|
|
|
|
ARG_BIT_RATE,
|
2004-05-21 22:39:29 +00:00
|
|
|
ARG_MPEG2
|
|
|
|
/* FILL ME */
|
2001-12-31 03:03:05 +00:00
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/mpeg, "
|
2004-03-15 19:32:25 +00:00
|
|
|
"mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) FALSE")
|
2004-03-14 22:34:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/mpeg, "
|
2004-03-15 19:32:25 +00:00
|
|
|
"mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) FALSE")
|
2004-03-14 22:34:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static void gst_rfc2250_enc_class_init (GstRFC2250EncClass * klass);
|
|
|
|
static void gst_rfc2250_enc_base_init (GstRFC2250EncClass * klass);
|
|
|
|
static void gst_rfc2250_enc_init (GstRFC2250Enc * rfc2250_enc);
|
2005-09-02 15:43:54 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_rfc2250_enc_change_state (GstElement * element, GstStateChange transition);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
static void gst_rfc2250_enc_loop (GstElement * element);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
static void gst_rfc2250_enc_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2004-03-14 22:34:30 +00:00
|
|
|
|
2001-12-31 03:03:05 +00:00
|
|
|
/*static guint gst_rfc2250_enc_signals[LAST_SIGNAL] = { 0 };*/
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_rfc2250_enc_get_type (void)
|
|
|
|
{
|
|
|
|
static GType rfc2250_enc_type = 0;
|
|
|
|
|
|
|
|
if (!rfc2250_enc_type) {
|
|
|
|
static const GTypeInfo rfc2250_enc_info = {
|
2004-03-14 22:34:30 +00:00
|
|
|
sizeof (GstRFC2250EncClass),
|
|
|
|
(GBaseInitFunc) gst_rfc2250_enc_base_init,
|
2001-12-31 03:03:05 +00:00
|
|
|
NULL,
|
2004-03-14 22:34:30 +00:00
|
|
|
(GClassInitFunc) gst_rfc2250_enc_class_init,
|
2001-12-31 03:03:05 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2004-03-14 22:34:30 +00:00
|
|
|
sizeof (GstRFC2250Enc),
|
2001-12-31 03:03:05 +00:00
|
|
|
0,
|
2004-03-14 22:34:30 +00:00
|
|
|
(GInstanceInitFunc) gst_rfc2250_enc_init,
|
2001-12-31 03:03:05 +00:00
|
|
|
};
|
2004-03-15 19:32:25 +00:00
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
rfc2250_enc_type =
|
2004-03-15 19:32:25 +00:00
|
|
|
g_type_register_static (GST_TYPE_ELEMENT, "GstRFC2250Enc",
|
|
|
|
&rfc2250_enc_info, 0);
|
2001-12-31 03:03:05 +00:00
|
|
|
}
|
|
|
|
return rfc2250_enc_type;
|
|
|
|
}
|
|
|
|
|
2003-11-02 21:22:09 +00:00
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_base_init (GstRFC2250EncClass * klass)
|
2003-11-02 21:22:09 +00:00
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_static_pad_template_get (&src_factory));
|
2003-11-02 21:22:09 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_static_pad_template_get (&sink_factory));
|
2003-11-02 21:22:09 +00:00
|
|
|
gst_element_class_set_details (element_class, &rfc2250_enc_details);
|
|
|
|
}
|
|
|
|
|
2001-12-31 03:03:05 +00:00
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_class_init (GstRFC2250EncClass * klass)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2001-12-31 03:03:05 +00:00
|
|
|
|
2006-04-08 21:42:19 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BIT_RATE,
|
|
|
|
g_param_spec_uint ("bit_rate", "bit_rate", "bit_rate",
|
2004-03-15 19:32:25 +00:00
|
|
|
0, G_MAXUINT, 0, G_PARAM_READABLE));
|
2001-12-31 03:03:05 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MPEG2,
|
2004-03-14 22:34:30 +00:00
|
|
|
g_param_spec_boolean ("mpeg2", "mpeg2", "is this an mpeg2 stream",
|
2004-03-15 19:32:25 +00:00
|
|
|
FALSE, G_PARAM_READABLE));
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
gobject_class->get_property = gst_rfc2250_enc_get_property;
|
|
|
|
|
|
|
|
gstelement_class->change_state = gst_rfc2250_enc_change_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_init (GstRFC2250Enc * rfc2250_enc)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
2004-03-14 22:34:30 +00:00
|
|
|
rfc2250_enc->sinkpad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get (&sink_factory),
|
|
|
|
"sink");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (rfc2250_enc), rfc2250_enc->sinkpad);
|
|
|
|
gst_element_set_loop_function (GST_ELEMENT (rfc2250_enc),
|
|
|
|
gst_rfc2250_enc_loop);
|
|
|
|
rfc2250_enc->srcpad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get (&src_factory),
|
|
|
|
"src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (rfc2250_enc), rfc2250_enc->srcpad);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
/* initialize parser state */
|
|
|
|
rfc2250_enc->packetize = NULL;
|
|
|
|
rfc2250_enc->next_ts = 0;
|
|
|
|
rfc2250_enc->packet = 0;
|
|
|
|
|
|
|
|
/* zero counters (should be done at RUNNING?) */
|
|
|
|
rfc2250_enc->bit_rate = 0;
|
|
|
|
rfc2250_enc->MTU = 3048;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_new_buffer (GstRFC2250Enc * enc)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
|
|
|
if (enc->packet) {
|
2003-10-08 16:08:10 +00:00
|
|
|
gst_pad_push (enc->srcpad, GST_DATA (enc->packet));
|
2001-12-31 03:03:05 +00:00
|
|
|
}
|
|
|
|
enc->packet = gst_buffer_new ();
|
|
|
|
enc->flags = 0;
|
|
|
|
enc->remaining = enc->MTU;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_add_slice (GstRFC2250Enc * enc, GstBuffer * buffer)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
|
|
|
gint slice_length = GST_BUFFER_SIZE (buffer);
|
|
|
|
|
|
|
|
/* see if the slice fits in the current buffer */
|
|
|
|
if (slice_length <= enc->remaining) {
|
2004-03-06 01:25:57 +00:00
|
|
|
GstBuffer *newbuf;
|
|
|
|
|
|
|
|
newbuf = gst_buffer_merge (enc->packet, buffer);
|
2001-12-31 03:03:05 +00:00
|
|
|
gst_buffer_unref (buffer);
|
2004-03-06 01:25:57 +00:00
|
|
|
gst_buffer_unref (enc->packet);
|
|
|
|
enc->packet = newbuf;
|
2001-12-31 03:03:05 +00:00
|
|
|
enc->remaining -= slice_length;
|
|
|
|
}
|
|
|
|
/* it doesn't fit */
|
|
|
|
else {
|
|
|
|
/* do we need to start a new packet? */
|
|
|
|
if (slice_length <= enc->MTU) {
|
2004-03-06 01:25:57 +00:00
|
|
|
GstBuffer *newbuf;
|
|
|
|
|
2001-12-31 03:03:05 +00:00
|
|
|
gst_rfc2250_enc_new_buffer (enc);
|
2004-03-06 01:25:57 +00:00
|
|
|
newbuf = gst_buffer_merge (enc->packet, buffer);
|
2001-12-31 03:03:05 +00:00
|
|
|
gst_buffer_unref (buffer);
|
2004-03-06 01:25:57 +00:00
|
|
|
gst_buffer_unref (enc->packet);
|
|
|
|
enc->packet = newbuf;
|
2001-12-31 03:03:05 +00:00
|
|
|
enc->remaining -= slice_length;
|
|
|
|
}
|
|
|
|
/* else we have to fragment */
|
|
|
|
else {
|
|
|
|
gint offset = 0;
|
|
|
|
|
|
|
|
while (slice_length > 0) {
|
2004-03-15 19:32:25 +00:00
|
|
|
GstBuffer *outbuf;
|
|
|
|
GstBuffer *newbuf;
|
|
|
|
|
|
|
|
outbuf =
|
|
|
|
gst_buffer_create_sub (buffer, offset, MIN (enc->remaining,
|
|
|
|
slice_length));
|
|
|
|
newbuf = gst_buffer_merge (enc->packet, outbuf);
|
|
|
|
slice_length -= GST_BUFFER_SIZE (outbuf);
|
|
|
|
offset += GST_BUFFER_SIZE (outbuf);
|
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
gst_buffer_unref (newbuf);
|
|
|
|
enc->packet = newbuf;
|
|
|
|
gst_rfc2250_enc_new_buffer (enc);
|
2001-12-31 03:03:05 +00:00
|
|
|
}
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_loop (GstElement * element)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
|
|
|
GstRFC2250Enc *enc = GST_RFC2250_ENC (element);
|
|
|
|
GstData *data;
|
|
|
|
guint id;
|
|
|
|
gboolean mpeg2;
|
|
|
|
|
|
|
|
data = gst_mpeg_packetize_read (enc->packetize);
|
|
|
|
|
|
|
|
id = GST_MPEG_PACKETIZE_ID (enc->packetize);
|
|
|
|
mpeg2 = GST_MPEG_PACKETIZE_IS_MPEG2 (enc->packetize);
|
2004-03-14 22:34:30 +00:00
|
|
|
|
2001-12-31 03:03:05 +00:00
|
|
|
if (GST_IS_BUFFER (data)) {
|
|
|
|
GstBuffer *buffer = GST_BUFFER (data);
|
|
|
|
|
2003-06-29 19:46:09 +00:00
|
|
|
GST_DEBUG ("rfc2250enc: have chunk 0x%02X", id);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case SEQUENCE_START_CODE:
|
2004-03-15 19:32:25 +00:00
|
|
|
gst_rfc2250_enc_new_buffer (enc);
|
|
|
|
enc->flags |= ENC_HAVE_SEQ;
|
|
|
|
break;
|
2001-12-31 03:03:05 +00:00
|
|
|
case GOP_START_CODE:
|
2004-03-15 19:32:25 +00:00
|
|
|
if (enc->flags & ENC_HAVE_DATA) {
|
|
|
|
gst_rfc2250_enc_new_buffer (enc);
|
|
|
|
}
|
|
|
|
enc->flags |= ENC_HAVE_GOP;
|
|
|
|
break;
|
2001-12-31 03:03:05 +00:00
|
|
|
case PICTURE_START_CODE:
|
2004-03-15 19:32:25 +00:00
|
|
|
if (enc->flags & ENC_HAVE_DATA) {
|
|
|
|
gst_rfc2250_enc_new_buffer (enc);
|
|
|
|
}
|
|
|
|
enc->flags |= ENC_HAVE_PIC;
|
|
|
|
break;
|
2001-12-31 03:03:05 +00:00
|
|
|
case EXT_START_CODE:
|
|
|
|
case USER_START_CODE:
|
|
|
|
case SEQUENCE_ERROR_START_CODE:
|
|
|
|
case SEQUENCE_END_START_CODE:
|
2004-03-15 19:32:25 +00:00
|
|
|
break;
|
2001-12-31 03:03:05 +00:00
|
|
|
default:
|
2004-03-15 19:32:25 +00:00
|
|
|
/* do this here because of the long range */
|
|
|
|
if (id >= SLICE_MIN_START_CODE && id <= SLICE_MAX_START_CODE) {
|
|
|
|
enc->flags |= ENC_HAVE_DATA;
|
|
|
|
gst_rfc2250_enc_add_slice (enc, buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
if (buffer) {
|
2002-07-08 19:43:33 +00:00
|
|
|
gst_buffer_merge (enc->packet, buffer);
|
2001-12-31 03:03:05 +00:00
|
|
|
enc->remaining -= GST_BUFFER_SIZE (buffer);
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
}
|
2004-03-14 22:34:30 +00:00
|
|
|
} else {
|
2001-12-31 03:03:05 +00:00
|
|
|
if (enc->packet) {
|
2003-10-08 16:08:10 +00:00
|
|
|
gst_pad_push (enc->srcpad, GST_DATA (enc->packet));
|
2001-12-31 03:03:05 +00:00
|
|
|
enc->packet = NULL;
|
|
|
|
enc->flags = 0;
|
|
|
|
enc->remaining = enc->MTU;
|
|
|
|
}
|
|
|
|
gst_pad_event_default (enc->sinkpad, GST_EVENT (data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-02 15:43:54 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_rfc2250_enc_change_state (GstElement * element, GstStateChange transition)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
|
|
|
GstRFC2250Enc *rfc2250_enc = GST_RFC2250_ENC (element);
|
2006-04-21 10:50:17 +00:00
|
|
|
GstStateChangeReturn ret;
|
2001-12-31 03:03:05 +00:00
|
|
|
|
2005-09-02 15:43:54 +00:00
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2001-12-31 03:03:05 +00:00
|
|
|
if (!rfc2250_enc->packetize) {
|
2004-03-15 19:32:25 +00:00
|
|
|
rfc2250_enc->packetize =
|
|
|
|
gst_mpeg_packetize_new (rfc2250_enc->sinkpad,
|
|
|
|
GST_MPEG_PACKETIZE_VIDEO);
|
2001-12-31 03:03:05 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-04-21 10:50:17 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
2005-09-02 15:43:54 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2001-12-31 03:03:05 +00:00
|
|
|
if (rfc2250_enc->packetize) {
|
2004-03-15 19:32:25 +00:00
|
|
|
gst_mpeg_packetize_destroy (rfc2250_enc->packetize);
|
|
|
|
rfc2250_enc->packetize = NULL;
|
2001-12-31 03:03:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-04-21 10:50:17 +00:00
|
|
|
return ret;
|
2001-12-31 03:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
|
|
|
GstRFC2250Enc *rfc2250_enc;
|
|
|
|
|
2005-11-14 21:20:21 +00:00
|
|
|
/* it's not null if we got it, but it might not be ours */
|
2004-03-14 22:34:30 +00:00
|
|
|
rfc2250_enc = GST_RFC2250_ENC (object);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
2004-03-14 22:34:30 +00:00
|
|
|
case ARG_BIT_RATE:
|
|
|
|
g_value_set_uint (value, rfc2250_enc->bit_rate);
|
2001-12-31 03:03:05 +00:00
|
|
|
break;
|
|
|
|
case ARG_MPEG2:
|
|
|
|
if (rfc2250_enc->packetize)
|
2004-03-15 19:32:25 +00:00
|
|
|
g_value_set_boolean (value,
|
|
|
|
GST_MPEG_PACKETIZE_IS_MPEG2 (rfc2250_enc->packetize));
|
2001-12-31 03:03:05 +00:00
|
|
|
else
|
2004-03-15 19:32:25 +00:00
|
|
|
g_value_set_boolean (value, FALSE);
|
2001-12-31 03:03:05 +00:00
|
|
|
break;
|
2004-03-14 22:34:30 +00:00
|
|
|
default:
|
2001-12-31 03:03:05 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_rfc2250_enc_plugin_init (GstPlugin * plugin)
|
2001-12-31 03:03:05 +00:00
|
|
|
{
|
2003-11-02 21:22:09 +00:00
|
|
|
return gst_element_register (plugin, "rfc2250enc",
|
2004-03-14 22:34:30 +00:00
|
|
|
GST_RANK_NONE, GST_TYPE_RFC2250_ENC);
|
2001-12-31 03:03:05 +00:00
|
|
|
}
|