res_format_attr_h264.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. *
  18. */
  19. /*! \file
  20. *
  21. * \brief H.264 Format Attribute Module
  22. *
  23. * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
  24. *
  25. * This is a format attribute module for the H.264 codec.
  26. * \ingroup applications
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/module.h"
  34. #include "asterisk/format.h"
  35. /*! \brief Value that indicates an attribute is actually unset */
  36. #define H264_ATTR_KEY_UNSET UINT8_MAX
  37. /*! \brief Maximum size for SPS / PPS values in sprop-parameter-sets attribute
  38. * if you change this value then you must change H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT
  39. * as well. */
  40. #define H264_MAX_SPS_PPS_SIZE 16
  41. /*! \brief This is used when executing sscanf on buffers of H264_MAX_SPS_PPS_SIZE
  42. * length. It must ALWAYS be a string literal representation of one less than
  43. * H264_MAX_SPS_PPS_SIZE */
  44. #define H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "15"
  45. struct h264_attr {
  46. unsigned int PROFILE_IDC;
  47. unsigned int PROFILE_IOP;
  48. unsigned int LEVEL;
  49. unsigned int MAX_MBPS;
  50. unsigned int MAX_FS;
  51. unsigned int MAX_CPB;
  52. unsigned int MAX_DPB;
  53. unsigned int MAX_BR;
  54. unsigned int MAX_SMBPS;
  55. unsigned int MAX_FPS;
  56. unsigned int REDUNDANT_PIC_CAP;
  57. unsigned int PARAMETER_ADD;
  58. unsigned int PACKETIZATION_MODE;
  59. unsigned int SPROP_INTERLEAVING_DEPTH;
  60. unsigned int SPROP_DEINT_BUF_REQ;
  61. unsigned int DEINT_BUF_CAP;
  62. unsigned int SPROP_INIT_BUF_TIME;
  63. unsigned int SPROP_MAX_DON_DIFF;
  64. unsigned int MAX_RCMD_NALU_SIZE;
  65. unsigned int LEVEL_ASYMMETRY_ALLOWED;
  66. char SPS[H264_MAX_SPS_PPS_SIZE];
  67. char PPS[H264_MAX_SPS_PPS_SIZE];
  68. };
  69. static void h264_destroy(struct ast_format *format)
  70. {
  71. struct h264_attr *attr = ast_format_get_attribute_data(format);
  72. ast_free(attr);
  73. }
  74. static int h264_clone(const struct ast_format *src, struct ast_format *dst)
  75. {
  76. struct h264_attr *original = ast_format_get_attribute_data(src);
  77. struct h264_attr *attr = ast_calloc(1, sizeof(*attr));
  78. if (!attr) {
  79. return -1;
  80. }
  81. if (original) {
  82. *attr = *original;
  83. }
  84. ast_format_set_attribute_data(dst, attr);
  85. return 0;
  86. }
  87. static enum ast_format_cmp_res h264_cmp(const struct ast_format *format1, const struct ast_format *format2)
  88. {
  89. struct h264_attr *attr1 = ast_format_get_attribute_data(format1);
  90. struct h264_attr *attr2 = ast_format_get_attribute_data(format2);
  91. if (!attr1 || !attr1->PROFILE_IDC || !attr2 || !attr2->PROFILE_IDC ||
  92. (attr1->PROFILE_IDC == attr2->PROFILE_IDC)) {
  93. return AST_FORMAT_CMP_EQUAL;
  94. }
  95. return AST_FORMAT_CMP_NOT_EQUAL;
  96. }
  97. #define DETERMINE_JOINT(joint, attr1, attr2, field) (joint->field = (attr1 && attr1->field) ? attr1->field : (attr2 && attr2->field) ? attr2->field : 0)
  98. static struct ast_format *h264_getjoint(const struct ast_format *format1, const struct ast_format *format2)
  99. {
  100. struct ast_format *cloned;
  101. struct h264_attr *attr, *attr1, *attr2;
  102. cloned = ast_format_clone(format1);
  103. if (!cloned) {
  104. return NULL;
  105. }
  106. attr = ast_format_get_attribute_data(cloned);
  107. attr1 = ast_format_get_attribute_data(format1);
  108. attr2 = ast_format_get_attribute_data(format2);
  109. DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IDC);
  110. DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IOP);
  111. DETERMINE_JOINT(attr, attr1, attr2, LEVEL);
  112. DETERMINE_JOINT(attr, attr1, attr2, MAX_MBPS);
  113. DETERMINE_JOINT(attr, attr1, attr2, MAX_FS);
  114. DETERMINE_JOINT(attr, attr1, attr2, MAX_CPB);
  115. DETERMINE_JOINT(attr, attr1, attr2, MAX_DPB);
  116. DETERMINE_JOINT(attr, attr1, attr2, MAX_BR);
  117. DETERMINE_JOINT(attr, attr1, attr2, MAX_SMBPS);
  118. DETERMINE_JOINT(attr, attr1, attr2, MAX_FPS);
  119. DETERMINE_JOINT(attr, attr1, attr2, REDUNDANT_PIC_CAP);
  120. DETERMINE_JOINT(attr, attr1, attr2, PARAMETER_ADD);
  121. DETERMINE_JOINT(attr, attr1, attr2, SPROP_INTERLEAVING_DEPTH);
  122. DETERMINE_JOINT(attr, attr1, attr2, SPROP_DEINT_BUF_REQ);
  123. DETERMINE_JOINT(attr, attr1, attr2, DEINT_BUF_CAP);
  124. DETERMINE_JOINT(attr, attr1, attr2, SPROP_INIT_BUF_TIME);
  125. DETERMINE_JOINT(attr, attr1, attr2, SPROP_MAX_DON_DIFF);
  126. DETERMINE_JOINT(attr, attr1, attr2, MAX_RCMD_NALU_SIZE);
  127. DETERMINE_JOINT(attr, attr1, attr2, LEVEL_ASYMMETRY_ALLOWED);
  128. DETERMINE_JOINT(attr, attr1, attr2, PACKETIZATION_MODE);
  129. if (attr1 && !ast_strlen_zero(attr1->SPS)) {
  130. ast_copy_string(attr->SPS, attr1->SPS, sizeof(attr->SPS));
  131. } else if (attr2 && !ast_strlen_zero(attr2->SPS)) {
  132. ast_copy_string(attr->SPS, attr1->SPS, sizeof(attr->SPS));
  133. }
  134. if (attr1 && !ast_strlen_zero(attr1->PPS)) {
  135. ast_copy_string(attr->PPS, attr1->PPS, sizeof(attr->PPS));
  136. } else if (attr2 && !ast_strlen_zero(attr2->PPS)) {
  137. ast_copy_string(attr->PPS, attr1->PPS, sizeof(attr->PPS));
  138. }
  139. return cloned;
  140. }
  141. static struct ast_format *h264_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  142. {
  143. char *attribs = ast_strdupa(attributes), *attrib;
  144. struct ast_format *cloned;
  145. struct h264_attr *attr;
  146. cloned = ast_format_clone(format);
  147. if (!cloned) {
  148. return NULL;
  149. }
  150. attr = ast_format_get_attribute_data(cloned);
  151. attr->REDUNDANT_PIC_CAP = H264_ATTR_KEY_UNSET;
  152. attr->PARAMETER_ADD = H264_ATTR_KEY_UNSET;
  153. attr->PACKETIZATION_MODE = H264_ATTR_KEY_UNSET;
  154. attr->LEVEL_ASYMMETRY_ALLOWED = H264_ATTR_KEY_UNSET;
  155. while ((attrib = strsep(&attribs, ";"))) {
  156. unsigned int val;
  157. unsigned long int val2;
  158. if (sscanf(attrib, "profile-level-id=%lx", &val2) == 1) {
  159. attr->PROFILE_IDC = ((val2 >> 16) & 0xFF);
  160. attr->PROFILE_IOP = ((val2 >> 8) & 0xFF);
  161. attr->LEVEL = (val2 & 0xFF);
  162. } else if (sscanf(attrib, "sprop-parameter-sets=%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "[^','],%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "s", attr->SPS, attr->PPS) == 2) {
  163. /* XXX sprop-parameter-sets can actually be of unlimited length. This may need to be addressed later. */
  164. } else if (sscanf(attrib, "max-mbps=%30u", &val) == 1) {
  165. attr->MAX_MBPS = val;
  166. } else if (sscanf(attrib, "max-fs=%30u", &val) == 1) {
  167. attr->MAX_FS = val;
  168. } else if (sscanf(attrib, "max-cpb=%30u", &val) == 1) {
  169. attr->MAX_CPB = val;
  170. } else if (sscanf(attrib, "max-dpb=%30u", &val) == 1) {
  171. attr->MAX_DPB = val;
  172. } else if (sscanf(attrib, "max-br=%30u", &val) == 1) {
  173. attr->MAX_BR = val;
  174. } else if (sscanf(attrib, "max-smbps=%30u", &val) == 1) {
  175. attr->MAX_SMBPS = val;
  176. } else if (sscanf(attrib, "max-fps=%30u", &val) == 1) {
  177. attr->MAX_FPS = val;
  178. } else if (sscanf(attrib, "redundant-pic-cap=%30u", &val) == 1) {
  179. attr->REDUNDANT_PIC_CAP = val;
  180. } else if (sscanf(attrib, "parameter-add=%30u", &val) == 1) {
  181. attr->PARAMETER_ADD = val;
  182. } else if (sscanf(attrib, "packetization-mode=%30u", &val) == 1) {
  183. attr->PACKETIZATION_MODE = val;
  184. } else if (sscanf(attrib, "sprop-interleaving-depth=%30u", &val) == 1) {
  185. attr->SPROP_INTERLEAVING_DEPTH = val;
  186. } else if (sscanf(attrib, "sprop-deint-buf-req=%30u", &val) == 1) {
  187. attr->SPROP_DEINT_BUF_REQ = val;
  188. } else if (sscanf(attrib, "deint-buf-cap=%30u", &val) == 1) {
  189. attr->DEINT_BUF_CAP = val;
  190. } else if (sscanf(attrib, "sprop-init-buf-time=%30u", &val) == 1) {
  191. attr->SPROP_INIT_BUF_TIME = val;
  192. } else if (sscanf(attrib, "sprop-max-don-diff=%30u", &val) == 1) {
  193. attr->SPROP_MAX_DON_DIFF = val;
  194. } else if (sscanf(attrib, "max-rcmd-nalu-size=%30u", &val) == 1) {
  195. attr->MAX_RCMD_NALU_SIZE = val;
  196. } else if (sscanf(attrib, "level-asymmetry-allowed=%30u", &val) == 1) {
  197. attr->LEVEL_ASYMMETRY_ALLOWED = val;
  198. }
  199. }
  200. return cloned;
  201. }
  202. #define APPEND_IF_NOT_H264_UNSET(field, str, name) do { \
  203. if (field != H264_ATTR_KEY_UNSET) { \
  204. if (added) { \
  205. ast_str_append(str, 0, ";"); \
  206. } else { \
  207. added = 1; \
  208. } \
  209. ast_str_append(str, 0, "%s=%u", name, field); \
  210. } \
  211. } while (0)
  212. #define APPEND_IF_NONZERO(field, str, name) do { \
  213. if (field) { \
  214. if (added) { \
  215. ast_str_append(str, 0, ";"); \
  216. } else { \
  217. added = 1; \
  218. } \
  219. ast_str_append(str, 0, "%s=%u", name, field); \
  220. } \
  221. } while (0)
  222. static void h264_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  223. {
  224. struct h264_attr *attr = ast_format_get_attribute_data(format);
  225. int added = 0;
  226. if (!attr) {
  227. return;
  228. }
  229. ast_str_append(str, 0, "a=fmtp:%u ", payload);
  230. APPEND_IF_NONZERO(attr->MAX_MBPS, str, "max-mbps");
  231. APPEND_IF_NONZERO(attr->MAX_FS, str, "max-fs");
  232. APPEND_IF_NONZERO(attr->MAX_CPB, str, "max-cpb");
  233. APPEND_IF_NONZERO(attr->MAX_DPB, str, "max-dpb");
  234. APPEND_IF_NONZERO(attr->MAX_BR, str, "max-br");
  235. APPEND_IF_NONZERO(attr->MAX_SMBPS, str, "max-smbps");
  236. APPEND_IF_NONZERO(attr->MAX_FPS, str, "max-fps");
  237. APPEND_IF_NONZERO(attr->SPROP_INTERLEAVING_DEPTH, str, "sprop-interleaving-depth");
  238. APPEND_IF_NONZERO(attr->SPROP_DEINT_BUF_REQ, str, "sprop-deint-buf-req");
  239. APPEND_IF_NONZERO(attr->DEINT_BUF_CAP, str, "deint-buf-cap");
  240. APPEND_IF_NONZERO(attr->SPROP_INIT_BUF_TIME, str, "sprop-init-buf-time");
  241. APPEND_IF_NONZERO(attr->SPROP_MAX_DON_DIFF, str, "sprop-max-don-diff");
  242. APPEND_IF_NONZERO(attr->MAX_RCMD_NALU_SIZE, str, "max-rcmd-nalu-size");
  243. APPEND_IF_NOT_H264_UNSET(attr->REDUNDANT_PIC_CAP, str, "redundant-pic-cap");
  244. APPEND_IF_NOT_H264_UNSET(attr->PARAMETER_ADD, str, "parameter-add");
  245. APPEND_IF_NOT_H264_UNSET(attr->PACKETIZATION_MODE, str, "packetization-mode");
  246. APPEND_IF_NOT_H264_UNSET(attr->LEVEL_ASYMMETRY_ALLOWED, str, "level-asymmetry-allowed");
  247. if (attr->PROFILE_IDC && attr->PROFILE_IOP && attr->LEVEL) {
  248. if (added) {
  249. ast_str_append(str, 0, ";");
  250. } else {
  251. added = 1;
  252. }
  253. ast_str_append(str, 0, "profile-level-id=%02X%02X%02X", attr->PROFILE_IDC, attr->PROFILE_IOP, attr->LEVEL);
  254. }
  255. if (!ast_strlen_zero(attr->SPS) && !ast_strlen_zero(attr->PPS)) {
  256. if (added) {
  257. ast_str_append(str, 0, ";");
  258. } else {
  259. added = 1;
  260. }
  261. ast_str_append(str, 0, ";sprop-parameter-sets=%s,%s", attr->SPS, attr->PPS);
  262. }
  263. if (!added) {
  264. ast_str_reset(*str);
  265. } else {
  266. ast_str_append(str, 0, "\r\n");
  267. }
  268. return;
  269. }
  270. static struct ast_format_interface h264_interface = {
  271. .format_destroy = h264_destroy,
  272. .format_clone = h264_clone,
  273. .format_cmp = h264_cmp,
  274. .format_get_joint = h264_getjoint,
  275. .format_parse_sdp_fmtp = h264_parse_sdp_fmtp,
  276. .format_generate_sdp_fmtp = h264_generate_sdp_fmtp,
  277. };
  278. static int unload_module(void)
  279. {
  280. return 0;
  281. }
  282. static int load_module(void)
  283. {
  284. if (ast_format_interface_register("h264", &h264_interface)) {
  285. return AST_MODULE_LOAD_DECLINE;
  286. }
  287. return AST_MODULE_LOAD_SUCCESS;
  288. }
  289. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "H.264 Format Attribute Module",
  290. .support_level = AST_MODULE_SUPPORT_CORE,
  291. .load = load_module,
  292. .unload = unload_module,
  293. .load_pri = AST_MODPRI_DEFAULT,
  294. );