vrawpay: Error out cleanly if mapping the video frame fails
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpvrawpay.c
index c354639..9368936 100644 (file)
@@ -13,8 +13,8 @@
  *
  * 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <string.h>
 
 #include <gst/rtp/gstrtpbuffer.h>
+#include <gst/video/video.h>
 
 #include "gstrtpvrawpay.h"
+#include "gstrtputils.h"
+
+enum
+{
+  PROP_CHUNKS_PER_FRAME = 1
+};
+
+#define DEFAULT_CHUNKS_PER_FRAME 10
 
 GST_DEBUG_CATEGORY_STATIC (rtpvrawpay_debug);
 #define GST_CAT_DEFAULT (rtpvrawpay_debug)
@@ -34,46 +43,8 @@ static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
     GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("video/x-raw-rgb, "
-        "bpp = (int) 24, "
-        "depth = (int) 24, "
-        "endianness = (int) BIG_ENDIAN, "
-        "red_mask = (int) 0xFF000000, "
-        "green_mask = (int) 0x00FF0000, "
-        "blue_mask = (int) 0x0000FF00, "
-        "width = (int) [ 1, 32767 ], "
-        "height = (int) [ 1, 32767 ]; "
-        "video/x-raw-rgb, "
-        "bpp = (int) 32, "
-        "depth = (int) 32, "
-        "endianness = (int) BIG_ENDIAN, "
-        "red_mask = (int) 0xFF000000, "
-        "green_mask = (int) 0x00FF0000, "
-        "blue_mask = (int) 0x0000FF00, "
-        "alpha_mask = (int) 0x000000FF, "
-        "width = (int) [ 1, 32767 ], "
-        "height = (int) [ 1, 32767 ]; "
-        "video/x-raw-rgb, "
-        "bpp = (int) 24, "
-        "depth = (int) 24, "
-        "endianness = (int) BIG_ENDIAN, "
-        "red_mask = (int) 0x0000FF00, "
-        "green_mask = (int) 0x00FF0000, "
-        "blue_mask = (int) 0xFF000000, "
-        "width = (int) [ 1, 32767 ], "
-        "height = (int) [ 1, 32767 ]; "
-        "video/x-raw-rgb, "
-        "bpp = (int) 32, "
-        "depth = (int) 32, "
-        "endianness = (int) BIG_ENDIAN, "
-        "red_mask = (int) 0x0000FF00, "
-        "green_mask = (int) 0x00FF0000, "
-        "blue_mask = (int) 0xFF000000, "
-        "alpha_mask = (int) 0x000000FF, "
-        "width = (int) [ 1, 32767 ], "
-        "height = (int) [ 1, 32767 ]; "
-        "video/x-raw-yuv, "
-        "format = (fourcc) { AYUV, UYVY, I420, Y41B, UYVP }, "
+    GST_STATIC_CAPS ("video/x-raw, "
+        "format = (string) { RGB, RGBA, BGR, BGRA, AYUV, UYVY, I420, Y41B, UYVP }, "
         "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
     );
 
@@ -104,229 +75,166 @@ GST_STATIC_PAD_TEMPLATE ("src",
     )
     );
 
-static gboolean gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload,
+static gboolean gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload,
     GstCaps * caps);
-static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload *
+static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload *
     payload, GstBuffer * buffer);
+static void gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
+static void gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
 
-GST_BOILERPLATE (GstRtpVRawPay, gst_rtp_vraw_pay, GstBaseRTPPayload,
-    GST_TYPE_BASE_RTP_PAYLOAD)
+G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_RTP_BASE_PAYLOAD)
 
-     static void gst_rtp_vraw_pay_base_init (gpointer klass)
+     static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
 {
-  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
-
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_rtp_vraw_pay_src_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_rtp_vraw_pay_sink_template));
-
-  gst_element_class_set_details_simple (element_class,
+  GstRTPBasePayloadClass *gstrtpbasepayload_class;
+  GstElementClass *gstelement_class;
+  GObjectClass *gobject_class;
+
+  gobject_class = (GObjectClass *) klass;
+  gstelement_class = (GstElementClass *) klass;
+  gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
+
+  gobject_class->set_property = gst_rtp_vraw_pay_set_property;
+  gobject_class->get_property = gst_rtp_vraw_pay_get_property;
+
+  g_object_class_install_property (gobject_class,
+      PROP_CHUNKS_PER_FRAME,
+      g_param_spec_int ("chunks-per-frame", "Chunks per Frame",
+          "Split and send out each frame in multiple chunks to reduce overhead",
+          1, G_MAXINT, DEFAULT_CHUNKS_PER_FRAME,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
+      );
+
+  gstrtpbasepayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
+  gstrtpbasepayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
+
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_vraw_pay_src_template);
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_vraw_pay_sink_template);
+
+  gst_element_class_set_static_metadata (gstelement_class,
       "RTP Raw Video payloader", "Codec/Payloader/Network/RTP",
       "Payload raw video as RTP packets (RFC 4175)",
       "Wim Taymans <wim.taymans@gmail.com>");
