gstrtpqdmdepay.c 12.3 KB
Newer Older
Edward Hervey's avatar
Edward Hervey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Edward Hervey's avatar
Edward Hervey committed
18 19 20 21 22 23 24 25 26
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <string.h>

#include <gst/rtp/gstrtpbuffer.h>
27
#include <gst/audio/audio.h>
Edward Hervey's avatar
Edward Hervey committed
28
#include "gstrtpqdmdepay.h"
29
#include "gstrtputils.h"
Edward Hervey's avatar
Edward Hervey committed
30

31 32 33
GST_DEBUG_CATEGORY (rtpqdm2depay_debug);
#define GST_CAT_DEFAULT rtpqdm2depay_debug

Edward Hervey's avatar
Edward Hervey committed
34 35 36 37 38 39 40 41 42 43 44 45
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, "
46
        "media = (string) \"audio\", " "encoding-name = (string)\"X-QDM\"")
Edward Hervey's avatar
Edward Hervey committed
47 48
    );

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
49 50
#define gst_rtp_qdm2_depay_parent_class parent_class
G_DEFINE_TYPE (GstRtpQDM2Depay, gst_rtp_qdm2_depay,
Wim Taymans's avatar
Wim Taymans committed
51
    GST_TYPE_RTP_BASE_DEPAYLOAD);
Edward Hervey's avatar
Edward Hervey committed
52 53 54 55 56 57

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
};
Wim Taymans's avatar
Wim Taymans committed
58

Edward Hervey's avatar
Edward Hervey committed
59 60 61 62 63
static void gst_rtp_qdm2_depay_finalize (GObject * object);

static GstStateChangeReturn gst_rtp_qdm2_depay_change_state (GstElement *
    element, GstStateChange transition);

Wim Taymans's avatar
Wim Taymans committed
64
static GstBuffer *gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload,
65
    GstRTPBuffer * rtp);
Wim Taymans's avatar
Wim Taymans committed
66
gboolean gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter,
Edward Hervey's avatar
Edward Hervey committed
67 68 69 70 71 72 73
    GstCaps * caps);

static void
gst_rtp_qdm2_depay_class_init (GstRtpQDM2DepayClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
Wim Taymans's avatar
Wim Taymans committed
74
  GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
Edward Hervey's avatar
Edward Hervey committed
75 76 77

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
78
  gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
Edward Hervey's avatar
Edward Hervey committed
79

80
  gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qdm2_depay_process;
Wim Taymans's avatar
Wim Taymans committed
81
  gstrtpbasedepayload_class->set_caps = gst_rtp_qdm2_depay_setcaps;
Edward Hervey's avatar
Edward Hervey committed
82 83 84 85

  gobject_class->finalize = gst_rtp_qdm2_depay_finalize;

  gstelement_class->change_state = gst_rtp_qdm2_depay_change_state;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
86

87 88 89 90
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_qdm2_depay_src_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_qdm2_depay_sink_template);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
91

92
  gst_element_class_set_static_metadata (gstelement_class,
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
93 94 95 96
      "RTP QDM2 depayloader",
      "Codec/Depayloader/Network/RTP",
      "Extracts QDM2 audio from RTP packets (no RFC)",
      "Edward Hervey <bilboed@bilboed.com>");
Edward Hervey's avatar
Edward Hervey committed
97 98 99
}

static void
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
100
gst_rtp_qdm2_depay_init (GstRtpQDM2Depay * rtpqdm2depay)
Edward Hervey's avatar
Edward Hervey committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
  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);
}

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
118
/* only on the sink */
Edward Hervey's avatar
Edward Hervey committed
119
gboolean
Wim Taymans's avatar
Wim Taymans committed
120
gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
Edward Hervey's avatar
Edward Hervey committed
121 122 123 124 125
{
  GstStructure *structure = gst_caps_get_structure (caps, 0);
  gint clock_rate;

  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
126
    clock_rate = 44100;         /* default */
Edward Hervey's avatar
Edward Hervey committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  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];
147 148
    guint32 crc = 0;
    int i = 0;
Edward Hervey's avatar
Edward Hervey committed
149 150 151
    GstBuffer *buf;
    guint8 *data;

152
    /* CRC is the sum of everything (including first bytes) */
Edward Hervey's avatar
Edward Hervey committed
153 154 155 156 157 158

    data = pack->data;

    if (G_UNLIKELY (data == NULL))
      continue;

159
    /* If the packet size is bigger than 0xff, we need 2 bytes to store the size */
Edward Hervey's avatar
Edward Hervey committed
160
    if (depay->packetsize > 0xff) {
161 162
      /* Expanded size 0x02 | 0x80 */
      data[0] = 0x82;
Edward Hervey's avatar
Edward Hervey committed
163 164
      GST_WRITE_UINT16_BE (data + 1, depay->packetsize - 3);
    } else {
165
      data[0] = 0x2;
Edward Hervey's avatar
Edward Hervey committed
166 167 168 169 170 171 172
      data[1] = depay->packetsize - 2;
    }

    /* Calculate CRC */
    for (; i < depay->packetsize; i++)
      crc += data[i];

173
    GST_DEBUG ("CRC is 0x%x", crc);
Edward Hervey's avatar
Edward Hervey committed
174

175
    /* Write CRC */
Edward Hervey's avatar
Edward Hervey committed
176 177 178 179 180
    if (depay->packetsize > 0xff)
      GST_WRITE_UINT16_BE (data + 3, crc);
    else
      GST_WRITE_UINT16_BE (data + 2, crc);

181
    GST_MEMDUMP ("Extracted packet", data, depay->packetsize);
Edward Hervey's avatar
Edward Hervey committed
182 183

    buf = gst_buffer_new ();
Wim Taymans's avatar
Wim Taymans committed
184
    gst_buffer_append_memory (buf,
Wim Taymans's avatar
Wim Taymans committed
185 186
        gst_memory_new_wrapped (0, data, depay->packetsize, 0,
            depay->packetsize, data, g_free));
Edward Hervey's avatar
Edward Hervey committed
187 188 189

    gst_adapter_push (depay->adapter, buf);

190
    pack->data = NULL;
Edward Hervey's avatar
Edward Hervey committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  }
}

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 *
228
gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
Edward Hervey's avatar
Edward Hervey committed
229 230
{
  GstRtpQDM2Depay *rtpqdm2depay;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
231
  GstBuffer *outbuf = NULL;
232
  guint16 seq;
Edward Hervey's avatar
Edward Hervey committed
233 234 235 236 237 238 239 240 241

  rtpqdm2depay = GST_RTP_QDM2_DEPAY (depayload);

  {
    gint payload_len;
    guint8 *payload;
    guint avail;
    guint pos = 0;

242
    payload_len = gst_rtp_buffer_get_payload_len (rtp);
Edward Hervey's avatar
Edward Hervey committed
243 244 245
    if (payload_len < 3)
      goto bad_packet;

246 247
    payload = gst_rtp_buffer_get_payload (rtp);
    seq = gst_rtp_buffer_get_seq (rtp);
248 249 250 251 252 253
    if (G_UNLIKELY (seq != rtpqdm2depay->nextseq)) {
      GST_DEBUG ("GAP in sequence number, Resetting data !");
      /* Flush previous data */
      flush_data (rtpqdm2depay);
      /* And store new timestamp */
      rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
254
      rtpqdm2depay->timestamp = GST_BUFFER_PTS (rtp->buffer);
255 256 257
      /* And that previous data will be pushed at the bottom */
    }
    rtpqdm2depay->nextseq = seq + 1;
Edward Hervey's avatar
Edward Hervey committed
258

259 260
    GST_DEBUG ("Payload size %d 0x%x sequence:%d", payload_len, payload_len,
        seq);
Edward Hervey's avatar
Edward Hervey committed
261

262
    GST_MEMDUMP ("Incoming payload", payload, payload_len);
Edward Hervey's avatar
Edward Hervey committed
263 264 265 266 267 268 269 270 271 272 273 274 275

    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;
276
          rtpqdm2depay->timestamp = GST_BUFFER_PTS (rtp->buffer);
Edward Hervey's avatar
Edward Hervey committed
277 278 279 280 281
          /* flush the internal data if needed */
          flush_data (rtpqdm2depay);
          if (G_UNLIKELY (!rtpqdm2depay->configured)) {
            guint8 *ourdata;
            GstBuffer *codecdata;
Wim Taymans's avatar
Wim Taymans committed
282
            GstMapInfo cmap;
Edward Hervey's avatar
Edward Hervey committed
283 284 285
            GstCaps *caps;

            /* First bytes are unknown */
286
            GST_MEMDUMP ("Header", payload + pos, 32);
Edward Hervey's avatar
Edward Hervey committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
            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);
302 303

            /* Caps */
Edward Hervey's avatar
Edward Hervey committed
304
            codecdata = gst_buffer_new_and_alloc (48);
Wim Taymans's avatar
Wim Taymans committed
305 306 307 308
            gst_buffer_map (codecdata, &cmap, GST_MAP_WRITE);
            memcpy (cmap.data, headheader, 20);
            memcpy (cmap.data + 20, ourdata, 28);
            gst_buffer_unmap (codecdata, &cmap);
Edward Hervey's avatar
Edward Hervey committed
309 310 311 312 313 314

            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);
Wim Taymans's avatar
Wim Taymans committed
315
            gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), caps);
Edward Hervey's avatar
Edward Hervey committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
            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);
          }

343
          if (packettype > 0x7f) {
Edward Hervey's avatar
Edward Hervey committed
344 345 346 347 348 349 350 351 352 353 354 355 356
            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)) {
357
      GST_DEBUG ("Pushing out %d bytes of collected data", avail);
Edward Hervey's avatar
Edward Hervey committed
358
      outbuf = gst_adapter_take_buffer (rtpqdm2depay->adapter, avail);
359
      GST_BUFFER_PTS (outbuf) = rtpqdm2depay->ptimestamp;
360 361
      GST_DEBUG ("Outgoing buffer timestamp %" GST_TIME_FORMAT,
          GST_TIME_ARGS (rtpqdm2depay->ptimestamp));
Edward Hervey's avatar
Edward Hervey committed
362 363
    }
  }
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
364 365

  return outbuf;
Edward Hervey's avatar
Edward Hervey committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

  /* 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)
{
409 410 411
  GST_DEBUG_CATEGORY_INIT (rtpqdm2depay_debug, "rtpqdm2depay", 0,
      "RTP QDM2 depayloader");

Edward Hervey's avatar
Edward Hervey committed
412
  return gst_element_register (plugin, "rtpqdm2depay",
413
      GST_RANK_SECONDARY, GST_TYPE_RTP_QDM2_DEPAY);
Edward Hervey's avatar
Edward Hervey committed
414
}