rtsp-server: fix memory leak
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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 "gstrtpelements.h"
41 #include "gstrtpj2kcommon.h"
42 #include "gstrtpj2kdepay.h"
43 #include "gstrtputils.h"
44
45 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
46 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
47
48 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
50     GST_PAD_SRC,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("image/x-jpc, "
53         "colorspace = (string) { sRGB, sYUV, GRAY }")
54     );
55
56 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
57     GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
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\";"
64         "application/x-rtp, "
65         "media = (string) \"video\", " "clock-rate = (int) 90000, "
66         "colorspace = (string) { sRGB, sYUV, GRAY }, "
67         "encoding-name = (string) \"JPEG2000\";")
68     );
69
70 enum
71 {
72   PROP_0,
73   PROP_LAST
74 };
75
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));
80
81 static void gst_rtp_j2k_depay_finalize (GObject * object);
82
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);
87
88 static GstStateChangeReturn
89 gst_rtp_j2k_depay_change_state (GstElement * element,
90     GstStateChange transition);
91
92 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
93     GstCaps * caps);
94 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
95     GstRTPBuffer * rtp);
96
97 static void
98 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
99 {
100   GObjectClass *gobject_class;
101   GstElementClass *gstelement_class;
102   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
103
104   gobject_class = (GObjectClass *) klass;
105   gstelement_class = (GstElementClass *) klass;
106   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
107
108   gobject_class->finalize = gst_rtp_j2k_depay_finalize;
109
110   gobject_class->set_property = gst_rtp_j2k_depay_set_property;
111   gobject_class->get_property = gst_rtp_j2k_depay_get_property;
112
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);
117
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>");
122
123   gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
124
125   gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
126   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
127
128   GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
129       "J2K Video RTP Depayloader");
130 }
131
132 static void
133 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
134 {
135   rtpj2kdepay->pu_adapter = gst_adapter_new ();
136   rtpj2kdepay->t_adapter = gst_adapter_new ();
137   rtpj2kdepay->f_adapter = gst_adapter_new ();
138 }
139
140 static void
141 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
142 {
143   GstBuffer *old;
144
145   GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
146       idx);
147   if ((old = rtpj2kdepay->MH[idx]))
148     gst_buffer_unref (old);
149   rtpj2kdepay->MH[idx] = buf;
150 }
151
152 static void
153 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
154 {
155   guint i;
156
157   for (i = 0; i < 8; i++)
158     store_mheader (rtpj2kdepay, i, NULL);
159 }
160
161 static void
162 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
163 {
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;
169 }
170
171 static void
172 gst_rtp_j2k_depay_finalize (GObject * object)
173 {
174   GstRtpJ2KDepay *rtpj2kdepay;
175
176   rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
177
178   clear_mheaders (rtpj2kdepay);
179
180   g_object_unref (rtpj2kdepay->pu_adapter);
181   g_object_unref (rtpj2kdepay->t_adapter);
182   g_object_unref (rtpj2kdepay->f_adapter);
183
184   G_OBJECT_CLASS (parent_class)->finalize (object);
185 }
186
187 static gboolean
188 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
189 {
190   GstStructure *structure = NULL;
191   gint clock_rate;
192   GstCaps *outcaps = NULL;
193   gboolean res = FALSE;
194   const gchar *colorspace = NULL;
195   const gchar *sampling = NULL;
196
197   structure = gst_caps_get_structure (caps, 0);
198
199   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
200     clock_rate = 90000;
201   depayload->clock_rate = clock_rate;
202
203   sampling = gst_structure_get_string (structure, "sampling");
204   if (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))
209       colorspace = "sRGB";
210     else if (!strcmp (sampling, GST_RTP_J2K_GRAYSCALE))
211       colorspace = "GRAY";
212     else
213       colorspace = "sYUV";
214   } else {
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;
220     }
221   }
222
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);
226
227   if (sampling)
228     gst_caps_set_simple (outcaps, "sampling", G_TYPE_STRING, sampling, NULL);
229
230   res = gst_pad_set_caps (depayload->srcpad, outcaps);
231
232   gst_caps_unref (outcaps);
233
234   return res;
235 }
236
237 static void
238 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
239 {
240   gst_adapter_clear (rtpj2kdepay->pu_adapter);
241   rtpj2kdepay->have_sync = FALSE;
242 }
243
244 static GstFlowReturn
245 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
246 {
247   GstRtpJ2KDepay *rtpj2kdepay;
248   GstBuffer *mheader;
249   guint avail, MHF, mh_id;
250
251   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
252
253   /* take all available buffers */
254   avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
255   if (avail == 0)
256     goto done;
257
258   MHF = rtpj2kdepay->pu_MHF;
259   mh_id = rtpj2kdepay->last_mh_id;
260
261   GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
262
263   if (MHF == 0) {
264     GList *packets, *walk;
265
266     packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
267     /* append packets */
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);
274     }
275     g_list_free (packets);
276   } else {
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);
282
283     store_mheader (rtpj2kdepay, mh_id, mheader);
284   }
285
286 done:
287   rtpj2kdepay->have_sync = FALSE;
288
289   return GST_FLOW_OK;
290 }
291
292 static GstFlowReturn
293 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
294 {
295   GstRtpJ2KDepay *rtpj2kdepay;
296   guint avail, mh_id;
297   GList *packets, *walk;
298   guint8 end[2];
299   GstFlowReturn ret = GST_FLOW_OK;
300   GstMapInfo map;
301   GstBuffer *buf;
302
303   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
304
305   /* flush pending PU */
306   gst_rtp_j2k_depay_flush_pu (depayload);
307
308   /* take all available buffers */
309   avail = gst_adapter_available (rtpj2kdepay->t_adapter);
310   if (avail == 0)
311     goto done;
312
313   mh_id = rtpj2kdepay->last_mh_id;
314
315   GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
316
317   if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
318     GstBuffer *mheader;
319
320     /* we need a header now */
321     if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
322       goto waiting_header;
323
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));
327   }
328
329   /* check for last bytes */
330   gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
331
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);
336
337     if (walk == packets) {
338       /* first buffer should contain the SOT */
339       gst_buffer_map (buf, &map, GST_MAP_READ);
340
341       if (map.size < 12)
342         goto invalid_tile;
343
344       if (map.data[0] == GST_J2K_MARKER && map.data[1] == GST_J2K_MARKER_SOT) {
345         guint Psot, nPsot;
346
347         if (end[0] == GST_J2K_MARKER && end[1] == GST_J2K_MARKER_EOC)
348           nPsot = avail - 2;
349         else
350           nPsot = avail;
351
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);
357
358           buf = gst_buffer_make_writable (buf);
359
360           gst_buffer_map (buf, &map, GST_MAP_WRITE);
361           GST_WRITE_UINT32_BE (&map.data[6], nPsot);
362         }
363       }
364       gst_buffer_unmap (buf, &map);
365     }
366
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);
370   }
371   g_list_free (packets);
372
373 done:
374   rtpj2kdepay->last_tile = -1;
375
376   return ret;
377
378   /* errors */
379 waiting_header:
380   {
381     GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
382     gst_adapter_clear (rtpj2kdepay->t_adapter);
383     rtpj2kdepay->last_tile = -1;
384     return ret;
385   }
386 invalid_tile:
387   {
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;
392     return ret;
393   }
394 }
395
396 static GstFlowReturn
397 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
398 {
399   GstRtpJ2KDepay *rtpj2kdepay;
400   guint8 end[2];
401   guint avail;
402
403   GstFlowReturn ret = GST_FLOW_OK;
404
405   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
406
407   /* flush pending tile */
408   gst_rtp_j2k_depay_flush_tile (depayload);
409
410   /* last buffer take all data out of the adapter */
411   avail = gst_adapter_available (rtpj2kdepay->f_adapter);
412   if (avail == 0)
413     goto done;
414
415   if (avail > 2) {
416     GstBuffer *outbuf;
417
418     /* take the last bytes of the JPEG 2000 data to see if there is an EOC
419      * marker */
420     gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
421
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;
425
426       GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
427
428       /* no EOI marker, add one */
429       outbuf = gst_buffer_new_and_alloc (2);
430       gst_buffer_fill (outbuf, 0, end, 2);
431
432       gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
433       avail += 2;
434     }
435
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);
440   } else {
441     GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
442     gst_adapter_clear (rtpj2kdepay->f_adapter);
443   }
444
445   /* we accept any mh_id now */
446   rtpj2kdepay->last_mh_id = -1;
447
448   /* reset state */
449   rtpj2kdepay->next_frag = 0;
450   rtpj2kdepay->have_sync = FALSE;
451
452 done:
453   /* we can't keep headers with mh_id of 0 */
454   store_mheader (rtpj2kdepay, 0, NULL);
455
456   return ret;
457 }
458
459 static GstBuffer *
460 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
461 {
462   GstRtpJ2KDepay *rtpj2kdepay;
463   guint8 *payload;
464   guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
465   gint gap;
466   guint32 rtptime;
467
468   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
469
470   payload = gst_rtp_buffer_get_payload (rtp);
471   payload_len = gst_rtp_buffer_get_payload_len (rtp);
472
473   /* we need at least a header */
474   if (payload_len < GST_RTP_J2K_HEADER_SIZE)
475     goto empty_packet;
476
477   rtptime = gst_rtp_buffer_get_timestamp (rtp);
478
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);
484   }
485
486   /*
487    *  0                   1                   2                   3
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    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494    */
495   MHF = (payload[0] & 0x30) >> 4;
496   mh_id = (payload[0] & 0xe) >> 1;
497
498   if (rtpj2kdepay->last_mh_id == -1)
499     rtpj2kdepay->last_mh_id = mh_id;
500   else if (rtpj2kdepay->last_mh_id != mh_id)
501     goto wrong_mh_id;
502
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;
506
507   GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
508       tile, frag_offset, rtpj2kdepay->next_frag);
509
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;
514
515   if (gap != 0) {
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);
519   }
520
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];
524
525     /* packets must start with SOC, SOT or SOP */
526     switch (marker) {
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
530          * changed above. */
531         gst_rtp_j2k_depay_flush_frame (depayload);
532         rtpj2kdepay->have_sync = TRUE;
533         break;
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;
541         break;
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;
554         } else {
555           rtpj2kdepay->have_sync = TRUE;
556         }
557         break;
558       default:
559         GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
560         break;
561     }
562   }
563
564   if (rtpj2kdepay->have_sync) {
565     GstBuffer *pu_frag;
566
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;
571     }
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);
576
577     if (MHF & 2) {
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);
581     }
582   } else {
583     GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
584   }
585
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);
591   }
592
593   return NULL;
594
595   /* ERRORS */
596 empty_packet:
597   {
598     GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
599         ("Empty Payload."), (NULL));
600     return NULL;
601   }
602 wrong_mh_id:
603   {
604     GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
605         ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
606         (NULL));
607     gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
608     return NULL;
609   }
610 }
611
612 static void
613 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
614     const GValue * value, GParamSpec * pspec)
615 {
616   switch (prop_id) {
617     default:
618       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619       break;
620   }
621 }
622
623 static void
624 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
625     GValue * value, GParamSpec * pspec)
626 {
627   switch (prop_id) {
628     default:
629       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
630       break;
631   }
632 }
633
634 static GstStateChangeReturn
635 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
636 {
637   GstRtpJ2KDepay *rtpj2kdepay;
638   GstStateChangeReturn ret;
639
640   rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
641
642   switch (transition) {
643     case GST_STATE_CHANGE_NULL_TO_READY:
644       break;
645     case GST_STATE_CHANGE_READY_TO_PAUSED:
646       gst_rtp_j2k_depay_reset (rtpj2kdepay);
647       break;
648     default:
649       break;
650   }
651
652   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
653
654   switch (transition) {
655     case GST_STATE_CHANGE_PAUSED_TO_READY:
656       gst_rtp_j2k_depay_reset (rtpj2kdepay);
657       break;
658     case GST_STATE_CHANGE_READY_TO_NULL:
659       break;
660     default:
661       break;
662   }
663   return ret;
664 }