-}
-
-static void
-gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
-{
-  GstBaseRTPPayloadClass *gstbasertppayload_class;
-
-  gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
-
-  gstbasertppayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
-  gstbasertppayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
 
   GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
       "Raw video RTP Payloader");
 }
 
 static void
-gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay, GstRtpVRawPayClass * klass)
+gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
 {
+  rtpvrawpay->chunks_per_frame = DEFAULT_CHUNKS_PER_FRAME;
 }
 
 static gboolean
-gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
+gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
 {
   GstRtpVRawPay *rtpvrawpay;
-  GstStructure *s;
   gboolean res;
-  const gchar *name;
-  gint width, height;
-  gint yp, up, vp;
-  gint pgroup, ystride, uvstride = 0, xinc, yinc;
-  GstVideoFormat sampling;
+  gint pgroup, xinc, yinc;
   const gchar *depthstr, *samplingstr, *colorimetrystr;
   gchar *wstr, *hstr;
-  gboolean interlaced;
-  const gchar *color_matrix;
-  gint depth;
+  GstVideoInfo info;
 
   rtpvrawpay = GST_RTP_VRAW_PAY (payload);
 
-  s = gst_caps_get_structure (caps, 0);
+  if (!gst_video_info_from_caps (&info, caps))
+    goto invalid_caps;
 
-  /* start parsing the format */
-  name = gst_structure_get_name (s);
+  rtpvrawpay->vinfo = info;
 
-  /* these values are the only thing we can do */
-  depthstr = "8";
-
-  /* parse common width/height */
-  res = gst_structure_get_int (s, "width", &width);
-  res &= gst_structure_get_int (s, "height", &height);
-  if (!res)
-    goto missing_dimension;
-
-  if (!gst_structure_get_boolean (s, "interlaced", &interlaced))
-    interlaced = FALSE;
-
-  color_matrix = gst_structure_get_string (s, "color-matrix");
-  colorimetrystr = "SMPTE240M";
-  if (color_matrix) {
-    if (g_str_equal (color_matrix, "sdtv")) {
-      /* BT.601 implies a bit more than just color-matrix */
-      colorimetrystr = "BT601-5";
-    } else if (g_str_equal (color_matrix, "hdtv")) {
-      colorimetrystr = "BT709-2";
-    }
+  if (gst_video_colorimetry_matches (&info.colorimetry,
+          GST_VIDEO_COLORIMETRY_BT601)) {
+    colorimetrystr = "BT601-5";
+  } else if (gst_video_colorimetry_matches (&info.colorimetry,
+          GST_VIDEO_COLORIMETRY_BT709)) {
+    colorimetrystr = "BT709-2";
+  } else if (gst_video_colorimetry_matches (&info.colorimetry,
+          GST_VIDEO_COLORIMETRY_SMPTE240M)) {
+    colorimetrystr = "SMPTE240M";
+  } else {
+    colorimetrystr = "SMPTE240M";
   }
 
-  yp = up = vp = 0;
   xinc = yinc = 1;
 
-  if (!strcmp (name, "video/x-raw-rgb")) {
-    gint amask, rmask;
-    gboolean has_alpha;
-
-    has_alpha = gst_structure_get_int (s, "alpha_mask", &amask);
-    depth = 8;
-
-    if (!gst_structure_get_int (s, "red_mask", &rmask))
-      goto unknown_mask;
+  /* these values are the only thing we can do */
+  depthstr = "8";
 
-    if (has_alpha) {
+  switch (GST_VIDEO_INFO_FORMAT (&info)) {
+    case GST_VIDEO_FORMAT_RGBA:
+      samplingstr = "RGBA";
       pgroup = 4;
-      ystride = width * 4;
-      if (rmask == 0xFF000000) {
-        sampling = GST_VIDEO_FORMAT_RGBA;
-        samplingstr = "RGBA";
-      } else {
-        sampling = GST_VIDEO_FORMAT_BGRA;
-        samplingstr = "BGRA";
-      }
-    } else {
+      break;
+    case GST_VIDEO_FORMAT_BGRA:
+      samplingstr = "BGRA";
+      pgroup = 4;
+      break;
+    case GST_VIDEO_FORMAT_RGB:
+      samplingstr = "RGB";
       pgroup = 3;
-      ystride = GST_ROUND_UP_4 (width * 3);
-      if (rmask == 0xFF000000) {
-        sampling = GST_VIDEO_FORMAT_RGB;
-        samplingstr = "RGB";
-      } else {
-        sampling = GST_VIDEO_FORMAT_BGR;
-        samplingstr = "BGR";
-      }
-    }
-  } else if (!strcmp (name, "video/x-raw-yuv")) {
-    guint32 fourcc;
-
-    if (!gst_structure_get_fourcc (s, "format", &fourcc))
-      goto unknown_fourcc;
-
-    GST_LOG_OBJECT (payload, "have fourcc %" GST_FOURCC_FORMAT,
-        GST_FOURCC_ARGS (fourcc));
-
-    switch (fourcc) {
-      case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
-        sampling = GST_VIDEO_FORMAT_AYUV;
-        samplingstr = "YCbCr-4:4:4";
-        pgroup = 3;
-        ystride = width * 4;
-        depth = 8;
-        break;
-      case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
-        sampling = GST_VIDEO_FORMAT_UYVY;
-        samplingstr = "YCbCr-4:2:2";
-        pgroup = 4;
-        xinc = 2;
-        ystride = GST_ROUND_UP_2 (width) * 2;
-        depth = 8;
-        break;
-      case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
-        sampling = GST_VIDEO_FORMAT_Y41B;
-        samplingstr = "YCbCr-4:1:1";
-        pgroup = 6;
-        xinc = 4;
-        ystride = GST_ROUND_UP_4 (width);
-        uvstride = GST_ROUND_UP_8 (width) / 4;
-        up = ystride * height;
-        vp = up + uvstride * height;
-        depth = 8;
-        break;
-      case GST_MAKE_FOURCC ('I', '4', '2', '0'):
-        sampling = GST_VIDEO_FORMAT_I420;
-        samplingstr = "YCbCr-4:2:0";
-        pgroup = 6;
-        xinc = yinc = 2;
-        ystride = GST_ROUND_UP_4 (width);
-        uvstride = GST_ROUND_UP_8 (width) / 2;
-        up = ystride * GST_ROUND_UP_2 (height);
-        vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
-        depth = 8;
-        break;
-      case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
-#define GST_VIDEO_FORMAT_UYVP GST_VIDEO_FORMAT_UYVY     /* FIXME */
-        sampling = GST_VIDEO_FORMAT_UYVP;
-        samplingstr = "YCbCr-4:2:2";
-        pgroup = 4;
-        xinc = 2;
-        ystride = GST_ROUND_UP_2 (width) * 2;
-        depth = 10;
-        break;
-      default:
-        goto unknown_fourcc;
-    }
-  } else
-    goto unknown_format;
+      break;
+    case GST_VIDEO_FORMAT_BGR:
+      samplingstr = "BGR";
+      pgroup = 3;
+      break;
+    case GST_VIDEO_FORMAT_AYUV:
+      samplingstr = "YCbCr-4:4:4";
+      pgroup = 3;
+      break;
+    case GST_VIDEO_FORMAT_UYVY:
+      samplingstr = "YCbCr-4:2:2";
+      pgroup = 4;
+      xinc = 2;
+      break;
+    case GST_VIDEO_FORMAT_Y41B:
+      samplingstr = "YCbCr-4:1:1";
+      pgroup = 6;
+      xinc = 4;
+      break;
+    case GST_VIDEO_FORMAT_I420:
+      samplingstr = "YCbCr-4:2:0";
+      pgroup = 6;
+      xinc = yinc = 2;
+      break;
+    case GST_VIDEO_FORMAT_UYVP:
+      samplingstr = "YCbCr-4:2:2";
+      pgroup = 5;
+      xinc = 2;
+      depthstr = "10";
+      break;
+    default:
+      goto unknown_format;
+      break;
+  }
 
-  if (interlaced) {
+  if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
     yinc *= 2;
   }
-  if (depth == 10) {
-    depthstr = "10";
-  }
 
-  rtpvrawpay->width = width;
-  rtpvrawpay->height = height;
-  rtpvrawpay->sampling = sampling;
   rtpvrawpay->pgroup = pgroup;
   rtpvrawpay->xinc = xinc;
   rtpvrawpay->yinc = yinc;
-  rtpvrawpay->yp = yp;
-  rtpvrawpay->up = up;
-  rtpvrawpay->vp = vp;
-  rtpvrawpay->ystride = ystride;
-  rtpvrawpay->uvstride = uvstride;
-  rtpvrawpay->interlaced = interlaced;
-  rtpvrawpay->depth = depth;
-
-  GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %d", width, height,
-      sampling);
-  GST_DEBUG_OBJECT (payload, "yp %d, up %d, vp %d", yp, up, vp);
-  GST_DEBUG_OBJECT (payload, "pgroup %d, ystride %d, uvstride %d", pgroup,
-      ystride, uvstride);
-
-  wstr = g_strdup_printf ("%d", rtpvrawpay->width);
-  hstr = g_strdup_printf ("%d", rtpvrawpay->height);
-
-  gst_basertppayload_set_options (payload, "video", TRUE, "RAW", 90000);
-  if (interlaced) {
-    res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
+
+  GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %s",
+      GST_VIDEO_INFO_WIDTH (&info), GST_VIDEO_INFO_HEIGHT (&info), samplingstr);
+  GST_DEBUG_OBJECT (payload, "xinc %d, yinc %d, pgroup %d", xinc, yinc, pgroup);
+
+  wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
+  hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
+
+  gst_rtp_base_payload_set_options (payload, "video", TRUE, "RAW", 90000);
+  if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
+    res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
         samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
         wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
         colorimetrystr, "interlace", G_TYPE_STRING, "true", NULL);
   } else {
-    res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
+    res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
         samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
         wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
         colorimetrystr, NULL);
@@ -337,9 +245,9 @@ gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
   return res;
 
   /* ERRORS */
-unknown_mask:
+invalid_caps:
   {
-    GST_ERROR_OBJECT (payload, "unknown red mask specified");
+    GST_ERROR_OBJECT (payload, "could not parse caps");
     return FALSE;
   }
 unknown_format:
@@ -347,65 +255,95 @@ unknown_format:
     GST_ERROR_OBJECT (payload, "unknown caps format");
     return FALSE;
   }
-unknown_fourcc:
-  {
-    GST_ERROR_OBJECT (payload, "invalid or missing fourcc");
-    return FALSE;
-  }
-missing_dimension:
-  {
-    GST_ERROR_OBJECT (payload, "missing width or height property");
-    return FALSE;
-  }
 }
 
 static GstFlowReturn
-gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
+gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
 {
   GstRtpVRawPay *rtpvrawpay;
   GstFlowReturn ret = GST_FLOW_OK;
+  gfloat packets_per_packline;
+  guint pgroups_per_packet;
+  guint packlines_per_list, buffers_per_list;
+  guint lines_delay;            /* after how many packed lines we push out a buffer list */
+  guint last_line;              /* last pack line number we pushed out a buffer list     */
   guint line, offset;
-  guint8 *data, *yp, *up, *vp;
+  guint8 *p0, *yp, *up, *vp;
   guint ystride, uvstride;
-  guint size, pgroup;
+  guint xinc, yinc;
+  guint pgroup;
   guint mtu;
   guint width, height;
-  gint field;
+  gint field, fields;
+  GstVideoFormat format;
+  GstVideoFrame frame;
+  gint interlaced;
+  gboolean use_buffer_lists;
+  GstBufferList *list = NULL;
+  GstRTPBuffer rtp = { NULL, };
 
   rtpvrawpay = GST_RTP_VRAW_PAY (payload);
 
-  data = GST_BUFFER_DATA (buffer);
-  size = GST_BUFFER_SIZE (buffer);
+  if (!gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ)) {
+    gst_buffer_unref (buffer);
+    return GST_FLOW_ERROR;
+  }
 
-  GST_LOG_OBJECT (rtpvrawpay, "new frame of %u bytes", size);
+  GST_LOG_OBJECT (rtpvrawpay, "new frame of %" G_GSIZE_FORMAT " bytes",
+      gst_buffer_get_size (buffer));
 
   /* get pointer and strides of the planes */
-  yp = data + rtpvrawpay->yp;
-  up = data + rtpvrawpay->up;
-  vp = data + rtpvrawpay->vp;
+  p0 = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
+  yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
+  up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
+  vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
 
-  ystride = rtpvrawpay->ystride;
-  uvstride = rtpvrawpay->uvstride;
+  ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
+  uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
 
-  mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
+  mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
 
   /* amount of bytes for one pixel */
   pgroup = rtpvrawpay->pgroup;
-  width = rtpvrawpay->width;
-  height = rtpvrawpay->height;
+  width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
+  height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
 
-  /* start with line 0, offset 0 */
+  interlaced = GST_VIDEO_INFO_IS_INTERLACED (&rtpvrawpay->vinfo);
+
+  format = GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo);
+
+  yinc = rtpvrawpay->yinc;
+  xinc = rtpvrawpay->xinc;
+
+  /* after how many packed lines we push out a buffer list */
+  lines_delay = GST_ROUND_UP_4 (height / rtpvrawpay->chunks_per_frame);
+
+  /* calculate how many buffers we expect to store in a single buffer list */
+  pgroups_per_packet = (mtu - (12 + 14)) / pgroup;
+  packets_per_packline = width / (xinc * pgroups_per_packet * 1.0);
+  packlines_per_list = height / (yinc * rtpvrawpay->chunks_per_frame);
+  buffers_per_list = packlines_per_list * packets_per_packline;
+  buffers_per_list = GST_ROUND_UP_8 (buffers_per_list);
+
+  use_buffer_lists = (rtpvrawpay->chunks_per_frame < (height / yinc));
+
+  fields = 1 + interlaced;
 
