gstrtpvrawpay.c 20 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GStreamer
 * Copyright (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
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.
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/video/video.h>
28 29

#include "gstrtpvrawpay.h"
30
#include "gstrtputils.h"
31

32 33 34 35 36 37
enum
{
  PROP_CHUNKS_PER_FRAME = 1
};

#define DEFAULT_CHUNKS_PER_FRAME 10
38

39 40 41 42 43 44 45
GST_DEBUG_CATEGORY_STATIC (rtpvrawpay_debug);
#define GST_CAT_DEFAULT (rtpvrawpay_debug)

static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
    GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
46
    GST_STATIC_CAPS ("video/x-raw, "
Wim Taymans's avatar
Wim Taymans committed
47
        "format = (string) { RGB, RGBA, BGR, BGRA, AYUV, UYVY, I420, Y41B, UYVP }, "
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
    );

static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"video\", "
        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
        "clock-rate = (int) 90000, "
        "encoding-name = (string) \"RAW\","
        "sampling = (string) { \"RGB\", \"RGBA\", \"BGR\", \"BGRA\", "
        "\"YCbCr-4:4:4\", \"YCbCr-4:2:2\", \"YCbCr-4:2:0\", "
        "\"YCbCr-4:1:1\" },"
        /* we cannot express these as strings 
         * "width = (string) [1 32767],"
         * "height = (string) [1 32767],"
         */
        "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
        "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
        /* optional 
         * interlace = 
         * top-field-first = 
         * chroma-position = (string) 
         * gamma = (float)
         */
    )
    );

Wim Taymans's avatar
Wim Taymans committed
78
static gboolean gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload,
79
    GstCaps * caps);
Wim Taymans's avatar
Wim Taymans committed
80
static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload *
81
    payload, GstBuffer * buffer);
82 83 84 85
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);
86

Wim Taymans's avatar
Wim Taymans committed
87
G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_RTP_BASE_PAYLOAD)
88

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
89
     static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
90
{
Wim Taymans's avatar
Wim Taymans committed
91
  GstRTPBasePayloadClass *gstrtpbasepayload_class;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
92
  GstElementClass *gstelement_class;
93
  GObjectClass *gobject_class;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
94

95
  gobject_class = (GObjectClass *) klass;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
96
  gstelement_class = (GstElementClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
97
  gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
98

99 100 101 102 103 104 105 106 107 108 109
  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)
      );

Wim Taymans's avatar
Wim Taymans committed
110 111
  gstrtpbasepayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
  gstrtpbasepayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
112

113 114 115 116
  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);
117

118
  gst_element_class_set_static_metadata (gstelement_class,
Wim Taymans's avatar
Wim Taymans committed
119
      "RTP Raw Video payloader", "Codec/Payloader/Network/RTP",
120 121
      "Payload raw video as RTP packets (RFC 4175)",
      "Wim Taymans <wim.taymans@gmail.com>");
122 123 124 125 126 127

  GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
      "Raw video RTP Payloader");
}

static void
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
128
gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
129
{
130
  rtpvrawpay->chunks_per_frame = DEFAULT_CHUNKS_PER_FRAME;
131 132 133
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
134
gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
135 136 137
{
  GstRtpVRawPay *rtpvrawpay;
  gboolean res;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
138
  gint pgroup, xinc, yinc;
139 140
  const gchar *depthstr, *samplingstr, *colorimetrystr;
  gchar *wstr, *hstr;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
141
  GstVideoInfo info;
142 143 144

  rtpvrawpay = GST_RTP_VRAW_PAY (payload);

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
145 146
  if (!gst_video_info_from_caps (&info, caps))
    goto invalid_caps;
147

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
148
  rtpvrawpay->vinfo = info;
149

150 151 152 153 154 155 156 157 158 159 160
  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";
161
  }
162

163 164
  xinc = yinc = 1;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
165 166
  /* these values are the only thing we can do */
  depthstr = "8";
167

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
168 169 170
  switch (GST_VIDEO_INFO_FORMAT (&info)) {
    case GST_VIDEO_FORMAT_RGBA:
      samplingstr = "RGBA";
171
      pgroup = 4;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
172 173 174 175 176 177 178
      break;
    case GST_VIDEO_FORMAT_BGRA:
      samplingstr = "BGRA";
      pgroup = 4;
      break;
    case GST_VIDEO_FORMAT_RGB:
      samplingstr = "RGB";
179
      pgroup = 3;
Edward Hervey's avatar
Edward Hervey committed
180
      break;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    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";
Wim Taymans's avatar
Wim Taymans committed
206
      pgroup = 5;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
207 208 209 210 211 212 213
      xinc = 2;
      depthstr = "10";
      break;
    default:
      goto unknown_format;
      break;
  }
214

215
  if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
216 217 218
    yinc *= 2;
  }

219 220 221 222
  rtpvrawpay->pgroup = pgroup;
  rtpvrawpay->xinc = xinc;
  rtpvrawpay->yinc = yinc;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
223 224 225
  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);
226

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
227 228
  wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
  hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
229

Wim Taymans's avatar
Wim Taymans committed
230
  gst_rtp_base_payload_set_options (payload, "video", TRUE, "RAW", 90000);
231
  if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
Wim Taymans's avatar
Wim Taymans committed
232
    res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
233 234 235 236
        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 {
Wim Taymans's avatar
Wim Taymans committed
237
    res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
238 239 240 241
        samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
        wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
        colorimetrystr, NULL);
  }
242 243 244
  g_free (wstr);
  g_free (hstr);

245
  return res;
246 247

  /* ERRORS */
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
248
invalid_caps:
249
  {
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
250
    GST_ERROR_OBJECT (payload, "could not parse caps");
251 252 253 254 255 256 257 258 259 260
    return FALSE;
  }
unknown_format:
  {
    GST_ERROR_OBJECT (payload, "unknown caps format");
    return FALSE;
  }
}

static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
261
gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
262 263 264
{
  GstRtpVRawPay *rtpvrawpay;
  GstFlowReturn ret = GST_FLOW_OK;
265 266 267
  gfloat packets_per_packline;
  guint pgroups_per_packet;
  guint packlines_per_list, buffers_per_list;
268 269
  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     */
270
  guint line, offset;
271
  guint8 *p0, *yp, *up, *vp;
272
  guint ystride, uvstride;
273
  guint xinc, yinc;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
274
  guint pgroup;
275 276
  guint mtu;
  guint width, height;
277
  gint field, fields;
278
  GstVideoFormat format;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
279 280
  GstVideoFrame frame;
  gint interlaced;
281 282
  gboolean use_buffer_lists;
  GstBufferList *list = NULL;
283
  GstRTPBuffer rtp = { NULL, };
284 285 286

  rtpvrawpay = GST_RTP_VRAW_PAY (payload);

287 288 289 290
  if (!gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ)) {
    gst_buffer_unref (buffer);
    return GST_FLOW_ERROR;
  }
291

292
  GST_LOG_OBJECT (rtpvrawpay, "new frame of %" G_GSIZE_FORMAT " bytes",
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
293
      gst_buffer_get_size (buffer));
294 295

  /* get pointer and strides of the planes */
296
  p0 = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
297 298 299
  yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
  up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
  vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
300

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
301 302
  ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
  uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
303

Wim Taymans's avatar
Wim Taymans committed
304
  mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
305 306 307

  /* amount of bytes for one pixel */
  pgroup = rtpvrawpay->pgroup;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
308 309
  width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
  height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
310

311
  interlaced = GST_VIDEO_INFO_IS_INTERLACED (&rtpvrawpay->vinfo);
312

313 314 315 316 317
  format = GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo);

  yinc = rtpvrawpay->yinc;
  xinc = rtpvrawpay->xinc;

318
  /* after how many packed lines we push out a buffer list */
319
  lines_delay = GST_ROUND_UP_4 (height / rtpvrawpay->chunks_per_frame);
