gstrtpvp8depay.c 8.62 KB
Newer Older
1
/* gstrtpvp8depay.c - Source for GstRtpVP8Depay
2
 * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
3 4
 * Copyright (C) 2011 Collabora Ltd.
 *   Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

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

#include "gstrtpvp8depay.h"
26
#include "gstrtputils.h"
27

28 29
#include <gst/video/video.h>

30 31
#include <stdio.h>

32 33 34
GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
#define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug

35 36
static void gst_rtp_vp8_depay_dispose (GObject * object);
static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
37
    GstRTPBuffer * rtp);
38 39 40 41
static GstStateChangeReturn gst_rtp_vp8_depay_change_state (GstElement *
    element, GstStateChange transition);
static gboolean gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay,
    GstEvent * event);
42

Olivier Crête's avatar
Olivier Crête committed
43
G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
44 45 46 47 48 49 50 51 52 53 54 55 56 57

static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-vp8"));

static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "clock-rate = (int) 90000,"
        "media = (string) \"video\","
58
        "encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
59 60

static void
Olivier Crête's avatar
Olivier Crête committed
61
gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
62 63 64 65 66 67
{
  self->adapter = gst_adapter_new ();
  self->started = FALSE;
}

static void
Olivier Crête's avatar
Olivier Crête committed
68
gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
69
{
Olivier Crête's avatar
Olivier Crête committed
70 71 72 73 74
  GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
  GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
  GstRTPBaseDepayloadClass *depay_class =
      (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);

75

76 77 78 79
  gst_element_class_add_static_pad_template (element_class,
      &gst_rtp_vp8_depay_sink_template);
  gst_element_class_add_static_pad_template (element_class,
      &gst_rtp_vp8_depay_src_template);
80

81
  gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
82 83 84 85 86 87
      "Codec/Depayloader/Network/RTP",
      "Extracts VP8 video from RTP packets)",
      "Sjoerd Simons <sjoerd@luon.net>");

  object_class->dispose = gst_rtp_vp8_depay_dispose;

88 89
  element_class->change_state = gst_rtp_vp8_depay_change_state;

90
  depay_class->process_rtp_packet = gst_rtp_vp8_depay_process;
91
  depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
      "VP8 Video RTP Depayloader");
}

static void
gst_rtp_vp8_depay_dispose (GObject * object)
{
  GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);

  if (self->adapter != NULL)
    g_object_unref (self->adapter);
  self->adapter = NULL;

  /* release any references held by the object here */

Olivier Crête's avatar
Olivier Crête committed
108 109
  if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
    G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
110 111 112
}

static GstBuffer *
113
gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
114 115 116 117
{
  GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
  GstBuffer *payload;
  guint8 *data;
118
  guint hdrsize;
Olivier Crête's avatar
Olivier Crête committed
119
  guint size;
120

121
  if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
122 123 124 125 126
    GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
    gst_adapter_clear (self->adapter);
    self->started = FALSE;
  }

127
  size = gst_rtp_buffer_get_payload_len (rtp);
Olivier Crête's avatar
Olivier Crête committed
128

129
  /* At least one header and one vp8 byte */
130 131 132
  if (G_UNLIKELY (size < 2))
    goto too_small;

133
  data = gst_rtp_buffer_get_payload (rtp);
134 135 136

  if (G_UNLIKELY (!self->started)) {
    /* Check if this is the start of a VP8 frame, otherwise bail */
137
    /* S=1 and PartID= 0 */
138
    if ((data[0] & 0x17) != 0x10)
Olivier Crête's avatar
Olivier Crête committed
139
      goto done;
140 141 142 143

    self->started = TRUE;
  }

144
  hdrsize = 1;
145 146
  /* Check X optional header */
  if ((data[0] & 0x80) != 0) {
147
    hdrsize++;
148 149
    /* Check I optional header */
    if ((data[1] & 0x80) != 0) {
150
      if (G_UNLIKELY (size < 3))
151
        goto too_small;
152
      hdrsize++;
153 154
      /* Check for 16 bits PictureID */
      if ((data[2] & 0x80) != 0)
155
        hdrsize++;
156
    }
157 158
    /* Check L optional header */
    if ((data[1] & 0x40) != 0)
159
      hdrsize++;
160 161
    /* Check T or K optional headers */
    if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
162
      hdrsize++;
163
  }
164
  GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
165

166
  if (G_UNLIKELY (hdrsize >= size))
167 168
    goto too_small;

169
  payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
170 171 172
  gst_adapter_push (self->adapter, payload);

  /* Marker indicates that it was the last rtp packet for this frame */
173
  if (gst_rtp_buffer_get_marker (rtp)) {
174
    GstBuffer *out;
175
    guint8 header[10];
176

177 178
    if (gst_adapter_available (self->adapter) < 10)
      goto too_small;
179
    gst_adapter_copy (self->adapter, &header, 0, 10);
180 181 182 183 184

    out = gst_adapter_take_buffer (self->adapter,
        gst_adapter_available (self->adapter));

    self->started = FALSE;
185 186 187

    /* mark keyframes */
    out = gst_buffer_make_writable (out);
188 189 190
    /* Filter away all metas that are not sensible to copy */
    gst_rtp_drop_meta (GST_ELEMENT_CAST (self), out,
        g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
191
    if ((header[0] & 0x01)) {
192
      GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
193 194 195 196

      if (!self->caps_sent) {
        gst_buffer_unref (out);
        out = NULL;
197 198 199 200
        GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
        gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
            gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
                TRUE, 0));
201 202
      }
    } else {
203
      guint profile, width, height;
204

205 206
      GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      profile = (header[0] & 0x0e) >> 1;
      width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
      height = GST_READ_UINT16_LE (header + 8) & 0x3fff;

      if (G_UNLIKELY (self->last_width != width ||
              self->last_height != height || self->last_profile != profile)) {
        gchar profile_str[3];
        GstCaps *srccaps;

        snprintf (profile_str, 3, "%u", profile);
        srccaps = gst_caps_new_simple ("video/x-vp8",
            "framerate", GST_TYPE_FRACTION, 0, 1,
            "height", G_TYPE_INT, height,
            "width", G_TYPE_INT, width,
            "profile", G_TYPE_STRING, profile_str, NULL);

        gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
        gst_caps_unref (srccaps);

        self->caps_sent = TRUE;
        self->last_width = width;
        self->last_height = height;
        self->last_profile = profile;
230 231 232
      }
    }

233 234 235
    return out;
  }

Olivier Crête's avatar
Olivier Crête committed
236
done:
237 238 239 240 241 242
  return NULL;

too_small:
  GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
  gst_adapter_clear (self->adapter);
  self->started = FALSE;
Olivier Crête's avatar
Olivier Crête committed
243 244

  goto done;
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
static GstStateChangeReturn
gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
{
  GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      self->last_profile = -1;
      self->last_height = -1;
      self->last_width = -1;
      self->caps_sent = FALSE;
      break;
    default:
      break;
  }

  return
      GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
      transition);
}

268
static gboolean
269
gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
270
{
271
  GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
272

273 274 275 276 277 278 279 280 281
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_FLUSH_STOP:
      self->last_profile = -1;
      self->last_height = -1;
      self->last_width = -1;
      break;
    default:
      break;
  }
282

283 284 285
  return
      GST_RTP_BASE_DEPAYLOAD_CLASS
      (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
286 287 288 289 290 291 292 293
}

gboolean
gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpvp8depay",
      GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);
}