-  for (field = 0; field < 1 + rtpvrawpay->interlaced; field++) {
+  /* start with line 0, offset 0 */
+  for (field = 0; field < fields; field++) {
     line = field;
     offset = 0;
+    last_line = 0;
+
+    if (use_buffer_lists)
+      list = gst_buffer_list_new_sized (buffers_per_list);
 
     /* write all lines */
     while (line < height) {
-      guint left;
+      guint left, pack_line;
       GstBuffer *out;
       guint8 *outdata, *headers;
-      gboolean next_line;
+      gboolean next_line, complete = FALSE;
       guint length, cont, pixels;
 
       /* get the max allowed payload length size, we try to fill the complete MTU */
@@ -413,13 +351,14 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
       out = gst_rtp_buffer_new_allocate (left, 0, 0);
 
       if (field == 0) {
-        GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
+        GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer);
       } else {
-        GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer) +
+        GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer) +
             GST_BUFFER_DURATION (buffer) / 2;
       }
 
-      outdata = gst_rtp_buffer_get_payload (out);
+      gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
+      outdata = gst_rtp_buffer_get_payload (&rtp);
 
       GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
           mtu);
@@ -450,6 +389,13 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
       /* the headers start here */
       headers = outdata;
 
+      /* make sure we can fit at least *one* header and pixel */
+      if (!(left > (6 + pgroup))) {
+        gst_rtp_buffer_unmap (&rtp);
+        gst_buffer_unref (out);
+        goto too_small;
+      }
+
       /* while we can fit at least one header and one pixel */
       while (left > (6 + pgroup)) {
         /* we need a 6 bytes header */
@@ -457,7 +403,7 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
 
         /* get how may bytes we need for the remaining pixels */
         pixels = width - offset;
-        length = (pixels * pgroup) / rtpvrawpay->xinc;
+        length = (pixels * pgroup) / xinc;
 
         if (left >= length) {
           /* pixels and header fit completely, we will write them and skip to the
@@ -465,8 +411,8 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
           next_line = TRUE;
         } else {
           /* line does not fit completely, see how many pixels fit */
-          pixels = (left / pgroup) * rtpvrawpay->xinc;
-          length = (pixels * pgroup) / rtpvrawpay->xinc;
+          pixels = (left / pgroup) * xinc;
+          length = (pixels * pgroup) / xinc;
           next_line = FALSE;
         }
         GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
@@ -483,7 +429,7 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
 
         if (next_line) {
           /* go to next line we do this here to make the check below easier */
-          line += rtpvrawpay->yinc;
+          line += yinc;
         }
 
         /* calculate continuation marker */
@@ -524,14 +470,15 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
             "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
             cont);
 
