gstrtpj2k: set sampling field required by RFC
[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  *
24  * Depayload an RTP-payloaded JPEG 2000 image into RTP packets according to RFC 5371
25  * and RFC 5372.
26  * For detailed information see: https://datatracker.ietf.org/doc/rfc5371/
27  * and https://datatracker.ietf.org/doc/rfc5372/
28  */
29
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #endif
34
35 #include <gst/rtp/gstrtpbuffer.h>
36 #include <gst/video/video.h>
37
38 #include <string.h>
39 #include "gstrtpj2kcommon.h"
40 #include "gstrtpj2kdepay.h"
41 #include "gstrtputils.h"
42
43 GST_DEBUG_CATEGORY_STATIC (rtpj2kdepay_debug);
44 #define GST_CAT_DEFAULT (rtpj2kdepay_debug)
45
46 static GstStaticPadTemplate gst_rtp_j2k_depay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("image/x-jpc, "
51         "colorspace = (string) { sRGB, sYUV, GRAY }")
52     );
53
54 static GstStaticPadTemplate gst_rtp_j2k_depay_sink_template =
55     GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("application/x-rtp, "
59         "media = (string) \"video\", " "clock-rate = (int) 90000, "
60         GST_RTP_J2K_SAMPLING_LIST ","
61         "encoding-name = (string) \"JPEG2000\";"
62         "application/x-rtp, "
63         "media = (string) \"video\", " "clock-rate = (int) 90000, "
64         "colorspace = (string) { sRGB, sYUV, GRAY }, "
65         "encoding-name = (string) \"JPEG2000\";")
66     );
67
68 enum
69 {
70   PROP_0,
71   PROP_LAST
72 };
73
74 #define gst_rtp_j2k_depay_parent_class parent_class
75 G_DEFINE_TYPE (GstRtpJ2KDepay, gst_rtp_j2k_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
76
77 static void gst_rtp_j2k_depay_finalize (GObject * object);
78
79 static void gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
80     const GValue * value, GParamSpec * pspec);
81 static void gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
82     GValue * value, GParamSpec * pspec);
83
84 static GstStateChangeReturn
85 gst_rtp_j2k_depay_change_state (GstElement * element,
86     GstStateChange transition);
87
88 static gboolean gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload,
89     GstCaps * caps);
90 static GstBuffer *gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload,
91     GstRTPBuffer * rtp);
92
93 static void
94 gst_rtp_j2k_depay_class_init (GstRtpJ2KDepayClass * klass)
95 {
96   GObjectClass *gobject_class;
97   GstElementClass *gstelement_class;
98   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
99
100   gobject_class = (GObjectClass *) klass;
101   gstelement_class = (GstElementClass *) klass;
102   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
103
104   gobject_class->finalize = gst_rtp_j2k_depay_finalize;
105
106   gobject_class->set_property = gst_rtp_j2k_depay_set_property;
107   gobject_class->get_property = gst_rtp_j2k_depay_get_property;
108
109   gst_element_class_add_static_pad_template (gstelement_class,
110       &gst_rtp_j2k_depay_src_template);
111   gst_element_class_add_static_pad_template (gstelement_class,
112       &gst_rtp_j2k_depay_sink_template);
113
114   gst_element_class_set_static_metadata (gstelement_class,
115       "RTP JPEG 2000 depayloader", "Codec/Depayloader/Network/RTP",
116       "Extracts JPEG 2000 video from RTP packets (RFC 5371)",
117       "Wim Taymans <wim.taymans@gmail.com>");
118
119   gstelement_class->change_state = gst_rtp_j2k_depay_change_state;
120
121   gstrtpbasedepayload_class->set_caps = gst_rtp_j2k_depay_setcaps;
122   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_j2k_depay_process;
123
124   GST_DEBUG_CATEGORY_INIT (rtpj2kdepay_debug, "rtpj2kdepay", 0,
125       "J2K Video RTP Depayloader");
126 }
127
128 static void
129 gst_rtp_j2k_depay_init (GstRtpJ2KDepay * rtpj2kdepay)
130 {
131   rtpj2kdepay->pu_adapter = gst_adapter_new ();
132   rtpj2kdepay->t_adapter = gst_adapter_new ();
133   rtpj2kdepay->f_adapter = gst_adapter_new ();
134 }
135
136 static void
137 store_mheader (GstRtpJ2KDepay * rtpj2kdepay, guint idx, GstBuffer * buf)
138 {
139   GstBuffer *old;
140
141   GST_DEBUG_OBJECT (rtpj2kdepay, "storing main header %p at index %u", buf,
142       idx);
143   if ((old = rtpj2kdepay->MH[idx]))
144     gst_buffer_unref (old);
145   rtpj2kdepay->MH[idx] = buf;
146 }
147
148 static void
149 clear_mheaders (GstRtpJ2KDepay * rtpj2kdepay)
150 {
151   guint i;
152
153   for (i = 0; i < 8; i++)
154     store_mheader (rtpj2kdepay, i, NULL);
155 }
156
157 static void
158 gst_rtp_j2k_depay_reset (GstRtpJ2KDepay * rtpj2kdepay)
159 {
160   clear_mheaders (rtpj2kdepay);
161   gst_adapter_clear (rtpj2kdepay->pu_adapter);
162   gst_adapter_clear (rtpj2kdepay->t_adapter);
163   gst_adapter_clear (rtpj2kdepay->f_adapter);
164   rtpj2kdepay->next_frag = 0;
165 }
166
167 static void
168 gst_rtp_j2k_depay_finalize (GObject * object)
169 {
170   GstRtpJ2KDepay *rtpj2kdepay;
171
172   rtpj2kdepay = GST_RTP_J2K_DEPAY (object);
173
174   clear_mheaders (rtpj2kdepay);
175
176   g_object_unref (rtpj2kdepay->pu_adapter);
177   g_object_unref (rtpj2kdepay->t_adapter);
178   g_object_unref (rtpj2kdepay->f_adapter);
179
180   G_OBJECT_CLASS (parent_class)->finalize (object);
181 }
182
183 static gboolean
184 gst_rtp_j2k_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
185 {
186   GstStructure *structure = NULL;
187   gint clock_rate;
188   GstCaps *outcaps = NULL;
189   gboolean res = FALSE;
190   const gchar *colorspace = NULL;
191   const gchar *sampling = NULL;
192
193   structure = gst_caps_get_structure (caps, 0);
194
195   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
196     clock_rate = 90000;
197   depayload->clock_rate = clock_rate;
198
199   sampling = gst_structure_get_string (structure, "sampling");
200   if (sampling) {
201     if (!strcmp (sampling, GST_RTP_J2K_RGB) ||
202         !strcmp (sampling, GST_RTP_J2K_RGBA) ||
203         !strcmp (sampling, GST_RTP_J2K_BGR) ||
204         !strcmp (sampling, GST_RTP_J2K_BGRA))
205       colorspace = "sRGB";
206     else if (!strcmp (sampling, GST_RTP_J2K_GRAYSCALE))
207       colorspace = "GRAY";
208     else
209       colorspace = "sYUV";
210   } else {
211     GST_ELEMENT_WARNING (depayload, STREAM, DEMUX, NULL,
212         ("Non-compliant stream: sampling field missing. Frames my appear incorrect"));
213     colorspace = gst_structure_get_string (structure, "colorspace");
214     if (!strcmp (colorspace, "GRAY")) {
215       sampling = GST_RTP_J2K_GRAYSCALE;
216     }
217   }
218
219   outcaps = gst_caps_new_simple ("image/x-jpc",
220       "framerate", GST_TYPE_FRACTION, 0, 1,
221       "fields", G_TYPE_INT, 1, "colorspace", G_TYPE_STRING, colorspace, NULL);
222
223   if (sampling)
224     gst_caps_set_simple (outcaps, "sampling", G_TYPE_STRING, sampling, NULL);
225
226   res = gst_pad_set_caps (depayload->srcpad, outcaps);
227
228   gst_caps_unref (outcaps);
229
230   return res;
231 }
232
233 static void
234 gst_rtp_j2k_depay_clear_pu (GstRtpJ2KDepay * rtpj2kdepay)
235 {
236   gst_adapter_clear (rtpj2kdepay->pu_adapter);
237   rtpj2kdepay->have_sync = FALSE;
238 }
239
240 static GstFlowReturn
241 gst_rtp_j2k_depay_flush_pu (GstRTPBaseDepayload * depayload)
242 {
243   GstRtpJ2KDepay *rtpj2kdepay;
244   GstBuffer *mheader;
245   guint avail, MHF, mh_id;
246
247   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
248
249   /* take all available buffers */
250   avail = gst_adapter_available (rtpj2kdepay->pu_adapter);
251   if (avail == 0)
252     goto done;
253
254   MHF = rtpj2kdepay->pu_MHF;
255   mh_id = rtpj2kdepay->last_mh_id;
256
257   GST_DEBUG_OBJECT (rtpj2kdepay, "flushing PU of size %u", avail);
258
259   if (MHF == 0) {
260     GList *packets, *walk;
261
262     packets = gst_adapter_take_list (rtpj2kdepay->pu_adapter, avail);
263     /* append packets */
264     for (walk = packets; walk; walk = g_list_next (walk)) {
265       GstBuffer *buf = GST_BUFFER_CAST (walk->data);
266       GST_DEBUG_OBJECT (rtpj2kdepay,
267           "append pu packet of size %" G_GSIZE_FORMAT,
268           gst_buffer_get_size (buf));
269       gst_adapter_push (rtpj2kdepay->t_adapter, buf);
270     }
271     g_list_free (packets);
272   } else {
273     /* we have a header */
274     GST_DEBUG_OBJECT (rtpj2kdepay, "keeping header %u", mh_id);
275     /* we managed to see the start and end of the header, take all from
276      * adapter and keep in header  */
277     mheader = gst_adapter_take_buffer (rtpj2kdepay->pu_adapter, avail);
278
279     store_mheader (rtpj2kdepay, mh_id, mheader);
280   }
281
282 done:
283   rtpj2kdepay->have_sync = FALSE;
284
285   return GST_FLOW_OK;
286 }
287
288 static GstFlowReturn
289 gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
290 {
291   GstRtpJ2KDepay *rtpj2kdepay;
292   guint avail, mh_id;
293   GList *packets, *walk;
294   guint8 end[2];
295   GstFlowReturn ret = GST_FLOW_OK;
296   GstMapInfo map;
297   GstBuffer *buf;
298
299   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
300
301   /* flush pending PU */
302   gst_rtp_j2k_depay_flush_pu (depayload);
303
304   /* take all available buffers */
305   avail = gst_adapter_available (rtpj2kdepay->t_adapter);
306   if (avail == 0)
307     goto done;
308
309   mh_id = rtpj2kdepay->last_mh_id;
310
311   GST_DEBUG_OBJECT (rtpj2kdepay, "flushing tile of size %u", avail);
312
313   if (gst_adapter_available (rtpj2kdepay->f_adapter) == 0) {
314     GstBuffer *mheader;
315
316     /* we need a header now */
317     if ((mheader = rtpj2kdepay->MH[mh_id]) == NULL)
318       goto waiting_header;
319
320     /* push header in the adapter */
321     GST_DEBUG_OBJECT (rtpj2kdepay, "pushing header %u", mh_id);
322     gst_adapter_push (rtpj2kdepay->f_adapter, gst_buffer_ref (mheader));
323   }
324
325   /* check for last bytes */
326   gst_adapter_copy (rtpj2kdepay->t_adapter, end, avail - 2, 2);
327
328   /* now append the tile packets to the frame */
329   packets = gst_adapter_take_list (rtpj2kdepay->t_adapter, avail);
330   for (walk = packets; walk; walk = g_list_next (walk)) {
331     buf = GST_BUFFER_CAST (walk->data);
332
333     if (walk == packets) {
334       /* first buffer should contain the SOT */
335       gst_buffer_map (buf, &map, GST_MAP_READ);
336
337       if (map.size < 12)
338         goto invalid_tile;
339
340       if (map.data[0] == GST_J2K_MARKER && map.data[1] == GST_J2K_MARKER_SOT) {
341         guint Psot, nPsot;
342
343         if (end[0] == GST_J2K_MARKER && end[1] == GST_J2K_MARKER_EOC)
344           nPsot = avail - 2;
345         else
346           nPsot = avail;
347
348         Psot = GST_READ_UINT32_BE (&map.data[6]);
349         if (Psot != nPsot && Psot != 0) {
350           /* Psot must match the size of the tile */
351           GST_DEBUG_OBJECT (rtpj2kdepay, "set Psot from %u to %u", Psot, nPsot);
352           gst_buffer_unmap (buf, &map);
353
354           buf = gst_buffer_make_writable (buf);
355
356           gst_buffer_map (buf, &map, GST_MAP_WRITE);
357           GST_WRITE_UINT32_BE (&map.data[6], nPsot);
358         }
359       }
360       gst_buffer_unmap (buf, &map);
361     }
362
363     GST_DEBUG_OBJECT (rtpj2kdepay, "append pu packet of size %" G_GSIZE_FORMAT,
364         gst_buffer_get_size (buf));
365     gst_adapter_push (rtpj2kdepay->f_adapter, buf);
366   }
367   g_list_free (packets);
368
369 done:
370   rtpj2kdepay->last_tile = -1;
371
372   return ret;
373
374   /* errors */
375 waiting_header:
376   {
377     GST_DEBUG_OBJECT (rtpj2kdepay, "waiting for header %u", mh_id);
378     gst_adapter_clear (rtpj2kdepay->t_adapter);
379     rtpj2kdepay->last_tile = -1;
380     return ret;
381   }
382 invalid_tile:
383   {
384     GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE, ("Invalid tile"), (NULL));
385     gst_buffer_unmap (buf, &map);
386     gst_adapter_clear (rtpj2kdepay->t_adapter);
387     rtpj2kdepay->last_tile = -1;
388     return ret;
389   }
390 }
391
392 static GstFlowReturn
393 gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
394 {
395   GstRtpJ2KDepay *rtpj2kdepay;
396   guint8 end[2];
397   guint avail;
398
399   GstFlowReturn ret = GST_FLOW_OK;
400
401   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
402
403   /* flush pending tile */
404   gst_rtp_j2k_depay_flush_tile (depayload);
405
406   /* last buffer take all data out of the adapter */
407   avail = gst_adapter_available (rtpj2kdepay->f_adapter);
408   if (avail == 0)
409     goto done;
410
411   if (avail > 2) {
412     GstBuffer *outbuf;
413
414     /* take the last bytes of the JPEG 2000 data to see if there is an EOC
415      * marker */
416     gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
417
418     if (end[0] != GST_J2K_MARKER && end[1] != GST_J2K_MARKER_EOC) {
419       end[0] = GST_J2K_MARKER;
420       end[1] = GST_J2K_MARKER_EOC;
421
422       GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
423
424       /* no EOI marker, add one */
425       outbuf = gst_buffer_new_and_alloc (2);
426       gst_buffer_fill (outbuf, 0, end, 2);
427
428       gst_adapter_push (rtpj2kdepay->f_adapter, outbuf);
429       avail += 2;
430     }
431
432     GST_DEBUG_OBJECT (rtpj2kdepay, "pushing buffer of %u bytes", avail);
433     outbuf = gst_adapter_take_buffer (rtpj2kdepay->f_adapter, avail);
434     gst_rtp_drop_meta (GST_ELEMENT_CAST (depayload),
435         outbuf, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
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 }