rtpvp9pay: rtpvp9depay: Initial implementation of draft 01

Quick and dirty implementation of an RTP payloader and depayloader
for VP9. In particalur it assumes no spatial or temporal layering,
non-flexible mode, and some other bits and pieces.

https://bugzilla.gnome.org/show_bug.cgi?id=754773
This commit is contained in:
Stian Selnes 2015-12-23 19:54:13 +01:00 committed by Sebastian Dröge
parent dc70bfd36a
commit 5faa9c11a6
6 changed files with 1074 additions and 0 deletions

View file

@ -83,6 +83,8 @@ libgstrtp_la_SOURCES = \
gstrtpvorbispay.c \
gstrtpvp8depay.c \
gstrtpvp8pay.c \
gstrtpvp9depay.c \
gstrtpvp9pay.c \
gstrtpvrawdepay.c \
gstrtpvrawpay.c \
gstrtpstreampay.c \
@ -188,6 +190,8 @@ noinst_HEADERS = \
gstrtpvorbispay.h \
gstrtpvp8depay.h \
gstrtpvp8pay.h \
gstrtpvp9depay.h \
gstrtpvp9pay.h \
gstrtpvrawdepay.h \
gstrtpvrawpay.h \
gstrtpstreampay.h \

View file

@ -101,6 +101,8 @@
#include "gstrtpvorbispay.h"
#include "gstrtpvp8depay.h"
#include "gstrtpvp8pay.h"
#include "gstrtpvp9depay.h"
#include "gstrtpvp9pay.h"
#include "gstrtpvrawdepay.h"
#include "gstrtpvrawpay.h"
#include "gstrtpstreampay.h"
@ -345,6 +347,12 @@ plugin_init (GstPlugin * plugin)
if (!gst_rtp_vp8_pay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_vp9_depay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_vp9_pay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_vraw_depay_plugin_init (plugin))
return FALSE;

364
gst/rtp/gstrtpvp9depay.c Normal file
View file

