res_format_attr_h263.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.263 Format Attribute Module
  22. *
  23. * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
  24. *
  25. * This is a format attribute module for the H.263 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. struct h263_attr {
  36. unsigned int SQCIF; /*!< Minimum picture interval for SQCIF resolution */
  37. unsigned int QCIF; /*!< Minimum picture interval for QCIF resolution */
  38. unsigned int CIF; /*!< Minimum picture interval for CIF resolution */
  39. unsigned int CIF4; /*!< Minimum picture interval for CIF4 resolution */
  40. unsigned int CIF16; /*!< Minimum picture interval for CIF16 resolution */
  41. unsigned int VGA; /*!< Minimum picture interval for VGA resolution */
  42. unsigned int CUSTOM_XMAX; /*!< Custom resolution (Xmax) */
  43. unsigned int CUSTOM_YMAX; /*!< Custom resolution (Ymax) */
  44. unsigned int CUSTOM_MPI; /*!< Custom resolution (MPI) */
  45. unsigned int F; /*!< F annex support */
  46. unsigned int I; /*!< I annex support */
  47. unsigned int J; /*!< J annex support */
  48. unsigned int T; /*!< T annex support */
  49. unsigned int K; /*!< K annex support */
  50. unsigned int N; /*!< N annex support */
  51. unsigned int P_SUB1; /*!< Reference picture resampling (sub mode 1) */
  52. unsigned int P_SUB2; /*!< Reference picture resampling (sub mode 2) */
  53. unsigned int P_SUB3; /*!< Reference picture resampling (sub mode 3) */
  54. unsigned int P_SUB4; /*!< Reference picture resampling (sub mode 4) */
  55. unsigned int PAR_WIDTH; /*!< Pixel aspect ratio (width) */
  56. unsigned int PAR_HEIGHT; /*!< Pixel aspect ratio (height) */
  57. unsigned int BPP; /*!< Bits per picture maximum */
  58. unsigned int HRD; /*!< Hypothetical reference decoder status */
  59. };
  60. static void h263_destroy(struct ast_format *format)
  61. {
  62. struct h263_attr *attr = ast_format_get_attribute_data(format);
  63. ast_free(attr);
  64. }
  65. static int h263_clone(const struct ast_format *src, struct ast_format *dst)
  66. {
  67. struct h263_attr *original = ast_format_get_attribute_data(src);
  68. struct h263_attr *attr = ast_calloc(1, sizeof(*attr));
  69. if (!attr) {
  70. return -1;
  71. }
  72. if (original) {
  73. *attr = *original;
  74. }
  75. ast_format_set_attribute_data(dst, attr);
  76. return 0;
  77. }
  78. static enum ast_format_cmp_res h263_cmp(const struct ast_format *format1, const struct ast_format *format2)
  79. {
  80. struct h263_attr *attr1 = ast_format_get_attribute_data(format1);
  81. struct h263_attr *attr2 = ast_format_get_attribute_data(format2);
  82. if (!attr1 || !attr2 || (attr1 && attr2 && !memcmp(attr1, attr2, sizeof(*attr1)))) {
  83. return AST_FORMAT_CMP_EQUAL;
  84. }
  85. return AST_FORMAT_CMP_NOT_EQUAL;
  86. }
  87. #define DETERMINE_JOINT(joint, attr1, attr2, field) (joint->field = (attr1 && attr1->field) ? attr1->field : (attr2 && attr2->field) ? attr2->field : 0)
  88. static struct ast_format *h263_getjoint(const struct ast_format *format1, const struct ast_format *format2)
  89. {
  90. struct ast_format *cloned;
  91. struct h263_attr *attr, *attr1, *attr2;
  92. cloned = ast_format_clone(format1);
  93. if (!cloned) {
  94. return NULL;
  95. }
  96. attr = ast_format_get_attribute_data(cloned);
  97. attr1 = ast_format_get_attribute_data(format1);
  98. attr2 = ast_format_get_attribute_data(format2);
  99. DETERMINE_JOINT(attr, attr1, attr2, SQCIF);
  100. DETERMINE_JOINT(attr, attr1, attr2, QCIF);
  101. DETERMINE_JOINT(attr, attr1, attr2, CIF);
  102. DETERMINE_JOINT(attr, attr1, attr2, CIF4);
  103. DETERMINE_JOINT(attr, attr1, attr2, CIF16);
  104. DETERMINE_JOINT(attr, attr1, attr2, VGA);
  105. DETERMINE_JOINT(attr, attr1, attr2, CUSTOM_XMAX);
  106. DETERMINE_JOINT(attr, attr1, attr2, CUSTOM_YMAX);
  107. DETERMINE_JOINT(attr, attr1, attr2, CUSTOM_MPI);
  108. DETERMINE_JOINT(attr, attr1, attr2, F);
  109. DETERMINE_JOINT(attr, attr1, attr2, I);
  110. DETERMINE_JOINT(attr, attr1, attr2, J);
  111. DETERMINE_JOINT(attr, attr1, attr2, T);
  112. DETERMINE_JOINT(attr, attr1, attr2, K);
  113. DETERMINE_JOINT(attr, attr1, attr2, N);
  114. DETERMINE_JOINT(attr, attr1, attr2, P_SUB1);
  115. DETERMINE_JOINT(attr, attr1, attr2, P_SUB2);
  116. DETERMINE_JOINT(attr, attr1, attr2, P_SUB3);
  117. DETERMINE_JOINT(attr, attr1, attr2, P_SUB4);
  118. DETERMINE_JOINT(attr, attr1, attr2, PAR_WIDTH);
  119. DETERMINE_JOINT(attr, attr1, attr2, PAR_HEIGHT);
  120. DETERMINE_JOINT(attr, attr1, attr2, BPP);
  121. DETERMINE_JOINT(attr, attr1, attr2, HRD);
  122. return cloned;
  123. }
  124. static struct ast_format *h263_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  125. {
  126. char *attribs = ast_strdupa(attributes), *attrib;
  127. struct ast_format *cloned;
  128. struct h263_attr *attr;
  129. cloned = ast_format_clone(format);
  130. if (!cloned) {
  131. return NULL;
  132. }
  133. attr = ast_format_get_attribute_data(cloned);
  134. while ((attrib = strsep(&attribs, ";"))) {
  135. unsigned int val, val2 = 0, val3 = 0, val4 = 0;
  136. if (sscanf(attrib, "SQCIF=%30u", &val) == 1) {
  137. attr->SQCIF = val;
  138. } else if (sscanf(attrib, "QCIF=%30u", &val) == 1) {
  139. attr->QCIF = val;
  140. } else if (sscanf(attrib, "CIF=%30u", &val) == 1) {
  141. attr->CIF = val;
  142. } else if (sscanf(attrib, "CIF4=%30u", &val) == 1) {
  143. attr->CIF4 = val;
  144. } else if (sscanf(attrib, "CIF16=%30u", &val) == 1) {
  145. attr->CIF16 = val;
  146. } else if (sscanf(attrib, "VGA=%30u", &val) == 1) {
  147. attr->VGA = val;
  148. } else if (sscanf(attrib, "CUSTOM=%30u,%30u,%30u", &val, &val2, &val3) == 3) {
  149. attr->CUSTOM_XMAX = val;
  150. attr->CUSTOM_YMAX = val2;
  151. attr->CUSTOM_MPI = val3;
  152. } else if (sscanf(attrib, "F=%30u", &val) == 1) {
  153. attr->F = val;
  154. } else if (sscanf(attrib, "I=%30u", &val) == 1) {
  155. attr->I = val;
  156. } else if (sscanf(attrib, "J=%30u", &val) == 1) {
  157. attr->J = val;
  158. } else if (sscanf(attrib, "T=%30u", &val) == 1) {
  159. attr->T = val;
  160. } else if (sscanf(attrib, "K=%30u", &val) == 1) {
  161. attr->K = val;
  162. } else if (sscanf(attrib, "N=%30u", &val) == 1) {
  163. attr->N = val;
  164. } else if (sscanf(attrib, "PAR=%30u:%30u", &val, &val2) == 2) {
  165. attr->PAR_WIDTH = val;
  166. attr->PAR_HEIGHT = val2;
  167. } else if (sscanf(attrib, "BPP=%30u", &val) == 1) {
  168. attr->BPP = val;
  169. } else if (sscanf(attrib, "HRD=%30u", &val) == 1) {
  170. attr->HRD = val;
  171. } else if (sscanf(attrib, "P=%30u,%30u,%30u,%30u", &val, &val2, &val3, &val4) > 0) {
  172. attr->P_SUB1 = val;
  173. attr->P_SUB2 = val2;
  174. attr->P_SUB3 = val3;
  175. attr->P_SUB4 = val4;
  176. }
  177. }
  178. return cloned;
  179. }
  180. static void h263_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  181. {
  182. struct h263_attr *attr = ast_format_get_attribute_data(format);
  183. if (!attr) {
  184. return;
  185. }
  186. ast_str_append(str, 0, "a=fmtp:%u SQCIF=%u;QCIF=%u;CIF=%u;CIF4=%u;CIF16=%u;VGA=%u;F=%u;I=%u;J=%u;T=%u;K=%u;N=%u;BPP=%u;HRD=%u",
  187. payload, attr->SQCIF, attr->QCIF, attr->CIF, attr->CIF4, attr->CIF16, attr->VGA, attr->F, attr->I, attr->J,
  188. attr->T, attr->K, attr->N, attr->BPP, attr->HRD);
  189. if (attr->CUSTOM_XMAX && attr->CUSTOM_YMAX && attr->CUSTOM_MPI) {
  190. ast_str_append(str, 0, ";CUSTOM=%u,%u,%u", attr->CUSTOM_XMAX, attr->CUSTOM_YMAX, attr->CUSTOM_MPI);
  191. }
  192. if (attr->PAR_WIDTH && attr->PAR_HEIGHT) {
  193. ast_str_append(str, 0, ";PAR=%u:%u", attr->PAR_WIDTH, attr->PAR_HEIGHT);
  194. }
  195. if (attr->P_SUB1) {
  196. ast_str_append(str, 0, ";P=%u", attr->P_SUB1);
  197. if (attr->P_SUB2) {
  198. ast_str_append(str, 0, ",%u", attr->P_SUB2);
  199. }
  200. if (attr->P_SUB3) {
  201. ast_str_append(str, 0, ",%u", attr->P_SUB3);
  202. }
  203. if (attr->P_SUB4) {
  204. ast_str_append(str, 0, ",%u", attr->P_SUB4);
  205. }
  206. }
  207. ast_str_append(str, 0, "\r\n");
  208. return;
  209. }
  210. static struct ast_format_interface h263_interface = {
  211. .format_destroy = h263_destroy,
  212. .format_clone = h263_clone,
  213. .format_cmp = h263_cmp,
  214. .format_get_joint = h263_getjoint,
  215. .format_parse_sdp_fmtp = h263_parse_sdp_fmtp,
  216. .format_generate_sdp_fmtp = h263_generate_sdp_fmtp,
  217. };
  218. static int unload_module(void)
  219. {
  220. return 0;
  221. }
  222. static int load_module(void)
  223. {
  224. if (ast_format_interface_register("h263", &h263_interface)) {
  225. return AST_MODULE_LOAD_DECLINE;
  226. }
  227. if (ast_format_interface_register("h263p", &h263_interface)) {
  228. return AST_MODULE_LOAD_DECLINE;
  229. }
  230. return AST_MODULE_LOAD_SUCCESS;
  231. }
  232. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "H.263 Format Attribute Module",
  233. .support_level = AST_MODULE_SUPPORT_CORE,
  234. .load = load_module,
  235. .unload = unload_module,
  236. .load_pri = AST_MODPRI_DEFAULT,
  237. );