gstrtph263depay.c 13.6 KB
Newer Older
1 2 3 4 5 6
/* GStreamer
 *
 * Copyright 2007 Nokia Corporation
 * Copyright 2007 Collabora Ltd,
 *  @author: Philippe Kalaf <philippe.kalaf@collabora.co.uk>
 *
7
 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
8
 *               <2007> Edward Hervey <bilboed@bilboed.com>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22 23
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
24 25 26 27 28 29 30 31 32
 */

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

#include <string.h>

#include <gst/rtp/gstrtpbuffer.h>
33
#include <gst/video/video.h>
34
#include "gstrtph263depay.h"
35
#include "gstrtputils.h"
36

37 38 39
GST_DEBUG_CATEGORY_STATIC (rtph263depay_debug);
#define GST_CAT_DEFAULT (rtph263depay_debug)

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#define GST_RFC2190A_HEADER_LEN 4
#define GST_RFC2190B_HEADER_LEN 8
#define GST_RFC2190C_HEADER_LEN 12

static GstStaticPadTemplate gst_rtp_h263_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-h263, "
        "variant = (string) \"itu\", " "h263version = (string) \"h263\"")
    );

static GstStaticPadTemplate gst_rtp_h263_depay_sink_template =
    GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"video\", "
        "payload = (int) " GST_RTP_PAYLOAD_H263_STRING ", "
59
        "clock-rate = (int) 90000; "
60 61 62
        /* optional SDP attribute:
         * "a-framesize = (string) \"1234-1234\", "
         */
63 64
        "application/x-rtp, "
        "media = (string) \"video\", "
65 66 67 68 69
        "clock-rate = (int) 90000, " "encoding-name = (string) \"H263\""
        /* optional SDP attribute:
         * "a-framesize = (string) \"1234-1234\", "
         */
    )
70 71
    );

Wim Taymans's avatar
Wim Taymans committed
72 73
#define gst_rtp_h263_depay_parent_class parent_class
G_DEFINE_TYPE (GstRtpH263Depay, gst_rtp_h263_depay,
Wim Taymans's avatar
Wim Taymans committed
74
    GST_TYPE_RTP_BASE_DEPAYLOAD);
75 76 77 78 79 80

static void gst_rtp_h263_depay_finalize (GObject * object);

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

Wim Taymans's avatar
Wim Taymans committed
81
static GstBuffer *gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload,
82
    GstRTPBuffer * rtp);
Wim Taymans's avatar
Wim Taymans committed
83
gboolean gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter,
84 85 86 87 88 89 90
    GstCaps * caps);

static void
gst_rtp_h263_depay_class_init (GstRtpH263DepayClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
Wim Taymans's avatar
Wim Taymans committed
91
  GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
92

Wim Taymans's avatar
Wim Taymans committed
93 94 95
  GST_DEBUG_CATEGORY_INIT (rtph263depay_debug, "rtph263depay", 0,
      "H263 Video RTP Depayloader");

96 97
  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
98
  gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
99 100 101 102

  gobject_class->finalize = gst_rtp_h263_depay_finalize;

  gstelement_class->change_state = gst_rtp_h263_depay_change_state;
103

104 105 106 107
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_h263_depay_src_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_h263_depay_sink_template);
Wim Taymans's avatar
Wim Taymans committed
108

109
  gst_element_class_set_static_metadata (gstelement_class,
Wim Taymans's avatar
Wim Taymans committed
110 111 112 113 114
      "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
      "Extracts H263 video from RTP packets (RFC 2190)",
      "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
      "Edward Hervey <bilboed@bilboed.com>");

115
  gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h263_depay_process;
Wim Taymans's avatar
Wim Taymans committed
116
  gstrtpbasedepayload_class->set_caps = gst_rtp_h263_depay_setcaps;
117 118 119
}

static void
Wim Taymans's avatar
Wim Taymans committed
120
gst_rtp_h263_depay_init (GstRtpH263Depay * rtph263depay)
121 122
{
  rtph263depay->adapter = gst_adapter_new ();
123 124 125

  rtph263depay->offset = 0;
  rtph263depay->leftover = 0;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
}

