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. attrib = ast_strip(attrib);
  137. if (sscanf(attrib, "SQCIF=%30u", &val) == 1) {
  138. attr->SQCIF = val;
  139. } else if (sscanf(attrib, "QCIF=%30u", &val) == 1) {
  140. attr->QCIF = val;
  141. } else if (sscanf(attrib, "CIF=%30u", &val) == 1) {
  142. attr->CIF = val;
  143. } else if (sscanf(attrib, "CIF4=%30u", &val) == 1) {
  144. attr->CIF4 = val;
  145. } else if (sscanf(attrib, "CIF16=%30u", &val) == 1) {
  146. attr->CIF16 = val;
  147. } else if (sscanf(attrib, "VGA=%30u", &val) == 1) {
  148. attr->VGA = val;
  149. } else if (sscanf(attrib, "CUSTOM=%30u,%30u,%30u", &val, &val2, &val3) == 3) {
  150. attr->CUSTOM_XMAX = val;
  151. attr->CUSTOM_YMAX = val2;
  152. attr->CUSTOM_MPI = val3;
  153. } else if (sscanf(attrib, "F=%30u", &val) == 1) {
  154. attr->F = val;
  155. } else if (sscanf(attrib, "I=%30u", &val) == 1) {
  156. attr->I = val;
  157. } else if (sscanf(attrib, "J=%30u", &val) == 1) {
  158. attr->J = val;
  159. } else if (sscanf(attrib, "T=%30u", &val) == 1) {
  160. attr->T = val;
  161. } else if (sscanf(attrib, "K=%30u", &val) == 1) {
  162. attr->K = val;
  163. } else if (sscanf(attrib, "N=%30u", &val) == 1) {
  164. attr->N = val;
  165. } else if (sscanf(attrib, "PAR=%30u:%30u", &val, &val2) == 2) {
  166. attr->PAR_WIDTH = val;
  167. attr->PAR_HEIGHT = val2;
  168. } else if (sscanf(attrib, "BPP=%30u", &val) == 1) {
  169. attr->BPP = val;
  170. } else if (sscanf(attrib, "HRD=%30u", &val) == 1) {
  171. attr->HRD = val;
  172. } else if (sscanf(attrib, "P=%30u,%30u,%30u,%30u", &val, &val2, &val3, &val4) > 0) {
  173. attr->P_SUB1 = val;
  174. attr->P_SUB2 = val2;
  175. attr->P_SUB3 = val3;
  176. attr->P_SUB4 = val4;
  177. }
  178. }
  179. return cloned;
  180. }
  181. static void h263_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  182. {
  183. struct h263_attr *attr = ast_format_get_attribute_data(format);
  184. if (!attr) {
  185. return;
  186. }
  187. 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",
  188. payload, attr->SQCIF, attr->QCIF, attr->CIF, attr->CIF4, attr->CIF16, attr->VGA, attr->F, attr->I, attr->J,
  189. attr->T, attr->K, attr->N, attr->BPP, attr->HRD);
  190. if (attr->CUSTOM_XMAX && attr->CUSTOM_YMAX && attr->CUSTOM_MPI) {
  191. ast_str_append(str, 0, ";CUSTOM=%u,%u,%u", attr->CUSTOM_XMAX, attr->CUSTOM_YMAX, attr->CUSTOM_MPI);
  192. }
  193. if (attr->PAR_WIDTH && attr->PAR_HEIGHT) {
  194. ast_str_append(str, 0, ";PAR=%u:%u", attr->PAR_WIDTH, attr->PAR_HEIGHT);
  195. }
  196. if (attr->P_SUB1) {
  197. ast_str_append(str, 0, ";P=%u", attr->P_SUB1);
  198. if (attr->P_SUB2) {
  199. ast_str_append(str, 0, ",%u", attr->P_SUB2);
  200. }
  201. if (attr->P_SUB3) {
  202. ast_str_append(str, 0, ",%u", attr->P_SUB3);
  203. }
  204. if (attr->P_SUB4) {
  205. ast_str_append(str, 0, ",%u", attr->P_SUB4);
  206. }
  207. }
  208. ast_str_append(str, 0, "\r\n");
  209. return;
  210. }
  211. static struct ast_format_interface h263_interface = {
  212. .format_destroy = h263_destroy,
  213. .format_clone = h263_clone,
  214. .format_cmp = h263_cmp,
  215. .format_get_joint = h263_getjoint,
  216. .format_parse_sdp_fmtp = h263_parse_sdp_fmtp,
  217. .format_generate_sdp_fmtp = h263_generate_sdp_fmtp,
  218. };
  219. static int unload_module(void)
  220. {
  221. return 0;
  222. }
  223. static int load_module(void)
  224. {
  225. if (ast_format_interface_register("h263", &h263_interface)) {
  226. return AST_MODULE_LOAD_DECLINE;
  227. }
  228. if (ast_format_interface_register("h263p", &h263_interface)) {
  229. return AST_MODULE_LOAD_DECLINE;
  230. }
  231. return AST_MODULE_LOAD_SUCCESS;
  232. }
  233. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "H.263 Format Attribute Module",
  234. .support_level = AST_MODULE_SUPPORT_CORE,
  235. .load = load_module,
  236. .unload = unload_module,
  237. );