Commit 77e26375 authored by Sebastian Dröge's avatar Sebastian Dröge
Browse files

rtp: Use GLib functions for encoding/decoding base64

parent 8cf0e9ff
......@@ -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;
......
......@@ -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);
......
......@@ -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);
......
......@@ -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 */
......
......@@ -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 */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment