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 "gstrtpelements.h"
41 #include "gstrtpj2kcommon.h"
42 #include "gstrtpj2kdepay.h"
43 #include "gstrtputils.h"
45 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
46 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
48 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
52 GST_STATIC_CAPS ("image/x-jpc, "
53 "colorspace = (string) { sRGB, sYUV, GRAY }")
56 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("application/x-rtp, "
61 "media = (string) \"video\", " "clock-rate = (int) 90000, "
62 GST_RTP_J2K_SAMPLING_LIST ","
63 "encoding-name = (string) \"JPEG2000\";"
65 "media = (string) \"video\", " "clock-rate = (int) 90000, "
66 "colorspace = (string) { sRGB, sYUV, GRAY }, "
67 "encoding-name = (string) \"JPEG2000\";")
76 #define gst_rtp_j2k_depay_parent_class parent_class
77 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
78 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpj2kdepay, "rtpj2kdepay",
79 GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY, rtp_element_init (plugin));
81 static void gst_rtp_j2k_depay_finalize (GObject * object);
83 static void gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
88 static GstStateChangeReturn
89 gst_rtp_j2k_depay_change_state (GstElement * element,
90 GstStateChange transition);
92 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
94 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
98 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
100 GObjectClass *gobject_class;
101 GstElementClass *gstelement_class;
102 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
104 gobject_class = (GObjectClass *) klass;
105 gstelement_class = (GstElementClass *) klass;
106 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
108 gobject_class->finalize = gst_rtp_j2k_depay_finalize;
110 gobject_class->set_property = gst_rtp_j2k_depay_set_property;
111 gobject_class->get_property = gst_rtp_j2k_depay_get_property;
113 gst_element_class_add_static_pad_template (gstelement_class,
114 &gst_rtp_j2k_depay_src_template);
115 gst_element_class_add_static_pad_template (gstelement_class,
116 &gst_rtp_j2k_depay_sink_template);
118 gst_element_class_set_static_metadata (gstelement_class,
119 "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
120 "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
121 "Wim Taymans <wim.taymans@gmail.com>");
123 gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
125 gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
126 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
128 GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
129 "J2K Video RTP Depayloader");
133 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
135 rtpj2kdepay->pu_adapter = gst_adapter_new ();
136 rtpj2kdepay->t_adapter = gst_adapter_new ();
137 rtpj2kdepay->f_adapter = gst_adapter_new ();
141 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
145 GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
147 if ((old = rtpj2kdepay->MH[idx]))
148 gst_buffer_unref (old);
149 rtpj2kdepay->MH[idx] = buf;
153 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
157 for (i = 0; i < 8; i++)
158 store_mheader (rtpj2kdepay, i, NULL);
162 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
164 clear_mheaders (rtpj2kdepay);
165 gst_adapter_clear (rtpj2kdepay->pu_adapter);
166 gst_adapter_clear (rtpj2kdepay->t_adapter);
167 gst_adapter_clear (rtpj2kdepay->f_adapter);
168 rtpj2kdepay->next_frag = 0;
172 gst_rtp_j2k_depay_finalize (GObject * object)
174 GstRtpJ2KDepay *rtpj2kdepay;
176 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
178 clear_mheaders (rtpj2kdepay);
180 g_object_unref (rtpj2kdepay->pu_adapter);
181 g_object_unref (rtpj2kdepay->t_adapter);
182 g_object_unref (rtpj2kdepay->f_adapter);
184 G_OBJECT_CLASS (parent_class)->finalize (object);
188 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
190 GstStructure *structure = NULL;
192 GstCaps *outcaps = NULL;
193 gboolean res = FALSE;
194 const gchar *colorspace = NULL;
195 const gchar *sampling = NULL;
197 structure = gst_caps_get_structure (caps, 0);
199 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
201 depayload->clock_rate = clock_rate;
203 sampling = gst_structure_get_string (structure, "sampling");
205 if (!strcmp (sampling, GST_RTP_J2K_RGB) ||
206 !strcmp (sampling, GST_RTP_J2K_RGBA) ||
207 !strcmp (sampling, GST_RTP_J2K_BGR) ||
208 !strcmp (sampling, GST_RTP_J2K_BGRA))
210 else if (!strcmp (sampling, GST_RTP_J2K_GRAYSCALE))
215 GST_ELEMENT_WARNING (depayload, STREAM, DEMUX, NULL,
216 ("Non-compliant stream: sampling field missing. Frames my appear incorrect"));
217 colorspace = gst_structure_get_string (structure, "colorspace");
218 if (!strcmp (colorspace, "GRAY")) {
219 sampling = GST_RTP_J2K_GRAYSCALE;
223 outcaps = gst_caps_new_simple ("image/x-jpc",
224 "framerate", GST_TYPE_FRACTION, 0, 1,
225 "fields", G_TYPE_INT, 1, "colorspace", G_TYPE_STRING, colorspace, NULL);
228 gst_caps_set_simple (outcaps, "sampling", G_TYPE_STRING, sampling, NULL);
230 res = gst_pad_set_caps (depayload->srcpad, outcaps);
232 gst_caps_unref (outcaps);
238 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
240 gst_adapter_clear (rtpj2kdepay->pu_adapter);
241 rtpj2kdepay->have_sync = FALSE;
245 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
247 GstRtpJ2KDepay *rtpj2kdepay;
249 guint avail, MHF, mh_id;
251 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
253 /* take all available buffers */
254 avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
258 MHF = rtpj2kdepay->pu_MHF;
259 mh_id = rtpj2kdepay->last_mh_id;
261 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
264 GList *packets, *walk;
266 packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
268 for (walk = packets; walk; walk = g_list_next (walk)) {
269 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
270 GST_DEBUG_OBJECT (rtpj2kdepay,
271 "append pu packet of size %" G_GSIZE_FORMAT,
272 gst_buffer_get_size (buf));
273 gst_adapter_push (rtpj2kdepay->t_adapter, buf);
275 g_list_free (packets);
277 /* we have a header */
278 GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
279 /* we managed to see the start and end of the header, take all from
280 * adapter and keep in header */
281 mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
283 store_mheader (rtpj2kdepay, mh_id, mheader);
287 rtpj2kdepay->have_sync = FALSE;
293 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
295 GstRtpJ2KDepay *rtpj2kdepay;
297 GList *packets, *walk;
299 GstFlowReturn ret = GST_FLOW_OK;
303 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
305 /* flush pending PU */
306 gst_rtp_j2k_depay_flush_pu (depayload);
308 /* take all available buffers */
309 avail = gst_adapter_available (rtpj2kdepay->t_adapter);
313 mh_id = rtpj2kdepay->last_mh_id;
315 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
317 if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
320 /* we need a header now */
321 if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
324 /* push header in the adapter */
325 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
326 gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
329 /* check for last bytes */
330 gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
332 /* now append the tile packets to the frame */
333 packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
334 for (walk = packets; walk; walk = g_list_next (walk)) {
335 buf = GST_BUFFER_CAST (walk->data);
337 if (walk == packets) {
338 /* first buffer should contain the SOT */
339 gst_buffer_map (buf, &map, GST_MAP_READ);
344 if (map.data[0] == GST_J2K_MARKER && map.data[1] == GST_J2K_MARKER_SOT) {
347 if (end[0] == GST_J2K_MARKER && end[1] == GST_J2K_MARKER_EOC)
352 Psot = GST_READ_UINT32_BE (&map.data[6]);
353 if (Psot != nPsot && Psot != 0) {
354 /* Psot must match the size of the tile */
355 GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
356 gst_buffer_unmap (buf, &map);
358 buf = gst_buffer_make_writable (buf);
360 gst_buffer_map (buf, &map, GST_MAP_WRITE);
361 GST_WRITE_UINT32_BE (&map.data[6], nPsot);
364 gst_buffer_unmap (buf, &map);
367 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %" G_GSIZE_FORMAT,
368 gst_buffer_get_size (buf));
369 gst_adapter_push (rtpj2kdepay->f_adapter, buf);
371 g_list_free (packets);
374 rtpj2kdepay->last_tile = -1;
381 GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
382 gst_adapter_clear (rtpj2kdepay->t_adapter);
383 rtpj2kdepay->last_tile = -1;
388 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
389 gst_buffer_unmap (buf, &map);
390 gst_adapter_clear (rtpj2kdepay->t_adapter);
391 rtpj2kdepay->last_tile = -1;
397 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
399 GstRtpJ2KDepay *rtpj2kdepay;
403 GstFlowReturn ret = GST_FLOW_OK;
405 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
407 /* flush pending tile */
408 gst_rtp_j2k_depay_flush_tile (depayload);
410 /* last buffer take all data out of the adapter */
411 avail = gst_adapter_available (rtpj2kdepay->f_adapter);
418 /* take the last bytes of the JPEG 2000 data to see if there is an EOC
420 gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
422 if (end[0] != GST_J2K_MARKER && end[1] != GST_J2K_MARKER_EOC) {
423 end[0] = GST_J2K_MARKER;
424 end[1] = GST_J2K_MARKER_EOC;
426 GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
428 /* no EOI marker, add one */
429 outbuf = gst_buffer_new_and_alloc (2);
430 gst_buffer_fill (outbuf, 0, end, 2);
432 gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
436 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
437 outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
438 gst_rtp_drop_non_video_meta (depayload, outbuf);
439 ret = gst_rtp_base_depayload_push (depayload, outbuf);
441 GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
442 gst_adapter_clear (rtpj2kdepay->f_adapter);
445 /* we accept any mh_id now */
446 rtpj2kdepay->last_mh_id = -1;
449 rtpj2kdepay->next_frag = 0;
450 rtpj2kdepay->have_sync = FALSE;
453 /* we can't keep headers with mh_id of 0 */
454 store_mheader (rtpj2kdepay, 0, NULL);
460 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
462 GstRtpJ2KDepay *rtpj2kdepay;
464 guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
468 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
470 payload = gst_rtp_buffer_get_payload (rtp);
471 payload_len = gst_rtp_buffer_get_payload_len (rtp);
473 /* we need at least a header */
474 if (payload_len < GST_RTP_J2K_HEADER_SIZE)
477 rtptime = gst_rtp_buffer_get_timestamp (rtp);
479 /* new timestamp marks new frame */
480 if (rtpj2kdepay->last_rtptime != rtptime) {
481 rtpj2kdepay->last_rtptime = rtptime;
482 /* flush pending frame */
483 gst_rtp_j2k_depay_flush_frame (depayload);
488 * 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
489 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
490 * |tp |MHF|mh_id|T| priority | tile number |
491 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
492 * |reserved | fragment offset |
493 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
495 MHF = (payload[0] & 0x30) >> 4;
496 mh_id = (payload[0] & 0xe) >> 1;
498 if (rtpj2kdepay->last_mh_id == -1)
499 rtpj2kdepay->last_mh_id = mh_id;
500 else if (rtpj2kdepay->last_mh_id != mh_id)
503 tile = (payload[2] << 8) | payload[3];
504 frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
505 j2klen = payload_len - GST_RTP_J2K_HEADER_SIZE;
507 GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
508 tile, frag_offset, rtpj2kdepay->next_frag);
510 /* calculate the gap between expected frag */
511 gap = frag_offset - rtpj2kdepay->next_frag;
512 /* calculate next frag */
513 rtpj2kdepay->next_frag = frag_offset + j2klen;
516 GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
517 /* discont, clear pu adapter and resync */
518 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
521 /* check for sync code */
522 if (j2klen > 2 && payload[GST_RTP_J2K_HEADER_SIZE] == GST_J2K_MARKER) {
523 guint marker = payload[GST_RTP_J2K_HEADER_SIZE + 1];
525 /* packets must start with SOC, SOT or SOP */
527 case GST_J2K_MARKER_SOC:
528 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
529 /* flush the previous frame, should have happened when the timestamp
531 gst_rtp_j2k_depay_flush_frame (depayload);
532 rtpj2kdepay->have_sync = TRUE;
534 case GST_J2K_MARKER_SOT:
535 /* flush the previous tile */
536 gst_rtp_j2k_depay_flush_tile (depayload);
537 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
538 rtpj2kdepay->have_sync = TRUE;
539 /* we sync on the tile now */
540 rtpj2kdepay->last_tile = tile;
542 case GST_J2K_MARKER_SOP:
543 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
544 /* flush the previous PU */
545 gst_rtp_j2k_depay_flush_pu (depayload);
546 if (rtpj2kdepay->last_tile != tile) {
547 /* wrong tile, we lose sync and we need a new SOT or SOC to regain
548 * sync. First flush out the previous tile if we have one. */
549 if (rtpj2kdepay->last_tile != -1)
550 gst_rtp_j2k_depay_flush_tile (depayload);
551 /* now we have no more valid tile and no sync */
552 rtpj2kdepay->last_tile = -1;
553 rtpj2kdepay->have_sync = FALSE;
555 rtpj2kdepay->have_sync = TRUE;
559 GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
564 if (rtpj2kdepay->have_sync) {
567 if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
568 /* first part of pu, record state */
569 GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
570 rtpj2kdepay->pu_MHF = MHF;
572 /* and push in pu adapter */
573 GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
574 pu_frag = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
575 gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
578 /* last part of main header received, we can flush it */
579 GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
580 gst_rtp_j2k_depay_flush_pu (depayload);
583 GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
586 /* marker bit finishes the frame */
587 if (gst_rtp_buffer_get_marker (rtp)) {
588 GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
589 /* then flush frame */
590 gst_rtp_j2k_depay_flush_frame (depayload);
598 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
599 ("Empty Payload."), (NULL));
604 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
605 ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
607 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
613 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
614 const GValue * value, GParamSpec * pspec)
618 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
624 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
625 GValue * value, GParamSpec * pspec)
629 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
634 static GstStateChangeReturn
635 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
637 GstRtpJ2KDepay *rtpj2kdepay;
638 GstStateChangeReturn ret;
640 rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
642 switch (transition) {
643 case GST_STATE_CHANGE_NULL_TO_READY:
645 case GST_STATE_CHANGE_READY_TO_PAUSED:
646 gst_rtp_j2k_depay_reset (rtpj2kdepay);
652 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
654 switch (transition) {
655 case GST_STATE_CHANGE_PAUSED_TO_READY:
656 gst_rtp_j2k_depay_reset (rtpj2kdepay);
658 case GST_STATE_CHANGE_READY_TO_NULL: