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