Reduce memory consumption and add the H.264 and H.263 modules I shamefully neglected...
[asterisk/asterisk.git] / res / res_format_attr_h264.c
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
20 /*! \file
21  *
22  * \brief H.264 Format Attribute Module
23  *
24  * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
25  *
26  * This is a format attribute module for the H.264 codec.
27  * \ingroup applications
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/module.h"
39 #include "asterisk/format.h"
40
41 /*! \brief Value that indicates an attribute is actually unset */
42 #define H264_ATTR_KEY_UNSET UINT8_MAX
43
44 /*! \brief Maximum size for SPS / PPS values in sprop-parameter-sets attribute */
45 #define H264_MAX_SPS_PPS_SIZE 16
46
47 enum h264_attr_keys {
48         H264_ATTR_KEY_PROFILE_IDC,
49         H264_ATTR_KEY_PROFILE_IOP,
50         H264_ATTR_KEY_LEVEL,
51         H264_ATTR_KEY_MAX_MBPS,
52         H264_ATTR_KEY_MAX_FS,
53         H264_ATTR_KEY_MAX_CPB,
54         H264_ATTR_KEY_MAX_DPB,
55         H264_ATTR_KEY_MAX_BR,
56         H264_ATTR_KEY_REDUNDANT_PIC_CAP,
57         H264_ATTR_KEY_PARAMETER_ADD,
58         H264_ATTR_KEY_PACKETIZATION_MODE,
59         H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH,
60         H264_ATTR_KEY_SPROP_DEINT_BUF_REQ,
61         H264_ATTR_KEY_DEINT_BUF_CAP,
62         H264_ATTR_KEY_SPROP_INIT_BUF_TIME,
63         H264_ATTR_KEY_SPROP_MAX_DON_DIFF,
64         H264_ATTR_KEY_MAX_RCMD_NALU_SIZE,
65         H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED,
66         H264_ATTR_KEY_SPS_LEN,
67         H264_ATTR_KEY_PPS_LEN,
68         H264_ATTR_KEY_SPS,
69         H264_ATTR_KEY_PPS = H264_ATTR_KEY_SPS + H264_MAX_SPS_PPS_SIZE,
70         H264_ATTR_KEY_END = H264_ATTR_KEY_PPS + H264_MAX_SPS_PPS_SIZE,
71 };
72
73 static enum ast_format_cmp_res h264_format_attr_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
74 {
75         unsigned int idc1 = fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] ? fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] : 0x42;
76         unsigned int idc2 = fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC] ? fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC] : 0x42;
77
78         /* We are as permissive as possible to ensure the maximum number of calls succeed */
79         return (idc1 == idc2) ? AST_FORMAT_CMP_EQUAL : AST_FORMAT_CMP_NOT_EQUAL;
80 }
81
82 static int h264_format_attr_get_joint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
83 {
84         int i;
85
86         for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
87                 result->format_attr[i] = fattr1->format_attr[i] ? fattr1->format_attr[i] : fattr2->format_attr[i];
88         }
89
90         return 0;
91 }
92
93 static int h264_format_attr_sdp_parse(struct ast_format_attr *format_attr, const char *attributes)
94 {
95         char *attribs = ast_strdupa(attributes), *attrib;
96
97         format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = H264_ATTR_KEY_UNSET;
98         format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = H264_ATTR_KEY_UNSET;
99         format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = H264_ATTR_KEY_UNSET;
100         format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = H264_ATTR_KEY_UNSET;
101
102         while ((attrib = strsep(&attribs, ";"))) {
103                 unsigned int val;
104                 unsigned long int val2;
105                 char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
106
107                 if (sscanf(attrib, "profile-level-id=%lx", &val2) == 1) {
108                         format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] = ((val2 >> 16) & 0xFF);
109                         format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] = ((val2 >> 8) & 0xFF);
110                         format_attr->format_attr[H264_ATTR_KEY_LEVEL] = (val2 & 0xFF);
111                 } else if (sscanf(attrib, "sprop-parameter-sets=%[^','],%s", sps, pps) == 2) {
112                         unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
113                         int i;
114
115                         ast_base64decode(spsdecoded, sps, sizeof(spsdecoded));
116                         ast_base64decode(ppsdecoded, pps, sizeof(ppsdecoded));
117
118                         format_attr->format_attr[H264_ATTR_KEY_SPS_LEN] = 0;
119                         format_attr->format_attr[H264_ATTR_KEY_PPS_LEN] = 0;
120
121                         for (i = 0; i < H264_MAX_SPS_PPS_SIZE; i++) {
122                                 if (spsdecoded[i]) {
123                                         format_attr->format_attr[H264_ATTR_KEY_SPS + i] = spsdecoded[i];
124                                         format_attr->format_attr[H264_ATTR_KEY_SPS_LEN]++;
125                                 }
126                                 if (ppsdecoded[i]) {
127                                         format_attr->format_attr[H264_ATTR_KEY_PPS + i] = ppsdecoded[i];
128                                         format_attr->format_attr[H264_ATTR_KEY_PPS_LEN]++;
129                                 }
130                         }
131                 } else if (sscanf(attrib, "max-mbps=%30u", &val) == 1) {
132                         format_attr->format_attr[H264_ATTR_KEY_MAX_MBPS] = val;
133                 } else if (sscanf(attrib, "max-fs=%30u", &val) == 1) {
134                         format_attr->format_attr[H264_ATTR_KEY_MAX_FS] = val;
135                 } else if (sscanf(attrib, "max-cpb=%30u", &val) == 1) {
136                         format_attr->format_attr[H264_ATTR_KEY_MAX_CPB] = val;
137                 } else if (sscanf(attrib, "max-dpb=%30u", &val) == 1) {
138                         format_attr->format_attr[H264_ATTR_KEY_MAX_DPB] = val;
139                 } else if (sscanf(attrib, "max-br=%30u", &val) == 1) {
140                         format_attr->format_attr[H264_ATTR_KEY_MAX_BR] = val;
141                 } else if (sscanf(attrib, "redundant-pic-cap=%30u", &val) == 1) {
142                         format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = val;
143                 } else if (sscanf(attrib, "parameter-add=%30u", &val) == 1) {
144                         format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = val;
145                 } else if (sscanf(attrib, "packetization-mode=%30u", &val) == 1) {
146                         format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = val;
147                 } else if (sscanf(attrib, "sprop-interleaving-depth=%30u", &val) == 1) {
148                         format_attr->format_attr[H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH] = val;
149                 } else if (sscanf(attrib, "sprop-deint-buf-req=%30u", &val) == 1) {
150                         format_attr->format_attr[H264_ATTR_KEY_SPROP_DEINT_BUF_REQ] = val;
151                 } else if (sscanf(attrib, "deint-buf-cap=%30u", &val) == 1) {
152                         format_attr->format_attr[H264_ATTR_KEY_DEINT_BUF_CAP] = val;
153                 } else if (sscanf(attrib, "sprop-init-buf-time=%30u", &val) == 1) {
154                         format_attr->format_attr[H264_ATTR_KEY_SPROP_INIT_BUF_TIME] = val;
155                 } else if (sscanf(attrib, "sprop-max-don-diff=%30u", &val) == 1) {
156                         format_attr->format_attr[H264_ATTR_KEY_SPROP_MAX_DON_DIFF] = val;
157                 } else if (sscanf(attrib, "max-rcmd-nalu-size=%30u", &val) == 1) {
158                         format_attr->format_attr[H264_ATTR_KEY_MAX_RCMD_NALU_SIZE] = val;
159                 } else if (sscanf(attrib, "level-asymmetry-allowed=%30u", &val) == 1) {
160                         format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = val;
161                 }
162         }
163
164         return 0;
165 }
166
167 /*! \brief Helper function which converts a key enum into a string value for SDP */
168 static const char *h264_attr_key_to_str(enum h264_attr_keys key)
169 {
170         switch (key) {
171         case H264_ATTR_KEY_MAX_MBPS:
172                 return "max-mbps";
173         case H264_ATTR_KEY_MAX_FS:
174                 return "max-fs";
175         case H264_ATTR_KEY_MAX_CPB:
176                 return "max-cpb";
177         case H264_ATTR_KEY_MAX_DPB:
178                 return "max-dpb";
179         case H264_ATTR_KEY_MAX_BR:
180                 return "max-br";
181         case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
182                 return "redundant-pic-cap";
183         case H264_ATTR_KEY_PARAMETER_ADD:
184                 return "parameter-add";
185         case H264_ATTR_KEY_PACKETIZATION_MODE:
186                 return "packetization-mode";
187         case H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH:
188                 return "sprop-interleaving-depth";
189         case H264_ATTR_KEY_SPROP_DEINT_BUF_REQ:
190                 return "sprop-deint-buf-req";
191         case H264_ATTR_KEY_DEINT_BUF_CAP:
192                 return "deint-buf-cap";
193         case H264_ATTR_KEY_SPROP_INIT_BUF_TIME:
194                 return "sprop-init-buf-time";
195         case H264_ATTR_KEY_SPROP_MAX_DON_DIFF:
196                 return "sprop-max-don-diff";
197         case H264_ATTR_KEY_MAX_RCMD_NALU_SIZE:
198                 return "max-rcmd-nalu-size";
199         case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
200                 return "level-asymmetry-allowed";
201         default:
202                 return NULL;
203         }
204
205         return NULL;
206 }
207
208 /*! \brief Helper function which determines if the value of an attribute can be placed into the SDP */
209 static int h264_attr_key_addable(const struct ast_format_attr *format_attr, enum h264_attr_keys key)
210 {
211         switch (key) {
212         case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
213         case H264_ATTR_KEY_PARAMETER_ADD:
214         case H264_ATTR_KEY_PACKETIZATION_MODE:
215         case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
216                 return (format_attr->format_attr[key] != H264_ATTR_KEY_UNSET) ? 1 : 0;
217         default:
218                 return format_attr->format_attr[key] ? 1 : 0;
219         }
220
221         return 1;
222 }
223
224 static void h264_format_attr_sdp_generate(const struct ast_format_attr *format_attr, unsigned int payload, struct ast_str **str)
225 {
226         int i, added = 0;
227
228         for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
229                 const char *name;
230
231                 if (i == H264_ATTR_KEY_SPS && format_attr->format_attr[H264_ATTR_KEY_SPS] && format_attr->format_attr[H264_ATTR_KEY_PPS]) {
232                         unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
233                         int pos;
234                         char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
235
236                         for (pos = 0; pos < H264_MAX_SPS_PPS_SIZE; pos++) {
237                                 spsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_SPS + pos];
238                                 ppsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_PPS + pos];
239                         }
240
241                         ast_base64encode(sps, spsdecoded, format_attr->format_attr[H264_ATTR_KEY_SPS_LEN], H264_MAX_SPS_PPS_SIZE);
242                         ast_base64encode(pps, ppsdecoded, format_attr->format_attr[H264_ATTR_KEY_PPS_LEN], H264_MAX_SPS_PPS_SIZE);
243
244                         if (!added) {
245                                 ast_str_append(str, 0, "a=fmtp:%d sprop-parameter-sets=%s,%s", payload, sps, pps);
246                                 added = 1;
247                         } else {
248                                 ast_str_append(str, 0, ";sprop-parameter-sets=%s,%s", sps, pps);
249                         }
250                 } else if (i == H264_ATTR_KEY_PROFILE_IDC && format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] &&
251                     format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] && format_attr->format_attr[H264_ATTR_KEY_LEVEL]) {
252                         if (!added) {
253                                 ast_str_append(str, 0, "a=fmtp:%d profile-level-id=%X%X%X", payload, format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
254                                                format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
255                                 added = 1;
256                         } else {
257                                 ast_str_append(str, 0, ";profile-level-id=%X%X%X", format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
258                                                format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
259                         }
260                 } else if ((name = h264_attr_key_to_str(i)) && h264_attr_key_addable(format_attr, i)) {
261                         if (!added) {
262                                 ast_str_append(str, 0, "a=fmtp:%d %s=%u", payload, name, format_attr->format_attr[i]);
263                                 added = 1;
264                         } else {
265                                 ast_str_append(str, 0, ";%s=%u", name, format_attr->format_attr[i]);
266                         }
267                 }
268         }
269         
270         if (added) {
271                 ast_str_append(str, 0, "\r\n");
272         }
273
274         return;
275 }
276
277 static struct ast_format_attr_interface h264_format_attr_interface = {
278         .id = AST_FORMAT_H264,
279         .format_attr_cmp = h264_format_attr_cmp,
280         .format_attr_get_joint = h264_format_attr_get_joint,
281         .format_attr_sdp_parse = h264_format_attr_sdp_parse,
282         .format_attr_sdp_generate = h264_format_attr_sdp_generate,
283 };
284
285 static int unload_module(void)
286 {
287         ast_format_attr_unreg_interface(&h264_format_attr_interface);
288
289         return 0;
290 }
291
292 static int load_module(void)
293 {
294         if (ast_format_attr_reg_interface(&h264_format_attr_interface)) {
295                 return AST_MODULE_LOAD_DECLINE;
296         }
297
298         return AST_MODULE_LOAD_SUCCESS;
299 }
300
301 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "H.264 Format Attribute Module",
302         .load = load_module,
303         .unload = unload_module,
304         .load_pri = AST_MODPRI_DEFAULT,
305 );