Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * This file is part of FFmpeg. | ||
3 | * | ||
4 | * FFmpeg is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * FFmpeg is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with FFmpeg; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | */ | ||
18 | |||
19 | #include <string.h> | ||
20 | |||
21 | #include "libavutil/avstring.h" | ||
22 | #include "libavutil/pixdesc.h" | ||
23 | #include "libavfilter/buffersink.h" | ||
24 | |||
25 | #include "ffmpeg.h" | ||
26 | |||
27 | static int nb_hw_devices; | ||
28 | static HWDevice **hw_devices; | ||
29 | |||
30 | 1653 | HWDevice *hw_device_get_by_type(enum AVHWDeviceType type) | |
31 | { | ||
32 | 1653 | HWDevice *found = NULL; | |
33 | int i; | ||
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1653 times.
|
1653 | for (i = 0; i < nb_hw_devices; i++) { |
35 | ✗ | if (hw_devices[i]->type == type) { | |
36 | ✗ | if (found) | |
37 | ✗ | return NULL; | |
38 | ✗ | found = hw_devices[i]; | |
39 | } | ||
40 | } | ||
41 | 1653 | return found; | |
42 | } | ||
43 | |||
44 | ✗ | HWDevice *hw_device_get_by_name(const char *name) | |
45 | { | ||
46 | int i; | ||
47 | ✗ | for (i = 0; i < nb_hw_devices; i++) { | |
48 | ✗ | if (!strcmp(hw_devices[i]->name, name)) | |
49 | ✗ | return hw_devices[i]; | |
50 | } | ||
51 | ✗ | return NULL; | |
52 | } | ||
53 | |||
54 | ✗ | static HWDevice *hw_device_add(void) | |
55 | { | ||
56 | int err; | ||
57 | ✗ | err = av_reallocp_array(&hw_devices, nb_hw_devices + 1, | |
58 | sizeof(*hw_devices)); | ||
59 | ✗ | if (err) { | |
60 | ✗ | nb_hw_devices = 0; | |
61 | ✗ | return NULL; | |
62 | } | ||
63 | ✗ | hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice)); | |
64 | ✗ | if (!hw_devices[nb_hw_devices]) | |
65 | ✗ | return NULL; | |
66 | ✗ | return hw_devices[nb_hw_devices++]; | |
67 | } | ||
68 | |||
69 | ✗ | static char *hw_device_default_name(enum AVHWDeviceType type) | |
70 | { | ||
71 | // Make an automatic name of the form "type%d". We arbitrarily | ||
72 | // limit at 1000 anonymous devices of the same type - there is | ||
73 | // probably something else very wrong if you get to this limit. | ||
74 | ✗ | const char *type_name = av_hwdevice_get_type_name(type); | |
75 | char *name; | ||
76 | size_t index_pos; | ||
77 | ✗ | int index, index_limit = 1000; | |
78 | ✗ | index_pos = strlen(type_name); | |
79 | ✗ | name = av_malloc(index_pos + 4); | |
80 | ✗ | if (!name) | |
81 | ✗ | return NULL; | |
82 | ✗ | for (index = 0; index < index_limit; index++) { | |
83 | ✗ | snprintf(name, index_pos + 4, "%s%d", type_name, index); | |
84 | ✗ | if (!hw_device_get_by_name(name)) | |
85 | ✗ | break; | |
86 | } | ||
87 | ✗ | if (index >= index_limit) { | |
88 | ✗ | av_freep(&name); | |
89 | ✗ | return NULL; | |
90 | } | ||
91 | ✗ | return name; | |
92 | } | ||
93 | |||
94 | ✗ | int hw_device_init_from_string(const char *arg, HWDevice **dev_out) | |
95 | { | ||
96 | // "type=name" | ||
97 | // "type=name,key=value,key2=value2" | ||
98 | // "type=name:device,key=value,key2=value2" | ||
99 | // "type:device,key=value,key2=value2" | ||
100 | // -> av_hwdevice_ctx_create() | ||
101 | // "type=name@name" | ||
102 | // "type@name" | ||
103 | // -> av_hwdevice_ctx_create_derived() | ||
104 | |||
105 | ✗ | AVDictionary *options = NULL; | |
106 | ✗ | const char *type_name = NULL, *name = NULL, *device = NULL; | |
107 | enum AVHWDeviceType type; | ||
108 | HWDevice *dev, *src; | ||
109 | ✗ | AVBufferRef *device_ref = NULL; | |
110 | int err; | ||
111 | const char *errmsg, *p, *q; | ||
112 | size_t k; | ||
113 | |||
114 | ✗ | k = strcspn(arg, ":=@"); | |
115 | ✗ | p = arg + k; | |
116 | |||
117 | ✗ | type_name = av_strndup(arg, k); | |
118 | ✗ | if (!type_name) { | |
119 | ✗ | err = AVERROR(ENOMEM); | |
120 | ✗ | goto fail; | |
121 | } | ||
122 | ✗ | type = av_hwdevice_find_type_by_name(type_name); | |
123 | ✗ | if (type == AV_HWDEVICE_TYPE_NONE) { | |
124 | ✗ | errmsg = "unknown device type"; | |
125 | ✗ | goto invalid; | |
126 | } | ||
127 | |||
128 | ✗ | if (*p == '=') { | |
129 | ✗ | k = strcspn(p + 1, ":@,"); | |
130 | |||
131 | ✗ | name = av_strndup(p + 1, k); | |
132 | ✗ | if (!name) { | |
133 | ✗ | err = AVERROR(ENOMEM); | |
134 | ✗ | goto fail; | |
135 | } | ||
136 | ✗ | if (hw_device_get_by_name(name)) { | |
137 | ✗ | errmsg = "named device already exists"; | |
138 | ✗ | goto invalid; | |
139 | } | ||
140 | |||
141 | ✗ | p += 1 + k; | |
142 | } else { | ||
143 | ✗ | name = hw_device_default_name(type); | |
144 | ✗ | if (!name) { | |
145 | ✗ | err = AVERROR(ENOMEM); | |
146 | ✗ | goto fail; | |
147 | } | ||
148 | } | ||
149 | |||
150 | ✗ | if (!*p) { | |
151 | // New device with no parameters. | ||
152 | ✗ | err = av_hwdevice_ctx_create(&device_ref, type, | |
153 | NULL, NULL, 0); | ||
154 | ✗ | if (err < 0) | |
155 | ✗ | goto fail; | |
156 | |||
157 | ✗ | } else if (*p == ':') { | |
158 | // New device with some parameters. | ||
159 | ✗ | ++p; | |
160 | ✗ | q = strchr(p, ','); | |
161 | ✗ | if (q) { | |
162 | ✗ | if (q - p > 0) { | |
163 | ✗ | device = av_strndup(p, q - p); | |
164 | ✗ | if (!device) { | |
165 | ✗ | err = AVERROR(ENOMEM); | |
166 | ✗ | goto fail; | |
167 | } | ||
168 | } | ||
169 | ✗ | err = av_dict_parse_string(&options, q + 1, "=", ",", 0); | |
170 | ✗ | if (err < 0) { | |
171 | ✗ | errmsg = "failed to parse options"; | |
172 | ✗ | goto invalid; | |
173 | } | ||
174 | } | ||
175 | |||
176 | ✗ | err = av_hwdevice_ctx_create(&device_ref, type, | |
177 | ✗ | q ? device : p[0] ? p : NULL, | |
178 | options, 0); | ||
179 | ✗ | if (err < 0) | |
180 | ✗ | goto fail; | |
181 | |||
182 | ✗ | } else if (*p == '@') { | |
183 | // Derive from existing device. | ||
184 | |||
185 | ✗ | src = hw_device_get_by_name(p + 1); | |
186 | ✗ | if (!src) { | |
187 | ✗ | errmsg = "invalid source device name"; | |
188 | ✗ | goto invalid; | |
189 | } | ||
190 | |||
191 | ✗ | err = av_hwdevice_ctx_create_derived(&device_ref, type, | |
192 | src->device_ref, 0); | ||
193 | ✗ | if (err < 0) | |
194 | ✗ | goto fail; | |
195 | ✗ | } else if (*p == ',') { | |
196 | ✗ | err = av_dict_parse_string(&options, p + 1, "=", ",", 0); | |
197 | |||
198 | ✗ | if (err < 0) { | |
199 | ✗ | errmsg = "failed to parse options"; | |
200 | ✗ | goto invalid; | |
201 | } | ||
202 | |||
203 | ✗ | err = av_hwdevice_ctx_create(&device_ref, type, | |
204 | NULL, options, 0); | ||
205 | ✗ | if (err < 0) | |
206 | ✗ | goto fail; | |
207 | } else { | ||
208 | ✗ | errmsg = "parse error"; | |
209 | ✗ | goto invalid; | |
210 | } | ||
211 | |||
212 | ✗ | dev = hw_device_add(); | |
213 | ✗ | if (!dev) { | |
214 | ✗ | err = AVERROR(ENOMEM); | |
215 | ✗ | goto fail; | |
216 | } | ||
217 | |||
218 | ✗ | dev->name = name; | |
219 | ✗ | dev->type = type; | |
220 | ✗ | dev->device_ref = device_ref; | |
221 | |||
222 | ✗ | if (dev_out) | |
223 | ✗ | *dev_out = dev; | |
224 | |||
225 | ✗ | name = NULL; | |
226 | ✗ | err = 0; | |
227 | ✗ | done: | |
228 | ✗ | av_freep(&type_name); | |
229 | ✗ | av_freep(&name); | |
230 | ✗ | av_freep(&device); | |
231 | ✗ | av_dict_free(&options); | |
232 | ✗ | return err; | |
233 | ✗ | invalid: | |
234 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
235 | "Invalid device specification \"%s\": %s\n", arg, errmsg); | ||
236 | ✗ | err = AVERROR(EINVAL); | |
237 | ✗ | goto done; | |
238 | ✗ | fail: | |
239 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
240 | "Device creation failed: %d.\n", err); | ||
241 | ✗ | av_buffer_unref(&device_ref); | |
242 | ✗ | goto done; | |
243 | } | ||
244 | |||
245 | ✗ | int hw_device_init_from_type(enum AVHWDeviceType type, | |
246 | const char *device, | ||
247 | HWDevice **dev_out) | ||
248 | { | ||
249 | ✗ | AVBufferRef *device_ref = NULL; | |
250 | HWDevice *dev; | ||
251 | char *name; | ||
252 | int err; | ||
253 | |||
254 | ✗ | name = hw_device_default_name(type); | |
255 | ✗ | if (!name) { | |
256 | ✗ | err = AVERROR(ENOMEM); | |
257 | ✗ | goto fail; | |
258 | } | ||
259 | |||
260 | ✗ | err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0); | |
261 | ✗ | if (err < 0) { | |
262 | ✗ | av_log(NULL, AV_LOG_ERROR, | |
263 | "Device creation failed: %d.\n", err); | ||
264 | ✗ | goto fail; | |
265 | } | ||
266 | |||
267 | ✗ | dev = hw_device_add(); | |
268 | ✗ | if (!dev) { | |
269 | ✗ | err = AVERROR(ENOMEM); | |
270 | ✗ | goto fail; | |
271 | } | ||
272 | |||
273 | ✗ | dev->name = name; | |
274 | ✗ | dev->type = type; | |
275 | ✗ | dev->device_ref = device_ref; | |
276 | |||
277 | ✗ | if (dev_out) | |
278 | ✗ | *dev_out = dev; | |
279 | |||
280 | ✗ | return 0; | |
281 | |||
282 | ✗ | fail: | |
283 | ✗ | av_freep(&name); | |
284 | ✗ | av_buffer_unref(&device_ref); | |
285 | ✗ | return err; | |
286 | } | ||
287 | |||
288 | 6756 | void hw_device_free_all(void) | |
289 | { | ||
290 | int i; | ||
291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6756 times.
|
6756 | for (i = 0; i < nb_hw_devices; i++) { |
292 | ✗ | av_freep(&hw_devices[i]->name); | |
293 | ✗ | av_buffer_unref(&hw_devices[i]->device_ref); | |
294 | ✗ | av_freep(&hw_devices[i]); | |
295 | } | ||
296 | 6756 | av_freep(&hw_devices); | |
297 | 6756 | nb_hw_devices = 0; | |
298 | 6756 | } | |
299 | |||
300 | 6424 | AVBufferRef *hw_device_for_filter(void) | |
301 | { | ||
302 | // Pick the last hardware device if the user doesn't pick the device for | ||
303 | // filters explicitly with the filter_hw_device option. | ||
304 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6424 times.
|
6424 | if (filter_hw_device) |
305 | ✗ | return filter_hw_device->device_ref; | |
306 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6424 times.
|
6424 | else if (nb_hw_devices > 0) { |
307 | ✗ | HWDevice *dev = hw_devices[nb_hw_devices - 1]; | |
308 | |||
309 | ✗ | if (nb_hw_devices > 1) | |
310 | ✗ | av_log(NULL, AV_LOG_WARNING, "There are %d hardware devices. device " | |
311 | "%s of type %s is picked for filters by default. Set hardware " | ||
312 | "device explicitly with the filter_hw_device option if device " | ||
313 | "%s is not usable for filters.\n", | ||
314 | nb_hw_devices, dev->name, | ||
315 | av_hwdevice_get_type_name(dev->type), dev->name); | ||
316 | |||
317 | ✗ | return dev->device_ref; | |
318 | } | ||
319 | |||
320 | 6424 | return NULL; | |
321 | } | ||
322 |