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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpj2kdepay.h"
29 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
30 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
32 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
36 GST_STATIC_CAPS ("image/x-jpc")
39 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_STATIC_CAPS ("application/x-rtp, "
44 "media = (string) \"video\", "
45 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
46 "clock-rate = (int) 90000, " "encoding-name = (string) \"JPEG2000\"")
52 J2K_MARKER_SOC = 0x4F,
53 J2K_MARKER_SOT = 0x90,
54 J2K_MARKER_SOP = 0x91,
55 J2K_MARKER_SOD = 0x93,
59 #define DEFAULT_BUFFER_LIST TRUE
68 #define gst_rtp_j2k_depay_parent_class parent_class
69 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
71 static void gst_rtp_j2k_depay_finalize (GObject * object);
73 static void gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
74 const GValue * value, GParamSpec * pspec);
75 static void gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
76 GValue * value, GParamSpec * pspec);
78 static GstStateChangeReturn
79 gst_rtp_j2k_depay_change_state (GstElement * element,
80 GstStateChange transition);
82 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
84 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
88 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
90 GObjectClass *gobject_class;
91 GstElementClass *gstelement_class;
92 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
94 gobject_class = (GObjectClass *) klass;
95 gstelement_class = (GstElementClass *) klass;
96 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
98 gobject_class->finalize = gst_rtp_j2k_depay_finalize;
100 gobject_class->set_property = gst_rtp_j2k_depay_set_property;
101 gobject_class->get_property = gst_rtp_j2k_depay_get_property;
103 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
104 g_param_spec_boolean ("buffer-list", "Buffer List",
106 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108 gst_element_class_add_pad_template (gstelement_class,
109 gst_static_pad_template_get (&gst_rtp_j2k_depay_src_template));
110 gst_element_class_add_pad_template (gstelement_class,
111 gst_static_pad_template_get (&gst_rtp_j2k_depay_sink_template));
113 gst_element_class_set_details_simple (gstelement_class,
114 "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
115 "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
116 "Wim Taymans <wim.taymans@gmail.com>");
118 gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
120 gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
121 gstrtpbasedepayload_class->process = gst_rtp_j2k_depay_process;
123 GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
124 "J2K Video RTP Depayloader");
128 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
130 rtpj2kdepay->buffer_list = DEFAULT_BUFFER_LIST;
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;
192 structure = gst_caps_get_structure (caps, 0);
194 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
196 depayload->clock_rate = clock_rate;
199 gst_caps_new_simple ("image/x-jpc", "framerate", GST_TYPE_FRACTION, 0, 1,
200 "fields", G_TYPE_INT, 1, "colorspace", G_TYPE_STRING, "sYUV", NULL);
201 res = gst_pad_set_caps (depayload->srcpad, outcaps);
202 gst_caps_unref (outcaps);
208 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
210 gst_adapter_clear (rtpj2kdepay->pu_adapter);
211 rtpj2kdepay->have_sync = FALSE;
215 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
217 GstRtpJ2KDepay *rtpj2kdepay;
219 guint avail, MHF, mh_id;
221 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
223 /* take all available buffers */
224 avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
228 MHF = rtpj2kdepay->pu_MHF;
229 mh_id = rtpj2kdepay->last_mh_id;
231 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
234 GList *packets, *walk;
236 packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
238 for (walk = packets; walk; walk = g_list_next (walk)) {
239 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
240 GST_DEBUG_OBJECT (rtpj2kdepay,
241 "append pu packet of size %" G_GSIZE_FORMAT,
242 gst_buffer_get_size (buf));
243 gst_adapter_push (rtpj2kdepay->t_adapter, buf);
245 g_list_free (packets);
247 /* we have a header */
248 GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
249 /* we managed to see the start and end of the header, take all from
250 * adapter and keep in header */
251 mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
253 store_mheader (rtpj2kdepay, mh_id, mheader);
257 rtpj2kdepay->have_sync = FALSE;
263 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
265 GstRtpJ2KDepay *rtpj2kdepay;
267 GList *packets, *walk;
269 GstFlowReturn ret = GST_FLOW_OK;
274 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
276 /* flush pending PU */
277 gst_rtp_j2k_depay_flush_pu (depayload);
279 /* take all available buffers */
280 avail = gst_adapter_available (rtpj2kdepay->t_adapter);
284 mh_id = rtpj2kdepay->last_mh_id;
286 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
288 if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
291 /* we need a header now */
292 if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
295 /* push header in the adapter */
296 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
297 gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
300 /* check for last bytes */
301 gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
303 /* now append the tile packets to the frame */
304 packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
305 for (walk = packets; walk; walk = g_list_next (walk)) {
306 buf = GST_BUFFER_CAST (walk->data);
308 if (walk == packets) {
309 /* first buffer should contain the SOT */
310 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
315 if (data[0] == 0xff && data[1] == J2K_MARKER_SOT) {
318 if (end[0] == 0xff && end[1] == J2K_MARKER_EOC)
323 Psot = GST_READ_UINT32_BE (&data[6]);
324 if (Psot != nPsot && Psot != 0) {
325 /* Psot must match the size of the tile */
326 GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
327 gst_buffer_unmap (buf, data, size);
329 buf = gst_buffer_make_writable (buf);
331 data = gst_buffer_map (buf, &size, NULL, GST_MAP_WRITE);
332 GST_WRITE_UINT32_BE (&data[6], nPsot);
335 gst_buffer_unmap (buf, data, size);
338 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %" G_GSIZE_FORMAT,
339 gst_buffer_get_size (buf));
340 gst_adapter_push (rtpj2kdepay->f_adapter, buf);
342 g_list_free (packets);
345 rtpj2kdepay->last_tile = -1;
352 GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
353 gst_adapter_clear (rtpj2kdepay->t_adapter);
354 rtpj2kdepay->last_tile = -1;
359 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
360 gst_buffer_unmap (buf, data, size);
361 gst_adapter_clear (rtpj2kdepay->t_adapter);
362 rtpj2kdepay->last_tile = -1;
368 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
370 GstRtpJ2KDepay *rtpj2kdepay;
374 GstFlowReturn ret = GST_FLOW_OK;
376 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
378 /* flush pending tile */
379 gst_rtp_j2k_depay_flush_tile (depayload);
381 /* last buffer take all data out of the adapter */
382 avail = gst_adapter_available (rtpj2kdepay->f_adapter);
389 /* take the last bytes of the JPEG 2000 data to see if there is an EOC
391 gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
393 if (end[0] != 0xff && end[1] != 0xd9) {
397 GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
399 /* no EOI marker, add one */
400 outbuf = gst_buffer_new_and_alloc (2);
401 gst_buffer_fill (outbuf, 0, end, 2);
403 gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
407 if (rtpj2kdepay->buffer_list) {
409 GstBufferList *buflist;
410 GstBufferListIterator *it;
412 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer list of %u bytes", avail);
413 list = gst_adapter_take_list (rtpj2kdepay->f_adapter, avail);
415 buflist = gst_buffer_list_new ();
416 it = gst_buffer_list_iterate (buflist);
417 gst_buffer_list_iterator_add_group (it);
418 gst_buffer_list_iterator_add_list (it, list);
419 gst_buffer_list_iterator_free (it);
421 ret = gst_rtp_base_depayload_push_list (depayload, buflist);
425 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
426 outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
427 ret = gst_rtp_base_depayload_push (depayload, outbuf);
430 GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
431 gst_adapter_clear (rtpj2kdepay->f_adapter);
434 /* we accept any mh_id now */
435 rtpj2kdepay->last_mh_id = -1;
438 rtpj2kdepay->next_frag = 0;
439 rtpj2kdepay->have_sync = FALSE;
442 /* we can't keep headers with mh_id of 0 */
443 store_mheader (rtpj2kdepay, 0, NULL);
449 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
451 GstRtpJ2KDepay *rtpj2kdepay;
453 guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
456 GstRTPBuffer rtp = { NULL };
458 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
460 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
462 payload = gst_rtp_buffer_get_payload (&rtp);
463 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
465 /* we need at least a header */
469 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
471 /* new timestamp marks new frame */
472 if (rtpj2kdepay->last_rtptime != rtptime) {
473 rtpj2kdepay->last_rtptime = rtptime;
474 /* flush pending frame */
475 gst_rtp_j2k_depay_flush_frame (depayload);
480 * 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
481 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
482 * |tp |MHF|mh_id|T| priority | tile number |
483 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484 * |reserved | fragment offset |
485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
487 MHF = (payload[0] & 0x30) >> 4;
488 mh_id = (payload[0] & 0xe) >> 1;
490 if (rtpj2kdepay->last_mh_id == -1)
491 rtpj2kdepay->last_mh_id = mh_id;
492 else if (rtpj2kdepay->last_mh_id != mh_id)
495 tile = (payload[2] << 8) | payload[3];
496 frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
497 j2klen = payload_len - 8;
499 GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
500 tile, frag_offset, rtpj2kdepay->next_frag);
502 /* calculate the gap between expected frag */
503 gap = frag_offset - rtpj2kdepay->next_frag;
504 /* calculate next frag */
505 rtpj2kdepay->next_frag = frag_offset + j2klen;
508 GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
509 /* discont, clear pu adapter and resync */
510 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
513 /* check for sync code */
514 if (j2klen > 2 && payload[8] == 0xff) {
515 guint marker = payload[9];
517 /* packets must start with SOC, SOT or SOP */
520 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
521 /* flush the previous frame, should have happened when the timestamp
523 gst_rtp_j2k_depay_flush_frame (depayload);
524 rtpj2kdepay->have_sync = TRUE;
527 /* flush the previous tile */
528 gst_rtp_j2k_depay_flush_tile (depayload);
529 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
530 rtpj2kdepay->have_sync = TRUE;
531 /* we sync on the tile now */
532 rtpj2kdepay->last_tile = tile;
535 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
536 /* flush the previous PU */
537 gst_rtp_j2k_depay_flush_pu (depayload);
538 if (rtpj2kdepay->last_tile != tile) {
539 /* wrong tile, we lose sync and we need a new SOT or SOC to regain
540 * sync. First flush out the previous tile if we have one. */
541 if (rtpj2kdepay->last_tile != -1)
542 gst_rtp_j2k_depay_flush_tile (depayload);
543 /* now we have no more valid tile and no sync */
544 rtpj2kdepay->last_tile = -1;
545 rtpj2kdepay->have_sync = FALSE;
547 rtpj2kdepay->have_sync = TRUE;
551 GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
556 if (rtpj2kdepay->have_sync) {
559 if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
560 /* first part of pu, record state */
561 GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
562 rtpj2kdepay->pu_MHF = MHF;
564 /* and push in pu adapter */
565 GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
566 pu_frag = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
567 gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
570 /* last part of main header received, we can flush it */
571 GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
572 gst_rtp_j2k_depay_flush_pu (depayload);
575 GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
578 /* marker bit finishes the frame */
579 if (gst_rtp_buffer_get_marker (&rtp)) {
580 GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
581 /* then flush frame */
582 gst_rtp_j2k_depay_flush_frame (depayload);
584 gst_rtp_buffer_unmap (&rtp);
591 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
592 ("Empty Payload."), (NULL));
593 gst_rtp_buffer_unmap (&rtp);
598 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
599 ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
601 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
602 gst_rtp_buffer_unmap (&rtp);
608 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
609 const GValue * value, GParamSpec * pspec)
611 GstRtpJ2KDepay *rtpj2kdepay;
613 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
616 case PROP_BUFFER_LIST:
617 rtpj2kdepay->buffer_list = g_value_get_boolean (value);
620 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
626 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
627 GValue * value, GParamSpec * pspec)
629 GstRtpJ2KDepay *rtpj2kdepay;
631 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
634 case PROP_BUFFER_LIST:
635 g_value_set_boolean (value, rtpj2kdepay->buffer_list);
638 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643 static GstStateChangeReturn
644 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
646 GstRtpJ2KDepay *rtpj2kdepay;
647 GstStateChangeReturn ret;
649 rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
651 switch (transition) {
652 case GST_STATE_CHANGE_NULL_TO_READY:
654 case GST_STATE_CHANGE_READY_TO_PAUSED:
655 gst_rtp_j2k_depay_reset (rtpj2kdepay);
661 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
663 switch (transition) {
664 case GST_STATE_CHANGE_PAUSED_TO_READY:
665 gst_rtp_j2k_depay_reset (rtpj2kdepay);
667 case GST_STATE_CHANGE_READY_TO_NULL:
676 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
678 return gst_element_register (plugin, "rtpj2kdepay",
679 GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);