2 * Copyright (C) <2009> 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.
22 * SECTION:element-rtpj2kdepay
24 * Depayload an RTP-payloaded JPEG 2000 image into RTP packets according to RFC 5371
26 * For detailed information see: https://datatracker.ietf.org/doc/rfc5371/
27 * and https://datatracker.ietf.org/doc/rfc5372/
35 #include <gst/rtp/gstrtpbuffer.h>
36 #include <gst/video/video.h>
39 #include "gstrtpj2kdepay.h"
40 #include "gstrtputils.h"
42 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
43 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
45 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("image/x-jpc")
52 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("application/x-rtp, "
57 "media = (string) \"video\", "
58 "clock-rate = (int) 90000, " "encoding-name = (string) \"JPEG2000\"")
64 J2K_MARKER_SOC = 0x4F,
65 J2K_MARKER_SOT = 0x90,
66 J2K_MARKER_SOP = 0x91,
67 J2K_MARKER_SOD = 0x93,
77 #define gst_rtp_j2k_depay_parent_class parent_class
78 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
80 static void gst_rtp_j2k_depay_finalize (GObject * object);
82 static void gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
87 static GstStateChangeReturn
88 gst_rtp_j2k_depay_change_state (GstElement * element,
89 GstStateChange transition);
91 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
93 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
97 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
99 GObjectClass *gobject_class;
100 GstElementClass *gstelement_class;
101 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
103 gobject_class = (GObjectClass *) klass;
104 gstelement_class = (GstElementClass *) klass;
105 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
107 gobject_class->finalize = gst_rtp_j2k_depay_finalize;
109 gobject_class->set_property = gst_rtp_j2k_depay_set_property;
110 gobject_class->get_property = gst_rtp_j2k_depay_get_property;
112 gst_element_class_add_static_pad_template (gstelement_class,
113 &gst_rtp_j2k_depay_src_template);
114 gst_element_class_add_static_pad_template (gstelement_class,
115 &gst_rtp_j2k_depay_sink_template);
117 gst_element_class_set_static_metadata (gstelement_class,
118 "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
119 "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
120 "Wim Taymans <wim.taymans@gmail.com>");
122 gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
124 gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
125 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
127 GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
128 "J2K Video RTP Depayloader");
132 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
134 rtpj2kdepay->pu_adapter = gst_adapter_new ();
135 rtpj2kdepay->t_adapter = gst_adapter_new ();
136 rtpj2kdepay->f_adapter = gst_adapter_new ();
140 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
144 GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
146 if ((old = rtpj2kdepay->MH[idx]))
147 gst_buffer_unref (old);
148 rtpj2kdepay->MH[idx] = buf;
152 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
156 for (i = 0; i < 8; i++)
157 store_mheader (rtpj2kdepay, i, NULL);
161 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
163 clear_mheaders (rtpj2kdepay);
164 gst_adapter_clear (rtpj2kdepay->pu_adapter);
165 gst_adapter_clear (rtpj2kdepay->t_adapter);
166 gst_adapter_clear (rtpj2kdepay->f_adapter);
167 rtpj2kdepay->next_frag = 0;
171 gst_rtp_j2k_depay_finalize (GObject * object)
173 GstRtpJ2KDepay *rtpj2kdepay;
175 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
177 clear_mheaders (rtpj2kdepay);
179 g_object_unref (rtpj2kdepay->pu_adapter);
180 g_object_unref (rtpj2kdepay->t_adapter);
181 g_object_unref (rtpj2kdepay->f_adapter);
183 G_OBJECT_CLASS (parent_class)->finalize (object);
187 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
189 GstStructure *structure;
194 structure = gst_caps_get_structure (caps, 0);
196 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
198 depayload->clock_rate = clock_rate;
201 gst_caps_new_simple ("image/x-jpc", "framerate", GST_TYPE_FRACTION, 0, 1,
202 "fields", G_TYPE_INT, 1, "colorspace", G_TYPE_STRING, "sYUV", NULL);
203 res = gst_pad_set_caps (depayload->srcpad, outcaps);
204 gst_caps_unref (outcaps);
210 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
212 gst_adapter_clear (rtpj2kdepay->pu_adapter);
213 rtpj2kdepay->have_sync = FALSE;
217 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
219 GstRtpJ2KDepay *rtpj2kdepay;
221 guint avail, MHF, mh_id;
223 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
225 /* take all available buffers */
226 avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
230 MHF = rtpj2kdepay->pu_MHF;
231 mh_id = rtpj2kdepay->last_mh_id;
233 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
236 GList *packets, *walk;
238 packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
240 for (walk = packets; walk; walk = g_list_next (walk)) {
241 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
242 GST_DEBUG_OBJECT (rtpj2kdepay,
243 "append pu packet of size %" G_GSIZE_FORMAT,
244 gst_buffer_get_size (buf));
245 gst_adapter_push (rtpj2kdepay->t_adapter, buf);
247 g_list_free (packets);
249 /* we have a header */
250 GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
251 /* we managed to see the start and end of the header, take all from
252 * adapter and keep in header */
253 mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
255 store_mheader (rtpj2kdepay, mh_id, mheader);
259 rtpj2kdepay->have_sync = FALSE;
265 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
267 GstRtpJ2KDepay *rtpj2kdepay;
269 GList *packets, *walk;
271 GstFlowReturn ret = GST_FLOW_OK;
275 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
277 /* flush pending PU */
278 gst_rtp_j2k_depay_flush_pu (depayload);
280 /* take all available buffers */
281 avail = gst_adapter_available (rtpj2kdepay->t_adapter);
285 mh_id = rtpj2kdepay->last_mh_id;
287 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
289 if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
292 /* we need a header now */
293 if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
296 /* push header in the adapter */
297 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
298 gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
301 /* check for last bytes */
302 gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
304 /* now append the tile packets to the frame */
305 packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
306 for (walk = packets; walk; walk = g_list_next (walk)) {
307 buf = GST_BUFFER_CAST (walk->data);
309 if (walk == packets) {
310 /* first buffer should contain the SOT */
311 gst_buffer_map (buf, &map, GST_MAP_READ);
316 if (map.data[0] == 0xff && map.data[1] == J2K_MARKER_SOT) {
319 if (end[0] == 0xff && end[1] == J2K_MARKER_EOC)
324 Psot = GST_READ_UINT32_BE (&map.data[6]);
325 if (Psot != nPsot && Psot != 0) {
326 /* Psot must match the size of the tile */
327 GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
328 gst_buffer_unmap (buf, &map);
330 buf = gst_buffer_make_writable (buf);
332 gst_buffer_map (buf, &map, GST_MAP_WRITE);
333 GST_WRITE_UINT32_BE (&map.data[6], nPsot);
336 gst_buffer_unmap (buf, &map);
339 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %" G_GSIZE_FORMAT,
340 gst_buffer_get_size (buf));
341 gst_adapter_push (rtpj2kdepay->f_adapter, buf);
343 g_list_free (packets);
346 rtpj2kdepay->last_tile = -1;
353 GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
354 gst_adapter_clear (rtpj2kdepay->t_adapter);
355 rtpj2kdepay->last_tile = -1;
360 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
361 gst_buffer_unmap (buf, &map);
362 gst_adapter_clear (rtpj2kdepay->t_adapter);
363 rtpj2kdepay->last_tile = -1;
369 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
371 GstRtpJ2KDepay *rtpj2kdepay;
375 GstFlowReturn ret = GST_FLOW_OK;
377 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
379 /* flush pending tile */
380 gst_rtp_j2k_depay_flush_tile (depayload);
382 /* last buffer take all data out of the adapter */
383 avail = gst_adapter_available (rtpj2kdepay->f_adapter);
390 /* take the last bytes of the JPEG 2000 data to see if there is an EOC
392 gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
394 if (end[0] != 0xff && end[1] != 0xd9) {
398 GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
400 /* no EOI marker, add one */
401 outbuf = gst_buffer_new_and_alloc (2);
402 gst_buffer_fill (outbuf, 0, end, 2);
404 gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
408 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
409 outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
410 gst_rtp_drop_meta (GST_ELEMENT_CAST (depayload),
411 outbuf, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
412 ret = gst_rtp_base_depayload_push (depayload, outbuf);
414 GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
415 gst_adapter_clear (rtpj2kdepay->f_adapter);
418 /* we accept any mh_id now */
419 rtpj2kdepay->last_mh_id = -1;
422 rtpj2kdepay->next_frag = 0;
423 rtpj2kdepay->have_sync = FALSE;
426 /* we can't keep headers with mh_id of 0 */
427 store_mheader (rtpj2kdepay, 0, NULL);
433 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
435 GstRtpJ2KDepay *rtpj2kdepay;
437 guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
441 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
443 payload = gst_rtp_buffer_get_payload (rtp);
444 payload_len = gst_rtp_buffer_get_payload_len (rtp);
446 /* we need at least a header */
450 rtptime = gst_rtp_buffer_get_timestamp (rtp);
452 /* new timestamp marks new frame */
453 if (rtpj2kdepay->last_rtptime != rtptime) {
454 rtpj2kdepay->last_rtptime = rtptime;
455 /* flush pending frame */
456 gst_rtp_j2k_depay_flush_frame (depayload);
461 * 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
462 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
463 * |tp |MHF|mh_id|T| priority | tile number |
464 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
465 * |reserved | fragment offset |
466 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
468 MHF = (payload[0] & 0x30) >> 4;
469 mh_id = (payload[0] & 0xe) >> 1;
471 if (rtpj2kdepay->last_mh_id == -1)
472 rtpj2kdepay->last_mh_id = mh_id;
473 else if (rtpj2kdepay->last_mh_id != mh_id)
476 tile = (payload[2] << 8) | payload[3];
477 frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
478 j2klen = payload_len - 8;
480 GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
481 tile, frag_offset, rtpj2kdepay->next_frag);
483 /* calculate the gap between expected frag */
484 gap = frag_offset - rtpj2kdepay->next_frag;
485 /* calculate next frag */
486 rtpj2kdepay->next_frag = frag_offset + j2klen;
489 GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
490 /* discont, clear pu adapter and resync */
491 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
494 /* check for sync code */
495 if (j2klen > 2 && payload[8] == 0xff) {
496 guint marker = payload[9];
498 /* packets must start with SOC, SOT or SOP */
501 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
502 /* flush the previous frame, should have happened when the timestamp
504 gst_rtp_j2k_depay_flush_frame (depayload);
505 rtpj2kdepay->have_sync = TRUE;
508 /* flush the previous tile */
509 gst_rtp_j2k_depay_flush_tile (depayload);
510 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
511 rtpj2kdepay->have_sync = TRUE;
512 /* we sync on the tile now */
513 rtpj2kdepay->last_tile = tile;
516 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
517 /* flush the previous PU */
518 gst_rtp_j2k_depay_flush_pu (depayload);
519 if (rtpj2kdepay->last_tile != tile) {
520 /* wrong tile, we lose sync and we need a new SOT or SOC to regain
521 * sync. First flush out the previous tile if we have one. */
522 if (rtpj2kdepay->last_tile != -1)
523 gst_rtp_j2k_depay_flush_tile (depayload);
524 /* now we have no more valid tile and no sync */
525 rtpj2kdepay->last_tile = -1;
526 rtpj2kdepay->have_sync = FALSE;
528 rtpj2kdepay->have_sync = TRUE;
532 GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
537 if (rtpj2kdepay->have_sync) {
540 if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
541 /* first part of pu, record state */
542 GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
543 rtpj2kdepay->pu_MHF = MHF;
545 /* and push in pu adapter */
546 GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
547 pu_frag = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
548 gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
551 /* last part of main header received, we can flush it */
552 GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
553 gst_rtp_j2k_depay_flush_pu (depayload);
556 GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
559 /* marker bit finishes the frame */
560 if (gst_rtp_buffer_get_marker (rtp)) {
561 GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
562 /* then flush frame */
563 gst_rtp_j2k_depay_flush_frame (depayload);
571 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
572 ("Empty Payload."), (NULL));
577 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
578 ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
580 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
586 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
587 const GValue * value, GParamSpec * pspec)
591 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
597 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
598 GValue * value, GParamSpec * pspec)
602 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
607 static GstStateChangeReturn
608 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
610 GstRtpJ2KDepay *rtpj2kdepay;
611 GstStateChangeReturn ret;
613 rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
615 switch (transition) {
616 case GST_STATE_CHANGE_NULL_TO_READY:
618 case GST_STATE_CHANGE_READY_TO_PAUSED:
619 gst_rtp_j2k_depay_reset (rtpj2kdepay);
625 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
627 switch (transition) {
628 case GST_STATE_CHANGE_PAUSED_TO_READY:
629 gst_rtp_j2k_depay_reset (rtpj2kdepay);
631 case GST_STATE_CHANGE_READY_TO_NULL:
640 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
642 return gst_element_register (plugin, "rtpj2kdepay",
643 GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);