2 * gstrtpvp9pay.c - Source for GstRtpVP9Pay
3 * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
4 * Copyright (C) 2011 Collabora Ltd.
5 * Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
6 * Copyright (C) 2015 Stian Selnes <stian@pexip.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <gst/base/gstbitreader.h>
32 #include <gst/rtp/gstrtppayloads.h>
33 #include <gst/rtp/gstrtpbuffer.h>
34 #include <gst/video/video.h>
35 #include "dboolhuff.h"
36 #include "gstrtpvp9pay.h"
37 #include "gstrtputils.h"
39 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_pay_debug);
40 #define GST_CAT_DEFAULT gst_rtp_vp9_pay_debug
42 #define DEFAULT_PICTURE_ID_MODE VP9_PAY_NO_PICTURE_ID
50 #define GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE (gst_rtp_vp9_pay_picture_id_mode_get_type())
52 gst_rtp_vp9_pay_picture_id_mode_get_type (void)
54 static GType mode_type = 0;
55 static const GEnumValue modes[] = {
56 {VP9_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
57 {VP9_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
58 {VP9_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
63 mode_type = g_enum_register_static ("GstVP9RTPPayMode", modes);
68 static void gst_rtp_vp9_pay_get_property (GObject * object, guint prop_id,
69 GValue * value, GParamSpec * pspec);
70 static void gst_rtp_vp9_pay_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
73 static GstFlowReturn gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload,
75 static gboolean gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload,
77 static gboolean gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload,
80 G_DEFINE_TYPE (GstRtpVP9Pay, gst_rtp_vp9_pay, GST_TYPE_RTP_BASE_PAYLOAD);
82 static GstStaticPadTemplate gst_rtp_vp9_pay_src_template =
83 GST_STATIC_PAD_TEMPLATE ("src",
86 GST_STATIC_CAPS ("application/x-rtp, "
87 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
88 "clock-rate = (int) 90000, encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
90 static GstStaticPadTemplate gst_rtp_vp9_pay_sink_template =
91 GST_STATIC_PAD_TEMPLATE ("sink",
94 GST_STATIC_CAPS ("video/x-vp9"));
97 gst_rtp_vp9_pay_init (GstRtpVP9Pay * obj)
99 obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
100 if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
101 obj->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
102 else if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
103 obj->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
107 gst_rtp_vp9_pay_class_init (GstRtpVP9PayClass * gst_rtp_vp9_pay_class)
109 GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp9_pay_class);
110 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_pay_class);
111 GstRTPBasePayloadClass *pay_class =
112 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_class);
114 gobject_class->set_property = gst_rtp_vp9_pay_set_property;
115 gobject_class->get_property = gst_rtp_vp9_pay_get_property;
117 g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
118 g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
119 "The picture ID mode for payloading",
120 GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
121 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
123 gst_element_class_add_static_pad_template (element_class,
124 &gst_rtp_vp9_pay_sink_template);
125 gst_element_class_add_static_pad_template (element_class,
126 &gst_rtp_vp9_pay_src_template);
128 gst_element_class_set_static_metadata (element_class, "RTP VP9 payloader",
129 "Codec/Payloader/Network/RTP",
130 "Puts VP9 video in RTP packets)", "Stian Selnes <stian@pexip.com>");
132 pay_class->handle_buffer = gst_rtp_vp9_pay_handle_buffer;
133 pay_class->sink_event = gst_rtp_vp9_pay_sink_event;
134 pay_class->set_caps = gst_rtp_vp9_pay_set_caps;
136 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_pay_debug, "rtpvp9pay", 0,
137 "VP9 Video RTP Payloader");
139 #ifndef TIZEN_FEATURE_GST_UPSTREAM_AVOID_BUILD_BREAK
140 gst_type_mark_as_plugin_api (GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE, 0);
145 gst_rtp_vp9_pay_set_property (GObject * object,
146 guint prop_id, const GValue * value, GParamSpec * pspec)
148 GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
151 case PROP_PICTURE_ID_MODE:
152 rtpvp9pay->picture_id_mode = g_value_get_enum (value);
153 if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
154 rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
155 else if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
156 rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 gst_rtp_vp9_pay_get_property (GObject * object,
166 guint prop_id, GValue * value, GParamSpec * pspec)
168 GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
171 case PROP_PICTURE_ID_MODE:
172 g_value_set_enum (value, rtpvp9pay->picture_id_mode);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 #define VP9_PROFILE_0 0
181 #define VP9_PROFILE_1 1
182 #define VP9_PROFILE_2 2
183 #define VP9_PROFILE_3 3
184 #define VP9_FRAME_MARKER 0x2
185 #define VPX_CS_SRGB 7
188 gst_rtp_vp9_pay_parse_frame (GstRtpVP9Pay * self, GstBuffer * buffer,
191 GstMapInfo map = GST_MAP_INFO_INIT;
196 guint32 tmp, profile;
198 if (G_UNLIKELY (buffer_size < 3))
201 if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
207 gst_bit_reader_init (&reader, data, size);
211 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 2)
212 || tmp != VP9_FRAME_MARKER)
215 /* profile, variable length */
216 if (!gst_bit_reader_get_bits_uint32 (&reader, &profile, 2))
219 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
224 /* show existing frame */
225 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
228 if (!gst_bit_reader_skip (&reader, 3))
234 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
236 self->is_keyframe = keyframe = (tmp == 0);
238 /* show frame and resilient mode */
239 if (!gst_bit_reader_skip (&reader, 2))
244 const guint32 sync_code = 0x498342;
245 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 24))
247 if (tmp != sync_code)
250 if (profile >= VP9_PROFILE_2) {
252 if (!gst_bit_reader_skip (&reader, 1))
257 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 3))
259 if (tmp != VPX_CS_SRGB) {
261 if (!gst_bit_reader_skip (&reader, 1))
263 if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3) {
264 /* subsampling + reserved bit */
265 if (!gst_bit_reader_skip (&reader, 2 + 1))
269 if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3)
271 if (!gst_bit_reader_skip (&reader, 1))
276 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
278 self->width = tmp + 1;
279 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
281 self->height = tmp + 1;
284 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
287 if (!gst_bit_reader_skip (&reader, 32))
291 GST_INFO_OBJECT (self, "parsed width=%d height=%d", self->width,
296 gst_buffer_unmap (buffer, &map);
300 GST_DEBUG ("Failed to parse frame");
301 if (map.memory != NULL) {
302 gst_buffer_unmap (buffer, &map);
308 gst_rtp_vp9_calc_header_len (GstRtpVP9Pay * self, gboolean start)
312 switch (self->picture_id_mode) {
313 case VP9_PAY_PICTURE_ID_7BITS:
316 case VP9_PAY_PICTURE_ID_15BITS:
322 /* Assume non-flexible mode */
323 /* Assume L-bit not set, no L header */
325 if (self->is_keyframe && start) {
326 /* Assume V-bit set */
327 /* FIXME: SS depends on layers and prediction structure */
328 /* For now assume 1 spatial and 1 temporal layer. */
329 /* FIXME: Only for the first packet in the key frame */
336 /* VP9 RTP header, non-flexible mode:
340 |I|P|L|F|B|E|V|-| (REQUIRED)
342 I: |M| PICTURE ID | (RECOMMENDED)
344 M: | EXTENDED PID | (RECOMMENDED)
346 L: | T |U| S |D| (CONDITIONALLY RECOMMENDED)
348 P,F: | P_DIFF |X|N| (CONDITIONALLY RECOMMENDED) .
349 +-+-+-+-+-+-+-+-+ . - up to 3 times
350 X: |EXTENDED P_DIFF| (OPTIONAL) .
356 Scalability structure (SS)
357 (from https://chromium.googlesource.com/external/webrtc/+/HEAD/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc
358 since latest draft is not up to date with chromium)
363 Y: | WIDTH | (OPTIONAL) .
366 +-+-+-+-+-+-+-+-+ . N_S + 1 times
367 | HEIGHT | (OPTIONAL) .
371 G: | N_G | (OPTIONAL)
373 N_G: | T |U| R |-|-| (OPTIONAL) .
374 +-+-+-+-+-+-+-+-+ -| . N_G times
375 | P_DIFF | (OPTIONAL) . R times .
376 +-+-+-+-+-+-+-+-+ -| -|
380 /* When growing the vp9 header keep max payload len calculation in sync */
382 gst_rtp_vp9_create_header_buffer (GstRtpVP9Pay * self,
383 gboolean start, gboolean mark, GstBuffer * in)
387 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
389 guint hdrlen = gst_rtp_vp9_calc_header_len (self, start);
391 out = gst_rtp_buffer_new_allocate (hdrlen, 0, 0);
392 gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
393 p = gst_rtp_buffer_get_payload (&rtpbuffer);
396 if (self->picture_id_mode != VP9_PAY_NO_PICTURE_ID) {
398 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS) {
400 p[off++] = self->picture_id & 0x7F;
403 p[off++] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
404 p[off++] = self->picture_id & 0xFF;
408 if (!self->is_keyframe)
415 if (self->is_keyframe && start) {
417 /* scalability structure, hard coded for now to be similar to chromium for
418 * quick and dirty interop */
419 p[off++] = 0x18; /* N_S=0 Y=1 G=1 */
420 p[off++] = self->width >> 8;
421 p[off++] = self->width & 0xFF;
422 p[off++] = self->height >> 8;
423 p[off++] = self->height & 0xFF;
424 p[off++] = 0x01; /* N_G=1 */
425 p[off++] = 0x04; /* T=0, U=0, R=1 */
426 p[off++] = 0x01; /* P_DIFF=1 */
429 g_assert_cmpint (off, ==, hdrlen);
431 gst_rtp_buffer_set_marker (&rtpbuffer, mark);
433 gst_rtp_buffer_unmap (&rtpbuffer);
435 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
436 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
442 gst_rtp_vp9_payload_next (GstRtpVP9Pay * self, GstBufferList * list,
443 guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len)
452 remaining = buffer_size - offset;
453 available = max_payload_len;
454 if (available > remaining)
455 available = remaining;
457 mark = (remaining == available);
458 header = gst_rtp_vp9_create_header_buffer (self, offset == 0, mark, buffer);
459 sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
461 gst_rtp_copy_video_meta (self, header, buffer);
463 out = gst_buffer_append (header, sub);
465 gst_buffer_list_insert (list, -1, out);
472 gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
474 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
477 gsize size, max_paylen;
478 guint offset, mtu, vp9_hdr_len;
480 size = gst_buffer_get_size (buffer);
482 if (G_UNLIKELY (!gst_rtp_vp9_pay_parse_frame (self, buffer, size))) {
483 GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
484 ("Failed to parse VP9 frame"));
485 return GST_FLOW_ERROR;
488 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
489 vp9_hdr_len = gst_rtp_vp9_calc_header_len (self, TRUE);
490 max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp9_hdr_len, 0, 0);
492 list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
495 while (offset < size) {
497 gst_rtp_vp9_payload_next (self, list, offset, buffer, size, max_paylen);
500 ret = gst_rtp_base_payload_push_list (payload, list);
502 /* Incremenent and wrap the picture id if it overflows */
503 if ((self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS &&
504 ++self->picture_id >= 0x80) ||
505 (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS &&
506 ++self->picture_id >= 0x8000))
507 self->picture_id = 0;
509 gst_buffer_unref (buffer);
515 gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
517 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
519 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
520 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
521 self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
522 else if (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
523 self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
526 return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_parent_class)->sink_event
531 gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
534 const char *encoding_name = "VP9";
536 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
541 s = gst_caps_get_structure (src_caps, 0);
543 if (gst_structure_has_field (s, "encoding-name")) {
544 GValue default_value = G_VALUE_INIT;
546 g_value_init (&default_value, G_TYPE_STRING);
547 g_value_set_static_string (&default_value, encoding_name);
549 value = gst_structure_get_value (s, "encoding-name");
550 if (!gst_value_can_intersect (&default_value, value))
551 encoding_name = "VP9-DRAFT-IETF-01";
553 gst_caps_unref (src_caps);
556 gst_rtp_base_payload_set_options (payload, "video", TRUE,
557 encoding_name, 90000);
559 return gst_rtp_base_payload_set_outcaps (payload, NULL);
563 gst_rtp_vp9_pay_plugin_init (GstPlugin * plugin)
565 return gst_element_register (plugin, "rtpvp9pay",
566 GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_PAY);