rtp: New QDM2 rtp depayloader.

Reverse-engineered by comparing:
* A rtp hinted file provided by DarwinStreamingServer
* The output procued by DSS for that same file

Also used various streaming sources available on the internet to fine-tune
the code.

The header/codec_data extraction methods are from FFMpeg (LGPL).
This commit is contained in:
Edward Hervey 2009-08-02 13:42:12 +02:00
parent e2b3665ae6
commit d39c057e42
4 changed files with 500 additions and 0 deletions

View file

@ -50,6 +50,7 @@ libgstrtp_la_SOURCES = \
gstrtpmp4gpay.c \
gstrtpmp4adepay.c \
gstrtpmp4apay.c \
gstrtpqdmdepay.c \
gstrtpsirenpay.c \
gstrtpsirendepay.c \
gstrtpspeexdepay.c \
@ -127,6 +128,7 @@ noinst_HEADERS = \
gstrtpmp4apay.h \
gstrtpdepay.h \
gstasteriskh263.h \
gstrtpqdmdepay.h \
gstrtpsirenpay.h \
gstrtpsirendepay.h \
gstrtpspeexdepay.h \

View file

@ -67,6 +67,7 @@
#include "gstrtpmp4apay.h"
#include "gstrtpmp4gdepay.h"
#include "gstrtpmp4gpay.h"
#include "gstrtpqdmdepay.h"
#include "gstrtpsirenpay.h"
#include "gstrtpsirendepay.h"
#include "gstrtpspeexpay.h"
@ -220,6 +221,9 @@ plugin_init (GstPlugin * plugin)
if (!gst_rtp_mp4g_pay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_qdm2_depay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_siren_pay_plugin_init (plugin))
return FALSE;

412
gst/rtp/gstrtpqdmdepay.c Normal file
View file

@ -0,0 +1,412 @@
/* GStreamer
* Copyright (C) <2009> Edward Hervey <bilboed@bilboed.com>
*
* 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 <gst/rtp/gstrtpbuffer.h>
#include "gstrtpqdmdepay.h"
/* elementfactory information */
static const GstElementDetails gst_rtp_qdm2depay_details =
GST_ELEMENT_DETAILS ("RTP QDM2 depayloader",
"Codec/Depayloader/Network",
"Extracts QDM2 audio from RTP packets (no RFC)",
"Edward Hervey <bilboed@bilboed.com>");
static GstStaticPadTemplate gst_rtp_qdm2_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-qdm2")
);
static GstStaticPadTemplate gst_rtp_qdm2_depay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-rtp, "
"media = (string) \"audio\", "
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
"encoding-name = (string)\"X-QDM\"")
);
GST_BOILERPLATE (GstRtpQDM2Depay, gst_rtp_qdm2_depay, GstBaseRTPDepayload,
GST_TYPE_BASE_RTP_DEPAYLOAD);
static const guint8 headheader[20] = {
0x0, 0x0, 0x0, 0xc, 0x66, 0x72, 0x6d, 0x61,
0x51, 0x44, 0x4d, 0x32, 0x0, 0x0, 0x0, 0x24,
0x51, 0x44, 0x43, 0x41
};
static void gst_rtp_qdm2_depay_finalize (GObject * object);
static GstStateChangeReturn gst_rtp_qdm2_depay_change_state (GstElement *
element, GstStateChange transition);
static GstBuffer *gst_rtp_qdm2_depay_process (GstBaseRTPDepayload * depayload,
GstBuffer * buf);
gboolean gst_rtp_qdm2_depay_setcaps (GstBaseRTPDepayload * filter,
GstCaps * caps);
static void
gst_rtp_qdm2_depay_base_init (gpointer klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_qdm2_depay_src_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_qdm2_depay_sink_template));
gst_element_class_set_details (element_class, &gst_rtp_qdm2depay_details);
}
static void
gst_rtp_qdm2_depay_class_init (GstRtpQDM2DepayClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gstbasertpdepayload_class->process = gst_rtp_qdm2_depay_process;
gstbasertpdepayload_class->set_caps = gst_rtp_qdm2_depay_setcaps;
gobject_class->finalize = gst_rtp_qdm2_depay_finalize;
gstelement_class->change_state = gst_rtp_qdm2_depay_change_state;
}
static void
gst_rtp_qdm2_depay_init (GstRtpQDM2Depay * rtpqdm2depay,
GstRtpQDM2DepayClass * klass)
{
rtpqdm2depay->adapter = gst_adapter_new ();
}
static void
gst_rtp_qdm2_depay_finalize (GObject * object)
{
GstRtpQDM2Depay *rtpqdm2depay;
rtpqdm2depay = GST_RTP_QDM2_DEPAY (object);
g_object_unref (rtpqdm2depay->adapter);
rtpqdm2depay->adapter = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
}
// only on the sink
gboolean
gst_rtp_qdm2_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
{
GstStructure *structure = gst_caps_get_structure (caps, 0);
gint clock_rate;
if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
clock_rate = 44100; // default
filter->clock_rate = clock_rate;
/* will set caps later */
return TRUE;
}
static void
flush_data (GstRtpQDM2Depay * depay)
{
guint i;
guint avail;
if ((avail = gst_adapter_available (depay->adapter)))
gst_adapter_flush (depay->adapter, avail);
GST_DEBUG ("Flushing %d packets", depay->nbpackets);
for (i = 0; depay->packets[i]; i++) {
QDM2Packet *pack = depay->packets[i];
guint32 crc;
int i;
GstBuffer *buf;
guint8 *data;
/* CRC is the sum of everything (including first bytes)
* minus the 2 bytes reserved for the checksum */
GST_DEBUG ("Packet #%d size:%d", i, pack->offs);
data = pack->data;
if (G_UNLIKELY (data == NULL))
continue;
/* type 2 */
if (depay->packetsize > 0xff) {
crc = data[0] = 0x82;
crc -= 3;
GST_WRITE_UINT16_BE (data + 1, depay->packetsize - 3);
i = 5;
} else {
crc = data[0] = 0x2;
data[1] = depay->packetsize - 2;
i = 4;
crc -= 2;
}
crc += depay->packetsize;
/* Calculate CRC */
for (; i < depay->packetsize; i++)
crc += data[i];
/* FIXME : Figure out why there's a difference of 0x1fe (0xff << 1) */
if (depay->packetsize > 0xff)
crc -= 510;
if (depay->packetsize > 0xff)
GST_WRITE_UINT16_BE (data + 3, crc);
else
GST_WRITE_UINT16_BE (data + 2, crc);
GST_DEBUG ("CRC is 0x%x", crc);
/* gst_util_dump_mem (data, depay->packetsize); */
buf = gst_buffer_new ();
GST_BUFFER_DATA (buf) = data;
GST_BUFFER_MALLOCDATA (buf) = data;
GST_BUFFER_SIZE (buf) = depay->packetsize;
gst_adapter_push (depay->adapter, buf);
if (pack->data) {
pack->data = NULL;
}
}
}
static void
add_packet (GstRtpQDM2Depay * depay, guint32 pid, guint32 len, guint8 * data)
{
QDM2Packet *packet;
if (G_UNLIKELY (!depay->configured))
return;
GST_DEBUG ("pid:%d, len:%d, data:%p", pid, len, data);
if (G_UNLIKELY (depay->packets[pid] == NULL)) {
depay->packets[pid] = g_malloc0 (sizeof (QDM2Packet));
depay->nbpackets = MAX (depay->nbpackets, pid + 1);
}
packet = depay->packets[pid];
GST_DEBUG ("packet:%p", packet);
GST_DEBUG ("packet->data:%p", packet->data);
if (G_UNLIKELY (packet->data == NULL)) {
packet->data = g_malloc0 (depay->packetsize);
/* We leave space for the header/crc */
if (depay->packetsize > 0xff)
packet->offs = 5;
else
packet->offs = 4;
}
/* Finally copy the data over */
memcpy (packet->data + packet->offs, data, len);
packet->offs += len;
}
static GstBuffer *
gst_rtp_qdm2_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
{
GstRtpQDM2Depay *rtpqdm2depay;
GstBuffer *outbuf;
rtpqdm2depay = GST_RTP_QDM2_DEPAY (depayload);
{
gint payload_len;
guint8 *payload;
guint avail;
guint pos = 0;
payload_len = gst_rtp_buffer_get_payload_len (buf);
if (payload_len < 3)
goto bad_packet;
payload = gst_rtp_buffer_get_payload (buf);
GST_DEBUG ("Payload size %d 0x%x", payload_len, payload_len);
/* gst_util_dump_mem (payload, payload_len); */
while (pos < payload_len) {
switch (payload[pos]) {
case 0x80:{
GST_DEBUG ("Unrecognized 0x80 marker, skipping 12 bytes");
pos += 12;
}
break;
case 0xff:
/* HEADERS */
GST_DEBUG ("Headers");
/* Store the incoming timestamp */
rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
rtpqdm2depay->timestamp = GST_BUFFER_TIMESTAMP (buf);
/* flush the internal data if needed */
flush_data (rtpqdm2depay);
if (G_UNLIKELY (!rtpqdm2depay->configured)) {
guint8 *ourdata;
GstBuffer *codecdata;
GstCaps *caps;
/* First bytes are unknown */
/* gst_util_dump_mem (payload + pos, 32); */
ourdata = payload + pos + 10;
pos += 10;
rtpqdm2depay->channs = GST_READ_UINT32_BE (payload + pos + 4);
rtpqdm2depay->samplerate = GST_READ_UINT32_BE (payload + pos + 8);
rtpqdm2depay->bitrate = GST_READ_UINT32_BE (payload + pos + 12);
rtpqdm2depay->blocksize = GST_READ_UINT32_BE (payload + pos + 16);
rtpqdm2depay->framesize = GST_READ_UINT32_BE (payload + pos + 20);
rtpqdm2depay->packetsize = GST_READ_UINT32_BE (payload + pos + 24);
/* 16 bit empty block (0x02 0x00) */
pos += 30;
GST_DEBUG
("channs:%d, samplerate:%d, bitrate:%d, blocksize:%d, framesize:%d, packetsize:%d",
rtpqdm2depay->channs, rtpqdm2depay->samplerate,
rtpqdm2depay->bitrate, rtpqdm2depay->blocksize,
rtpqdm2depay->framesize, rtpqdm2depay->packetsize);
/* FIXME : SET CAPS ! */
/* audio/x-qdm2, codec_data, samplesize=(int)16, rate=(int), channels=(int) */
codecdata = gst_buffer_new_and_alloc (48);
memcpy (GST_BUFFER_DATA (codecdata), headheader, 20);
memcpy (GST_BUFFER_DATA (codecdata) + 20, ourdata, 28);
caps = gst_caps_new_simple ("audio/x-qdm2",
"samplesize", G_TYPE_INT, 16,
"rate", G_TYPE_INT, rtpqdm2depay->samplerate,
"channels", G_TYPE_INT, rtpqdm2depay->channs,
"codec_data", GST_TYPE_BUFFER, codecdata, NULL);
gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), caps);
gst_caps_unref (caps);
rtpqdm2depay->configured = TRUE;
} else {
GST_DEBUG ("Already configured, skipping headers");
pos += 40;
}
break;
default:{
/* Shuffled packet contents */
guint packetid = payload[pos++];
guint packettype = payload[pos++];
guint packlen = payload[pos++];
guint hsize = 2;
GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
packetid, packettype, packlen);
/* Packets bigger than 0xff bytes have a type with the high bit set */
if (G_UNLIKELY (packettype & 0x80)) {
packettype &= 0x7f;
packlen <<= 8;
packlen |= payload[pos++];
hsize = 3;
GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
packetid, packettype, packlen);
}
if (packettype > 0x17) {
GST_ERROR ("HOUSTON WE HAVE A PROBLEM !!!!");
}
add_packet (rtpqdm2depay, packetid, packlen + hsize,
payload + pos - hsize);
pos += packlen;
}
}
}
GST_DEBUG ("final pos %d", pos);
avail = gst_adapter_available (rtpqdm2depay->adapter);
if (G_UNLIKELY (avail)) {
outbuf = gst_adapter_take_buffer (rtpqdm2depay->adapter, avail);
GST_BUFFER_TIMESTAMP (outbuf) = rtpqdm2depay->ptimestamp;
return outbuf;
}
}
return NULL;
/* ERRORS */
bad_packet:
{
GST_ELEMENT_WARNING (rtpqdm2depay, STREAM, DECODE,
(NULL), ("Packet was too short"));
return NULL;
}
}
static GstStateChangeReturn
gst_rtp_qdm2_depay_change_state (GstElement * element,
GstStateChange transition)
{
GstRtpQDM2Depay *rtpqdm2depay;
GstStateChangeReturn ret;
rtpqdm2depay = GST_RTP_QDM2_DEPAY (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_adapter_clear (rtpqdm2depay->adapter);
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
gboolean
gst_rtp_qdm2_depay_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "rtpqdm2depay",
GST_RANK_MARGINAL, GST_TYPE_RTP_QDM2_DEPAY);
}