320 321 322 323

  /* 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);
324
  packlines_per_list = height / (yinc * rtpvrawpay->chunks_per_frame);
325 326
  buffers_per_list = packlines_per_list * packets_per_packline;
  buffers_per_list = GST_ROUND_UP_8 (buffers_per_list);
327

328 329
  use_buffer_lists = (rtpvrawpay->chunks_per_frame < (height / yinc));

330 331
  fields = 1 + interlaced;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
332
  /* start with line 0, offset 0 */
333
  for (field = 0; field < fields; field++) {
334 335
    line = field;
    offset = 0;
336 337
    last_line = 0;

338 339
    if (use_buffer_lists)
      list = gst_buffer_list_new_sized (buffers_per_list);
340 341 342

    /* write all lines */
    while (line < height) {
343
      guint left, pack_line;
344 345
      GstBuffer *out;
      guint8 *outdata, *headers;
346
      gboolean next_line, complete = FALSE;
347 348 349 350 351 352 353
      guint length, cont, pixels;

      /* get the max allowed payload length size, we try to fill the complete MTU */
      left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
      out = gst_rtp_buffer_new_allocate (left, 0, 0);

      if (field == 0) {
354
        GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer);
355
      } else {
356
        GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer) +
357
            GST_BUFFER_DURATION (buffer) / 2;
358 359
      }

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
360 361
      gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
      outdata = gst_rtp_buffer_get_payload (&rtp);
362 363 364 365 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

      GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
          mtu);

      /*
       *   0                   1                   2                   3
       *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       *  |   Extended Sequence Number    |            Length             |
       *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       *  |F|          Line No            |C|           Offset            |
       *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       *  |            Length             |F|          Line No            |
       *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       *  |C|           Offset            |                               .
       *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               .
       *  .                                                               .
       *  .                 Two (partial) lines of video data             .
       *  .                                                               .
       *  +---------------------------------------------------------------+
       */

      /* need 2 bytes for the extended sequence number */
      *outdata++ = 0;
      *outdata++ = 0;
      left -= 2;

      /* the headers start here */
      headers = outdata;

392 393 394 395 396 397 398
      /* 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;
      }

399 400 401 402 403 404 405
      /* while we can fit at least one header and one pixel */
      while (left > (6 + pgroup)) {
        /* we need a 6 bytes header */
        left -= 6;

        /* get how may bytes we need for the remaining pixels */
        pixels = width - offset;
406
        length = (pixels * pgroup) / xinc;
407 408 409 410 411 412 413

        if (left >= length) {
          /* pixels and header fit completely, we will write them and skip to the
           * next line. */
          next_line = TRUE;
        } else {
          /* line does not fit completely, see how many pixels fit */
414 415
          pixels = (left / pgroup) * xinc;
          length = (pixels * pgroup) / xinc;
416 417 418 419 420
          next_line = FALSE;
        }
        GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
            pixels);
        left -= length;
421

422 423 424
        /* write length */
        *outdata++ = (length >> 8) & 0xff;
        *outdata++ = length & 0xff;
425

426 427 428
        /* write line no */
        *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
        *outdata++ = line & 0xff;
429

430 431
        if (next_line) {
          /* go to next line we do this here to make the check below easier */
432
          line += yinc;
433
        }
434

435 436
        /* calculate continuation marker */
        cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
437

438 439 440
        /* write offset and continuation marker */
        *outdata++ = ((offset >> 8) & 0x7f) | cont;
        *outdata++ = offset & 0xff;
441

442 443 444 445 446 447 448 449
        if (next_line) {
          /* reset offset */
          offset = 0;
          GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
        } else {
          offset += pixels;
          GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
        }
450

451
        if (!cont)
