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 "gstrtpj2kcommon.h"
40 #include "gstrtpj2kdepay.h"
41 #include "gstrtputils.h"
43 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
44 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
46 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
50 GST_STATIC_CAPS ("image/x-jpc, "
51 "colorspace = (string) { sRGB, sYUV, GRAY }")
54 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS ("application/x-rtp, "
59 "media = (string) \"video\", " "clock-rate = (int) 90000, "
60 GST_RTP_J2K_SAMPLING_LIST ","
61 "encoding-name = (string) \"JPEG2000\";"
63 "media = (string) \"video\", " "clock-rate = (int) 90000, "
64 "colorspace = (string) { sRGB, sYUV, GRAY }, "
65 "encoding-name = (string) \"JPEG2000\";")
74 #define gst_rtp_j2k_depay_parent_class parent_class
75 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
77 static void gst_rtp_j2k_depay_finalize (GObject * object);
79 static void gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
81 static void gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
84 static GstStateChangeReturn
85 gst_rtp_j2k_depay_change_state (GstElement * element,
86 GstStateChange transition);
88 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
90 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
94 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
98 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
102 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
104 gobject_class->finalize = gst_rtp_j2k_depay_finalize;
106 gobject_class->set_property = gst_rtp_j2k_depay_set_property;
107 gobject_class->get_property = gst_rtp_j2k_depay_get_property;
109 gst_element_class_add_static_pad_template (gstelement_class,
110 &gst_rtp_j2k_depay_src_template);
111 gst_element_class_add_static_pad_template (gstelement_class,
112 &gst_rtp_j2k_depay_sink_template);
114 gst_element_class_set_static_metadata (gstelement_class,
115 "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
116 "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
117 "Wim Taymans <wim.taymans@gmail.com>");
119 gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
121 gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
122 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
124 GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
125 "J2K Video RTP Depayloader");
129 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
131 rtpj2kdepay->pu_adapter = gst_adapter_new ();
132 rtpj2kdepay->t_adapter = gst_adapter_new ();
133 rtpj2kdepay->f_adapter = gst_adapter_new ();
137 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
141 GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
143 if ((old = rtpj2kdepay->MH[idx]))
144 gst_buffer_unref (old);
145 rtpj2kdepay->MH[idx] = buf;
149 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
153 for (i = 0; i < 8; i++)
154 store_mheader (rtpj2kdepay, i, NULL);
158 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
160 clear_mheaders (rtpj2kdepay);
161 gst_adapter_clear (rtpj2kdepay->pu_adapter);
162 gst_adapter_clear (rtpj2kdepay->t_adapter);
163 gst_adapter_clear (rtpj2kdepay->f_adapter);
164 rtpj2kdepay->next_frag = 0;
168 gst_rtp_j2k_depay_finalize (GObject * object)
170 GstRtpJ2KDepay *rtpj2kdepay;
172 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
174 clear_mheaders (rtpj2kdepay);
176 g_object_unref (rtpj2kdepay->pu_adapter);
177 g_object_unref (rtpj2kdepay->t_adapter);
178 g_object_unref (rtpj2kdepay->f_adapter);
180 G_OBJECT_CLASS (parent_class)->finalize (object);
184 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
186 GstStructure *structure = NULL;
188 GstCaps *outcaps = NULL;
189 gboolean res = FALSE;
190 const gchar *colorspace = NULL;
191 const gchar *sampling = NULL;
193 structure = gst_caps_get_structure (caps, 0);
195 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
197 depayload->clock_rate = clock_rate;
199 sampling = gst_structure_get_string (structure, "sampling");
201 if (!strcmp (sampling, GST_RTP_J2K_RGB) ||
202 !strcmp (sampling, GST_RTP_J2K_RGBA) ||
203 !strcmp (sampling, GST_RTP_J2K_BGR) ||
204 !strcmp (sampling, GST_RTP_J2K_BGRA))
206 else if (!strcmp (sampling, GST_RTP_J2K_GRAYSCALE))
211 GST_ELEMENT_WARNING (depayload, STREAM, DEMUX, NULL,
212 ("Non-compliant stream: sampling field missing. Frames my appear incorrect"));
213 colorspace = gst_structure_get_string (structure, "colorspace");
214 if (!strcmp (colorspace, "GRAY")) {
215 sampling = GST_RTP_J2K_GRAYSCALE;
219 outcaps = gst_caps_new_simple ("image/x-jpc",
220 "framerate", GST_TYPE_FRACTION, 0, 1,
221 "fields", G_TYPE_INT, 1, "colorspace", G_TYPE_STRING, colorspace, NULL);
224 gst_caps_set_simple (outcaps, "sampling", G_TYPE_STRING, sampling, NULL);
226 res = gst_pad_set_caps (depayload->srcpad, outcaps);
228 gst_caps_unref (outcaps);
234 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
236 gst_adapter_clear (rtpj2kdepay->pu_adapter);
237 rtpj2kdepay->have_sync = FALSE;
241 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
243 GstRtpJ2KDepay *rtpj2kdepay;
245 guint avail, MHF, mh_id;
247 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
249 /* take all available buffers */
250 avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
254 MHF = rtpj2kdepay->pu_MHF;
255 mh_id = rtpj2kdepay->last_mh_id;
257 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
260 GList *packets, *walk;
262 packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
264 for (walk = packets; walk; walk = g_list_next (walk)) {
265 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
266 GST_DEBUG_OBJECT (rtpj2kdepay,
267 "append pu packet of size %" G_GSIZE_FORMAT,
268 gst_buffer_get_size (buf));
269 gst_adapter_push (rtpj2kdepay->t_adapter, buf);
271 g_list_free (packets);
273 /* we have a header */
274 GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
275 /* we managed to see the start and end of the header, take all from
276 * adapter and keep in header */
277 mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
279 store_mheader (rtpj2kdepay, mh_id, mheader);
283 rtpj2kdepay->have_sync = FALSE;
289 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
291 GstRtpJ2KDepay *rtpj2kdepay;
293 GList *packets, *walk;
295 GstFlowReturn ret = GST_FLOW_OK;
299 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
301 /* flush pending PU */
302 gst_rtp_j2k_depay_flush_pu (depayload);
304 /* take all available buffers */
305 avail = gst_adapter_available (rtpj2kdepay->t_adapter);
309 mh_id = rtpj2kdepay->last_mh_id;
311 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
313 if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
316 /* we need a header now */
317 if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
320 /* push header in the adapter */
321 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
322 gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
325 /* check for last bytes */
326 gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
328 /* now append the tile packets to the frame */
329 packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
330 for (walk = packets; walk; walk = g_list_next (walk)) {
331 buf = GST_BUFFER_CAST (walk->data);
333 if (walk == packets) {
334 /* first buffer should contain the SOT */
335 gst_buffer_map (buf, &map, GST_MAP_READ);
340 if (map.data[0] == GST_J2K_MARKER && map.data[1] == GST_J2K_MARKER_SOT) {
343 if (end[0] == GST_J2K_MARKER && end[1] == GST_J2K_MARKER_EOC)
348 Psot = GST_READ_UINT32_BE (&map.data[6]);
349 if (Psot != nPsot && Psot != 0) {
350 /* Psot must match the size of the tile */
351 GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
352 gst_buffer_unmap (buf, &map);
354 buf = gst_buffer_make_writable (buf);
356 gst_buffer_map (buf, &map, GST_MAP_WRITE);
357 GST_WRITE_UINT32_BE (&map.data[6], nPsot);
360 gst_buffer_unmap (buf, &map);
363 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %" G_GSIZE_FORMAT,
364 gst_buffer_get_size (buf));
365 gst_adapter_push (rtpj2kdepay->f_adapter, buf);
367 g_list_free (packets);
370 rtpj2kdepay->last_tile = -1;
377 GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
378 gst_adapter_clear (rtpj2kdepay->t_adapter);
379 rtpj2kdepay->last_tile = -1;
384 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
385 gst_buffer_unmap (buf, &map);
386 gst_adapter_clear (rtpj2kdepay->t_adapter);
387 rtpj2kdepay->last_tile = -1;
393 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
395 GstRtpJ2KDepay *rtpj2kdepay;
399 GstFlowReturn ret = GST_FLOW_OK;
401 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
403 /* flush pending tile */
404 gst_rtp_j2k_depay_flush_tile (depayload);
406 /* last buffer take all data out of the adapter */
407 avail = gst_adapter_available (rtpj2kdepay->f_adapter);
414 /* take the last bytes of the JPEG 2000 data to see if there is an EOC
416 gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
418 if (end[0] != GST_J2K_MARKER && end[1] != GST_J2K_MARKER_EOC) {
419 end[0] = GST_J2K_MARKER;
420 end[1] = GST_J2K_MARKER_EOC;
422 GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
424 /* no EOI marker, add one */
425 outbuf = gst_buffer_new_and_alloc (2);
426 gst_buffer_fill (outbuf, 0, end, 2);
428 gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
432 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
433 outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
434 gst_rtp_drop_non_video_meta (depayload, outbuf);
435 ret = gst_rtp_base_depayload_push (depayload, outbuf);
437 GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
438 gst_adapter_clear (rtpj2kdepay->f_adapter);
441 /* we accept any mh_id now */
442 rtpj2kdepay->last_mh_id = -1;
445 rtpj2kdepay->next_frag = 0;
446 rtpj2kdepay->have_sync = FALSE;
449 /* we can't keep headers with mh_id of 0 */
450 store_mheader (rtpj2kdepay, 0, NULL);
456 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
458 GstRtpJ2KDepay *rtpj2kdepay;
460 guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
464 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
466 payload = gst_rtp_buffer_get_payload (rtp);
467 payload_len = gst_rtp_buffer_get_payload_len (rtp);
469 /* we need at least a header */
470 if (payload_len < GST_RTP_J2K_HEADER_SIZE)
473 rtptime = gst_rtp_buffer_get_timestamp (rtp);
475 /* new timestamp marks new frame */
476 if (rtpj2kdepay->last_rtptime != rtptime) {
477 rtpj2kdepay->last_rtptime = rtptime;
478 /* flush pending frame */
479 gst_rtp_j2k_depay_flush_frame (depayload);
484 * 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
485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
486 * |tp |MHF|mh_id|T| priority | tile number |
487 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
488 * |reserved | fragment offset |
489 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491 MHF = (payload[0] & 0x30) >> 4;
492 mh_id = (payload[0] & 0xe) >> 1;
494 if (rtpj2kdepay->last_mh_id == -1)
495 rtpj2kdepay->last_mh_id = mh_id;
496 else if (rtpj2kdepay->last_mh_id != mh_id)
499 tile = (payload[2] << 8) | payload[3];
500 frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
501 j2klen = payload_len - GST_RTP_J2K_HEADER_SIZE;
503 GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
504 tile, frag_offset, rtpj2kdepay->next_frag);
506 /* calculate the gap between expected frag */
507 gap = frag_offset - rtpj2kdepay->next_frag;
508 /* calculate next frag */
509 rtpj2kdepay->next_frag = frag_offset + j2klen;
512 GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
513 /* discont, clear pu adapter and resync */
514 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
517 /* check for sync code */
518 if (j2klen > 2 && payload[GST_RTP_J2K_HEADER_SIZE] == GST_J2K_MARKER) {
519 guint marker = payload[GST_RTP_J2K_HEADER_SIZE + 1];
521 /* packets must start with SOC, SOT or SOP */
523 case GST_J2K_MARKER_SOC:
524 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
525 /* flush the previous frame, should have happened when the timestamp
527 gst_rtp_j2k_depay_flush_frame (depayload);
528 rtpj2kdepay->have_sync = TRUE;
530 case GST_J2K_MARKER_SOT:
531 /* flush the previous tile */
532 gst_rtp_j2k_depay_flush_tile (depayload);
533 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
534 rtpj2kdepay->have_sync = TRUE;
535 /* we sync on the tile now */
536 rtpj2kdepay->last_tile = tile;
538 case GST_J2K_MARKER_SOP:
539 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
540 /* flush the previous PU */
541 gst_rtp_j2k_depay_flush_pu (depayload);
542 if (rtpj2kdepay->last_tile != tile) {
543 /* wrong tile, we lose sync and we need a new SOT or SOC to regain
544 * sync. First flush out the previous tile if we have one. */
545 if (rtpj2kdepay->last_tile != -1)
546 gst_rtp_j2k_depay_flush_tile (depayload);
547 /* now we have no more valid tile and no sync */
548 rtpj2kdepay->last_tile = -1;
549 rtpj2kdepay->have_sync = FALSE;
551 rtpj2kdepay->have_sync = TRUE;
555 GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
560 if (rtpj2kdepay->have_sync) {
563 if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
564 /* first part of pu, record state */
565 GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
566 rtpj2kdepay->pu_MHF = MHF;
568 /* and push in pu adapter */
569 GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
570 pu_frag = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
571 gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
574 /* last part of main header received, we can flush it */
575 GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
576 gst_rtp_j2k_depay_flush_pu (depayload);
579 GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
582 /* marker bit finishes the frame */
583 if (gst_rtp_buffer_get_marker (rtp)) {
584 GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
585 /* then flush frame */
586 gst_rtp_j2k_depay_flush_frame (depayload);
594 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
595 ("Empty Payload."), (NULL));
600 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
601 ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
603 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
609 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
610 const GValue * value, GParamSpec * pspec)
614 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
620 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
621 GValue * value, GParamSpec * pspec)
625 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
630 static GstStateChangeReturn
631 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
633 GstRtpJ2KDepay *rtpj2kdepay;
634 GstStateChangeReturn ret;
636 rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
638 switch (transition) {
639 case GST_STATE_CHANGE_NULL_TO_READY:
641 case GST_STATE_CHANGE_READY_TO_PAUSED:
642 gst_rtp_j2k_depay_reset (rtpj2kdepay);
648 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
650 switch (transition) {
651 case GST_STATE_CHANGE_PAUSED_TO_READY:
652 gst_rtp_j2k_depay_reset (rtpj2kdepay);
654 case GST_STATE_CHANGE_READY_TO_NULL:
663 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
665 return gst_element_register (plugin, "rtpj2kdepay",
666 GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);