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.
21 * SECTION:element-rtpj2kpay
23 * Payload encode JPEG 2000 images into RTP packets according to RFC 5371
25 * For detailed information see: https://datatracker.ietf.org/doc/rfc5371/
26 * and https://datatracker.ietf.org/doc/rfc5372/
28 * The payloader takes a JPEG 2000 image, scans it for "packetization
29 * units" and constructs the RTP packet header followed by the JPEG 2000
30 * codestream. A "packetization unit" is defined as either a JPEG 2000 main header,
31 * a JPEG 2000 tile-part header, or a JPEG 2000 packet.
41 #include <gst/rtp/gstrtpbuffer.h>
42 #include <gst/video/video.h>
44 #include "gstrtpj2kpay.h"
45 #include "gstrtputils.h"
47 static GstStaticPadTemplate gst_rtp_j2k_pay_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_STATIC_CAPS ("image/x-jpc")
54 static GstStaticPadTemplate gst_rtp_j2k_pay_src_template =
55 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS ("application/x-rtp, "
59 " media = (string) \"video\", "
60 " payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
61 " clock-rate = (int) 90000, "
62 " encoding-name = (string) \"JPEG2000\"")
65 GST_DEBUG_CATEGORY_STATIC (rtpj2kpay_debug);
66 #define GST_CAT_DEFAULT (rtpj2kpay_debug)
70 * @J2K_MARKER: Prefix for JPEG 2000 marker
71 * @J2K_MARKER_SOC: Start of Codestream
72 * @J2K_MARKER_SOT: Start of tile
73 * @J2K_MARKER_EOC: End of Codestream
75 * Identifers for markers in JPEG 2000 codestreams
80 J2K_MARKER_SOC = 0x4F,
81 J2K_MARKER_SOT = 0x90,
82 J2K_MARKER_SOP = 0x91,
83 J2K_MARKER_EPH = 0x92,
84 J2K_MARKER_SOD = 0x93,
105 #define HEADER_SIZE 8
107 static void gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
108 const GValue * value, GParamSpec * pspec);
109 static void gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
112 static gboolean gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload,
115 static GstFlowReturn gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * pad,
118 #define gst_rtp_j2k_pay_parent_class parent_class
119 G_DEFINE_TYPE (GstRtpJ2KPay, gst_rtp_j2k_pay, GST_TYPE_RTP_BASE_PAYLOAD);
122 gst_rtp_j2k_pay_class_init (GstRtpJ2KPayClass * klass)
124 GObjectClass *gobject_class;
125 GstElementClass *gstelement_class;
126 GstRTPBasePayloadClass *gstrtpbasepayload_class;
128 gobject_class = (GObjectClass *) klass;
129 gstelement_class = (GstElementClass *) klass;
130 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
132 gobject_class->set_property = gst_rtp_j2k_pay_set_property;
133 gobject_class->get_property = gst_rtp_j2k_pay_get_property;
135 gst_element_class_add_static_pad_template (gstelement_class,
136 &gst_rtp_j2k_pay_src_template);
137 gst_element_class_add_static_pad_template (gstelement_class,
138 &gst_rtp_j2k_pay_sink_template);
140 gst_element_class_set_static_metadata (gstelement_class,
141 "RTP JPEG 2000 payloader", "Codec/Payloader/Network/RTP",
142 "Payload-encodes JPEG 2000 pictures into RTP packets (RFC 5371)",
143 "Wim Taymans <wim.taymans@gmail.com>");
145 gstrtpbasepayload_class->set_caps = gst_rtp_j2k_pay_setcaps;
146 gstrtpbasepayload_class->handle_buffer = gst_rtp_j2k_pay_handle_buffer;
148 GST_DEBUG_CATEGORY_INIT (rtpj2kpay_debug, "rtpj2kpay", 0,
149 "JPEG 2000 RTP Payloader");
153 gst_rtp_j2k_pay_init (GstRtpJ2KPay * pay)
158 gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
160 GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
162 gint width = 0, height = 0;
165 pay = GST_RTP_J2K_PAY (basepayload);
167 /* these properties are not mandatory, we can get them from the stream */
168 if (gst_structure_get_int (caps_structure, "height", &height)) {
169 pay->height = height;
171 if (gst_structure_get_int (caps_structure, "width", &width)) {
175 gst_rtp_base_payload_set_options (basepayload, "video", TRUE, "JPEG2000",
177 res = gst_rtp_base_payload_set_outcaps (basepayload, NULL);
184 gst_rtp_j2k_pay_header_size (const guint8 * data, guint offset)
186 return data[offset] << 8 | data[offset + 1];
190 gst_rtp_j2k_pay_scan_marker (const guint8 * data, guint size, guint * offset)
192 while ((data[(*offset)++] != J2K_MARKER) && ((*offset) < size));
194 if (G_UNLIKELY ((*offset) >= size)) {
195 return J2K_MARKER_EOC;
197 guint8 marker = data[(*offset)++];
208 gboolean force_packet;
212 find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
213 guint offset, RtpJ2KState * state)
215 gboolean cut_sop = FALSE;
218 /* parse the j2k header for 'start of codestream' */
219 GST_LOG_OBJECT (pay, "checking from offset %u", offset);
220 while (offset < size) {
221 marker = gst_rtp_j2k_pay_scan_marker (data, size, &offset);
223 if (state->bitstream) {
224 /* parsing bitstream, only look for SOP */
227 GST_LOG_OBJECT (pay, "found SOP at %u", offset);
233 /* just skip over EPH */
234 GST_LOG_OBJECT (pay, "found EPH at %u", offset);
237 if (offset >= state->next_sot) {
238 GST_LOG_OBJECT (pay, "reached next SOT at %u", offset);
239 state->bitstream = FALSE;
240 state->force_packet = TRUE;
241 if (marker == J2K_MARKER_EOC && state->next_sot + 2 <= size)
242 /* include EOC but never go past the max size */
243 return state->next_sot + 2;
245 return state->next_sot;
252 GST_LOG_OBJECT (pay, "found SOC at %u", offset);
253 state->header.MHF = 1;
259 GST_LOG_OBJECT (pay, "found SOT at %u", offset);
260 /* we found SOT but also had a header first */
261 if (state->header.MHF) {
262 state->force_packet = TRUE;
266 /* parse SOT but do some sanity checks first */
267 len = gst_rtp_j2k_pay_header_size (data, offset);
268 GST_LOG_OBJECT (pay, "SOT length %u", len);
271 if (offset + len >= size)
274 if (state->n_tiles == 0)
275 /* first tile, T is valid */
278 /* more tiles, T becomes invalid */
280 state->header.tile = GST_READ_UINT16_BE (&data[offset + 2]);
283 /* get offset of next tile, if it's 0, it goes all the way to the end of
285 Psot = GST_READ_UINT32_BE (&data[offset + 4]);
287 state->next_sot = size;
289 state->next_sot = offset - 2 + Psot;
292 GST_LOG_OBJECT (pay, "Isot %u, Psot %u, next %u", state->header.tile,
293 Psot, state->next_sot);
297 GST_LOG_OBJECT (pay, "found SOD at %u", offset);
298 /* can't have more tiles now */
300 /* go to bitstream parsing */
301 state->bitstream = TRUE;
302 /* cut at the next SOP or else include all data */
304 /* force a new packet when we see SOP, this can be optional but the
305 * spec recommends packing headers separately */
306 state->force_packet = TRUE;
309 GST_LOG_OBJECT (pay, "found EOC at %u", offset);
313 guint len = gst_rtp_j2k_pay_header_size (data, offset);
314 GST_LOG_OBJECT (pay, "skip 0x%02x len %u", marker, len);
321 GST_DEBUG_OBJECT (pay, "reached end of data");
326 gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * basepayload,
330 GstClockTime timestamp;
331 GstFlowReturn ret = GST_FLOW_ERROR;
333 GstBufferList *list = NULL;
339 pay = GST_RTP_J2K_PAY (basepayload);
340 mtu = GST_RTP_BASE_PAYLOAD_MTU (pay);
342 gst_buffer_map (buffer, &map, GST_MAP_READ);
343 timestamp = GST_BUFFER_PTS (buffer);
344 offset = pos = end = 0;
347 "got buffer size %" G_GSIZE_FORMAT ", timestamp %" GST_TIME_FORMAT,
348 map.size, GST_TIME_ARGS (timestamp));
350 /* do some header defaults first */
351 state.header.tp = 0; /* only progressive scan */
352 state.header.MHF = 0; /* no header */
353 state.header.mh_id = 0; /* always 0 for now */
354 state.header.T = 1; /* invalid tile */
355 state.header.priority = 255; /* always 255 for now */
356 state.header.tile = 0; /* no tile number */
357 state.header.offset = 0; /* offset of 0 */
358 state.bitstream = FALSE;
361 state.force_packet = FALSE;
363 /* get max packet length */
364 max_size = gst_rtp_buffer_calc_payload_len (mtu - HEADER_SIZE, 0, 0);
366 list = gst_buffer_list_new_sized ((mtu / max_size) + 1);
373 GstRTPBuffer rtp = { NULL };
375 /* try to pack as much as we can */
377 /* see how much we have scanned already */
378 pu_size = end - offset;
379 GST_DEBUG_OBJECT (pay, "scanned pu size %u", pu_size);
381 /* we need to make a new packet */
382 if (state.force_packet) {
383 GST_DEBUG_OBJECT (pay, "need to force a new packet");
384 state.force_packet = FALSE;
389 /* else see if we have enough */
390 if (pu_size > max_size) {
392 /* the packet became too large, use previous scanpos */
393 pu_size = pos - offset;
395 /* the already scanned data was already too big, make sure we start
396 * scanning from the last searched position */
399 GST_DEBUG_OBJECT (pay, "max size exceeded pu_size %u", pu_size);
405 /* exit when finished */
409 /* scan next packetization unit and fill in the header */
410 end = find_pu_end (pay, map.data, map.size, pos, &state);
413 while (pu_size > 0) {
414 guint packet_size, data_size;
417 /* calculate the packet size */
419 gst_rtp_buffer_calc_packet_len (pu_size + HEADER_SIZE, 0, 0);
421 if (packet_size > mtu) {
422 GST_DEBUG_OBJECT (pay, "needed packet size %u clamped to MTU %u",
426 GST_DEBUG_OBJECT (pay, "needed packet size %u fits in MTU %u",
430 /* get total payload size and data size */
431 payload_size = gst_rtp_buffer_calc_payload_len (packet_size, 0, 0);
432 data_size = payload_size - HEADER_SIZE;
434 /* make buffer for header */
435 outbuf = gst_rtp_buffer_new_allocate (HEADER_SIZE, 0, 0);
437 GST_BUFFER_PTS (outbuf) = timestamp;
439 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
441 /* get pointer to header */
442 header = gst_rtp_buffer_get_payload (&rtp);
444 pu_size -= data_size;
446 /* reached the end of a packetization unit */
447 if (state.header.MHF) {
448 /* we were doing a header, see if all fit in one packet or if
449 * we had to fragment it */
451 state.header.MHF = 3;
453 state.header.MHF = 2;
456 gst_rtp_buffer_set_marker (&rtp, TRUE);
461 * @tp: type (0 progressive, 1 odd field, 2 even field)
462 * @MHF: Main Header Flag
463 * @mh_id: Main Header Identification
464 * @T: Tile field invalidation flag
465 * @priority: priority
466 * @tile number: the tile number of the payload
467 * @reserved: set to 0
468 * @fragment offset: the byte offset of the current payload
471 * 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
472 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
473 * |tp |MHF|mh_id|T| priority | tile number |
474 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
475 * |reserved | fragment offset |
476 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
478 header[0] = (state.header.tp << 6) | (state.header.MHF << 4) |
479 (state.header.mh_id << 1) | state.header.T;
480 header[1] = state.header.priority;
481 header[2] = state.header.tile >> 8;
482 header[3] = state.header.tile & 0xff;
484 header[5] = state.header.offset >> 16;
485 header[6] = (state.header.offset >> 8) & 0xff;
486 header[7] = state.header.offset & 0xff;
488 gst_rtp_buffer_unmap (&rtp);
490 /* make subbuffer of j2k data */
491 paybuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL,
493 gst_rtp_copy_meta (GST_ELEMENT_CAST (basepayload), outbuf, paybuf,
494 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
495 outbuf = gst_buffer_append (outbuf, paybuf);
497 gst_buffer_list_add (list, outbuf);
499 /* reset header for next round */
500 state.header.MHF = 0;
502 state.header.tile = 0;
505 state.header.offset = offset;
508 } while (offset < map.size);
510 gst_buffer_unref (buffer);
512 /* push the whole buffer list at once */
513 ret = gst_rtp_base_payload_push_list (basepayload, list);
519 gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
520 const GValue * value, GParamSpec * pspec)
524 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530 gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
531 GValue * value, GParamSpec * pspec)
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 gst_rtp_j2k_pay_plugin_init (GstPlugin * plugin)
543 return gst_element_register (plugin, "rtpj2kpay", GST_RANK_SECONDARY,
544 GST_TYPE_RTP_J2K_PAY);