Merge "res_pjsip/res_pjsip_callerid: NULL check on caller id name string"
[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 #include "asterisk/module.h"
37 #include "asterisk/format.h"
38
39 /*! \brief Value that indicates an attribute is actually unset */
40 #define H264_ATTR_KEY_UNSET UINT8_MAX
41
42 /*! \brief Maximum size for SPS / PPS values in sprop-parameter-sets attribute
43  *   if you change this value then you must change H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT
44  *   as well. */
45 #define H264_MAX_SPS_PPS_SIZE 16
46 /*! \brief This is used when executing sscanf on buffers of H264_MAX_SPS_PPS_SIZE
47  *   length. It must ALWAYS be a string literal representation of one less than
48  *   H264_MAX_SPS_PPS_SIZE */
49 #define H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "15"
50
51 struct h264_attr {
52         unsigned int PROFILE_IDC;
53         unsigned int PROFILE_IOP;
54         unsigned int LEVEL;
55         unsigned int MAX_MBPS;
56         unsigned int MAX_FS;
57         unsigned int MAX_CPB;
58         unsigned int MAX_DPB;
59         unsigned int MAX_BR;
60         unsigned int MAX_SMBPS;
61         unsigned int MAX_FPS;
62         unsigned int REDUNDANT_PIC_CAP;
63         unsigned int PARAMETER_ADD;
64         unsigned int PACKETIZATION_MODE;
65         unsigned int SPROP_INTERLEAVING_DEPTH;
66         unsigned int SPROP_DEINT_BUF_REQ;
67         unsigned int DEINT_BUF_CAP;
68         unsigned int SPROP_INIT_BUF_TIME;
69         unsigned int SPROP_MAX_DON_DIFF;
70         unsigned int MAX_RCMD_NALU_SIZE;
71         unsigned int LEVEL_ASYMMETRY_ALLOWED;
72         char SPS[H264_MAX_SPS_PPS_SIZE];
73         char PPS[H264_MAX_SPS_PPS_SIZE];
74 };
75
76 static void h264_destroy(struct ast_format *format)
77 {
78         struct h264_attr *attr = ast_format_get_attribute_data(format);
79
80         ast_free(attr);
81 }
82
83 static int h264_clone(const struct ast_format *src, struct ast_format *dst)
84 {
85         struct h264_attr *original = ast_format_get_attribute_data(src);
86         struct h264_attr *attr = ast_calloc(1, sizeof(*attr));
87
88         if (!attr) {
89                 return -1;
90         }
91
92         if (original) {
93                 *attr = *original;
94         }
95
96         ast_format_set_attribute_data(dst, attr);
97
98         return 0;
99 }
100
101 static enum ast_format_cmp_res h264_cmp(const struct ast_format *format1, const struct ast_format *format2)
102 {
103         struct h264_attr *attr1 = ast_format_get_attribute_data(format1);
104         struct h264_attr *attr2 = ast_format_get_attribute_data(format2);
105
106         if (!attr1 || !attr1->PROFILE_IDC || !attr2 || !attr2->PROFILE_IDC ||
107                 (attr1->PROFILE_IDC == attr2->PROFILE_IDC)) {
108                 return AST_FORMAT_CMP_EQUAL;
109         }
110
111         return AST_FORMAT_CMP_NOT_EQUAL;
112 }
113
114 #define DETERMINE_JOINT(joint, attr1, attr2, field) (joint->field = (attr1 && attr1->field) ? attr1->field : (attr2 && attr2->field) ? attr2->field : 0)
115
116 static struct ast_format *h264_getjoint(const struct ast_format *format1, const struct ast_format *format2)
117 {
118         struct ast_format *cloned;
119         struct h264_attr *attr, *attr1, *attr2;
120
121         cloned = ast_format_clone(format1);
122         if (!cloned) {
123                 return NULL;
124         }
125         attr = ast_format_get_attribute_data(cloned);
126
127         attr1 = ast_format_get_attribute_data(format1);
128         attr2 = ast_format_get_attribute_data(format2);
129
130         DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IDC);
131         DETERMINE_JOINT(attr, attr1, attr2, PROFILE_IOP);
132         DETERMINE_JOINT(attr, attr1, attr2, LEVEL);
133         DETERMINE_JOINT(attr, attr1, attr2, MAX_MBPS);
134         DETERMINE_JOINT(attr, attr1, attr2, MAX_FS);
135         DETERMINE_JOINT(attr, attr1, attr2, MAX_CPB);
136         DETERMINE_JOINT(attr, attr1, attr2, MAX_DPB);
137         DETERMINE_JOINT(attr, attr1, attr2, MAX_BR);
138         DETERMINE_JOINT(attr, attr1, attr2, MAX_SMBPS);
139         DETERMINE_JOINT(attr, attr1, attr2, MAX_FPS);
140         DETERMINE_JOINT(attr, attr1, attr2, REDUNDANT_PIC_CAP);
141         DETERMINE_JOINT(attr, attr1, attr2, PARAMETER_ADD);
142         DETERMINE_JOINT(attr, attr1, attr2, SPROP_INTERLEAVING_DEPTH);
143         DETERMINE_JOINT(attr, attr1, attr2, SPROP_DEINT_BUF_REQ);
144         DETERMINE_JOINT(attr, attr1, attr2, DEINT_BUF_CAP);
145         DETERMINE_JOINT(attr, attr1, attr2, SPROP_INIT_BUF_TIME);
146         DETERMINE_JOINT(attr, attr1, attr2, SPROP_MAX_DON_DIFF);
147         DETERMINE_JOINT(attr, attr1, attr2, MAX_RCMD_NALU_SIZE);
148         DETERMINE_JOINT(attr, attr1, attr2, LEVEL_ASYMMETRY_ALLOWED);
149         DETERMINE_JOINT(attr, attr1, attr2, PACKETIZATION_MODE);
150
151         if (attr1 && !ast_strlen_zero(attr1->SPS)) {
152                 ast_copy_string(attr->SPS, attr1->SPS, sizeof(attr->SPS));
153         } else if (attr2 && !ast_strlen_zero(attr2->SPS)) {
154                 ast_copy_string(attr->SPS, attr2->SPS, sizeof(attr->SPS));
155         }
156
157         if (attr1 && !ast_strlen_zero(attr1->PPS)) {
158                 ast_copy_string(attr->PPS, attr1->PPS, sizeof(attr->PPS));
159         } else if (attr2 && !ast_strlen_zero(attr2->PPS)) {
160                 ast_copy_string(attr->PPS, attr2->PPS, sizeof(attr->PPS));
161         }
162
163         return cloned;
164 }
165
166 static struct ast_format *h264_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
167 {
168         char *attribs = ast_strdupa(attributes), *attrib;
169         struct ast_format *cloned;
170         struct h264_attr *attr;
171
172         cloned = ast_format_clone(format);
173         if (!cloned) {
174                 return NULL;
175         }
176         attr = ast_format_get_attribute_data(cloned);
177
178         attr->REDUNDANT_PIC_CAP = H264_ATTR_KEY_UNSET;
179         attr->PARAMETER_ADD = H264_ATTR_KEY_UNSET;
180         attr->PACKETIZATION_MODE = H264_ATTR_KEY_UNSET;
181         attr->LEVEL_ASYMMETRY_ALLOWED = H264_ATTR_KEY_UNSET;
182
183         while ((attrib = strsep(&attribs, ";"))) {
184                 unsigned int val;
185                 unsigned long int val2;
186
187                 if (sscanf(attrib, "profile-level-id=%lx", &val2) == 1) {
188                         attr->PROFILE_IDC = ((val2 >> 16) & 0xFF);
189                         attr->PROFILE_IOP = ((val2 >> 8) & 0xFF);
190                         attr->LEVEL = (val2 & 0xFF);
191                 } else if (sscanf(attrib, "sprop-parameter-sets=%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "[^','],%" H264_MAX_SPS_PPS_SIZE_SCAN_LIMIT "s", attr->SPS, attr->PPS) == 2) {
192                         /* XXX sprop-parameter-sets can actually be of unlimited length. This may need to be addressed later. */
193                 } else if (sscanf(attrib, "max-mbps=%30u", &val) == 1) {
194                         attr->MAX_MBPS = val;
195                 } else if (sscanf(attrib, "max-fs=%30u", &val) == 1) {
196                         attr->MAX_FS = val;
197                 } else if (sscanf(attrib, "max-cpb=%30u", &val) == 1) {
198                         attr->MAX_CPB = val;
199                 } else if (sscanf(attrib, "max-dpb=%30u", &val) == 1) {
200                         attr->MAX_DPB = val;
201                 } else if (sscanf(attrib, "max-br=%30u", &val) == 1) {
202                         attr->MAX_BR = val;
203                 } else if (sscanf(attrib, "max-smbps=%30u", &val) == 1) {
204                         attr->MAX_SMBPS = val;
205                 } else if (sscanf(attrib, "max-fps=%30u", &val) == 1) {
206                         attr->MAX_FPS = val;
207                 } else if (sscanf(attrib, "redundant-pic-cap=%30u", &val) == 1) {
208                         attr->REDUNDANT_PIC_CAP = val;
209                 } else if (sscanf(attrib, "parameter-add=%30u", &val) == 1) {
210                         attr->PARAMETER_ADD = val;
211                 } else if (sscanf(attrib, "packetization-mode=%30u", &val) == 1) {
212                         attr->PACKETIZATION_MODE = val;
213                 } else if (sscanf(attrib, "sprop-interleaving-depth=%30u", &val) == 1) {
214                         attr->SPROP_INTERLEAVING_DEPTH = val;
215                 } else if (sscanf(attrib, "sprop-deint-buf-req=%30u", &val) == 1) {
216                         attr->SPROP_DEINT_BUF_REQ = val;
217                 } else if (sscanf(attrib, "deint-buf-cap=%30u", &val) == 1) {
218                         attr->DEINT_BUF_CAP = val;
219                 } else if (sscanf(attrib, "sprop-init-buf-time=%30u", &val) == 1) {
220                         attr->SPROP_INIT_BUF_TIME = val;
221                 } else if (sscanf(attrib, "sprop-max-don-diff=%30u", &val) == 1) {
222                         attr->SPROP_MAX_DON_DIFF = val;
223                 } else if (sscanf(attrib, "max-rcmd-nalu-size=%30u", &val) == 1) {
224                         attr->MAX_RCMD_NALU_SIZE = val;
225                 } else if (sscanf(attrib, "level-asymmetry-allowed=%30u", &val) == 1) {
226                         attr->LEVEL_ASYMMETRY_ALLOWED = val;
227                 }
228         }
229
230         return cloned;
231 }
232
233 #define APPEND_IF_NOT_H264_UNSET(field, str, name) do {         \
234         if (field != H264_ATTR_KEY_UNSET) {     \
235                 if (added) {    \
236                         ast_str_append(str, 0, ";");    \
237                 } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) { \
238                         added = 1;      \
239                 }       \
240                 ast_str_append(str, 0, "%s=%u", name, field);   \
241         }       \
242 } while (0)
243
244 #define APPEND_IF_NONZERO(field, str, name) do {                \
245         if (field) {    \
246                 if (added) {    \
247                         ast_str_append(str, 0, ";");    \
248                 } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) { \
249                         added = 1;      \
250                 }       \
251                 ast_str_append(str, 0, "%s=%u", name, field);   \
252         }       \
253 } while (0)
254
255 static void h264_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
256 {
257         struct h264_attr *attr = ast_format_get_attribute_data(format);
258         int added = 0;
259
260         if (!attr) {
261                 return;
262         }
263
264         APPEND_IF_NONZERO(attr->MAX_MBPS, str, "max-mbps");
265         APPEND_IF_NONZERO(attr->MAX_FS, str, "max-fs");
266         APPEND_IF_NONZERO(attr->MAX_CPB, str, "max-cpb");
267         APPEND_IF_NONZERO(attr->MAX_DPB, str, "max-dpb");
268         APPEND_IF_NONZERO(attr->MAX_BR, str, "max-br");
269         APPEND_IF_NONZERO(attr->MAX_SMBPS, str, "max-smbps");
270         APPEND_IF_NONZERO(attr->MAX_FPS, str, "max-fps");
271         APPEND_IF_NONZERO(attr->SPROP_INTERLEAVING_DEPTH, str, "sprop-interleaving-depth");
272         APPEND_IF_NONZERO(attr->SPROP_DEINT_BUF_REQ, str, "sprop-deint-buf-req");
273         APPEND_IF_NONZERO(attr->DEINT_BUF_CAP, str, "deint-buf-cap");
274         APPEND_IF_NONZERO(attr->SPROP_INIT_BUF_TIME, str, "sprop-init-buf-time");
275         APPEND_IF_NONZERO(attr->SPROP_MAX_DON_DIFF, str, "sprop-max-don-diff");
276         APPEND_IF_NONZERO(attr->MAX_RCMD_NALU_SIZE, str, "max-rcmd-nalu-size");
277
278         APPEND_IF_NOT_H264_UNSET(attr->REDUNDANT_PIC_CAP, str, "redundant-pic-cap");
279         APPEND_IF_NOT_H264_UNSET(attr->PARAMETER_ADD, str, "parameter-add");
280         APPEND_IF_NOT_H264_UNSET(attr->PACKETIZATION_MODE, str, "packetization-mode");
281         APPEND_IF_NOT_H264_UNSET(attr->LEVEL_ASYMMETRY_ALLOWED, str, "level-asymmetry-allowed");
282
283         if (attr->PROFILE_IDC && attr->PROFILE_IOP && attr->LEVEL) {
284                 if (added) {
285                         ast_str_append(str, 0, ";");
286                 } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
287                         added = 1;
288                 }
289                 ast_str_append(str, 0, "profile-level-id=%02X%02X%02X", attr->PROFILE_IDC, attr->PROFILE_IOP, attr->LEVEL);
290         }
291
292         if (!ast_strlen_zero(attr->SPS) && !ast_strlen_zero(attr->PPS)) {
293                 if (added) {
294                         ast_str_append(str, 0, ";");
295                 } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
296                         added = 1;
297                 }
298                 ast_str_append(str, 0, "sprop-parameter-sets=%s,%s", attr->SPS, attr->PPS);
299         }
300
301         if (added) {
302                 ast_str_append(str, 0, "\r\n");
303         }
304
305         return;
306 }
307
308 static struct ast_format_interface h264_interface = {
309         .format_destroy = h264_destroy,
310         .format_clone = h264_clone,
311         .format_cmp = h264_cmp,
312         .format_get_joint = h264_getjoint,
313         .format_parse_sdp_fmtp = h264_parse_sdp_fmtp,
314         .format_generate_sdp_fmtp = h264_generate_sdp_fmtp,
315 };
316
317 static int unload_module(void)
318 {
319         return 0;
320 }
321
322 static int load_module(void)
323 {
324         if (ast_format_interface_register("h264", &h264_interface)) {
325                 return AST_MODULE_LOAD_DECLINE;
326         }
327
328         return AST_MODULE_LOAD_SUCCESS;
329 }
330
331 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "H.264 Format Attribute Module",
332         .support_level = AST_MODULE_SUPPORT_CORE,
333         .load = load_module,
334         .unload = unload_module,
335         .load_pri = AST_MODPRI_DEFAULT,
336 );