1 | /* |
2 | Copyright (c) 2012, Broadcom Europe Ltd |
3 | All rights reserved. |
4 | |
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the following conditions are met: |
7 | * Redistributions of source code must retain the above copyright |
8 | notice, this list of conditions and the following disclaimer. |
9 | * Redistributions in binary form must reproduce the above copyright |
10 | notice, this list of conditions and the following disclaimer in the |
11 | documentation and/or other materials provided with the distribution. |
12 | * Neither the name of the copyright holder nor the |
13 | names of its contributors may be used to endorse or promote products |
14 | derived from this software without specific prior written permission. |
15 | |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | #include "mmal_param_convert.h" |
28 | #include <stdlib.h> |
29 | #include <stdio.h> |
30 | |
31 | typedef struct string_pair_t |
32 | { |
33 | const char *string; |
34 | int value; |
35 | } string_pair_t; |
36 | |
37 | static MMAL_STATUS_T parse_enum(int *dest, string_pair_t *pairs, size_t n_pairs, const char *str) |
38 | { |
39 | size_t i; |
40 | for (i=0; i<n_pairs; i++) |
41 | { |
42 | if (vcos_strcasecmp(str, pairs[i].string) == 0) |
43 | { |
44 | *dest = pairs[i].value; |
45 | return MMAL_SUCCESS; |
46 | } |
47 | } |
48 | return MMAL_EINVAL; |
49 | } |
50 | |
51 | MMAL_STATUS_T mmal_parse_video_size(uint32_t *w, uint32_t *h, const char *str) |
52 | { |
53 | static struct { |
54 | const char *name; |
55 | uint32_t width; |
56 | uint32_t height; |
57 | } sizes[] = { |
58 | { "1080p" , 1920, 1080 }, |
59 | { "720p" , 1280, 720 }, |
60 | { "vga" , 640, 480 }, |
61 | { "wvga" , 800, 480 }, |
62 | { "cif" , 352, 288 }, |
63 | { "qcif" , 352/2, 288/2 }, |
64 | }; |
65 | size_t i; |
66 | for (i=0; i<vcos_countof(sizes); i++) |
67 | { |
68 | if (vcos_strcasecmp(str, sizes[i].name) == 0) |
69 | { |
70 | *w = sizes[i].width; |
71 | *h = sizes[i].height; |
72 | return MMAL_SUCCESS; |
73 | } |
74 | } |
75 | return MMAL_EINVAL; |
76 | } |
77 | |
78 | MMAL_STATUS_T mmal_parse_rational(MMAL_RATIONAL_T *dest, const char *str) |
79 | { |
80 | MMAL_STATUS_T ret; |
81 | char *endptr; |
82 | long num, den = 1; |
83 | num = strtoul(str, &endptr, 0); |
84 | if (endptr[0] == '\0') |
85 | { |
86 | /* that's it */ |
87 | ret = MMAL_SUCCESS; |
88 | } |
89 | else if (endptr[0] == '/') |
90 | { |
91 | den = strtoul(endptr+1, &endptr, 0); |
92 | if (endptr[0] == '\0') |
93 | ret = MMAL_SUCCESS; |
94 | else |
95 | ret = MMAL_EINVAL; |
96 | } |
97 | else |
98 | { |
99 | ret = MMAL_EINVAL; |
100 | } |
101 | dest->num = num; |
102 | dest->den = den; |
103 | return ret; |
104 | } |
105 | |
106 | MMAL_STATUS_T mmal_parse_int(int *dest, const char *str) |
107 | { |
108 | char *endptr; |
109 | long i = strtol(str, &endptr, 0); |
110 | if (endptr[0] == '\0') |
111 | { |
112 | *dest = i; |
113 | return MMAL_SUCCESS; |
114 | } |
115 | else |
116 | { |
117 | return MMAL_EINVAL; |
118 | } |
119 | } |
120 | |
121 | MMAL_STATUS_T mmal_parse_uint(unsigned int *dest, const char *str) |
122 | { |
123 | char *endptr; |
124 | unsigned long i = strtoul(str, &endptr, 0); |
125 | if (endptr[0] == '\0') |
126 | { |
127 | *dest = i; |
128 | return MMAL_SUCCESS; |
129 | } |
130 | else |
131 | { |
132 | return MMAL_EINVAL; |
133 | } |
134 | } |
135 | |
136 | MMAL_STATUS_T mmal_parse_video_codec(uint32_t *dest, const char *str) |
137 | { |
138 | static string_pair_t video_codec_enums[] = { |
139 | { "h264" , MMAL_ENCODING_H264 }, |
140 | { "h263" , MMAL_ENCODING_H263 }, |
141 | { "mpeg4" , MMAL_ENCODING_MP4V }, |
142 | { "mpeg2" , MMAL_ENCODING_MP2V }, |
143 | { "vp8" , MMAL_ENCODING_VP8 }, |
144 | { "vp7" , MMAL_ENCODING_VP7 }, |
145 | { "vp6" , MMAL_ENCODING_VP6 }, |
146 | }; |
147 | int i = 0; |
148 | MMAL_STATUS_T ret; |
149 | |
150 | ret = parse_enum(&i, video_codec_enums, vcos_countof(video_codec_enums), str); |
151 | *dest = i; |
152 | return ret; |
153 | } |
154 | |
155 | MMAL_STATUS_T mmal_parse_geometry(MMAL_RECT_T *dest, const char *str) |
156 | { |
157 | MMAL_STATUS_T ret; |
158 | uint32_t w, h, x, y; |
159 | x = y = w = h = 0; |
160 | /* coverity[secure_coding] */ |
161 | if (sscanf(str, "%d*%d+%d+%d" , &w,&h,&x,&y) == 4 || |
162 | sscanf(str, "%d*%d" , &w,&h) == 2) |
163 | { |
164 | dest->x = x; |
165 | dest->y = y; |
166 | dest->width = w; |
167 | dest->height = h; |
168 | ret = MMAL_SUCCESS; |
169 | } |
170 | else |
171 | { |
172 | ret = MMAL_EINVAL; |
173 | } |
174 | return ret; |
175 | } |
176 | |
177 | |