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 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>
37 #include <gst/video/video.h>
39 #include "gstrtpj2kpay.h"
40 #include "gstrtputils.h"
42 static GstStaticPadTemplate gst_rtp_j2k_pay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("image/x-jpc")
49 static GstStaticPadTemplate gst_rtp_j2k_pay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("application/x-rtp, "
54 " media = (string) \"video\", "
55 " payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56 " clock-rate = (int) 90000, "
57 " encoding-name = (string) \"JPEG2000\"")
60 GST_DEBUG_CATEGORY_STATIC (rtpj2kpay_debug);
61 #define GST_CAT_DEFAULT (rtpj2kpay_debug)
65 * @J2K_MARKER: Prefix for JPEG 2000 marker
66 * @J2K_MARKER_SOC: Start of Codestream
67 * @J2K_MARKER_SOT: Start of tile
68 * @J2K_MARKER_EOC: End of Codestream
70 * Identifers for markers in JPEG 2000 codestreams
75 J2K_MARKER_SOC = 0x4F,
76 J2K_MARKER_SOT = 0x90,
77 J2K_MARKER_SOP = 0x91,
78 J2K_MARKER_EPH = 0x92,
79 J2K_MARKER_SOD = 0x93,
100 #define HEADER_SIZE 8
102 static void gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static gboolean gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload,
110 static GstFlowReturn gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * pad,
113 #define gst_rtp_j2k_pay_parent_class parent_class
114 G_DEFINE_TYPE (GstRtpJ2KPay, gst_rtp_j2k_pay, GST_TYPE_RTP_BASE_PAYLOAD);
117 gst_rtp_j2k_pay_class_init (GstRtpJ2KPayClass * klass)
119 GObjectClass *gobject_class;
120 GstElementClass *gstelement_class;
121 GstRTPBasePayloadClass *gstrtpbasepayload_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
125 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
127 gobject_class->set_property = gst_rtp_j2k_pay_set_property;
128 gobject_class->get_property = gst_rtp_j2k_pay_get_property;
130 gst_element_class_add_pad_template (gstelement_class,
131 gst_static_pad_template_get (&gst_rtp_j2k_pay_src_template));
132 gst_element_class_add_pad_template (gstelement_class,
133 gst_static_pad_template_get (&gst_rtp_j2k_pay_sink_template));
135 gst_element_class_set_static_metadata (gstelement_class,
136 "RTP JPEG 2000 payloader", "Codec/Payloader/Network/RTP",
137 "Payload-encodes JPEG 2000 pictures into RTP packets (RFC 5371)",
138 "Wim Taymans <wim.taymans@gmail.com>");
140 gstrtpbasepayload_class->set_caps = gst_rtp_j2k_pay_setcaps;
141 gstrtpbasepayload_class->handle_buffer = gst_rtp_j2k_pay_handle_buffer;
143 GST_DEBUG_CATEGORY_INIT (rtpj2kpay_debug, "rtpj2kpay", 0,
144 "JPEG 2000 RTP Payloader");
148 gst_rtp_j2k_pay_init (GstRtpJ2KPay * pay)
153 gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
155 GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
157 gint width = 0, height = 0;
160 pay = GST_RTP_J2K_PAY (basepayload);
162 /* these properties are not mandatory, we can get them from the stream */
163 if (gst_structure_get_int (caps_structure, "height", &height)) {
164 pay->height = height;
166 if (gst_structure_get_int (caps_structure, "width", &width)) {
170 gst_rtp_base_payload_set_options (basepayload, "video", TRUE, "JPEG2000",
172 res = gst_rtp_base_payload_set_outcaps (basepayload, NULL);
179 gst_rtp_j2k_pay_header_size (const guint8 * data, guint offset)
181 return data[offset] << 8 | data[offset + 1];
185 gst_rtp_j2k_pay_scan_marker (const guint8 * data, guint size, guint * offset)
187 while ((data[(*offset)++] != J2K_MARKER) && ((*offset) < size));
189 if (G_UNLIKELY ((*offset) >= size)) {
190 return J2K_MARKER_EOC;
192 guint8 marker = data[(*offset)++];
203 gboolean force_packet;
207 find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
208 guint offset, RtpJ2KState * state)
210 gboolean cut_sop = FALSE;
213 /* parse the j2k header for 'start of codestream' */
214 GST_LOG_OBJECT (pay, "checking from offset %u", offset);
215 while (offset < size) {
216 marker = gst_rtp_j2k_pay_scan_marker (data, size, &offset);
218 if (state->bitstream) {
219 /* parsing bitstream, only look for SOP */
222 GST_LOG_OBJECT (pay, "found SOP at %u", offset);
228 /* just skip over EPH */
229 GST_LOG_OBJECT (pay, "found EPH at %u", offset);
232 if (offset >= state->next_sot) {
233 GST_LOG_OBJECT (pay, "reached next SOT at %u", offset);
234 state->bitstream = FALSE;
235 state->force_packet = TRUE;
236 if (marker == J2K_MARKER_EOC && state->next_sot + 2 <= size)
237 /* include EOC but never go past the max size */
238 return state->next_sot + 2;
240 return state->next_sot;
247 GST_LOG_OBJECT (pay, "found SOC at %u", offset);
248 state->header.MHF = 1;
254 GST_LOG_OBJECT (pay, "found SOT at %u", offset);
255 /* we found SOT but also had a header first */
256 if (state->header.MHF) {
257 state->force_packet = TRUE;
261 /* parse SOT but do some sanity checks first */
262 len = gst_rtp_j2k_pay_header_size (data, offset);
263 GST_LOG_OBJECT (pay, "SOT length %u", len);
266 if (offset + len >= size)
269 if (state->n_tiles == 0)
270 /* first tile, T is valid */
273 /* more tiles, T becomes invalid */
275 state->header.tile = GST_READ_UINT16_BE (&data[offset + 2]);
278 /* get offset of next tile, if it's 0, it goes all the way to the end of
280 Psot = GST_READ_UINT32_BE (&data[offset + 4]);
282 state->next_sot = size;
284 state->next_sot = offset - 2 + Psot;
287 GST_LOG_OBJECT (pay, "Isot %u, Psot %u, next %u", state->header.tile,
288 Psot, state->next_sot);
292 GST_LOG_OBJECT (pay, "found SOD at %u", offset);
293 /* can't have more tiles now */
295 /* go to bitstream parsing */
296 state->bitstream = TRUE;
297 /* cut at the next SOP or else include all data */
299 /* force a new packet when we see SOP, this can be optional but the
300 * spec recommends packing headers separately */
301 state->force_packet = TRUE;
304 GST_LOG_OBJECT (pay, "found EOC at %u", offset);
308 guint len = gst_rtp_j2k_pay_header_size (data, offset);
309 GST_LOG_OBJECT (pay, "skip 0x%02x len %u", marker, len);
316 GST_DEBUG_OBJECT (pay, "reached end of data");
321 gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * basepayload,
325 GstClockTime timestamp;
326 GstFlowReturn ret = GST_FLOW_ERROR;
328 GstBufferList *list = NULL;
334 pay = GST_RTP_J2K_PAY (basepayload);
335 mtu = GST_RTP_BASE_PAYLOAD_MTU (pay);
337 gst_buffer_map (buffer, &map, GST_MAP_READ);
338 timestamp = GST_BUFFER_PTS (buffer);
339 offset = pos = end = 0;
342 "got buffer size %" G_GSIZE_FORMAT ", timestamp %" GST_TIME_FORMAT,
343 map.size, GST_TIME_ARGS (timestamp));
345 /* do some header defaults first */
346 state.header.tp = 0; /* only progressive scan */
347 state.header.MHF = 0; /* no header */
348 state.header.mh_id = 0; /* always 0 for now */
349 state.header.T = 1; /* invalid tile */
350 state.header.priority = 255; /* always 255 for now */
351 state.header.tile = 0; /* no tile number */
352 state.header.offset = 0; /* offset of 0 */
353 state.bitstream = FALSE;
356 state.force_packet = FALSE;
358 /* get max packet length */
359 max_size = gst_rtp_buffer_calc_payload_len (mtu - HEADER_SIZE, 0, 0);
361 list = gst_buffer_list_new_sized ((mtu / max_size) + 1);
368 GstRTPBuffer rtp = { NULL };
370 /* try to pack as much as we can */
372 /* see how much we have scanned already */
373 pu_size = end - offset;
374 GST_DEBUG_OBJECT (pay, "scanned pu size %u", pu_size);
376 /* we need to make a new packet */
377 if (state.force_packet) {
378 GST_DEBUG_OBJECT (pay, "need to force a new packet");
379 state.force_packet = FALSE;
384 /* else see if we have enough */
385 if (pu_size > max_size) {
387 /* the packet became too large, use previous scanpos */
388 pu_size = pos - offset;
390 /* the already scanned data was already too big, make sure we start
391 * scanning from the last searched position */
394 GST_DEBUG_OBJECT (pay, "max size exceeded pu_size %u", pu_size);
400 /* exit when finished */
404 /* scan next packetization unit and fill in the header */
405 end = find_pu_end (pay, map.data, map.size, pos, &state);
408 while (pu_size > 0) {
409 guint packet_size, data_size;
412 /* calculate the packet size */
414 gst_rtp_buffer_calc_packet_len (pu_size + HEADER_SIZE, 0, 0);
416 if (packet_size > mtu) {
417 GST_DEBUG_OBJECT (pay, "needed packet size %u clamped to MTU %u",
421 GST_DEBUG_OBJECT (pay, "needed packet size %u fits in MTU %u",
425 /* get total payload size and data size */
426 payload_size = gst_rtp_buffer_calc_payload_len (packet_size, 0, 0);
427 data_size = payload_size - HEADER_SIZE;
429 /* make buffer for header */
430 outbuf = gst_rtp_buffer_new_allocate (HEADER_SIZE, 0, 0);
432 GST_BUFFER_PTS (outbuf) = timestamp;
434 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
436 /* get pointer to header */
437 header = gst_rtp_buffer_get_payload (&rtp);
439 pu_size -= data_size;
441 /* reached the end of a packetization unit */
442 if (state.header.MHF) {
443 /* we were doing a header, see if all fit in one packet or if
444 * we had to fragment it */
446 state.header.MHF = 3;
448 state.header.MHF = 2;
451 gst_rtp_buffer_set_marker (&rtp, TRUE);
456 * @tp: type (0 progressive, 1 odd field, 2 even field)
457 * @MHF: Main Header Flag
458 * @mh_id: Main Header Identification
459 * @T: Tile field invalidation flag
460 * @priority: priority
461 * @tile number: the tile number of the payload
462 * @reserved: set to 0
463 * @fragment offset: the byte offset of the current payload
466 * 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
467 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
468 * |tp |MHF|mh_id|T| priority | tile number |
469 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
470 * |reserved | fragment offset |
471 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
473 header[0] = (state.header.tp << 6) | (state.header.MHF << 4) |
474 (state.header.mh_id << 1) | state.header.T;
475 header[1] = state.header.priority;
476 header[2] = state.header.tile >> 8;
477 header[3] = state.header.tile & 0xff;
479 header[5] = state.header.offset >> 16;
480 header[6] = (state.header.offset >> 8) & 0xff;
481 header[7] = state.header.offset & 0xff;
483 gst_rtp_buffer_unmap (&rtp);
485 /* make subbuffer of j2k data */
486 paybuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL,
488 gst_rtp_copy_meta (GST_ELEMENT_CAST (basepayload), outbuf, paybuf,
489 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
490 outbuf = gst_buffer_append (outbuf, paybuf);
492 gst_buffer_list_add (list, outbuf);
494 /* reset header for next round */
495 state.header.MHF = 0;
497 state.header.tile = 0;
500 state.header.offset = offset;
503 } while (offset < map.size);
505 gst_buffer_unref (buffer);
507 /* push the whole buffer list at once */
508 ret = gst_rtp_base_payload_push_list (basepayload, list);
514 gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
515 const GValue * value, GParamSpec * pspec)
519 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
525 gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
526 GValue * value, GParamSpec * pspec)
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536 gst_rtp_j2k_pay_plugin_init (GstPlugin * plugin)
538 return gst_element_register (plugin, "rtpj2kpay", GST_RANK_SECONDARY,
539 GST_TYPE_RTP_J2K_PAY);