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
25 * Depayload an RTP-payloaded JPEG 2000 image into RTP packets according to RFC 5371
27 * For detailed information see: https://datatracker.ietf.org/doc/rfc5371/
28 * and https://datatracker.ietf.org/doc/rfc5372/
36 #include <gst/rtp/gstrtpbuffer.h>
37 #include <gst/video/video.h>
40 #include "gstrtpj2kcommon.h"
41 #include "gstrtpj2kdepay.h"
42 #include "gstrtputils.h"
44 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
45 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
47 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
48 GST_STATIC_PAD_TEMPLATE ("src",
51 GST_STATIC_CAPS ("image/x-jpc, "
52 "colorspace = (string) { sRGB, sYUV, GRAY }")
55 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("application/x-rtp, "
60 "media = (string) \"video\", " "clock-rate = (int) 90000, "
61 GST_RTP_J2K_SAMPLING_LIST ","
62 "encoding-name = (string) \"JPEG2000\";"
64 "media = (string) \"video\", " "clock-rate = (int) 90000, "
65 "colorspace = (string) { sRGB, sYUV, GRAY }, "
66 "encoding-name = (string) \"JPEG2000\";")
75 #define gst_rtp_j2k_depay_parent_class parent_class
76 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
78 static void gst_rtp_j2k_depay_finalize (GObject * object);
80 static void gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstStateChangeReturn
86 gst_rtp_j2k_depay_change_state (GstElement * element,
87 GstStateChange transition);
89 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
91 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
95 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
97 GObjectClass *gobject_class;
98 GstElementClass *gstelement_class;
99 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
101 gobject_class = (GObjectClass *) klass;
102 gstelement_class = (GstElementClass *) klass;
103 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
105 gobject_class->finalize = gst_rtp_j2k_depay_finalize;
107 gobject_class->set_property = gst_rtp_j2k_depay_set_property;
108 gobject_class->get_property = gst_rtp_j2k_depay_get_property;
110 gst_element_class_add_static_pad_template (gstelement_class,
111 &gst_rtp_j2k_depay_src_template);
112 gst_element_class_add_static_pad_template (gstelement_class,
113 &gst_rtp_j2k_depay_sink_template);
115 gst_element_class_set_static_metadata (gstelement_class,
116 "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
117 "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
118 "Wim Taymans <wim.taymans@gmail.com>");
120 gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
122 gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
123 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
125 GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
126 "J2K Video RTP Depayloader");
130 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
132 rtpj2kdepay->pu_adapter = gst_adapter_new ();
133 rtpj2kdepay->t_adapter = gst_adapter_new ();
134 rtpj2kdepay->f_adapter = gst_adapter_new ();
138 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
142 GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
144 if ((old = rtpj2kdepay->MH[idx]))
145 gst_buffer_unref (old);
146 rtpj2kdepay->MH[idx] = buf;
150 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
154 for (i = 0; i < 8; i++)
155 store_mheader (rtpj2kdepay, i, NULL);
159 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
161 clear_mheaders (rtpj2kdepay);
162 gst_adapter_clear (rtpj2kdepay->pu_adapter);
163 gst_adapter_clear (rtpj2kdepay->t_adapter);
164 gst_adapter_clear (rtpj2kdepay->f_adapter);
165 rtpj2kdepay->next_frag = 0;
169 gst_rtp_j2k_depay_finalize (GObject * object)
171 GstRtpJ2KDepay *rtpj2kdepay;
173 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
175 clear_mheaders (rtpj2kdepay);
177 g_object_unref (rtpj2kdepay->pu_adapter);
178 g_object_unref (rtpj2kdepay->t_adapter);
179 g_object_unref (rtpj2kdepay->f_adapter);
181 G_OBJECT_CLASS (parent_class)->finalize (object);
185 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
187 GstStructure *structure = NULL;
189 GstCaps *outcaps = NULL;
190 gboolean res = FALSE;
191 const gchar *colorspace = NULL;
192 const gchar *sampling = NULL;
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;
200 sampling = gst_structure_get_string (structure, "sampling");
202 if (!strcmp (sampling, GST_RTP_J2K_RGB) ||
203 !strcmp (sampling, GST_RTP_J2K_RGBA) ||
204 !strcmp (sampling, GST_RTP_J2K_BGR) ||
205 !strcmp (sampling, GST_RTP_J2K_BGRA))
207 else if (!strcmp (sampling, GST_RTP_J2K_GRAYSCALE))
212 GST_ELEMENT_WARNING (depayload, STREAM, DEMUX, NULL,
213 ("Non-compliant stream: sampling field missing. Frames my appear incorrect"));
214 colorspace = gst_structure_get_string (structure, "colorspace");
215 if (!strcmp (colorspace, "GRAY")) {
216 sampling = GST_RTP_J2K_GRAYSCALE;
220 outcaps = gst_caps_new_simple ("image/x-jpc",
221 "framerate", GST_TYPE_FRACTION, 0, 1,
222 "fields", G_TYPE_INT, 1, "colorspace", G_TYPE_STRING, colorspace, NULL);
225 gst_caps_set_simple (outcaps, "sampling", G_TYPE_STRING, sampling, NULL);
227 res = gst_pad_set_caps (depayload->srcpad, outcaps);
229 gst_caps_unref (outcaps);
235 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
237 gst_adapter_clear (rtpj2kdepay->pu_adapter);
238 rtpj2kdepay->have_sync = FALSE;
242 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
244 GstRtpJ2KDepay *rtpj2kdepay;
246 guint avail, MHF, mh_id;
248 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
250 /* take all available buffers */
251 avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
255 MHF = rtpj2kdepay->pu_MHF;
256 mh_id = rtpj2kdepay->last_mh_id;
258 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
261 GList *packets, *walk;
263 packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
265 for (walk = packets; walk; walk = g_list_next (walk)) {
266 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
267 GST_DEBUG_OBJECT (rtpj2kdepay,
268 "append pu packet of size %" G_GSIZE_FORMAT,
269 gst_buffer_get_size (buf));
270 gst_adapter_push (rtpj2kdepay->t_adapter, buf);
272 g_list_free (packets);
274 /* we have a header */
275 GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
276 /* we managed to see the start and end of the header, take all from
277 * adapter and keep in header */
278 mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
280 store_mheader (rtpj2kdepay, mh_id, mheader);
284 rtpj2kdepay->have_sync = FALSE;
290 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
292 GstRtpJ2KDepay *rtpj2kdepay;
294 GList *packets, *walk;
296 GstFlowReturn ret = GST_FLOW_OK;
300 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
302 /* flush pending PU */
303 gst_rtp_j2k_depay_flush_pu (depayload);
305 /* take all available buffers */
306 avail = gst_adapter_available (rtpj2kdepay->t_adapter);
310 mh_id = rtpj2kdepay->last_mh_id;
312 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
314 if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
317 /* we need a header now */
318 if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
321 /* push header in the adapter */
322 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
323 gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
326 /* check for last bytes */
327 gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
329 /* now append the tile packets to the frame */
330 packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
331 for (walk = packets; walk; walk = g_list_next (walk)) {
332 buf = GST_BUFFER_CAST (walk->data);
334 if (walk == packets) {
335 /* first buffer should contain the SOT */
336 gst_buffer_map (buf, &map, GST_MAP_READ);
341 if (map.data[0] == GST_J2K_MARKER && map.data[1] == GST_J2K_MARKER_SOT) {
344 if (end[0] == GST_J2K_MARKER && end[1] == GST_J2K_MARKER_EOC)
349 Psot = GST_READ_UINT32_BE (&map.data[6]);
350 if (Psot != nPsot && Psot != 0) {
351 /* Psot must match the size of the tile */
352 GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
353 gst_buffer_unmap (buf, &map);
355 buf = gst_buffer_make_writable (buf);
357 gst_buffer_map (buf, &map, GST_MAP_WRITE);
358 GST_WRITE_UINT32_BE (&map.data[6], nPsot);
361 gst_buffer_unmap (buf, &map);
364 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %" G_GSIZE_FORMAT,
365 gst_buffer_get_size (buf));
366 gst_adapter_push (rtpj2kdepay->f_adapter, buf);
368 g_list_free (packets);
371 rtpj2kdepay->last_tile = -1;
378 GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
379 gst_adapter_clear (rtpj2kdepay->t_adapter);
380 rtpj2kdepay->last_tile = -1;
385 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
386 gst_buffer_unmap (buf, &map);
387 gst_adapter_clear (rtpj2kdepay->t_adapter);
388 rtpj2kdepay->last_tile = -1;
394 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
396 GstRtpJ2KDepay *rtpj2kdepay;
400 GstFlowReturn ret = GST_FLOW_OK;
402 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
404 /* flush pending tile */
405 gst_rtp_j2k_depay_flush_tile (depayload);
407 /* last buffer take all data out of the adapter */
408 avail = gst_adapter_available (rtpj2kdepay->f_adapter);
415 /* take the last bytes of the JPEG 2000 data to see if there is an EOC
417 gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
419 if (end[0] != GST_J2K_MARKER && end[1] != GST_J2K_MARKER_EOC) {
420 end[0] = GST_J2K_MARKER;
421 end[1] = GST_J2K_MARKER_EOC;
423 GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
425 /* no EOI marker, add one */
426 outbuf = gst_buffer_new_and_alloc (2);
427 gst_buffer_fill (outbuf, 0, end, 2);
429 gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
433 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
434 outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
435 gst_rtp_drop_non_video_meta (depayload, outbuf);
436 ret = gst_rtp_base_depayload_push (depayload, outbuf);
438 GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
439 gst_adapter_clear (rtpj2kdepay->f_adapter);
442 /* we accept any mh_id now */
443 rtpj2kdepay->last_mh_id = -1;
446 rtpj2kdepay->next_frag = 0;
447 rtpj2kdepay->have_sync = FALSE;
450 /* we can't keep headers with mh_id of 0 */
451 store_mheader (rtpj2kdepay, 0, NULL);
457 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
459 GstRtpJ2KDepay *rtpj2kdepay;
461 guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
465 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
467 payload = gst_rtp_buffer_get_payload (rtp);
468 payload_len = gst_rtp_buffer_get_payload_len (rtp);
470 /* we need at least a header */
471 if (payload_len < GST_RTP_J2K_HEADER_SIZE)
474 rtptime = gst_rtp_buffer_get_timestamp (rtp);
476 /* new timestamp marks new frame */
477 if (rtpj2kdepay->last_rtptime != rtptime) {
478 rtpj2kdepay->last_rtptime = rtptime;
479 /* flush pending frame */
480 gst_rtp_j2k_depay_flush_frame (depayload);
485 * 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
486 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
487 * |tp |MHF|mh_id|T| priority | tile number |
488 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 * |reserved | fragment offset |
490 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
492 MHF = (payload[0] & 0x30) >> 4;
493 mh_id = (payload[0] & 0xe) >> 1;
495 if (rtpj2kdepay->last_mh_id == -1)
496 rtpj2kdepay->last_mh_id = mh_id;
497 else if (rtpj2kdepay->last_mh_id != mh_id)
500 tile = (payload[2] << 8) | payload[3];
501 frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
502 j2klen = payload_len - GST_RTP_J2K_HEADER_SIZE;
504 GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
505 tile, frag_offset, rtpj2kdepay->next_frag);
507 /* calculate the gap between expected frag */
508 gap = frag_offset - rtpj2kdepay->next_frag;
509 /* calculate next frag */
510 rtpj2kdepay->next_frag = frag_offset + j2klen;
513 GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
514 /* discont, clear pu adapter and resync */
515 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
518 /* check for sync code */
519 if (j2klen > 2 && payload[GST_RTP_J2K_HEADER_SIZE] == GST_J2K_MARKER) {
520 guint marker = payload[GST_RTP_J2K_HEADER_SIZE + 1];
522 /* packets must start with SOC, SOT or SOP */
524 case GST_J2K_MARKER_SOC:
525 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
526 /* flush the previous frame, should have happened when the timestamp
528 gst_rtp_j2k_depay_flush_frame (depayload);
529 rtpj2kdepay->have_sync = TRUE;
531 case GST_J2K_MARKER_SOT:
532 /* flush the previous tile */
533 gst_rtp_j2k_depay_flush_tile (depayload);
534 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
535 rtpj2kdepay->have_sync = TRUE;
536 /* we sync on the tile now */
537 rtpj2kdepay->last_tile = tile;
539 case GST_J2K_MARKER_SOP:
540 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
541 /* flush the previous PU */
542 gst_rtp_j2k_depay_flush_pu (depayload);
543 if (rtpj2kdepay->last_tile != tile) {
544 /* wrong tile, we lose sync and we need a new SOT or SOC to regain
545 * sync. First flush out the previous tile if we have one. */
546 if (rtpj2kdepay->last_tile != -1)
547 gst_rtp_j2k_depay_flush_tile (depayload);
548 /* now we have no more valid tile and no sync */
549 rtpj2kdepay->last_tile = -1;
550 rtpj2kdepay->have_sync = FALSE;
552 rtpj2kdepay->have_sync = TRUE;
556 GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
561 if (rtpj2kdepay->have_sync) {
564 if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
565 /* first part of pu, record state */
566 GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
567 rtpj2kdepay->pu_MHF = MHF;
569 /* and push in pu adapter */
570 GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
571 pu_frag = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
572 gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
575 /* last part of main header received, we can flush it */
576 GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
577 gst_rtp_j2k_depay_flush_pu (depayload);
580 GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
583 /* marker bit finishes the frame */
584 if (gst_rtp_buffer_get_marker (rtp)) {
585 GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
586 /* then flush frame */
587 gst_rtp_j2k_depay_flush_frame (depayload);
595 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
596 ("Empty Payload."), (NULL));
601 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
602 ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
604 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
610 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
611 const GValue * value, GParamSpec * pspec)
615 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
621 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
622 GValue * value, GParamSpec * pspec)
626 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
631 static GstStateChangeReturn
632 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
634 GstRtpJ2KDepay *rtpj2kdepay;
635 GstStateChangeReturn ret;
637 rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
639 switch (transition) {
640 case GST_STATE_CHANGE_NULL_TO_READY:
642 case GST_STATE_CHANGE_READY_TO_PAUSED:
643 gst_rtp_j2k_depay_reset (rtpj2kdepay);
649 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
651 switch (transition) {
652 case GST_STATE_CHANGE_PAUSED_TO_READY:
653 gst_rtp_j2k_depay_reset (rtpj2kdepay);
655 case GST_STATE_CHANGE_READY_TO_NULL:
664 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
666 return gst_element_register (plugin, "rtpj2kdepay",
667 GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);