gst/rtp/: Add DV pay and depayloaders. Fixes #532423.
authorOlivier Crete <tester@tester.ca>
Mon, 12 May 2008 18:27:24 +0000 (18:27 +0000)
committerWim Taymans <wim.taymans@gmail.com>
Mon, 12 May 2008 18:27:24 +0000 (18:27 +0000)
Original commit message from CVS:
Based on patch by: Olivier Crete <tester at tester dot ca>
* gst/rtp/Makefile.am:
* gst/rtp/gstrtp.c: (plugin_init):
* gst/rtp/gstrtpdvdepay.c: (gst_rtp_dv_depay_base_init),
(gst_rtp_dv_depay_class_init), (gst_rtp_dv_depay_init),
(parse_encode), (gst_rtp_dv_depay_setcaps),
(calculate_difblock_location), (gst_rtp_dv_depay_process),
(gst_rtp_dv_depay_reset), (gst_rtp_dv_depay_change_state),
(gst_rtp_dv_depay_plugin_init):
* gst/rtp/gstrtpdvdepay.h:
* gst/rtp/gstrtpdvpay.c: (gst_dv_pay_mode_get_type),
(gst_rtp_dv_pay_base_init), (gst_rtp_dv_pay_class_init),
(gst_rtp_dv_pay_init), (gst_dv_pay_set_property),
(gst_dv_pay_get_property), (gst_rtp_dv_pay_setcaps),
(gst_dv_pay_negotiate), (include_dif),
(gst_rtp_dv_pay_handle_buffer), (gst_rtp_dv_pay_plugin_init):
* gst/rtp/gstrtpdvpay.h:
Add DV pay and depayloaders. Fixes #532423.

ChangeLog
gst/rtp/Makefile.am
gst/rtp/gstrtp.c
gst/rtp/gstrtpdvdepay.c [new file with mode: 0644]
gst/rtp/gstrtpdvdepay.h [new file with mode: 0644]
gst/rtp/gstrtpdvpay.c [new file with mode: 0644]
gst/rtp/gstrtpdvpay.h [new file with mode: 0644]

index 3512018..6f53d8c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2008-05-12  Wim Taymans  <wim.taymans@collabora.co.uk>
+
+       Based on patch by: Olivier Crete <tester at tester dot ca>
+
+       * gst/rtp/Makefile.am:
+       * gst/rtp/gstrtp.c: (plugin_init):
+       * gst/rtp/gstrtpdvdepay.c: (gst_rtp_dv_depay_base_init),
+       (gst_rtp_dv_depay_class_init), (gst_rtp_dv_depay_init),
+       (parse_encode), (gst_rtp_dv_depay_setcaps),
+       (calculate_difblock_location), (gst_rtp_dv_depay_process),
+       (gst_rtp_dv_depay_reset), (gst_rtp_dv_depay_change_state),
+       (gst_rtp_dv_depay_plugin_init):
+       * gst/rtp/gstrtpdvdepay.h:
+       * gst/rtp/gstrtpdvpay.c: (gst_dv_pay_mode_get_type),
+       (gst_rtp_dv_pay_base_init), (gst_rtp_dv_pay_class_init),
+       (gst_rtp_dv_pay_init), (gst_dv_pay_set_property),
+       (gst_dv_pay_get_property), (gst_rtp_dv_pay_setcaps),
+       (gst_dv_pay_negotiate), (include_dif),
+       (gst_rtp_dv_pay_handle_buffer), (gst_rtp_dv_pay_plugin_init):
+       * gst/rtp/gstrtpdvpay.h:
+       Add DV pay and depayloaders. Fixes #532423.
+
 2008-05-12  Mark Nauwelaerts  <mnauw@users.sf.net>
 
        * gst/matroska/matroska-demux.c:
index e78d9bd..a797f2b 100644 (file)
@@ -5,6 +5,8 @@ libgstrtp_la_SOURCES = \
        gstrtp.c \
        gstrtpdepay.c \
        gstrtpac3depay.c \
+       gstrtpdvdepay.c \
+       gstrtpdvpay.c \
        gstrtpilbcdepay.c \
        gstrtpilbcpay.c \
        gstrtpmpadepay.c \