@ -0,0 +1,364 @@
/* gstrtpvp9depay.c - Source for GstRtpVP9Depay
* Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
* Copyright (C) 2011 Collabora Ltd.
* Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
* Copyright (C) 2015 Stian Selnes <stian@pexip.com>
*
* 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 "gstrtpvp9depay.h"
#include "gstrtputils.h"
#include <gst/video/video.h>
#include <stdio.h>
GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_depay_debug);
#define GST_CAT_DEFAULT gst_rtp_vp9_depay_debug
static void gst_rtp_vp9_depay_dispose (GObject * object);
static GstBuffer *gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depayload,
GstRTPBuffer * rtp);
static GstStateChangeReturn gst_rtp_vp9_depay_change_state (GstElement *
element, GstStateChange transition);
static gboolean gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay,
GstEvent * event);
G_DEFINE_TYPE (GstRtpVP9Depay, gst_rtp_vp9_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
static GstStaticPadTemplate gst_rtp_vp9_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-vp9"));
static GstStaticPadTemplate gst_rtp_vp9_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\","
"encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
static void
gst_rtp_vp9_depay_init (GstRtpVP9Depay * self)
{
self->adapter = gst_adapter_new ();
self->started = FALSE;
}
static void
gst_rtp_vp9_depay_class_init (GstRtpVP9DepayClass * gst_rtp_vp9_depay_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp9_depay_class);
GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_depay_class);
GstRTPBaseDepayloadClass *depay_class =
(GstRTPBaseDepayloadClass *) (gst_rtp_vp9_depay_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_vp9_depay_sink_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_vp9_depay_src_template));
gst_element_class_set_static_metadata (element_class, "RTP VP9 depayloader",
"Codec/Depayloader/Network/RTP",
"Extracts VP9 video from RTP packets)",
"Stian Selnes <stian@pexip.com>");
object_class->dispose = gst_rtp_vp9_depay_dispose;
element_class->change_state = gst_rtp_vp9_depay_change_state;
depay_class->process_rtp_packet = gst_rtp_vp9_depay_process;
depay_class->handle_event = gst_rtp_vp9_depay_handle_event;
GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_depay_debug, "rtpvp9depay", 0,
"VP9 Video RTP Depayloader");
}
static void
gst_rtp_vp9_depay_dispose (GObject * object)
{
GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (object);
if (self->adapter != NULL)
g_object_unref (self->adapter);
self->adapter = NULL;
/* release any references held by the object here */
if (G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose)
G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose (object);
}
static GstBuffer *
gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
{
GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
GstBuffer *payload;
guint8 *data;
guint hdrsize = 1;
guint size;
gint spatial_layer = 0;
gboolean i_bit, p_bit, l_bit, f_bit, b_bit, e_bit, v_bit;
if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
gst_adapter_clear (self->adapter);
self->started = FALSE;
}
size = gst_rtp_buffer_get_payload_len (rtp);
/* Mandatory with at least one header and one vp9 byte */
if (G_UNLIKELY (size < hdrsize + 1))
goto too_small;
data = gst_rtp_buffer_get_payload (rtp);
i_bit = (data[0] & 0x80) != 0;
p_bit = (data[0] & 0x40) != 0;
l_bit = (data[0] & 0x20) != 0;
f_bit = (data[0] & 0x10) != 0;
b_bit = (data[0] & 0x08) != 0;
e_bit = (data[0] & 0x04) != 0;
v_bit = (data[0] & 0x02) != 0;
if (G_UNLIKELY (!self->started)) {
/* Check if this is the start of a VP9 layer frame, otherwise bail */
if (!b_bit)
goto done;
self->started = TRUE;
}
GST_TRACE_OBJECT (self, "IPLFBEV : %d%d%d%d%d%d%d", i_bit, p_bit, l_bit,
f_bit, b_bit, e_bit, v_bit);
/* Check I optional header Picture ID */
if (i_bit) {
hdrsize++;
if (G_UNLIKELY (size < hdrsize + 1))
goto too_small;
/* Check M for 15 bits PictureID */
if ((data[1] & 0x80) != 0) {
hdrsize++;
if (G_UNLIKELY (size < hdrsize + 1))
goto too_small;
}
}
/* flexible-mode not implemented */
g_assert (!f_bit);
/* Check L optional header layer indices */
if (l_bit) {
hdrsize++;
/* Check TL0PICIDX temporal layer zero index (non-flexible mode) */
if (!f_bit)
hdrsize++;
}
/* Check V optional Scalability Structure */
if (v_bit) {
guint n_s, y_bit, g_bit;
guint8 *ss = &data[hdrsize];
guint sssize = 1;
if (G_UNLIKELY (size < hdrsize + sssize + 1))
goto too_small;
n_s = (ss[0] & 0xe0) >> 5;
y_bit = (ss[0] & 0x10) != 0;
g_bit = (ss[0] & 0x08) != 0;
GST_TRACE_OBJECT (self, "SS header: N_S=%u, Y=%u, G=%u", n_s, y_bit, g_bit);
sssize += y_bit ? (n_s + 1) * 4 : 0;
if (G_UNLIKELY (size < hdrsize + sssize + 1))
goto too_small;
if (y_bit) {
guint i;
for (i = 0; i <= n_s; i++) {
/* For now, simply use the last layer specified for width and height */
self->ss_width = ss[1 + i * 4] * 256 + ss[2 + i * 4];
self->ss_height = ss[3 + i * 4] * 256 + ss[4 + i * 4];
GST_TRACE_OBJECT (self, "N_S[%d]: WIDTH=%u, HEIGHT=%u", i,
self->ss_width, self->ss_height);
}
}
if (g_bit) {
guint i, j;
guint n_g = ss[sssize];
sssize++;
if (G_UNLIKELY (size < hdrsize + sssize + 1))
goto too_small;
for (i = 0; i < n_g; i++) {
guint t = (ss[sssize] & 0xe0) >> 5;
guint u = (ss[sssize] & 0x10) >> 4;
guint r = (ss[sssize] & 0x0c) >> 2;
GST_TRACE_OBJECT (self, "N_G[%u]: 0x%02x -> T=%u, U=%u, R=%u", i,
ss[sssize], t, u, r);
for (j = 0; j < r; j++)
GST_TRACE_OBJECT (self, " R[%u]: P_DIFF=%u", j, ss[sssize + 1 + j]);
sssize += 1 + r;
if (G_UNLIKELY (size < hdrsize + sssize + 1))
goto too_small;
}
}
hdrsize += sssize;
}
GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
if (G_UNLIKELY (hdrsize >= size))
goto too_small;
payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
{
GstMapInfo map;
gst_buffer_map (payload, &map, GST_MAP_READ);
GST_MEMDUMP_OBJECT (self, "vp9 payload", map.data, 16);
gst_buffer_unmap (payload, &map);
}
gst_adapter_push (self->adapter, payload);
/* Marker indicates that it was the last rtp packet for this frame */
if (gst_rtp_buffer_get_marker (rtp)) {
GstBuffer *out;
gboolean key_frame_first_layer = !p_bit && spatial_layer == 0;
if (gst_adapter_available (self->adapter) < 10)
goto too_small;
out = gst_adapter_take_buffer (self->adapter,
gst_adapter_available (self->adapter));
self->started = FALSE;
/* mark keyframes */
out = gst_buffer_make_writable (out);
/* 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));
if (!key_frame_first_layer) {
GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
if (!self->caps_sent) {
gst_buffer_unref (out);
out = NULL;
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));
}
} else {
GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
if (self->last_width != self->ss_width ||
self->last_height != self->ss_height) {
GstCaps *srccaps;
/* Width and height are optional in the RTP header. Consider to parse
* the frame header in addition if missing from RTP header */
if (self->ss_width != 0 && self->ss_height != 0) {
srccaps = gst_caps_new_simple ("video/x-vp9",
"framerate", GST_TYPE_FRACTION, 0, 1,
"width", G_TYPE_INT, self->ss_width,
"height", G_TYPE_INT, self->ss_height, NULL);
} else {
srccaps = gst_caps_new_simple ("video/x-vp9",
"framerate", GST_TYPE_FRACTION, 0, 1, NULL);
}
gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
gst_caps_unref (srccaps);
self->caps_sent = TRUE;
self->last_width = self->ss_width;
self->last_height = self->ss_height;
self->ss_width = 0;
self->ss_height = 0;
}
}
return out;
}
done:
return NULL;
too_small:
GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
gst_adapter_clear (self->adapter);
self->started = FALSE;
goto done;
}
static GstStateChangeReturn
gst_rtp_vp9_depay_change_state (GstElement * element, GstStateChange transition)
{
GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (element);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
self->last_width = -1;
self->last_height = -1;
self->caps_sent = FALSE;
break;
default:
break;
}
return
GST_ELEMENT_CLASS (gst_rtp_vp9_depay_parent_class)->change_state (element,
transition);
}
static gboolean
gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
{
GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_FLUSH_STOP:
self->last_width = -1;
self->last_height = -1;
break;
default:
break;
}
return
GST_RTP_BASE_DEPAYLOAD_CLASS
(gst_rtp_vp9_depay_parent_class)->handle_event (depay, event);
}
gboolean
gst_rtp_vp9_depay_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "rtpvp9depay",
GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_DEPAY);
}

71
gst/rtp/gstrtpvp9depay.h Normal file
View file

@ -0,0 +1,71 @@
/*
* gstrtpvp9depay.h - Header for GstRtpVP9Depay
* Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
* Copyright (C) 2015 Stian Selnes <stian@pexip.com>
*
* 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
*/
#ifndef __GST_RTP_VP9_DEPAY_H__
#define __GST_RTP_VP9_DEPAY_H__
#include <gst/base/gstadapter.h>
#include <gst/rtp/gstrtpbasedepayload.h>
G_BEGIN_DECLS
#define GST_TYPE_RTP_VP9_DEPAY \
(gst_rtp_vp9_depay_get_type())
#define GST_RTP_VP9_DEPAY(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_RTP_VP9_DEPAY, GstRtpVP9Depay))
#define GST_RTP_VP9_DEPAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_RTP_VP9_DEPAY, \
GstRtpVP9DepayClass))
#define GST_IS_RTP_VP9_DEPAY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_RTP_VP9_DEPAY))
#define GST_IS_RTP_VP9_DEPAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_RTP_VP9_DEPAY))
#define GST_RTP_VP9_DEPAY_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_RTP_VP9_DEPAY, \
GstRtpVP9DepayClass))
typedef struct _GstRtpVP9Depay GstRtpVP9Depay;
typedef struct _GstRtpVP9DepayClass GstRtpVP9DepayClass;
struct _GstRtpVP9DepayClass
{
GstRTPBaseDepayloadClass parent_class;
};
struct _GstRtpVP9Depay
{
GstRTPBaseDepayload parent;
GstAdapter *adapter;
gboolean started;
gint ss_width;
gint ss_height;
gint last_width;
gint last_height;
gboolean caps_sent;
};
GType gst_rtp_vp9_depay_get_type (void);
gboolean gst_rtp_vp9_depay_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* #ifndef __GST_RTP_VP9_DEPAY_H__ */

