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
gst_rtp_j2k_depay_class_init(GstRtpJ2KDepayClass * klass)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
gst_rtp_j2k_depay_init(GstRtpJ2KDepay * rtpj2kdepay)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
store_mheader(GstRtpJ2KDepay * rtpj2kdepay,guint idx,GstBuffer * buf)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
clear_mheaders(GstRtpJ2KDepay * rtpj2kdepay)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
gst_rtp_j2k_depay_reset(GstRtpJ2KDepay * rtpj2kdepay)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
gst_rtp_j2k_depay_finalize(GObject * object)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
gst_rtp_j2k_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)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
gst_rtp_j2k_depay_clear_pu(GstRtpJ2KDepay * rtpj2kdepay)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
gst_rtp_j2k_depay_flush_pu(GstRTPBaseDepayload * depayload)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
gst_rtp_j2k_depay_flush_tile(GstRTPBaseDepayload * depayload)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
gst_rtp_j2k_depay_flush_frame(GstRTPBaseDepayload * depayload)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_non_video_meta (depayload, outbuf);
435     ret = gst_rtp_base_depayload_push (depayload, outbuf);
436   } else {
437     GST_WARNING_OBJECT (rtpj2kdepay, "empty packet");
438     gst_adapter_clear (rtpj2kdepay->f_adapter);
439   }
440 
441   /* we accept any mh_id now */
442   rtpj2kdepay->last_mh_id = -1;
443 
444   /* reset state */
445   rtpj2kdepay->next_frag = 0;
446   rtpj2kdepay->have_sync = FALSE;
447 
448 done:
449   /* we can't keep headers with mh_id of 0 */
450   store_mheader (rtpj2kdepay, 0, NULL);
451 
452   return ret;
453 }
454 
455 static GstBuffer *
gst_rtp_j2k_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)456 gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
457 {
458   GstRtpJ2KDepay *rtpj2kdepay;
459   guint8 *payload;
460   guint MHF, mh_id, frag_offset, tile, payload_len, j2klen;
461   gint gap;
462   guint32 rtptime;
463 
464   rtpj2kdepay = GST_RTP_J2K_DEPAY (depayload);
465 
466   payload = gst_rtp_buffer_get_payload (rtp);
467   payload_len = gst_rtp_buffer_get_payload_len (rtp);
468 
469   /* we need at least a header */
470   if (payload_len < GST_RTP_J2K_HEADER_SIZE)
471     goto empty_packet;
472 
473   rtptime = gst_rtp_buffer_get_timestamp (rtp);
474 
475   /* new timestamp marks new frame */
476   if (rtpj2kdepay->last_rtptime != rtptime) {
477     rtpj2kdepay->last_rtptime = rtptime;
478     /* flush pending frame */
479     gst_rtp_j2k_depay_flush_frame (depayload);
480   }
481 
482   /*
483    *  0                   1                   2                   3
484    *  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
485    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
486    * |tp |MHF|mh_id|T|     priority  |           tile number         |
487    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
488    * |reserved       |             fragment offset                   |
489    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
490    */
491   MHF = (payload[0] & 0x30) >> 4;
492   mh_id = (payload[0] & 0xe) >> 1;
493 
494   if (rtpj2kdepay->last_mh_id == -1)
495     rtpj2kdepay->last_mh_id = mh_id;
496   else if (rtpj2kdepay->last_mh_id != mh_id)
497     goto wrong_mh_id;
498 
499   tile = (payload[2] << 8) | payload[3];
500   frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
501   j2klen = payload_len - GST_RTP_J2K_HEADER_SIZE;
502 
503   GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
504       tile, frag_offset, rtpj2kdepay->next_frag);
505 
506   /* calculate the gap between expected frag */
507   gap = frag_offset - rtpj2kdepay->next_frag;
508   /* calculate next frag */
509   rtpj2kdepay->next_frag = frag_offset + j2klen;
510 
511   if (gap != 0) {
512     GST_DEBUG_OBJECT (rtpj2kdepay, "discont of %d, clear PU", gap);
513     /* discont, clear pu adapter and resync */
514     gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
515   }
516 
517   /* check for sync code */
518   if (j2klen > 2 && payload[GST_RTP_J2K_HEADER_SIZE] == GST_J2K_MARKER) {
519     guint marker = payload[GST_RTP_J2K_HEADER_SIZE + 1];
520 
521     /* packets must start with SOC, SOT or SOP */
522     switch (marker) {
523       case GST_J2K_MARKER_SOC:
524         GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
525         /* flush the previous frame, should have happened when the timestamp
526          * changed above. */
527         gst_rtp_j2k_depay_flush_frame (depayload);
528         rtpj2kdepay->have_sync = TRUE;
529         break;
530       case GST_J2K_MARKER_SOT:
531         /* flush the previous tile */
532         gst_rtp_j2k_depay_flush_tile (depayload);
533         GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
534         rtpj2kdepay->have_sync = TRUE;
535         /* we sync on the tile now */
536         rtpj2kdepay->last_tile = tile;
537         break;
538       case GST_J2K_MARKER_SOP:
539         GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
540         /* flush the previous PU */
541         gst_rtp_j2k_depay_flush_pu (depayload);
542         if (rtpj2kdepay->last_tile != tile) {
543           /* wrong tile, we lose sync and we need a new SOT or SOC to regain
544            * sync. First flush out the previous tile if we have one. */
545           if (rtpj2kdepay->last_tile != -1)
546             gst_rtp_j2k_depay_flush_tile (depayload);
547           /* now we have no more valid tile and no sync */
548           rtpj2kdepay->last_tile = -1;
549           rtpj2kdepay->have_sync = FALSE;
550         } else {
551           rtpj2kdepay->have_sync = TRUE;
552         }
553         break;
554       default:
555         GST_DEBUG_OBJECT (rtpj2kdepay, "no sync packet 0x%02d", marker);
556         break;
557     }
558   }
559 
560   if (rtpj2kdepay->have_sync) {
561     GstBuffer *pu_frag;
562 
563     if (gst_adapter_available (rtpj2kdepay->pu_adapter) == 0) {
564       /* first part of pu, record state */
565       GST_DEBUG_OBJECT (rtpj2kdepay, "first PU");
566       rtpj2kdepay->pu_MHF = MHF;
567     }
568     /* and push in pu adapter */
569     GST_DEBUG_OBJECT (rtpj2kdepay, "push pu of size %u in adapter", j2klen);
570     pu_frag = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
571     gst_adapter_push (rtpj2kdepay->pu_adapter, pu_frag);
572 
573     if (MHF & 2) {
574       /* last part of main header received, we can flush it */
575       GST_DEBUG_OBJECT (rtpj2kdepay, "header end, flush pu");
576       gst_rtp_j2k_depay_flush_pu (depayload);
577     }
578   } else {
579     GST_DEBUG_OBJECT (rtpj2kdepay, "discard packet, no sync");
580   }
581 
582   /* marker bit finishes the frame */
583   if (gst_rtp_buffer_get_marker (rtp)) {
584     GST_DEBUG_OBJECT (rtpj2kdepay, "marker set, last buffer");
585     /* then flush frame */
586     gst_rtp_j2k_depay_flush_frame (depayload);
587   }
588 
589   return NULL;
590 
591   /* ERRORS */
592 empty_packet:
593   {
594     GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
595         ("Empty Payload."), (NULL));
596     return NULL;
597   }
598 wrong_mh_id:
599   {
600     GST_ELEMENT_WARNING (rtpj2kdepay, STREAM, DECODE,
601         ("Invalid mh_id %u, expected %u", mh_id, rtpj2kdepay->last_mh_id),
602         (NULL));
603     gst_rtp_j2k_depay_clear_pu (rtpj2kdepay);
604     return NULL;
605   }
606 }
607 
608 static void
gst_rtp_j2k_depay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)609 gst_rtp_j2k_depay_set_property (GObject * object, guint prop_id,
610     const GValue * value, GParamSpec * pspec)
611 {
612   switch (prop_id) {
613     default:
614       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
615       break;
616   }
617 }
618 
619 static void
gst_rtp_j2k_depay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)620 gst_rtp_j2k_depay_get_property (GObject * object, guint prop_id,
621     GValue * value, GParamSpec * pspec)
622 {
623   switch (prop_id) {
624     default:
625       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
626       break;
627   }
628 }
629 
630 static GstStateChangeReturn
gst_rtp_j2k_depay_change_state(GstElement * element,GstStateChange transition)631 gst_rtp_j2k_depay_change_state (GstElement * element, GstStateChange transition)
632 {
633   GstRtpJ2KDepay *rtpj2kdepay;
634   GstStateChangeReturn ret;
635 
636   rtpj2kdepay = GST_RTP_J2K_DEPAY (element);
637 
638   switch (transition) {
639     case GST_STATE_CHANGE_NULL_TO_READY:
640       break;
641     case GST_STATE_CHANGE_READY_TO_PAUSED:
642       gst_rtp_j2k_depay_reset (rtpj2kdepay);
643       break;
644     default:
645       break;
646   }
647 
648   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
649 
650   switch (transition) {
651     case GST_STATE_CHANGE_PAUSED_TO_READY:
652       gst_rtp_j2k_depay_reset (rtpj2kdepay);
653       break;
654     case GST_STATE_CHANGE_READY_TO_NULL:
655       break;
656     default:
657       break;
658   }
659   return ret;
660 }
661 
662 gboolean
gst_rtp_j2k_depay_plugin_init(GstPlugin * plugin)663 gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin)
664 {
665   return gst_element_register (plugin, "rtpj2kdepay",
666       GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_DEPAY);
667 }
668