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_pad_template (element_class,
124 gst_static_pad_template_get (&gst_rtp_vp9_pay_sink_template));
125 gst_element_class_add_pad_template (element_class,
126 gst_static_pad_template_get (&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");
141 gst_rtp_vp9_pay_set_property (GObject * object,
142 guint prop_id, const GValue * value, GParamSpec * pspec)
144 GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
147 case PROP_PICTURE_ID_MODE:
148 rtpvp9pay->picture_id_mode = g_value_get_enum (value);
149 if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
150 rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
151 else if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
152 rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 gst_rtp_vp9_pay_get_property (GObject * object,
162 guint prop_id, GValue * value, GParamSpec * pspec)
164 GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
167 case PROP_PICTURE_ID_MODE:
168 g_value_set_enum (value, rtpvp9pay->picture_id_mode);
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 #define VP9_PROFILE_0 0
177 #define VP9_PROFILE_1 1
178 #define VP9_PROFILE_2 2
179 #define VP9_PROFILE_3 3
180 #define VP9_FRAME_MARKER 0x2
181 #define VPX_CS_SRGB 7
184 gst_rtp_vp9_pay_parse_frame (GstRtpVP9Pay * self, GstBuffer * buffer,
187 GstMapInfo map = GST_MAP_INFO_INIT;
192 guint32 tmp, profile;
194 if (G_UNLIKELY (buffer_size < 3))
197 if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
203 gst_bit_reader_init (&reader, data, size);
207 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 2)
208 || tmp != VP9_FRAME_MARKER)
211 /* profile, variable length */
212 if (!gst_bit_reader_get_bits_uint32 (&reader, &profile, 2))
215 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
220 /* show existing frame */
221 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
224 if (!gst_bit_reader_skip (&reader, 3))
230 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
232 self->is_keyframe = keyframe = (tmp == 0);
234 /* show frame and resilient mode */
235 if (!gst_bit_reader_skip (&reader, 2))
240 const guint32 sync_code = 0x498342;
241 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 24))
243 if (tmp != sync_code)
246 if (profile >= VP9_PROFILE_2) {
248 if (!gst_bit_reader_skip (&reader, 1))
253 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 3))
255 if (tmp != VPX_CS_SRGB) {
257 if (!gst_bit_reader_skip (&reader, 1))
259 if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3) {
260 /* subsampling + reserved bit */
261 if (!gst_bit_reader_skip (&reader, 2 + 1))
265 if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3)
267 if (!gst_bit_reader_skip (&reader, 1))
272 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
274 self->width = tmp + 1;
275 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
277 self->height = tmp + 1;
280 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
283 if (!gst_bit_reader_skip (&reader, 32))
287 GST_INFO_OBJECT (self, "parsed width=%d height=%d", self->width,
292 gst_buffer_unmap (buffer, &map);
296 GST_DEBUG ("Failed to parse frame");
297 if (map.memory != NULL) {
298 gst_buffer_unmap (buffer, &map);
304 gst_rtp_vp9_calc_header_len (GstRtpVP9Pay * self, gboolean start)
308 switch (self->picture_id_mode) {
309 case VP9_PAY_PICTURE_ID_7BITS:
311 case VP9_PAY_PICTURE_ID_15BITS:
317 /* Assume non-flexible mode */
318 /* Assume L-bit not set, no L header */
320 if (self->is_keyframe && start) {
321 /* Assume V-bit set */
322 /* FIXME: SS depends on layers and prediction structure */
323 /* For now assume 1 spatial and 1 temporal layer. */
324 /* FIXME: Only for the first packet in the key frame */
331 /* VP9 RTP header, non-flexible mode:
335 |I|P|L|F|B|E|V|-| (REQUIRED)
337 I: |M| PICTURE ID | (RECOMMENDED)
339 M: | EXTENDED PID | (RECOMMENDED)
341 L: | T |U| S |D| (CONDITIONALLY RECOMMENDED)
343 P,F: | P_DIFF |X|N| (CONDITIONALLY RECOMMENDED) .
344 +-+-+-+-+-+-+-+-+ . - up to 3 times
345 X: |EXTENDED P_DIFF| (OPTIONAL) .
351 Scalability structure (SS)
352 (from https://chromium.googlesource.com/external/webrtc/+/HEAD/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc
353 since latest draft is not up to date with chromium)
358 Y: | WIDTH | (OPTIONAL) .
361 +-+-+-+-+-+-+-+-+ . N_S + 1 times
362 | HEIGHT | (OPTIONAL) .
366 G: | N_G | (OPTIONAL)
368 N_G: | T |U| R |-|-| (OPTIONAL) .
369 +-+-+-+-+-+-+-+-+ -| . N_G times
370 | P_DIFF | (OPTIONAL) . R times .
371 +-+-+-+-+-+-+-+-+ -| -|
375 /* When growing the vp9 header keep max payload len calculation in sync */
377 gst_rtp_vp9_create_header_buffer (GstRtpVP9Pay * self,
378 gboolean start, gboolean mark, GstBuffer * in)
382 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
384 guint hdrlen = gst_rtp_vp9_calc_header_len (self, start);
386 out = gst_rtp_buffer_new_allocate (hdrlen, 0, 0);
387 gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
388 p = gst_rtp_buffer_get_payload (&rtpbuffer);
391 if (self->picture_id_mode != VP9_PAY_NO_PICTURE_ID) {
393 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS) {
395 p[off++] = self->picture_id & 0x7F;
398 p[off++] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
399 p[off++] = self->picture_id & 0xFF;
403 if (!self->is_keyframe)
410 if (self->is_keyframe && start) {
412 /* scalability structure, hard coded for now to be similar to chromium for
413 * quick and dirty interop */
414 p[off++] = 0x18; /* N_S=0 Y=1 G=1 */
415 p[off++] = self->width >> 8;
416 p[off++] = self->width & 0xFF;
417 p[off++] = self->height >> 8;
418 p[off++] = self->height & 0xFF;
419 p[off++] = 0x01; /* N_G=1 */
420 p[off++] = 0x04; /* T=0, U=0, R=1 */
421 p[off++] = 0x01; /* P_DIFF=1 */
424 g_assert_cmpint (off, ==, hdrlen);
426 gst_rtp_buffer_set_marker (&rtpbuffer, mark);
428 gst_rtp_buffer_unmap (&rtpbuffer);
430 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
431 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
437 gst_rtp_vp9_payload_next (GstRtpVP9Pay * self, GstBufferList * list,
438 guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len)
447 remaining = buffer_size - offset;
448 available = max_payload_len;
449 if (available > remaining)
450 available = remaining;
452 mark = (remaining == available);
453 header = gst_rtp_vp9_create_header_buffer (self, offset == 0, mark, buffer);
454 sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
456 gst_rtp_copy_meta (GST_ELEMENT_CAST (self), header, buffer,
457 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
458 out = gst_buffer_append (header, sub);
460 gst_buffer_list_insert (list, -1, out);
467 gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
469 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
472 gsize size, max_paylen;
473 guint offset, mtu, vp9_hdr_len;
475 size = gst_buffer_get_size (buffer);
477 if (G_UNLIKELY (!gst_rtp_vp9_pay_parse_frame (self, buffer, size))) {
478 GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
479 ("Failed to parse VP9 frame"));
480 return GST_FLOW_ERROR;
483 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
484 vp9_hdr_len = gst_rtp_vp9_calc_header_len (self, TRUE);
485 max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp9_hdr_len, 0, 0);
487 list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
490 while (offset < size) {
492 gst_rtp_vp9_payload_next (self, list, offset, buffer, size, max_paylen);
495 ret = gst_rtp_base_payload_push_list (payload, list);
497 /* Incremenent and wrap the picture id if it overflows */
498 if ((self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS &&
499 ++self->picture_id >= 0x80) ||
500 (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS &&
501 ++self->picture_id >= 0x8000))
502 self->picture_id = 0;
504 gst_buffer_unref (buffer);
510 gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
512 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
514 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
515 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
516 self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
517 else if (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
518 self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
521 return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_parent_class)->sink_event
526 gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
532 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
534 src_caps = gst_caps_make_writable (src_caps);
535 src_caps = gst_caps_truncate (src_caps);
536 s = gst_caps_get_structure (src_caps, 0);
537 gst_structure_fixate_field_string (s, "encoding-name", "VP9");
538 encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
539 gst_caps_unref (src_caps);
541 encoding_name = g_strdup ("VP9-DRAFT-IETF-01");
544 gst_rtp_base_payload_set_options (payload, "video", TRUE,
545 encoding_name, 90000);
546 g_free (encoding_name);
547 return gst_rtp_base_payload_set_outcaps (payload, NULL);
551 gst_rtp_vp9_pay_plugin_init (GstPlugin * plugin)
553 return gst_element_register (plugin, "rtpvp9pay",
554 GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_PAY);