rtpvp8: Reject unknown bitstream versions
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpvp8pay.c
1 /*
2  * gst-rtp-vp8-pay.c - Source for GstRtpVP8Pay
3  * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <gst/base/gstbitreader.h>
26 #include <gst/rtp/gstrtppayloads.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpvp8pay.h"
29
30 #define FI_FRAG_UNFRAGMENTED 0x0
31 #define FI_FRAG_START 0x1
32 #define FI_FRAG_MIDDLE 0x2
33 #define FI_FRAG_END 0x3
34
35 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_pay_debug);
36 #define GST_CAT_DEFAULT gst_rtp_vp8_pay_debug
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-0-3-2\""));
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 }
59
60 static GstFlowReturn gst_rtp_vp8_pay_handle_buffer (GstBaseRTPPayload * payload,
61     GstBuffer * buffer);
62 static gboolean gst_rtp_vp8_pay_set_caps (GstBaseRTPPayload * payload,
63     GstCaps * caps);
64
65 static void
66 gst_rtp_vp8_pay_base_init (gpointer klass)
67 {
68   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
69
70   gst_element_class_add_pad_template (element_class,
71       gst_static_pad_template_get (&gst_rtp_vp8_pay_sink_template));
72   gst_element_class_add_pad_template (element_class,
73       gst_static_pad_template_get (&gst_rtp_vp8_pay_src_template));
74
75   gst_element_class_set_details_simple (element_class, "RTP VP8 payloader",
76       "Codec/Payloader/Network/RTP",
77       "Puts VP8 video in RTP packets)", "Sjoerd Simons <sjoerd@luon.net>");
78 }
79
80 static void
81 gst_rtp_vp8_pay_class_init (GstRtpVP8PayClass * gst_rtp_vp8_pay_class)
82 {
83   GstBaseRTPPayloadClass *pay_class =
84       GST_BASE_RTP_PAYLOAD_CLASS (gst_rtp_vp8_pay_class);
85
86   pay_class->handle_buffer = gst_rtp_vp8_pay_handle_buffer;
87   pay_class->set_caps = gst_rtp_vp8_pay_set_caps;
88
89   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_pay_debug, "rtpvp8pay", 0,
90       "VP8 Video RTP Payloader");
91 }
92
93 static gsize
94 gst_rtp_vp8_calc_payload_len (GstBaseRTPPayload * payload)
95 {
96   return gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU (payload) -
97       1, 0, 0);
98 }
99
100 /* When growing the vp8 header keep gst_rtp_vp8_calc_payload_len in sync */
101 static GstBuffer *
102 gst_rtp_vp8_create_header_buffer (gboolean start, gboolean mark, guint fi,
103     GstBuffer * in)
104 {
105   GstBuffer *out;
106   guint8 *p;
107
108   out = gst_rtp_buffer_new_allocate (1, 0, 0);
109   p = gst_rtp_buffer_get_payload (out);
110   /* Hardcode I = 0 and N = 0, only set FI and B */
111   p[0] = (fi & 0x3) << 1 | (start ? 1 : 0);
112
113   gst_rtp_buffer_set_marker (out, mark);
114
115   GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
116   GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (in);
117
118   return out;
119 }
120
121
122 static gboolean
123 gst_rtp_vp8_pay_parse_frame (GstRtpVP8Pay * self, GstBuffer * buffer)
124 {
125   GstBitReader *reader;
126   int i;
127   gboolean keyframe;
128   guint32 header_size;
129   guint8 version;
130   guint8 tmp8 = 0;
131   guint8 *data;
132   guint8 partitions;
133
134   reader = gst_bit_reader_new_from_buffer (buffer);
135
136   if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 3))
137     goto error;
138
139   data = GST_BUFFER_DATA (buffer);
140
141   self->is_keyframe = keyframe = ((data[0] & 0x1) == 0);
142   version = (data[0] >> 1) & 0x7;
143
144   if (G_UNLIKELY (version > 3)) {
145     GST_ERROR_OBJECT (self, "Unknown VP8 version %u", version);
146     goto error;
147   }
148
149   /* keyframe, version and show_frame use 5 bits */
150   header_size = data[2] << 11 | data[1] << 3 | (data[0] >> 5);
151
152   /* Include the uncompressed data blob in the header */
153   header_size += keyframe ? 10 : 3;
154
155   if (!gst_bit_reader_skip (reader, 24))
156     goto error;
157
158   if (keyframe) {
159     /* check start tag: 0x9d 0x01 0x2a */
160     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x9d)
161       goto error;
162
163     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x01)
164       goto error;
165
166     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x2a)
167       goto error;
168
169     /* Skip horizontal size code (16 bits) vertical size code (16 bits),
170      * color space (1 bit) and clamping type (1 bit) */
171     if (!gst_bit_reader_skip (reader, 34))
172       goto error;
173   }
174
175   /* segmentation_enabled */
176   if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
177     goto error;
178
179   if (tmp8 != 0) {
180     gboolean update_mb_segmentation_map;
181     gboolean update_segment_feature_data;
182
183     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 2))
184       goto error;
185
186     update_mb_segmentation_map = (tmp8 & 0x2) != 0;
187     update_segment_feature_data = (tmp8 & 0x1) != 0;
188
189     if (update_segment_feature_data) {
190       /* skip segment feature mode */
191       if (!gst_bit_reader_skip (reader, 1))
192         goto error;
193
194       for (i = 0; i < 4; i++) {
195         /* quantizer update */
196         if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
197           goto error;
198
199         if (tmp8 != 0) {
200           /* skip quantizer value (7 bits) and sign (1 bit) */
201           if (!gst_bit_reader_skip (reader, 8))
202             goto error;
203         }
204       }
205
206       for (i = 0; i < 4; i++) {
207         /* loop filter update */
208         if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
209           goto error;
210
211         if (tmp8 != 0) {
212           /* skip lf update value (6 bits) and sign (1 bit) */
213           if (!gst_bit_reader_skip (reader, 7))
214             goto error;
215         }
216       }
217     }
218
219     if (update_mb_segmentation_map) {
220       for (i = 0; i < 3; i++) {
221         /* segment prob update */
222         if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
223           goto error;
224
225         if (tmp8 != 0) {
226           /* skip segment prob */
227           if (!gst_bit_reader_skip (reader, 8))
228             goto error;
229         }
230       }
231     }
232   }
233
234   /* skip filter type (1 bit), loop filter level (6 bits) and
235    * sharpness level (3 bits) */
236   if (!gst_bit_reader_skip (reader, 10))
237     goto error;
238
239   /* loop_filter_adj_enabled */
240   if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
241     goto error;
242
243   if (tmp8 != 0) {
244     /* loop filter adj enabled */
245
246     /* mode_ref_lf_delta_update */
247     if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
248       goto error;
249
250     if (tmp8 != 0) {
251       /* mode_ref_lf_data_update */
252       int i;
253
254       for (i = 0; i < 8; i++) {
255         /* 8 updates, 1 bit indicate whether there is one and if follow by a
256          * 7 bit update */
257         if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 1))
258           goto error;
259
260         if (tmp8 != 0) {
261           /* skip delta magnitude (6 bits) and sign (1 bit) */
262           if (!gst_bit_reader_skip (reader, 7))
263             goto error;
264         }
265       }
266     }
267   }
268
269   if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 2))
270     goto error;
271
272   partitions = 1 << tmp8;
273
274   /* Check if things are still sensible */
275   if (header_size + (partitions - 1) * 3 >= GST_BUFFER_SIZE (buffer))
276     goto error;
277
278   /* partition data is right after the frame header */
279   data = GST_BUFFER_DATA (buffer) + header_size;
280
281   /* Set up mapping, count the initial header as a partition to make other
282    * sections of the code easier */
283   self->n_partitions = partitions + 1;
284   self->partition_offset[0] = 0;
285   self->partition_size[0] = header_size + (partitions - 1) * 3;
286
287   self->partition_offset[1] = self->partition_size[0];
288   for (i = 1; i < partitions; i++) {
289     guint size = (data[2] << 16 | data[1] << 8 | data[0]);
290
291     data += 3;
292     self->partition_size[i] = size;
293     self->partition_offset[i + 1] = self->partition_offset[i] + size;
294   }
295
296   /* Check that our partition offsets and sizes don't go outsize the buffer
297    * size. */
298   if (self->partition_offset[i] >= GST_BUFFER_SIZE (buffer))
299     goto error;
300
301   self->partition_size[i] = GST_BUFFER_SIZE (buffer)
302       - self->partition_offset[i];
303
304   self->partition_offset[i + 1] = GST_BUFFER_SIZE (buffer);
305
306   gst_bit_reader_free (reader);
307   return TRUE;
308
309 error:
310   GST_DEBUG ("Failed to parse frame");
311   gst_bit_reader_free (reader);
312   return FALSE;
313 }
314
315 static guint
316 gst_rtp_vp8_fit_partitions (GstRtpVP8Pay * self, gint first, gsize available)
317 {
318   guint num = 0;
319   int i;
320
321   g_assert (first < self->n_partitions);
322
323   for (i = first;
324       i < self->n_partitions && self->partition_size[i] < available; i++) {
325     num++;
326     available -= self->partition_size[i];
327   }
328
329   return num;
330 }
331
332 static GstBuffer *
333 gst_rtp_vp8_create_sub (GstRtpVP8Pay * self,
334     GstBuffer * buffer, guint current, guint num)
335 {
336   guint offset = self->partition_offset[current];
337   guint size = self->partition_offset[current + num] - offset;
338
339   return gst_buffer_create_sub (buffer, offset, size);
340 }
341
342
343 static guint
344 gst_rtp_vp8_payload_next (GstRtpVP8Pay * self,
345     GstBufferListIterator * it, guint first, GstBuffer * buffer)
346 {
347   guint num;
348   GstBuffer *header;
349   GstBuffer *sub;
350   gboolean mark;
351   gsize available = gst_rtp_vp8_calc_payload_len (GST_BASE_RTP_PAYLOAD (self));
352
353   g_assert (first < 9);
354
355   /* How many partitions can we fit */
356   num = gst_rtp_vp8_fit_partitions (self, first, available);
357
358   if (num > 0) {
359     mark = (first + num == self->n_partitions);
360     /* whole set of partitions, payload them and done */
361     header = gst_rtp_vp8_create_header_buffer (first == 0, mark,
362         FI_FRAG_UNFRAGMENTED, buffer);
363     sub = gst_rtp_vp8_create_sub (self, buffer, first, num);
364
365     gst_buffer_list_iterator_add_group (it);
366     gst_buffer_list_iterator_add (it, header);
367     gst_buffer_list_iterator_add (it, sub);
368   } else {
369     /* Fragmented packets */
370     guint offset = self->partition_offset[first];
371     guint left = self->partition_size[first];
372     gboolean start = (first == 0);
373
374     header = gst_rtp_vp8_create_header_buffer (start, FALSE,
375         FI_FRAG_START, buffer);
376     sub = gst_buffer_create_sub (buffer, offset, available);
377     offset += available;
378
379     gst_buffer_list_iterator_add_group (it);
380     gst_buffer_list_iterator_add (it, header);
381     gst_buffer_list_iterator_add (it, sub);
382
383     left -= available;
384
385     for (; left > available; left -= available) {
386       header = gst_rtp_vp8_create_header_buffer (start, FALSE,
387           FI_FRAG_MIDDLE, buffer);
388
389       sub = gst_buffer_create_sub (buffer, offset, available);
390       offset += available;
391
392       gst_buffer_list_iterator_add_group (it);
393       gst_buffer_list_iterator_add (it, header);
394       gst_buffer_list_iterator_add (it, sub);
395     }
396
397     mark = (first + 1 == self->n_partitions);
398
399     header = gst_rtp_vp8_create_header_buffer (start, mark,
400         FI_FRAG_END, buffer);
401     sub = gst_buffer_create_sub (buffer, offset, left);
402
403     gst_buffer_list_iterator_add_group (it);
404     gst_buffer_list_iterator_add (it, header);
405     gst_buffer_list_iterator_add (it, sub);
406
407     return 1;
408   }
409
410   return num;
411 }
412
413
414 static GstFlowReturn
415 gst_rtp_vp8_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
416 {
417   GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
418   GstFlowReturn ret;
419   GstBufferList *list;
420   GstBufferListIterator *it;
421   guint current;
422
423   if (G_UNLIKELY (!gst_rtp_vp8_pay_parse_frame (self, buffer))) {
424     /* FIXME throw flow error */
425     g_message ("Failed to parse frame");
426     return GST_FLOW_ERROR;
427   }
428
429   list = gst_buffer_list_new ();
430   it = gst_buffer_list_iterate (list);
431
432   for (current = 0; current < self->n_partitions;) {
433     guint n;
434
435     n = gst_rtp_vp8_payload_next (self, it, current, buffer);
436     current += n;
437   }
438
439   ret = gst_basertppayload_push_list (payload, list);
440   gst_buffer_list_iterator_free (it);
441
442   return ret;
443 }
444
445 static gboolean
446 gst_rtp_vp8_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
447 {
448   gst_basertppayload_set_options (payload, "video", TRUE,
449       "VP8-DRAFT-0-3-2", 90000);
450   return gst_basertppayload_set_outcaps (payload, NULL);
451 }
452
453 gboolean
454 gst_rtp_vp8_pay_plugin_init (GstPlugin * plugin)
455 {
456   return gst_element_register (plugin, "rtpvp8pay",
457       GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_PAY);
458 }