summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Dröge <sebastian.droege@collabora.co.uk>2009-03-18 14:50:17 +0100
committerSebastian Dröge <sebastian.droege@collabora.co.uk>2009-03-18 14:50:17 +0100
commit77e2637590e8c08a7a934982900fcd2a77769e0e (patch)
tree0a678956b046c1dd6b3637aae3fc2278c359c97c
parent8cf0e9ff87bb8fbc5200caee5154d64501a33191 (diff)
rtp: Use GLib functions for encoding/decoding base64
-rw-r--r--gst/rtp/gstrtph264depay.c61
-rw-r--r--gst/rtp/gstrtph264pay.c36
-rw-r--r--gst/rtp/gstrtptheoradepay.c54
-rw-r--r--gst/rtp/gstrtptheorapay.c27
-rw-r--r--gst/rtp/gstrtpvorbispay.c28
5 files changed, 19 insertions, 187 deletions
diff --git a/gst/rtp/gstrtph264depay.c b/gst/rtp/gstrtph264depay.c
index e8028f40..54cdbd7a 100644
--- a/gst/rtp/gstrtph264depay.c
+++ b/gst/rtp/gstrtph264depay.c
@@ -202,54 +202,6 @@ gst_rtp_h264_depay_get_property (GObject * object, guint prop_id,
}
}
-static const guint8 a2bin[256] = {
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
- 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
- 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
- 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
-};
-
-static guint
-decode_base64 (gchar * in, guint8 * out)
-{
- guint8 v1, v2;
- guint len = 0;
-
- v1 = a2bin[(gint) * in];
- while (v1 <= 63) {
- /* read 4 bytes, write 3 bytes, invalid base64 are zeroes */
- v2 = a2bin[(gint) * ++in];
- *out++ = (v1 << 2) | ((v2 & 0x3f) >> 4);
- v1 = (v2 > 63 ? 64 : a2bin[(gint) * ++in]);
- *out++ = (v2 << 4) | ((v1 & 0x3f) >> 2);
- v2 = (v1 > 63 ? 64 : a2bin[(gint) * ++in]);
- *out++ = (v1 << 6) | (v2 & 0x3f);
- v1 = (v2 > 63 ? 64 : a2bin[(gint) * ++in]);
- len += 3;
- }
- /* move to '\0' */
- while (*in != '\0')
- in++;
-
- /* subtract padding */
- while (len > 0 && *--in == '=')
- len--;
-
- return len;
-}
-
static gboolean
gst_rtp_h264_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
{
@@ -296,10 +248,13 @@ gst_rtp_h264_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
b64 = GST_BUFFER_DATA (codec_data);
total = 0;
for (i = 0; params[i]; i++) {
+ guint save = 0;
+ gint state = 0;
+
GST_DEBUG_OBJECT (depayload, "decoding param %d", i);
memcpy (b64, sync_bytes, sizeof (sync_bytes));
b64 += sizeof (sync_bytes);
- len = decode_base64 (params[i], b64);
+ len = g_base64_decode_step (params[i], -1, b64, &state, &save);
total += len + sizeof (sync_bytes);
b64 += len;
}
@@ -335,12 +290,16 @@ gst_rtp_h264_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
/* start with 7 bytes header */
len = 7;
for (i = 0; params[i]; i++) {
- gint nal_len;
+ gsize nal_len;
guint8 *nalp;
+ guint save = 0;
+ gint state = 0;
nal_len = strlen (params[i]);
nalp = g_malloc (nal_len + 2);
- nal_len = decode_base64 (params[i], nalp + 2);
+
+ nal_len =
+ g_base64_decode_step (params[i], nal_len, nalp + 2, &state, &save);
nalp[0] = (nal_len >> 8) & 0xff;
nalp[1] = nal_len & 0xff;
len += nal_len + 2;
diff --git a/gst/rtp/gstrtph264pay.c b/gst/rtp/gstrtph264pay.c
index a788316b..50770b54 100644
--- a/gst/rtp/gstrtph264pay.c
+++ b/gst/rtp/gstrtph264pay.c
@@ -206,31 +206,6 @@ gst_rtp_h264_pay_finalize (GObject * object)
G_OBJECT_CLASS (parent_class)->finalize (object);
}
-static gchar *
-encode_base64 (const guint8 * in, guint size, guint * len)
-{
- gchar *ret, *d;
- static const gchar *v =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- *len = ((size + 2) / 3) * 4;
- d = ret = (gchar *) g_malloc (*len + 1);
- for (; size; in += 3) { /* process tuplets */
- *d++ = v[in[0] >> 2]; /* byte 1: high 6 bits (1) */
- /* byte 2: low 2 bits (1), high 4 bits (2) */
- *d++ = v[((in[0] << 4) + (--size ? (in[1] >> 4) : 0)) & 0x3f];
- /* byte 3: low 4 bits (2), high 2 bits (3) */
- *d++ = size ? v[((in[1] << 2) + (--size ? (in[2] >> 6) : 0)) & 0x3f] : '=';
- /* byte 4: low 6 bits (3) */
- *d++ = size ? v[in[2] & 0x3f] : '=';
- if (size)
- size--; /* count third character if processed */
- }
- *d = '\0'; /* tie off string */
-
- return ret; /* return the resulting string */
-}
-
static gboolean
gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
{
@@ -295,7 +270,6 @@ gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
for (i = 0; i < num_sps; i++) {
gchar *set;
- guint len;
if (size < 2)
goto avcc_error;
@@ -309,7 +283,7 @@ gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
if (size < nal_size)
goto avcc_error;
- set = encode_base64 (data, nal_size, &len);
+ set = g_base64_encode (data, nal_size);
g_string_append_printf (sprops, "%s%s", count ? "," : "", set);
count++;
g_free (set);
@@ -328,7 +302,6 @@ gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
GST_DEBUG_OBJECT (rtph264pay, "num PPS %u", num_pps);
for (i = 0; i < num_pps; i++) {
gchar *set;
- guint len;
if (size < 2)
goto avcc_error;
@@ -342,7 +315,7 @@ gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
if (size < nal_size)
goto avcc_error;
- set = encode_base64 (data, nal_size, &len);
+ set = g_base64_encode (data, nal_size);
g_string_append_printf (sprops, "%s%s", count ? "," : "", set);
count++;
g_free (set);
@@ -553,16 +526,15 @@ gst_rtp_h264_pay_parse_sps_pps (GstBaseRTPPayload * basepayload,
gchar *sps;
gchar *pps;
gchar *sprops;
- guint len;
/* profile is 24 bit. Force it to respect the limit */
profile = g_strdup_printf ("%06x", payloader->profile & 0xffffff);
/* build the sprop-parameter-sets */
sps = (payloader->sps_len > 0)
- ? encode_base64 (payloader->sps, payloader->sps_len, &len) : NULL;
+ ? g_base64_encode (payloader->sps, payloader->sps_len) : NULL;
pps = (payloader->pps_len > 0)
- ? encode_base64 (payloader->pps, payloader->pps_len, &len) : NULL;
+ ? g_base64_encode (payloader->pps, payloader->pps_len) : NULL;
if (sps)
sprops = g_strjoin (",", sps, pps, NULL);
diff --git a/gst/rtp/gstrtptheoradepay.c b/gst/rtp/gstrtptheoradepay.c
index 588929db..7a902d56 100644
--- a/gst/rtp/gstrtptheoradepay.c
+++ b/gst/rtp/gstrtptheoradepay.c
@@ -124,6 +124,7 @@ gst_rtp_theora_depay_init (GstRtpTheoraDepay * rtptheoradepay,
{
rtptheoradepay->adapter = gst_adapter_new ();
}
+
static void
gst_rtp_theora_depay_finalize (GObject * object)
{
@@ -134,54 +135,6 @@ gst_rtp_theora_depay_finalize (GObject * object)
G_OBJECT_CLASS (parent_class)->finalize (object);
}
-static const guint8 a2bin[256] = {
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
- 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
- 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
- 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
- 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
-};
-
-static guint
-decode_base64 (const gchar * in, guint8 * out)
-{
- guint8 v1, v2;
- guint len = 0;
-
- v1 = a2bin[(gint) * in];
- while (v1 <= 63) {
- /* read 4 bytes, write 3 bytes, invalid base64 are zeroes */
- v2 = a2bin[(gint) * ++in];
- *out++ = (v1 << 2) | ((v2 & 0x3f) >> 4);
- v1 = (v2 > 63 ? 64 : a2bin[(gint) * ++in]);
- *out++ = (v2 << 4) | ((v1 & 0x3f) >> 2);
- v2 = (v1 > 63 ? 64 : a2bin[(gint) * ++in]);
- *out++ = (v1 << 6) | (v2 & 0x3f);
- v1 = (v2 > 63 ? 64 : a2bin[(gint) * ++in]);
- len += 3;
- }
- /* move to '\0' */
- while (*in != '\0')
- in++;
-
- /* subtract padding */
- while (len > 0 && *--in == '=')
- len--;
-
- return len;
-}
-
static gboolean
gst_rtp_theora_depay_parse_configuration (GstRtpTheoraDepay * rtptheoradepay,
const gchar * configuration)
@@ -189,15 +142,14 @@ gst_rtp_theora_depay_parse_configuration (GstRtpTheoraDepay * rtptheoradepay,
GstBuffer *buf;
guint32 num_headers;
guint8 *data;
- guint size;
+ gsize size;
gint i, j;
/* deserialize base64 to buffer */
size = strlen (configuration);
GST_DEBUG_OBJECT (rtptheoradepay, "base64 config size %u", size);
- data = g_malloc (size);
- size = decode_base64 (configuration, data);
+ data = g_base64_decode (configuration, &size);
GST_DEBUG_OBJECT (rtptheoradepay, "config size %u", size);
diff --git a/gst/rtp/gstrtptheorapay.c b/gst/rtp/gstrtptheorapay.c
index b96d3fe7..183d611f 100644
--- a/gst/rtp/gstrtptheorapay.c
+++ b/gst/rtp/gstrtptheorapay.c
@@ -233,31 +233,6 @@ gst_rtp_theora_pay_flush_packet (GstRtpTheoraPay * rtptheorapay)
return ret;
}
-static gchar *
-encode_base64 (const guint8 * in, guint size, guint * len)
-{
- gchar *ret, *d;
- static const gchar *v =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- *len = ((size + 2) / 3) * 4;
- d = ret = (gchar *) g_malloc (*len + 1);
- for (; size; in += 3) { /* process tuplets */
- *d++ = v[in[0] >> 2]; /* byte 1: high 6 bits (1) */
- /* byte 2: low 2 bits (1), high 4 bits (2) */
- *d++ = v[((in[0] << 4) + (--size ? (in[1] >> 4) : 0)) & 0x3f];
- /* byte 3: low 4 bits (2), high 2 bits (3) */
- *d++ = size ? v[((in[1] << 2) + (--size ? (in[2] >> 6) : 0)) & 0x3f] : '=';
- /* byte 4: low 6 bits (3) */
- *d++ = size ? v[in[2] & 0x3f] : '=';
- if (size)
- size--; /* count third character if processed */
- }
- *d = '\0'; /* tie off string */
-
- return ret; /* return the resulting string */
-}
-
static gboolean
gst_rtp_theora_pay_finish_headers (GstBaseRTPPayload * basepayload)
{
@@ -412,7 +387,7 @@ gst_rtp_theora_pay_finish_headers (GstBaseRTPPayload * basepayload)
}
/* serialize to base64 */
- configuration = encode_base64 (config, configlen, &size);
+ configuration = g_base64_encode (config, configlen);
g_free (config);
/* configure payloader settings */
diff --git a/gst/rtp/gstrtpvorbispay.c b/gst/rtp/gstrtpvorbispay.c
index 40011dba..4e3820e6 100644
--- a/gst/rtp/gstrtpvorbispay.c
+++ b/gst/rtp/gstrtpvorbispay.c
@@ -222,32 +222,6 @@ gst_rtp_vorbis_pay_flush_packet (GstRtpVorbisPay * rtpvorbispay)
return ret;
}
-static gchar *
-encode_base64 (const guint8 * in, guint size, guint * len)
-{
- gchar *ret, *d;
- static const gchar v[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- *len = ((size + 2) / 3) * 4;
- d = ret = (gchar *) g_malloc (*len + 1);
- for (; size; in += 3) { /* process tuplets */
- *d++ = v[in[0] >> 2]; /* byte 1: high 6 bits (1) */
- /* byte 2: low 2 bits (1), high 4 bits (2) */
- *d++ = v[((in[0] << 4) + (--size ? (in[1] >> 4) : 0)) & 0x3f];
- /* byte 3: low 4 bits (2), high 2 bits (3) */
- *d++ = size ? v[((in[1] << 2) + (--size ? (in[2] >> 6) : 0)) & 0x3f] : '=';
- /* byte 4: low 6 bits (3) */
- *d++ = size ? v[in[2] & 0x3f] : '=';
- if (size)
- size--; /* count third character if processed */
- }
- *d = '\0'; /* tie off string */
-
- return ret; /* return the resulting string */
-}
-
-
static gboolean
gst_rtp_vorbis_pay_finish_headers (GstBaseRTPPayload * basepayload)
{
@@ -398,7 +372,7 @@ gst_rtp_vorbis_pay_finish_headers (GstBaseRTPPayload * basepayload)
}
/* serialize to base64 */
- configuration = encode_base64 (config, configlen, &size);
+ configuration = g_base64_encode (config, configlen);
g_free (config);
/* configure payloader settings */