-        switch (rtpvrawpay->sampling) {
+        switch (format) {
           case GST_VIDEO_FORMAT_RGB:
           case GST_VIDEO_FORMAT_RGBA:
           case GST_VIDEO_FORMAT_BGR:
           case GST_VIDEO_FORMAT_BGRA:
           case GST_VIDEO_FORMAT_UYVY:
-            offs /= rtpvrawpay->xinc;
-            memcpy (outdata, yp + (lin * ystride) + (offs * pgroup), length);
+          case GST_VIDEO_FORMAT_UYVP:
+            offs /= xinc;
+            memcpy (outdata, p0 + (lin * ystride) + (offs * pgroup), length);
             outdata += length;
             break;
           case GST_VIDEO_FORMAT_AYUV:
@@ -539,7 +486,7 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
             gint i;
             guint8 *datap;
 
-            datap = yp + (lin * ystride) + (offs * 4);
+            datap = p0 + (lin * ystride) + (offs * 4);
 
             for (i = 0; i < pixels; i++) {
               *outdata++ = datap[2];
@@ -557,8 +504,7 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
 
             yd1p = yp + (lin * ystride) + (offs);
             yd2p = yd1p + ystride;
-            uvoff =
-                (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
+            uvoff = (lin / yinc * uvstride) + (offs / xinc);
             udp = up + uvoff;
             vdp = vp + uvoff;
 
@@ -579,8 +525,7 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
             guint8 *ydp, *udp, *vdp;
 
             ydp = yp + (lin * ystride) + offs;
-            uvoff =
-                (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
+            uvoff = (lin / yinc * uvstride) + (offs / xinc);
             udp = up + uvoff;
             vdp = vp + uvoff;
 
@@ -595,6 +540,7 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
             break;
           }
           default:
+            gst_rtp_buffer_unmap (&rtp);
             gst_buffer_unref (out);
             goto unknown_sampling;
         }
@@ -605,18 +551,44 @@ gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
 
       if (line >= height) {
         GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
-        gst_rtp_buffer_set_marker (out, TRUE);
+        gst_rtp_buffer_set_marker (&rtp, TRUE);
+        complete = TRUE;
       }
+      gst_rtp_buffer_unmap (&rtp);
       if (left > 0) {
         GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
-        GST_BUFFER_SIZE (out) -= left;
+        gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
+      }
+
+      gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpvrawpay), out, buffer,
+          g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
+
+
+      /* Now either push out the buffer directly */
+      if (!use_buffer_lists) {
+        ret = gst_rtp_base_payload_push (payload, out);
+        continue;
       }
 
-      /* push buffer */
-      ret = gst_basertppayload_push (payload, out);
+      /* or add the buffer to buffer list ... */
+      gst_buffer_list_add (list, out);
+
+      /* .. and check if we need to push out the list */
+      pack_line = (line - field) / fields;
+      if (complete || (pack_line > last_line && pack_line % lines_delay == 0)) {
+        GST_LOG_OBJECT (rtpvrawpay, "pushing list of %u buffers up to pack "
+            "line %u", gst_buffer_list_length (list), pack_line);
+        ret = gst_rtp_base_payload_push_list (payload, list);
+        list = NULL;
+        if (!complete)
+          list = gst_buffer_list_new_sized (buffers_per_list);
+        last_line = pack_line;
+      }
     }
 
   }
+
+  gst_video_frame_unmap (&frame);
   gst_buffer_unref (buffer);
 
   return ret;
@@ -626,9 +598,54 @@ unknown_sampling:
   {
     GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
         (NULL), ("unimplemented sampling"));
+    gst_video_frame_unmap (&frame);
     gst_buffer_unref (buffer);
     return GST_FLOW_NOT_SUPPORTED;
   }
+too_small:
+  {
+    GST_ELEMENT_ERROR (payload, RESOURCE, NO_SPACE_LEFT,
+        (NULL), ("not enough space to send at least one pixel"));
+    gst_video_frame_unmap (&frame);
+    gst_buffer_unref (buffer);
+    return GST_FLOW_NOT_SUPPORTED;
+  }
+}
+
+static void
+gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  GstRtpVRawPay *rtpvrawpay;
+
+  rtpvrawpay = GST_RTP_VRAW_PAY (object);
+
+  switch (prop_id) {
+    case PROP_CHUNKS_PER_FRAME:
+      rtpvrawpay->chunks_per_frame = g_value_get_int (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  GstRtpVRawPay *rtpvrawpay;
+
+  rtpvrawpay = GST_RTP_VRAW_PAY (object);
+
+  switch (prop_id) {
+    case PROP_CHUNKS_PER_FRAME:
+      g_value_set_int (value, rtpvrawpay->chunks_per_frame);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
 }
 
 gboolean