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