2 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avstring.h"
25 static int nb_hw_devices;
26 static HWDevice **hw_devices;
28 static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
30 HWDevice *found = NULL;
32 for (i = 0; i < nb_hw_devices; i++) {
33 if (hw_devices[i]->type == type) {
36 found = hw_devices[i];
42 HWDevice *hw_device_get_by_name(const char *name)
45 for (i = 0; i < nb_hw_devices; i++) {
46 if (!strcmp(hw_devices[i]->name, name))
52 static HWDevice *hw_device_add(void)
55 err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
61 hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
62 if (!hw_devices[nb_hw_devices])
64 return hw_devices[nb_hw_devices++];
67 static char *hw_device_default_name(enum AVHWDeviceType type)
69 // Make an automatic name of the form "type%d". We arbitrarily
70 // limit at 1000 anonymous devices of the same type - there is
71 // probably something else very wrong if you get to this limit.
72 const char *type_name = av_hwdevice_get_type_name(type);
75 int index, index_limit = 1000;
76 index_pos = strlen(type_name);
77 name = av_malloc(index_pos + 4);
80 for (index = 0; index < index_limit; index++) {
81 snprintf(name, index_pos + 4, "%s%d", type_name, index);
82 if (!hw_device_get_by_name(name))
85 if (index >= index_limit) {
92 int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
94 // "type=name:device,key=value,key2=value2"
95 // "type:device,key=value,key2=value2"
96 // -> av_hwdevice_ctx_create()
99 // -> av_hwdevice_ctx_create_derived()
101 AVDictionary *options = NULL;
102 const char *type_name = NULL, *name = NULL, *device = NULL;
103 enum AVHWDeviceType type;
105 AVBufferRef *device_ref = NULL;
107 const char *errmsg, *p, *q;
110 k = strcspn(arg, ":=@");
113 type_name = av_strndup(arg, k);
115 err = AVERROR(ENOMEM);
118 type = av_hwdevice_find_type_by_name(type_name);
119 if (type == AV_HWDEVICE_TYPE_NONE) {
120 errmsg = "unknown device type";
125 k = strcspn(p + 1, ":@");
127 name = av_strndup(p + 1, k);
129 err = AVERROR(ENOMEM);
132 if (hw_device_get_by_name(name)) {
133 errmsg = "named device already exists";
139 name = hw_device_default_name(type);
141 err = AVERROR(ENOMEM);
147 // New device with no parameters.
148 err = av_hwdevice_ctx_create(&device_ref, type,
153 } else if (*p == ':') {
154 // New device with some parameters.
159 device = av_strndup(p, q - p);
161 err = AVERROR(ENOMEM);
165 err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
167 errmsg = "failed to parse options";
172 err = av_hwdevice_ctx_create(&device_ref, type,
173 q ? device : p[0] ? p : NULL,
178 } else if (*p == '@') {
179 // Derive from existing device.
181 src = hw_device_get_by_name(p + 1);
183 errmsg = "invalid source device name";
187 err = av_hwdevice_ctx_create_derived(&device_ref, type,
192 errmsg = "parse error";
196 dev = hw_device_add();
198 err = AVERROR(ENOMEM);
204 dev->device_ref = device_ref;
212 av_freep(&type_name);
215 av_dict_free(&options);
218 av_log(NULL, AV_LOG_ERROR,
219 "Invalid device specification \"%s\": %s\n", arg, errmsg);
220 err = AVERROR(EINVAL);
223 av_log(NULL, AV_LOG_ERROR,
224 "Device creation failed: %d.\n", err);
225 av_buffer_unref(&device_ref);
229 static int hw_device_init_from_type(enum AVHWDeviceType type,
233 AVBufferRef *device_ref = NULL;
238 name = hw_device_default_name(type);
240 err = AVERROR(ENOMEM);
244 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
246 av_log(NULL, AV_LOG_ERROR,
247 "Device creation failed: %d.\n", err);
251 dev = hw_device_add();
253 err = AVERROR(ENOMEM);
259 dev->device_ref = device_ref;
268 av_buffer_unref(&device_ref);
272 void hw_device_free_all(void)
275 for (i = 0; i < nb_hw_devices; i++) {
276 av_freep(&hw_devices[i]->name);
277 av_buffer_unref(&hw_devices[i]->device_ref);
278 av_freep(&hw_devices[i]);
280 av_freep(&hw_devices);
284 static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
286 const AVCodecHWConfig *config;
290 config = avcodec_get_hw_config(codec, i);
293 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
295 dev = hw_device_get_by_type(config->device_type);
301 int hw_device_setup_for_decode(InputStream *ist)
303 const AVCodecHWConfig *config;
304 enum AVHWDeviceType type;
305 HWDevice *dev = NULL;
306 int err, auto_device = 0;
308 if (ist->hwaccel_device) {
309 dev = hw_device_get_by_name(ist->hwaccel_device);
311 if (ist->hwaccel_id == HWACCEL_AUTO) {
313 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
314 type = ist->hwaccel_device_type;
315 err = hw_device_init_from_type(type, ist->hwaccel_device,
318 // This will be dealt with by API-specific initialisation
319 // (using hwaccel_device), so nothing further needed here.
323 if (ist->hwaccel_id == HWACCEL_AUTO) {
324 ist->hwaccel_device_type = dev->type;
325 } else if (ist->hwaccel_device_type != dev->type) {
326 av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device "
327 "specified for decoder: device %s of type %s is not "
328 "usable with hwaccel %s.\n", dev->name,
329 av_hwdevice_get_type_name(dev->type),
330 av_hwdevice_get_type_name(ist->hwaccel_device_type));
331 return AVERROR(EINVAL);
335 if (ist->hwaccel_id == HWACCEL_AUTO) {
337 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
338 type = ist->hwaccel_device_type;
339 dev = hw_device_get_by_type(type);
341 err = hw_device_init_from_type(type, NULL, &dev);
343 dev = hw_device_match_by_codec(ist->dec);
345 // No device for this codec, but not using generic hwaccel
346 // and therefore may well not need one - ignore.
354 if (!avcodec_get_hw_config(ist->dec, 0)) {
355 // Decoder does not support any hardware devices.
358 for (i = 0; !dev; i++) {
359 config = avcodec_get_hw_config(ist->dec, i);
362 type = config->device_type;
363 dev = hw_device_get_by_type(type);
365 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
366 "hwaccel type %s with existing device %s.\n",
367 av_hwdevice_get_type_name(type), dev->name);
370 for (i = 0; !dev; i++) {
371 config = avcodec_get_hw_config(ist->dec, i);
374 type = config->device_type;
375 // Try to make a new device of this type.
376 err = hw_device_init_from_type(type, ist->hwaccel_device,
379 // Can't make a device of this type.
382 if (ist->hwaccel_device) {
383 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
384 "hwaccel type %s with new device created "
385 "from %s.\n", av_hwdevice_get_type_name(type),
386 ist->hwaccel_device);
388 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
389 "hwaccel type %s with new default device.\n",
390 av_hwdevice_get_type_name(type));
394 ist->hwaccel_device_type = type;
396 av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel "
397 "disabled: no device found.\n");
398 ist->hwaccel_id = HWACCEL_NONE;
404 av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available "
405 "for decoder: device type %s needed for codec %s.\n",
406 av_hwdevice_get_type_name(type), ist->dec->name);
410 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
411 if (!ist->dec_ctx->hw_device_ctx)
412 return AVERROR(ENOMEM);
417 int hw_device_setup_for_encode(OutputStream *ost)
421 dev = hw_device_match_by_codec(ost->enc);
423 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
424 if (!ost->enc_ctx->hw_device_ctx)
425 return AVERROR(ENOMEM);
428 // No device required, or no device available.
433 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
435 InputStream *ist = avctx->opaque;
436 AVFrame *output = NULL;
437 enum AVPixelFormat output_format = ist->hwaccel_output_format;
440 if (input->format == output_format) {
445 output = av_frame_alloc();
447 return AVERROR(ENOMEM);
449 output->format = output_format;
451 err = av_hwframe_transfer_data(output, input, 0);
453 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
454 "output frame: %d.\n", err);
458 err = av_frame_copy_props(output, input);
460 av_frame_unref(output);
464 av_frame_unref(input);
465 av_frame_move_ref(input, output);
466 av_frame_free(&output);
471 av_frame_free(&output);
475 int hwaccel_decode_init(AVCodecContext *avctx)
477 InputStream *ist = avctx->opaque;
479 ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;