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");
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:
312 case VP9_PAY_PICTURE_ID_15BITS:
318 /* Assume non-flexible mode */
319 /* Assume L-bit not set, no L header */
321 if (self->is_keyframe && start) {
322 /* Assume V-bit set */
323 /* FIXME: SS depends on layers and prediction structure */
324 /* For now assume 1 spatial and 1 temporal layer. */
325 /* FIXME: Only for the first packet in the key frame */
332 /* VP9 RTP header, non-flexible mode:
336 |I|P|L|F|B|E|V|-| (REQUIRED)
338 I: |M| PICTURE ID | (RECOMMENDED)
340 M: | EXTENDED PID | (RECOMMENDED)
342 L: | T |U| S |D| (CONDITIONALLY RECOMMENDED)
344 P,F: | P_DIFF |X|N| (CONDITIONALLY RECOMMENDED) .
345 +-+-+-+-+-+-+-+-+ . - up to 3 times
346 X: |EXTENDED P_DIFF| (OPTIONAL) .
352 Scalability structure (SS)
353 (from https://chromium.googlesource.com/external/webrtc/+/HEAD/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc
354 since latest draft is not up to date with chromium)
359 Y: | WIDTH | (OPTIONAL) .
362 +-+-+-+-+-+-+-+-+ . N_S + 1 times
363 | HEIGHT | (OPTIONAL) .
367 G: | N_G | (OPTIONAL)
369 N_G: | T |U| R |-|-| (OPTIONAL) .
370 +-+-+-+-+-+-+-+-+ -| . N_G times
371 | P_DIFF | (OPTIONAL) . R times .
372 +-+-+-+-+-+-+-+-+ -| -|
376 /* When growing the vp9 header keep max payload len calculation in sync */
378 gst_rtp_vp9_create_header_buffer (GstRtpVP9Pay * self,
379 gboolean start, gboolean mark, GstBuffer * in)
383 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
385 guint hdrlen = gst_rtp_vp9_calc_header_len (self, start);
387 out = gst_rtp_buffer_new_allocate (hdrlen, 0, 0);
388 gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
389 p = gst_rtp_buffer_get_payload (&rtpbuffer);
392 if (self->picture_id_mode != VP9_PAY_NO_PICTURE_ID) {
394 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS) {
396 p[off++] = self->picture_id & 0x7F;
399 p[off++] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
400 p[off++] = self->picture_id & 0xFF;
404 if (!self->is_keyframe)
411 if (self->is_keyframe && start) {
413 /* scalability structure, hard coded for now to be similar to chromium for
414 * quick and dirty interop */
415 p[off++] = 0x18; /* N_S=0 Y=1 G=1 */
416 p[off++] = self->width >> 8;
417 p[off++] = self->width & 0xFF;
418 p[off++] = self->height >> 8;
419 p[off++] = self->height & 0xFF;
420 p[off++] = 0x01; /* N_G=1 */
421 p[off++] = 0x04; /* T=0, U=0, R=1 */
422 p[off++] = 0x01; /* P_DIFF=1 */
425 g_assert_cmpint (off, ==, hdrlen);
427 gst_rtp_buffer_set_marker (&rtpbuffer, mark);
429 gst_rtp_buffer_unmap (&rtpbuffer);
431 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
432 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
438 gst_rtp_vp9_payload_next (GstRtpVP9Pay * self, GstBufferList * list,
439 guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len)
448 remaining = buffer_size - offset;
449 available = max_payload_len;
450 if (available > remaining)
451 available = remaining;
453 mark = (remaining == available);
454 header = gst_rtp_vp9_create_header_buffer (self, offset == 0, mark, buffer);
455 sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
457 gst_rtp_copy_video_meta (self, header, buffer);
459 out = gst_buffer_append (header, sub);
461 gst_buffer_list_insert (list, -1, out);
468 gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
470 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
473 gsize size, max_paylen;
474 guint offset, mtu, vp9_hdr_len;
476 size = gst_buffer_get_size (buffer);
478 if (G_UNLIKELY (!gst_rtp_vp9_pay_parse_frame (self, buffer, size))) {
479 GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
480 ("Failed to parse VP9 frame"));
481 return GST_FLOW_ERROR;
484 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
485 vp9_hdr_len = gst_rtp_vp9_calc_header_len (self, TRUE);
486 max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp9_hdr_len, 0, 0);
488 list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
491 while (offset < size) {
493 gst_rtp_vp9_payload_next (self, list, offset, buffer, size, max_paylen);
496 ret = gst_rtp_base_payload_push_list (payload, list);
498 /* Incremenent and wrap the picture id if it overflows */
499 if ((self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS &&
500 ++self->picture_id >= 0x80) ||
501 (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS &&
502 ++self->picture_id >= 0x8000))
503 self->picture_id = 0;
505 gst_buffer_unref (buffer);
511 gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
513 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
515 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
516 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
517 self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
518 else if (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
519 self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
522 return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_parent_class)->sink_event
527 gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
533 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
535 src_caps = gst_caps_make_writable (src_caps);
536 src_caps = gst_caps_truncate (src_caps);
537 s = gst_caps_get_structure (src_caps, 0);
538 gst_structure_fixate_field_string (s, "encoding-name", "VP9");
539 encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
540 gst_caps_unref (src_caps);
542 encoding_name = g_strdup ("VP9-DRAFT-IETF-01");
545 gst_rtp_base_payload_set_options (payload, "video", TRUE,
546 encoding_name, 90000);
547 g_free (encoding_name);
548 return gst_rtp_base_payload_set_outcaps (payload, NULL);
552 gst_rtp_vp9_pay_plugin_init (GstPlugin * plugin)
554 return gst_element_register (plugin, "rtpvp9pay",
555 GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_PAY);