452
          break;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
      }
      GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
          (guint) (outdata - headers));

      /* second pass, read headers and write the data */
      while (TRUE) {
        guint offs, lin;

        /* read length and cont */
        length = (headers[0] << 8) | headers[1];
        lin = ((headers[2] & 0x7f) << 8) | headers[3];
        offs = ((headers[4] & 0x7f) << 8) | headers[5];
        cont = headers[4] & 0x80;
        pixels = length / pgroup;
        headers += 6;

        GST_LOG_OBJECT (payload,
            "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
            cont);

473
        switch (format) {
474 475 476 477 478
          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:
Wim Taymans's avatar
Wim Taymans committed
479
          case GST_VIDEO_FORMAT_UYVP:
480
            offs /= xinc;
481
            memcpy (outdata, p0 + (lin * ystride) + (offs * pgroup), length);
482 483 484 485 486 487 488
            outdata += length;
            break;
          case GST_VIDEO_FORMAT_AYUV:
          {
            gint i;
            guint8 *datap;

489
            datap = p0 + (lin * ystride) + (offs * 4);
490 491 492 493 494 495 496 497

            for (i = 0; i < pixels; i++) {
              *outdata++ = datap[2];
              *outdata++ = datap[1];
              *outdata++ = datap[3];
              datap += 4;
            }
            break;
498
          }
499 500 501 502 503 504 505 506
          case GST_VIDEO_FORMAT_I420:
          {
            gint i;
            guint uvoff;
            guint8 *yd1p, *yd2p, *udp, *vdp;

            yd1p = yp + (lin * ystride) + (offs);
            yd2p = yd1p + ystride;
507
            uvoff = (lin / yinc * uvstride) + (offs / xinc);
508 509 510 511 512 513 514 515 516 517 518 519
            udp = up + uvoff;
            vdp = vp + uvoff;

            for (i = 0; i < pixels; i++) {
              *outdata++ = *yd1p++;
              *outdata++ = *yd1p++;
              *outdata++ = *yd2p++;
              *outdata++ = *yd2p++;
              *outdata++ = *udp++;
              *outdata++ = *vdp++;
            }
            break;
520
          }
521 522 523 524 525 526 527
          case GST_VIDEO_FORMAT_Y41B:
          {
            gint i;
            guint uvoff;
            guint8 *ydp, *udp, *vdp;

            ydp = yp + (lin * ystride) + offs;
528
            uvoff = (lin / yinc * uvstride) + (offs / xinc);
529 530 531 532 533 534 535 536 537 538 539 540
            udp = up + uvoff;
            vdp = vp + uvoff;

            for (i = 0; i < pixels; i++) {
              *outdata++ = *udp++;
              *outdata++ = *ydp++;
              *outdata++ = *ydp++;
              *outdata++ = *vdp++;
              *outdata++ = *ydp++;
              *outdata++ = *ydp++;
            }
            break;
541
          }
542
          default:
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
543
            gst_rtp_buffer_unmap (&rtp);
544 545
            gst_buffer_unref (out);
            goto unknown_sampling;
546
        }
547 548 549

        if (!cont)
          break;
550 551
      }

552 553
      if (line >= height) {
        GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
554
        gst_rtp_buffer_set_marker (&rtp, TRUE);
555
        complete = TRUE;
556
      }
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
557
      gst_rtp_buffer_unmap (&rtp);
558 559
      if (left > 0) {
        GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
560
        gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
561
      }
562

563
      gst_rtp_copy_video_meta (rtpvrawpay, out, buffer);
564

565 566 567 568 569 570 571
      /* Now either push out the buffer directly */
      if (!use_buffer_lists) {
        ret = gst_rtp_base_payload_push (payload, out);
        continue;
      }

      /* or add the buffer to buffer list ... */
572 573
      gst_buffer_list_add (list, out);

574
      /* .. and check if we need to push out the list */
575 576 577 578 579 580 581
      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)
582
          list = gst_buffer_list_new_sized (buffers_per_list);
583 584
        last_line = pack_line;
      }
Wim Taymans's avatar
Wim Taymans committed
585
    }
586 587

  }
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
588 589

  gst_video_frame_unmap (&frame);
590 591 592 593 594 595 596 597 598
  gst_buffer_unref (buffer);

  return ret;

  /* ERRORS */
unknown_sampling:
  {
    GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
        (NULL), ("unimplemented sampling"));
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
599
    gst_video_frame_unmap (&frame);
600 601 602
    gst_buffer_unref (buffer);
    return GST_FLOW_NOT_SUPPORTED;
  }
603 604 605 606 607 608 609 610
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;
  }
611 612
}

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
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;
  }
}

649 650 651 652
gboolean
gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpvrawpay",
653
      GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);
654
}