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