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