555
gst/rtp/gstrtpvp9pay.c Normal file
View file

@ -0,0 +1,555 @@
/*
* gstrtpvp9pay.c - Source for GstRtpVP9Pay
* Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
* Copyright (C) 2011 Collabora Ltd.
* Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
* Copyright (C) 2015 Stian Selnes <stian@pexip.com>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gst/base/gstbitreader.h>
#include <gst/rtp/gstrtppayloads.h>
#include <gst/rtp/gstrtpbuffer.h>
#include <gst/video/video.h>
#include "dboolhuff.h"
#include "gstrtpvp9pay.h"
#include "gstrtputils.h"
GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_pay_debug);
#define GST_CAT_DEFAULT gst_rtp_vp9_pay_debug
#define DEFAULT_PICTURE_ID_MODE VP9_PAY_NO_PICTURE_ID
enum
{
PROP_0,
PROP_PICTURE_ID_MODE
};
#define GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE (gst_rtp_vp9_pay_picture_id_mode_get_type())
static GType
gst_rtp_vp9_pay_picture_id_mode_get_type (void)
{
static GType mode_type = 0;
static const GEnumValue modes[] = {
{VP9_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
{VP9_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
{VP9_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
{0, NULL, NULL},
};
if (!mode_type) {
mode_type = g_enum_register_static ("GstVP9RTPPayMode", modes);
}
return mode_type;
}
static void gst_rtp_vp9_pay_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_rtp_vp9_pay_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static GstFlowReturn gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload,
GstBuffer * buffer);
static gboolean gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload,
GstEvent * event);
static gboolean gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload,
GstCaps * caps);
G_DEFINE_TYPE (GstRtpVP9Pay, gst_rtp_vp9_pay, GST_TYPE_RTP_BASE_PAYLOAD);
static GstStaticPadTemplate gst_rtp_vp9_pay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-rtp, "
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
"clock-rate = (int) 90000, encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
static GstStaticPadTemplate gst_rtp_vp9_pay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-vp9"));
static void
gst_rtp_vp9_pay_init (GstRtpVP9Pay * obj)
{
obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
obj->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
else if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
obj->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
}
static void
gst_rtp_vp9_pay_class_init (GstRtpVP9PayClass * gst_rtp_vp9_pay_class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp9_pay_class);
GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_pay_class);
GstRTPBasePayloadClass *pay_class =
GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_class);
gobject_class->set_property = gst_rtp_vp9_pay_set_property;
gobject_class->get_property = gst_rtp_vp9_pay_get_property;
g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
"The picture ID mode for payloading",
GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_vp9_pay_sink_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_vp9_pay_src_template));
gst_element_class_set_static_metadata (element_class, "RTP VP9 payloader",
"Codec/Payloader/Network/RTP",
"Puts VP9 video in RTP packets)", "Stian Selnes <stian@pexip.com>");
pay_class->handle_buffer = gst_rtp_vp9_pay_handle_buffer;
pay_class->sink_event = gst_rtp_vp9_pay_sink_event;
pay_class->set_caps = gst_rtp_vp9_pay_set_caps;
GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_pay_debug, "rtpvp9pay", 0,
"VP9 Video RTP Payloader");
}
static void
gst_rtp_vp9_pay_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec)
{
GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
switch (prop_id) {
case PROP_PICTURE_ID_MODE:
rtpvp9pay->picture_id_mode = g_value_get_enum (value);
if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
else if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_rtp_vp9_pay_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
{
GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
switch (prop_id) {
case PROP_PICTURE_ID_MODE:
g_value_set_enum (value, rtpvp9pay->picture_id_mode);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
#define VP9_PROFILE_0 0
#define VP9_PROFILE_1 1
#define VP9_PROFILE_2 2
#define VP9_PROFILE_3 3
#define VP9_FRAME_MARKER 0x2
#define VPX_CS_SRGB 7
static gboolean
gst_rtp_vp9_pay_parse_frame (GstRtpVP9Pay * self, GstBuffer * buffer,
gsize buffer_size)
{
GstMapInfo map = GST_MAP_INFO_INIT;
GstBitReader reader;
guint8 *data;
gsize size;
gboolean keyframe;
guint32 tmp, profile;
if (G_UNLIKELY (buffer_size < 3))
goto error;
if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
goto error;
data = map.data;
size = map.size;
gst_bit_reader_init (&reader, data, size);
/* frame marker */
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 2)
|| tmp != VP9_FRAME_MARKER)
goto error;
/* profile, variable length */
if (!gst_bit_reader_get_bits_uint32 (&reader, &profile, 2))
goto error;
if (profile > 2) {
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
goto error;
profile += tmp;
}
/* show existing frame */
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
goto error;
if (tmp) {
if (!gst_bit_reader_skip (&reader, 3))
goto error;
return TRUE;
}
/* frame type */
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
goto error;
self->is_keyframe = keyframe = (tmp == 0);
/* show frame and resilient mode */
if (!gst_bit_reader_skip (&reader, 2))
goto error;
if (keyframe) {
/* sync code */
const guint32 sync_code = 0x498342;
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 24))
goto error;
if (tmp != sync_code)
goto error;
if (profile >= VP9_PROFILE_2) {
/* bit depth */
if (!gst_bit_reader_skip (&reader, 1))
goto error;
}
/* color space */
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 3))
goto error;
if (tmp != VPX_CS_SRGB) {
/* color range */
if (!gst_bit_reader_skip (&reader, 1))
goto error;
if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3) {
/* subsampling + reserved bit */
if (!gst_bit_reader_skip (&reader, 2 + 1))
goto error;
}
} else {
if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3)
/* reserved bit */
if (!gst_bit_reader_skip (&reader, 1))
goto error;
}
/* frame size */
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
goto error;
self->width = tmp + 1;
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
goto error;
self->height = tmp + 1;
/* render size */
if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
goto error;
if (tmp) {
if (!gst_bit_reader_skip (&reader, 32))
goto error;
}
GST_INFO_OBJECT (self, "parsed width=%d height=%d", self->width,
self->height);
}
gst_buffer_unmap (buffer, &map);
return TRUE;
error:
GST_DEBUG ("Failed to parse frame");
if (map.memory != NULL) {
gst_buffer_unmap (buffer, &map);
}
return FALSE;
}
static gsize
gst_rtp_vp9_calc_header_len (GstRtpVP9Pay * self, gboolean start)
{
gsize len = 1;
switch (self->picture_id_mode) {
case VP9_PAY_PICTURE_ID_7BITS:
len += 1;
case VP9_PAY_PICTURE_ID_15BITS:
len += 2;
default:
break;
}
/* Assume non-flexible mode */
/* Assume L-bit not set, no L header */
if (self->is_keyframe && start) {
/* Assume V-bit set */
/* FIXME: SS depends on layers and prediction structure */
/* For now assume 1 spatial and 1 temporal layer. */
/* FIXME: Only for the first packet in the key frame */
len += 8;
}
return len;
}
/* VP9 RTP header, non-flexible mode:
0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+
|I|P|L|F|B|E|V|-| (REQUIRED)
+-+-+-+-+-+-+-+-+
I: |M| PICTURE ID | (RECOMMENDED)
+-+-+-+-+-+-+-+-+
M: | EXTENDED PID | (RECOMMENDED)
+-+-+-+-+-+-+-+-+
L: | T |U| S |D| (CONDITIONALLY RECOMMENDED)
+-+-+-+-+-+-+-+-+ -\
P,F: | P_DIFF |X|N| (CONDITIONALLY RECOMMENDED) .
+-+-+-+-+-+-+-+-+ . - up to 3 times
X: |EXTENDED P_DIFF| (OPTIONAL) .
+-+-+-+-+-+-+-+-+ -/
V: | SS |
| .. |
+-+-+-+-+-+-+-+-+
Scalability structure (SS)
(from https://chromium.googlesource.com/external/webrtc/+/HEAD/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc
since latest draft is not up to date with chromium)
+-+-+-+-+-+-+-+-+
V: | N_S |Y|G|-|-|-|
+-+-+-+-+-+-+-+-+ -|
Y: | WIDTH | (OPTIONAL) .
+ + .
| | (OPTIONAL) .
+-+-+-+-+-+-+-+-+ . N_S + 1 times
| HEIGHT | (OPTIONAL) .
+ + .
| | (OPTIONAL) .
+-+-+-+-+-+-+-+-+ -|
G: | N_G | (OPTIONAL)
+-+-+-+-+-+-+-+-+ -|
N_G: | T |U| R |-|-| (OPTIONAL) .
+-+-+-+-+-+-+-+-+ -| . N_G times
| P_DIFF | (OPTIONAL) . R times .
+-+-+-+-+-+-+-+-+ -| -|
**/
/* When growing the vp9 header keep max payload len calculation in sync */
static GstBuffer *
gst_rtp_vp9_create_header_buffer (GstRtpVP9Pay * self,
gboolean start, gboolean mark, GstBuffer * in)
{
GstBuffer *out;
guint8 *p;
GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
guint off = 1;
guint hdrlen = gst_rtp_vp9_calc_header_len (self, start);
out = gst_rtp_buffer_new_allocate (hdrlen, 0, 0);
gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
p = gst_rtp_buffer_get_payload (&rtpbuffer);
p[0] = 0x0;
if (self->picture_id_mode != VP9_PAY_NO_PICTURE_ID) {
p[0] |= 0x80;
if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS) {
/* M=0 */
p[off++] = self->picture_id & 0x7F;
} else {
/* M=1 */
p[off++] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
p[off++] = self->picture_id & 0xFF;
}
}
if (!self->is_keyframe)
p[0] |= 0x40;
if (start)
p[0] |= 0x08;
if (mark)
p[0] |= 0x04;
if (self->is_keyframe && start) {
p[0] |= 0x02;
/* scalability structure, hard coded for now to be similar to chromium for
* quick and dirty interop */
p[off++] = 0x18; /* N_S=0 Y=1 G=1 */
p[off++] = self->width >> 8;
p[off++] = self->width & 0xFF;
p[off++] = self->height >> 8;
p[off++] = self->height & 0xFF;
p[off++] = 0x01; /* N_G=1 */
p[off++] = 0x04; /* T=0, U=0, R=1 */
p[off++] = 0x01; /* P_DIFF=1 */
}
g_assert_cmpint (off, ==, hdrlen);
gst_rtp_buffer_set_marker (&rtpbuffer, mark);
gst_rtp_buffer_unmap (&rtpbuffer);
GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
return out;
}
static guint
gst_rtp_vp9_payload_next (GstRtpVP9Pay * self, GstBufferList * list,
guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len)
{
GstBuffer *header;
GstBuffer *sub;
GstBuffer *out;
gboolean mark;
gsize remaining;
gsize available;
remaining = buffer_size - offset;
available = max_payload_len;
if (available > remaining)
available = remaining;
mark = (remaining == available);
header = gst_rtp_vp9_create_header_buffer (self, offset == 0, mark, buffer);
sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
gst_rtp_copy_meta (GST_ELEMENT_CAST (self), header, buffer,
g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
out = gst_buffer_append (header, sub);
gst_buffer_list_insert (list, -1, out);
return available;
}
static GstFlowReturn
gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
{
GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
GstFlowReturn ret;
GstBufferList *list;
gsize size, max_paylen;
guint offset, mtu, vp9_hdr_len;
size = gst_buffer_get_size (buffer);
if (G_UNLIKELY (!gst_rtp_vp9_pay_parse_frame (self, buffer, size))) {
GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
("Failed to parse VP9 frame"));
return GST_FLOW_ERROR;
}
mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
vp9_hdr_len = gst_rtp_vp9_calc_header_len (self, TRUE);
max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp9_hdr_len, 0, 0);
list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
offset = 0;
while (offset < size) {
offset +=
gst_rtp_vp9_payload_next (self, list, offset, buffer, size, max_paylen);
}
ret = gst_rtp_base_payload_push_list (payload, list);
/* Incremenent and wrap the picture id if it overflows */
if ((self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS &&
++self->picture_id >= 0x80) ||
(self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS &&
++self->picture_id >= 0x8000))
self->picture_id = 0;
gst_buffer_unref (buffer);
return ret;
}
static gboolean
gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
{
GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
else if (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
}
return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_parent_class)->sink_event
(payload, event);
}
static gboolean
gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
{
GstCaps *src_caps;
GstStructure *s;
char *encoding_name;
src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
if (src_caps) {
src_caps = gst_caps_make_writable (src_caps);
src_caps = gst_caps_truncate (src_caps);
s = gst_caps_get_structure (src_caps, 0);
gst_structure_fixate_field_string (s, "encoding-name", "VP9");
encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
gst_caps_unref (src_caps);
} else {
encoding_name = g_strdup ("VP9-DRAFT-IETF-01");
}
gst_rtp_base_payload_set_options (payload, "video", TRUE,
encoding_name, 90000);
g_free (encoding_name);
return gst_rtp_base_payload_set_outcaps (payload, NULL);
}
gboolean
gst_rtp_vp9_pay_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "rtpvp9pay",
GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_PAY);
}

