Reduce memory consumption and add the H.264 and H.263 modules I shamefully neglected...
authorJoshua Colp <jcolp@digium.com>
Fri, 13 Jul 2012 18:41:07 +0000 (18:41 +0000)
committerJoshua Colp <jcolp@digium.com>
Fri, 13 Jul 2012 18:41:07 +0000 (18:41 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@370060 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/format.h
res/res_format_attr_h263.c [new file with mode: 0644]
res/res_format_attr_h264.c [new file with mode: 0644]

index 9dd6eb8..61ddf90 100644 (file)
@@ -29,7 +29,7 @@
 #include "asterisk/astobj2.h"
 #include "asterisk/silk.h"
 #include "asterisk/celt.h"
-#define AST_FORMAT_ATTR_SIZE 128
+#define AST_FORMAT_ATTR_SIZE 64
 #define AST_FORMAT_INC 100000
 
 /*! This is the value that ends a var list of format attribute
diff --git a/res/res_format_attr_h263.c b/res/res_format_attr_h263.c
new file mode 100644 (file)
index 0000000..2ca39a9
--- /dev/null
@@ -0,0 +1,295 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ *
+ */
+
+/*! \file
+ *
+ * \brief H.263 Format Attribute Module
+ *
+ * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
+ *
+ * This is a format attribute module for the H.263 codec.
+ * \ingroup applications
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/format.h"
+
+enum h263_attr_keys {
+       H263_ATTR_KEY_SQCIF,       /*!< Minimum picture interval for SQCIF resolution */
+       H263_ATTR_KEY_QCIF,        /*!< Minimum picture interval for QCIF resolution */
+       H263_ATTR_KEY_CIF,         /*!< Minimum picture interval for CIF resolution */
+       H263_ATTR_KEY_CIF4,        /*!< Minimum picture interval for CIF4 resolution */
+       H263_ATTR_KEY_CIF16,       /*!< Minimum picture interval for CIF16 resolution */
+       H263_ATTR_KEY_VGA,         /*!< Minimum picture interval for VGA resolution */
+       H263_ATTR_KEY_CUSTOM_XMAX, /*!< Custom resolution (Xmax) */
+       H263_ATTR_KEY_CUSTOM_YMAX, /*!< Custom resolution (Ymax) */
+       H263_ATTR_KEY_CUSTOM_MPI,  /*!< Custom resolution (MPI) */
+       H263_ATTR_KEY_F,           /*!< F annex support */
+       H263_ATTR_KEY_I,           /*!< I annex support */
+       H263_ATTR_KEY_J,           /*!< J annex support */
+       H263_ATTR_KEY_T,           /*!< T annex support */
+       H263_ATTR_KEY_K,           /*!< K annex support */
+       H263_ATTR_KEY_N,           /*!< N annex support */
+       H263_ATTR_KEY_P_SUB1,      /*!< Reference picture resampling (sub mode 1) */
+       H263_ATTR_KEY_P_SUB2,      /*!< Reference picture resampling (sub mode 2) */
+       H263_ATTR_KEY_P_SUB3,      /*!< Reference picture resampling (sub mode 3) */
+       H263_ATTR_KEY_P_SUB4,      /*!< Reference picture resampling (sub mode 4) */
+       H263_ATTR_KEY_PAR_WIDTH,   /*!< Pixel aspect ratio (width) */
+       H263_ATTR_KEY_PAR_HEIGHT,  /*!< Pixel aspect ratio (height) */
+       H263_ATTR_KEY_BPP,         /*!< Bits per picture maximum */
+       H263_ATTR_KEY_HRD,         /*!< Hypothetical reference decoder status */
+       H263_ATTR_KEY_END,         /*!< End terminator for list */
+};
+
+static int h263_format_attr_get_joint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
+{
+       int i;
+
+       /* These are all receiver options so we just copy over what they sent */
+       for (i = H263_ATTR_KEY_SQCIF; i < H263_ATTR_KEY_END; i++) {
+               result->format_attr[i] = fattr1->format_attr[i] ? fattr1->format_attr[i] : fattr2->format_attr[i];
+       }
+
+       return 0;
+}
+
+static int h263_format_attr_sdp_parse(struct ast_format_attr *format_attr, const char *attributes)
+{
+       char *attribs = ast_strdupa(attributes), *attrib;
+
+       while ((attrib = strsep(&attribs, ";"))) {
+               unsigned int val, val2 = 0, val3 = 0, val4 = 0;
+
+               if (sscanf(attrib, "SQCIF=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_SQCIF] = val;
+               } else if (sscanf(attrib, "QCIF=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_QCIF] = val;
+               } else if (sscanf(attrib, "CIF=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_CIF] = val;
+               } else if (sscanf(attrib, "CIF4=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_CIF4] = val;
+               } else if (sscanf(attrib, "CIF16=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_CIF16] = val;
+               } else if (sscanf(attrib, "VGA=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_VGA] = val;
+               } else if (sscanf(attrib, "CUSTOM=%30u,%30u,%30u", &val, &val2, &val3) == 3) {
+                       format_attr->format_attr[H263_ATTR_KEY_CUSTOM_XMAX] = val;
+                       format_attr->format_attr[H263_ATTR_KEY_CUSTOM_YMAX] = val2;
+                       format_attr->format_attr[H263_ATTR_KEY_CUSTOM_MPI] = val3;
+               } else if (sscanf(attrib, "F=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_F] = val;
+               } else if (sscanf(attrib, "I=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_I] = val;
+               } else if (sscanf(attrib, "J=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_J] = val;
+               } else if (sscanf(attrib, "T=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_T] = val;
+               } else if (sscanf(attrib, "K=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_K] = val;
+               } else if (sscanf(attrib, "N=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_N] = val;
+               } else if (sscanf(attrib, "PAR=%30u:%30u", &val, &val2) == 2) {
+                       format_attr->format_attr[H263_ATTR_KEY_PAR_WIDTH] = val;
+                       format_attr->format_attr[H263_ATTR_KEY_PAR_HEIGHT] = val2;
+               } else if (sscanf(attrib, "BPP=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_BPP] = val;
+               } else if (sscanf(attrib, "HRD=%30u", &val) == 1) {
+                       format_attr->format_attr[H263_ATTR_KEY_HRD] = val;
+               } else if (sscanf(attrib, "P=%30u,%30u,%30u,%30u", &val, &val2, &val3, &val4) > 0) {
+                       format_attr->format_attr[H263_ATTR_KEY_P_SUB1] = val;
+                       format_attr->format_attr[H263_ATTR_KEY_P_SUB2] = val2;
+                       format_attr->format_attr[H263_ATTR_KEY_P_SUB3] = val3;
+                       format_attr->format_attr[H263_ATTR_KEY_P_SUB4] = val4;
+               }
+       }
+
+       return 0;
+}
+
+/*! \brief Helper function which converts a key enum into a string value for SDP */
+static const char *h263_attr_key_to_str(enum h263_attr_keys key, const struct ast_format_attr *format_attr)
+{
+       switch (key) {
+       case H263_ATTR_KEY_SQCIF:
+               return format_attr->format_attr[key] ? "SQCIF" : NULL;
+       case H263_ATTR_KEY_QCIF:
+               return format_attr->format_attr[key] ? "QCIF" : NULL;
+       case H263_ATTR_KEY_CIF:
+               return format_attr->format_attr[key] ? "CIF" : NULL;
+       case H263_ATTR_KEY_CIF4:
+               return format_attr->format_attr[key] ? "CIF4" : NULL;
+       case H263_ATTR_KEY_CIF16:
+               return format_attr->format_attr[key] ? "CIF16" : NULL;
+       case H263_ATTR_KEY_VGA:
+               return format_attr->format_attr[key] ? "VGA" : NULL;
+       case H263_ATTR_KEY_F:
+               return "F";
+       case H263_ATTR_KEY_I:
+               return "I";
+       case H263_ATTR_KEY_J:
+               return "J";
+       case H263_ATTR_KEY_T:
+               return "T";
+       case H263_ATTR_KEY_K:
+               return "K";
+       case H263_ATTR_KEY_N:
+               return "N";
+       case H263_ATTR_KEY_BPP:
+               return "BPP";
+       case H263_ATTR_KEY_HRD:
+               return "HRD";
+       case H263_ATTR_KEY_CUSTOM_XMAX:
+       case H263_ATTR_KEY_CUSTOM_YMAX:
+       case H263_ATTR_KEY_CUSTOM_MPI:
+       case H263_ATTR_KEY_P_SUB1:
+       case H263_ATTR_KEY_P_SUB2:
+       case H263_ATTR_KEY_P_SUB3:
+       case H263_ATTR_KEY_P_SUB4:
+       case H263_ATTR_KEY_PAR_WIDTH:
+       case H263_ATTR_KEY_PAR_HEIGHT:
+       case H263_ATTR_KEY_END:
+       default:
+               return NULL;
+       }
+
+       return NULL;
+}
+
+static void h263_format_attr_sdp_generate(const struct ast_format_attr *format_attr, unsigned int payload, struct ast_str **str)
+{
+       int i, added = 0;
+
+       for (i = H263_ATTR_KEY_SQCIF; i < H263_ATTR_KEY_END; i++) {
+               const char *name;
+
+               if (i == H263_ATTR_KEY_CUSTOM_XMAX) {
+                       if (!format_attr->format_attr[H263_ATTR_KEY_CUSTOM_XMAX] || !format_attr->format_attr[H263_ATTR_KEY_CUSTOM_YMAX] ||
+                           !format_attr->format_attr[H263_ATTR_KEY_CUSTOM_MPI]) {
+                               continue;
+                       }
+
+                       if (!added) {
+                               ast_str_append(str, 0, "a=fmtp:%d CUSTOM=%u,%u,%u", payload, format_attr->format_attr[H263_ATTR_KEY_CUSTOM_XMAX],
+                                              format_attr->format_attr[H263_ATTR_KEY_CUSTOM_YMAX], format_attr->format_attr[H263_ATTR_KEY_CUSTOM_MPI]);
+                               added = 1;
+                       } else {
+                               ast_str_append(str, 0, ";CUSTOM=%u,%u,%u", format_attr->format_attr[H263_ATTR_KEY_CUSTOM_XMAX],
+                                              format_attr->format_attr[H263_ATTR_KEY_CUSTOM_YMAX], format_attr->format_attr[H263_ATTR_KEY_CUSTOM_MPI]);
+                       }
+               } else if (i == H263_ATTR_KEY_PAR_WIDTH) {
+                       if (!format_attr->format_attr[H263_ATTR_KEY_PAR_WIDTH] || !format_attr->format_attr[H263_ATTR_KEY_PAR_HEIGHT]) {
+                               continue;
+                       }
+
+                       if (!added) {
+                               ast_str_append(str, 0, "a=fmtp:%d PAR=%u:%u", payload, format_attr->format_attr[H263_ATTR_KEY_PAR_WIDTH],
+                                              format_attr->format_attr[H263_ATTR_KEY_PAR_HEIGHT]);
+                               added = 1;
+                       } else {
+                               ast_str_append(str, 0, ";PAR=%u:%u", format_attr->format_attr[H263_ATTR_KEY_PAR_WIDTH],
+                                              format_attr->format_attr[H263_ATTR_KEY_PAR_HEIGHT]);
+                       }
+               } else if (i == H263_ATTR_KEY_P_SUB1) {
+                       if (!format_attr->format_attr[H263_ATTR_KEY_P_SUB1]) {
+                               continue;
+                       }
+
+                       if (!added) {
+                               ast_str_append(str, 0, "a=fmtp:%d P=%u", payload, format_attr->format_attr[H263_ATTR_KEY_P_SUB1]);
+                               added = 1;
+                       } else {
+                               ast_str_append(str, 0, ";P=%u", format_attr->format_attr[H263_ATTR_KEY_P_SUB1]);
+                       }
+
+                       if (format_attr->format_attr[H263_ATTR_KEY_P_SUB2]) {
+                               ast_str_append(str, 0, ",%u", format_attr->format_attr[H263_ATTR_KEY_P_SUB2]);
+                       }
+                        if (format_attr->format_attr[H263_ATTR_KEY_P_SUB3]) {
+                                ast_str_append(str, 0, ",%u", format_attr->format_attr[H263_ATTR_KEY_P_SUB3]);
+                        }
+                        if (format_attr->format_attr[H263_ATTR_KEY_P_SUB4]) {
+                                ast_str_append(str, 0, ",%u", format_attr->format_attr[H263_ATTR_KEY_P_SUB4]);
+                        }
+
+               } else if ((name = h263_attr_key_to_str(i, format_attr))) {
+                       if (!added) {
+                               ast_str_append(str, 0, "a=fmtp:%d %s=%u", payload, name, format_attr->format_attr[i]);
+                               added = 1;
+                       } else {
+                               ast_str_append(str, 0, ";%s=%u", name, format_attr->format_attr[i]);
+                       }
+               }
+       }
+
+       if (added) {
+               ast_str_append(str, 0, "\r\n");
+       }
+
+       return;
+}
+
+static struct ast_format_attr_interface h263_format_attr_interface = {
+       .id = AST_FORMAT_H263,
+       .format_attr_get_joint = h263_format_attr_get_joint,
+       .format_attr_sdp_parse = h263_format_attr_sdp_parse,
+       .format_attr_sdp_generate = h263_format_attr_sdp_generate,
+};
+
+static struct ast_format_attr_interface h263p_format_attr_interface = {
+        .id = AST_FORMAT_H263_PLUS,
+       .format_attr_get_joint = h263_format_attr_get_joint,
+        .format_attr_sdp_parse = h263_format_attr_sdp_parse,
+        .format_attr_sdp_generate = h263_format_attr_sdp_generate,
+};
+
+static int unload_module(void)
+{
+       ast_format_attr_unreg_interface(&h263_format_attr_interface);
+       ast_format_attr_unreg_interface(&h263p_format_attr_interface);
+
+       return 0;
+}
+
+static int load_module(void)
+{
+       if (ast_format_attr_reg_interface(&h263_format_attr_interface)) {
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       if (ast_format_attr_reg_interface(&h263p_format_attr_interface)) {
+               ast_format_attr_unreg_interface(&h263_format_attr_interface);
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "H.263 Format Attribute Module",
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_DEFAULT,
+);
diff --git a/res/res_format_attr_h264.c b/res/res_format_attr_h264.c
new file mode 100644 (file)
index 0000000..c488bab
--- /dev/null
@@ -0,0 +1,305 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ *
+ */
+
+/*! \file
+ *
+ * \brief H.264 Format Attribute Module
+ *
+ * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
+ *
+ * This is a format attribute module for the H.264 codec.
+ * \ingroup applications
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/format.h"
+
+/*! \brief Value that indicates an attribute is actually unset */
+#define H264_ATTR_KEY_UNSET UINT8_MAX
+
+/*! \brief Maximum size for SPS / PPS values in sprop-parameter-sets attribute */
+#define H264_MAX_SPS_PPS_SIZE 16
+
+enum h264_attr_keys {
+       H264_ATTR_KEY_PROFILE_IDC,
+       H264_ATTR_KEY_PROFILE_IOP,
+       H264_ATTR_KEY_LEVEL,
+       H264_ATTR_KEY_MAX_MBPS,
+       H264_ATTR_KEY_MAX_FS,
+       H264_ATTR_KEY_MAX_CPB,
+       H264_ATTR_KEY_MAX_DPB,
+       H264_ATTR_KEY_MAX_BR,
+       H264_ATTR_KEY_REDUNDANT_PIC_CAP,
+       H264_ATTR_KEY_PARAMETER_ADD,
+       H264_ATTR_KEY_PACKETIZATION_MODE,
+       H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH,
+       H264_ATTR_KEY_SPROP_DEINT_BUF_REQ,
+       H264_ATTR_KEY_DEINT_BUF_CAP,
+       H264_ATTR_KEY_SPROP_INIT_BUF_TIME,
+       H264_ATTR_KEY_SPROP_MAX_DON_DIFF,
+       H264_ATTR_KEY_MAX_RCMD_NALU_SIZE,
+       H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED,
+       H264_ATTR_KEY_SPS_LEN,
+       H264_ATTR_KEY_PPS_LEN,
+       H264_ATTR_KEY_SPS,
+       H264_ATTR_KEY_PPS = H264_ATTR_KEY_SPS + H264_MAX_SPS_PPS_SIZE,
+       H264_ATTR_KEY_END = H264_ATTR_KEY_PPS + H264_MAX_SPS_PPS_SIZE,
+};
+
+static enum ast_format_cmp_res h264_format_attr_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
+{
+       unsigned int idc1 = fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] ? fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] : 0x42;
+       unsigned int idc2 = fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC] ? fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC] : 0x42;
+
+       /* We are as permissive as possible to ensure the maximum number of calls succeed */
+       return (idc1 == idc2) ? AST_FORMAT_CMP_EQUAL : AST_FORMAT_CMP_NOT_EQUAL;
+}
+
+static int h264_format_attr_get_joint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
+{
+       int i;
+
+       for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
+               result->format_attr[i] = fattr1->format_attr[i] ? fattr1->format_attr[i] : fattr2->format_attr[i];
+       }
+
+       return 0;
+}
+
+static int h264_format_attr_sdp_parse(struct ast_format_attr *format_attr, const char *attributes)
+{
+       char *attribs = ast_strdupa(attributes), *attrib;
+
+       format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = H264_ATTR_KEY_UNSET;
+       format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = H264_ATTR_KEY_UNSET;
+       format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = H264_ATTR_KEY_UNSET;
+       format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = H264_ATTR_KEY_UNSET;
+
+       while ((attrib = strsep(&attribs, ";"))) {
+               unsigned int val;
+               unsigned long int val2;
+               char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
+
+               if (sscanf(attrib, "profile-level-id=%lx", &val2) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] = ((val2 >> 16) & 0xFF);
+                       format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] = ((val2 >> 8) & 0xFF);
+                       format_attr->format_attr[H264_ATTR_KEY_LEVEL] = (val2 & 0xFF);
+               } else if (sscanf(attrib, "sprop-parameter-sets=%[^','],%s", sps, pps) == 2) {
+                       unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
+                       int i;
+
+                       ast_base64decode(spsdecoded, sps, sizeof(spsdecoded));
+                       ast_base64decode(ppsdecoded, pps, sizeof(ppsdecoded));
+
+                       format_attr->format_attr[H264_ATTR_KEY_SPS_LEN] = 0;
+                       format_attr->format_attr[H264_ATTR_KEY_PPS_LEN] = 0;
+
+                       for (i = 0; i < H264_MAX_SPS_PPS_SIZE; i++) {
+                               if (spsdecoded[i]) {
+                                       format_attr->format_attr[H264_ATTR_KEY_SPS + i] = spsdecoded[i];
+                                       format_attr->format_attr[H264_ATTR_KEY_SPS_LEN]++;
+                               }
+                               if (ppsdecoded[i]) {
+                                       format_attr->format_attr[H264_ATTR_KEY_PPS + i] = ppsdecoded[i];
+                                       format_attr->format_attr[H264_ATTR_KEY_PPS_LEN]++;
+                               }
+                       }
+               } else if (sscanf(attrib, "max-mbps=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_MAX_MBPS] = val;
+               } else if (sscanf(attrib, "max-fs=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_MAX_FS] = val;
+               } else if (sscanf(attrib, "max-cpb=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_MAX_CPB] = val;
+               } else if (sscanf(attrib, "max-dpb=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_MAX_DPB] = val;
+               } else if (sscanf(attrib, "max-br=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_MAX_BR] = val;
+               } else if (sscanf(attrib, "redundant-pic-cap=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = val;
+               } else if (sscanf(attrib, "parameter-add=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = val;
+               } else if (sscanf(attrib, "packetization-mode=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = val;
+               } else if (sscanf(attrib, "sprop-interleaving-depth=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH] = val;
+               } else if (sscanf(attrib, "sprop-deint-buf-req=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_SPROP_DEINT_BUF_REQ] = val;
+               } else if (sscanf(attrib, "deint-buf-cap=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_DEINT_BUF_CAP] = val;
+               } else if (sscanf(attrib, "sprop-init-buf-time=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_SPROP_INIT_BUF_TIME] = val;
+               } else if (sscanf(attrib, "sprop-max-don-diff=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_SPROP_MAX_DON_DIFF] = val;
+               } else if (sscanf(attrib, "max-rcmd-nalu-size=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_MAX_RCMD_NALU_SIZE] = val;
+               } else if (sscanf(attrib, "level-asymmetry-allowed=%30u", &val) == 1) {
+                       format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = val;
+               }
+       }
+
+       return 0;
+}
+
+/*! \brief Helper function which converts a key enum into a string value for SDP */
+static const char *h264_attr_key_to_str(enum h264_attr_keys key)
+{
+       switch (key) {
+       case H264_ATTR_KEY_MAX_MBPS:
+               return "max-mbps";
+       case H264_ATTR_KEY_MAX_FS:
+               return "max-fs";
+       case H264_ATTR_KEY_MAX_CPB:
+               return "max-cpb";
+       case H264_ATTR_KEY_MAX_DPB:
+               return "max-dpb";
+       case H264_ATTR_KEY_MAX_BR:
+               return "max-br";
+       case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
+               return "redundant-pic-cap";
+       case H264_ATTR_KEY_PARAMETER_ADD:
+               return "parameter-add";
+       case H264_ATTR_KEY_PACKETIZATION_MODE:
+               return "packetization-mode";
+       case H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH:
+               return "sprop-interleaving-depth";
+       case H264_ATTR_KEY_SPROP_DEINT_BUF_REQ:
+               return "sprop-deint-buf-req";
+       case H264_ATTR_KEY_DEINT_BUF_CAP:
+               return "deint-buf-cap";
+       case H264_ATTR_KEY_SPROP_INIT_BUF_TIME:
+               return "sprop-init-buf-time";
+       case H264_ATTR_KEY_SPROP_MAX_DON_DIFF:
+               return "sprop-max-don-diff";
+       case H264_ATTR_KEY_MAX_RCMD_NALU_SIZE:
+               return "max-rcmd-nalu-size";
+       case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
+               return "level-asymmetry-allowed";
+       default:
+               return NULL;
+       }
+
+       return NULL;
+}
+
+/*! \brief Helper function which determines if the value of an attribute can be placed into the SDP */
+static int h264_attr_key_addable(const struct ast_format_attr *format_attr, enum h264_attr_keys key)
+{
+       switch (key) {
+       case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
+       case H264_ATTR_KEY_PARAMETER_ADD:
+       case H264_ATTR_KEY_PACKETIZATION_MODE:
+       case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
+               return (format_attr->format_attr[key] != H264_ATTR_KEY_UNSET) ? 1 : 0;
+       default:
+               return format_attr->format_attr[key] ? 1 : 0;
+       }
+
+       return 1;
+}
+
+static void h264_format_attr_sdp_generate(const struct ast_format_attr *format_attr, unsigned int payload, struct ast_str **str)
+{
+       int i, added = 0;
+
+        for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
+                const char *name;
+
+               if (i == H264_ATTR_KEY_SPS && format_attr->format_attr[H264_ATTR_KEY_SPS] && format_attr->format_attr[H264_ATTR_KEY_PPS]) {
+                       unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
+                       int pos;
+                       char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
+
+                       for (pos = 0; pos < H264_MAX_SPS_PPS_SIZE; pos++) {
+                               spsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_SPS + pos];
+                               ppsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_PPS + pos];
+                       }
+
+                       ast_base64encode(sps, spsdecoded, format_attr->format_attr[H264_ATTR_KEY_SPS_LEN], H264_MAX_SPS_PPS_SIZE);
+                       ast_base64encode(pps, ppsdecoded, format_attr->format_attr[H264_ATTR_KEY_PPS_LEN], H264_MAX_SPS_PPS_SIZE);
+
+                       if (!added) {
+                               ast_str_append(str, 0, "a=fmtp:%d sprop-parameter-sets=%s,%s", payload, sps, pps);
+                               added = 1;
+                       } else {
+                               ast_str_append(str, 0, ";sprop-parameter-sets=%s,%s", sps, pps);
+                       }
+               } else if (i == H264_ATTR_KEY_PROFILE_IDC && format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] &&
+                   format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] && format_attr->format_attr[H264_ATTR_KEY_LEVEL]) {
+                       if (!added) {
+                               ast_str_append(str, 0, "a=fmtp:%d profile-level-id=%X%X%X", payload, format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
+                                              format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
+                               added = 1;
+                       } else {
+                               ast_str_append(str, 0, ";profile-level-id=%X%X%X", format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
+                                              format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
+                       }
+               } else if ((name = h264_attr_key_to_str(i)) && h264_attr_key_addable(format_attr, i)) {
+                       if (!added) {
+                               ast_str_append(str, 0, "a=fmtp:%d %s=%u", payload, name, format_attr->format_attr[i]);
+                               added = 1;
+                       } else {
+                               ast_str_append(str, 0, ";%s=%u", name, format_attr->format_attr[i]);
+                       }
+               }
+       }
+       
+       if (added) {
+               ast_str_append(str, 0, "\r\n");
+       }
+
+       return;
+}
+
+static struct ast_format_attr_interface h264_format_attr_interface = {
+       .id = AST_FORMAT_H264,
+       .format_attr_cmp = h264_format_attr_cmp,
+       .format_attr_get_joint = h264_format_attr_get_joint,
+       .format_attr_sdp_parse = h264_format_attr_sdp_parse,
+       .format_attr_sdp_generate = h264_format_attr_sdp_generate,
+};
+
+static int unload_module(void)
+{
+       ast_format_attr_unreg_interface(&h264_format_attr_interface);
+
+       return 0;
+}
+
+static int load_module(void)
+{
+       if (ast_format_attr_reg_interface(&h264_format_attr_interface)) {
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "H.264 Format Attribute Module",
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_DEFAULT,
+);