1 /*
2 * gstrtpvp8pay.c - Source for GstRtpVP8Pay
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 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gst/base/gstbitreader.h>
31 #include <gst/rtp/gstrtppayloads.h>
32 #include <gst/rtp/gstrtpbuffer.h>
33 #include <gst/video/video.h>
34 #include "gstrtpelements.h"
35 #include "dboolhuff.h"
36 #include "gstrtpvp8pay.h"
37 #include "gstrtputils.h"
38
39 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_pay_debug);
40 #define GST_CAT_DEFAULT gst_rtp_vp8_pay_debug
41
42 #define DEFAULT_PICTURE_ID_MODE VP8_PAY_NO_PICTURE_ID
43 #define DEFAULT_PICTURE_ID_OFFSET (-1)
44
45 enum
46 {
47 PROP_0,
48 PROP_PICTURE_ID_MODE,
49 PROP_PICTURE_ID_OFFSET
50 };
51
52 #define GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE (gst_rtp_vp8_pay_picture_id_mode_get_type())
53 static GType
gst_rtp_vp8_pay_picture_id_mode_get_type(void)54 gst_rtp_vp8_pay_picture_id_mode_get_type (void)
55 {
56 static GType mode_type = 0;
57 static const GEnumValue modes[] = {
58 {VP8_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
59 {VP8_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
60 {VP8_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
61 {0, NULL, NULL},
62 };
63
64 if (!mode_type) {
65 mode_type = g_enum_register_static ("GstVP8RTPPayMode", modes);
66 }
67 return mode_type;
68 }
69
70 static void gst_rtp_vp8_pay_get_property (GObject * object, guint prop_id,
71 GValue * value, GParamSpec * pspec);
72 static void gst_rtp_vp8_pay_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74
75 static GstFlowReturn gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload,
76 GstBuffer * buffer);
77 static gboolean gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload,
78 GstEvent * event);
79 static gboolean gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload,
80 GstCaps * caps);
81
82 G_DEFINE_TYPE (GstRtpVP8Pay, gst_rtp_vp8_pay, GST_TYPE_RTP_BASE_PAYLOAD);
83 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpvp8pay, "rtpvp8pay",
84 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_PAY, rtp_element_init (plugin));
85
86 static GstStaticPadTemplate gst_rtp_vp8_pay_src_template =
87 GST_STATIC_PAD_TEMPLATE ("src",
88 GST_PAD_SRC,
89 GST_PAD_ALWAYS,
90 GST_STATIC_CAPS ("application/x-rtp, "
91 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
92 "clock-rate = (int) 90000, encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
93
94 static GstStaticPadTemplate gst_rtp_vp8_pay_sink_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
96 GST_PAD_SINK,
97 GST_PAD_ALWAYS,
98 GST_STATIC_CAPS ("video/x-vp8"));
99
100 static gint
picture_id_field_len(PictureIDMode mode)101 picture_id_field_len (PictureIDMode mode)
102 {
103 if (VP8_PAY_NO_PICTURE_ID == mode)
104 return 0;
105 if (VP8_PAY_PICTURE_ID_7BITS == mode)
106 return 7;
107 return 15;
108 }
109
110 static void
gst_rtp_vp8_pay_picture_id_reset(GstRtpVP8Pay * obj)111 gst_rtp_vp8_pay_picture_id_reset (GstRtpVP8Pay * obj)
112 {
113 gint nbits;
114
115 if (obj->picture_id_offset == -1)
116 obj->picture_id = g_random_int ();
117 else
118 obj->picture_id = obj->picture_id_offset;
119
120 nbits = picture_id_field_len (obj->picture_id_mode);
121 obj->picture_id &= (1 << nbits) - 1;
122 }
123
124 static void
gst_rtp_vp8_pay_picture_id_increment(GstRtpVP8Pay * obj)125 gst_rtp_vp8_pay_picture_id_increment (GstRtpVP8Pay * obj)
126 {
127 gint nbits;
128
129 if (obj->picture_id_mode == VP8_PAY_NO_PICTURE_ID)
130 return;
131
132 nbits = picture_id_field_len (obj->picture_id_mode);
133 obj->picture_id++;
134 obj->picture_id &= (1 << nbits) - 1;
135 }
136
137 static void
gst_rtp_vp8_pay_reset(GstRtpVP8Pay * obj)138 gst_rtp_vp8_pay_reset (GstRtpVP8Pay * obj)
139 {
140 gst_rtp_vp8_pay_picture_id_reset (obj);
141 /* tl0picidx MAY start at a random value, but there's no point. Initialize
142 * so that first packet will use 0 for convenience */
143 obj->tl0picidx = -1;
144 obj->temporal_scalability_fields_present = FALSE;
145 }
146
147 static void
gst_rtp_vp8_pay_init(GstRtpVP8Pay * obj)148 gst_rtp_vp8_pay_init (GstRtpVP8Pay * obj)
149 {
150 obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
151 obj->picture_id_offset = DEFAULT_PICTURE_ID_OFFSET;
152 gst_rtp_vp8_pay_reset (obj);
153 }
154
155 static void
gst_rtp_vp8_pay_class_init(GstRtpVP8PayClass * gst_rtp_vp8_pay_class)156 gst_rtp_vp8_pay_class_init (GstRtpVP8PayClass * gst_rtp_vp8_pay_class)
157 {
158 GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp8_pay_class);
159 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_pay_class);
160 GstRTPBasePayloadClass *pay_class =
161 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_class);
162
163 gobject_class->set_property = gst_rtp_vp8_pay_set_property;
164 gobject_class->get_property = gst_rtp_vp8_pay_get_property;
165
166 g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
167 g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
168 "The picture ID mode for payloading",
169 GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 /**
172 * rtpvp8pay:picture-id-offset:
173 *
174 * Offset to add to the initial picture-id (-1 = random)
175 *
176 * Since: 1.20
177 */
178 g_object_class_install_property (gobject_class, PROP_PICTURE_ID_OFFSET,
179 g_param_spec_int ("picture-id-offset", "Picture ID offset",
180 "Offset to add to the initial picture-id (-1 = random)",
181 -1, 0x7FFF, DEFAULT_PICTURE_ID_OFFSET,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183
184 gst_element_class_add_static_pad_template (element_class,
185 &gst_rtp_vp8_pay_sink_template);
186 gst_element_class_add_static_pad_template (element_class,
187 &gst_rtp_vp8_pay_src_template);
188
189 gst_element_class_set_static_metadata (element_class, "RTP VP8 payloader",
190 "Codec/Payloader/Network/RTP",
191 "Puts VP8 video in RTP packets", "Sjoerd Simons <sjoerd@luon.net>");
192
193 pay_class->handle_buffer = gst_rtp_vp8_pay_handle_buffer;
194 pay_class->sink_event = gst_rtp_vp8_pay_sink_event;
195 pay_class->set_caps = gst_rtp_vp8_pay_set_caps;
196
197 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_pay_debug, "rtpvp8pay", 0,
198 "VP8 Video RTP Payloader");
199
200 gst_type_mark_as_plugin_api (GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE, 0);
201 }
202
203 static void
gst_rtp_vp8_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)204 gst_rtp_vp8_pay_set_property (GObject * object,
205 guint prop_id, const GValue * value, GParamSpec * pspec)
206 {
207 GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
208
209 switch (prop_id) {
210 case PROP_PICTURE_ID_MODE:
211 rtpvp8pay->picture_id_mode = g_value_get_enum (value);
212 gst_rtp_vp8_pay_picture_id_reset (rtpvp8pay);
213 break;
214 case PROP_PICTURE_ID_OFFSET:
215 rtpvp8pay->picture_id_offset = g_value_get_int (value);
216 gst_rtp_vp8_pay_picture_id_reset (rtpvp8pay);
217 break;
218 default:
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 break;
221 }
222 }
223
224 static void
gst_rtp_vp8_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)225 gst_rtp_vp8_pay_get_property (GObject * object,
226 guint prop_id, GValue * value, GParamSpec * pspec)
227 {
228 GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
229
230 switch (prop_id) {
231 case PROP_PICTURE_ID_MODE:
232 g_value_set_enum (value, rtpvp8pay->picture_id_mode);
233 break;
234 case PROP_PICTURE_ID_OFFSET:
235 g_value_set_int (value, rtpvp8pay->picture_id_offset);
236 break;
237 default:
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 break;
240 }
241 }
242
243 static gboolean
gst_rtp_vp8_pay_parse_frame(GstRtpVP8Pay * self,GstBuffer * buffer,gsize buffer_size)244 gst_rtp_vp8_pay_parse_frame (GstRtpVP8Pay * self, GstBuffer * buffer,
245 gsize buffer_size)
246 {
247 GstMapInfo map = GST_MAP_INFO_INIT;
248 GstBitReader reader;
249 guint8 *data;
250 gsize size;
251 int i;
252 gboolean keyframe;
253 guint32 partition0_size;
254 guint8 version;
255 guint8 tmp8 = 0;
256 guint8 partitions;
257 guint offset;
258 BOOL_DECODER bc;
259 guint8 *pdata;
260
261 if (G_UNLIKELY (buffer_size < 3))
262 goto error;
263
264 if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
265 goto error;
266
267 data = map.data;
268 size = map.size;
269
270 gst_bit_reader_init (&reader, data, size);
271
272 self->is_keyframe = keyframe = ((data[0] & 0x1) == 0);
273 version = (data[0] >> 1) & 0x7;
274
275 if (G_UNLIKELY (version > 3)) {
276 GST_ERROR_OBJECT (self, "Unknown VP8 version %u", version);
277 goto error;
278 }
279
280 /* keyframe, version and show_frame use 5 bits */
281 partition0_size = data[2] << 11 | data[1] << 3 | (data[0] >> 5);
282
283 /* Include the uncompressed data blob in the first partition */
284 offset = keyframe ? 10 : 3;
285 partition0_size += offset;
286
287 if (!gst_bit_reader_skip (&reader, 24))
288 goto error;
289
290 if (keyframe) {
291 /* check start tag: 0x9d 0x01 0x2a */
292 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x9d)
293 goto error;
294
295 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x01)
296 goto error;
297
298 if (!gst_bit_reader_get_bits_uint8 (&reader, &tmp8, 8) || tmp8 != 0x2a)
299 goto error;
300
301 /* Skip horizontal size code (16 bits) vertical size code (16 bits) */
302 if (!gst_bit_reader_skip (&reader, 32))
303 goto error;
304 }
305
306 offset = keyframe ? 10 : 3;
307 vp8dx_start_decode (&bc, data + offset, size - offset);
308
309 if (keyframe) {
310 /* color space (1 bit) and clamping type (1 bit) */
311 vp8dx_decode_bool (&bc, 0x80);
312 vp8dx_decode_bool (&bc, 0x80);
313 }
314
315 /* segmentation_enabled */
316 if (vp8dx_decode_bool (&bc, 0x80)) {
317 guint8 update_mb_segmentation_map = vp8dx_decode_bool (&bc, 0x80);
318 guint8 update_segment_feature_data = vp8dx_decode_bool (&bc, 0x80);
319
320 if (update_segment_feature_data) {
321 /* skip segment feature mode */
322 vp8dx_decode_bool (&bc, 0x80);
323
324 /* quantizer update */
325 for (i = 0; i < 4; i++) {
326 /* skip flagged quantizer value (7 bits) and sign (1 bit) */
327 if (vp8dx_decode_bool (&bc, 0x80))
328 vp8_decode_value (&bc, 8);
329 }
330
331 /* loop filter update */
332 for (i = 0; i < 4; i++) {
333 /* skip flagged lf update value (6 bits) and sign (1 bit) */
334 if (vp8dx_decode_bool (&bc, 0x80))
335 vp8_decode_value (&bc, 7);
336 }
337 }
338
339 if (update_mb_segmentation_map) {
340 /* segment prob update */
341 for (i = 0; i < 3; i++) {
342 /* skip flagged segment prob */
343 if (vp8dx_decode_bool (&bc, 0x80))
344 vp8_decode_value (&bc, 8);
345 }
346 }
347 }
348
349 /* skip filter type (1 bit), loop filter level (6 bits) and
350 * sharpness level (3 bits) */
351 vp8_decode_value (&bc, 1);
352 vp8_decode_value (&bc, 6);
353 vp8_decode_value (&bc, 3);
354
355 /* loop_filter_adj_enabled */
356 if (vp8dx_decode_bool (&bc, 0x80)) {
357
358 /* delta update */
359 if (vp8dx_decode_bool (&bc, 0x80)) {
360
361 for (i = 0; i < 8; i++) {
362 /* 8 updates, 1 bit indicate whether there is one and if follow by a
363 * 7 bit update */
364 if (vp8dx_decode_bool (&bc, 0x80))
365 vp8_decode_value (&bc, 7);
366 }
367 }
368 }
369
370 if (vp8dx_bool_error (&bc))
371 goto error;
372
373 tmp8 = vp8_decode_value (&bc, 2);
374
375 partitions = 1 << tmp8;
376
377 /* Check if things are still sensible */
378 if (partition0_size + (partitions - 1) * 3 >= size)
379 goto error;
380
381 /* partition data is right after the mode partition */
382 pdata = data + partition0_size;
383
384 /* Set up mapping */
385 self->n_partitions = partitions + 1;
386 self->partition_offset[0] = 0;
387 self->partition_size[0] = partition0_size + (partitions - 1) * 3;
388
389 self->partition_offset[1] = self->partition_size[0];
390 for (i = 1; i < partitions; i++) {
391 guint psize = (pdata[2] << 16 | pdata[1] << 8 | pdata[0]);
392
393 pdata += 3;
394 self->partition_size[i] = psize;
395 self->partition_offset[i + 1] = self->partition_offset[i] + psize;
396 }
397
398 /* Check that our partition offsets and sizes don't go outsize the buffer
399 * size. */
400 if (self->partition_offset[i] >= size)
401 goto error;
402
403 self->partition_size[i] = size - self->partition_offset[i];
404
405 self->partition_offset[i + 1] = size;
406
407 gst_buffer_unmap (buffer, &map);
408
409 if (keyframe)
410 GST_DEBUG_OBJECT (self, "Parsed keyframe");
411
412 return TRUE;
413
414 error:
415 GST_DEBUG ("Failed to parse frame");
416 if (map.memory != NULL) {
417 gst_buffer_unmap (buffer, &map);
418 }
419 return FALSE;
420 }
421
422 static guint
gst_rtp_vp8_offset_to_partition(GstRtpVP8Pay * self,guint offset)423 gst_rtp_vp8_offset_to_partition (GstRtpVP8Pay * self, guint offset)
424 {
425 int i;
426
427 for (i = 1; i < self->n_partitions; i++) {
428 if (offset < self->partition_offset[i])
429 return i - 1;
430 }
431
432 return i - 1;
433 }
434
435 static gsize
gst_rtp_vp8_calc_header_len(GstRtpVP8Pay * self)436 gst_rtp_vp8_calc_header_len (GstRtpVP8Pay * self)
437 {
438 gsize len;
439
440 switch (self->picture_id_mode) {
441 case VP8_PAY_PICTURE_ID_7BITS:
442 len = 1;
443 break;
444 case VP8_PAY_PICTURE_ID_15BITS:
445 len = 2;
446 break;
447 case VP8_PAY_NO_PICTURE_ID:
448 default:
449 len = 0;
450 break;
451 }
452
453 if (self->temporal_scalability_fields_present) {
454 /* Add on space for TL0PICIDX and TID/Y/KEYIDX */
455 len += 2;
456 }
457
458 if (len > 0) {
459 /* All fields above are extension, so allocate space for the ECB field */
460 len++;
461 }
462
463 return len + 1; /* computed + fixed size header */
464 }
465
466 /* When growing the vp8 header keep max payload len calculation in sync */
467 static GstBuffer *
gst_rtp_vp8_create_header_buffer(GstRtpVP8Pay * self,guint8 partid,gboolean start,gboolean mark,GstBuffer * in,GstCustomMeta * meta)468 gst_rtp_vp8_create_header_buffer (GstRtpVP8Pay * self, guint8 partid,
469 gboolean start, gboolean mark, GstBuffer * in, GstCustomMeta * meta)
470 {
471 GstBuffer *out;
472 guint8 *p;
473 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
474
475 out =
476 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD_CAST
477 (self), gst_rtp_vp8_calc_header_len (self), 0, 0);
478 gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
479 p = gst_rtp_buffer_get_payload (&rtpbuffer);
480
481 /* X=0,R=0,N=0,S=start,PartID=partid */
482 p[0] = (start << 4) | partid;
483 if (GST_BUFFER_FLAG_IS_SET (in, GST_BUFFER_FLAG_DROPPABLE)) {
484 /* Enable N=1 */
485 p[0] |= 0x20;
486 }
487
488 if (self->picture_id_mode != VP8_PAY_NO_PICTURE_ID ||
489 self->temporal_scalability_fields_present) {
490 gint index;
491
492 /* Enable X=1 */
493 p[0] |= 0x80;
494
495 /* X: I=0,L=0,T=0,K=0,RSV=0 */
496 p[1] = 0x00;
497 if (self->picture_id_mode != VP8_PAY_NO_PICTURE_ID) {
498 /* Set I bit */
499 p[1] |= 0x80;
500 }
501 if (self->temporal_scalability_fields_present) {
502 /* Set L and T bits */
503 p[1] |= 0x60;
504 }
505
506 /* Insert picture ID */
507 if (self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS) {
508 /* I: 7 bit picture_id */
509 p[2] = self->picture_id & 0x7F;
510 index = 3;
511 } else if (self->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS) {
512 /* I: 15 bit picture_id */
513 p[2] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
514 p[3] = self->picture_id & 0xFF;
515 index = 4;
516 } else {
517 index = 2;
518 }
519
520 /* Insert TL0PICIDX and TID/Y/KEYIDX */
521 if (self->temporal_scalability_fields_present) {
522 /* The meta contains tl0picidx from the encoder, but we need to ensure
523 * that tl0picidx is increasing correctly. The encoder may reset it's
524 * state and counter, but we cannot. Therefore, we cannot simply copy
525 * the value into the header.*/
526 guint temporal_layer = 0;
527 gboolean layer_sync = FALSE;
528 gboolean use_temporal_scaling = FALSE;
529
530 if (meta) {
531 GstStructure *s = gst_custom_meta_get_structure (meta);
532 gst_structure_get_boolean (s, "use-temporal-scaling",
533 &use_temporal_scaling);
534
535 if (use_temporal_scaling)
536 gst_structure_get (s, "layer-id", G_TYPE_UINT, &temporal_layer,
537 "layer-sync", G_TYPE_BOOLEAN, &layer_sync, NULL);
538 }
539
540 /* FIXME: Support a prediction structure where higher layers don't
541 * necessarily refer to the last base layer frame, ie they use an older
542 * tl0picidx as signalled in the meta */
543 if (temporal_layer == 0 && start)
544 self->tl0picidx++;
545 p[index] = self->tl0picidx & 0xFF;
546 p[index + 1] = ((temporal_layer << 6) | (layer_sync << 5)) & 0xFF;
547 }
548 }
549
550 gst_rtp_buffer_set_marker (&rtpbuffer, mark);
551
552 gst_rtp_buffer_unmap (&rtpbuffer);
553
554 GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
555 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
556
557 return out;
558 }
559
560 static gboolean
foreach_metadata_drop(GstBuffer * buf,GstMeta ** meta,gpointer user_data)561 foreach_metadata_drop (GstBuffer * buf, GstMeta ** meta, gpointer user_data)
562 {
563 GstElement *element = user_data;
564 const GstMetaInfo *info = (*meta)->info;
565
566 if (gst_meta_info_is_custom (info) &&
567 gst_custom_meta_has_name ((GstCustomMeta *) * meta, "GstVP8Meta")) {
568 GST_DEBUG_OBJECT (element, "dropping GstVP8Meta");
569 *meta = NULL;
570 }
571
572 return TRUE;
573 }
574
575 static void
gst_rtp_vp8_drop_vp8_meta(gpointer element,GstBuffer * buf)576 gst_rtp_vp8_drop_vp8_meta (gpointer element, GstBuffer * buf)
577 {
578 gst_buffer_foreach_meta (buf, foreach_metadata_drop, element);
579 }
580
581 static guint
gst_rtp_vp8_payload_next(GstRtpVP8Pay * self,GstBufferList * list,guint offset,GstBuffer * buffer,gsize buffer_size,gsize max_payload_len,GstCustomMeta * meta)582 gst_rtp_vp8_payload_next (GstRtpVP8Pay * self, GstBufferList * list,
583 guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len,
584 GstCustomMeta * meta)
585 {
586 guint partition;
587 GstBuffer *header;
588 GstBuffer *sub;
589 GstBuffer *out;
590 gboolean mark;
591 gboolean start;
592 gsize remaining;
593 gsize available;
594
595 remaining = buffer_size - offset;
596 available = max_payload_len;
597 if (available > remaining)
598 available = remaining;
599
600 if (meta) {
601 /* If meta is present, then we have no partition offset information,
602 * so always emit PID 0 and set the start bit for the first packet
603 * of a frame only (c.f. RFC7741 $4.4)
604 */
605 partition = 0;
606 start = (offset == 0);
607 } else {
608 partition = gst_rtp_vp8_offset_to_partition (self, offset);
609 g_assert (partition < self->n_partitions);
610 start = (offset == self->partition_offset[partition]);
611 }
612
613 mark = (remaining == available);
614 /* whole set of partitions, payload them and done */
615 header = gst_rtp_vp8_create_header_buffer (self, partition,
616 start, mark, buffer, meta);
617 sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
618
619 gst_rtp_copy_video_meta (self, header, buffer);
620 gst_rtp_vp8_drop_vp8_meta (self, header);
621
622 out = gst_buffer_append (header, sub);
623
624 gst_buffer_list_insert (list, -1, out);
625
626 return available;
627 }
628
629
630 static GstFlowReturn
gst_rtp_vp8_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buffer)631 gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
632 {
633 GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
634 GstFlowReturn ret;
635 GstBufferList *list;
636 GstCustomMeta *meta;
637 gsize size, max_paylen;
638 guint offset, mtu, vp8_hdr_len;
639
640 size = gst_buffer_get_size (buffer);
641 meta = gst_buffer_get_custom_meta (buffer, "GstVP8Meta");
642 if (G_UNLIKELY (!gst_rtp_vp8_pay_parse_frame (self, buffer, size))) {
643 GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
644 ("Failed to parse VP8 frame"));
645 return GST_FLOW_ERROR;
646 }
647
648 if (meta) {
649 GstStructure *s = gst_custom_meta_get_structure (meta);
650 gboolean use_temporal_scaling;
651 /* For interop it's most likely better to keep the temporal scalability
652 * fields present if the stream previously had them present. Alternating
653 * whether these fields are present or not may confuse the receiver. */
654
655 gst_structure_get_boolean (s, "use-temporal-scaling",
656 &use_temporal_scaling);
657 if (use_temporal_scaling)
658 self->temporal_scalability_fields_present = TRUE;
659 }
660
661 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
662 vp8_hdr_len = gst_rtp_vp8_calc_header_len (self);
663 max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp8_hdr_len, 0,
664 gst_rtp_base_payload_get_source_count (payload, buffer));
665
666 list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
667
668 offset = 0;
669 while (offset < size) {
670 offset +=
671 gst_rtp_vp8_payload_next (self, list, offset, buffer, size,
672 max_paylen, meta);
673 }
674
675 ret = gst_rtp_base_payload_push_list (payload, list);
676
677 gst_rtp_vp8_pay_picture_id_increment (self);
678
679 gst_buffer_unref (buffer);
680
681 return ret;
682 }
683
684 static gboolean
gst_rtp_vp8_pay_sink_event(GstRTPBasePayload * payload,GstEvent * event)685 gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
686 {
687 GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
688
689 if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
690 gst_rtp_vp8_pay_reset (self);
691 }
692
693 return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_parent_class)->sink_event
694 (payload, event);
695 }
696
697 static gboolean
gst_rtp_vp8_pay_set_caps(GstRTPBasePayload * payload,GstCaps * caps)698 gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
699 {
700 GstCaps *src_caps;
701 const char *encoding_name = "VP8";
702
703 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
704 if (src_caps) {
705 GstStructure *s;
706 const GValue *value;
707
708 s = gst_caps_get_structure (src_caps, 0);
709
710 if (gst_structure_has_field (s, "encoding-name")) {
711 GValue default_value = G_VALUE_INIT;
712
713 g_value_init (&default_value, G_TYPE_STRING);
714 g_value_set_static_string (&default_value, encoding_name);
715
716 value = gst_structure_get_value (s, "encoding-name");
717 if (!gst_value_can_intersect (&default_value, value))
718 encoding_name = "VP8-DRAFT-IETF-01";
719 }
720 gst_caps_unref (src_caps);
721 }
722
723 gst_rtp_base_payload_set_options (payload, "video", TRUE,
724 encoding_name, 90000);
725
726 return gst_rtp_base_payload_set_outcaps (payload, NULL);
727 }
728