Commit ef8d630a authored by Stian Selnes's avatar Stian Selnes Committed by Tim-Philipp Müller

rtp: add H.261 RTP payloader and depayloader

Implementation according to RFC 4587.

Payloader create fragments on MB boundaries in order to match MTU size
the best it can. Some decoders/depayloaders in the wild are very strict
about receiving a continuous bit-stream (e.g. no no-op bits between
frames), so the payloader will shift the compressed bit-stream of a
frame to align with the last significant bit of the previous frame.

Depayloader does not try to be fancy in case of packet loss. It simply
drops all packets for a frame if there is a loss, keeping it simple.

https://bugzilla.gnome.org/show_bug.cgi?id=751886
parent 9dfae825
......@@ -38,6 +38,8 @@ libgstrtp_la_SOURCES = \
gstrtpgsmpay.c \
gstrtpamrdepay.c \
gstrtpamrpay.c \
gstrtph261pay.c \
gstrtph261depay.c \
gstrtph263pdepay.c \
gstrtph263ppay.c \
gstrtph263depay.c \
......@@ -139,6 +141,8 @@ noinst_HEADERS = \
gstrtpmpapay.h \
gstrtpmpvdepay.h \
gstrtpmpvpay.h \
gstrtph261pay.h \
gstrtph261depay.h \
gstrtph263pdepay.h \
gstrtph263ppay.h \
gstrtph263depay.h \
......
......@@ -56,6 +56,8 @@
#include "gstrtpmparobustdepay.h"
#include "gstrtpmpvdepay.h"
#include "gstrtpmpvpay.h"
#include "gstrtph261pay.h"
#include "gstrtph261depay.h"
#include "gstrtph263pdepay.h"
#include "gstrtph263ppay.h"
#include "gstrtph263depay.h"
......@@ -204,6 +206,12 @@ plugin_init (GstPlugin * plugin)
if (!gst_rtp_mpv_pay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_h261_pay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_h261_depay_plugin_init (plugin))
return FALSE;
if (!gst_rtp_h263p_pay_plugin_init (plugin))
return FALSE;
......
/* GStreamer
*
* Copyright (C) <2014> Stian Selnes <stian@pexip.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
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <gst/rtp/gstrtpbuffer.h>
#include "gstrtph261depay.h"
#include "gstrtph261pay.h" /* GstRtpH261PayHeader */
GST_DEBUG_CATEGORY_STATIC (rtph261depay_debug);
#define GST_CAT_DEFAULT (rtph261depay_debug)
static const guint8 NO_LEFTOVER = 0xFF;
static GstStaticPadTemplate gst_rtp_h261_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-h261")
);
static GstStaticPadTemplate gst_rtp_h261_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_H261_STRING ", "
"clock-rate = (int) 90000, " "encoding-name = (string) \"H261\"; "
"application/x-rtp, "
"media = (string) \"video\", "
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
"clock-rate = (int) 90000, " "encoding-name = (string) \"H261\"")
);
G_DEFINE_TYPE (GstRtpH261Depay, gst_rtp_h261_depay,
GST_TYPE_RTP_BASE_DEPAYLOAD);
#define parent_class gst_rtp_h261_depay_parent_class
static GstBuffer *
gst_rtp_h261_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
{
GstRtpH261Depay *depay;
GstBuffer *outbuf;
gint payload_len;
guint8 *payload;
const guint header_len = GST_RTP_H261_PAYLOAD_HEADER_LEN;
gboolean marker;
GstRtpH261PayHeader *header;
GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
depay = GST_RTP_H261_DEPAY (depayload);
if (GST_BUFFER_IS_DISCONT (buf)) {
GST_DEBUG_OBJECT (depay, "Discont buffer, flushing adapter");
gst_adapter_clear (depay->adapter);
depay->leftover = NO_LEFTOVER;
depay->start = FALSE;
}
gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
payload_len = gst_rtp_buffer_get_payload_len (&rtp);
payload = gst_rtp_buffer_get_payload (&rtp);
marker = gst_rtp_buffer_get_marker (&rtp);
if (payload_len < 4) {
GST_WARNING_OBJECT (depay,
"Dropping packet with payload length invalid length");
gst_rtp_buffer_unmap (&rtp);
return NULL;
}
header = (GstRtpH261PayHeader *) payload;
GST_DEBUG_OBJECT (depay,
"payload_len: %d, header_len: %d, sbit: %d, ebit: %d, marker %d",
payload_len, header_len, header->sbit, header->ebit, marker);
payload += header_len;
payload_len -= header_len;
if (!depay->start) {
/* Check for picture start code */
guint32 bits = GST_READ_UINT32_BE (payload) << header->sbit;
if (payload_len > 4 && bits >> 12 == 0x10) {
GST_DEBUG_OBJECT (depay, "Found picture start code");
depay->start = TRUE;
} else {
GST_DEBUG_OBJECT (depay, "No picture start code yet, skipping payload");
goto skip;
}
}
if (header->sbit != 0) {
/* Take the leftover from previous packet and merge it at the beginning */
payload[0] &= 0xFF >> header->sbit;
if (depay->leftover != NO_LEFTOVER) {
/* Happens if sbit is set for first packet in frame. Then previous byte
* has already been flushed. */
payload[0] |= depay->leftover;
}
depay->leftover = NO_LEFTOVER;
}
if (header->ebit == 0) {
/* H.261 stream ends on byte boundary, take entire packet */
gst_adapter_push (depay->adapter,
gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len));
} else {
/* Take the entire buffer except for the last byte, which will be kept to
* merge with next packet */
gst_adapter_push (depay->adapter,
gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len,
payload_len - 1));
depay->leftover = payload[payload_len - 1] & (0xFF << header->ebit);
}
skip:
if (marker) {
if (depay->start) {
guint avail;
if (depay->leftover != NO_LEFTOVER) {
GstBuffer *buf = gst_buffer_new_and_alloc (1);
gst_buffer_memset (buf, 0, depay->leftover, 1);
gst_adapter_push (depay->adapter, buf);
depay->leftover = NO_LEFTOVER;
}
avail = gst_adapter_available (depay->adapter);
outbuf = gst_adapter_take_buffer (depay->adapter, avail);
/* Note that the I flag does not mean intra frame, but that the entire
* stream is intra coded. */
if (header->i)
GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
else
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
GST_DEBUG_OBJECT (depay, "Pushing out a buffer of %u bytes", avail);
gst_rtp_base_depayload_push (depayload, outbuf);
depay->start = FALSE;
} else {
depay->start = TRUE;
}
}
gst_rtp_buffer_unmap (&rtp);
return NULL;
}
static gboolean
gst_rtp_h261_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
{
GstCaps *srccaps;
srccaps = gst_caps_new_empty_simple ("video/x-h261");
gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
gst_caps_unref (srccaps);
return TRUE;
}
static GstStateChangeReturn
gst_rtp_h261_depay_change_state (GstElement * element,
GstStateChange transition)
{
GstRtpH261Depay *depay;
GstStateChangeReturn ret;
depay = GST_RTP_H261_DEPAY (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_adapter_clear (depay->adapter);
depay->start = FALSE;
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;
}
static void
gst_rtp_h261_depay_dispose (GObject * object)
{
GstRtpH261Depay *depay;
depay = GST_RTP_H261_DEPAY (object);
if (depay->adapter) {
gst_object_unref (depay->adapter);
depay->adapter = NULL;
}
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gst_rtp_h261_depay_class_init (GstRtpH261DepayClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
gobject_class = G_OBJECT_CLASS (klass);
gstelement_class = GST_ELEMENT_CLASS (klass);
gstrtpbasedepayload_class = GST_RTP_BASE_DEPAYLOAD_CLASS (klass);
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_rtp_h261_depay_src_template));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_rtp_h261_depay_sink_template));
gst_element_class_set_static_metadata (gstelement_class,
"RTP H261 depayloader", "Codec/Depayloader/Network/RTP",
"Extracts H261 video from RTP packets (RFC 4587)",
"Stian Selnes <stian@pexip.com>");
gstrtpbasedepayload_class->process = gst_rtp_h261_depay_process;
gstrtpbasedepayload_class->set_caps = gst_rtp_h261_depay_setcaps;
gobject_class->dispose = gst_rtp_h261_depay_dispose;
gstelement_class->change_state = gst_rtp_h261_depay_change_state;
GST_DEBUG_CATEGORY_INIT (rtph261depay_debug, "rtph261depay", 0,
"H261 Video RTP Depayloader");
}
static void
gst_rtp_h261_depay_init (GstRtpH261Depay * depay)
{
depay->adapter = gst_adapter_new ();
depay->leftover = NO_LEFTOVER;
}
gboolean
gst_rtp_h261_depay_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "rtph261depay",
GST_RANK_SECONDARY, GST_TYPE_RTP_H261_DEPAY);
}
/* GStreamer
* Copyright (C) <2014> Stian Selnes <stian@pexip.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
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_RTP_H261_DEPAY_H__
#define __GST_RTP_H261_DEPAY_H__
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include <gst/rtp/gstrtpbasedepayload.h>
G_BEGIN_DECLS
#define GST_TYPE_RTP_H261_DEPAY \
(gst_rtp_h261_depay_get_type())
#define GST_RTP_H261_DEPAY(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_H261_DEPAY,GstRtpH261Depay))
#define GST_RTP_H261_DEPAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_H261_DEPAY,GstRtpH261DepayClass))
#define GST_IS_RTP_H261_DEPAY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_H261_DEPAY))
#define GST_IS_RTP_H261_DEPAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_H261_DEPAY))
typedef struct _GstRtpH261Depay GstRtpH261Depay;
typedef struct _GstRtpH261DepayClass GstRtpH261DepayClass;
struct _GstRtpH261Depay
{
GstRTPBaseDepayload depayload;
GstAdapter *adapter;
gboolean start;
guint8 leftover;
};
struct _GstRtpH261DepayClass
{
GstRTPBaseDepayloadClass parent_class;
};
GType gst_rtp_h261_depay_get_type (void);
gboolean gst_rtp_h261_depay_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* __GST_RTP_H261_DEPAY_H__ */
This diff is collapsed.
/* GStreamer
* Copyright (C) <2014> Stian Selnes <stian@pexip.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
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Dejan Sakelsak sahel@kiberpipa.org
*/
#ifndef __GST_RTP_H261_PAY_H__
#define __GST_RTP_H261_PAY_H__
#include <gst/gst.h>
#include <gst/rtp/gstrtpbasepayload.h>
#include <gst/base/gstadapter.h>
G_BEGIN_DECLS
#define GST_TYPE_RTP_H261_PAY \
(gst_rtp_h261_pay_get_type())
#define GST_RTP_H261_PAY(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_H261_PAY,GstRtpH261Pay))
#define GST_RTP_H261_PAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_H261_PAY,GstRtpH261PayClass))
#define GST_IS_RTP_H261_PAY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_H261_PAY))
#define GST_IS_RTP_H261_PAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_H261_PAY))
typedef struct _GstRtpH261PayClass GstRtpH261PayClass;
typedef struct _GstRtpH261Pay GstRtpH261Pay;
struct _GstRtpH261Pay
{
GstRTPBasePayload payload;
GstAdapter *adapter;
gint offset;
GstClockTime timestamp;
};
struct _GstRtpH261PayClass
{
GstRTPBasePayloadClass parent_class;
};
typedef struct _GstRtpH261PayHeader
{
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
unsigned int v:1; /* Motion vector flag */
unsigned int i:1; /* Intra encoded data */
unsigned int ebit:3; /* End position */
unsigned int sbit:3; /* Start position */
unsigned int mbap1:4; /* MB address predictor - part1 */
unsigned int gobn:4; /* GOB number */
unsigned int hmvd1:2; /* Horizontal motion vector data - part1 */
unsigned int quant:5; /* Quantizer */
unsigned int mbap2:1; /* MB address predictor - part2 */
unsigned int vmvd:5; /* Horizontal motion vector data - part1 */
unsigned int hmvd2:3; /* Vertical motion vector data */
#elif G_BYTE_ORDER == G_BIG_ENDIAN
unsigned int sbit:3; /* Start position */
unsigned int ebit:3; /* End position */
unsigned int i:1; /* Intra encoded data */
unsigned int v:1; /* Motion vector flag */
unsigned int gobn:4; /* GOB number */
unsigned int mbap1:4; /* MB address predictor - part1 */
unsigned int mbap2:1; /* MB address predictor - part2 */
unsigned int quant:5; /* Quantizer */
unsigned int hmvd1:2; /* Horizontal motion vector data - part1 */
unsigned int hmvd2:3; /* Vertical motion vector data */
unsigned int vmvd:5; /* Horizontal motion vector data - part1 */
#else
#error "G_BYTE_ORDER should be big or little endian."
#endif
} GstRtpH261PayHeader;
#define GST_RTP_H261_PAYLOAD_HEADER_LEN 4
GType gst_rtp_h261_pay_get_type (void);
gboolean gst_rtp_h261_pay_plugin_init (GstPlugin * plugin);
G_END_DECLS
#endif /* __GST_RTP_H261_PAY_H__ */
......@@ -464,6 +464,22 @@ GST_START_TEST (rtp_mpa)
}
GST_END_TEST;
static const guint8 rtp_h261_frame_data[] = {
0x00, 0x01, 0x00, 0x06, 0x00, 0x01, 0x11, 0x00, 0x00, 0x4c, 0x40, 0x00,
0x15, 0x10,
};
static int rtp_h261_frame_data_size = 14;
static int rtp_h261_frame_count = 1;
GST_START_TEST (rtp_h261)
{
rtp_pipeline_test (rtp_h261_frame_data, rtp_h261_frame_data_size,
rtp_h261_frame_count, "video/x-h261", "rtph261pay", "rtph261depay",
0, 0, FALSE);
}
GST_END_TEST;
static const guint8 rtp_h263_frame_data[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
......@@ -945,6 +961,7 @@ rtp_payloading_suite (void)
tcase_add_test (tc_chain, rtp_pcma);
tcase_add_test (tc_chain, rtp_pcmu);
tcase_add_test (tc_chain, rtp_mpa);
tcase_add_test (tc_chain, rtp_h261);
tcase_add_test (tc_chain, rtp_h263);
tcase_add_test (tc_chain, rtp_h263p);
tcase_add_test (tc_chain, rtp_h264);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment