2 * gstrtpvp8pay.c - Source for GstRtpVP8Pay
3 * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
4 * Copyright (C) 2011 Collabora Ltd.
5 * Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <gst/base/gstbitreader.h>
31 #include <gst/rtp/gstrtppayloads.h>
32 #include <gst/rtp/gstrtpbuffer.h>
33 #include <gst/video/video.h>
34 #include "dboolhuff.h"
35 #include "gstrtpvp8pay.h"
36 #include "gstrtputils.h"
38 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_pay_debug);
39 #define GST_CAT_DEFAULT gst_rtp_vp8_pay_debug
41 #define DEFAULT_PICTURE_ID_MODE VP8_PAY_NO_PICTURE_ID
49 #define GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE (gst_rtp_vp8_pay_picture_id_mode_get_type())
51 gst_rtp_vp8_pay_picture_id_mode_get_type (void)
53 static GType mode_type = 0;
54 static const GEnumValue modes[] = {
55 {VP8_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
56 {VP8_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
57 {VP8_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
62 mode_type = g_enum_register_static ("GstVP8RTPPayMode", modes);
67 static void gst_rtp_vp8_pay_get_property (GObject * object, guint prop_id,
68 GValue * value, GParamSpec * pspec);
69 static void gst_rtp_vp8_pay_set_property (GObject * object, guint prop_id,
70 const GValue * value, GParamSpec * pspec);
72 static GstFlowReturn gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload,
74 static gboolean gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload,
76 static gboolean gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload,
79 G_DEFINE_TYPE (GstRtpVP8Pay, gst_rtp_vp8_pay, GST_TYPE_RTP_BASE_PAYLOAD);
81 static GstStaticPadTemplate gst_rtp_vp8_pay_src_template =
82 GST_STATIC_PAD_TEMPLATE ("src",
85 GST_STATIC_CAPS ("application/x-rtp, "
86 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
87 "clock-rate = (int) 90000, encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
89 static GstStaticPadTemplate gst_rtp_vp8_pay_sink_template =
90 GST_STATIC_PAD_TEMPLATE ("sink",
93 GST_STATIC_CAPS ("video/x-vp8"));
96 gst_rtp_vp8_pay_init (GstRtpVP8Pay * obj)
98 obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
99 if (obj->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS)
100 obj->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
101 else if (obj->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS)
102 obj->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
106 gst_rtp_vp8_pay_class_init (GstRtpVP8PayClass * gst_rtp_vp8_pay_class)
108 GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp8_pay_class);
109 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_pay_class);
110 GstRTPBasePayloadClass *pay_class =
111 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_class);
113 gobject_class->set_property = gst_rtp_vp8_pay_set_property;
114 gobject_class->get_property = gst_rtp_vp8_pay_get_property;
116 g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
117 g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
118 "The picture ID mode for payloading",
119 GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
120 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122 gst_element_class_add_static_pad_template (element_class,
123 &gst_rtp_vp8_pay_sink_template);
124 gst_element_class_add_static_pad_template (element_class,
125 &gst_rtp_vp8_pay_src_template);
127 gst_element_class_set_static_metadata (element_class, "RTP VP8 payloader",
128 "Codec/Payloader/Network/RTP",
129 "Puts VP8 video in RTP packets", "Sjoerd Simons <sjoerd@luon.net>");
131 pay_class->handle_buffer = gst_rtp_vp8_pay_handle_buffer;
132 pay_class->sink_event = gst_rtp_vp8_pay_sink_event;
133 pay_class->set_caps = gst_rtp_vp8_pay_set_caps;
135 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_pay_debug, "rtpvp8pay", 0,
136 "VP8 Video RTP Payloader");
140 gst_rtp_vp8_pay_set_property (GObject * object,
141 guint prop_id, const GValue * value, GParamSpec * pspec)
143 GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
146 case PROP_PICTURE_ID_MODE:
147 rtpvp8pay->picture_id_mode = g_value_get_enum (value);
148 if (rtpvp8pay->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS)
149 rtpvp8pay->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
150 else if (rtpvp8pay->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS)
151 rtpvp8pay->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 gst_rtp_vp8_pay_get_property (GObject * object,
161 guint prop_id, GValue * value, GParamSpec * pspec)
163 GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
166 case PROP_PICTURE_ID_MODE:
167 g_value_set_enum (value, rtpvp8pay->picture_id_mode);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 gst_rtp_vp8_pay_parse_frame (GstRtpVP8Pay * self, GstBuffer * buffer,
179 GstMapInfo map = GST_MAP_INFO_INIT;
185 guint32 partition0_size;
193 if (G_UNLIKELY (buffer_size < 3))
196 if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
202 gst_bit_reader_init (&reader, data, size);
204 self->is_keyframe = keyframe = ((data[0] & 0x1) == 0);
205 version = (data[0] >> 1) & 0x7;
207 if (G_UNLIKELY (version > 3)) {
208 GST_ERROR_OBJECT (self, "Unknown VP8 version %u", version);
212 /* keyframe, version and show_frame use 5 bits */
213 partition0_size = data[2] << 11 | data[1] << 3 | (data[0] >> 5);
215 /* Include the uncompressed data blob in the first partition */
216 offset = keyframe ? 10 : 3;
217 partition0_size += offset;
219 if (!gst_bit_reader_skip (&reader, 24))
223 /* check start tag: 0x9d 0x01 0x2a */
224 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x9d)
227 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x01)
230 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x2a)
233 /* Skip horizontal size code (16 bits) vertical size code (16 bits) */
234 if (!gst_bit_reader_skip (&reader, 32))
238 offset = keyframe ? 10 : 3;
239 vp8dx_start_decode (&bc, data + offset, size - offset);
242 /* color space (1 bit) and clamping type (1 bit) */
243 vp8dx_decode_bool (&bc, 0x80);
244 vp8dx_decode_bool (&bc, 0x80);
247 /* segmentation_enabled */
248 if (vp8dx_decode_bool (&bc, 0x80)) {
249 guint8 update_mb_segmentation_map = vp8dx_decode_bool (&bc, 0x80);
250 guint8 update_segment_feature_data = vp8dx_decode_bool (&bc, 0x80);
252 if (update_segment_feature_data) {
253 /* skip segment feature mode */
254 vp8dx_decode_bool (&bc, 0x80);
256 /* quantizer update */
257 for (i = 0; i < 4; i++) {
258 /* skip flagged quantizer value (7 bits) and sign (1 bit) */
259 if (vp8dx_decode_bool (&bc, 0x80))
260 vp8_decode_value (&bc, 8);
263 /* loop filter update */
264 for (i = 0; i < 4; i++) {
265 /* skip flagged lf update value (6 bits) and sign (1 bit) */
266 if (vp8dx_decode_bool (&bc, 0x80))
267 vp8_decode_value (&bc, 7);
271 if (update_mb_segmentation_map) {
272 /* segment prob update */
273 for (i = 0; i < 3; i++) {
274 /* skip flagged segment prob */
275 if (vp8dx_decode_bool (&bc, 0x80))
276 vp8_decode_value (&bc, 8);
281 /* skip filter type (1 bit), loop filter level (6 bits) and
282 * sharpness level (3 bits) */
283 vp8_decode_value (&bc, 1);
284 vp8_decode_value (&bc, 6);
285 vp8_decode_value (&bc, 3);
287 /* loop_filter_adj_enabled */
288 if (vp8dx_decode_bool (&bc, 0x80)) {
291 if (vp8dx_decode_bool (&bc, 0x80)) {
293 for (i = 0; i < 8; i++) {
294 /* 8 updates, 1 bit indicate whether there is one and if follow by a
296 if (vp8dx_decode_bool (&bc, 0x80))
297 vp8_decode_value (&bc, 7);
302 if (vp8dx_bool_error (&bc))
305 tmp8 = vp8_decode_value (&bc, 2);
307 partitions = 1 << tmp8;
309 /* Check if things are still sensible */
310 if (partition0_size + (partitions - 1) * 3 >= size)
313 /* partition data is right after the mode partition */
314 pdata = data + partition0_size;
317 self->n_partitions = partitions + 1;
318 self->partition_offset[0] = 0;
319 self->partition_size[0] = partition0_size + (partitions - 1) * 3;
321 self->partition_offset[1] = self->partition_size[0];
322 for (i = 1; i < partitions; i++) {
323 guint psize = (pdata[2] << 16 | pdata[1] << 8 | pdata[0]);
326 self->partition_size[i] = psize;
327 self->partition_offset[i + 1] = self->partition_offset[i] + psize;
330 /* Check that our partition offsets and sizes don't go outsize the buffer
332 if (self->partition_offset[i] >= size)
335 self->partition_size[i] = size - self->partition_offset[i];
337 self->partition_offset[i + 1] = size;
339 gst_buffer_unmap (buffer, &map);
343 GST_DEBUG ("Failed to parse frame");
344 if (map.memory != NULL) {
345 gst_buffer_unmap (buffer, &map);
351 gst_rtp_vp8_offset_to_partition (GstRtpVP8Pay * self, guint offset)
355 for (i = 1; i < self->n_partitions; i++) {
356 if (offset < self->partition_offset[i])
364 gst_rtp_vp8_calc_header_len (GstRtpVP8Pay * self)
366 switch (self->picture_id_mode) {
367 case VP8_PAY_PICTURE_ID_7BITS:
369 case VP8_PAY_PICTURE_ID_15BITS:
371 case VP8_PAY_NO_PICTURE_ID:
377 /* When growing the vp8 header keep max payload len calculation in sync */
379 gst_rtp_vp8_create_header_buffer (GstRtpVP8Pay * self, guint8 partid,
380 gboolean start, gboolean mark, GstBuffer * in)
384 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
386 out = gst_rtp_buffer_new_allocate (gst_rtp_vp8_calc_header_len (self), 0, 0);
387 gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
388 p = gst_rtp_buffer_get_payload (&rtpbuffer);
389 /* X=0,R=0,N=0,S=start,PartID=partid */
390 p[0] = (start << 4) | partid;
391 if (self->picture_id_mode != VP8_PAY_NO_PICTURE_ID) {
394 /* X: I=1,L=0,T=0,K=0,RSV=0 */
396 if (self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS) {
397 /* I: 7 bit picture_id */
398 p[2] = self->picture_id & 0x7F;
400 /* I: 15 bit picture_id */
401 p[2] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
402 p[3] = self->picture_id & 0xFF;
406 gst_rtp_buffer_set_marker (&rtpbuffer, mark);
408 gst_rtp_buffer_unmap (&rtpbuffer);
410 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
411 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
417 gst_rtp_vp8_payload_next (GstRtpVP8Pay * self, GstBufferList * list,
418 guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len)
428 remaining = buffer_size - offset;
429 available = max_payload_len;
430 if (available > remaining)
431 available = remaining;
433 partition = gst_rtp_vp8_offset_to_partition (self, offset);
434 g_assert (partition < self->n_partitions);
436 mark = (remaining == available);
437 /* whole set of partitions, payload them and done */
438 header = gst_rtp_vp8_create_header_buffer (self, partition,
439 offset == self->partition_offset[partition], mark, buffer);
440 sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
442 gst_rtp_copy_video_meta (self, header, buffer);
444 out = gst_buffer_append (header, sub);
446 gst_buffer_list_insert (list, -1, out);
453 gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
455 GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
458 gsize size, max_paylen;
459 guint offset, mtu, vp8_hdr_len;
461 size = gst_buffer_get_size (buffer);
463 if (G_UNLIKELY (!gst_rtp_vp8_pay_parse_frame (self, buffer, size))) {
464 GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
465 ("Failed to parse VP8 frame"));
466 return GST_FLOW_ERROR;
469 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
470 vp8_hdr_len = gst_rtp_vp8_calc_header_len (self);
471 max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp8_hdr_len, 0, 0);
473 list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
476 while (offset < size) {
478 gst_rtp_vp8_payload_next (self, list, offset, buffer, size, max_paylen);
481 ret = gst_rtp_base_payload_push_list (payload, list);
483 /* Incremenent and wrap the picture id if it overflows */
484 if ((self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS &&
485 ++self->picture_id >= 0x80) ||
486 (self->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS &&
487 ++self->picture_id >= 0x8000))
488 self->picture_id = 0;
490 gst_buffer_unref (buffer);
496 gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
498 GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
500 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
501 if (self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS)
502 self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
503 else if (self->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS)
504 self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
507 return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_parent_class)->sink_event
512 gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
518 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
520 src_caps = gst_caps_make_writable (src_caps);
521 src_caps = gst_caps_truncate (src_caps);
522 s = gst_caps_get_structure (src_caps, 0);
523 gst_structure_fixate_field_string (s, "encoding-name", "VP8");
524 encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
525 gst_caps_unref (src_caps);
527 encoding_name = g_strdup ("VP8-DRAFT-IETF-01");
530 gst_rtp_base_payload_set_options (payload, "video", TRUE,
531 encoding_name, 90000);
532 g_free (encoding_name);
533 return gst_rtp_base_payload_set_outcaps (payload, NULL);
537 gst_rtp_vp8_pay_plugin_init (GstPlugin * plugin)
539 return gst_element_register (plugin, "rtpvp8pay",
540 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_PAY);