rtpac3depay: should output audio/x-ac3 not audio/ac3
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpvp9pay.c
1 /*
2  * gstrtpvp9pay.c - Source for GstRtpVP9Pay
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  * Copyright (C) 2015 Stian Selnes <stian@pexip.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <gst/base/gstbitreader.h>
32 #include <gst/rtp/gstrtppayloads.h>
33 #include <gst/rtp/gstrtpbuffer.h>
34 #include <gst/video/video.h>
35 #include "gstrtpelements.h"
36 #include "dboolhuff.h"
37 #include "gstrtpvp9pay.h"
38 #include "gstrtputils.h"
39
40 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_pay_debug);
41 #define GST_CAT_DEFAULT gst_rtp_vp9_pay_debug
42
43 #define DEFAULT_PICTURE_ID_MODE VP9_PAY_NO_PICTURE_ID
44
45 enum
46 {
47   PROP_0,
48   PROP_PICTURE_ID_MODE
49 };
50
51 #define GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE (gst_rtp_vp9_pay_picture_id_mode_get_type())
52 static GType
53 gst_rtp_vp9_pay_picture_id_mode_get_type (void)
54 {
55   static GType mode_type = 0;
56   static const GEnumValue modes[] = {
57     {VP9_PAY_NO_PICTURE_ID, "No Picture ID", "none"},
58     {VP9_PAY_PICTURE_ID_7BITS, "7-bit Picture ID", "7-bit"},
59     {VP9_PAY_PICTURE_ID_15BITS, "15-bit Picture ID", "15-bit"},
60     {0, NULL, NULL},
61   };
62
63   if (!mode_type) {
64     mode_type = g_enum_register_static ("GstVP9RTPPayMode", modes);
65   }
66   return mode_type;
67 }
68
69 static void gst_rtp_vp9_pay_get_property (GObject * object, guint prop_id,
70     GValue * value, GParamSpec * pspec);
71 static void gst_rtp_vp9_pay_set_property (GObject * object, guint prop_id,
72     const GValue * value, GParamSpec * pspec);
73
74 static GstFlowReturn gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload,
75     GstBuffer * buffer);
76 static gboolean gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload,
77     GstEvent * event);
78 static gboolean gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload,
79     GstCaps * caps);
80
81 G_DEFINE_TYPE (GstRtpVP9Pay, gst_rtp_vp9_pay, GST_TYPE_RTP_BASE_PAYLOAD);
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpvp9pay, "rtpvp9pay",
83     GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_PAY, rtp_element_init (plugin));
84
85 static GstStaticPadTemplate gst_rtp_vp9_pay_src_template =
86 GST_STATIC_PAD_TEMPLATE ("src",
87     GST_PAD_SRC,
88     GST_PAD_ALWAYS,
89     GST_STATIC_CAPS ("application/x-rtp, "
90         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
91         "clock-rate = (int) 90000, encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
92
93 static GstStaticPadTemplate gst_rtp_vp9_pay_sink_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
95     GST_PAD_SINK,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS ("video/x-vp9"));
98
99 static void
100 gst_rtp_vp9_pay_init (GstRtpVP9Pay * obj)
101 {
102   obj->picture_id_mode = DEFAULT_PICTURE_ID_MODE;
103   if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
104     obj->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
105   else if (obj->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
106     obj->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
107 }
108
109 static void
110 gst_rtp_vp9_pay_class_init (GstRtpVP9PayClass * gst_rtp_vp9_pay_class)
111 {
112   GObjectClass *gobject_class = G_OBJECT_CLASS (gst_rtp_vp9_pay_class);
113   GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_pay_class);
114   GstRTPBasePayloadClass *pay_class =
115       GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_class);
116
117   gobject_class->set_property = gst_rtp_vp9_pay_set_property;
118   gobject_class->get_property = gst_rtp_vp9_pay_get_property;
119
120   g_object_class_install_property (gobject_class, PROP_PICTURE_ID_MODE,
121       g_param_spec_enum ("picture-id-mode", "Picture ID Mode",
122           "The picture ID mode for payloading",
123           GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE, DEFAULT_PICTURE_ID_MODE,
124           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125
126   gst_element_class_add_static_pad_template (element_class,
127       &gst_rtp_vp9_pay_sink_template);
128   gst_element_class_add_static_pad_template (element_class,
129       &gst_rtp_vp9_pay_src_template);
130
131   gst_element_class_set_static_metadata (element_class, "RTP VP9 payloader",
132       "Codec/Payloader/Network/RTP",
133       "Puts VP9 video in RTP packets)", "Stian Selnes <stian@pexip.com>");
134
135   pay_class->handle_buffer = gst_rtp_vp9_pay_handle_buffer;
136   pay_class->sink_event = gst_rtp_vp9_pay_sink_event;
137   pay_class->set_caps = gst_rtp_vp9_pay_set_caps;
138
139   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_pay_debug, "rtpvp9pay", 0,
140       "VP9 Video RTP Payloader");
141
142   gst_type_mark_as_plugin_api (GST_TYPE_RTP_VP9_PAY_PICTURE_ID_MODE, 0);
143 }
144
145 static void
146 gst_rtp_vp9_pay_set_property (GObject * object,
147     guint prop_id, const GValue * value, GParamSpec * pspec)
148 {
149   GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
150
151   switch (prop_id) {
152     case PROP_PICTURE_ID_MODE:
153       rtpvp9pay->picture_id_mode = g_value_get_enum (value);
154       if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
155         rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
156       else if (rtpvp9pay->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
157         rtpvp9pay->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
158       break;
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162   }
163 }
164
165 static void
166 gst_rtp_vp9_pay_get_property (GObject * object,
167     guint prop_id, GValue * value, GParamSpec * pspec)
168 {
169   GstRtpVP9Pay *rtpvp9pay = GST_RTP_VP9_PAY (object);
170
171   switch (prop_id) {
172     case PROP_PICTURE_ID_MODE:
173       g_value_set_enum (value, rtpvp9pay->picture_id_mode);
174       break;
175     default:
176       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177       break;
178   }
179 }
180
181 #define VP9_PROFILE_0 0
182 #define VP9_PROFILE_1 1
183 #define VP9_PROFILE_2 2
184 #define VP9_PROFILE_3 3
185 #define VP9_FRAME_MARKER 0x2
186 #define VPX_CS_SRGB 7
187
188 static gboolean
189 gst_rtp_vp9_pay_parse_frame (GstRtpVP9Pay * self, GstBuffer * buffer,
190     gsize buffer_size)
191 {
192   GstMapInfo map = GST_MAP_INFO_INIT;
193   GstBitReader reader;
194   guint8 *data;
195   gsize size;
196   gboolean keyframe;
197   guint32 tmp, profile;
198
199   if (G_UNLIKELY (buffer_size < 3))
200     goto error;
201
202   if (!gst_buffer_map (buffer, &map, GST_MAP_READ) || !map.data)
203     goto error;
204
205   data = map.data;
206   size = map.size;
207
208   gst_bit_reader_init (&reader, data, size);
209
210
211   /* frame marker */
212   if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 2)
213       || tmp != VP9_FRAME_MARKER)
214     goto error;
215
216   /* profile, variable length */
217   if (!gst_bit_reader_get_bits_uint32 (&reader, &profile, 2))
218     goto error;
219   if (profile > 2) {
220     if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
221       goto error;
222     profile += tmp;
223   }
224
225   /* show existing frame */
226   if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
227     goto error;
228   if (tmp) {
229     if (!gst_bit_reader_skip (&reader, 3))
230       goto error;
231     return TRUE;
232   }
233
234   /* frame type */
235   if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
236     goto error;
237   self->is_keyframe = keyframe = (tmp == 0);
238
239   /* show frame and resilient mode */
240   if (!gst_bit_reader_skip (&reader, 2))
241     goto error;
242
243   if (keyframe) {
244     /* sync code */
245     const guint32 sync_code = 0x498342;
246     if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 24))
247       goto error;
248     if (tmp != sync_code)
249       goto error;
250
251     if (profile >= VP9_PROFILE_2) {
252       /* bit depth */
253       if (!gst_bit_reader_skip (&reader, 1))
254         goto error;
255     }
256
257     /* color space */
258     if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 3))
259       goto error;
260     if (tmp != VPX_CS_SRGB) {
261       /* color range */
262       if (!gst_bit_reader_skip (&reader, 1))
263         goto error;
264       if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3) {
265         /* subsampling + reserved bit */
266         if (!gst_bit_reader_skip (&reader, 2 + 1))
267           goto error;
268       }
269     } else {
270       if (profile == VP9_PROFILE_1 || profile == VP9_PROFILE_3)
271         /* reserved bit */
272         if (!gst_bit_reader_skip (&reader, 1))
273           goto error;
274     }
275
276     /* frame size */
277     if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
278       goto error;
279     self->width = tmp + 1;
280     if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 16))
281       goto error;
282     self->height = tmp + 1;
283
284     /* render size */
285     if (!gst_bit_reader_get_bits_uint32 (&reader, &tmp, 1))
286       goto error;
287     if (tmp) {
288       if (!gst_bit_reader_skip (&reader, 32))
289         goto error;
290     }
291
292     GST_INFO_OBJECT (self, "parsed width=%d height=%d", self->width,
293         self->height);
294   }
295
296
297   gst_buffer_unmap (buffer, &map);
298   return TRUE;
299
300 error:
301   GST_DEBUG ("Failed to parse frame");
302   if (map.memory != NULL) {
303     gst_buffer_unmap (buffer, &map);
304   }
305   return FALSE;
306 }
307
308 static gsize
309 gst_rtp_vp9_calc_header_len (GstRtpVP9Pay * self, gboolean start)
310 {
311   gsize len = 1;
312
313   switch (self->picture_id_mode) {
314     case VP9_PAY_PICTURE_ID_7BITS:
315       len += 1;
316       break;
317     case VP9_PAY_PICTURE_ID_15BITS:
318       len += 2;
319     default:
320       break;
321   }
322
323   /* Assume non-flexible mode */
324   /* Assume L-bit not set, no L header */
325
326   if (self->is_keyframe && start) {
327     /* Assume V-bit set */
328     /* FIXME: SS depends on layers and prediction structure */
329     /* For now assume 1 spatial and 1 temporal layer. */
330     /* FIXME: Only for the first packet in the key frame */
331     len += 8;
332   }
333
334   return len;
335 }
336
337 /* VP9 RTP header, non-flexible mode:
338
339          0 1 2 3 4 5 6 7
340         +-+-+-+-+-+-+-+-+
341         |I|P|L|F|B|E|V|-| (REQUIRED)
342         +-+-+-+-+-+-+-+-+
343    I:   |M| PICTURE ID  | (RECOMMENDED)
344         +-+-+-+-+-+-+-+-+
345    M:   | EXTENDED PID  | (RECOMMENDED)
346         +-+-+-+-+-+-+-+-+
347    L:   |  T  |U|  S  |D| (CONDITIONALLY RECOMMENDED)
348         +-+-+-+-+-+-+-+-+                             -\
349    P,F: | P_DIFF    |X|N| (CONDITIONALLY RECOMMENDED)  .
350         +-+-+-+-+-+-+-+-+                              . - up to 3 times
351    X:   |EXTENDED P_DIFF| (OPTIONAL)                   .
352         +-+-+-+-+-+-+-+-+                             -/
353    V:   | SS            |
354         | ..            |
355         +-+-+-+-+-+-+-+-+
356
357    Scalability structure (SS)
358      (from https://chromium.googlesource.com/external/webrtc/+/HEAD/webrtc/modules/rtp_rtcp/source/rtp_format_vp9.cc
359      since latest draft is not up to date with chromium)
360
361         +-+-+-+-+-+-+-+-+
362    V:   | N_S |Y|G|-|-|-|
363         +-+-+-+-+-+-+-+-+              -|
364    Y:   |     WIDTH     | (OPTIONAL)    .
365         +               +               .
366         |               | (OPTIONAL)    .
367         +-+-+-+-+-+-+-+-+               . N_S + 1 times
368         |     HEIGHT    | (OPTIONAL)    .
369         +               +               .
370         |               | (OPTIONAL)    .
371         +-+-+-+-+-+-+-+-+              -|
372    G:   |      N_G      | (OPTIONAL)
373         +-+-+-+-+-+-+-+-+                           -|
374    N_G: |  T  |U| R |-|-| (OPTIONAL)                 .
375         +-+-+-+-+-+-+-+-+              -|            . N_G times
376         |    P_DIFF     | (OPTIONAL)    . R times    .
377         +-+-+-+-+-+-+-+-+              -|           -|
378
379 **/
380
381 /* When growing the vp9 header keep max payload len calculation in sync */
382 static GstBuffer *
383 gst_rtp_vp9_create_header_buffer (GstRtpVP9Pay * self,
384     gboolean start, gboolean mark, GstBuffer * in)
385 {
386   GstBuffer *out;
387   guint8 *p;
388   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
389   guint off = 1;
390   guint hdrlen = gst_rtp_vp9_calc_header_len (self, start);
391
392   out =
393       gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD (self),
394       hdrlen, 0, 0);
395   gst_rtp_buffer_map (out, GST_MAP_READWRITE, &rtpbuffer);
396   p = gst_rtp_buffer_get_payload (&rtpbuffer);
397   p[0] = 0x0;
398
399   if (self->picture_id_mode != VP9_PAY_NO_PICTURE_ID) {
400     p[0] |= 0x80;
401     if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS) {
402       /* M=0 */
403       p[off++] = self->picture_id & 0x7F;
404     } else {
405       /* M=1 */
406       p[off++] = 0x80 | ((self->picture_id & 0x7FFF) >> 8);
407       p[off++] = self->picture_id & 0xFF;
408     }
409   }
410
411   if (!self->is_keyframe)
412     p[0] |= 0x40;
413   if (start)
414     p[0] |= 0x08;
415   if (mark)
416     p[0] |= 0x04;
417
418   if (self->is_keyframe && start) {
419     p[0] |= 0x02;
420     /* scalability structure, hard coded for now to be similar to chromium for
421      * quick and dirty interop */
422     p[off++] = 0x18;            /* N_S=0 Y=1 G=1 */
423     p[off++] = self->width >> 8;
424     p[off++] = self->width & 0xFF;
425     p[off++] = self->height >> 8;
426     p[off++] = self->height & 0xFF;
427     p[off++] = 0x01;            /* N_G=1 */
428     p[off++] = 0x04;            /* T=0, U=0, R=1 */
429     p[off++] = 0x01;            /* P_DIFF=1 */
430   }
431
432   g_assert_cmpint (off, ==, hdrlen);
433
434   gst_rtp_buffer_set_marker (&rtpbuffer, mark);
435   if (mark)
436     GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_MARKER);
437
438   gst_rtp_buffer_unmap (&rtpbuffer);
439
440   GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
441   GST_BUFFER_PTS (out) = GST_BUFFER_PTS (in);
442
443   return out;
444 }
445
446 static guint
447 gst_rtp_vp9_payload_next (GstRtpVP9Pay * self, GstBufferList * list,
448     guint offset, GstBuffer * buffer, gsize buffer_size, gsize max_payload_len)
449 {
450   GstBuffer *header;
451   GstBuffer *sub;
452   GstBuffer *out;
453   gboolean mark;
454   gsize remaining;
455   gsize available;
456
457   remaining = buffer_size - offset;
458   available = max_payload_len;
459   if (available > remaining)
460     available = remaining;
461
462   mark = (remaining == available);
463   header = gst_rtp_vp9_create_header_buffer (self, offset == 0, mark, buffer);
464   sub = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, available);
465
466   gst_rtp_copy_video_meta (self, header, buffer);
467
468   out = gst_buffer_append (header, sub);
469
470   gst_buffer_list_insert (list, -1, out);
471
472   return available;
473 }
474
475
476 static GstFlowReturn
477 gst_rtp_vp9_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
478 {
479   GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
480   GstFlowReturn ret;
481   GstBufferList *list;
482   gsize size, max_paylen;
483   guint offset, mtu, vp9_hdr_len;
484
485   size = gst_buffer_get_size (buffer);
486
487   if (G_UNLIKELY (!gst_rtp_vp9_pay_parse_frame (self, buffer, size))) {
488     GST_ELEMENT_ERROR (self, STREAM, ENCODE, (NULL),
489         ("Failed to parse VP9 frame"));
490     return GST_FLOW_ERROR;
491   }
492
493   mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
494   vp9_hdr_len = gst_rtp_vp9_calc_header_len (self, TRUE);
495   max_paylen = gst_rtp_buffer_calc_payload_len (mtu - vp9_hdr_len, 0, 0);
496
497   list = gst_buffer_list_new_sized ((size / max_paylen) + 1);
498
499   offset = 0;
500   while (offset < size) {
501     offset +=
502         gst_rtp_vp9_payload_next (self, list, offset, buffer, size, max_paylen);
503   }
504
505   ret = gst_rtp_base_payload_push_list (payload, list);
506
507   /* Incremenent and wrap the picture id if it overflows */
508   if ((self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS &&
509           ++self->picture_id >= 0x80) ||
510       (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS &&
511           ++self->picture_id >= 0x8000))
512     self->picture_id = 0;
513
514   gst_buffer_unref (buffer);
515
516   return ret;
517 }
518
519 static gboolean
520 gst_rtp_vp9_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
521 {
522   GstRtpVP9Pay *self = GST_RTP_VP9_PAY (payload);
523
524   if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START) {
525     if (self->picture_id_mode == VP9_PAY_PICTURE_ID_7BITS)
526       self->picture_id = g_random_int_range (0, G_MAXUINT8) & 0x7F;
527     else if (self->picture_id_mode == VP9_PAY_PICTURE_ID_15BITS)
528       self->picture_id = g_random_int_range (0, G_MAXUINT16) & 0x7FFF;
529   }
530
531   return GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_vp9_pay_parent_class)->sink_event
532       (payload, event);
533 }
534
535 static gboolean
536 gst_rtp_vp9_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
537 {
538   GstCaps *src_caps;
539   const char *encoding_name = "VP9";
540
541   src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
542   if (src_caps) {
543     GstStructure *s;
544     const GValue *value;
545
546     s = gst_caps_get_structure (src_caps, 0);
547
548     if (gst_structure_has_field (s, "encoding-name")) {
549       GValue default_value = G_VALUE_INIT;
550
551       g_value_init (&default_value, G_TYPE_STRING);
552       g_value_set_static_string (&default_value, encoding_name);
553
554       value = gst_structure_get_value (s, "encoding-name");
555       if (!gst_value_can_intersect (&default_value, value))
556         encoding_name = "VP9-DRAFT-IETF-01";
557     }
558     gst_caps_unref (src_caps);
559   }
560
561   gst_rtp_base_payload_set_options (payload, "video", TRUE,
562       encoding_name, 90000);
563
564   return gst_rtp_base_payload_set_outcaps (payload, NULL);
565 }