rtpj2k: move common code to shared header, code clean up

https://bugzilla.gnome.org/show_bug.cgi?id=745187
This commit is contained in:
Aaron Boxer 2016-05-11 15:04:26 -04:00 committed by Sebastian Dröge
parent 82c2a5cbf8
commit d2765be120
5 changed files with 82 additions and 66 deletions

49
gst/rtp/gstrtpj2kcommon.h Normal file
View file

@ -0,0 +1,49 @@
/* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_RTP_J2K_COMMON_H__
#define __GST_RTP_J2K_COMMON_H__
/*
* GstRtpJ2KMarker:
* @GST_J2K_MARKER: Prefix for JPEG 2000 marker
* @GST_J2K_MARKER_SOC: Start of Codestream
* @GST_J2K_MARKER_SOT: Start of tile
* @GST_J2K_MARKER_EOC: End of Codestream
*
* Identifers for markers in JPEG 2000 codestreams
*/
typedef enum
{
GST_J2K_MARKER = 0xFF,
GST_J2K_MARKER_SOC = 0x4F,
GST_J2K_MARKER_SOT = 0x90,
GST_J2K_MARKER_SOP = 0x91,
GST_J2K_MARKER_EPH = 0x92,
GST_J2K_MARKER_SOD = 0x93,
GST_J2K_MARKER_EOC = 0xD9
} GstRtpJ2KMarker;
#define GST_RTP_J2K_HEADER_SIZE 8
#endif /* __GST_RTP_J2K_COMMON_H__ */

View file

