2 * gst-rtp-vp8-pay.c - Source for GstRtpVP8Pay
3 * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <gst/base/gstbitreader.h>
26 #include <gst/rtp/gstrtppayloads.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpvp8pay.h"
30 #define FI_FRAG_UNFRAGMENTED 0x0
31 #define FI_FRAG_START 0x1
32 #define FI_FRAG_MIDDLE 0x2
33 #define FI_FRAG_END 0x3
35 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_pay_debug);
36 #define GST_CAT_DEFAULT gst_rtp_vp8_pay_debug
38 GST_BOILERPLATE (GstRtpVP8Pay, gst_rtp_vp8_pay, GstBaseRTPPayload,
39 GST_TYPE_BASE_RTP_PAYLOAD);
41 static GstStaticPadTemplate gst_rtp_vp8_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
45 GST_STATIC_CAPS ("application/x-rtp, "
46 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
47 "clock-rate = (int) 90000, encoding-name = (string) \"VP8-DRAFT-0-3-2\""));
49 static GstStaticPadTemplate gst_rtp_vp8_pay_sink_template =
50 GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_STATIC_CAPS ("video/x-vp8"));
56 gst_rtp_vp8_pay_init (GstRtpVP8Pay * obj, GstRtpVP8PayClass * klass)
60 static GstFlowReturn gst_rtp_vp8_pay_handle_buffer (GstBaseRTPPayload * payload,
62 static gboolean gst_rtp_vp8_pay_set_caps (GstBaseRTPPayload * payload,
66 gst_rtp_vp8_pay_base_init (gpointer klass)
68 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
70 gst_element_class_add_pad_template (element_class,
71 gst_static_pad_template_get (&gst_rtp_vp8_pay_sink_template));
72 gst_element_class_add_pad_template (element_class,
73 gst_static_pad_template_get (&gst_rtp_vp8_pay_src_template));
75 gst_element_class_set_details_simple (element_class, "RTP VP8 payloader",
76 "Codec/Payloader/Network/RTP",
77 "Puts VP8 video in RTP packets)", "Sjoerd Simons <sjoerd@luon.net>");
81 gst_rtp_vp8_pay_class_init (GstRtpVP8PayClass * gst_rtp_vp8_pay_class)
83 GstBaseRTPPayloadClass *pay_class =
84 GST_BASE_RTP_PAYLOAD_CLASS (gst_rtp_vp8_pay_class);
86 pay_class->handle_buffer = gst_rtp_vp8_pay_handle_buffer;
87 pay_class->set_caps = gst_rtp_vp8_pay_set_caps;
89 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_pay_debug, "rtpvp8pay", 0,
90 "VP8 Video RTP Payloader");
94 gst_rtp_vp8_calc_payload_len (GstBaseRTPPayload * payload)
96 return gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU (payload) -
100 /* When growing the vp8 header keep gst_rtp_vp8_calc_payload_len in sync */
102 gst_rtp_vp8_create_header_buffer (gboolean start, gboolean mark, guint fi,
108 out = gst_rtp_buffer_new_allocate (1, 0, 0);
109 p = gst_rtp_buffer_get_payload (out);
110 /* Hardcode I = 0 and N = 0, only set FI and B */
111 p[0] = (fi & 0x3) << 1 | (start ? 1 : 0);
113 gst_rtp_buffer_set_marker (out, mark);
115 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
116 GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (in);
123 gst_rtp_vp8_pay_parse_frame (GstRtpVP8Pay * self, GstBuffer * buffer)
125 GstBitReader *reader;
134 reader = gst_bit_reader_new_from_buffer (buffer);
136 if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 3))
139 data = GST_BUFFER_DATA (buffer);
141 self->is_keyframe = keyframe = ((data[0] & 0x1) == 0);
142 version = (data[0] >> 1) & 0x7;
144 if (G_UNLIKELY (version > 3)) {
145 GST_ERROR_OBJECT (self, "Unknown VP8 version %u", version);
149 /* keyframe, version and show_frame use 5 bits */
150 header_size = data[2] << 11 | data[1] << 3 | (data[0] >> 5);
152 /* Include the uncompressed data blob in the header */
153 header_size += keyframe ? 10 : 3;
155 if (!gst_bit_reader_skip (reader, 24))
159 /* check start tag: 0x9d 0x01 0x2a */
160 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x9d)
163 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x01)
166 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x2a)
169 /* Skip horizontal size code (16 bits) vertical size code (16 bits),
170 * color space (1 bit) and clamping type (1 bit) */
171 if (!gst_bit_reader_skip (reader, 34))
175 /* segmentation_enabled */
176 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
180 gboolean update_mb_segmentation_map;
181 gboolean update_segment_feature_data;
183 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 2))
186 update_mb_segmentation_map = (tmp8 & 0x2) != 0;
187 update_segment_feature_data = (tmp8 & 0x1) != 0;
189 if (update_segment_feature_data) {
190 /* skip segment feature mode */
191 if (!gst_bit_reader_skip (reader, 1))
194 for (i = 0; i < 4; i++) {
195 /* quantizer update */
196 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
200 /* skip quantizer value (7 bits) and sign (1 bit) */
201 if (!gst_bit_reader_skip (reader, 8))
206 for (i = 0; i < 4; i++) {
207 /* loop filter update */
208 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
212 /* skip lf update value (6 bits) and sign (1 bit) */
213 if (!gst_bit_reader_skip (reader, 7))
219 if (update_mb_segmentation_map) {
220 for (i = 0; i < 3; i++) {
221 /* segment prob update */
222 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
226 /* skip segment prob */
227 if (!gst_bit_reader_skip (reader, 8))
234 /* skip filter type (1 bit), loop filter level (6 bits) and
235 * sharpness level (3 bits) */
236 if (!gst_bit_reader_skip (reader, 10))
239 /* loop_filter_adj_enabled */
240 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
244 /* loop filter adj enabled */
246 /* mode_ref_lf_delta_update */
247 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
251 /* mode_ref_lf_data_update */
254 for (i = 0; i < 8; i++) {
255 /* 8 updates, 1 bit indicate whether there is one and if follow by a
257 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
261 /* skip delta magnitude (6 bits) and sign (1 bit) */
262 if (!gst_bit_reader_skip (reader, 7))
269 if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 2))
272 partitions = 1 << tmp8;
274 /* Check if things are still sensible */
275 if (header_size + (partitions - 1) * 3 >= GST_BUFFER_SIZE (buffer))
278 /* partition data is right after the frame header */
279 data = GST_BUFFER_DATA (buffer) + header_size;
281 /* Set up mapping, count the initial header as a partition to make other
282 * sections of the code easier */
283 self->n_partitions = partitions + 1;
284 self->partition_offset[0] = 0;
285 self->partition_size[0] = header_size + (partitions - 1) * 3;
287 self->partition_offset[1] = self->partition_size[0];
288 for (i = 1; i < partitions; i++) {
289 guint size = (data[2] << 16 | data[1] << 8 | data[0]);
292 self->partition_size[i] = size;
293 self->partition_offset[i + 1] = self->partition_offset[i] + size;
296 /* Check that our partition offsets and sizes don't go outsize the buffer
298 if (self->partition_offset[i] >= GST_BUFFER_SIZE (buffer))
301 self->partition_size[i] = GST_BUFFER_SIZE (buffer)
302 - self->partition_offset[i];
304 self->partition_offset[i + 1] = GST_BUFFER_SIZE (buffer);
306 gst_bit_reader_free (reader);
310 GST_DEBUG ("Failed to parse frame");
311 gst_bit_reader_free (reader);
316 gst_rtp_vp8_fit_partitions (GstRtpVP8Pay * self, gint first, gsize available)
321 g_assert (first < self->n_partitions);
324 i < self->n_partitions && self->partition_size[i] < available; i++) {
326 available -= self->partition_size[i];
333 gst_rtp_vp8_create_sub (GstRtpVP8Pay * self,
334 GstBuffer * buffer, guint current, guint num)
336 guint offset = self->partition_offset[current];
337 guint size = self->partition_offset[current + num] - offset;
339 return gst_buffer_create_sub (buffer, offset, size);
344 gst_rtp_vp8_payload_next (GstRtpVP8Pay * self,
345 GstBufferListIterator * it, guint first, GstBuffer * buffer)
351 gsize available = gst_rtp_vp8_calc_payload_len (GST_BASE_RTP_PAYLOAD (self));
353 g_assert (first < 9);
355 /* How many partitions can we fit */
356 num = gst_rtp_vp8_fit_partitions (self, first, available);
359 mark = (first + num == self->n_partitions);
360 /* whole set of partitions, payload them and done */
361 header = gst_rtp_vp8_create_header_buffer (first == 0, mark,
362 FI_FRAG_UNFRAGMENTED, buffer);
363 sub = gst_rtp_vp8_create_sub (self, buffer, first, num);
365 gst_buffer_list_iterator_add_group (it);
366 gst_buffer_list_iterator_add (it, header);
367 gst_buffer_list_iterator_add (it, sub);
369 /* Fragmented packets */
370 guint offset = self->partition_offset[first];
371 guint left = self->partition_size[first];
372 gboolean start = (first == 0);
374 header = gst_rtp_vp8_create_header_buffer (start, FALSE,
375 FI_FRAG_START, buffer);
376 sub = gst_buffer_create_sub (buffer, offset, available);
379 gst_buffer_list_iterator_add_group (it);
380 gst_buffer_list_iterator_add (it, header);
381 gst_buffer_list_iterator_add (it, sub);
385 for (; left > available; left -= available) {
386 header = gst_rtp_vp8_create_header_buffer (start, FALSE,
387 FI_FRAG_MIDDLE, buffer);
389 sub = gst_buffer_create_sub (buffer, offset, available);
392 gst_buffer_list_iterator_add_group (it);
393 gst_buffer_list_iterator_add (it, header);
394 gst_buffer_list_iterator_add (it, sub);
397 mark = (first + 1 == self->n_partitions);
399 header = gst_rtp_vp8_create_header_buffer (start, mark,
400 FI_FRAG_END, buffer);
401 sub = gst_buffer_create_sub (buffer, offset, left);
403 gst_buffer_list_iterator_add_group (it);
404 gst_buffer_list_iterator_add (it, header);
405 gst_buffer_list_iterator_add (it, sub);
415 gst_rtp_vp8_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
417 GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
420 GstBufferListIterator *it;
423 if (G_UNLIKELY (!gst_rtp_vp8_pay_parse_frame (self, buffer))) {
424 /* FIXME throw flow error */
425 g_message ("Failed to parse frame");
426 return GST_FLOW_ERROR;
429 list = gst_buffer_list_new ();
430 it = gst_buffer_list_iterate (list);
432 for (current = 0; current < self->n_partitions;) {
435 n = gst_rtp_vp8_payload_next (self, it, current, buffer);
439 ret = gst_basertppayload_push_list (payload, list);
440 gst_buffer_list_iterator_free (it);
446 gst_rtp_vp8_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
448 gst_basertppayload_set_options (payload, "video", TRUE,
449 "VP8-DRAFT-0-3-2", 90000);
450 return gst_basertppayload_set_outcaps (payload, NULL);
454 gst_rtp_vp8_pay_plugin_init (GstPlugin * plugin)
456 return gst_element_register (plugin, "rtpvp8pay",
457 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_PAY);