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.
24 #include <gst/rtp/gstrtpbuffer.h>
25 #include <gst/video/video.h>
28 #include "gstrtpj2kdepay.h"
29 #include "gstrtputils.h"
31 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
32 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
34 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
38 GST_STATIC_CAPS ("image/x-jpc")
41 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("application/x-rtp, "
46 "media = (string) \"video\", "
47 "clock-rate = (int) 90000, " "encoding-name = (string) \"JPEG2000\"")
53 J2K_MARKER_SOC = 0x4F,
54 J2K_MARKER_SOT = 0x90,
55 J2K_MARKER_SOP = 0x91,
56 J2K_MARKER_SOD = 0x93,
66 #define gst_rtp_j2k_depay_parent_class parent_class
67 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
69 static void gst_rtp_j2k_depay_finalize (GObject * object);
71 static void gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
72 const GValue * value, GParamSpec * pspec);
73 static void gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
76 static GstStateChangeReturn
77 gst_rtp_j2k_depay_change_state (GstElement * element,
78 GstStateChange transition);
80 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
82 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
86 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
90 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
92 gobject_class = (GObjectClass *) klass;
93 gstelement_class = (GstElementClass *) klass;
94 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
96 gobject_class->finalize = gst_rtp_j2k_depay_finalize;
98 gobject_class->set_property = gst_rtp_j2k_depay_set_property;
99 gobject_class->get_property = gst_rtp_j2k_depay_get_property;
101 gst_element_class_add_static_pad_template (gstelement_class,
102 &gst_rtp_j2k_depay_src_template);
103 gst_element_class_add_static_pad_template (gstelement_class,
104 &gst_rtp_j2k_depay_sink_template);
106 gst_element_class_set_static_metadata (gstelement_class,
107 "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
108 "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
109 "Wim Taymans <wim.taymans@gmail.com>");
111 gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
113 gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
114 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
116 GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
117 "J2K Video RTP Depayloader");
121 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
123 rtpj2kdepay->pu_adapter = gst_adapter_new ();
124 rtpj2kdepay->t_adapter = gst_adapter_new ();
125 rtpj2kdepay->f_adapter = gst_adapter_new ();
129 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
133 GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
135 if ((old = rtpj2kdepay->MH[idx]))
136 gst_buffer_unref (old);
137 rtpj2kdepay->MH[idx] = buf;
141 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
145 for (i = 0; i < 8; i++)
146 store_mheader (rtpj2kdepay, i, NULL);
150 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
152 clear_mheaders (rtpj2kdepay);
153 gst_adapter_clear (rtpj2kdepay->pu_adapter);
154 gst_adapter_clear (rtpj2kdepay->t_adapter);
155 gst_adapter_clear (rtpj2kdepay->f_adapter);
156 rtpj2kdepay->next_frag = 0;
160 gst_rtp_j2k_depay_finalize (GObject * object)
162 GstRtpJ2KDepay *rtpj2kdepay;
164 rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
166 clear_mheaders (rtpj2kdepay);
168 g_object_unref (rtpj2kdepay->pu_adapter);
169 g_object_unref (rtpj2kdepay->t_adapter);
170 g_object_unref (rtpj2kdepay->f_adapter);
172 G_OBJECT_CLASS (parent_class)->finalize (object);
176 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
178 GstStructure *structure;
183 structure = gst_caps_get_structure (caps, 0);
185 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
187 depayload->clock_rate = clock_rate;
190 gst_caps_new_simple ("image/x-jpc", "framerate", GST_TYPE_FRACTION, 0, 1,
191 "fields", G_TYPE_INT, 1, "colorspace", G_TYPE_STRING, "sYUV", NULL);
192 res = gst_pad_set_caps (depayload->srcpad, outcaps);
193 gst_caps_unref (outcaps);
199 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
201 gst_adapter_clear (rtpj2kdepay->pu_adapter);
202 rtpj2kdepay->have_sync = FALSE;
206 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
208 GstRtpJ2KDepay *rtpj2kdepay;
210 guint avail, MHF, mh_id;
212 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
214 /* take all available buffers */
215 avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
219 MHF = rtpj2kdepay->pu_MHF;
220 mh_id = rtpj2kdepay->last_mh_id;
222 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
225 GList *packets, *walk;
227 packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
229 for (walk = packets; walk; walk = g_list_next (walk)) {
230 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
231 GST_DEBUG_OBJECT (rtpj2kdepay,
232 "append pu packet of size %" G_GSIZE_FORMAT,
233 gst_buffer_get_size (buf));
234 gst_adapter_push (rtpj2kdepay->t_adapter, buf);
236 g_list_free (packets);
238 /* we have a header */
239 GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
240 /* we managed to see the start and end of the header, take all from
241 * adapter and keep in header */
242 mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
244 store_mheader (rtpj2kdepay, mh_id, mheader);
248 rtpj2kdepay->have_sync = FALSE;
254 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
256 GstRtpJ2KDepay *rtpj2kdepay;
258 GList *packets, *walk;
260 GstFlowReturn ret = GST_FLOW_OK;
264 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
266 /* flush pending PU */
267 gst_rtp_j2k_depay_flush_pu (depayload);
269 /* take all available buffers */
270 avail = gst_adapter_available (rtpj2kdepay->t_adapter);
274 mh_id = rtpj2kdepay->last_mh_id;
276 GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
278 if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
281 /* we need a header now */
282 if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
285 /* push header in the adapter */
286 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
287 gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
290 /* check for last bytes */
291 gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
293 /* now append the tile packets to the frame */
294 packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
295 for (walk = packets; walk; walk = g_list_next (walk)) {
296 buf = GST_BUFFER_CAST (walk->data);
298 if (walk == packets) {
299 /* first buffer should contain the SOT */
300 gst_buffer_map (buf, &map, GST_MAP_READ);
305 if (map.data[0] == 0xff && map.data[1] == J2K_MARKER_SOT) {
308 if (end[0] == 0xff && end[1] == J2K_MARKER_EOC)
313 Psot = GST_READ_UINT32_BE (&map.data[6]);
314 if (Psot != nPsot && Psot != 0) {
315 /* Psot must match the size of the tile */
316 GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
317 gst_buffer_unmap (buf, &map);
319 buf = gst_buffer_make_writable (buf);
321 gst_buffer_map (buf, &map, GST_MAP_WRITE);
322 GST_WRITE_UINT32_BE (&map.data[6], nPsot);
325 gst_buffer_unmap (buf, &map);
328 GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %" G_GSIZE_FORMAT,
329 gst_buffer_get_size (buf));
330 gst_adapter_push (rtpj2kdepay->f_adapter, buf);
332 g_list_free (packets);
335 rtpj2kdepay->last_tile = -1;
342 GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
343 gst_adapter_clear (rtpj2kdepay->t_adapter);
344 rtpj2kdepay->last_tile = -1;
349 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
350 gst_buffer_unmap (buf, &map);
351 gst_adapter_clear (rtpj2kdepay->t_adapter);
352 rtpj2kdepay->last_tile = -1;
358 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
360 GstRtpJ2KDepay *rtpj2kdepay;
364 GstFlowReturn ret = GST_FLOW_OK;
366 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
368 /* flush pending tile */
369 gst_rtp_j2k_depay_flush_tile (depayload);
371 /* last buffer take all data out of the adapter */
372 avail = gst_adapter_available (rtpj2kdepay->f_adapter);
379 /* take the last bytes of the JPEG 2000 data to see if there is an EOC
381 gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
383 if (end[0] != 0xff && end[1] != 0xd9) {
387 GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
389 /* no EOI marker, add one */
390 outbuf = gst_buffer_new_and_alloc (2);
391 gst_buffer_fill (outbuf, 0, end, 2);
393 gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
397 GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
398 outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
399 gst_rtp_drop_meta (GST_ELEMENT_CAST (depayload),
400 outbuf, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
401 ret = gst_rtp_base_depayload_push (depayload, outbuf);
403 GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
404 gst_adapter_clear (rtpj2kdepay->f_adapter);
407 /* we accept any mh_id now */
408 rtpj2kdepay->last_mh_id = -1;
411 rtpj2kdepay->next_frag = 0;
412 rtpj2kdepay->have_sync = FALSE;
415 /* we can't keep headers with mh_id of 0 */
416 store_mheader (rtpj2kdepay, 0, NULL);
422 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
424 GstRtpJ2KDepay *rtpj2kdepay;
426 guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
430 rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
432 payload = gst_rtp_buffer_get_payload (rtp);
433 payload_len = gst_rtp_buffer_get_payload_len (rtp);
435 /* we need at least a header */
439 rtptime = gst_rtp_buffer_get_timestamp (rtp);
441 /* new timestamp marks new frame */
442 if (rtpj2kdepay->last_rtptime != rtptime) {
443 rtpj2kdepay->last_rtptime = rtptime;
444 /* flush pending frame */
445 gst_rtp_j2k_depay_flush_frame (depayload);
450 * 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
451 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
452 * |tp |MHF|mh_id|T| priority | tile number |
453 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
454 * |reserved | fragment offset |
455 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
457 MHF = (payload[0] & 0x30) >> 4;
458 mh_id = (payload[0] & 0xe) >> 1;
460 if (rtpj2kdepay->last_mh_id == -1)
461 rtpj2kdepay->last_mh_id = mh_id;
462 else if (rtpj2kdepay->last_mh_id != mh_id)
465 tile = (payload[2] << 8) | payload[3];
466 frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
467 j2klen = payload_len - 8;
469 GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
470 tile, frag_offset, rtpj2kdepay->next_frag);
472 /* calculate the gap between expected frag */
473 gap = frag_offset - rtpj2kdepay->next_frag;
474 /* calculate next frag */
475 rtpj2kdepay->next_frag = frag_offset + j2klen;
478 GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
479 /* discont, clear pu adapter and resync */
480 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
483 /* check for sync code */
484 if (j2klen > 2 && payload[8] == 0xff) {
485 guint marker = payload[9];
487 /* packets must start with SOC, SOT or SOP */
490 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
491 /* flush the previous frame, should have happened when the timestamp
493 gst_rtp_j2k_depay_flush_frame (depayload);
494 rtpj2kdepay->have_sync = TRUE;
497 /* flush the previous tile */
498 gst_rtp_j2k_depay_flush_tile (depayload);
499 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
500 rtpj2kdepay->have_sync = TRUE;
501 /* we sync on the tile now */
502 rtpj2kdepay->last_tile = tile;
505 GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
506 /* flush the previous PU */
507 gst_rtp_j2k_depay_flush_pu (depayload);
508 if (rtpj2kdepay->last_tile != tile) {
509 /* wrong tile, we lose sync and we need a new SOT or SOC to regain
510 * sync. First flush out the previous tile if we have one. */
511 if (rtpj2kdepay->last_tile != -1)
512 gst_rtp_j2k_depay_flush_tile (depayload);
513 /* now we have no more valid tile and no sync */
514 rtpj2kdepay->last_tile = -1;
515 rtpj2kdepay->have_sync = FALSE;
517 rtpj2kdepay->have_sync = TRUE;
521 GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
526 if (rtpj2kdepay->have_sync) {
529 if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
530 /* first part of pu, record state */
531 GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
532 rtpj2kdepay->pu_MHF = MHF;
534 /* and push in pu adapter */
535 GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
536 pu_frag = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
537 gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
540 /* last part of main header received, we can flush it */
541 GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
542 gst_rtp_j2k_depay_flush_pu (depayload);
545 GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
548 /* marker bit finishes the frame */
549 if (gst_rtp_buffer_get_marker (rtp)) {
550 GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
551 /* then flush frame */
552 gst_rtp_j2k_depay_flush_frame (depayload);
560 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
561 ("Empty Payload."), (NULL));
566 GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
567 ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
569 gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
575 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
576 const GValue * value, GParamSpec * pspec)
580 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
586 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
587 GValue * value, GParamSpec * pspec)
591 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
596 static GstStateChangeReturn
597 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
599 GstRtpJ2KDepay *rtpj2kdepay;
600 GstStateChangeReturn ret;
602 rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
604 switch (transition) {
605 case GST_STATE_CHANGE_NULL_TO_READY:
607 case GST_STATE_CHANGE_READY_TO_PAUSED:
608 gst_rtp_j2k_depay_reset (rtpj2kdepay);
614 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
616 switch (transition) {
617 case GST_STATE_CHANGE_PAUSED_TO_READY:
618 gst_rtp_j2k_depay_reset (rtpj2kdepay);
620 case GST_STATE_CHANGE_READY_TO_NULL:
629 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
631 return gst_element_register (plugin, "rtpj2kdepay",
632 GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);