rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.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 "dboolhuff.h"
34 #include "gstrtpvp8pay.h"
35
36 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_pay_debug);
37 #define GST_CAT_DEFAULT gst_rtp_vp8_pay_debug
38
39 #define DEFAULT_PICTURE_ID_MODE VP8_PAY_NO_PICTURE_ID
40
41 enum
42 {
43   PROP_0,
44   PROP_PICTURE_ID_MODE
45 };
46
47 #define GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE (gst_rtp_vp8_pay_picture_id_mode_get_type())
48 static GType
49 gst_rtp_vp8_pay_picture_id_mode_get_type (void)
50 {
51   static GType mode_type = 0;
52   static const GEnumValue modes[] = {
53     {VP8_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
54     {VP8_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
55     {VP8_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
56     {0, NULL, NULL},
57   };
58
59   if (!mode_type) {
60     mode_type = g_enum_register_static ("GstVP8RTPPayMode", modes);
61   }
62   return mode_type;
63 }
64
65 static void gst_rtp_vp8_pay_get_property (GObject * object, guint prop_id,
66     GValue * value, GParamSpec * pspec);
67 static void gst_rtp_vp8_pay_set_property (GObject * object, guint prop_id,
68     const GValue * value, GParamSpec * pspec);
69
70 static GstFlowReturn gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload,
71     GstBuffer * buffer);
72 static gboolean gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload,
73     GstEvent * event);
74 static gboolean gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload,
75     GstCaps * caps);
76
77 G_DEFINE_TYPE (GstRtpVP8Pay, gst_rtp_vp8_pay, GST_TYPE_RTP_BASE_PAYLOAD);
78
79 static GstStaticPadTemplate gst_rtp_vp8_pay_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
81     GST_PAD_SRC,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS ("application/x-rtp, "
84         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
85         "clock-rate = (int) 90000, encoding-name = (string) \"VP8-DRAFT-IETF-01\""));
86
87 static GstStaticPadTemplate gst_rtp_vp8_pay_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS ("video/x-vp8"));
92
93 static void
94 gst_rtp_vp8_pay_init (GstRtpVP8Pay * obj)
95 {
96   obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
97   if (obj->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS)
98     obj->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
99   else if (obj->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS)
100     obj->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
101 }
102
103 static void
104 gst_rtp_vp8_pay_class_init (GstRtpVP8PayClass * gst_rtp_vp8_pay_class)
105 {
106   GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp8_pay_class);
107   GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_pay_class);
108   GstRTPBasePayloadClass *pay_class =
109       GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_class);
110
111   gobject_class->set_property = gst_rtp_vp8_pay_set_property;
112   gobject_class->get_property = gst_rtp_vp8_pay_get_property;
113
114   g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
115       g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
116           "The picture ID mode for payloading",
117           GST_TYPE_RTP_VP8_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
118           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119
120   gst_element_class_add_pad_template (element_class,
121       gst_static_pad_template_get (&gst_rtp_vp8_pay_sink_template));
122   gst_element_class_add_pad_template (element_class,
123       gst_static_pad_template_get (&gst_rtp_vp8_pay_src_template));
124
125   gst_element_class_set_static_metadata (element_class, "RTP VP8 payloader",
126       "Codec/Payloader/Network/RTP",
127       "Puts VP8 video in RTP packets)", "Sjoerd Simons <sjoerd@luon.net>");
128
129   pay_class->handle_buffer = gst_rtp_vp8_pay_handle_buffer;
130   pay_class->sink_event = gst_rtp_vp8_pay_sink_event;
131   pay_class->set_caps = gst_rtp_vp8_pay_set_caps;
132
133   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_pay_debug, "rtpvp8pay", 0,
134       "VP8 Video RTP Payloader");
135 }
136
137 static void
138 gst_rtp_vp8_pay_set_property (GObject * object,
139     guint prop_id, const GValue * value, GParamSpec * pspec)
140 {
141   GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
142
143   switch (prop_id) {
144     case PROP_PICTURE_ID_MODE:
145       rtpvp8pay->picture_id_mode = g_value_get_enum (value);
146       if (rtpvp8pay->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS)
147         rtpvp8pay->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
148       else if (rtpvp8pay->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS)
149         rtpvp8pay->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
150       break;
151     default:
152       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153       break;
154   }
155 }
156
157 static void
158 gst_rtp_vp8_pay_get_property (GObject * object,
159     guint prop_id, GValue * value, GParamSpec * pspec)
160 {
161   GstRtpVP8Pay *rtpvp8pay = GST_RTP_VP8_PAY (object);
162
163   switch (prop_id) {
164     case PROP_PICTURE_ID_MODE:
165       g_value_set_enum (value, rtpvp8pay->picture_id_mode);
166       break;
167     default:
168       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169       break;
170   }
171 }
172
173 static gboolean
174 gst_rtp_vp8_pay_parse_frame (GstRtpVP8Pay * self, GstBuffer * buffer)
175 {
176   GstBitReader *reader = NULL;
177   guint8 *data;
178   gsize size;
179   GstMapInfo map;
180   int i;
181   gboolean keyframe;
182   guint32 partition0_size;
183   guint8 version;
184   guint8 tmp8 = 0;
185   guint8 partitions;
186   guint offset;
187   BOOL_DECODER bc;
188   guint8 *pdata;
189
190   if (G_UNLIKELY (gst_buffer_get_size (buffer) < 3))
191     goto error;
192
193   if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
194     goto error;
195
196   data = map.data;
197   size = map.size;
198   reader = gst_bit_reader_new (data, size);
199
200   self->is_keyframe = keyframe = ((data[0] & 0x1) == 0);
201   version = (data[0] >> 1) & 0x7;
202
203   if (G_UNLIKELY (version > 3)) {
204     GST_ERROR_OBJECT (self, "Unknown VP8 version %u", version);
205     goto error;
206   }
207
208   /* keyframe, version and show_frame use 5 bits */
209   partition0_size = data[2] << 11 | data[1] << 3 | (data[0] >> 5);
210
211   /* Include the uncompressed data blob in the first partition */
212   offset = keyframe ? 10 : 3;
213   partition0_size += offset;
214
215   if (!gst_bit_reader_skip (reader, 24))
216     goto error;
217
218   if (keyframe) {
219     /* check start tag: 0x9d 0x01 0x2a */
220     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x9d)
221       goto error;
222
223     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x01)
224       goto error;
225
226     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x2a)
227       goto error;
228
229     /* Skip horizontal size code (16 bits) vertical size code (16 bits) */
230     if (!gst_bit_reader_skip (reader, 32))
231       goto error;
232   }
233
234   offset = keyframe ? 10 : 3;
235   vp8dx_start_decode (&bc, data + offset, size - offset);
236
237   if (keyframe) {
238     /* color space (1 bit) and clamping type (1 bit) */
239     vp8dx_decode_bool (&bc, 0x80);
240     vp8dx_decode_bool (&bc, 0x80);
241   }
242
243   /* segmentation_enabled */
244   if (vp8dx_decode_bool (&bc, 0x80)) {
245     guint8 update_mb_segmentation_map = vp8dx_decode_bool (&bc, 0x80);
246     guint8 update_segment_feature_data = vp8dx_decode_bool (&bc, 0x80);
247
248     if (update_segment_feature_data) {
249       /* skip segment feature mode */
250       vp8dx_decode_bool (&bc, 0x80);
251
252       /* quantizer update */
253       for (i = 0; i < 4; i++) {
254         /* skip flagged quantizer value (7 bits) and sign (1 bit) */
255         if (vp8dx_decode_bool (&bc, 0x80))
256           vp8_decode_value (&bc, 8);
257       }
258
259       /* loop filter update */
260       for (i = 0; i < 4; i++) {
261         /* skip flagged lf update value (6 bits) and sign (1 bit) */
262         if (vp8dx_decode_bool (&bc, 0x80))
263           vp8_decode_value (&bc, 7);
264       }
265     }
266
267     if (update_mb_segmentation_map) {
268       /* segment prob update */
269       for (i = 0; i < 3; i++) {
270         /* skip flagged segment prob */
271         if (vp8dx_decode_bool (&bc, 0x80))
272           vp8_decode_value (&bc, 8);
273       }
274     }
275   }
276
277   /* skip filter type (1 bit), loop filter level (6 bits) and
278    * sharpness level (3 bits) */
279   vp8_decode_value (&bc, 1);
280   vp8_decode_value (&bc, 6);
281   vp8_decode_value (&bc, 3);
282
283   /* loop_filter_adj_enabled */
284   if (vp8dx_decode_bool (&bc, 0x80)) {
285
286     /* delta update */
287     if (vp8dx_decode_bool (&bc, 0x80)) {
288
289       for (i = 0; i < 8; i++) {
290         /* 8 updates, 1 bit indicate whether there is one and if follow by a
291          * 7 bit update */
292         if (vp8dx_decode_bool (&bc, 0x80))
293           vp8_decode_value (&bc, 7);
294       }
295     }
296   }
297
298   if (vp8dx_bool_error (&bc))
299     goto error;
300
301   tmp8 = vp8_decode_value (&bc, 2);
302
303   partitions = 1 << tmp8;
304
305   /* Check if things are still sensible */
306   if (partition0_size + (partitions - 1) * 3 >= size)
307     goto error;
308
309   /* partition data is right after the mode partition */
310   pdata = data + partition0_size;
311
312   /* Set up mapping */
313   self->n_partitions = partitions + 1;
314   self->partition_offset[0] = 0;
315   self->partition_size[0] = partition0_size + (partitions - 1) * 3;
316
317   self->partition_offset[1] = self->partition_size[0];
318   for (i = 1; i < partitions; i++) {
319     guint psize = (pdata[2] << 16 | pdata[1] << 8 | pdata[0]);
320
321     pdata += 3;
322     self->partition_size[i] = psize;
323     self->partition_offset[i + 1] = self->partition_offset[i] + psize;
324   }
325
326   /* Check that our partition offsets and sizes don't go outsize the buffer
327    * size. */
328   if (self->partition_offset[i] >= size)
329     goto error;
330
331   self->partition_size[i] = size - self->partition_offset[i];
332
333   self->partition_offset[i + 1] = size;
334
335   gst_bit_reader_free (reader);
336   gst_buffer_unmap (buffer, &map);
337   return TRUE;
338
339 error:
340   GST_DEBUG ("Failed to parse frame");
341   if (reader) {
342     gst_bit_reader_free (reader);
343     gst_buffer_unmap (buffer, &map);
344   }
345   return FALSE;
346 }
347
348 static guint
349 gst_rtp_vp8_offset_to_partition (GstRtpVP8Pay * self, guint offset)
350 {
351   int i;
352
353   for (i = 0; i < self->n_partitions; i++) {
354     if (offset >= self->partition_offset[i] &&
355         offset < self->partition_offset[i + 1])
356       return i;
357   }
358
359   return i;
360 }
361
362 static gsize
363 gst_rtp_vp8_calc_header_len (GstRtpVP8Pay * self)
364 {
365   switch (self->picture_id_mode) {
366     case VP8_PAY_PICTURE_ID_7BITS:
367       return 3;
368     case VP8_PAY_PICTURE_ID_15BITS:
369       return 4;
370     case VP8_PAY_NO_PICTURE_ID:
371     default:
372       return 1;
373   }
374 }
375
376 static gsize
377 gst_rtp_vp8_calc_payload_len (GstRtpVP8Pay * self)
378 {
379   GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (self);
380
381   return gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (payload) -
382       gst_rtp_vp8_calc_header_len (self), 0, 0);
383 }
384
385 /* When growing the vp8 header keep gst_rtp_vp8_calc_payload_len in sync */
386 static GstBuffer *
387 gst_rtp_vp8_create_header_buffer (GstRtpVP8Pay * self, guint8 partid,
388     gboolean start, gboolean mark, GstBuffer * in)
389 {
390   GstBuffer *out;
391   guint8 *p;
392   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
393
394   out = gst_rtp_buffer_new_allocate (gst_rtp_vp8_calc_header_len (self), 0, 0);
395   gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
396   p = gst_rtp_buffer_get_payload (&rtpbuffer);
397   /* X=0,R=0,N=0,S=start,PartID=partid */
398   p[0] = (start << 4) | partid;
399   if (self->picture_id_mode != VP8_PAY_NO_PICTURE_ID) {
400     /* Enable X=1 */
401     p[0] |= 0x80;
402     /* X: I=1,L=0,T=0,K=0,RSV=0 */
403     p[1] = 0x80;
404     if (self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS) {
405       /* I: 7 bit picture_id */
406       p[2] = self->picture_id & 0x7F;
407     } else {
408       /* I: 15 bit picture_id */
409       p[2] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
410       p[3] = self->picture_id & 0xFF;
411     }
412   }
413
414   gst_rtp_buffer_set_marker (&rtpbuffer, mark);
415
416   gst_rtp_buffer_unmap (&rtpbuffer);
417
418   GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
419   GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
420
421   return out;
422 }
423
424
425 static guint
426 gst_rtp_vp8_payload_next (GstRtpVP8Pay * self,
427     GstBufferList * list, guint offset, GstBuffer * buffer)
428 {
429   guint partition;
430   GstBuffer *header;
431   GstBuffer *sub;
432   GstBuffer *out;
433   gboolean mark;
434   gsize remaining;
435   gsize available;
436
437   remaining = gst_buffer_get_size (buffer) - offset;
438   available = gst_rtp_vp8_calc_payload_len (self);
439   if (available > remaining)
440     available = remaining;
441
442   partition = gst_rtp_vp8_offset_to_partition (self, offset);
443   g_assert (partition < self->n_partitions);
444
445   mark = (remaining == available);
446   /* whole set of partitions, payload them and done */
447   header = gst_rtp_vp8_create_header_buffer (self, partition,
448       offset == self->partition_offset[partition], mark, buffer);
449   sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
450
451   out = gst_buffer_append (header, sub);
452
453   gst_buffer_list_insert (list, -1, out);
454
455   return available;
456 }
457
458
459 static GstFlowReturn
460 gst_rtp_vp8_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
461 {
462   GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
463   GstFlowReturn ret;
464   GstBufferList *list;
465   guint offset;
466
467   if (G_UNLIKELY (!gst_rtp_vp8_pay_parse_frame (self, buffer))) {
468     g_message ("Failed to parse frame");
469     return GST_FLOW_ERROR;
470   }
471
472   list = gst_buffer_list_new ();
473
474   for (offset = 0; offset < gst_buffer_get_size (buffer);)
475     offset += gst_rtp_vp8_payload_next (self, list, offset, buffer);
476
477   ret = gst_rtp_base_payload_push_list (payload, list);
478
479   /* Incremenent and wrap the picture id if it overflows */
480   if ((self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS &&
481           ++self->picture_id >= 0x80) ||
482       (self->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS &&
483           ++self->picture_id >= 0x8000))
484     self->picture_id = 0;
485
486   return ret;
487 }
488
489 static gboolean
490 gst_rtp_vp8_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
491 {
492   GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
493
494   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
495     if (self->picture_id_mode == VP8_PAY_PICTURE_ID_7BITS)
496       self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
497     else if (self->picture_id_mode == VP8_PAY_PICTURE_ID_15BITS)
498       self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
499   }
500
501   return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp8_pay_parent_class)->sink_event
502       (payload, event);
503 }
504
505 static gboolean
506 gst_rtp_vp8_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
507 {
508   gst_rtp_base_payload_set_options (payload, "video", TRUE,
509       "VP8-DRAFT-IETF-01", 90000);
510   return gst_rtp_base_payload_set_outcaps (payload, NULL);
511 }
512
513 gboolean
514 gst_rtp_vp8_pay_plugin_init (GstPlugin * plugin)
515 {
516   return gst_element_register (plugin, "rtpvp8pay",
517       GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_PAY);
518 }