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 GST_BOILERPLATE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GstBaseRTPDepayload,
69 GST_TYPE_BASE_RTP_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 (GstBaseRTPDepayload * depayload,
84 static GstBuffer *gst_rtp_j2k_depay_process (GstBaseRTPDepayload * depayload,
88 gst_rtp_j2k_depay_base_init (gpointer klass)
90 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
92 gst_element_class_add_static_pad_template (element_class,
93 &gst_rtp_j2k_depay_src_template);
94 gst_element_class_add_static_pad_template (element_class,
95 &gst_rtp_j2k_depay_sink_template);
97 gst_element_class_set_details_simple (element_class,
98 "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
99 "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
100 "Wim Taymans <wim.taymans@gmail.com>");
104 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
106 GObjectClass *gobject_class;
107 GstElementClass *gstelement_class;
108 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
110 gobject_class = (GObjectClass *) klass;
111 gstelement_class = (GstElementClass *) klass;
112 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
114 gobject_class->finalize = gst_rtp_j2k_depay_finalize;
116 gobject_class->set_property = gst_rtp_j2k_depay_set_property;
117 gobject_class->get_property = gst_rtp_j2k_depay_get_property;
119 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
120 g_param_spec_boolean ("buffer-list", "Buffer List",
122 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124 gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
126 gstbasertpdepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
127 gstbasertpdepayload_class->process = gst_rtp_j2k_depay_process;
129 GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
130 "J2K Video RTP Depayloader");
134 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay,
135 GstRtpJ2KDepayClass * klass)
137 rtpj2kdepay->buffer_list = DEFAULT_BUFFER_LIST;
139 rtpj2kdepay->pu_adapter = gst_adapter_new ();
140 rtpj2kdepay->t_adapter = gst_adapter_new ();
141 rtpj2kdepay->f_adapter = gst_adapter_new ();
145 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
149 GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
151 if ((old = rtpj2kdepay->MH[idx]))
152 gst_buffer_unref (old);
153 rtpj2kdepay->MH[idx] = buf;
157 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
161 for (i = 0; i < 8; i++)
162 store_mheader (rtpj2kdepay, i, NULL);
166 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
168 clear_mheaders (rtpj2kdepay);
169 gst_adapter_clear (rtpj2kdepay->pu_adapter);
170 gst_adapter_clear (rtpj2kdepay->t_adapter);
171 gst_adapter_clear (rtpj2kdepay->f_adapter);
172 rtpj2kdepay->next_frag = 0;
176 gst_rtp_j2k_depay_finalize (GObject * object)
178 GstRtpJ2KDepay *rtpj2kdepay;
180 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
182 clear_mheaders (rtpj2kdepay);
184 g_object_unref (rtpj2kdepay->pu_adapter);
185 g_object_unref (rtpj2kdepay->t_adapter);
186 g_object_unref (rtpj2kdepay->f_adapter);
188 G_OBJECT_CLASS (parent_class)->finalize (object);
192 gst_rtp_j2k_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
194 GstStructure *structure;
199 structure = gst_caps_get_structure (caps, 0);
201 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
203 depayload->clock_rate = clock_rate;
206 gst_caps_new_simple ("image/x-jpc", "framerate", GST_TYPE_FRACTION, 0, 1,
207 "fields", G_TYPE_INT, 1, "fourcc", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('s',
208 'Y', 'U', 'V'), NULL);
209 res = gst_pad_set_caps (depayload->srcpad, outcaps);
210 gst_caps_unref (outcaps);
216 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
218 gst_adapter_clear (rtpj2kdepay->pu_adapter);
219 rtpj2kdepay->have_sync = FALSE;
223 gst_rtp_j2k_depay_flush_pu (GstBaseRTPDepayload * depayload)
225 GstRtpJ2KDepay *rtpj2kdepay;
227 guint avail, MHF, mh_id;
229 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
231 /* take all available buffers */
232 avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
236 MHF = rtpj2kdepay->pu_MHF;
237 mh_id = rtpj2kdepay->last_mh_id;
239 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
242 GList *packets, *walk;
244 packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
246 for (walk = packets; walk; walk = g_list_next (walk)) {
247 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
248 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %u",
249 GST_BUFFER_SIZE (buf));
250 gst_adapter_push (rtpj2kdepay->t_adapter, buf);
252 g_list_free (packets);
254 /* we have a header */
255 GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
256 /* we managed to see the start and end of the header, take all from
257 * adapter and keep in header */
258 mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
260 store_mheader (rtpj2kdepay, mh_id, mheader);
264 rtpj2kdepay->have_sync = FALSE;
270 gst_rtp_j2k_depay_flush_tile (GstBaseRTPDepayload * depayload)
272 GstRtpJ2KDepay *rtpj2kdepay;
274 GList *packets, *walk;
276 GstFlowReturn ret = GST_FLOW_OK;
278 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
280 /* flush pending PU */
281 gst_rtp_j2k_depay_flush_pu (depayload);
283 /* take all available buffers */
284 avail = gst_adapter_available (rtpj2kdepay->t_adapter);
288 mh_id = rtpj2kdepay->last_mh_id;
290 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
292 if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
295 /* we need a header now */
296 if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
299 /* push header in the adapter */
300 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
301 gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
304 /* check for last bytes */
305 gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
307 /* now append the tile packets to the frame */
308 packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
309 for (walk = packets; walk; walk = g_list_next (walk)) {
310 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
312 if (walk == packets) {
316 /* first buffer should contain the SOT */
317 data = GST_BUFFER_DATA (buf);
318 size = GST_BUFFER_SIZE (buf);
323 if (data[0] == 0xff && data[1] == J2K_MARKER_SOT) {
326 if (end[0] == 0xff && end[1] == J2K_MARKER_EOC)
331 Psot = GST_READ_UINT32_BE (&data[6]);
332 if (Psot != nPsot && Psot != 0) {
333 /* Psot must match the size of the tile */
334 GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
335 buf = gst_buffer_make_writable (buf);
336 data = GST_BUFFER_DATA (buf);
337 GST_WRITE_UINT32_BE (&data[6], nPsot);
342 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %u",
343 GST_BUFFER_SIZE (buf));
344 gst_adapter_push (rtpj2kdepay->f_adapter, buf);
346 g_list_free (packets);
349 rtpj2kdepay->last_tile = -1;
356 GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
357 gst_adapter_clear (rtpj2kdepay->t_adapter);
358 rtpj2kdepay->last_tile = -1;
363 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
364 gst_adapter_clear (rtpj2kdepay->t_adapter);
365 rtpj2kdepay->last_tile = -1;
371 gst_rtp_j2k_depay_flush_frame (GstBaseRTPDepayload * depayload)
373 GstRtpJ2KDepay *rtpj2kdepay;
378 GstFlowReturn ret = GST_FLOW_OK;
380 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
382 /* flush pending tile */
383 gst_rtp_j2k_depay_flush_tile (depayload);
385 /* last buffer take all data out of the adapter */
386 avail = gst_adapter_available (rtpj2kdepay->f_adapter);
393 /* take the last bytes of the JPEG 2000 data to see if there is an EOC
395 gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
397 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 data = GST_BUFFER_DATA (outbuf);
406 gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
410 if (rtpj2kdepay->buffer_list) {
412 GstBufferList *buflist;
413 GstBufferListIterator *it;
415 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer list of %u bytes", avail);
416 list = gst_adapter_take_list (rtpj2kdepay->f_adapter, avail);
418 buflist = gst_buffer_list_new ();
419 it = gst_buffer_list_iterate (buflist);
420 gst_buffer_list_iterator_add_group (it);
421 gst_buffer_list_iterator_add_list (it, list);
422 gst_buffer_list_iterator_free (it);
424 ret = gst_base_rtp_depayload_push_list (depayload, buflist);
426 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
427 outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
428 ret = gst_base_rtp_depayload_push (depayload, outbuf);
431 GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
432 gst_adapter_clear (rtpj2kdepay->f_adapter);
435 /* we accept any mh_id now */
436 rtpj2kdepay->last_mh_id = -1;
439 rtpj2kdepay->next_frag = 0;
440 rtpj2kdepay->have_sync = FALSE;
443 /* we can't keep headers with mh_id of 0 */
444 store_mheader (rtpj2kdepay, 0, NULL);
450 gst_rtp_j2k_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
452 GstRtpJ2KDepay *rtpj2kdepay;
454 guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
458 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
460 payload = gst_rtp_buffer_get_payload (buf);
461 payload_len = gst_rtp_buffer_get_payload_len (buf);
463 /* we need at least a header */
467 rtptime = gst_rtp_buffer_get_timestamp (buf);
469 /* new timestamp marks new frame */
470 if (rtpj2kdepay->last_rtptime != rtptime) {
471 rtpj2kdepay->last_rtptime = rtptime;
472 /* flush pending frame */
473 gst_rtp_j2k_depay_flush_frame (depayload);
478 * 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
479 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
480 * |tp |MHF|mh_id|T| priority | tile number |
481 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
482 * |reserved | fragment offset |
483 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
485 MHF = (payload[0] & 0x30) >> 4;
486 mh_id = (payload[0] & 0xe) >> 1;
488 if (rtpj2kdepay->last_mh_id == -1)
489 rtpj2kdepay->last_mh_id = mh_id;
490 else if (rtpj2kdepay->last_mh_id != mh_id)
493 tile = (payload[2] << 8) | payload[3];
494 frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
495 j2klen = payload_len - 8;
497 GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
498 tile, frag_offset, rtpj2kdepay->next_frag);
500 /* calculate the gap between expected frag */
501 gap = frag_offset - rtpj2kdepay->next_frag;
502 /* calculate next frag */
503 rtpj2kdepay->next_frag = frag_offset + j2klen;
506 GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
507 /* discont, clear pu adapter and resync */
508 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
511 /* check for sync code */
512 if (j2klen > 2 && payload[8] == 0xff) {
513 guint marker = payload[9];
515 /* packets must start with SOC, SOT or SOP */
518 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
519 /* flush the previous frame, should have happened when the timestamp
521 gst_rtp_j2k_depay_flush_frame (depayload);
522 rtpj2kdepay->have_sync = TRUE;
525 /* flush the previous tile */
526 gst_rtp_j2k_depay_flush_tile (depayload);
527 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
528 rtpj2kdepay->have_sync = TRUE;
529 /* we sync on the tile now */
530 rtpj2kdepay->last_tile = tile;
533 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
534 /* flush the previous PU */
535 gst_rtp_j2k_depay_flush_pu (depayload);
536 if (rtpj2kdepay->last_tile != tile) {
537 /* wrong tile, we lose sync and we need a new SOT or SOC to regain
538 * sync. First flush out the previous tile if we have one. */
539 if (rtpj2kdepay->last_tile != -1)
540 gst_rtp_j2k_depay_flush_tile (depayload);
541 /* now we have no more valid tile and no sync */
542 rtpj2kdepay->last_tile = -1;
543 rtpj2kdepay->have_sync = FALSE;
545 rtpj2kdepay->have_sync = TRUE;
549 GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
554 if (rtpj2kdepay->have_sync) {
557 if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
558 /* first part of pu, record state */
559 GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
560 rtpj2kdepay->pu_MHF = MHF;
562 /* and push in pu adapter */
563 GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
564 pu_frag = gst_rtp_buffer_get_payload_subbuffer (buf, 8, -1);
565 gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
568 /* last part of main header received, we can flush it */
569 GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
570 gst_rtp_j2k_depay_flush_pu (depayload);
573 GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
576 /* marker bit finishes the frame */
577 if (gst_rtp_buffer_get_marker (buf)) {
578 GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
579 /* then flush frame */
580 gst_rtp_j2k_depay_flush_frame (depayload);
587 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
588 ("Empty Payload."), (NULL));
593 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
594 ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
596 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
602 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
603 const GValue * value, GParamSpec * pspec)
605 GstRtpJ2KDepay *rtpj2kdepay;
607 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
610 case PROP_BUFFER_LIST:
611 rtpj2kdepay->buffer_list = g_value_get_boolean (value);
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)
623 GstRtpJ2KDepay *rtpj2kdepay;
625 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
628 case PROP_BUFFER_LIST:
629 g_value_set_boolean (value, rtpj2kdepay->buffer_list);
632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
637 static GstStateChangeReturn
638 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
640 GstRtpJ2KDepay *rtpj2kdepay;
641 GstStateChangeReturn ret;
643 rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
645 switch (transition) {
646 case GST_STATE_CHANGE_NULL_TO_READY:
648 case GST_STATE_CHANGE_READY_TO_PAUSED:
649 gst_rtp_j2k_depay_reset (rtpj2kdepay);
655 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
657 switch (transition) {
658 case GST_STATE_CHANGE_PAUSED_TO_READY:
659 gst_rtp_j2k_depay_reset (rtpj2kdepay);
661 case GST_STATE_CHANGE_READY_TO_NULL:
670 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
672 return gst_element_register (plugin, "rtpj2kdepay",
673 GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);