@@ -61,6 +63,8 @@ noinst_HEADERS =                      \
                 gstrtpL16depay.h       \
                 gstrtpL16pay.h         \
                 gstrtpac3depay.h       \
+                gstrtpdvdepay.h        \
+                gstrtpdvpay.h          \
                 gstrtpamrdepay.h       \
                 gstrtpamrpay.h         \
                 gstrtpilbcdepay.h      \
index 5fde406..5cf19a5 100644 (file)
@@ -23,6 +23,8 @@
 
 #include "gstrtpdepay.h"
 #include "gstrtpac3depay.h"
+#include "gstrtpdvdepay.h"
+#include "gstrtpdvpay.h"
 #include "gstrtpilbcdepay.h"
 #include "gstrtpilbcpay.h"
 #include "gstrtppcmupay.h"
@@ -70,6 +72,12 @@ plugin_init (GstPlugin * plugin)
   if (!gst_rtp_ac3_depay_plugin_init (plugin))
     return FALSE;
 
+  if (!gst_rtp_dv_depay_plugin_init (plugin))
+    return FALSE;
+
+  if (!gst_rtp_dv_pay_plugin_init (plugin))
+    return FALSE;
+
   if (!gst_rtp_ilbc_pay_plugin_init (plugin))
     return FALSE;
 
