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