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
42 #define DEFAULT_PICTURE_ID_OFFSET (-1)
48 PROP_PICTURE_ID_OFFSET
51 #define GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE (gst_rtp_vp8_pay_picture_id_mode_get_type())
53 gst_rtp_vp8_pay_picture_id_mode_get_type (void)
55 static GType mode_type = 0;
56 static const GEnumValue modes[] = {
57 {VP8_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
58 {VP8_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
59 {VP8_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
64 mode_type = g_enum_register_static ("GstVP8RTPPayMode", modes);
69 static void gst_rtp_vp8_pay_get_property (GObject * object, guint prop_id,
70 GValue * value, GParamSpec * pspec);
71 static void gst_rtp_vp8_pay_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
74 static GstFlowReturn gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload,
76 static gboolean gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload,
78 static gboolean gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload,
81 G_DEFINE_TYPE (GstRtpVP8Pay, gst_rtp_vp8_pay, GST_TYPE_RTP_BASE_PAYLOAD);
83 static GstStaticPadTemplate gst_rtp_vp8_pay_src_template =
84 GST_STATIC_PAD_TEMPLATE ("src",
87 GST_STATIC_CAPS ("application/x-rtp, "
88 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
89 "clock-rate = (int) 90000, encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
91 static GstStaticPadTemplate gst_rtp_vp8_pay_sink_template =
92 GST_STATIC_PAD_TEMPLATE ("sink",
95 GST_STATIC_CAPS ("video/x-vp8"));
98 picture_id_field_len (PictureIDMode mode)
100 if (VP8_PAY_NO_PICTURE_ID == mode)
102 if (VP8_PAY_PICTURE_ID_7BITS == mode)
108 gst_rtp_vp8_pay_picture_id_reset (GstRtpVP8Pay * obj)
112 if (obj->picture_id_offset == -1)
113 obj->picture_id = g_random_int ();
115 obj->picture_id = obj->picture_id_offset;
117 nbits = picture_id_field_len (obj->picture_id_mode);
118 obj->picture_id &= (1 << nbits) - 1;
122 gst_rtp_vp8_pay_picture_id_increment (GstRtpVP8Pay * obj)
126 if (obj->picture_id_mode == VP8_PAY_NO_PICTURE_ID)
129 nbits = picture_id_field_len (obj->picture_id_mode);
131 obj->picture_id &= (1 << nbits) - 1;
135 gst_rtp_vp8_pay_reset (GstRtpVP8Pay * obj)
137 gst_rtp_vp8_pay_picture_id_reset (obj);
138 /* tl0picidx MAY start at a random value, but there's no point. Initialize
139 * so that first packet will use 0 for convenience */
141 obj->temporal_scalability_fields_present = FALSE;
145 gst_rtp_vp8_pay_init (GstRtpVP8Pay * obj)
147 obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
148 obj->picture_id_offset = DEFAULT_PICTURE_ID_OFFSET;
149 gst_rtp_vp8_pay_reset (obj);
153 gst_rtp_vp8_pay_class_init (GstRtpVP8PayClass * gst_rtp_vp8_pay_class)
155 GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp8_pay_class);
156 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_pay_class);
157 GstRTPBasePayloadClass *pay_class =
158 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_class);
160 gobject_class->set_property = gst_rtp_vp8_pay_set_property;
161 gobject_class->get_property = gst_rtp_vp8_pay_get_property;
163 g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
164 g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
165 "The picture ID mode for payloading",
166 GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 * rtpvp8pay:picture-id-offset:
171 * Offset to add to the initial picture-id (-1 = random)
175 g_object_class_install_property (gobject_class, PROP_PICTURE_ID_OFFSET,
176 g_param_spec_int ("picture-id-offset", "Picture ID offset",
177 "Offset to add to the initial picture-id (-1 = random)",
178 -1, 0x7FFF, DEFAULT_PICTURE_ID_OFFSET,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 gst_element_class_add_static_pad_template (element_class,
182 &gst_rtp_vp8_pay_sink_template);
183 gst_element_class_add_static_pad_template (element_class,
184 &gst_rtp_vp8_pay_src_template);
186 gst_element_class_set_static_metadata (element_class, "RTP VP8 payloader",
187 "Codec/Payloader/Network/RTP",
188 "Puts VP8 video in RTP packets", "Sjoerd Simons <sjoerd@luon.net>");
190 pay_class->handle_buffer = gst_rtp_vp8_pay_handle_buffer;
191 pay_class->sink_event = gst_rtp_vp8_pay_sink_event;
192 pay_class->set_caps = gst_rtp_vp8_pay_set_caps;
194 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_pay_debug, "rtpvp8pay", 0,
195 "VP8 Video RTP Payloader");
197 gst_type_mark_as_plugin_api (GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE, 0);
201 gst_rtp_vp8_pay_set_property (GObject * object,
202 guint prop_id, const GValue * value, GParamSpec * pspec)
204 GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
207 case PROP_PICTURE_ID_MODE:
208 rtpvp8pay->picture_id_mode = g_value_get_enum (value);
209 gst_rtp_vp8_pay_picture_id_reset (rtpvp8pay);
211 case PROP_PICTURE_ID_OFFSET:
212 rtpvp8pay->picture_id_offset = g_value_get_int (value);
213 gst_rtp_vp8_pay_picture_id_reset (rtpvp8pay);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 gst_rtp_vp8_pay_get_property (GObject * object,
223 guint prop_id, GValue * value, GParamSpec * pspec)
225 GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
228 case PROP_PICTURE_ID_MODE:
229 g_value_set_enum (value, rtpvp8pay->picture_id_mode);
231 case PROP_PICTURE_ID_OFFSET:
232 g_value_set_int (value, rtpvp8pay->picture_id_offset);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 gst_rtp_vp8_pay_parse_frame (GstRtpVP8Pay * self, GstBuffer * buffer,
244 GstMapInfo map = GST_MAP_INFO_INIT;
250 guint32 partition0_size;
258 if (G_UNLIKELY (buffer_size < 3))
261 if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
267 gst_bit_reader_init (&reader, data, size);
269 self->is_keyframe = keyframe = ((data[0] & 0x1) == 0);
270 version = (data[0] >> 1) & 0x7;
272 if (G_UNLIKELY (version > 3)) {
273 GST_ERROR_OBJECT (self, "Unknown VP8 version %u", version);
277 /* keyframe, version and show_frame use 5 bits */
278 partition0_size = data[2] << 11 | data[1] << 3 | (data[0] >> 5);
280 /* Include the uncompressed data blob in the first partition */
281 offset = keyframe ? 10 : 3;
282 partition0_size += offset;
284 if (!gst_bit_reader_skip (&reader, 24))
288 /* check start tag: 0x9d 0x01 0x2a */
289 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x9d)
292 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x01)
295 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x2a)
298 /* Skip horizontal size code (16 bits) vertical size code (16 bits) */
299 if (!gst_bit_reader_skip (&reader, 32))
303 offset = keyframe ? 10 : 3;
304 vp8dx_start_decode (&bc, data + offset, size - offset);
307 /* color space (1 bit) and clamping type (1 bit) */
308 vp8dx_decode_bool (&bc, 0x80);
309 vp8dx_decode_bool (&bc, 0x80);
312 /* segmentation_enabled */
313 if (vp8dx_decode_bool (&bc, 0x80)) {
314 guint8 update_mb_segmentation_map = vp8dx_decode_bool (&bc, 0x80);
315 guint8 update_segment_feature_data = vp8dx_decode_bool (&bc, 0x80);
317 if (update_segment_feature_data) {
318 /* skip segment feature mode */
319 vp8dx_decode_bool (&bc, 0x80);
321 /* quantizer update */
322 for (i = 0; i < 4; i++) {
323 /* skip flagged quantizer value (7 bits) and sign (1 bit) */
324 if (vp8dx_decode_bool (&bc, 0x80))
325 vp8_decode_value (&bc, 8);
328 /* loop filter update */
329 for (i = 0; i < 4; i++) {
330 /* skip flagged lf update value (6 bits) and sign (1 bit) */
331 if (vp8dx_decode_bool (&bc, 0x80))
332 vp8_decode_value (&bc, 7);
336 if (update_mb_segmentation_map) {
337 /* segment prob update */
338 for (i = 0; i < 3; i++) {
339 /* skip flagged segment prob */
340 if (vp8dx_decode_bool (&bc, 0x80))
341 vp8_decode_value (&bc, 8);
346 /* skip filter type (1 bit), loop filter level (6 bits) and
347 * sharpness level (3 bits) */
348 vp8_decode_value (&bc, 1);
349 vp8_decode_value (&bc, 6);
350 vp8_decode_value (&bc, 3);
352 /* loop_filter_adj_enabled */
353 if (vp8dx_decode_bool (&bc, 0x80)) {
356 if (vp8dx_decode_bool (&bc, 0x80)) {
358 for (i = 0; i < 8; i++) {
359 /* 8 updates, 1 bit indicate whether there is one and if follow by a
361 if (vp8dx_decode_bool (&bc, 0x80))
362 vp8_decode_value (&bc, 7);
367 if (vp8dx_bool_error (&bc))
370 tmp8 = vp8_decode_value (&bc, 2);
372 partitions = 1 << tmp8;
374 /* Check if things are still sensible */
375 if (partition0_size + (partitions - 1) * 3 >= size)
378 /* partition data is right after the mode partition */
379 pdata = data + partition0_size;
382 self->n_partitions = partitions + 1;
383 self->partition_offset[0] = 0;
384 self->partition_size[0] = partition0_size + (partitions - 1) * 3;
386 self->partition_offset[1] = self->partition_size[0];
387 for (i = 1; i < partitions; i++) {
388 guint psize = (pdata[2] << 16 | pdata[1] << 8 | pdata[0]);
391 self->partition_size[i] = psize;
392 self->partition_offset[i + 1] = self->partition_offset[i] + psize;
395 /* Check that our partition offsets and sizes don't go outsize the buffer
397 if (self->partition_offset[i] >= size)
400 self->partition_size[i] = size - self->partition_offset[i];
402 self->partition_offset[i + 1] = size;
404 gst_buffer_unmap (buffer, &map);
408 GST_DEBUG ("Failed to parse frame");
409 if (map.memory != NULL) {
410 gst_buffer_unmap (buffer, &map);
416 gst_rtp_vp8_offset_to_partition (GstRtpVP8Pay * self, guint offset)
420 for (i = 1; i < self->n_partitions; i++) {
421 if (offset < self->partition_offset[i])
429 gst_rtp_vp8_calc_header_len (GstRtpVP8Pay * self)
433 switch (self->picture_id_mode) {
434 case VP8_PAY_PICTURE_ID_7BITS:
437 case VP8_PAY_PICTURE_ID_15BITS:
440 case VP8_PAY_NO_PICTURE_ID:
446 if (self->temporal_scalability_fields_present) {
447 /* Add on space for TL0PICIDX and TID/Y/KEYIDX */
452 /* All fields above are extension, so allocate space for the ECB field */
456 return len + 1; /* computed + fixed size header */
459 /* When growing the vp8 header keep max payload len calculation in sync */
461 gst_rtp_vp8_create_header_buffer (GstRtpVP8Pay * self, guint8 partid,
462 gboolean start, gboolean mark, GstBuffer * in, GstCustomMeta * meta)
466 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
469 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD_CAST
470 (self), gst_rtp_vp8_calc_header_len (self), 0, 0);
471 gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
472 p = gst_rtp_buffer_get_payload (&rtpbuffer);
474 /* X=0,R=0,N=0,S=start,PartID=partid */
475 p[0] = (start << 4) | partid;
476 if (GST_BUFFER_FLAG_IS_SET (in, GST_BUFFER_FLAG_DROPPABLE)) {
481 if (self->picture_id_mode != VP8_PAY_NO_PICTURE_ID ||
482 self->temporal_scalability_fields_present) {
488 /* X: I=0,L=0,T=0,K=0,RSV=0 */
490 if (self->picture_id_mode != VP8_PAY_NO_PICTURE_ID) {
494 if (self->temporal_scalability_fields_present) {
495 /* Set L and T bits */
499 /* Insert picture ID */
500 if (self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS) {
501 /* I: 7 bit picture_id */
502 p[2] = self->picture_id & 0x7F;
504 } else if (self->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS) {
505 /* I: 15 bit picture_id */
506 p[2] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
507 p[3] = self->picture_id & 0xFF;
513 /* Insert TL0PICIDX and TID/Y/KEYIDX */
514 if (self->temporal_scalability_fields_present) {
515 /* The meta contains tl0picidx from the encoder, but we need to ensure
516 * that tl0picidx is increasing correctly. The encoder may reset it's
517 * state and counter, but we cannot. Therefore, we cannot simply copy
518 * the value into the header.*/
519 guint temporal_layer = 0;
520 gboolean layer_sync = FALSE;
521 gboolean use_temporal_scaling = FALSE;
524 GstStructure *s = gst_custom_meta_get_structure (meta);
525 gst_structure_get_boolean (s, "use-temporal-scaling",
526 &use_temporal_scaling);
528 if (use_temporal_scaling)
529 gst_structure_get (s, "layer-id", G_TYPE_UINT, &temporal_layer,
530 "layer-sync", G_TYPE_BOOLEAN, &layer_sync, NULL);
533 /* FIXME: Support a prediction structure where higher layers don't
534 * necessarily refer to the last base layer frame, ie they use an older
535 * tl0picidx as signalled in the meta */
536 if (temporal_layer == 0 && start)
538 p[index] = self->tl0picidx & 0xFF;
539 p[index + 1] = ((temporal_layer << 6) | (layer_sync << 5)) & 0xFF;
543 gst_rtp_buffer_set_marker (&rtpbuffer, mark);
545 gst_rtp_buffer_unmap (&rtpbuffer);
547 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
548 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
554 foreach_metadata_drop (GstBuffer * buf, GstMeta ** meta, gpointer user_data)
556 GstElement *element = user_data;
557 const GstMetaInfo *info = (*meta)->info;
559 if (gst_meta_info_is_custom (info) &&
560 gst_custom_meta_has_name ((GstCustomMeta *) * meta, "GstVP8Meta")) {
561 GST_DEBUG_OBJECT (element, "dropping GstVP8Meta");
569 gst_rtp_vp8_drop_vp8_meta (gpointer element, GstBuffer * buf)
571 gst_buffer_foreach_meta (buf, foreach_metadata_drop, element);
575 gst_rtp_vp8_payload_next (GstRtpVP8Pay * self, GstBufferList * list,
576 guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len,
577 GstCustomMeta * meta)
588 remaining = buffer_size - offset;
589 available = max_payload_len;
590 if (available > remaining)
591 available = remaining;
594 /* If meta is present, then we have no partition offset information,
595 * so always emit PID 0 and set the start bit for the first packet
596 * of a frame only (c.f. RFC7741 $4.4)
599 start = (offset == 0);
601 partition = gst_rtp_vp8_offset_to_partition (self, offset);
602 g_assert (partition < self->n_partitions);
603 start = (offset == self->partition_offset[partition]);
606 mark = (remaining == available);
607 /* whole set of partitions, payload them and done */
608 header = gst_rtp_vp8_create_header_buffer (self, partition,
609 start, mark, buffer, meta);
610 sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
612 gst_rtp_copy_video_meta (self, header, buffer);
613 gst_rtp_vp8_drop_vp8_meta (self, header);
615 out = gst_buffer_append (header, sub);
617 gst_buffer_list_insert (list, -1, out);
624 gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
626 GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
630 gsize size, max_paylen;
631 guint offset, mtu, vp8_hdr_len;
633 size = gst_buffer_get_size (buffer);
634 meta = gst_buffer_get_custom_meta (buffer, "GstVP8Meta");
635 if (G_UNLIKELY (!gst_rtp_vp8_pay_parse_frame (self, buffer, size))) {
636 GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
637 ("Failed to parse VP8 frame"));
638 return GST_FLOW_ERROR;
642 GstStructure *s = gst_custom_meta_get_structure (meta);
643 gboolean use_temporal_scaling;
644 /* For interop it's most likely better to keep the temporal scalability
645 * fields present if the stream previously had them present. Alternating
646 * whether these fields are present or not may confuse the receiver. */
648 gst_structure_get_boolean (s, "use-temporal-scaling",
649 &use_temporal_scaling);
650 if (use_temporal_scaling)
651 self->temporal_scalability_fields_present = TRUE;
654 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
655 vp8_hdr_len = gst_rtp_vp8_calc_header_len (self);
656 max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp8_hdr_len, 0,
657 gst_rtp_base_payload_get_source_count (payload, buffer));
659 list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
662 while (offset < size) {
664 gst_rtp_vp8_payload_next (self, list, offset, buffer, size,
668 ret = gst_rtp_base_payload_push_list (payload, list);
670 gst_rtp_vp8_pay_picture_id_increment (self);
672 gst_buffer_unref (buffer);
678 gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
680 GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
682 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
683 gst_rtp_vp8_pay_reset (self);
686 return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_parent_class)->sink_event
691 gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
694 const char *encoding_name = "VP8";
696 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
701 s = gst_caps_get_structure (src_caps, 0);
703 if (gst_structure_has_field (s, "encoding-name")) {
704 GValue default_value = G_VALUE_INIT;
706 g_value_init (&default_value, G_TYPE_STRING);
707 g_value_set_static_string (&default_value, encoding_name);
709 value = gst_structure_get_value (s, "encoding-name");
710 if (!gst_value_can_intersect (&default_value, value))
711 encoding_name = "VP8-DRAFT-IETF-01";
713 gst_caps_unref (src_caps);
716 gst_rtp_base_payload_set_options (payload, "video", TRUE,
717 encoding_name, 90000);
719 return gst_rtp_base_payload_set_outcaps (payload, NULL);
723 gst_rtp_vp8_pay_plugin_init (GstPlugin * plugin)
725 return gst_element_register (plugin, "rtpvp8pay",
726 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_PAY);