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