rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpj2kdepay.c
1 /* GStreamer
2  * Copyright (C) <2009> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20
21  /**
22  * SECTION:element-rtpj2kdepay
23  * @title: rtpj2kdepay
24  *
25  * Depayload an RTP-payloaded JPEG 2000 image into RTP packets according to RFC 5371
26  * and RFC 5372.
27  * For detailed information see: https://datatracker.ietf.org/doc/rfc5371/
28  * and https://datatracker.ietf.org/doc/rfc5372/
29  */
30
31
32 #ifdef HAVE_CONFIG_H
33 #  include "config.h"
34 #endif
35
36 #include <gst/rtp/gstrtpbuffer.h>
37 #include <gst/video/video.h>
38
39 #include <string.h>
40 #include "gstrtpj2kcommon.h"
41 #include "gstrtpj2kdepay.h"
42 #include "gstrtputils.h"
43
44 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
45 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
46
47 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
48 GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("image/x-jpc, "
52         "colorspace = (string) { sRGB, sYUV, GRAY }")
53     );
54
55 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
56     GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
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\";"
63         "application/x-rtp, "
64         "media = (string) \"video\", " "clock-rate = (int) 90000, "
65         "colorspace = (string) { sRGB, sYUV, GRAY }, "
66         "encoding-name = (string) \"JPEG2000\";")
67     );
68
69 enum
70 {
71   PROP_0,
72   PROP_LAST
73 };
74
75 #define gst_rtp_j2k_depay_parent_class parent_class
76 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
77
78 static void gst_rtp_j2k_depay_finalize (GObject * object);
79
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);
84
85 static GstStateChangeReturn
86 gst_rtp_j2k_depay_change_state (GstElement * element,
87     GstStateChange transition);
88
89 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
90     GstCaps * caps);
91 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
92     GstRTPBuffer * rtp);
93
94 static void
95 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
96 {
97   GObjectClass *gobject_class;
98   GstElementClass *gstelement_class;
99   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
100
101   gobject_class = (GObjectClass *) klass;
102   gstelement_class = (GstElementClass *) klass;
103   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
104
105   gobject_class->finalize = gst_rtp_j2k_depay_finalize;
106
107   gobject_class->set_property = gst_rtp_j2k_depay_set_property;
108   gobject_class->get_property = gst_rtp_j2k_depay_get_property;
109
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);
114
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>");
119
120   gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
121
122   gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
123   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
124
125   GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
126       "J2K Video RTP Depayloader");
127 }
128
129 static void
130 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
131 {
132   rtpj2kdepay->pu_adapter = gst_adapter_new ();
133   rtpj2kdepay->t_adapter = gst_adapter_new ();
134   rtpj2kdepay->f_adapter = gst_adapter_new ();
135 }
136
137 static void
138 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
139 {
140   GstBuffer *old;
141
142   GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
143       idx);
144   if ((old = rtpj2kdepay->MH[idx]))
145     gst_buffer_unref (old);
146   rtpj2kdepay->MH[idx] = buf;
147 }
148
149 static void
150 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
151 {
152   guint i;
153
154   for (i = 0; i < 8; i++)
155     store_mheader (rtpj2kdepay, i, NULL);
156 }
157
158 static void
159 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
160 {
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;
166 }
167
168 static void
169 gst_rtp_j2k_depay_finalize (GObject * object)
170 {
171   GstRtpJ2KDepay *rtpj2kdepay;
172
173   rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
174
175   clear_mheaders (rtpj2kdepay);
176
177   g_object_unref (rtpj2kdepay->pu_adapter);
178   g_object_unref (rtpj2kdepay->t_adapter);
179   g_object_unref (rtpj2kdepay->f_adapter);
180
181   G_OBJECT_CLASS (parent_class)->finalize (object);
182 }
183
184 static gboolean
185 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
186 {
187   GstStructure *structure = NULL;
188   gint clock_rate;
189   GstCaps *outcaps = NULL;
190   gboolean res = FALSE;
191   const gchar *colorspace = NULL;
192   const gchar *sampling = NULL;
193
194   structure = gst_caps_get_structure (caps, 0);
195
196   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
197     clock_rate = 90000;
198   depayload->clock_rate = clock_rate;
199
200   sampling = gst_structure_get_string (structure, "sampling");
201   if (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))
206       colorspace = "sRGB";
207     else if (!strcmp (sampling, GST_RTP_J2K_GRAYSCALE))
208       colorspace = "GRAY";
209     else
210       colorspace = "sYUV";
211   } else {
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;
217     }
218   }
219
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);
223
224   if (sampling)
225     gst_caps_set_simple (outcaps, "sampling", G_TYPE_STRING, sampling, NULL);
226
227   res = gst_pad_set_caps (depayload->srcpad, outcaps);
228
229   gst_caps_unref (outcaps);
230
231   return res;
232 }
233
234 static void
235 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
236 {
237   gst_adapter_clear (rtpj2kdepay->pu_adapter);
238   rtpj2kdepay->have_sync = FALSE;
239 }
240
241 static GstFlowReturn
242 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
243 {
244   GstRtpJ2KDepay *rtpj2kdepay;
245   GstBuffer *mheader;
246   guint avail, MHF, mh_id;
247
248   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
249
250   /* take all available buffers */
251   avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
252   if (avail == 0)
253     goto done;
254
255   MHF = rtpj2kdepay->pu_MHF;
256   mh_id = rtpj2kdepay->last_mh_id;
257
258   GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
259
260   if (MHF == 0) {
261     GList *packets, *walk;
262
263     packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
264     /* append packets */
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);
271     }
272     g_list_free (packets);
273   } else {
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);
279
280     store_mheader (rtpj2kdepay, mh_id, mheader);
281   }
282
283 done:
284   rtpj2kdepay->have_sync = FALSE;
285
286   return GST_FLOW_OK;
287 }
288
289 static GstFlowReturn
290 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
291 {
292   GstRtpJ2KDepay *rtpj2kdepay;
293   guint avail, mh_id;
294   GList *packets, *walk;
295   guint8 end[2];
296   GstFlowReturn ret = GST_FLOW_OK;
297   GstMapInfo map;
298   GstBuffer *buf;
299
300   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
301
302   /* flush pending PU */
303   gst_rtp_j2k_depay_flush_pu (depayload);
304
305   /* take all available buffers */
306   avail = gst_adapter_available (rtpj2kdepay->t_adapter);
307   if (avail == 0)
308     goto done;
309
310   mh_id = rtpj2kdepay->last_mh_id;
311
312   GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
313
314   if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
315     GstBuffer *mheader;
316
317     /* we need a header now */
318     if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
319       goto waiting_header;
320
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));
324   }
325
326   /* check for last bytes */
327   gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
328
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);
333
334     if (walk == packets) {
335       /* first buffer should contain the SOT */
336       gst_buffer_map (buf, &map, GST_MAP_READ);
337
338       if (map.size < 12)
339         goto invalid_tile;
340
341       if (map.data[0] == GST_J2K_MARKER && map.data[1] == GST_J2K_MARKER_SOT) {
342         guint Psot, nPsot;
343
344         if (end[0] == GST_J2K_MARKER && end[1] == GST_J2K_MARKER_EOC)
345           nPsot = avail - 2;
346         else
347           nPsot = avail;
348
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);
354
355           buf = gst_buffer_make_writable (buf);
356
357           gst_buffer_map (buf, &map, GST_MAP_WRITE);
358           GST_WRITE_UINT32_BE (&map.data[6], nPsot);
359         }
360       }
361       gst_buffer_unmap (buf, &map);
362     }
363
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);
367   }
368   g_list_free (packets);
369
370 done:
371   rtpj2kdepay->last_tile = -1;
372
373   return ret;
374
375   /* errors */
376 waiting_header:
377   {
378     GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
379     gst_adapter_clear (rtpj2kdepay->t_adapter);
380     rtpj2kdepay->last_tile = -1;
381     return ret;
382   }
383 invalid_tile:
384   {
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;
389     return ret;
390   }
391 }
392
393 static GstFlowReturn
394 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
395 {
396   GstRtpJ2KDepay *rtpj2kdepay;
397   guint8 end[2];
398   guint avail;
399
400   GstFlowReturn ret = GST_FLOW_OK;
401
402   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
403
404   /* flush pending tile */
405   gst_rtp_j2k_depay_flush_tile (depayload);
406
407   /* last buffer take all data out of the adapter */
408   avail = gst_adapter_available (rtpj2kdepay->f_adapter);
409   if (avail == 0)
410     goto done;
411
412   if (avail > 2) {
413     GstBuffer *outbuf;
414
415     /* take the last bytes of the JPEG 2000 data to see if there is an EOC
416      * marker */
417     gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
418
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;
422
423       GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
424
425       /* no EOI marker, add one */
426       outbuf = gst_buffer_new_and_alloc (2);
427       gst_buffer_fill (outbuf, 0, end, 2);
428
429       gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
430       avail += 2;
431     }
432
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);
437   } else {
438     GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
439     gst_adapter_clear (rtpj2kdepay->f_adapter);
440   }
441
442   /* we accept any mh_id now */
443   rtpj2kdepay->last_mh_id = -1;
444
445   /* reset state */
446   rtpj2kdepay->next_frag = 0;
447   rtpj2kdepay->have_sync = FALSE;
448
449 done:
450   /* we can't keep headers with mh_id of 0 */
451   store_mheader (rtpj2kdepay, 0, NULL);
452
453   return ret;
454 }
455
456 static GstBuffer *
457 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
458 {
459   GstRtpJ2KDepay *rtpj2kdepay;
460   guint8 *payload;
461   guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
462   gint gap;
463   guint32 rtptime;
464
465   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
466
467   payload = gst_rtp_buffer_get_payload (rtp);
468   payload_len = gst_rtp_buffer_get_payload_len (rtp);
469
470   /* we need at least a header */
471   if (payload_len < GST_RTP_J2K_HEADER_SIZE)
472     goto empty_packet;
473
474   rtptime = gst_rtp_buffer_get_timestamp (rtp);
475
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);
481   }
482
483   /*
484    *  0                   1                   2                   3
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    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491    */
492   MHF = (payload[0] & 0x30) >> 4;
493   mh_id = (payload[0] & 0xe) >> 1;
494
495   if (rtpj2kdepay->last_mh_id == -1)
496     rtpj2kdepay->last_mh_id = mh_id;
497   else if (rtpj2kdepay->last_mh_id != mh_id)
498     goto wrong_mh_id;
499
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;
503
504   GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
505       tile, frag_offset, rtpj2kdepay->next_frag);
506
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;
511
512   if (gap != 0) {
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);
516   }
517
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];
521
522     /* packets must start with SOC, SOT or SOP */
523     switch (marker) {
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
527          * changed above. */
528         gst_rtp_j2k_depay_flush_frame (depayload);
529         rtpj2kdepay->have_sync = TRUE;
530         break;
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;
538         break;
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;
551         } else {
552           rtpj2kdepay->have_sync = TRUE;
553         }
554         break;
555       default:
556         GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
557         break;
558     }
559   }
560
561   if (rtpj2kdepay->have_sync) {
562     GstBuffer *pu_frag;
563
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;
568     }
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);
573
574     if (MHF & 2) {
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);
578     }
579   } else {
580     GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
581   }
582
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);
588   }
589
590   return NULL;
591
592   /* ERRORS */
593 empty_packet:
594   {
595     GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
596         ("Empty Payload."), (NULL));
597     return NULL;
598   }
599 wrong_mh_id:
600   {
601     GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
602         ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
603         (NULL));
604     gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
605     return NULL;
606   }
607 }
608
609 static void
610 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
611     const GValue * value, GParamSpec * pspec)
612 {
613   switch (prop_id) {
614     default:
615       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
616       break;
617   }
618 }
619
620 static void
621 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
622     GValue * value, GParamSpec * pspec)
623 {
624   switch (prop_id) {
625     default:
626       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
627       break;
628   }
629 }
630
631 static GstStateChangeReturn
632 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
633 {
634   GstRtpJ2KDepay *rtpj2kdepay;
635   GstStateChangeReturn ret;
636
637   rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
638
639   switch (transition) {
640     case GST_STATE_CHANGE_NULL_TO_READY:
641       break;
642     case GST_STATE_CHANGE_READY_TO_PAUSED:
643       gst_rtp_j2k_depay_reset (rtpj2kdepay);
644       break;
645     default:
646       break;
647   }
648
649   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
650
651   switch (transition) {
652     case GST_STATE_CHANGE_PAUSED_TO_READY:
653       gst_rtp_j2k_depay_reset (rtpj2kdepay);
654       break;
655     case GST_STATE_CHANGE_READY_TO_NULL:
656       break;
657     default:
658       break;
659   }
660   return ret;
661 }
662
663 gboolean
664 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
665 {
666   return gst_element_register (plugin, "rtpj2kdepay",
667       GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);
668 }