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 "gstrtpelements.h"
36 #include "dboolhuff.h"
37 #include "gstrtpvp9pay.h"
38 #include "gstrtputils.h"
40 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_pay_debug);
41 #define GST_CAT_DEFAULT gst_rtp_vp9_pay_debug
43 #define DEFAULT_PICTURE_ID_MODE VP9_PAY_NO_PICTURE_ID
51 #define GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE (gst_rtp_vp9_pay_picture_id_mode_get_type())
53 gst_rtp_vp9_pay_picture_id_mode_get_type (void)
55 static GType mode_type = 0;
56 static const GEnumValue modes[] = {
57 {VP9_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
58 {VP9_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
59 {VP9_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
64 mode_type = g_enum_register_static ("GstVP9RTPPayMode", modes);
69 static void gst_rtp_vp9_pay_get_property (GObject * object, guint prop_id,
70 GValue * value, GParamSpec * pspec);
71 static void gst_rtp_vp9_pay_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
74 static GstFlowReturn gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload,
76 static gboolean gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload,
78 static gboolean gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload,
81 G_DEFINE_TYPE (GstRtpVP9Pay, gst_rtp_vp9_pay, GST_TYPE_RTP_BASE_PAYLOAD);
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpvp9pay, "rtpvp9pay",
83 GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_PAY, rtp_element_init (plugin));
85 static GstStaticPadTemplate gst_rtp_vp9_pay_src_template =
86 GST_STATIC_PAD_TEMPLATE ("src",
89 GST_STATIC_CAPS ("application/x-rtp, "
90 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
91 "clock-rate = (int) 90000, encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
93 static GstStaticPadTemplate gst_rtp_vp9_pay_sink_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
97 GST_STATIC_CAPS ("video/x-vp9"));
100 gst_rtp_vp9_pay_init (GstRtpVP9Pay * obj)
102 obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
103 if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
104 obj->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
105 else if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
106 obj->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
110 gst_rtp_vp9_pay_class_init (GstRtpVP9PayClass * gst_rtp_vp9_pay_class)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp9_pay_class);
113 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_pay_class);
114 GstRTPBasePayloadClass *pay_class =
115 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_class);
117 gobject_class->set_property = gst_rtp_vp9_pay_set_property;
118 gobject_class->get_property = gst_rtp_vp9_pay_get_property;
120 g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
121 g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
122 "The picture ID mode for payloading",
123 GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
124 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126 gst_element_class_add_static_pad_template (element_class,
127 &gst_rtp_vp9_pay_sink_template);
128 gst_element_class_add_static_pad_template (element_class,
129 &gst_rtp_vp9_pay_src_template);
131 gst_element_class_set_static_metadata (element_class, "RTP VP9 payloader",
132 "Codec/Payloader/Network/RTP",
133 "Puts VP9 video in RTP packets)", "Stian Selnes <stian@pexip.com>");
135 pay_class->handle_buffer = gst_rtp_vp9_pay_handle_buffer;
136 pay_class->sink_event = gst_rtp_vp9_pay_sink_event;
137 pay_class->set_caps = gst_rtp_vp9_pay_set_caps;
139 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_pay_debug, "rtpvp9pay", 0,
140 "VP9 Video RTP Payloader");
142 gst_type_mark_as_plugin_api (GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE, 0);
146 gst_rtp_vp9_pay_set_property (GObject * object,
147 guint prop_id, const GValue * value, GParamSpec * pspec)
149 GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
152 case PROP_PICTURE_ID_MODE:
153 rtpvp9pay->picture_id_mode = g_value_get_enum (value);
154 if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
155 rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
156 else if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
157 rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 gst_rtp_vp9_pay_get_property (GObject * object,
167 guint prop_id, GValue * value, GParamSpec * pspec)
169 GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
172 case PROP_PICTURE_ID_MODE:
173 g_value_set_enum (value, rtpvp9pay->picture_id_mode);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 #define VP9_PROFILE_0 0
182 #define VP9_PROFILE_1 1
183 #define VP9_PROFILE_2 2
184 #define VP9_PROFILE_3 3
185 #define VP9_FRAME_MARKER 0x2
186 #define VPX_CS_SRGB 7
189 gst_rtp_vp9_pay_parse_frame (GstRtpVP9Pay * self, GstBuffer * buffer,
192 GstMapInfo map = GST_MAP_INFO_INIT;
197 guint32 tmp, profile;
199 if (G_UNLIKELY (buffer_size < 3))
202 if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
208 gst_bit_reader_init (&reader, data, size);
212 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 2)
213 || tmp != VP9_FRAME_MARKER)
216 /* profile, variable length */
217 if (!gst_bit_reader_get_bits_uint32 (&reader, &profile, 2))
220 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
225 /* show existing frame */
226 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
229 if (!gst_bit_reader_skip (&reader, 3))
235 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
237 self->is_keyframe = keyframe = (tmp == 0);
239 /* show frame and resilient mode */
240 if (!gst_bit_reader_skip (&reader, 2))
245 const guint32 sync_code = 0x498342;
246 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 24))
248 if (tmp != sync_code)
251 if (profile >= VP9_PROFILE_2) {
253 if (!gst_bit_reader_skip (&reader, 1))
258 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 3))
260 if (tmp != VPX_CS_SRGB) {
262 if (!gst_bit_reader_skip (&reader, 1))
264 if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3) {
265 /* subsampling + reserved bit */
266 if (!gst_bit_reader_skip (&reader, 2 + 1))
270 if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3)
272 if (!gst_bit_reader_skip (&reader, 1))
277 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
279 self->width = tmp + 1;
280 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
282 self->height = tmp + 1;
285 if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
288 if (!gst_bit_reader_skip (&reader, 32))
292 GST_INFO_OBJECT (self, "parsed width=%d height=%d", self->width,
297 gst_buffer_unmap (buffer, &map);
301 GST_DEBUG ("Failed to parse frame");
302 if (map.memory != NULL) {
303 gst_buffer_unmap (buffer, &map);
309 gst_rtp_vp9_calc_header_len (GstRtpVP9Pay * self, gboolean start)
313 switch (self->picture_id_mode) {
314 case VP9_PAY_PICTURE_ID_7BITS:
317 case VP9_PAY_PICTURE_ID_15BITS:
323 /* Assume non-flexible mode */
324 /* Assume L-bit not set, no L header */
326 if (self->is_keyframe && start) {
327 /* Assume V-bit set */
328 /* FIXME: SS depends on layers and prediction structure */
329 /* For now assume 1 spatial and 1 temporal layer. */
330 /* FIXME: Only for the first packet in the key frame */
337 /* VP9 RTP header, non-flexible mode:
341 |I|P|L|F|B|E|V|-| (REQUIRED)
343 I: |M| PICTURE ID | (RECOMMENDED)
345 M: | EXTENDED PID | (RECOMMENDED)
347 L: | T |U| S |D| (CONDITIONALLY RECOMMENDED)
349 P,F: | P_DIFF |X|N| (CONDITIONALLY RECOMMENDED) .
350 +-+-+-+-+-+-+-+-+ . - up to 3 times
351 X: |EXTENDED P_DIFF| (OPTIONAL) .
357 Scalability structure (SS)
358 (from https://chromium.googlesource.com/external/webrtc/+/HEAD/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc
359 since latest draft is not up to date with chromium)
364 Y: | WIDTH | (OPTIONAL) .
367 +-+-+-+-+-+-+-+-+ . N_S + 1 times
368 | HEIGHT | (OPTIONAL) .
372 G: | N_G | (OPTIONAL)
374 N_G: | T |U| R |-|-| (OPTIONAL) .
375 +-+-+-+-+-+-+-+-+ -| . N_G times
376 | P_DIFF | (OPTIONAL) . R times .
377 +-+-+-+-+-+-+-+-+ -| -|
381 /* When growing the vp9 header keep max payload len calculation in sync */
383 gst_rtp_vp9_create_header_buffer (GstRtpVP9Pay * self,
384 gboolean start, gboolean mark, GstBuffer * in)
388 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
390 guint hdrlen = gst_rtp_vp9_calc_header_len (self, start);
393 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD (self),
395 gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
396 p = gst_rtp_buffer_get_payload (&rtpbuffer);
399 if (self->picture_id_mode != VP9_PAY_NO_PICTURE_ID) {
401 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS) {
403 p[off++] = self->picture_id & 0x7F;
406 p[off++] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
407 p[off++] = self->picture_id & 0xFF;
411 if (!self->is_keyframe)
418 if (self->is_keyframe && start) {
420 /* scalability structure, hard coded for now to be similar to chromium for
421 * quick and dirty interop */
422 p[off++] = 0x18; /* N_S=0 Y=1 G=1 */
423 p[off++] = self->width >> 8;
424 p[off++] = self->width & 0xFF;
425 p[off++] = self->height >> 8;
426 p[off++] = self->height & 0xFF;
427 p[off++] = 0x01; /* N_G=1 */
428 p[off++] = 0x04; /* T=0, U=0, R=1 */
429 p[off++] = 0x01; /* P_DIFF=1 */
432 g_assert_cmpint (off, ==, hdrlen);
434 gst_rtp_buffer_set_marker (&rtpbuffer, mark);
436 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_MARKER);
438 gst_rtp_buffer_unmap (&rtpbuffer);
440 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
441 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
447 gst_rtp_vp9_payload_next (GstRtpVP9Pay * self, GstBufferList * list,
448 guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len)
457 remaining = buffer_size - offset;
458 available = max_payload_len;
459 if (available > remaining)
460 available = remaining;
462 mark = (remaining == available);
463 header = gst_rtp_vp9_create_header_buffer (self, offset == 0, mark, buffer);
464 sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
466 gst_rtp_copy_video_meta (self, header, buffer);
468 out = gst_buffer_append (header, sub);
470 gst_buffer_list_insert (list, -1, out);
477 gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
479 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
482 gsize size, max_paylen;
483 guint offset, mtu, vp9_hdr_len;
485 size = gst_buffer_get_size (buffer);
487 if (G_UNLIKELY (!gst_rtp_vp9_pay_parse_frame (self, buffer, size))) {
488 GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
489 ("Failed to parse VP9 frame"));
490 return GST_FLOW_ERROR;
493 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
494 vp9_hdr_len = gst_rtp_vp9_calc_header_len (self, TRUE);
495 max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp9_hdr_len, 0, 0);
497 list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
500 while (offset < size) {
502 gst_rtp_vp9_payload_next (self, list, offset, buffer, size, max_paylen);
505 ret = gst_rtp_base_payload_push_list (payload, list);
507 /* Incremenent and wrap the picture id if it overflows */
508 if ((self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS &&
509 ++self->picture_id >= 0x80) ||
510 (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS &&
511 ++self->picture_id >= 0x8000))
512 self->picture_id = 0;
514 gst_buffer_unref (buffer);
520 gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
522 GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
524 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
525 if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
526 self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
527 else if (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
528 self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
531 return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_parent_class)->sink_event
536 gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
539 const char *encoding_name = "VP9";
541 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
546 s = gst_caps_get_structure (src_caps, 0);
548 if (gst_structure_has_field (s, "encoding-name")) {
549 GValue default_value = G_VALUE_INIT;
551 g_value_init (&default_value, G_TYPE_STRING);
552 g_value_set_static_string (&default_value, encoding_name);
554 value = gst_structure_get_value (s, "encoding-name");
555 if (!gst_value_can_intersect (&default_value, value))
556 encoding_name = "VP9-DRAFT-IETF-01";
558 gst_caps_unref (src_caps);
561 gst_rtp_base_payload_set_options (payload, "video", TRUE,
562 encoding_name, 90000);
564 return gst_rtp_base_payload_set_outcaps (payload, NULL);