2 * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include <gst/tag/tag.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include <gst/video/video.h>
29 #include "gstrtptheoradepay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY_STATIC (rtptheoradepay_debug);
33 #define GST_CAT_DEFAULT (rtptheoradepay_debug)
35 static GstStaticPadTemplate gst_rtp_theora_depay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("application/x-rtp, "
40 "media = (string) \"video\", "
41 "clock-rate = (int) 90000, " "encoding-name = (string) \"THEORA\""
42 /* All required parameters
44 * "sampling = (string) { "YCbCr-4:2:0", "YCbCr-4:2:2", "YCbCr-4:4:4" } "
45 * "width = (string) [1, 1048561] (multiples of 16) "
46 * "height = (string) [1, 1048561] (multiples of 16) "
47 * "delivery-method = (string) { inline, in_band, out_band/<specific_name> } "
48 * "configuration = (string) ANY"
50 /* All optional parameters
52 * "configuration-uri ="
57 static GstStaticPadTemplate gst_rtp_theora_depay_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
61 GST_STATIC_CAPS ("video/x-theora")
64 #define gst_rtp_theora_depay_parent_class parent_class
65 G_DEFINE_TYPE (GstRtpTheoraDepay, gst_rtp_theora_depay,
66 GST_TYPE_RTP_BASE_DEPAYLOAD);
68 static gboolean gst_rtp_theora_depay_setcaps (GstRTPBaseDepayload * depayload,
70 static GstBuffer *gst_rtp_theora_depay_process (GstRTPBaseDepayload * depayload,
72 static gboolean gst_rtp_theora_depay_packet_lost (GstRTPBaseDepayload *
73 depayload, GstEvent * event);
75 static void gst_rtp_theora_depay_finalize (GObject * object);
77 static GstStateChangeReturn gst_rtp_theora_depay_change_state (GstElement *
78 element, GstStateChange transition);
81 gst_rtp_theora_depay_class_init (GstRtpTheoraDepayClass * klass)
83 GObjectClass *gobject_class;
84 GstElementClass *gstelement_class;
85 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
87 gobject_class = (GObjectClass *) klass;
88 gstelement_class = (GstElementClass *) klass;
89 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
91 gobject_class->finalize = gst_rtp_theora_depay_finalize;
93 gstelement_class->change_state = gst_rtp_theora_depay_change_state;
95 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_theora_depay_process;
96 gstrtpbasedepayload_class->set_caps = gst_rtp_theora_depay_setcaps;
97 gstrtpbasedepayload_class->packet_lost = gst_rtp_theora_depay_packet_lost;
99 gst_element_class_add_static_pad_template (gstelement_class,
100 &gst_rtp_theora_depay_sink_template);
101 gst_element_class_add_static_pad_template (gstelement_class,
102 &gst_rtp_theora_depay_src_template);
104 gst_element_class_set_static_metadata (gstelement_class,
105 "RTP Theora depayloader", "Codec/Depayloader/Network/RTP",
106 "Extracts Theora video from RTP packets (draft-01 of RFC XXXX)",
107 "Wim Taymans <wim.taymans@gmail.com>");
109 GST_DEBUG_CATEGORY_INIT (rtptheoradepay_debug, "rtptheoradepay", 0,
110 "Theora RTP Depayloader");
114 gst_rtp_theora_depay_init (GstRtpTheoraDepay * rtptheoradepay)
116 rtptheoradepay->adapter = gst_adapter_new ();
120 free_config (GstRtpTheoraConfig * config)
122 g_list_free_full (config->headers, (GDestroyNotify) gst_buffer_unref);
127 free_indents (GstRtpTheoraDepay * rtptheoradepay)
129 g_list_free_full (rtptheoradepay->configs, (GDestroyNotify) free_config);
130 rtptheoradepay->configs = NULL;
134 gst_rtp_theora_depay_finalize (GObject * object)
136 GstRtpTheoraDepay *rtptheoradepay = GST_RTP_THEORA_DEPAY (object);
138 g_object_unref (rtptheoradepay->adapter);
140 G_OBJECT_CLASS (parent_class)->finalize (object);
144 gst_rtp_theora_depay_parse_configuration (GstRtpTheoraDepay * rtptheoradepay,
154 gst_buffer_map (confbuf, &map, GST_MAP_READ);
158 GST_DEBUG_OBJECT (rtptheoradepay, "config size %" G_GSIZE_FORMAT, size);
160 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 * | Number of packed headers |
162 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
168 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
171 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
176 num_headers = GST_READ_UINT32_BE (data);
180 GST_DEBUG_OBJECT (rtptheoradepay, "have %u headers", num_headers);
183 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
184 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
185 * | Ident | length ..
186 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
187 * .. | n. of headers | length1 | length2 ..
188 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
189 * .. | Identification Header ..
190 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
191 * .................................................................
192 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
193 * .. | Comment Header ..
194 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
195 * .................................................................
196 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
197 * .. Comment Header |
198 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
200 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201 * .................................................................
202 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206 for (i = 0; i < num_headers; i++) {
210 GstRtpTheoraConfig *conf;
217 ident = (data[0] << 16) | (data[1] << 8) | data[2];
218 length = (data[3] << 8) | data[4];
223 GST_DEBUG_OBJECT (rtptheoradepay,
224 "header %d, ident 0x%08x, length %u, left %" G_GSIZE_FORMAT, i, ident,
227 /* FIXME check if we already got this ident */
229 /* length might also include count of following size fields */
230 if (size < length && size + 1 != length)
233 /* read header sizes we read 2 sizes, the third size (for which we allocate
234 * space) must be derived from the total packed header length. */
235 h_sizes = g_newa (guint, n_headers + 1);
236 for (j = 0; j < n_headers; j++) {
246 h_size = (h_size << 7) | (b & 0x7f);
248 GST_DEBUG_OBJECT (rtptheoradepay, "headers %d: size: %u", j, h_size);
252 /* last header length is the remaining space */
253 GST_DEBUG_OBJECT (rtptheoradepay, "last header size: %u", length);
256 GST_DEBUG_OBJECT (rtptheoradepay, "preparing headers");
257 conf = g_new0 (GstRtpTheoraConfig, 1);
260 for (j = 0; j <= n_headers; j++) {
265 if (j != n_headers || size + extra != h_size) {
269 /* otherwise means that overall length field contained total length,
270 * including extra fields */
275 GST_DEBUG_OBJECT (rtptheoradepay, "reading header %d, size %u", j,
279 gst_buffer_copy_region (confbuf, GST_BUFFER_COPY_ALL, data - map.data,
281 conf->headers = g_list_append (conf->headers, buf);
285 rtptheoradepay->configs = g_list_append (rtptheoradepay->configs, conf);
288 gst_buffer_unmap (confbuf, &map);
289 gst_buffer_unref (confbuf);
296 GST_DEBUG_OBJECT (rtptheoradepay, "configuration too small");
297 gst_buffer_unmap (confbuf, &map);
298 gst_buffer_unref (confbuf);
304 gst_rtp_theora_depay_parse_inband_configuration (GstRtpTheoraDepay *
305 rtptheoradepay, guint ident, guint8 * configuration, guint size,
311 if (G_UNLIKELY (size < 4))
314 /* transform inline to out-of-band and parse that one */
315 confbuf = gst_buffer_new_and_alloc (size + 9);
316 gst_buffer_map (confbuf, &map, GST_MAP_WRITE);
318 GST_WRITE_UINT32_BE (map.data, 1);
320 GST_WRITE_UINT24_BE (map.data + 4, ident);
321 /* write sort-of-length */
322 GST_WRITE_UINT16_BE (map.data + 7, length);
324 memcpy (map.data + 9, configuration, size);
325 gst_buffer_unmap (confbuf, &map);
327 return gst_rtp_theora_depay_parse_configuration (rtptheoradepay, confbuf);
331 gst_rtp_theora_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
333 GstStructure *structure;
334 GstRtpTheoraDepay *rtptheoradepay;
336 const gchar *configuration;
339 rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload);
341 rtptheoradepay->needs_keyframe = FALSE;
343 structure = gst_caps_get_structure (caps, 0);
345 /* read and parse configuration string */
346 configuration = gst_structure_get_string (structure, "configuration");
352 /* deserialize base64 to buffer */
353 data = g_base64_decode (configuration, &size);
355 confbuf = gst_buffer_new ();
356 gst_buffer_append_memory (confbuf,
357 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
359 if (!gst_rtp_theora_depay_parse_configuration (rtptheoradepay, confbuf))
360 goto invalid_configuration;
363 /* set caps on pad and on header */
364 srccaps = gst_caps_new_empty_simple ("video/x-theora");
365 res = gst_pad_set_caps (depayload->srcpad, srccaps);
366 gst_caps_unref (srccaps);
368 /* Clock rate is always 90000 according to draft-barbato-avt-rtp-theora-01 */
369 depayload->clock_rate = 90000;
374 invalid_configuration:
376 GST_ERROR_OBJECT (rtptheoradepay, "invalid configuration specified");
382 gst_rtp_theora_depay_switch_codebook (GstRtpTheoraDepay * rtptheoradepay,
386 gboolean res = FALSE;
388 for (walk = rtptheoradepay->configs; walk; walk = g_list_next (walk)) {
389 GstRtpTheoraConfig *conf = (GstRtpTheoraConfig *) walk->data;
391 if (conf->ident == ident) {
394 /* FIXME, remove pads, create new pad.. */
396 /* push out all the headers */
397 for (headers = conf->headers; headers; headers = g_list_next (headers)) {
398 GstBuffer *header = GST_BUFFER_CAST (headers->data);
400 gst_buffer_ref (header);
401 gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtptheoradepay),
404 /* remember the current config */
405 rtptheoradepay->config = conf;
410 /* we don't know about the headers, figure out an alternative method for
411 * getting the codebooks. FIXME, fail for now. */
417 gst_rtp_theora_depay_process (GstRTPBaseDepayload * depayload,
420 GstRtpTheoraDepay *rtptheoradepay;
425 GstBuffer *payload_buffer = NULL;
427 guint32 header, ident;
428 guint8 F, TDT, packets;
431 rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload);
433 payload_len = gst_rtp_buffer_get_payload_len (rtp);
435 GST_DEBUG_OBJECT (depayload, "got RTP packet of size %d", payload_len);
437 /* we need at least 4 bytes for the packet header */
438 if (G_UNLIKELY (payload_len < 4))
441 payload = gst_rtp_buffer_get_payload (rtp);
443 header = GST_READ_UINT32_BE (payload);
446 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
447 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
448 * | Ident | F |TDT|# pkts.|
449 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
451 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
452 * TDT: Theora data type (0=theora, 1=config, 2=comment, 3=reserved)
453 * pkts: number of packets.
455 TDT = (header & 0x30) >> 4;
456 if (G_UNLIKELY (TDT == 3))
457 goto ignore_reserved;
459 ident = (header >> 8) & 0xffffff;
460 F = (header & 0xc0) >> 6;
461 packets = (header & 0xf);
463 GST_DEBUG_OBJECT (depayload, "ident: 0x%08x, F: %d, TDT: %d, packets: %d",
464 ident, F, TDT, packets);
467 gboolean do_switch = FALSE;
469 /* we have a raw payload, find the codebook for the ident */
470 if (!rtptheoradepay->config) {
471 /* we don't have an active codebook, find the codebook and
474 } else if (rtptheoradepay->config->ident != ident) {
475 /* codebook changed */
479 if (!gst_rtp_theora_depay_switch_codebook (rtptheoradepay, ident))
484 /* fragmented packets, assemble */
489 /* if we start a packet, clear adapter and start assembling. */
490 gst_adapter_clear (rtptheoradepay->adapter);
491 GST_DEBUG_OBJECT (depayload, "start assemble");
492 rtptheoradepay->assembling = TRUE;
495 if (!rtptheoradepay->assembling)
498 /* skip header and length. */
499 vdata = gst_rtp_buffer_get_payload_subbuffer (rtp, 6, -1);
501 GST_DEBUG_OBJECT (depayload, "assemble theora packet");
502 gst_adapter_push (rtptheoradepay->adapter, vdata);
504 /* packet is not complete, we are done */
508 /* construct assembled buffer */
509 length = gst_adapter_available (rtptheoradepay->adapter);
510 payload_buffer = gst_adapter_take_buffer (rtptheoradepay->adapter, length);
513 payload_buffer = gst_rtp_buffer_get_payload_subbuffer (rtp, 4, -1);
516 GST_DEBUG_OBJECT (depayload, "assemble done, payload_len %d", payload_len);
518 gst_buffer_map (payload_buffer, &map, GST_MAP_READ);
520 payload_len = map.size;
522 /* we not assembling anymore now */
523 rtptheoradepay->assembling = FALSE;
524 gst_adapter_clear (rtptheoradepay->adapter);
526 /* payload now points to a length with that many theora data bytes.
527 * Iterate over the packets and send them out.
530 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532 * | length | theora data ..
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
535 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * | length | next theora packet data ..
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*
541 while (payload_len >= 2) {
542 /* If length is not 0, we have a reassembled packet for which we
543 * calculated the length already and don't have to skip over the
544 * length field anymore
547 length = GST_READ_UINT16_BE (payload);
552 GST_DEBUG_OBJECT (depayload, "read length %u, avail: %d", length,
555 /* skip packet if something odd happens */
556 if (G_UNLIKELY (length > payload_len))
559 /* handle in-band configuration */
560 if (G_UNLIKELY (TDT == 1)) {
561 GST_DEBUG_OBJECT (rtptheoradepay, "in-band configuration");
562 if (!gst_rtp_theora_depay_parse_inband_configuration (rtptheoradepay,
563 ident, payload, payload_len, length))
564 goto invalid_configuration;
568 /* create buffer for packet */
570 gst_buffer_copy_region (payload_buffer, GST_BUFFER_COPY_ALL,
571 payload - map.data, length);
573 if (payload_len > 0 && (payload[0] & 0xC0) == 0x0) {
574 rtptheoradepay->needs_keyframe = FALSE;
576 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
580 payload_len -= length;
581 /* make sure to read next length */
584 ret = gst_rtp_base_depayload_push (depayload, outbuf);
585 if (ret != GST_FLOW_OK)
589 if (rtptheoradepay->needs_keyframe)
590 goto request_keyframe;
595 if (payload_buffer) {
596 gst_buffer_unmap (payload_buffer, &map);
597 gst_buffer_unref (payload_buffer);
605 GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE,
606 (NULL), ("Could not switch codebooks"));
611 GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE,
612 (NULL), ("Packet was too short (%d < 4)", payload_len));
613 goto request_keyframe;
617 GST_WARNING_OBJECT (rtptheoradepay, "reserved TDT ignored");
622 GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE,
623 (NULL), ("Packet contains invalid data"));
624 goto request_keyframe;
626 invalid_configuration:
628 /* fatal, as we otherwise risk carrying on without output */
629 GST_ELEMENT_ERROR (rtptheoradepay, STREAM, DECODE,
630 (NULL), ("Packet contains invalid configuration"));
635 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
636 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
637 gst_structure_new ("GstForceKeyUnit",
638 "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
643 rtptheoradepay->needs_keyframe = TRUE;
644 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
645 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
646 gst_structure_new_empty ("GstForceKeyUnit")));
651 static GstStateChangeReturn
652 gst_rtp_theora_depay_change_state (GstElement * element,
653 GstStateChange transition)
655 GstRtpTheoraDepay *rtptheoradepay;
656 GstStateChangeReturn ret;
658 rtptheoradepay = GST_RTP_THEORA_DEPAY (element);
660 switch (transition) {
661 case GST_STATE_CHANGE_NULL_TO_READY:
663 case GST_STATE_CHANGE_READY_TO_PAUSED:
669 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
671 switch (transition) {
672 case GST_STATE_CHANGE_PAUSED_TO_READY:
673 free_indents (rtptheoradepay);
675 case GST_STATE_CHANGE_READY_TO_NULL:
684 gst_rtp_theora_depay_plugin_init (GstPlugin * plugin)
686 return gst_element_register (plugin, "rtptheoradepay",
687 GST_RANK_SECONDARY, GST_TYPE_RTP_THEORA_DEPAY);
691 gst_rtp_theora_depay_packet_lost (GstRTPBaseDepayload * depayload,
694 GstRtpTheoraDepay *rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload);
697 gst_structure_get_uint (gst_event_get_structure (event), "seqnum", &seqnum);
698 GST_LOG_OBJECT (depayload, "Requested keyframe because frame with seqnum %u"
699 " is missing", seqnum);
700 rtptheoradepay->needs_keyframe = TRUE;
702 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depayload),
703 gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
704 gst_structure_new_empty ("GstForceKeyUnit")));