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