2009-05-08 08:38:42 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-05-08 08:38:42 +00:00
|
|
|
*/
|
|
|
|
|
2016-05-11 19:01:32 +00:00
|
|
|
/**
|
2010-04-08 14:17:06 +00:00
|
|
|
* SECTION:element-rtpj2kpay
|
2018-10-22 09:39:24 +00:00
|
|
|
* @title: rtpj2kpay
|
2009-05-08 08:38:42 +00:00
|
|
|
*
|
2016-05-11 19:01:32 +00:00
|
|
|
* Payload encode JPEG 2000 images into RTP packets according to RFC 5371
|
|
|
|
* and RFC 5372.
|
|
|
|
* For detailed information see: https://datatracker.ietf.org/doc/rfc5371/
|
|
|
|
* and https://datatracker.ietf.org/doc/rfc5372/
|
|
|
|
*
|
|
|
|
* The payloader takes a JPEG 2000 image, scans it for "packetization
|
|
|
|
* units" and constructs the RTP packet header followed by the JPEG 2000
|
|
|
|
* codestream. A "packetization unit" is defined as either a JPEG 2000 main header,
|
|
|
|
* a JPEG 2000 tile-part header, or a JPEG 2000 packet.
|
|
|
|
*
|
2009-05-08 08:38:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <gst/rtp/gstrtpbuffer.h>
|
2015-08-04 17:59:17 +00:00
|
|
|
#include <gst/video/video.h>
|
2021-02-12 12:16:28 +00:00
|
|
|
#include "gstrtpelements.h"
|
2016-05-11 19:04:26 +00:00
|
|
|
#include "gstrtpj2kcommon.h"
|
2009-05-08 08:38:42 +00:00
|
|
|
#include "gstrtpj2kpay.h"
|
2015-08-04 17:59:17 +00:00
|
|
|
#include "gstrtputils.h"
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_rtp_j2k_pay_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2016-05-13 19:08:24 +00:00
|
|
|
GST_STATIC_CAPS ("image/x-jpc, " GST_RTP_J2K_SAMPLING_LIST)
|
2009-05-08 08:38:42 +00:00
|
|
|
);
|
|
|
|
|
2016-05-13 19:08:24 +00:00
|
|
|
|
2009-05-08 08:38:42 +00:00
|
|
|
static GstStaticPadTemplate gst_rtp_j2k_pay_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtp, "
|
|
|
|
" media = (string) \"video\", "
|
|
|
|
" payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
|
|
|
|
" clock-rate = (int) 90000, "
|
2016-05-13 19:08:24 +00:00
|
|
|
GST_RTP_J2K_SAMPLING_LIST "," " encoding-name = (string) \"JPEG2000\"")
|
2009-05-08 08:38:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (rtpj2kpay_debug);
|
|
|
|
#define GST_CAT_DEFAULT (rtpj2kpay_debug)
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
guint tp:2;
|
|
|
|
guint MHF:2;
|
|
|
|
guint mh_id:3;
|
|
|
|
guint T:1;
|
|
|
|
guint priority:8;
|
|
|
|
guint tile:16;
|
|
|
|
guint offset:24;
|
|
|
|
} RtpJ2KHeader;
|
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
static void gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
static gboolean gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload,
|
2009-05-08 08:38:42 +00:00
|
|
|
GstCaps * caps);
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
static GstFlowReturn gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * pad,
|
2009-05-08 08:38:42 +00:00
|
|
|
GstBuffer * buffer);
|
|
|
|
|
2011-06-13 15:14:00 +00:00
|
|
|
#define gst_rtp_j2k_pay_parent_class parent_class
|
2011-11-11 11:25:01 +00:00
|
|
|
G_DEFINE_TYPE (GstRtpJ2KPay, gst_rtp_j2k_pay, GST_TYPE_RTP_BASE_PAYLOAD);
|
2021-02-12 12:16:28 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpj2kpay, "rtpj2kpay",
|
|
|
|
GST_RANK_SECONDARY, GST_TYPE_RTP_J2K_PAY, rtp_element_init (plugin));
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtp_j2k_pay_class_init (GstRtpJ2KPayClass * klass)
|
|
|
|
{
|
2010-11-18 11:49:47 +00:00
|
|
|
GObjectClass *gobject_class;
|
2011-06-13 15:14:00 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2011-11-11 11:25:01 +00:00
|
|
|
GstRTPBasePayloadClass *gstrtpbasepayload_class;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-06-13 15:14:00 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
gobject_class->set_property = gst_rtp_j2k_pay_set_property;
|
|
|
|
gobject_class->get_property = gst_rtp_j2k_pay_get_property;
|
|
|
|
|
2016-03-04 01:30:12 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&gst_rtp_j2k_pay_src_template);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&gst_rtp_j2k_pay_sink_template);
|
2011-06-13 15:14:00 +00:00
|
|
|
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2011-06-13 15:14:00 +00:00
|
|
|
"RTP JPEG 2000 payloader", "Codec/Payloader/Network/RTP",
|
|
|
|
"Payload-encodes JPEG 2000 pictures into RTP packets (RFC 5371)",
|
|
|
|
"Wim Taymans <wim.taymans@gmail.com>");
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class->set_caps = gst_rtp_j2k_pay_setcaps;
|
|
|
|
gstrtpbasepayload_class->handle_buffer = gst_rtp_j2k_pay_handle_buffer;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (rtpj2kpay_debug, "rtpj2kpay", 0,
|
|
|
|
"JPEG 2000 RTP Payloader");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-13 15:14:00 +00:00
|
|
|
gst_rtp_j2k_pay_init (GstRtpJ2KPay * pay)
|
2009-05-08 08:38:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_j2k_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
|
2009-05-08 08:38:42 +00:00
|
|
|
{
|
|
|
|
GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
|
2010-02-04 14:59:25 +00:00
|
|
|
gboolean res;
|
2016-05-13 19:08:24 +00:00
|
|
|
gint width = 0, height = 0;
|
|
|
|
const gchar *sampling = NULL;
|
|
|
|
|
|
|
|
gboolean has_width = gst_structure_get_int (caps_structure, "width", &width);
|
|
|
|
gboolean has_height =
|
|
|
|
gst_structure_get_int (caps_structure, "height", &height);
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
|
2016-05-13 19:08:24 +00:00
|
|
|
/* sampling is a required field */
|
|
|
|
sampling = gst_structure_get_string (caps_structure, "sampling");
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_base_payload_set_options (basepayload, "video", TRUE, "JPEG2000",
|
2009-05-08 08:38:42 +00:00
|
|
|
90000);
|
|
|
|
|
2016-05-13 19:08:24 +00:00
|
|
|
if (has_width && has_height)
|
|
|
|
res = gst_rtp_base_payload_set_outcaps (basepayload,
|
|
|
|
"sampling", G_TYPE_STRING, sampling, "width", G_TYPE_INT, width,
|
|
|
|
"height", G_TYPE_INT, height, NULL);
|
|
|
|
else
|
|
|
|
res =
|
|
|
|
gst_rtp_base_payload_set_outcaps (basepayload, "sampling",
|
|
|
|
G_TYPE_STRING, sampling, NULL);
|
2010-02-04 14:59:25 +00:00
|
|
|
return res;
|
2009-05-08 08:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gst_rtp_j2k_pay_header_size (const guint8 * data, guint offset)
|
|
|
|
{
|
|
|
|
return data[offset] << 8 | data[offset + 1];
|
|
|
|
}
|
|
|
|
|
2016-05-11 19:04:26 +00:00
|
|
|
|
|
|
|
static GstRtpJ2KMarker
|
2009-05-08 08:38:42 +00:00
|
|
|
gst_rtp_j2k_pay_scan_marker (const guint8 * data, guint size, guint * offset)
|
|
|
|
{
|
2016-05-11 19:04:26 +00:00
|
|
|
while ((data[(*offset)++] != GST_J2K_MARKER) && ((*offset) < size));
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY ((*offset) >= size)) {
|
2016-05-11 19:04:26 +00:00
|
|
|
return GST_J2K_MARKER_EOC;
|
2009-05-08 08:38:42 +00:00
|
|
|
} else {
|
|
|
|
guint8 marker = data[(*offset)++];
|
2016-05-11 19:04:26 +00:00
|
|
|
return (GstRtpJ2KMarker) marker;
|
2009-05-08 08:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 16:53:42 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
RtpJ2KHeader header;
|
2016-06-21 11:40:42 +00:00
|
|
|
gboolean multi_tile;
|
2010-12-02 16:53:42 +00:00
|
|
|
gboolean bitstream;
|
|
|
|
guint next_sot;
|
|
|
|
gboolean force_packet;
|
|
|
|
} RtpJ2KState;
|
|
|
|
|
2016-05-12 12:41:51 +00:00
|
|
|
|
|
|
|
/* Note: The standard recommends that headers be put in their own RTP packets, so we follow
|
|
|
|
* this recommendation in the code. Also, this method groups together all J2K packets
|
|
|
|
* for a tile part and treats this group as a packetization unit. According to the RFC,
|
|
|
|
* only an individual J2K packet is considered a packetization unit.
|
|
|
|
*/
|
|
|
|
|
2009-05-08 08:38:42 +00:00
|
|
|
static guint
|
|
|
|
find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
|
2010-12-02 16:53:42 +00:00
|
|
|
guint offset, RtpJ2KState * state)
|
2009-05-08 08:38:42 +00:00
|
|
|
{
|
2010-12-02 16:53:42 +00:00
|
|
|
gboolean cut_sop = FALSE;
|
2016-05-11 19:04:26 +00:00
|
|
|
GstRtpJ2KMarker marker;
|
2010-12-02 16:53:42 +00:00
|
|
|
|
2009-05-08 08:38:42 +00:00
|
|
|
/* parse the j2k header for 'start of codestream' */
|
2010-12-02 16:53:42 +00:00
|
|
|
GST_LOG_OBJECT (pay, "checking from offset %u", offset);
|
2009-05-08 08:38:42 +00:00
|
|
|
while (offset < size) {
|
2010-12-02 16:53:42 +00:00
|
|
|
marker = gst_rtp_j2k_pay_scan_marker (data, size, &offset);
|
|
|
|
|
|
|
|
if (state->bitstream) {
|
|
|
|
/* parsing bitstream, only look for SOP */
|
|
|
|
switch (marker) {
|
2016-05-11 19:04:26 +00:00
|
|
|
case GST_J2K_MARKER_SOP:
|
2010-12-02 16:53:42 +00:00
|
|
|
GST_LOG_OBJECT (pay, "found SOP at %u", offset);
|
|
|
|
if (cut_sop)
|
|
|
|
return offset - 2;
|
|
|
|
cut_sop = TRUE;
|
|
|
|
break;
|
2016-05-11 19:04:26 +00:00
|
|
|
case GST_J2K_MARKER_EPH:
|
2011-02-01 15:38:20 +00:00
|
|
|
/* just skip over EPH */
|
|
|
|
GST_LOG_OBJECT (pay, "found EPH at %u", offset);
|
|
|
|
break;
|
2010-12-02 16:53:42 +00:00
|
|
|
default:
|
|
|
|
if (offset >= state->next_sot) {
|
|
|
|
GST_LOG_OBJECT (pay, "reached next SOT at %u", offset);
|
|
|
|
state->bitstream = FALSE;
|
|
|
|
state->force_packet = TRUE;
|
2016-05-11 19:04:26 +00:00
|
|
|
if (marker == GST_J2K_MARKER_EOC && state->next_sot + 2 <= size)
|
2010-12-16 17:55:43 +00:00
|
|
|
/* include EOC but never go past the max size */
|
2010-12-02 16:53:42 +00:00
|
|
|
return state->next_sot + 2;
|
|
|
|
else
|
|
|
|
return state->next_sot;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (marker) {
|
2016-05-11 19:04:26 +00:00
|
|
|
case GST_J2K_MARKER_SOC:
|
2010-12-02 16:53:42 +00:00
|
|
|
GST_LOG_OBJECT (pay, "found SOC at %u", offset);
|
2016-05-12 12:41:51 +00:00
|
|
|
/* start off by assuming that we will fit the entire header
|
|
|
|
into the RTP payload */
|
|
|
|
state->header.MHF = 3;
|
2010-12-02 16:53:42 +00:00
|
|
|
break;
|
2016-05-11 19:04:26 +00:00
|
|
|
case GST_J2K_MARKER_SOT:
|
2010-12-02 16:53:42 +00:00
|
|
|
{
|
2016-05-12 12:43:39 +00:00
|
|
|
guint len, Psot, tile;
|
2010-12-02 16:53:42 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (pay, "found SOT at %u", offset);
|
2016-05-12 12:41:51 +00:00
|
|
|
/* SOT for first tile part in code stream:
|
|
|
|
force close of current RTP packet, so that it
|
|
|
|
only contains main header */
|
2010-12-02 16:53:42 +00:00
|
|
|
if (state->header.MHF) {
|
|
|
|
state->force_packet = TRUE;
|
|
|
|
return offset - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse SOT but do some sanity checks first */
|
|
|
|
len = gst_rtp_j2k_pay_header_size (data, offset);
|
|
|
|
GST_LOG_OBJECT (pay, "SOT length %u", len);
|
|
|
|
if (len < 8)
|
|
|
|
return size;
|
|
|
|
if (offset + len >= size)
|
|
|
|
return size;
|
|
|
|
|
2016-05-12 12:43:39 +00:00
|
|
|
/* Isot */
|
|
|
|
tile = GST_READ_UINT16_BE (&data[offset + 2]);
|
|
|
|
|
2016-06-21 11:40:42 +00:00
|
|
|
if (!state->multi_tile) {
|
|
|
|
/* we have detected multiple tiles in this rtp packet : tile bit is now invalid */
|
|
|
|
if (state->header.T == 0 && state->header.tile != tile) {
|
2016-05-12 12:43:39 +00:00
|
|
|
state->header.T = 1;
|
2016-06-21 11:40:42 +00:00
|
|
|
state->multi_tile = TRUE;
|
|
|
|
} else {
|
|
|
|
state->header.T = 0;
|
2016-05-12 12:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
state->header.tile = tile;
|
|
|
|
|
|
|
|
/* Note: Tile parts from multiple tiles in single RTP packet
|
|
|
|
will make T invalid.
|
|
|
|
This cannot happen in our case since we always
|
|
|
|
send tile headers in their own RTP packets, so we cannot mix
|
|
|
|
tile parts in a single RTP packet */
|
|
|
|
|
|
|
|
/* Psot: offset of next tile. If it's 0, next tile goes all the way
|
|
|
|
to the end of the data */
|
2010-12-02 16:53:42 +00:00
|
|
|
Psot = GST_READ_UINT32_BE (&data[offset + 4]);
|
|
|
|
if (Psot == 0)
|
|
|
|
state->next_sot = size;
|
|
|
|
else
|
|
|
|
state->next_sot = offset - 2 + Psot;
|
|
|
|
|
|
|
|
offset += len;
|
|
|
|
GST_LOG_OBJECT (pay, "Isot %u, Psot %u, next %u", state->header.tile,
|
|
|
|
Psot, state->next_sot);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-11 19:04:26 +00:00
|
|
|
case GST_J2K_MARKER_SOD:
|
2010-12-02 16:53:42 +00:00
|
|
|
GST_LOG_OBJECT (pay, "found SOD at %u", offset);
|
|
|
|
/* go to bitstream parsing */
|
|
|
|
state->bitstream = TRUE;
|
|
|
|
/* cut at the next SOP or else include all data */
|
|
|
|
cut_sop = TRUE;
|
|
|
|
/* force a new packet when we see SOP, this can be optional but the
|
|
|
|
* spec recommends packing headers separately */
|
|
|
|
state->force_packet = TRUE;
|
|
|
|
break;
|
2016-05-11 19:04:26 +00:00
|
|
|
case GST_J2K_MARKER_EOC:
|
2010-12-02 16:53:42 +00:00
|
|
|
GST_LOG_OBJECT (pay, "found EOC at %u", offset);
|
|
|
|
return offset;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
guint len = gst_rtp_j2k_pay_header_size (data, offset);
|
|
|
|
GST_LOG_OBJECT (pay, "skip 0x%02x len %u", marker, len);
|
|
|
|
offset += len;
|
|
|
|
break;
|
|
|
|
}
|
2009-05-08 08:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (pay, "reached end of data");
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * basepayload,
|
2009-05-08 08:38:42 +00:00
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstRtpJ2KPay *pay;
|
|
|
|
GstClockTime timestamp;
|
|
|
|
GstFlowReturn ret = GST_FLOW_ERROR;
|
2010-12-02 16:53:42 +00:00
|
|
|
RtpJ2KState state;
|
2010-11-18 11:49:47 +00:00
|
|
|
GstBufferList *list = NULL;
|
2012-01-23 16:25:37 +00:00
|
|
|
GstMapInfo map;
|
2010-12-02 16:53:42 +00:00
|
|
|
guint mtu, max_size;
|
2009-05-08 08:38:42 +00:00
|
|
|
guint offset;
|
2010-12-02 16:53:42 +00:00
|
|
|
guint end, pos;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
pay = GST_RTP_J2K_PAY (basepayload);
|
2011-11-11 11:25:01 +00:00
|
|
|
mtu = GST_RTP_BASE_PAYLOAD_MTU (pay);
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2012-01-23 16:25:37 +00:00
|
|
|
gst_buffer_map (buffer, &map, GST_MAP_READ);
|
2015-06-10 12:33:50 +00:00
|
|
|
timestamp = GST_BUFFER_PTS (buffer);
|
2010-12-02 16:53:42 +00:00
|
|
|
offset = pos = end = 0;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2011-11-21 19:31:31 +00:00
|
|
|
GST_LOG_OBJECT (pay,
|
2012-01-23 16:25:37 +00:00
|
|
|
"got buffer size %" G_GSIZE_FORMAT ", timestamp %" GST_TIME_FORMAT,
|
|
|
|
map.size, GST_TIME_ARGS (timestamp));
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
/* do some header defaults first */
|
2010-12-02 16:53:42 +00:00
|
|
|
state.header.tp = 0; /* only progressive scan */
|
|
|
|
state.header.MHF = 0; /* no header */
|
|
|
|
state.header.mh_id = 0; /* always 0 for now */
|
2016-05-12 12:43:39 +00:00
|
|
|
state.header.T = 1; /* invalid tile, because we always begin with the main header */
|
2010-12-02 16:53:42 +00:00
|
|
|
state.header.priority = 255; /* always 255 for now */
|
2016-06-21 08:34:56 +00:00
|
|
|
state.header.tile = 0xffff; /* no tile number */
|
2010-12-15 17:21:34 +00:00
|
|
|
state.header.offset = 0; /* offset of 0 */
|
2016-06-21 11:40:42 +00:00
|
|
|
state.multi_tile = FALSE;
|
2010-12-02 16:53:42 +00:00
|
|
|
state.bitstream = FALSE;
|
|
|
|
state.next_sot = 0;
|
|
|
|
state.force_packet = FALSE;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-12-02 16:53:42 +00:00
|
|
|
/* get max packet length */
|
2016-05-11 19:04:26 +00:00
|
|
|
max_size =
|
|
|
|
gst_rtp_buffer_calc_payload_len (mtu - GST_RTP_J2K_HEADER_SIZE, 0, 0);
|
2010-12-02 16:53:42 +00:00
|
|
|
|
2014-06-18 13:38:55 +00:00
|
|
|
list = gst_buffer_list_new_sized ((mtu / max_size) + 1);
|
|
|
|
|
2009-05-08 08:38:42 +00:00
|
|
|
do {
|
|
|
|
GstBuffer *outbuf;
|
2010-11-18 11:49:47 +00:00
|
|
|
guint8 *header;
|
2010-12-02 16:53:42 +00:00
|
|
|
guint payload_size;
|
|
|
|
guint pu_size;
|
2011-06-13 15:14:00 +00:00
|
|
|
GstRTPBuffer rtp = { NULL };
|
2010-12-02 16:53:42 +00:00
|
|
|
|
|
|
|
/* try to pack as much as we can */
|
|
|
|
do {
|
|
|
|
/* see how much we have scanned already */
|
|
|
|
pu_size = end - offset;
|
|
|
|
GST_DEBUG_OBJECT (pay, "scanned pu size %u", pu_size);
|
|
|
|
|
|
|
|
/* we need to make a new packet */
|
|
|
|
if (state.force_packet) {
|
|
|
|
GST_DEBUG_OBJECT (pay, "need to force a new packet");
|
|
|
|
state.force_packet = FALSE;
|
|
|
|
pos = end;
|
|
|
|
break;
|
|
|
|
}
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-12-02 16:53:42 +00:00
|
|
|
/* else see if we have enough */
|
|
|
|
if (pu_size > max_size) {
|
|
|
|
if (pos != offset)
|
|
|
|
/* the packet became too large, use previous scanpos */
|
|
|
|
pu_size = pos - offset;
|
|
|
|
else
|
|
|
|
/* the already scanned data was already too big, make sure we start
|
|
|
|
* scanning from the last searched position */
|
|
|
|
pos = end;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-12-02 16:53:42 +00:00
|
|
|
GST_DEBUG_OBJECT (pay, "max size exceeded pu_size %u", pu_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = end;
|
2010-12-14 14:27:52 +00:00
|
|
|
|
|
|
|
/* exit when finished */
|
2012-01-23 16:25:37 +00:00
|
|
|
if (pos == map.size)
|
2010-12-14 14:27:52 +00:00
|
|
|
break;
|
|
|
|
|
2010-12-02 16:53:42 +00:00
|
|
|
/* scan next packetization unit and fill in the header */
|
2012-01-23 16:25:37 +00:00
|
|
|
end = find_pu_end (pay, map.data, map.size, pos, &state);
|
2010-12-02 16:53:42 +00:00
|
|
|
} while (TRUE);
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
while (pu_size > 0) {
|
2010-12-02 16:53:42 +00:00
|
|
|
guint packet_size, data_size;
|
2012-07-23 14:42:56 +00:00
|
|
|
GstBuffer *paybuf;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
|
|
|
/* calculate the packet size */
|
|
|
|
packet_size =
|
2016-05-11 19:04:26 +00:00
|
|
|
gst_rtp_buffer_calc_packet_len (pu_size + GST_RTP_J2K_HEADER_SIZE, 0,
|
|
|
|
0);
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-12-02 16:53:42 +00:00
|
|
|
if (packet_size > mtu) {
|
|
|
|
GST_DEBUG_OBJECT (pay, "needed packet size %u clamped to MTU %u",
|
|
|
|
packet_size, mtu);
|
|
|
|
packet_size = mtu;
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (pay, "needed packet size %u fits in MTU %u",
|
|
|
|
packet_size, mtu);
|
|
|
|
}
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
/* get total payload size and data size */
|
|
|
|
payload_size = gst_rtp_buffer_calc_payload_len (packet_size, 0, 0);
|
2016-05-11 19:04:26 +00:00
|
|
|
data_size = payload_size - GST_RTP_J2K_HEADER_SIZE;
|
2010-11-18 11:49:47 +00:00
|
|
|
|
2012-07-23 14:42:56 +00:00
|
|
|
/* make buffer for header */
|
2020-09-28 15:25:21 +00:00
|
|
|
outbuf =
|
|
|
|
gst_rtp_base_payload_allocate_output_buffer (basepayload,
|
|
|
|
GST_RTP_J2K_HEADER_SIZE, 0, 0);
|
2012-07-23 14:42:56 +00:00
|
|
|
|
2015-06-10 12:33:50 +00:00
|
|
|
GST_BUFFER_PTS (outbuf) = timestamp;
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2011-06-13 15:14:00 +00:00
|
|
|
gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
|
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
/* get pointer to header */
|
2011-06-13 15:14:00 +00:00
|
|
|
header = gst_rtp_buffer_get_payload (&rtp);
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
pu_size -= data_size;
|
2016-05-12 12:41:51 +00:00
|
|
|
|
|
|
|
/* reached the end of a packetization unit */
|
|
|
|
if (pu_size == 0 && end >= map.size) {
|
|
|
|
gst_rtp_buffer_set_marker (&rtp, TRUE);
|
2022-02-22 09:18:53 +00:00
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
|
2016-05-12 12:41:51 +00:00
|
|
|
}
|
|
|
|
/* If we were processing a header, see if all fits in one RTP packet
|
|
|
|
or if we have to fragment it */
|
|
|
|
if (state.header.MHF) {
|
|
|
|
switch (state.header.MHF) {
|
|
|
|
case 3:
|
|
|
|
if (pu_size > 0)
|
|
|
|
state.header.MHF = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (pu_size == 0)
|
|
|
|
state.header.MHF = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2009-05-08 08:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 15:22:54 +00:00
|
|
|
/*
|
|
|
|
* RtpJ2KHeader:
|
|
|
|
* @tp: type (0 progressive, 1 odd field, 2 even field)
|
|
|
|
* @MHF: Main Header Flag
|
|
|
|
* @mh_id: Main Header Identification
|
|
|
|
* @T: Tile field invalidation flag
|
|
|
|
* @priority: priority
|
|
|
|
* @tile number: the tile number of the payload
|
|
|
|
* @reserved: set to 0
|
|
|
|
* @fragment offset: the byte offset of the current payload
|
|
|
|
*
|
|
|
|
* 0 1 2 3
|
|
|
|
* 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
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* |tp |MHF|mh_id|T| priority | tile number |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* |reserved | fragment offset |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
header[0] = (state.header.tp << 6) | (state.header.MHF << 4) |
|
|
|
|
(state.header.mh_id << 1) | state.header.T;
|
|
|
|
header[1] = state.header.priority;
|
|
|
|
header[2] = state.header.tile >> 8;
|
|
|
|
header[3] = state.header.tile & 0xff;
|
|
|
|
header[4] = 0;
|
|
|
|
header[5] = state.header.offset >> 16;
|
|
|
|
header[6] = (state.header.offset >> 8) & 0xff;
|
|
|
|
header[7] = state.header.offset & 0xff;
|
2010-11-18 11:49:47 +00:00
|
|
|
|
2012-07-23 14:42:56 +00:00
|
|
|
gst_rtp_buffer_unmap (&rtp);
|
2010-11-18 11:49:47 +00:00
|
|
|
|
2012-07-23 14:42:56 +00:00
|
|
|
/* make subbuffer of j2k data */
|
2015-08-04 17:59:17 +00:00
|
|
|
paybuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL,
|
2012-07-23 14:42:56 +00:00
|
|
|
offset, data_size);
|
2017-05-24 11:57:10 +00:00
|
|
|
gst_rtp_copy_video_meta (basepayload, outbuf, paybuf);
|
2012-07-23 14:42:56 +00:00
|
|
|
outbuf = gst_buffer_append (outbuf, paybuf);
|
2010-11-18 11:49:47 +00:00
|
|
|
|
2012-07-23 14:42:56 +00:00
|
|
|
gst_buffer_list_add (list, outbuf);
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2016-05-12 12:43:39 +00:00
|
|
|
/* reset multi_tile */
|
2016-06-21 11:40:42 +00:00
|
|
|
state.multi_tile = FALSE;
|
2016-05-12 12:43:39 +00:00
|
|
|
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2016-05-12 12:41:51 +00:00
|
|
|
/* set MHF to zero if there is no more main header to process */
|
|
|
|
if (state.header.MHF & 2)
|
|
|
|
state.header.MHF = 0;
|
|
|
|
|
2016-05-12 12:43:39 +00:00
|
|
|
/* tile is valid, if there is no more header to process */
|
|
|
|
if (!state.header.MHF)
|
|
|
|
state.header.T = 0;
|
|
|
|
|
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
offset += data_size;
|
2015-10-13 15:42:56 +00:00
|
|
|
state.header.offset = offset;
|
2009-05-08 08:38:42 +00:00
|
|
|
}
|
2010-12-02 16:53:42 +00:00
|
|
|
offset = pos;
|
2012-01-23 16:25:37 +00:00
|
|
|
} while (offset < map.size);
|
2009-05-08 08:38:42 +00:00
|
|
|
|
2016-05-25 15:11:13 +00:00
|
|
|
gst_buffer_unmap (buffer, &map);
|
2009-05-08 08:38:42 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
|
2012-07-23 14:42:56 +00:00
|
|
|
/* push the whole buffer list at once */
|
|
|
|
ret = gst_rtp_base_payload_push_list (basepayload, list);
|
2010-11-18 11:49:47 +00:00
|
|
|
|
2009-05-08 08:38:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-18 11:49:47 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (prop_id) {
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (prop_id) {
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|