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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-rtpj2kpay
23 * Payload encode JPEG 2000 pictures into RTP packets according to RFC 5371.
24 * For detailed information see: http://www.rfc-editor.org/rfc/rfc5371.txt
26 * The payloader takes a JPEG 2000 picture, scans the header for packetization
27 * units and constructs the RTP packet header followed by the actual JPEG 2000
36 #include <gst/rtp/gstrtpbuffer.h>
38 #include "gstrtpj2kpay.h"
40 static GstStaticPadTemplate gst_rtp_j2k_pay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("image/x-jpc")
47 static GstStaticPadTemplate gst_rtp_j2k_pay_src_template =
48 GST_STATIC_PAD_TEMPLATE ("src",
51 GST_STATIC_CAPS ("application/x-rtp, "
52 " media = (string) \"video\", "
53 " payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
54 " clock-rate = (int) 90000, "
55 " encoding-name = (string) \"JPEG2000\"")
58 GST_DEBUG_CATEGORY_STATIC (rtpj2kpay_debug);
59 #define GST_CAT_DEFAULT (rtpj2kpay_debug)
63 * @J2K_MARKER: Prefix for JPEG 2000 marker
64 * @J2K_MARKER_SOC: Start of Codestream
65 * @J2K_MARKER_SOT: Start of tile
66 * @J2K_MARKER_EOC: End of Codestream
68 * Identifers for markers in JPEG 2000 codestreams
73 J2K_MARKER_SOC = 0x4F,
74 J2K_MARKER_SOT = 0x90,
75 J2K_MARKER_SOP = 0x91,
76 J2K_MARKER_EPH = 0x92,
77 J2K_MARKER_SOD = 0x93,
81 #define DEFAULT_BUFFER_LIST TRUE
101 #define HEADER_SIZE 8
103 static void gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
104 const GValue * value, GParamSpec * pspec);
105 static void gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
106 GValue * value, GParamSpec * pspec);
108 static gboolean gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload,
111 static GstFlowReturn gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * pad,
114 #define gst_rtp_j2k_pay_parent_class parent_class
115 G_DEFINE_TYPE (GstRtpJ2KPay, gst_rtp_j2k_pay, GST_TYPE_RTP_BASE_PAYLOAD);
118 gst_rtp_j2k_pay_class_init (GstRtpJ2KPayClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
122 GstRTPBasePayloadClass *gstrtpbasepayload_class;
124 gobject_class = (GObjectClass *) klass;
125 gstelement_class = (GstElementClass *) klass;
126 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
128 gobject_class->set_property = gst_rtp_j2k_pay_set_property;
129 gobject_class->get_property = gst_rtp_j2k_pay_get_property;
131 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
132 g_param_spec_boolean ("buffer-list", "Buffer List",
134 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136 gst_element_class_add_pad_template (gstelement_class,
137 gst_static_pad_template_get (&gst_rtp_j2k_pay_src_template));
138 gst_element_class_add_pad_template (gstelement_class,
139 gst_static_pad_template_get (&gst_rtp_j2k_pay_sink_template));
141 gst_element_class_set_details_simple (gstelement_class,
142 "RTP JPEG 2000 payloader", "Codec/Payloader/Network/RTP",
143 "Payload-encodes JPEG 2000 pictures into RTP packets (RFC 5371)",
144 "Wim Taymans <wim.taymans@gmail.com>");
146 gstrtpbasepayload_class->set_caps = gst_rtp_j2k_pay_setcaps;
147 gstrtpbasepayload_class->handle_buffer = gst_rtp_j2k_pay_handle_buffer;
149 GST_DEBUG_CATEGORY_INIT (rtpj2kpay_debug, "rtpj2kpay", 0,
150 "JPEG 2000 RTP Payloader");
154 gst_rtp_j2k_pay_init (GstRtpJ2KPay * pay)
156 pay->buffer_list = DEFAULT_BUFFER_LIST;
160 gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
162 GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
164 gint width = 0, height = 0;
167 pay = GST_RTP_J2K_PAY (basepayload);
169 /* these properties are not mandatory, we can get them from the stream */
170 if (gst_structure_get_int (caps_structure, "height", &height)) {
171 pay->height = height;
173 if (gst_structure_get_int (caps_structure, "width", &width)) {
177 gst_rtp_base_payload_set_options (basepayload, "video", TRUE, "JPEG2000",
179 res = gst_rtp_base_payload_set_outcaps (basepayload, NULL);
186 gst_rtp_j2k_pay_header_size (const guint8 * data, guint offset)
188 return data[offset] << 8 | data[offset + 1];
192 gst_rtp_j2k_pay_scan_marker (const guint8 * data, guint size, guint * offset)
194 while ((data[(*offset)++] != J2K_MARKER) && ((*offset) < size));
196 if (G_UNLIKELY ((*offset) >= size)) {
197 return J2K_MARKER_EOC;
199 guint8 marker = data[(*offset)++];
210 gboolean force_packet;
214 find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
215 guint offset, RtpJ2KState * state)
217 gboolean cut_sop = FALSE;
220 /* parse the j2k header for 'start of codestream' */
221 GST_LOG_OBJECT (pay, "checking from offset %u", offset);
222 while (offset < size) {
223 marker = gst_rtp_j2k_pay_scan_marker (data, size, &offset);
225 if (state->bitstream) {
226 /* parsing bitstream, only look for SOP */
229 GST_LOG_OBJECT (pay, "found SOP at %u", offset);
235 /* just skip over EPH */
236 GST_LOG_OBJECT (pay, "found EPH at %u", offset);
239 if (offset >= state->next_sot) {
240 GST_LOG_OBJECT (pay, "reached next SOT at %u", offset);
241 state->bitstream = FALSE;
242 state->force_packet = TRUE;
243 if (marker == J2K_MARKER_EOC && state->next_sot + 2 <= size)
244 /* include EOC but never go past the max size */
245 return state->next_sot + 2;
247 return state->next_sot;
254 GST_LOG_OBJECT (pay, "found SOC at %u", offset);
255 state->header.MHF = 1;
261 GST_LOG_OBJECT (pay, "found SOT at %u", offset);
262 /* we found SOT but also had a header first */
263 if (state->header.MHF) {
264 state->force_packet = TRUE;
268 /* parse SOT but do some sanity checks first */
269 len = gst_rtp_j2k_pay_header_size (data, offset);
270 GST_LOG_OBJECT (pay, "SOT length %u", len);
273 if (offset + len >= size)
276 if (state->n_tiles == 0)
277 /* first tile, T is valid */
280 /* more tiles, T becomes invalid */
282 state->header.tile = GST_READ_UINT16_BE (&data[offset + 2]);
285 /* get offset of next tile, if it's 0, it goes all the way to the end of
287 Psot = GST_READ_UINT32_BE (&data[offset + 4]);
289 state->next_sot = size;
291 state->next_sot = offset - 2 + Psot;
294 GST_LOG_OBJECT (pay, "Isot %u, Psot %u, next %u", state->header.tile,
295 Psot, state->next_sot);
299 GST_LOG_OBJECT (pay, "found SOD at %u", offset);
300 /* can't have more tiles now */
302 /* go to bitstream parsing */
303 state->bitstream = TRUE;
304 /* cut at the next SOP or else include all data */
306 /* force a new packet when we see SOP, this can be optional but the
307 * spec recommends packing headers separately */
308 state->force_packet = TRUE;
311 GST_LOG_OBJECT (pay, "found EOC at %u", offset);
315 guint len = gst_rtp_j2k_pay_header_size (data, offset);
316 GST_LOG_OBJECT (pay, "skip 0x%02x len %u", marker, len);
323 GST_DEBUG_OBJECT (pay, "reached end of data");
328 gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * basepayload,
332 GstClockTime timestamp;
333 GstFlowReturn ret = GST_FLOW_ERROR;
336 GstBufferList *list = NULL;
343 pay = GST_RTP_J2K_PAY (basepayload);
344 mtu = GST_RTP_BASE_PAYLOAD_MTU (pay);
346 gst_buffer_map (buffer, &map, GST_MAP_READ);
347 timestamp = GST_BUFFER_TIMESTAMP (buffer);
348 offset = pos = end = 0;
351 "got buffer size %" G_GSIZE_FORMAT ", timestamp %" GST_TIME_FORMAT,
352 map.size, GST_TIME_ARGS (timestamp));
354 /* do some header defaults first */
355 state.header.tp = 0; /* only progressive scan */
356 state.header.MHF = 0; /* no header */
357 state.header.mh_id = 0; /* always 0 for now */
358 state.header.T = 1; /* invalid tile */
359 state.header.priority = 255; /* always 255 for now */
360 state.header.tile = 0; /* no tile number */
361 state.header.offset = 0; /* offset of 0 */
362 state.bitstream = FALSE;
365 state.force_packet = FALSE;
368 if (pay->buffer_list) {
369 list = gst_buffer_list_new ();
370 it = gst_buffer_list_iterate (list);
374 /* get max packet length */
375 max_size = gst_rtp_buffer_calc_payload_len (mtu - HEADER_SIZE, 0, 0);
382 GstRTPBuffer rtp = { NULL };
384 /* try to pack as much as we can */
386 /* see how much we have scanned already */
387 pu_size = end - offset;
388 GST_DEBUG_OBJECT (pay, "scanned pu size %u", pu_size);
390 /* we need to make a new packet */
391 if (state.force_packet) {
392 GST_DEBUG_OBJECT (pay, "need to force a new packet");
393 state.force_packet = FALSE;
398 /* else see if we have enough */
399 if (pu_size > max_size) {
401 /* the packet became too large, use previous scanpos */
402 pu_size = pos - offset;
404 /* the already scanned data was already too big, make sure we start
405 * scanning from the last searched position */
408 GST_DEBUG_OBJECT (pay, "max size exceeded pu_size %u", pu_size);
414 /* exit when finished */
418 /* scan next packetization unit and fill in the header */
419 end = find_pu_end (pay, map.data, map.size, pos, &state);
422 while (pu_size > 0) {
423 guint packet_size, data_size;
425 /* calculate the packet size */
427 gst_rtp_buffer_calc_packet_len (pu_size + HEADER_SIZE, 0, 0);
429 if (packet_size > mtu) {
430 GST_DEBUG_OBJECT (pay, "needed packet size %u clamped to MTU %u",
434 GST_DEBUG_OBJECT (pay, "needed packet size %u fits in MTU %u",
438 /* get total payload size and data size */
439 payload_size = gst_rtp_buffer_calc_payload_len (packet_size, 0, 0);
440 data_size = payload_size - HEADER_SIZE;
443 if (pay->buffer_list) {
444 /* make buffer for header */
445 outbuf = gst_rtp_buffer_new_allocate (HEADER_SIZE, 0, 0);
449 /* make buffer for header and data */
450 outbuf = gst_rtp_buffer_new_allocate (payload_size, 0, 0);
452 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
454 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
456 /* get pointer to header */
457 header = gst_rtp_buffer_get_payload (&rtp);
459 pu_size -= data_size;
461 /* reached the end of a packetization unit */
462 if (state.header.MHF) {
463 /* we were doing a header, see if all fit in one packet or if
464 * we had to fragment it */
466 state.header.MHF = 3;
468 state.header.MHF = 2;
471 gst_rtp_buffer_set_marker (&rtp, TRUE);
476 * @tp: type (0 progressive, 1 odd field, 2 even field)
477 * @MHF: Main Header Flag
478 * @mh_id: Main Header Identification
479 * @T: Tile field invalidation flag
480 * @priority: priority
481 * @tile number: the tile number of the payload
482 * @reserved: set to 0
483 * @fragment offset: the byte offset of the current payload
486 * 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
487 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
488 * |tp |MHF|mh_id|T| priority | tile number |
489 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
490 * |reserved | fragment offset |
491 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493 header[0] = (state.header.tp << 6) | (state.header.MHF << 4) |
494 (state.header.mh_id << 1) | state.header.T;
495 header[1] = state.header.priority;
496 header[2] = state.header.tile >> 8;
497 header[3] = state.header.tile & 0xff;
499 header[5] = state.header.offset >> 16;
500 header[6] = (state.header.offset >> 8) & 0xff;
501 header[7] = state.header.offset & 0xff;
504 if (pay->buffer_list) {
507 /* make subbuffer of j2k data */
508 paybuf = gst_buffer_create_sub (buffer, offset, data_size);
510 /* create a new group to hold the header and the payload */
511 gst_buffer_list_iterator_add_group (it);
513 /* add both buffers to the buffer list */
514 gst_buffer_list_iterator_add (it, outbuf);
515 gst_buffer_list_iterator_add (it, paybuf);
520 memcpy (header + HEADER_SIZE, &map.data[offset], data_size);
521 gst_rtp_buffer_unmap (&rtp);
523 ret = gst_rtp_base_payload_push (basepayload, outbuf);
524 if (ret != GST_FLOW_OK)
528 /* reset header for next round */
529 state.header.MHF = 0;
531 state.header.tile = 0;
536 } while (offset < map.size);
539 gst_buffer_unref (buffer);
542 if (pay->buffer_list) {
543 /* free iterator and push the whole buffer list at once */
544 gst_buffer_list_iterator_free (it);
545 ret = gst_rtp_base_payload_push_list (basepayload, list);
553 gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
554 const GValue * value, GParamSpec * pspec)
556 GstRtpJ2KPay *rtpj2kpay;
558 rtpj2kpay = GST_RTP_J2K_PAY (object);
561 case PROP_BUFFER_LIST:
562 rtpj2kpay->buffer_list = g_value_get_boolean (value);
565 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571 gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
572 GValue * value, GParamSpec * pspec)
574 GstRtpJ2KPay *rtpj2kpay;
576 rtpj2kpay = GST_RTP_J2K_PAY (object);
579 case PROP_BUFFER_LIST:
580 g_value_set_boolean (value, rtpj2kpay->buffer_list);
583 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
589 gst_rtp_j2k_pay_plugin_init (GstPlugin * plugin)
591 return gst_element_register (plugin, "rtpj2kpay", GST_RANK_SECONDARY,
592 GST_TYPE_RTP_J2K_PAY);