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 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.
158 device = av_strndup(p, q - p);
160 err = AVERROR(ENOMEM);
163 err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
165 errmsg = "failed to parse options";
170 err = av_hwdevice_ctx_create(&device_ref, type,
171 device ? device : p, options, 0);
175 } else if (*p == '@') {
176 // Derive from existing device.
178 src = hw_device_get_by_name(p + 1);
180 errmsg = "invalid source device name";
184 err = av_hwdevice_ctx_create_derived(&device_ref, type,
189 errmsg = "parse error";
193 dev = hw_device_add();
195 err = AVERROR(ENOMEM);
201 dev->device_ref = device_ref;
209 av_freep(&type_name);
212 av_dict_free(&options);
215 av_log(NULL, AV_LOG_ERROR,
216 "Invalid device specification \"%s\": %s\n", arg, errmsg);
217 err = AVERROR(EINVAL);
220 av_log(NULL, AV_LOG_ERROR,
221 "Device creation failed: %d.\n", err);
222 av_buffer_unref(&device_ref);
226 static int hw_device_init_from_type(enum AVHWDeviceType type,
230 AVBufferRef *device_ref = NULL;
235 name = hw_device_default_name(type);
237 err = AVERROR(ENOMEM);
241 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
243 av_log(NULL, AV_LOG_ERROR,
244 "Device creation failed: %d.\n", err);
248 dev = hw_device_add();
250 err = AVERROR(ENOMEM);
256 dev->device_ref = device_ref;
265 av_buffer_unref(&device_ref);
269 void hw_device_free_all(void)
272 for (i = 0; i < nb_hw_devices; i++) {
273 av_freep(&hw_devices[i]->name);
274 av_buffer_unref(&hw_devices[i]->device_ref);
275 av_freep(&hw_devices[i]);
277 av_freep(&hw_devices);
281 static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
283 const AVCodecHWConfig *config;
287 config = avcodec_get_hw_config(codec, i);
290 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
292 dev = hw_device_get_by_type(config->device_type);
298 int hw_device_setup_for_decode(InputStream *ist)
300 const AVCodecHWConfig *config;
301 enum AVHWDeviceType type;
302 HWDevice *dev = NULL;
303 int err, auto_device = 0;
305 if (ist->hwaccel_device) {
306 dev = hw_device_get_by_name(ist->hwaccel_device);
308 if (ist->hwaccel_id == HWACCEL_AUTO) {
310 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
311 type = ist->hwaccel_device_type;
312 err = hw_device_init_from_type(type, ist->hwaccel_device,
315 // This will be dealt with by API-specific initialisation
316 // (using hwaccel_device), so nothing further needed here.
320 if (ist->hwaccel_id == HWACCEL_AUTO) {
321 ist->hwaccel_device_type = dev->type;
322 } else if (ist->hwaccel_device_type != dev->type) {
323 av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device "
324 "specified for decoder: device %s of type %s is not "
325 "usable with hwaccel %s.\n", dev->name,
326 av_hwdevice_get_type_name(dev->type),
327 av_hwdevice_get_type_name(ist->hwaccel_device_type));
328 return AVERROR(EINVAL);
332 if (ist->hwaccel_id == HWACCEL_AUTO) {
334 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
335 type = ist->hwaccel_device_type;
336 dev = hw_device_get_by_type(type);
338 err = hw_device_init_from_type(type, NULL, &dev);
340 dev = hw_device_match_by_codec(ist->dec);
342 // No device for this codec, but not using generic hwaccel
343 // and therefore may well not need one - ignore.
351 if (!avcodec_get_hw_config(ist->dec, 0)) {
352 // Decoder does not support any hardware devices.
355 for (i = 0; !dev; i++) {
356 config = avcodec_get_hw_config(ist->dec, i);
359 type = config->device_type;
360 dev = hw_device_get_by_type(type);
362 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
363 "hwaccel type %s with existing device %s.\n",
364 av_hwdevice_get_type_name(type), dev->name);
367 for (i = 0; !dev; i++) {
368 config = avcodec_get_hw_config(ist->dec, i);
371 type = config->device_type;
372 // Try to make a new device of this type.
373 err = hw_device_init_from_type(type, ist->hwaccel_device,
376 // Can't make a device of this type.
379 if (ist->hwaccel_device) {
380 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
381 "hwaccel type %s with new device created "
382 "from %s.\n", av_hwdevice_get_type_name(type),
383 ist->hwaccel_device);
385 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
386 "hwaccel type %s with new default device.\n",
387 av_hwdevice_get_type_name(type));
391 ist->hwaccel_device_type = type;
393 av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel "
394 "disabled: no device found.\n");
395 ist->hwaccel_id = HWACCEL_NONE;
401 av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available "
402 "for decoder: device type %s needed for codec %s.\n",
403 av_hwdevice_get_type_name(type), ist->dec->name);
407 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
408 if (!ist->dec_ctx->hw_device_ctx)
409 return AVERROR(ENOMEM);
414 int hw_device_setup_for_encode(OutputStream *ost)
418 dev = hw_device_match_by_codec(ost->enc);
420 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
421 if (!ost->enc_ctx->hw_device_ctx)
422 return AVERROR(ENOMEM);
425 // No device required, or no device available.
430 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
432 InputStream *ist = avctx->opaque;
433 AVFrame *output = NULL;
434 enum AVPixelFormat output_format = ist->hwaccel_output_format;
437 if (input->format == output_format) {
442 output = av_frame_alloc();
444 return AVERROR(ENOMEM);
446 output->format = output_format;
448 err = av_hwframe_transfer_data(output, input, 0);
450 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
451 "output frame: %d.\n", err);
455 err = av_frame_copy_props(output, input);
457 av_frame_unref(output);
461 av_frame_unref(input);
462 av_frame_move_ref(input, output);
463 av_frame_free(&output);
468 av_frame_free(&output);
472 int hwaccel_decode_init(AVCodecContext *avctx)
474 InputStream *ist = avctx->opaque;
476 ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;