@ -36,6 +36,7 @@
#include <gst/video/video.h> #include <gst/video/video.h>
#include <string.h> #include <string.h>
#include "gstrtpj2kcommon.h"
#include "gstrtpj2kdepay.h" #include "gstrtpj2kdepay.h"
#include "gstrtputils.h" #include "gstrtputils.h"
@ -58,16 +59,6 @@ GST_STATIC_PAD_TEMPLATE ("sink",
"clock-rate = (int) 90000, " "encoding-name = (string) \"JPEG2000\"") "clock-rate = (int) 90000, " "encoding-name = (string) \"JPEG2000\"")
); );
typedef enum
{
J2K_MARKER = 0xFF,
J2K_MARKER_SOC = 0x4F,
J2K_MARKER_SOT = 0x90,
J2K_MARKER_SOP = 0x91,
J2K_MARKER_SOD = 0x93,
J2K_MARKER_EOC = 0xD9
} RtpJ2KMarker;
enum enum
{ {
PROP_0, PROP_0,
@ -313,10 +304,10 @@ gst_rtp_j2k_depay_flush_tile (GstRTPBaseDepayload * depayload)
if (map.size < 12) if (map.size < 12)
goto invalid_tile; goto invalid_tile;
if (map.data[0] == 0xff && map.data[1] == J2K_MARKER_SOT) { if (map.data[0] == GST_J2K_MARKER && map.data[1] == GST_J2K_MARKER_SOT) {
guint Psot, nPsot; guint Psot, nPsot;
if (end[0] == 0xff && end[1] == J2K_MARKER_EOC) if (end[0] == GST_J2K_MARKER && end[1] == GST_J2K_MARKER_EOC)
nPsot = avail - 2; nPsot = avail - 2;
else else
nPsot = avail; nPsot = avail;
@ -391,9 +382,9 @@ gst_rtp_j2k_depay_flush_frame (GstRTPBaseDepayload * depayload)
* marker */ * marker */
gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2); gst_adapter_copy (rtpj2kdepay->f_adapter, end, avail - 2, 2);
if (end[0] != 0xff && end[1] != 0xd9) { if (end[0] != GST_J2K_MARKER && end[1] != GST_J2K_MARKER_EOC) {
end[0] = 0xff; end[0] = GST_J2K_MARKER;
end[1] = 0xd9; end[1] = GST_J2K_MARKER_EOC;
GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one"); GST_DEBUG_OBJECT (rtpj2kdepay, "no EOC marker, adding one");
@ -444,7 +435,7 @@ gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
payload_len = gst_rtp_buffer_get_payload_len (rtp); payload_len = gst_rtp_buffer_get_payload_len (rtp);
/* we need at least a header */ /* we need at least a header */
if (payload_len < 8) if (payload_len < GST_RTP_J2K_HEADER_SIZE)
goto empty_packet; goto empty_packet;
rtptime = gst_rtp_buffer_get_timestamp (rtp); rtptime = gst_rtp_buffer_get_timestamp (rtp);
@ -475,7 +466,7 @@ gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
tile = (payload[2] << 8) | payload[3]; tile = (payload[2] << 8) | payload[3];
frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7]; frag_offset = (payload[5] << 16) | (payload[6] << 8) | payload[7];
j2klen = payload_len - 8; j2klen = payload_len - GST_RTP_J2K_HEADER_SIZE;
GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF, GST_DEBUG_OBJECT (rtpj2kdepay, "MHF %u, tile %u, frag %u, expected %u", MHF,
tile, frag_offset, rtpj2kdepay->next_frag); tile, frag_offset, rtpj2kdepay->next_frag);
@ -492,19 +483,19 @@ gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
} }
/* check for sync code */ /* check for sync code */
if (j2klen > 2 && payload[8] == 0xff) { if (j2klen > 2 && payload[GST_RTP_J2K_HEADER_SIZE] == GST_J2K_MARKER) {
guint marker = payload[9]; guint marker = payload[GST_RTP_J2K_HEADER_SIZE + 1];
/* packets must start with SOC, SOT or SOP */ /* packets must start with SOC, SOT or SOP */
switch (marker) { switch (marker) {
case J2K_MARKER_SOC: case GST_J2K_MARKER_SOC:
GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet"); GST_DEBUG_OBJECT (rtpj2kdepay, "found SOC packet");
/* flush the previous frame, should have happened when the timestamp /* flush the previous frame, should have happened when the timestamp
* changed above. */ * changed above. */
gst_rtp_j2k_depay_flush_frame (depayload); gst_rtp_j2k_depay_flush_frame (depayload);
rtpj2kdepay->have_sync = TRUE; rtpj2kdepay->have_sync = TRUE;
break; break;
case J2K_MARKER_SOT: case GST_J2K_MARKER_SOT:
/* flush the previous tile */ /* flush the previous tile */
gst_rtp_j2k_depay_flush_tile (depayload); gst_rtp_j2k_depay_flush_tile (depayload);
GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet"); GST_DEBUG_OBJECT (rtpj2kdepay, "found SOT packet");
@ -512,7 +503,7 @@ gst_rtp_j2k_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
/* we sync on the tile now */ /* we sync on the tile now */
rtpj2kdepay->last_tile = tile; rtpj2kdepay->last_tile = tile;
break; break;
case J2K_MARKER_SOP: case GST_J2K_MARKER_SOP:
GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet"); GST_DEBUG_OBJECT (rtpj2kdepay, "found SOP packet");
/* flush the previous PU */ /* flush the previous PU */
gst_rtp_j2k_depay_flush_pu (depayload); gst_rtp_j2k_depay_flush_pu (depayload);

View file

@ -25,7 +25,6 @@
#include <gst/rtp/gstrtpbasedepayload.h> #include <gst/rtp/gstrtpbasedepayload.h>
G_BEGIN_DECLS G_BEGIN_DECLS
#define GST_TYPE_RTP_J2K_DEPAY \ #define GST_TYPE_RTP_J2K_DEPAY \
(gst_rtp_j2k_depay_get_type()) (gst_rtp_j2k_depay_get_type())
#define GST_RTP_J2K_DEPAY(obj) \ #define GST_RTP_J2K_DEPAY(obj) \
@ -36,7 +35,6 @@ G_BEGIN_DECLS
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_J2K_DEPAY)) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_J2K_DEPAY))
#define GST_IS_RTP_J2K_DEPAY_CLASS(klass) \ #define GST_IS_RTP_J2K_DEPAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_J2K_DEPAY)) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_J2K_DEPAY))
typedef struct _GstRtpJ2KDepay GstRtpJ2KDepay; typedef struct _GstRtpJ2KDepay GstRtpJ2KDepay;
typedef struct _GstRtpJ2KDepayClass GstRtpJ2KDepayClass; typedef struct _GstRtpJ2KDepayClass GstRtpJ2KDepayClass;
@ -71,5 +69,4 @@ GType gst_rtp_j2k_depay_get_type (void);
gboolean gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin); gboolean gst_rtp_j2k_depay_plugin_init (GstPlugin * plugin);
G_END_DECLS G_END_DECLS
#endif /* __GST_RTP_J2K_DEPAY_H__ */ #endif /* __GST_RTP_J2K_DEPAY_H__ */

View file