diff --git a/gst/rtp/gstrtpdvdepay.c b/gst/rtp/gstrtpdvdepay.c
new file mode 100644 (file)
index 0000000..c61b20f
--- /dev/null
@@ -0,0 +1,419 @@
+/* Farsight
+ * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
+ *           (C) 2008 Wim Taymans <wim.taymans@gmail.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.
+ */
+
+/*
+ * RTP DV depayloader.
+ *
+ * Important note for NTSC-users:
+ *
+ * Because the author uses PAL video, and he does not have proper DV
+ * documentation (the DV format specification is not freely available),
+ * this code may very well contain PAL-specific assumptions.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <gst/gst.h>
+
+#include "gstrtpdvdepay.h"
+
+GST_DEBUG_CATEGORY (rtpdvdepay_debug);
+#define GST_CAT_DEFAULT (rtpdvdepay_debug)
+/* Filter signals and args */
+enum
+{
+  /* FILL ME */
+  LAST_SIGNAL
+};
+
+enum
+{
+  ARG_0,
+};
+
+static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/x-dv")
+    );
+
+static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("application/x-rtp, "
+        "media = (string) { \"video\", \"audio\" },"
+        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
+        "encoding-name = (string) \"DV\", "
+        "clock-rate = (int) 90000,"
+        "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
+        "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
+        "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
+        "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
+        /* optional parameters can't go in the template
+         * "audio = (string) { \"bundled\", \"none\" }"
+         */
+    )
+    );
+
+static GstStateChangeReturn
+gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition);
+
+static GstBuffer *gst_rtp_dv_depay_process (GstBaseRTPDepayload * base,
+    GstBuffer * in);
+static gboolean gst_rtp_dv_depay_setcaps (GstBaseRTPDepayload * depayload,
+    GstCaps * caps);
+
+GST_BOILERPLATE (GstRTPDVDepay, gst_rtp_dv_depay, GstBaseRTPDepayload,
+    GST_TYPE_BASE_RTP_DEPAYLOAD)
+
+     static void gst_rtp_dv_depay_base_init (gpointer g_class)
+{
+  static GstElementDetails plugin_details = {
+    "RTP DV Depayloader",
+    "Codec/Depayloader/Network",
+    "Depayloads DV from RTP packets (RFC 3189)",
+    "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>"
+  };
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&src_factory));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&sink_factory));
+
+  gst_element_class_set_details (element_class, &plugin_details);
+}
+
+/* initialize the plugin's class */
+static void
+gst_rtp_dv_depay_class_init (GstRTPDVDepayClass * klass)
+{
+  GstElementClass *gstelement_class = (GstElementClass *) klass;
+  GstBaseRTPDepayloadClass *gstbasertpdepayload_class =
+      (GstBaseRTPDepayloadClass *) klass;
+  gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
+
+  gstelement_class->change_state =
+      GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_change_state);
+
+  gstbasertpdepayload_class->process =
+      GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_process);
+  gstbasertpdepayload_class->set_caps =
+      GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_setcaps);
+
+  GST_DEBUG_CATEGORY_INIT (rtpdvdepay_debug, "rtpdvdepay", 0,
+      "DV RTP Depayloader");
+}
+
+/* initialize the new element
+ * instantiate pads and add them to element
+ * set functions
+ * initialize structure
+ */
+static void
+gst_rtp_dv_depay_init (GstRTPDVDepay * filter, GstRTPDVDepayClass * klass)
+{
+}
+
+static gboolean
+parse_encode (GstRTPDVDepay * rtpdvdepay, const gchar * encode)
+{
+  rtpdvdepay->width = 720;
+  if (!strcmp (encode, "314M-25/525-60")) {
+    rtpdvdepay->frame_size = 240000;
+    rtpdvdepay->height = 480;
+    rtpdvdepay->rate_num = 30000;
+    rtpdvdepay->rate_denom = 1001;
+  } else if (!strcmp (encode, "SD-VCR/525-60")) {
+    rtpdvdepay->frame_size = 120000;
+    rtpdvdepay->height = 480;
+    rtpdvdepay->rate_num = 30000;
+    rtpdvdepay->rate_denom = 1001;
+  } else if (!strcmp (encode, "314M-50/625-50")) {
+    rtpdvdepay->frame_size = 288000;
+    rtpdvdepay->height = 576;
+    rtpdvdepay->rate_num = 25;
+    rtpdvdepay->rate_denom = 1;
+  } else if (!strcmp (encode, "SD-VCR/625-50")) {
+    rtpdvdepay->frame_size = 144000;
+    rtpdvdepay->height = 576;
+    rtpdvdepay->rate_num = 25;
+    rtpdvdepay->rate_denom = 1;
+  } else if (!strcmp (encode, "314M-25/625-50")) {
+    rtpdvdepay->frame_size = 144000;
+    rtpdvdepay->height = 576;
+    rtpdvdepay->rate_num = 25;
+    rtpdvdepay->rate_denom = 1;
+  } else
+    rtpdvdepay->frame_size = -1;
+
+  return rtpdvdepay->frame_size != -1;
+}
+
+static gboolean
+gst_rtp_dv_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
+{
+  GstStructure *structure;
+  GstRTPDVDepay *rtpdvdepay;
+  GstCaps *srccaps;
+  gint clock_rate = 90000;      /* default */
+  gboolean systemstream, ret;
+  const gchar *encode, *media;
+
+  rtpdvdepay = GST_RTP_DV_DEPAY (depayload);
+
+  structure = gst_caps_get_structure (caps, 0);
+
+  if (gst_structure_has_field (structure, "clock-rate"))
+    gst_structure_get_int (structure, "clock-rate", &clock_rate);
+
+  /* we really need the encode property to figure out the frame size, it's also
+   * required by the spec */
+  if (!(encode = gst_structure_get_string (structure, "encode")))
+    goto no_encode;
+
+  /* figure out the size of one frame */
+  if (!parse_encode (rtpdvdepay, encode))
+    goto unknown_encode;
+
+  /* check the media, this tells us that the stream has video or not */
+  if (!(media = gst_structure_get_string (structure, "media")))
+    goto no_media;
+
+  systemstream = FALSE;
+
+  if (!strcmp (media, "audio")) {
+    /* we need a demuxer for audio only */
+    systemstream = TRUE;
+  } else if (!strcmp (media, "video")) {
+    const gchar *audio;
+
+    /* check the optional audio field, if it's present and set to bundled, we
+     * are dealing with a system stream. */
+    if ((audio = gst_structure_get_string (structure, "audio"))) {
+      if (!strcmp (audio, "bundled"))
+        systemstream = TRUE;
+    }
+  }
+
+  /* allocate accumulator */
+  rtpdvdepay->acc = gst_buffer_new_and_alloc (rtpdvdepay->frame_size);
+
+  /* Initialize the new accumulator frame.
+   * If the previous frame exists, copy that into the accumulator frame.
+   * This way, missing packets in the stream won't show up badly. */
+  memset (GST_BUFFER_DATA (rtpdvdepay->acc), 0, rtpdvdepay->frame_size);
+
+  srccaps = gst_caps_new_simple ("video/x-dv",
+      "systemstream", G_TYPE_BOOLEAN, systemstream,
+      "width", G_TYPE_INT, rtpdvdepay->width,
+      "height", G_TYPE_INT, rtpdvdepay->height,
+      "framerate", GST_TYPE_FRACTION, rtpdvdepay->rate_num,
+      rtpdvdepay->rate_denom, NULL);
+  ret = gst_pad_set_caps (depayload->srcpad, srccaps);
+  gst_caps_unref (srccaps);
+
+  depayload->clock_rate = clock_rate;
+  rtpdvdepay->negotiated = TRUE;
+
+  return ret;
+
+  /* ERRORS */
+no_encode:
+  {
+    GST_ERROR_OBJECT (rtpdvdepay, "required encode property not found in caps");
+    return FALSE;
+  }
+unknown_encode:
+  {
+    GST_ERROR_OBJECT (rtpdvdepay, "unknown encode property %s found", encode);
+    return FALSE;
+  }
+no_media:
+  {
+    GST_ERROR_OBJECT (rtpdvdepay, "required media property not found in caps");
+    return FALSE;
+  }
+}
+
+/* A DV frame consists of a bunch of 80-byte DIF blocks.
+ * Each DIF block contains a 3-byte header telling where in the DV frame the
+ * DIF block should go. We use this information to calculate its position.
+ */
+static guint
+calculate_difblock_location (guint8 * block)
+{
+  gint block_type, dif_sequence, dif_block;
+  guint location;
+
+  block_type = block[0] >> 5;
+  dif_sequence = block[1] >> 4;
+  dif_block = block[2];
+
+  switch (block_type) {
+    case 0:                    /* Header block */
+      location = dif_sequence * 150 * 80;
+      break;
+    case 1:                    /* Subcode block */
+      location = dif_sequence * 150 * 80 + (1 + dif_block) * 80;
+      break;
+    case 2:                    /* VAUX block */
+      location = dif_sequence * 150 * 80 + (3 + dif_block) * 80;
+      break;
+    case 3:                    /* Audio block */
+      location = dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80;
+      break;
+    case 4:                    /* Video block */
+      location = dif_sequence * 150 * 80 +
+          (7 + (dif_block / 15) + dif_block) * 80;
+      break;
+    default:                   /* Something bogus */
+      GST_DEBUG ("UNKNOWN BLOCK");
+      location = -1;
+      break;
+  }
+  return location;
+}
+
+/* Process one RTP packet. Accumulate RTP payload in the proper place in a DV
+ * frame, and return that frame if we detect a new frame, or NULL otherwise.
+ * We assume a DV frame is 144000 bytes. That should accomodate PAL as well as
+ * NTSC.
+ */
+static GstBuffer *
+gst_rtp_dv_depay_process (GstBaseRTPDepayload * base, GstBuffer * in)
+{
+  GstBuffer *out = NULL;
+  guint8 *payload;
+  guint32 rtp_ts;
+  guint payload_len, location;
+  GstRTPDVDepay *dvdepay = GST_RTP_DV_DEPAY (base);
+  gboolean marker;
+
+  if (!dvdepay->negotiated)
+    goto not_negotiated;
+
+  marker = gst_rtp_buffer_get_marker (in);
+
+  /* Check if the received packet contains (the start of) a new frame, we do
+   * this by checking the RTP timestamp. */
+  rtp_ts = gst_rtp_buffer_get_timestamp (in);
+
+  /* we cannot copy the packet yet if the marker is set, we will do that below
+   * after taking out the data */
+  if (dvdepay->prev_ts != -1 && rtp_ts != dvdepay->prev_ts && !marker) {
+    /* the timestamp changed */
+    GST_DEBUG_OBJECT (dvdepay, "new frame with ts %u, old ts %u", rtp_ts,
+        dvdepay->prev_ts);
+
+    /* return copy of accumulator. */
+    out = gst_buffer_copy (dvdepay->acc);
+  }
+
+  /* Extract the payload */
+  payload_len = gst_rtp_buffer_get_payload_len (in);
+  payload = gst_rtp_buffer_get_payload (in);
+
+  /* copy all DIF chunks in their place. */
+  while (payload_len >= 80) {
+    /* Calculate where in the frame the payload should go */
+    location = calculate_difblock_location (payload);
+
+    /* Check if we received a header. We will not pass on frames until
+     * we've received a header, otherwise the DV decoder goes wacko. */
+    if (location == 0)
+      dvdepay->have_header = TRUE;
+
+    /* And copy it in, provided the location is sane. */
+    if (location >= 0 && location <= dvdepay->frame_size - 80)
+      memcpy (GST_BUFFER_DATA (dvdepay->acc) + location, payload, 80);
+
+    payload += 80;
+    payload_len -= 80;
+  }
+
+  if (marker) {
+    /* The marker marks the end of a frame that we need to push. The next frame
+     * will change the timestamp but we won't copy the accumulator again because
+     * we set the prev_ts to -1. */
+    out = gst_buffer_copy (dvdepay->acc);
+    GST_DEBUG_OBJECT (dvdepay, "marker bit complete frame %u", rtp_ts);
+    dvdepay->prev_ts = -1;
+  } else {
+    /* save last timestamp */
+    dvdepay->prev_ts = rtp_ts;
+  }
+  return out;
+
+  /* ERRORS */
+not_negotiated:
+  {
+    GST_ELEMENT_ERROR (dvdepay, STREAM, NOT_IMPLEMENTED,
+        (NULL), ("not negotiated"));
+    return NULL;
+  }
+}
+
+static void
+gst_rtp_dv_depay_reset (GstRTPDVDepay * depay)
+{
+  if (depay->acc)
+    gst_buffer_unref (depay->acc);
+  depay->acc = NULL;
+
+  depay->prev_ts = -1;
+  depay->have_header = FALSE;
+  depay->frame_nr = 0;
+  depay->negotiated = FALSE;
+}
+
+static GstStateChangeReturn
+gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition)
+{
+  GstStateChangeReturn ret;
+  GstRTPDVDepay *depay = GST_RTP_DV_DEPAY (element);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_READY_TO_PAUSED:
+      gst_rtp_dv_depay_reset (depay);
+      break;
+    default:
+      break;
+  }
+
+  ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
+      (element, transition), GST_STATE_CHANGE_FAILURE);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_PAUSED_TO_READY:
+      gst_rtp_dv_depay_reset (depay);
+      break;
+    default:
+      break;
+  }
+  return ret;
+}
+
+gboolean
+gst_rtp_dv_depay_plugin_init (GstPlugin * plugin)
+{
+  return gst_element_register (plugin, "rtpdvdepay",
+      GST_RANK_NONE, GST_TYPE_RTP_DV_DEPAY);
+}
diff --git a/gst/rtp/gstrtpdvdepay.h b/gst/rtp/gstrtpdvdepay.h
new file mode 100644 (file)
index 0000000..70234ca
--- /dev/null
@@ -0,0 +1,67 @@
+/* Farsight
+ * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
+ *
+ * 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 __GSTRTPDVDEPAY_H__
+#define __GSTRTPDVDEPAY_H__
+
+#include <gst/gst.h>
+#include <gst/rtp/gstbasertpdepayload.h>
+
+G_BEGIN_DECLS
+
+/* #define's don't like whitespacey bits */
+#define GST_TYPE_RTP_DV_DEPAY (gst_rtp_dv_depay_get_type())
+#define GST_RTP_DV_DEPAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_DV_DEPAY,GstRTPDVDepay))
+#define GST_RTP_DV_DEPAY_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_DV_DEPAY,GstRTPDVDepay))
+#define GST_IS_RTP_DV_DEPAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_DV_DEPAY))
+#define GST_IS_RTP_DV_DEPAY_CLASS(obj) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_DV_DEPAY))
+
+typedef struct _GstRTPDVDepay      GstRTPDVDepay;
+typedef struct _GstRTPDVDepayClass GstRTPDVDepayClass;
+
+struct _GstRTPDVDepay
+{
+  GstBaseRTPDepayload parent;
+
+  GstBuffer *acc;
+  guint frame_size;
+  guint frame_nr;
+  guint32 prev_ts;
+  gboolean have_header;
+
+  gint width, height;
+  gint rate_num, rate_denom;
+  gboolean negotiated;
+};
+
+struct _GstRTPDVDepayClass
+{
+  GstBaseRTPDepayloadClass parent_class;
+};
+
+GType gst_rtp_dv_depay_get_type (void);
+gboolean gst_rtp_dv_depay_plugin_init (GstPlugin * plugin);
+
+G_END_DECLS
+
+#endif /* __GSTRTPDVDEPAY_H__ */
diff --git a/gst/rtp/gstrtpdvpay.c b/gst/rtp/gstrtpdvpay.c
new file mode 100644 (file)
index 0000000..65cfaf9
--- /dev/null
@@ -0,0 +1,386 @@
+/* Farsight
+ * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
+ *           (C) 2008 Wim Taymans <wim.taymans@gmail.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 <stdlib.h>
+#include <string.h>
+#include <gst/rtp/gstrtpbuffer.h>
+
+#include "gstrtpdvpay.h"
+
+GST_DEBUG_CATEGORY (rtpdvpay_debug);
+#define GST_CAT_DEFAULT (rtpdvpay_debug)
+
+/* Elementfactory information */
+static GstElementDetails gst_rtp_dv_pay_details = {
+  "RTP DV Payloader",
+  "Codec/Payloader/Network",
+  "Payloads DV into RTP packets (RFC 3189)",
+  "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>"
+};
+
+#define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
+enum
+{
+  PROP_0,
+  PROP_MODE
+};
+
+/* takes both system and non-system streams */
+static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
+GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("video/x-dv")
+    );
+
+static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
+GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("application/x-rtp, "
+        "media = (string) { \"video\", \"audio\" } ,"
+        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
+        "encoding-name = (string) \"DV\", "
+        "clock-rate = (int) 90000,"
+        "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
+        "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
+        "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
+        "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
+        /* optional parameters can't go in the template
+         * "audio = (string) { \"bundled\", \"none\" }"
+         */
+    )
+    );
+
+static gboolean gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload,
+    GstCaps * caps);
+static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * payload,
+    GstBuffer * buffer);
+
+#define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
+static GType
+gst_dv_pay_mode_get_type (void)
+{
+  static GType dv_pay_mode_type = 0;
+  static const GEnumValue dv_pay_modes[] = {
+    {GST_DV_PAY_MODE_VIDEO, "Video only", "video"},
+    {GST_DV_PAY_MODE_BUNDLED, "Video and Audio bundled", "bundled"},
+    {GST_DV_PAY_MODE_AUDIO, "Audio only", "audio"},
+    {0, NULL, NULL},
+  };
+
+  if (!dv_pay_mode_type) {
+    dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
+  }
+  return dv_pay_mode_type;
+}
+
+
+static void gst_dv_pay_set_property (GObject * object,
+    guint prop_id, const GValue * value, GParamSpec * pspec);
+static void gst_dv_pay_get_property (GObject * object,
+    guint prop_id, GValue * value, GParamSpec * pspec);
+
+GST_BOILERPLATE (GstRTPDVPay, gst_rtp_dv_pay, GstBaseRTPPayload,
+    GST_TYPE_BASE_RTP_PAYLOAD)
+
+     static void gst_rtp_dv_pay_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&gst_rtp_dv_pay_sink_template));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&gst_rtp_dv_pay_src_template));
+  gst_element_class_set_details (element_class, &gst_rtp_dv_pay_details);
+}
+
+static void
+gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
+{
+  GObjectClass *gobject_class;
+  GstElementClass *gstelement_class;
+  GstBaseRTPPayloadClass *gstbasertppayload_class;
+
+  gobject_class = (GObjectClass *) klass;
+  gstelement_class = (GstElementClass *) klass;
+  gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
+
+  gobject_class->set_property = gst_dv_pay_set_property;
+  gobject_class->get_property = gst_dv_pay_get_property;
+
+  gstbasertppayload_class->set_caps = gst_rtp_dv_pay_setcaps;
+  gstbasertppayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
+
+  g_object_class_install_property (gobject_class, PROP_MODE,
+      g_param_spec_enum ("mode", "Mode",
+          "The payload mode of payloading",
+          GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
+}
+
+static void
+gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay, GstRTPDVPayClass * klass)
+{
+}
+
+static void
+gst_dv_pay_set_property (GObject * object,
+    guint prop_id, const GValue * value, GParamSpec * pspec)
+{
+  GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
+
+  switch (prop_id) {
+    case PROP_MODE:
+      rtpdvpay->mode = g_value_get_enum (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_dv_pay_get_property (GObject * object,
+    guint prop_id, GValue * value, GParamSpec * pspec)
+{
+  GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
+
+  switch (prop_id) {
+    case PROP_MODE:
+      g_value_set_enum (value, rtpdvpay->mode);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static gboolean
+gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
+{
+  GstRTPDVPay *rtpdvpay;
+
+  rtpdvpay = GST_RTP_DV_PAY (payload);
+
+  /* We don't do anything here, but we could check if it's a system stream and if
+   * it's not, default to sending the video only. We will negotiate downstream
+   * caps when we get to see the first frame. */
+
+  return TRUE;
+}
+
+static gboolean
+gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, guint size)
+{
+  gchar *encode, *media;
+  gboolean audio_bundled;
+
+  if ((data[3] & 0x80) == 0) {  /* DSF flag */
+    /* it's an NTSC format */
+    if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
+      /* NTSC 50Mbps */
+      encode = "314M-25/525-60";
+    } else {                    /* 4:1:1 sampling */
+      /* NTSC 25Mbps */
+      encode = "SD-VCR/525-60";
+    }
+  } else {
+    /* it's a PAL format */
+    if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
+      /* PAL 50Mbps */
+      encode = "314M-50/625-50";
+    } else if ((data[5] & 0x07) == 0) { /* APT flag */
+      /* PAL 25Mbps 4:2:0 */
+      encode = "SD-VCR/625-50";
+    } else
+      /* PAL 25Mbps 4:1:1 */
+      encode = "314M-25/625-50";
+  }
+
+  media = "video";
+  audio_bundled = FALSE;
+
+  switch (rtpdvpay->mode) {
+    case GST_DV_PAY_MODE_AUDIO:
+      media = "audio";
+      break;
+    case GST_DV_PAY_MODE_BUNDLED:
+      audio_bundled = TRUE;
+      break;
+    default:
+      break;
+  }
+  gst_basertppayload_set_options (GST_BASE_RTP_PAYLOAD (rtpdvpay), media, TRUE,
+      "DV", 90000);
+
+  if (audio_bundled) {
+    gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
+        "encode", G_TYPE_STRING, encode,
+        "audio", G_TYPE_STRING, "bundled", NULL);
+  } else {
+    gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
+        "encode", G_TYPE_STRING, encode, NULL);
+  }
+  return TRUE;
+}
+
+static gboolean
+include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
+{
+  gint block_type;
+  gboolean res;
+
+  block_type = data[0] >> 5;
+
+  switch (block_type) {
+    case 0:                    /* Header block */
+    case 1:                    /* Subcode block */
+    case 2:                    /* VAUX block */
+      /* always include these blocks */
+      res = TRUE;
+      break;
+    case 3:                    /* Audio block */
+      /* never include audio if we are doing video only */
+      if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
+        res = FALSE;
+      else
+        res = TRUE;
+      break;
+    case 4:                    /* Video block */
+      /* never include video if we are doing audio only */
+      if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
+        res = FALSE;
+      else
+        res = TRUE;
+      break;
+    default:                   /* Something bogus, just ignore */
+      res = FALSE;
+      break;
+  }
+  return res;
+}
+
+/* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
+ */
+static GstFlowReturn
+gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
+    GstBuffer * buffer)
+{
+  GstRTPDVPay *rtpdvpay;
+  guint max_payload_size;
+  GstBuffer *outbuf;
+  GstFlowReturn ret = GST_FLOW_OK;
+  gint hdrlen;
+  guint size;
+  guint8 *data;
+  guint8 *dest;
+  guint filled;
+
+  rtpdvpay = GST_RTP_DV_PAY (basepayload);
+
+  hdrlen = gst_rtp_buffer_calc_header_len (0);
+  /* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
+   * each, and we should put an integral number of them in each RTP packet.
+   * Therefore, we round the available room down to the nearest multiple of 80.
+   *
+   * The available room is just the packet MTU, minus the RTP header length. */
+  max_payload_size = ((GST_BASE_RTP_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
+
+  /* The length of the buffer to transmit. */
+  size = GST_BUFFER_SIZE (buffer);
+  data = GST_BUFFER_DATA (buffer);
+
+  GST_DEBUG_OBJECT (rtpdvpay,
+      "DV RTP payloader got buffer of %u bytes, splitting in %u byte "
+      "payload fragments, at time %" GST_TIME_FORMAT, size, max_payload_size,
+      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
+
+  if (!rtpdvpay->negotiated) {
+    gst_dv_pay_negotiate (rtpdvpay, data, size);
+    /* if we have not yet scanned the stream for its type, do so now */
+    rtpdvpay->negotiated = TRUE;
+  }
+
+  outbuf = NULL;
+  dest = NULL;
+  filled = 0;
+
+  /* while we have a complete DIF chunks left */
+  while (size >= 80) {
+    /* Allocate a new buffer, set the timestamp */
+    if (outbuf == NULL) {
+      outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
+      GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
+      dest = gst_rtp_buffer_get_payload (outbuf);
+      filled = 0;
+    }
+
+    /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
+    if (include_dif (rtpdvpay, data)) {
+      /* copy data in packet */
+      memcpy (dest, data, 80);
+
+      dest += 80;
+      filled += 80;
+    }
+
+    /* go to next dif chunk */
+    size -= 80;
+    data += 80;
+
+    /* push out the buffer if the next one would exceed the max packet size or
+     * when we are pushing the last packet */
+    if (filled + 80 > max_payload_size || size < 80) {
+      if (size < 160) {
+        guint hlen;
+
+        /* set marker */
+        gst_rtp_buffer_set_marker (outbuf, TRUE);
+
+        /* shrink buffer to last packet */
+        hlen = gst_rtp_buffer_get_header_len (outbuf);
+        gst_rtp_buffer_set_packet_len (outbuf, hlen + filled);
+      }
+      /* Push out the created piece, and check for errors. */
+      ret = gst_basertppayload_push (basepayload, outbuf);
+      if (ret != GST_FLOW_OK)
+        break;
+
+      outbuf = NULL;
+    }
+  }
+  gst_buffer_unref (buffer);
+
+  return ret;
+}
+
+gboolean
+gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
+{
+  return gst_element_register (plugin, "rtpdvpay",
+      GST_RANK_NONE, GST_TYPE_RTP_DV_PAY);
+}
diff --git a/gst/rtp/gstrtpdvpay.h b/gst/rtp/gstrtpdvpay.h
new file mode 100644 (file)
index 0000000..5aaf971
--- /dev/null
@@ -0,0 +1,67 @@
+/* Farsight
+ * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
+ *
+ * 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 __GSTRTPDVPAY_H__
+#define __GSTRTPDVPAY_H__
+
+#include <gst/gst.h>
+#include <gst/rtp/gstbasertppayload.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GstRTPDVPay GstRTPDVPay;
+typedef struct _GstRTPDVPayClass GstRTPDVPayClass;
+
+#define GST_TYPE_RTP_DV_PAY \
+  (gst_rtp_dv_pay_get_type())
+#define GST_RTP_DV_PAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_DV_PAY,GstRTPDVPay))
+#define GST_RTP_DV_PAY_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_DV_PAY,GstRTPDVPay))
+#define GST_IS_RTP_DV_PAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_DV_PAY))
+#define GST_IS_RTP_DV_PAY_CLASS(obj) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_DV_PAY))
+
+typedef enum
+{
+  GST_DV_PAY_MODE_VIDEO,
+  GST_DV_PAY_MODE_BUNDLED,
+  GST_DV_PAY_MODE_AUDIO
+} GstDVPayMode;
+
+struct _GstRTPDVPay
+{
+  GstBaseRTPPayload payload;
+
+  gboolean negotiated;
+  GstDVPayMode mode;
+};
+
+struct _GstRTPDVPayClass
+{
+  GstBaseRTPPayloadClass parent_class;
+};
+
+gboolean gst_rtp_dv_pay_plugin_init (GstPlugin * plugin);
+
+G_END_DECLS
+
+#endif /* __GSTRTPDVPAY_H__ */