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 <gstmpegpacketize.h>
|
|
|
|
|
|
|
|
GstMPEGPacketize*
|
2001-12-31 03:03:05 +00:00
|
|
|
gst_mpeg_packetize_new (GstPad *pad, GstMPEGPacketizeType type)
|
2001-12-22 23:26:48 +00:00
|
|
|
{
|
|
|
|
GstMPEGPacketize *new;
|
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
g_return_val_if_fail (pad != NULL, NULL);
|
|
|
|
g_return_val_if_fail (GST_IS_PAD (pad), NULL);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
new = g_malloc (sizeof (GstMPEGPacketize));
|
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
gst_object_ref (GST_OBJECT (pad));
|
2001-12-22 23:26:48 +00:00
|
|
|
new->id = 0;
|
2001-12-23 22:37:07 +00:00
|
|
|
new->pad = pad;
|
|
|
|
new->bs = gst_bytestream_new (pad);
|
2001-12-22 23:26:48 +00:00
|
|
|
new->MPEG2 = FALSE;
|
2001-12-31 03:03:05 +00:00
|
|
|
new->type = type;
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_mpeg_packetize_destroy (GstMPEGPacketize *packetize)
|
|
|
|
{
|
|
|
|
g_return_if_fail (packetize != NULL);
|
|
|
|
|
2001-12-23 22:37:07 +00:00
|
|
|
gst_bytestream_destroy (packetize->bs);
|
|
|
|
gst_object_unref (GST_OBJECT (packetize->pad));
|
|
|
|
|
2001-12-22 23:26:48 +00:00
|
|
|
g_free (packetize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstData*
|
|
|
|
parse_packhead (GstMPEGPacketize * packetize)
|
|
|
|
{
|
|
|
|
gint length = 8 + 4;
|
|
|
|
guint8 *buf;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "packetize: in parse_packhead");
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
buf = gst_bytestream_peek_bytes (packetize->bs, length);
|
|
|
|
if (!buf) return NULL;
|
|
|
|
buf += 4;
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "code %02x", *buf);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
/* start parsing the stream */
|
|
|
|
if ((*buf & 0xf0) == 0x40) {
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "packetize::parse_packhead setting mpeg2");
|
2001-12-22 23:26:48 +00:00
|
|
|
packetize->MPEG2 = TRUE;
|
|
|
|
length += 2;
|
|
|
|
buf = gst_bytestream_peek_bytes (packetize->bs, length);
|
|
|
|
if (!buf) return NULL;
|
|
|
|
}
|
|
|
|
else {
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "packetize::parse_packhead setting mpeg1");
|
2001-12-22 23:26:48 +00:00
|
|
|
packetize->MPEG2 = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf = gst_bytestream_read (packetize->bs, length);
|
|
|
|
if (!outbuf) return NULL;
|
|
|
|
|
|
|
|
return GST_DATA (outbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline GstData*
|
|
|
|
parse_generic (GstMPEGPacketize *packetize)
|
|
|
|
{
|
|
|
|
guint16 length;
|
|
|
|
GstByteStream *bs = packetize->bs;
|
|
|
|
guchar *buf;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "packetize: in parse_syshead");
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
buf = gst_bytestream_peek_bytes (bs, 2 + 4);
|
|
|
|
if (!buf) return NULL;
|
|
|
|
buf += 4;
|
|
|
|
|
|
|
|
length = GUINT16_FROM_BE (*(guint16 *) buf);
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "packetize: header_length %d", length);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
outbuf = gst_bytestream_read (packetize->bs, 2 + length + 4);
|
|
|
|
if (!outbuf) return NULL;
|
|
|
|
|
|
|
|
return GST_DATA (outbuf);
|
|
|
|
}
|
|
|
|
|
2001-12-31 03:03:05 +00:00
|
|
|
static inline GstData*
|
|
|
|
parse_chunk (GstMPEGPacketize *packetize)
|
|
|
|
{
|
|
|
|
GstByteStream *bs = packetize->bs;
|
|
|
|
guchar *buf;
|
|
|
|
gint offset;
|
|
|
|
guint32 code;
|
|
|
|
const gint chunksize = 4096;
|
|
|
|
GstBuffer *outbuf = NULL;
|
|
|
|
|
|
|
|
buf = gst_bytestream_peek_bytes (bs, chunksize);
|
|
|
|
if (!buf)
|
|
|
|
return FALSE;
|
|
|
|
offset = 4;
|
|
|
|
|
|
|
|
code = GUINT32_FROM_BE (*((guint32 *)(buf+offset)));
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "code = %08x", code);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
while ((code & 0xffffff00) != 0x100L) {
|
|
|
|
code = (code << 8) | buf[offset++];
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, " code = %08x", code);
|
2001-12-31 03:03:05 +00:00
|
|
|
|
|
|
|
if ((offset % chunksize) == 0) {
|
|
|
|
buf = gst_bytestream_peek_bytes (bs, offset + chunksize);
|
|
|
|
if (!buf)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset > 4) {
|
|
|
|
outbuf = gst_bytestream_read (bs, offset-4);
|
|
|
|
}
|
|
|
|
return GST_DATA (outbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-22 23:26:48 +00:00
|
|
|
/* FIXME mmx-ify me */
|
|
|
|
static inline gboolean
|
|
|
|
find_start_code (GstMPEGPacketize *packetize)
|
|
|
|
{
|
|
|
|
GstByteStream *bs = packetize->bs;
|
|
|
|
guchar *buf;
|
|
|
|
gint offset;
|
|
|
|
guint32 code;
|
|
|
|
const gint chunksize = 4096;
|
|
|
|
|
|
|
|
buf = gst_bytestream_peek_bytes (bs, chunksize);
|
|
|
|
if (!buf)
|
|
|
|
return FALSE;
|
|
|
|
offset = 4;
|
|
|
|
|
2001-12-31 03:03:05 +00:00
|
|
|
code = GUINT32_FROM_BE (*((guint32 *)(buf)));
|
2001-12-22 23:26:48 +00:00
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "code = %08x", code);
|
2001-12-22 23:26:48 +00:00
|
|
|
|
|
|
|
while ((code & 0xffffff00) != 0x100L) {
|
|
|
|
code = (code << 8) | buf[offset++];
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, " code = %08x", code);
|
2001-12-22 23:26:48 +00:00
|
|
|
/* g_print (" code = %08x\n", code); */
|
|
|
|
|
|
|
|
if (offset == chunksize) {
|
|
|
|
if (!gst_bytestream_flush (bs, offset))
|
|
|
|
return FALSE;
|
|
|
|
buf = gst_bytestream_peek_bytes (bs, chunksize);
|
|
|
|
if (!buf)
|
|
|
|
return FALSE;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
packetize->id = code & 0xff;
|
|
|
|
if (offset > 4) {
|
|
|
|
if (!gst_bytestream_flush (bs, offset - 4))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstData*
|
|
|
|
gst_mpeg_packetize_read (GstMPEGPacketize *packetize)
|
|
|
|
{
|
|
|
|
gboolean got_event = FALSE;
|
|
|
|
GstData *outbuf = NULL;
|
|
|
|
|
|
|
|
while (outbuf == NULL) {
|
|
|
|
if (!find_start_code (packetize))
|
|
|
|
got_event = TRUE;
|
|
|
|
else {
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "packetize: have chunk 0x%02X", packetize->id);
|
2001-12-31 03:03:05 +00:00
|
|
|
if (packetize->type == GST_MPEG_PACKETIZE_SYSTEM) {
|
|
|
|
switch (packetize->id) {
|
|
|
|
case PACK_START_CODE:
|
|
|
|
outbuf = parse_packhead (packetize);
|
|
|
|
if (!outbuf)
|
|
|
|
got_event = TRUE;
|
|
|
|
break;
|
|
|
|
case SYS_HEADER_START_CODE:
|
2001-12-22 23:26:48 +00:00
|
|
|
outbuf = parse_generic (packetize);
|
|
|
|
if (!outbuf)
|
|
|
|
got_event = TRUE;
|
2001-12-31 03:03:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (packetize->MPEG2 && ((packetize->id < 0xBD) || (packetize->id > 0xFE))) {
|
|
|
|
g_warning ("packetize: ******** unknown id 0x%02X",packetize->id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
outbuf = parse_generic (packetize);
|
|
|
|
if (!outbuf)
|
|
|
|
got_event = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (packetize->type == GST_MPEG_PACKETIZE_VIDEO) {
|
|
|
|
outbuf = parse_chunk (packetize);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g_assert_not_reached ();
|
2001-12-22 23:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (got_event) {
|
|
|
|
guint32 remaining;
|
|
|
|
GstEvent *event;
|
|
|
|
gint etype;
|
|
|
|
|
|
|
|
gst_bytestream_get_status (packetize->bs, &remaining, &event);
|
|
|
|
etype = event? GST_EVENT_TYPE (event) : GST_EVENT_EOS;
|
|
|
|
|
|
|
|
switch (etype) {
|
|
|
|
case GST_EVENT_DISCONTINUOUS:
|
|
|
|
gst_bytestream_flush_fast (packetize->bs, remaining);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_DATA (event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outbuf;
|
|
|
|
}
|