security_events: Fix error caused by DTD validation error
[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  *   if you change this value then you must change H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT
46  *   as well. */
47 #define H264_MAX_SPS_PPS_SIZE 16
48 /*! \brief This is used when executing sscanf on buffers of H264_MAX_SPS_PPS_SIZE
49  *   length. It must ALWAYS be a string literal representation of one less than
50  *   H264_MAX_SPS_PPS_SIZE */
51 #define H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "15"
52
53 enum h264_attr_keys {
54         H264_ATTR_KEY_PROFILE_IDC,
55         H264_ATTR_KEY_PROFILE_IOP,
56         H264_ATTR_KEY_LEVEL,
57         H264_ATTR_KEY_MAX_MBPS,
58         H264_ATTR_KEY_MAX_FS,
59         H264_ATTR_KEY_MAX_CPB,
60         H264_ATTR_KEY_MAX_DPB,
61         H264_ATTR_KEY_MAX_BR,
62         H264_ATTR_KEY_MAX_SMBPS,
63         H264_ATTR_KEY_MAX_FPS,
64         H264_ATTR_KEY_REDUNDANT_PIC_CAP,
65         H264_ATTR_KEY_PARAMETER_ADD,
66         H264_ATTR_KEY_PACKETIZATION_MODE,
67         H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH,
68         H264_ATTR_KEY_SPROP_DEINT_BUF_REQ,
69         H264_ATTR_KEY_DEINT_BUF_CAP,
70         H264_ATTR_KEY_SPROP_INIT_BUF_TIME,
71         H264_ATTR_KEY_SPROP_MAX_DON_DIFF,
72         H264_ATTR_KEY_MAX_RCMD_NALU_SIZE,
73         H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED,
74         H264_ATTR_KEY_SPS_LEN,
75         H264_ATTR_KEY_PPS_LEN,
76         H264_ATTR_KEY_SPS,
77         H264_ATTR_KEY_PPS = H264_ATTR_KEY_SPS + H264_MAX_SPS_PPS_SIZE,
78         H264_ATTR_KEY_END = H264_ATTR_KEY_PPS + H264_MAX_SPS_PPS_SIZE,
79 };
80
81 static enum ast_format_cmp_res h264_format_attr_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
82 {
83         if (!fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] || !fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC] ||
84             (fattr1->format_attr[H264_ATTR_KEY_PROFILE_IDC] == fattr2->format_attr[H264_ATTR_KEY_PROFILE_IDC])) {
85                 return AST_FORMAT_CMP_EQUAL;
86         }
87
88         return AST_FORMAT_CMP_NOT_EQUAL;
89 }
90
91 static int h264_format_attr_get_joint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
92 {
93         int i;
94
95         for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
96                 result->format_attr[i] = fattr1->format_attr[i] ? fattr1->format_attr[i] : fattr2->format_attr[i];
97         }
98
99         return 0;
100 }
101
102 static int h264_format_attr_sdp_parse(struct ast_format_attr *format_attr, const char *attributes)
103 {
104         char *attribs = ast_strdupa(attributes), *attrib;
105
106         format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = H264_ATTR_KEY_UNSET;
107         format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = H264_ATTR_KEY_UNSET;
108         format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = H264_ATTR_KEY_UNSET;
109         format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = H264_ATTR_KEY_UNSET;
110
111         while ((attrib = strsep(&attribs, ";"))) {
112                 unsigned int val;
113                 unsigned long int val2;
114                 char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
115
116                 if (sscanf(attrib, "profile-level-id=%lx", &val2) == 1) {
117                         format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] = ((val2 >> 16) & 0xFF);
118                         format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] = ((val2 >> 8) & 0xFF);
119                         format_attr->format_attr[H264_ATTR_KEY_LEVEL] = (val2 & 0xFF);
120                 } else if (sscanf(attrib, "sprop-parameter-sets=%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "[^','],%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "s", sps, pps) == 2) {
121                         /* XXX sprop-parameter-sets can actually be of unlimited length. This may need to be addressed later. */
122                         unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
123                         int i;
124
125                         ast_base64decode(spsdecoded, sps, sizeof(spsdecoded));
126                         ast_base64decode(ppsdecoded, pps, sizeof(ppsdecoded));
127
128                         format_attr->format_attr[H264_ATTR_KEY_SPS_LEN] = 0;
129                         format_attr->format_attr[H264_ATTR_KEY_PPS_LEN] = 0;
130
131                         for (i = 0; i < H264_MAX_SPS_PPS_SIZE; i++) {
132                                 if (spsdecoded[i]) {
133                                         format_attr->format_attr[H264_ATTR_KEY_SPS + i] = spsdecoded[i];
134                                         format_attr->format_attr[H264_ATTR_KEY_SPS_LEN]++;
135                                 }
136                                 if (ppsdecoded[i]) {
137                                         format_attr->format_attr[H264_ATTR_KEY_PPS + i] = ppsdecoded[i];
138                                         format_attr->format_attr[H264_ATTR_KEY_PPS_LEN]++;
139                                 }
140                         }
141                 } else if (sscanf(attrib, "max-mbps=%30u", &val) == 1) {
142                         format_attr->format_attr[H264_ATTR_KEY_MAX_MBPS] = val;
143                 } else if (sscanf(attrib, "max-fs=%30u", &val) == 1) {
144                         format_attr->format_attr[H264_ATTR_KEY_MAX_FS] = val;
145                 } else if (sscanf(attrib, "max-cpb=%30u", &val) == 1) {
146                         format_attr->format_attr[H264_ATTR_KEY_MAX_CPB] = val;
147                 } else if (sscanf(attrib, "max-dpb=%30u", &val) == 1) {
148                         format_attr->format_attr[H264_ATTR_KEY_MAX_DPB] = val;
149                 } else if (sscanf(attrib, "max-br=%30u", &val) == 1) {
150                         format_attr->format_attr[H264_ATTR_KEY_MAX_BR] = val;
151                 } else if (sscanf(attrib, "max-smbps=%30u", &val) == 1) {
152                         format_attr->format_attr[H264_ATTR_KEY_MAX_SMBPS] = val;
153                 } else if (sscanf(attrib, "max-fps=%30u", &val) == 1) {
154                         format_attr->format_attr[H264_ATTR_KEY_MAX_FPS] = val;
155                 } else if (sscanf(attrib, "redundant-pic-cap=%30u", &val) == 1) {
156                         format_attr->format_attr[H264_ATTR_KEY_REDUNDANT_PIC_CAP] = val;
157                 } else if (sscanf(attrib, "parameter-add=%30u", &val) == 1) {
158                         format_attr->format_attr[H264_ATTR_KEY_PARAMETER_ADD] = val;
159                 } else if (sscanf(attrib, "packetization-mode=%30u", &val) == 1) {
160                         format_attr->format_attr[H264_ATTR_KEY_PACKETIZATION_MODE] = val;
161                 } else if (sscanf(attrib, "sprop-interleaving-depth=%30u", &val) == 1) {
162                         format_attr->format_attr[H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH] = val;
163                 } else if (sscanf(attrib, "sprop-deint-buf-req=%30u", &val) == 1) {
164                         format_attr->format_attr[H264_ATTR_KEY_SPROP_DEINT_BUF_REQ] = val;
165                 } else if (sscanf(attrib, "deint-buf-cap=%30u", &val) == 1) {
166                         format_attr->format_attr[H264_ATTR_KEY_DEINT_BUF_CAP] = val;
167                 } else if (sscanf(attrib, "sprop-init-buf-time=%30u", &val) == 1) {
168                         format_attr->format_attr[H264_ATTR_KEY_SPROP_INIT_BUF_TIME] = val;
169                 } else if (sscanf(attrib, "sprop-max-don-diff=%30u", &val) == 1) {
170                         format_attr->format_attr[H264_ATTR_KEY_SPROP_MAX_DON_DIFF] = val;
171                 } else if (sscanf(attrib, "max-rcmd-nalu-size=%30u", &val) == 1) {
172                         format_attr->format_attr[H264_ATTR_KEY_MAX_RCMD_NALU_SIZE] = val;
173                 } else if (sscanf(attrib, "level-asymmetry-allowed=%30u", &val) == 1) {
174                         format_attr->format_attr[H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED] = val;
175                 }
176         }
177
178         return 0;
179 }
180
181 /*! \brief Helper function which converts a key enum into a string value for SDP */
182 static const char *h264_attr_key_to_str(enum h264_attr_keys key)
183 {
184         switch (key) {
185         case H264_ATTR_KEY_MAX_MBPS:
186                 return "max-mbps";
187         case H264_ATTR_KEY_MAX_FS:
188                 return "max-fs";
189         case H264_ATTR_KEY_MAX_CPB:
190                 return "max-cpb";
191         case H264_ATTR_KEY_MAX_DPB:
192                 return "max-dpb";
193         case H264_ATTR_KEY_MAX_BR:
194                 return "max-br";
195         case H264_ATTR_KEY_MAX_SMBPS:
196                 return "max-smbps";
197         case H264_ATTR_KEY_MAX_FPS:
198                 return "max-fps";
199         case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
200                 return "redundant-pic-cap";
201         case H264_ATTR_KEY_PARAMETER_ADD:
202                 return "parameter-add";
203         case H264_ATTR_KEY_PACKETIZATION_MODE:
204                 return "packetization-mode";
205         case H264_ATTR_KEY_SPROP_INTERLEAVING_DEPTH:
206                 return "sprop-interleaving-depth";
207         case H264_ATTR_KEY_SPROP_DEINT_BUF_REQ:
208                 return "sprop-deint-buf-req";
209         case H264_ATTR_KEY_DEINT_BUF_CAP:
210                 return "deint-buf-cap";
211         case H264_ATTR_KEY_SPROP_INIT_BUF_TIME:
212                 return "sprop-init-buf-time";
213         case H264_ATTR_KEY_SPROP_MAX_DON_DIFF:
214                 return "sprop-max-don-diff";
215         case H264_ATTR_KEY_MAX_RCMD_NALU_SIZE:
216                 return "max-rcmd-nalu-size";
217         case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
218                 return "level-asymmetry-allowed";
219         default:
220                 return NULL;
221         }
222
223         return NULL;
224 }
225
226 /*! \brief Helper function which determines if the value of an attribute can be placed into the SDP */
227 static int h264_attr_key_addable(const struct ast_format_attr *format_attr, enum h264_attr_keys key)
228 {
229         switch (key) {
230         case H264_ATTR_KEY_REDUNDANT_PIC_CAP:
231         case H264_ATTR_KEY_PARAMETER_ADD:
232         case H264_ATTR_KEY_PACKETIZATION_MODE:
233         case H264_ATTR_KEY_LEVEL_ASYMMETRY_ALLOWED:
234                 return (format_attr->format_attr[key] != H264_ATTR_KEY_UNSET) ? 1 : 0;
235         default:
236                 return format_attr->format_attr[key] ? 1 : 0;
237         }
238
239         return 1;
240 }
241
242 static void h264_format_attr_sdp_generate(const struct ast_format_attr *format_attr, unsigned int payload, struct ast_str **str)
243 {
244         int i, added = 0;
245
246         for (i = H264_ATTR_KEY_PROFILE_IDC; i < H264_ATTR_KEY_END; i++) {
247                 const char *name;
248
249                 if (i == H264_ATTR_KEY_SPS && format_attr->format_attr[H264_ATTR_KEY_SPS] && format_attr->format_attr[H264_ATTR_KEY_PPS]) {
250                         unsigned char spsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, }, ppsdecoded[H264_MAX_SPS_PPS_SIZE] = { 0, };
251                         int pos;
252                         char sps[H264_MAX_SPS_PPS_SIZE], pps[H264_MAX_SPS_PPS_SIZE];
253
254                         for (pos = 0; pos < H264_MAX_SPS_PPS_SIZE; pos++) {
255                                 spsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_SPS + pos];
256                                 ppsdecoded[pos] = format_attr->format_attr[H264_ATTR_KEY_PPS + pos];
257                         }
258
259                         ast_base64encode(sps, spsdecoded, format_attr->format_attr[H264_ATTR_KEY_SPS_LEN], H264_MAX_SPS_PPS_SIZE);
260                         ast_base64encode(pps, ppsdecoded, format_attr->format_attr[H264_ATTR_KEY_PPS_LEN], H264_MAX_SPS_PPS_SIZE);
261
262                         if (!added) {
263                                 ast_str_append(str, 0, "a=fmtp:%d sprop-parameter-sets=%s,%s", payload, sps, pps);
264                                 added = 1;
265                         } else {
266                                 ast_str_append(str, 0, ";sprop-parameter-sets=%s,%s", sps, pps);
267                         }
268                 } else if (i == H264_ATTR_KEY_PROFILE_IDC && format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC] &&
269                     format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP] && format_attr->format_attr[H264_ATTR_KEY_LEVEL]) {
270                         if (!added) {
271                                 ast_str_append(str, 0, "a=fmtp:%d profile-level-id=%X%X%X", payload, format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
272                                                format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
273                                 added = 1;
274                         } else {
275                                 ast_str_append(str, 0, ";profile-level-id=%X%X%X", format_attr->format_attr[H264_ATTR_KEY_PROFILE_IDC],
276                                                format_attr->format_attr[H264_ATTR_KEY_PROFILE_IOP], format_attr->format_attr[H264_ATTR_KEY_LEVEL]);
277                         }
278                 } else if ((name = h264_attr_key_to_str(i)) && h264_attr_key_addable(format_attr, i)) {
279                         if (!added) {
280                                 ast_str_append(str, 0, "a=fmtp:%d %s=%u", payload, name, format_attr->format_attr[i]);
281                                 added = 1;
282                         } else {
283                                 ast_str_append(str, 0, ";%s=%u", name, format_attr->format_attr[i]);
284                         }
285                 }
286         }
287         
288         if (added) {
289                 ast_str_append(str, 0, "\r\n");
290         }
291
292         return;
293 }
294
295 static struct ast_format_attr_interface h264_format_attr_interface = {
296         .id = AST_FORMAT_H264,
297         .format_attr_cmp = h264_format_attr_cmp,
298         .format_attr_get_joint = h264_format_attr_get_joint,
299         .format_attr_sdp_parse = h264_format_attr_sdp_parse,
300         .format_attr_sdp_generate = h264_format_attr_sdp_generate,
301 };
302
303 static int unload_module(void)
304 {
305         ast_format_attr_unreg_interface(&h264_format_attr_interface);
306
307         return 0;
308 }
309
310 static int load_module(void)
311 {
312         if (ast_format_attr_reg_interface(&h264_format_attr_interface)) {
313                 return AST_MODULE_LOAD_DECLINE;
314         }
315
316         return AST_MODULE_LOAD_SUCCESS;
317 }
318
319 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "H.264 Format Attribute Module",
320         .load = load_module,
321         .unload = unload_module,
322         .load_pri = AST_MODPRI_DEFAULT,
323 );