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