2 * This file is part of Libav.
4 * Libav 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.
9 * Libav 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 static int nb_hw_devices;
24 static HWDevice **hw_devices;
26 static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
28 HWDevice *found = NULL;
30 for (i = 0; i < nb_hw_devices; i++) {
31 if (hw_devices[i]->type == type) {
34 found = hw_devices[i];
40 HWDevice *hw_device_get_by_name(const char *name)
43 for (i = 0; i < nb_hw_devices; i++) {
44 if (!strcmp(hw_devices[i]->name, name))
50 static HWDevice *hw_device_add(void)
53 err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
59 hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
60 if (!hw_devices[nb_hw_devices])
62 return hw_devices[nb_hw_devices++];
65 int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
67 // "type=name:device,key=value,key2=value2"
68 // "type:device,key=value,key2=value2"
69 // -> av_hwdevice_ctx_create()
72 // -> av_hwdevice_ctx_create_derived()
74 AVDictionary *options = NULL;
75 char *type_name = NULL, *name = NULL, *device = NULL;
76 enum AVHWDeviceType type;
78 AVBufferRef *device_ref = NULL;
80 const char *errmsg, *p, *q;
83 k = strcspn(arg, ":=@");
86 type_name = av_strndup(arg, k);
88 err = AVERROR(ENOMEM);
91 type = av_hwdevice_find_type_by_name(type_name);
92 if (type == AV_HWDEVICE_TYPE_NONE) {
93 errmsg = "unknown device type";
98 k = strcspn(p + 1, ":@");
100 name = av_strndup(p + 1, k);
102 err = AVERROR(ENOMEM);
105 if (hw_device_get_by_name(name)) {
106 errmsg = "named device already exists";
112 // Give the device an automatic name of the form "type%d".
113 // We arbitrarily limit at 1000 anonymous devices of the same
114 // type - there is probably something else very wrong if you
115 // get to this limit.
117 int index, index_limit = 1000;
118 index_pos = strlen(type_name);
119 name = av_malloc(index_pos + 4);
121 err = AVERROR(ENOMEM);
124 for (index = 0; index < index_limit; index++) {
125 snprintf(name, index_pos + 4, "%s%d", type_name, index);
126 if (!hw_device_get_by_name(name))
129 if (index >= index_limit) {
130 errmsg = "too many devices";
136 // New device with no parameters.
137 err = av_hwdevice_ctx_create(&device_ref, type,
142 } else if (*p == ':') {
143 // New device with some parameters.
147 device = av_strndup(p, q - p);
149 err = AVERROR(ENOMEM);
152 err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
154 errmsg = "failed to parse options";
159 err = av_hwdevice_ctx_create(&device_ref, type,
160 device ? device : p, options, 0);
164 } else if (*p == '@') {
165 // Derive from existing device.
167 src = hw_device_get_by_name(p + 1);
169 errmsg = "invalid source device name";
173 err = av_hwdevice_ctx_create_derived(&device_ref, type,
178 errmsg = "parse error";
182 dev = hw_device_add();
184 err = AVERROR(ENOMEM);
190 dev->device_ref = device_ref;
198 av_freep(&type_name);
201 av_dict_free(&options);
204 av_log(NULL, AV_LOG_ERROR,
205 "Invalid device specification \"%s\": %s\n", arg, errmsg);
206 err = AVERROR(EINVAL);
209 av_log(NULL, AV_LOG_ERROR,
210 "Device creation failed: %d.\n", err);
211 av_buffer_unref(&device_ref);
215 void hw_device_free_all(void)
218 for (i = 0; i < nb_hw_devices; i++) {
219 av_freep(&hw_devices[i]->name);
220 av_buffer_unref(&hw_devices[i]->device_ref);
221 av_freep(&hw_devices[i]);
223 av_freep(&hw_devices);
227 static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
230 if (hwaccel_id == HWACCEL_NONE)
231 return AV_HWDEVICE_TYPE_NONE;
232 for (i = 0; hwaccels[i].name; i++) {
233 if (hwaccels[i].id == hwaccel_id)
234 return hwaccels[i].device_type;
236 return AV_HWDEVICE_TYPE_NONE;
239 static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
241 const char *type_name;
242 enum AVHWDeviceType type;
243 for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
244 type != AV_HWDEVICE_TYPE_NONE;
245 type = av_hwdevice_iterate_types(type)) {
246 type_name = av_hwdevice_get_type_name(type);
247 if (strstr(codec_name, type_name))
250 return AV_HWDEVICE_TYPE_NONE;
253 int hw_device_setup_for_decode(InputStream *ist)
255 enum AVHWDeviceType type;
257 const char *type_name;
260 if (ist->hwaccel_device) {
261 dev = hw_device_get_by_name(ist->hwaccel_device);
265 type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
266 if (type == AV_HWDEVICE_TYPE_NONE) {
267 // No match - this isn't necessarily invalid, though,
268 // because an explicit device might not be needed or
269 // the hwaccel setup could be handled elsewhere.
272 type_name = av_hwdevice_get_type_name(type);
273 len = strlen(type_name) + 1 +
274 strlen(ist->hwaccel_device) + 1;
275 tmp = av_malloc(len);
277 return AVERROR(ENOMEM);
278 snprintf(tmp, len, "%s:%s", type_name, ist->hwaccel_device);
279 err = hw_device_init_from_string(tmp, &dev);
285 if (ist->hwaccel_id != HWACCEL_NONE)
286 type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
288 type = hw_device_match_type_in_name(ist->dec->name);
289 if (type != AV_HWDEVICE_TYPE_NONE) {
290 dev = hw_device_get_by_type(type);
292 hw_device_init_from_string(av_hwdevice_get_type_name(type),
296 // No device required.
302 av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
303 "for decoder (device type %s for codec %s).\n",
304 av_hwdevice_get_type_name(type), ist->dec->name);
308 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
309 if (!ist->dec_ctx->hw_device_ctx)
310 return AVERROR(ENOMEM);
315 int hw_device_setup_for_encode(OutputStream *ost)
317 enum AVHWDeviceType type;
320 type = hw_device_match_type_in_name(ost->enc->name);
321 if (type != AV_HWDEVICE_TYPE_NONE) {
322 dev = hw_device_get_by_type(type);
324 av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
325 "for encoder (device type %s for codec %s).\n",
326 av_hwdevice_get_type_name(type), ost->enc->name);
329 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
330 if (!ost->enc_ctx->hw_device_ctx)
331 return AVERROR(ENOMEM);
334 // No device required.
339 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
341 InputStream *ist = avctx->opaque;
342 AVFrame *output = NULL;
343 enum AVPixelFormat output_format = ist->hwaccel_output_format;
346 if (input->format == output_format) {
351 output = av_frame_alloc();
353 return AVERROR(ENOMEM);
355 output->format = output_format;
357 err = av_hwframe_transfer_data(output, input, 0);
359 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
360 "output frame: %d.\n", err);
364 err = av_frame_copy_props(output, input);
366 av_frame_unref(output);
370 av_frame_unref(input);
371 av_frame_move_ref(input, output);
372 av_frame_free(&output);
377 av_frame_free(&output);
381 int hwaccel_decode_init(AVCodecContext *avctx)
383 InputStream *ist = avctx->opaque;
385 ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;