72
gst/rtp/gstrtpvp9pay.h Normal file
View file

@ -0,0 +1,72 @@
/*
* gstrtpvp9pay.h - Header for GstRtpVP9Pay
* Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
* Copyright (C) 2015 Stian Selnes <stian@pexip.com>
*
* 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
*/
#ifndef __GST_RTP_VP9_PAY_H__
#define __GST_RTP_VP9_PAY_H__
#include <gst/rtp/gstrtpbasepayload.h>
G_BEGIN_DECLS
#define GST_TYPE_RTP_VP9_PAY \
(gst_rtp_vp9_pay_get_type())
#define GST_RTP_VP9_PAY(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_RTP_VP9_PAY, GstRtpVP9Pay))
#define GST_RTP_VP9_PAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_RTP_VP9_PAY, GstRtpVP9PayClass))
#define GST_IS_RTP_VP9_PAY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_RTP_VP9_PAY))
#define GST_IS_RTP_VP9_PAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_RTP_VP9_PAY))
#define GST_RTP_VP9_PAY_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_RTP_VP9_PAY, GstRtpVP9PayClass))
typedef struct _GstRtpVP9Pay GstRtpVP9Pay;
typedef struct _GstRtpVP9PayClass GstRtpVP9PayClass;
typedef enum _VP9PictureIDMode VP9PictureIDMode;
enum _VP9PictureIDMode {
VP9_PAY_NO_PICTURE_ID,
VP9_PAY_PICTURE_ID_7BITS,
VP9_PAY_PICTURE_ID_15BITS,
};
struct _GstRtpVP9PayClass
{
GstRTPBasePayloadClass parent_class;
};
struct _GstRtpVP9Pay
{
GstRTPBasePayload parent;
gboolean is_keyframe;
guint width;
guint height;
VP9PictureIDMode picture_id_mode;
guint16 picture_id;
};
GType gst_rtp_vp9_pay_get_type (void);
gboolean gst_rtp_vp9_pay_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* #ifndef __GST_RTP_VP9_PAY_H__ */