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