static void
gst_rtp_h263_depay_finalize (GObject * object)
{
  GstRtpH263Depay *rtph263depay;

  rtph263depay = GST_RTP_H263_DEPAY (object);

  g_object_unref (rtph263depay->adapter);
  rtph263depay->adapter = NULL;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

141 142 143 144 145
static gboolean
gst_rtp_h263_parse_framesize (GstRTPBaseDepayload * filter,
    const gchar * media_attr, GstCaps * srccaps)
{
  gchar *dimension, *endptr;
146
  gint width, height;
147 148
  GstStructure *d;

149
  width = g_ascii_strtoull (media_attr, &endptr, 10);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  if (width <= 0) {
    GST_ERROR_OBJECT (filter,
        "Framesize media attribute width out of valid range");
    return FALSE;
  } else if (*endptr != '-') {
    GST_ERROR_OBJECT (filter,
        "Framesize media attribute has invalid dimension separator");
    return FALSE;
  }

  dimension = endptr + 1;
  height = g_ascii_strtoull (dimension, &endptr, 10);
  if (height <= 0) {
    GST_ERROR_OBJECT (filter,
        "Framesize media attribute height out of valid range");
    return FALSE;
  } else if (*endptr != '\0') {
    GST_ERROR_OBJECT (filter,
        "Framesize media attribute unexpectedly has trailing characters");
    return FALSE;
  }

  d = gst_caps_get_structure (srccaps, 0);
  gst_structure_set (d, "width", G_TYPE_INT, width, "height", G_TYPE_INT,
      height, NULL);

  return TRUE;
}

179
gboolean
Wim Taymans's avatar
Wim Taymans committed
180
gst_rtp_h263_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
181 182 183
{
  GstCaps *srccaps;
  GstStructure *structure = gst_caps_get_structure (caps, 0);
184
  gint clock_rate;
185 186 187 188 189
  const gchar *framesize;

  srccaps = gst_caps_new_simple ("video/x-h263",
      "variant", G_TYPE_STRING, "itu",
      "h263version", G_TYPE_STRING, "h263", NULL);
190

191 192
  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
    clock_rate = 90000;         /* default */
193 194
  filter->clock_rate = clock_rate;

195 196 197 198 199 200 201
  framesize = gst_structure_get_string (structure, "a-framesize");
  if (framesize != NULL) {
    if (!gst_rtp_h263_parse_framesize (filter, framesize, srccaps)) {
      return FALSE;
    }
  }

Wim Taymans's avatar
Wim Taymans committed
202
  gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
203 204 205 206 207 208
  gst_caps_unref (srccaps);

  return TRUE;
}

static GstBuffer *
209
gst_rtp_h263_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
210 211 212
{
  GstRtpH263Depay *rtph263depay;
  GstBuffer *outbuf;
213 214 215 216 217 218
  gint payload_len;
  guint8 *payload;
  guint header_len;
  guint SBIT, EBIT;
  gboolean F, P, M;
  gboolean I;
219 220 221

  rtph263depay = GST_RTP_H263_DEPAY (depayload);

222
  /* flush remaining data on discont */
223
  if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
224
    GST_LOG_OBJECT (depayload, "Discont buffer, flushing adapter");
225 226 227
    gst_adapter_clear (rtph263depay->adapter);
    rtph263depay->offset = 0;
    rtph263depay->leftover = 0;
228
    rtph263depay->start = FALSE;
229 230
  }

231 232
  payload_len = gst_rtp_buffer_get_payload_len (rtp);
  payload = gst_rtp_buffer_get_payload (rtp);
233

234
  M = gst_rtp_buffer_get_marker (rtp);
235

236 237 238
  if (payload_len < 1)
    goto too_small;

239 240 241
  /* Let's see what mode we are using */
  F = (payload[0] & 0x80) == 0x80;
  P = (payload[0] & 0x40) == 0x40;
242

243 244 245
  /* Bit shifting */
  SBIT = (payload[0] & 0x38) >> 3;
  EBIT = (payload[0] & 0x07);
246

247 248 249 250 251 252
  /* Figure out header length and I-flag */
  if (F == 0) {
    /* F == 0 and P == 0 or 1
     * mode A */
    header_len = GST_RFC2190A_HEADER_LEN;
    GST_LOG ("Mode A");
253

254 255 256 257 258 259
    /* 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
     * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     * |F|P|SBIT |EBIT | SRC |I|U|S|A|R      |DBQ| TRB |    TR         |
     * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     */
260 261
    if (payload_len <= header_len)
      goto too_small;
262 263 264
    I = (payload[1] & 0x10) == 0x10;
  } else {
    if (P == 0) {
265 266 267
      /* F == 1 and P == 0
       * mode B */
      header_len = GST_RFC2190B_HEADER_LEN;
268
      GST_LOG ("Mode B");
269 270 271 272 273 274 275 276 277

      /* 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
       * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
       * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
       * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       */
278 279
      if (payload_len <= header_len)
        goto too_small;
280
      I = (payload[4] & 0x80) == 0x80;
281 282 283 284
    } else {
      /* F == 1 and P == 1
       * mode C */
      header_len = GST_RFC2190C_HEADER_LEN;
285
      GST_LOG ("Mode C");
286 287 288 289 290 291 292 293 294 295 296

      /* 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
       * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       * |F|P|SBIT |EBIT | SRC | QUANT   |  GOBN   |   MBA           |R  |
       * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       * |I|U|S|A| HMV1        | VMV1        | HMV2        | VMV2        |
       * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       * | RR                                  |DBQ| TRB |    TR         |
       * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       */
297 298
      if (payload_len <= header_len)
        goto too_small;
299
      I = (payload[4] & 0x80) == 0x80;
300
    }
301
  }
302

303 304 305 306
  GST_LOG ("F/P/M/I : %d/%d/%d/%d", F, P, M, I);
  GST_LOG ("SBIT : %d , EBIT : %d", SBIT, EBIT);
  GST_LOG ("payload_len : %d, header_len : %d , leftover : 0x%x",
      payload_len, header_len, rtph263depay->leftover);
307

308 309 310 311
  /* skip header */
  payload += header_len;
  payload_len -= header_len;

312
  if (!rtph263depay->start) {
313 314 315
    /* only mode A should be used when there is a picture start code, but
     * buggy payloaders may send mode B/C in start of frame */
    if (payload_len > 4 && (GST_READ_UINT32_BE (payload) >> 10 == 0x20)) {
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
316
      GST_DEBUG ("Mode %c with PSC => frame start", "ABC"[F + P]);
317
      rtph263depay->start = TRUE;
318
      if ((! !(payload[4] & 0x02)) != I) {
319 320 321 322
        GST_DEBUG ("Wrong Picture Coding Type Flag in rtp header");
        I = !I;
      }
      rtph263depay->psc_I = I;
323 324 325 326 327 328
    } else {
      GST_DEBUG ("no frame start yet, skipping payload");
      goto skip;
    }
  }

329 330 331 332
  /* only trust I info from Mode A starting packet
   * from buggy payloaders or hw */
  I = rtph263depay->psc_I;

333
  if (SBIT) {
Wim Taymans's avatar
Wim Taymans committed
334 335
    /* take the leftover and merge it at the beginning, FIXME make the buffer
     * data writable. */
336 337 338 339 340 341 342 343
    GST_LOG ("payload[0] : 0x%x", payload[0]);
    payload[0] &= 0xFF >> SBIT;
    GST_LOG ("payload[0] : 0x%x", payload[0]);
    payload[0] |= rtph263depay->leftover;
    GST_LOG ("payload[0] : 0x%x", payload[0]);
    rtph263depay->leftover = 0;
    rtph263depay->offset = 0;
  }
344

345
  if (!EBIT) {
346
    GstBuffer *tmp;
347

348
    /* Take the entire buffer */
349
    tmp = gst_rtp_buffer_get_payload_subbuffer (rtp, header_len, payload_len);
350
    gst_adapter_push (rtph263depay->adapter, tmp);
351
  } else {
352
    GstBuffer *tmp;
353

354
    /* Take the entire buffer except for the last byte */
355
    tmp = gst_rtp_buffer_get_payload_subbuffer (rtp, header_len,
356
        payload_len - 1);
357 358 359 360 361 362 363 364
    gst_adapter_push (rtph263depay->adapter, tmp);

    /* Put the last byte into the leftover */
    GST_DEBUG ("payload[payload_len - 1] : 0x%x", payload[payload_len - 1]);
    GST_DEBUG ("mask : 0x%x", 0xFF << EBIT);
    rtph263depay->leftover = (payload[payload_len - 1] >> EBIT) << EBIT;
    rtph263depay->offset = 1;
    GST_DEBUG ("leftover : 0x%x", rtph263depay->leftover);
365
  }
366

367
skip:
368
  if (M) {
369
    if (rtph263depay->start) {
370 371 372 373 374 375 376 377
      /* frame is completed */
      guint avail;

      if (rtph263depay->offset) {
        /* push in the leftover */
        GstBuffer *buf = gst_buffer_new_and_alloc (1);

        GST_DEBUG ("Pushing leftover in adapter");
Wim Taymans's avatar
Wim Taymans committed
378
        gst_buffer_fill (buf, 0, &rtph263depay->leftover, 1);
379 380 381 382 383 384 385 386 387 388 389
        gst_adapter_push (rtph263depay->adapter, buf);
      }

      avail = gst_adapter_available (rtph263depay->adapter);
      outbuf = gst_adapter_take_buffer (rtph263depay->adapter, avail);

      if (I)
        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);

      GST_DEBUG ("Pushing out a buffer of %d bytes", avail);

390
      gst_rtp_drop_non_video_meta (rtph263depay, outbuf);
391

Wim Taymans's avatar
Wim Taymans committed
392
      gst_rtp_base_depayload_push (depayload, outbuf);
393 394
      rtph263depay->offset = 0;
      rtph263depay->leftover = 0;
395
      rtph263depay->start = FALSE;
396
    } else {
397
      rtph263depay->start = TRUE;
398
    }
399
  }
400

401
  return NULL;
402 403 404 405 406 407 408

too_small:
  {
    GST_ELEMENT_WARNING (rtph263depay, STREAM, DECODE,
        ("Packet payload was too small"), (NULL));
    return NULL;
  }
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
}

static GstStateChangeReturn
gst_rtp_h263_depay_change_state (GstElement * element,
    GstStateChange transition)
{
  GstRtpH263Depay *rtph263depay;
  GstStateChangeReturn ret;

  rtph263depay = GST_RTP_H263_DEPAY (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      gst_adapter_clear (rtph263depay->adapter);
425
      rtph263depay->start = TRUE;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
      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_h263_depay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtph263depay",
446
      GST_RANK_SECONDARY, GST_TYPE_RTP_H263_DEPAY);
447
}