@ -40,7 +40,7 @@
#include <string.h> #include <string.h>
#include <gst/rtp/gstrtpbuffer.h> #include <gst/rtp/gstrtpbuffer.h>
#include <gst/video/video.h> #include <gst/video/video.h>
#include "gstrtpj2kcommon.h"
#include "gstrtpj2kpay.h" #include "gstrtpj2kpay.h"
#include "gstrtputils.h" #include "gstrtputils.h"
@ -65,25 +65,6 @@ GST_STATIC_PAD_TEMPLATE ("src",
GST_DEBUG_CATEGORY_STATIC (rtpj2kpay_debug); GST_DEBUG_CATEGORY_STATIC (rtpj2kpay_debug);
#define GST_CAT_DEFAULT (rtpj2kpay_debug) #define GST_CAT_DEFAULT (rtpj2kpay_debug)
/*
* RtpJ2KMarker:
* @J2K_MARKER: Prefix for JPEG 2000 marker
* @J2K_MARKER_SOC: Start of Codestream
* @J2K_MARKER_SOT: Start of tile
* @J2K_MARKER_EOC: End of Codestream
*
* Identifers for markers in JPEG 2000 codestreams
*/
typedef enum
{
J2K_MARKER = 0xFF,
J2K_MARKER_SOC = 0x4F,
J2K_MARKER_SOT = 0x90,
J2K_MARKER_SOP = 0x91,
J2K_MARKER_EPH = 0x92,
J2K_MARKER_SOD = 0x93,
J2K_MARKER_EOC = 0xD9
} RtpJ2KMarker;
enum enum
{ {
@ -102,8 +83,6 @@ typedef struct
guint offset:24; guint offset:24;
} RtpJ2KHeader; } RtpJ2KHeader;
#define HEADER_SIZE 8
static void gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id, static void gst_rtp_j2k_pay_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec); const GValue * value, GParamSpec * pspec);
static void gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id, static void gst_rtp_j2k_pay_get_property (GObject * object, guint prop_id,
@ -186,16 +165,17 @@ gst_rtp_j2k_pay_header_size (const guint8 * data, guint offset)
return data[offset] << 8 | data[offset + 1]; return data[offset] << 8 | data[offset + 1];
} }
static RtpJ2KMarker
static GstRtpJ2KMarker
gst_rtp_j2k_pay_scan_marker (const guint8 * data, guint size, guint * offset) gst_rtp_j2k_pay_scan_marker (const guint8 * data, guint size, guint * offset)
{ {
while ((data[(*offset)++] != J2K_MARKER) && ((*offset) < size)); while ((data[(*offset)++] != GST_J2K_MARKER) && ((*offset) < size));
if (G_UNLIKELY ((*offset) >= size)) { if (G_UNLIKELY ((*offset) >= size)) {
return J2K_MARKER_EOC; return GST_J2K_MARKER_EOC;
} else { } else {
guint8 marker = data[(*offset)++]; guint8 marker = data[(*offset)++];
return marker; return (GstRtpJ2KMarker) marker;
} }
} }
@ -213,7 +193,7 @@ find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
guint offset, RtpJ2KState * state) guint offset, RtpJ2KState * state)
{ {
gboolean cut_sop = FALSE; gboolean cut_sop = FALSE;
RtpJ2KMarker marker; GstRtpJ2KMarker marker;
/* parse the j2k header for 'start of codestream' */ /* parse the j2k header for 'start of codestream' */
GST_LOG_OBJECT (pay, "checking from offset %u", offset); GST_LOG_OBJECT (pay, "checking from offset %u", offset);
@ -223,13 +203,13 @@ find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
if (state->bitstream) { if (state->bitstream) {
/* parsing bitstream, only look for SOP */ /* parsing bitstream, only look for SOP */
switch (marker) { switch (marker) {
case J2K_MARKER_SOP: case GST_J2K_MARKER_SOP:
GST_LOG_OBJECT (pay, "found SOP at %u", offset); GST_LOG_OBJECT (pay, "found SOP at %u", offset);
if (cut_sop) if (cut_sop)
return offset - 2; return offset - 2;
cut_sop = TRUE; cut_sop = TRUE;
break; break;
case J2K_MARKER_EPH: case GST_J2K_MARKER_EPH:
/* just skip over EPH */ /* just skip over EPH */
GST_LOG_OBJECT (pay, "found EPH at %u", offset); GST_LOG_OBJECT (pay, "found EPH at %u", offset);
break; break;
@ -238,7 +218,7 @@ find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
GST_LOG_OBJECT (pay, "reached next SOT at %u", offset); GST_LOG_OBJECT (pay, "reached next SOT at %u", offset);
state->bitstream = FALSE; state->bitstream = FALSE;
state->force_packet = TRUE; state->force_packet = TRUE;
if (marker == J2K_MARKER_EOC && state->next_sot + 2 <= size) if (marker == GST_J2K_MARKER_EOC && state->next_sot + 2 <= size)
/* include EOC but never go past the max size */ /* include EOC but never go past the max size */
return state->next_sot + 2; return state->next_sot + 2;
else else
@ -248,11 +228,11 @@ find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
} }
} else { } else {
switch (marker) { switch (marker) {
case J2K_MARKER_SOC: case GST_J2K_MARKER_SOC:
GST_LOG_OBJECT (pay, "found SOC at %u", offset); GST_LOG_OBJECT (pay, "found SOC at %u", offset);
state->header.MHF = 1; state->header.MHF = 1;
break; break;
case J2K_MARKER_SOT: case GST_J2K_MARKER_SOT:
{ {
guint len, Psot; guint len, Psot;
@ -293,7 +273,7 @@ find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
Psot, state->next_sot); Psot, state->next_sot);
break; break;
} }
case J2K_MARKER_SOD: case GST_J2K_MARKER_SOD:
GST_LOG_OBJECT (pay, "found SOD at %u", offset); GST_LOG_OBJECT (pay, "found SOD at %u", offset);
/* can't have more tiles now */ /* can't have more tiles now */
state->n_tiles = 0; state->n_tiles = 0;
@ -305,7 +285,7 @@ find_pu_end (GstRtpJ2KPay * pay, const guint8 * data, guint size,
* spec recommends packing headers separately */ * spec recommends packing headers separately */
state->force_packet = TRUE; state->force_packet = TRUE;
break; break;
case J2K_MARKER_EOC: case GST_J2K_MARKER_EOC:
GST_LOG_OBJECT (pay, "found EOC at %u", offset); GST_LOG_OBJECT (pay, "found EOC at %u", offset);
return offset; return offset;
default: default:
@ -361,7 +341,8 @@ gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * basepayload,
state.force_packet = FALSE; state.force_packet = FALSE;
/* get max packet length */ /* get max packet length */
max_size = gst_rtp_buffer_calc_payload_len (mtu - HEADER_SIZE, 0, 0); max_size =
gst_rtp_buffer_calc_payload_len (mtu - GST_RTP_J2K_HEADER_SIZE, 0, 0);
list = gst_buffer_list_new_sized ((mtu / max_size) + 1); list = gst_buffer_list_new_sized ((mtu / max_size) + 1);
@ -416,7 +397,8 @@ gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * basepayload,
/* calculate the packet size */ /* calculate the packet size */
packet_size = packet_size =
gst_rtp_buffer_calc_packet_len (pu_size + HEADER_SIZE, 0, 0); gst_rtp_buffer_calc_packet_len (pu_size + GST_RTP_J2K_HEADER_SIZE, 0,
0);
if (packet_size > mtu) { if (packet_size > mtu) {
GST_DEBUG_OBJECT (pay, "needed packet size %u clamped to MTU %u", GST_DEBUG_OBJECT (pay, "needed packet size %u clamped to MTU %u",
@ -429,10 +411,10 @@ gst_rtp_j2k_pay_handle_buffer (GstRTPBasePayload * basepayload,
/* get total payload size and data size */ /* get total payload size and data size */
payload_size = gst_rtp_buffer_calc_payload_len (packet_size, 0, 0); payload_size = gst_rtp_buffer_calc_payload_len (packet_size, 0, 0);
data_size = payload_size - HEADER_SIZE; data_size = payload_size - GST_RTP_J2K_HEADER_SIZE;
/* make buffer for header */ /* make buffer for header */
outbuf = gst_rtp_buffer_new_allocate (HEADER_SIZE, 0, 0); outbuf = gst_rtp_buffer_new_allocate (GST_RTP_J2K_HEADER_SIZE, 0, 0);
GST_BUFFER_PTS (outbuf) = timestamp; GST_BUFFER_PTS (outbuf) = timestamp;

View file

@ -24,7 +24,6 @@
#include <gst/rtp/gstrtpbasepayload.h> #include <gst/rtp/gstrtpbasepayload.h>
G_BEGIN_DECLS G_BEGIN_DECLS
#define GST_TYPE_RTP_J2K_PAY \ #define GST_TYPE_RTP_J2K_PAY \
(gst_rtp_j2k_pay_get_type()) (gst_rtp_j2k_pay_get_type())
#define GST_RTP_J2K_PAY(obj) \ #define GST_RTP_J2K_PAY(obj) \
@ -35,7 +34,6 @@ G_BEGIN_DECLS
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_J2K_PAY)) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_J2K_PAY))
#define GST_IS_RTP_J2K_PAY_CLASS(klass) \ #define GST_IS_RTP_J2K_PAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_J2K_PAY)) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_J2K_PAY))
typedef struct _GstRtpJ2KPay GstRtpJ2KPay; typedef struct _GstRtpJ2KPay GstRtpJ2KPay;
typedef struct _GstRtpJ2KPayClass GstRtpJ2KPayClass; typedef struct _GstRtpJ2KPayClass GstRtpJ2KPayClass;
@ -57,5 +55,4 @@ GType gst_rtp_j2k_pay_get_type (void);
gboolean gst_rtp_j2k_pay_plugin_init (GstPlugin * plugin); gboolean gst_rtp_j2k_pay_plugin_init (GstPlugin * plugin);
G_END_DECLS G_END_DECLS
#endif /* __GST_RTP_J2K_PAY_H__ */ #endif /* __GST_RTP_J2K_PAY_H__ */