Add "rtp-efr" format support for RTP payload according to RFC3551 4.5.9

The RTP EFR payload is a bit like the FR payload: one nibble magic
marker, then followed by the actual codec bits.  So we need to
add/remove that magic marker and shift the remainder by one nibble.
diff --git a/src/Makefile.am b/src/Makefile.am
index 07f1d51..c62e4e7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -6,7 +6,7 @@
 
 COM_SOURCES = procqueue.c pq_file.c pq_format.c pq_codec.c pq_rtp.c pq_alsa.c \
 		formats.c fmt_amr.c fmt_gsm.c fmt_hr_ref.c fmt_racal.c \
-		fmt_amr_opencore.c fmt_rtp_amr.c fmt_rawpcm.c fmt_ti.c benchmark.c \
+		fmt_amr_opencore.c fmt_rtp_amr.c fmt_rtp_efr.c fmt_rawpcm.c fmt_ti.c benchmark.c \
 		codecs.c codec_pcm.c codec_hr.c codec_fr.c codec_efr.c codec_amr.c
 
 bin_PROGRAMS = gapk
diff --git a/src/fmt_rtp_efr.c b/src/fmt_rtp_efr.c
new file mode 100644
index 0000000..0132e32
--- /dev/null
+++ b/src/fmt_rtp_efr.c
@@ -0,0 +1,73 @@
+/* EFR RTP Payload according to RFC3551. Only one codec frame per RTP */
+/* (C) 2017 by Harald Welte <laforge@gnumonks.org> */
+
+/*
+ * This file is part of gapk (GSM Audio Pocket Knife).
+ *
+ * gapk is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * gapk 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gapk.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdint.h>
+#include <assert.h>
+
+#include <osmocom/codec/codec.h>
+
+#include <gapk/codecs.h>
+#include <gapk/formats.h>
+#include <gapk/utils.h>
+
+#define EFR_LEN	  31
+#define EFR_MAGIC 0xc
+
+/* conversion function: RTP payload -> canonical format */
+static int
+rtp_efr_from_canon(uint8_t *dst, const uint8_t *src, unsigned int src_len)
+{
+	int i;
+
+	/* according to RFC3551 4.5.9 */
+	assert(src_len == EFR_CANON_LEN);
+
+	dst[0] = (EFR_MAGIC << 4) | (src[0] >> 4);
+	for (i=1; i<EFR_LEN; i++)
+		dst[i] = (src[i-1] << 4) | (src[i] >> 4);
+
+	return EFR_LEN;
+}
+
+/* conversion function: canonical format -> RTP payload */
+static int
+rtp_efr_to_canon(uint8_t *dst, const uint8_t *src, unsigned int src_len)
+{
+	int i;
+
+	assert(src_len == EFR_LEN);
+
+	for (i=0; i<(EFR_LEN-1); i++)
+		dst[i] = (src[i] << 4) | (src[i+1] >> 4);
+	dst[EFR_LEN-1] = src[EFR_LEN-1] << 4;
+
+	return EFR_CANON_LEN;
+}
+
+const struct format_desc fmt_rtp_efr = {
+	.type			= FMT_RTP_EFR,
+	.codec_type		= CODEC_EFR,
+	.name			= "rtp-efr",
+	.description		= "RTP payload for EFR according to RFC3551",
+
+	.frame_len		= EFR_LEN,
+	.conv_from_canon	= rtp_efr_from_canon,
+	.conv_to_canon		= rtp_efr_to_canon,
+};
diff --git a/src/formats.c b/src/formats.c
index 0d4c4a0..6b2ee91 100644
--- a/src/formats.c
+++ b/src/formats.c
@@ -36,6 +36,7 @@
 extern const struct format_desc fmt_ti_efr;
 extern const struct format_desc fmt_amr_opencore;
 extern const struct format_desc fmt_rtp_amr;
+extern const struct format_desc fmt_rtp_efr;
 
 static const struct format_desc *supported_formats[_FMT_MAX] = {
 	[FMT_INVALID]		= NULL,
@@ -52,6 +53,7 @@
 	[FMT_TI_EFR]		= &fmt_ti_efr,
 	[FMT_AMR_OPENCORE]	= &fmt_amr_opencore,
 	[FMT_RTP_AMR]		= &fmt_rtp_amr,
+	[FMT_RTP_EFR]		= &fmt_rtp_efr,
 };