82
gst/rtp/gstrtpqdmdepay.h Normal file
View file

@ -0,0 +1,82 @@
/* GStreamer
* Copyright (C) <2009> Edward Hervey <bilboed@bilboed.com>
*
* 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.
*/
#ifndef __GST_RTP_QDM2_DEPAY_H__
#define __GST_RTP_QDM2_DEPAY_H__
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include <gst/rtp/gstbasertpdepayload.h>
G_BEGIN_DECLS
#define GST_TYPE_RTP_QDM2_DEPAY \
(gst_rtp_qdm2_depay_get_type())
#define GST_RTP_QDM2_DEPAY(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_QDM2_DEPAY,GstRtpQDM2Depay))
#define GST_RTP_QDM2_DEPAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_QDM2_DEPAY,GstRtpQDM2DepayClass))
#define GST_IS_RTP_QDM2_DEPAY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_QDM2_DEPAY))
#define GST_IS_RTP_QDM2_DEPAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_QDM2_DEPAY))
typedef struct _GstRtpQDM2Depay GstRtpQDM2Depay;
typedef struct _GstRtpQDM2DepayClass GstRtpQDM2DepayClass;
typedef struct _QDM2Packet {
guint8* data;
guint offs; /* Starts at 4 to give room for the prefix */
} QDM2Packet;
#define MAX_SCRAMBLED_PACKETS 64
struct _GstRtpQDM2Depay
{
GstBaseRTPDepayload depayload;
GstAdapter *adapter;
gboolean configured;
GstClockTime timestamp; /* Timestamp of current incoming data */
GstClockTime ptimestamp; /* Timestamp of data stored in the adapter */
guint32 channs;
guint32 samplerate;
guint32 bitrate;
guint32 blocksize;
guint32 framesize;
guint32 packetsize;
guint nbpackets; /* Number of packets to unscramble */
QDM2Packet *packets[MAX_SCRAMBLED_PACKETS];
};
struct _GstRtpQDM2DepayClass
{
GstBaseRTPDepayloadClass parent_class;
};
gboolean gst_rtp_qdm2_depay_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* __GST_RTP_QDM2_DEPAY_H__ */