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"
22 #include "libavutil/pixdesc.h"
23 #include "libavfilter/buffersink.h"
27 static int nb_hw_devices;
28 static HWDevice **hw_devices;
30 static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
32 HWDevice *found = NULL;
34 for (i = 0; i < nb_hw_devices; i++) {
35 if (hw_devices[i]->type == type) {
38 found = hw_devices[i];
44 HWDevice *hw_device_get_by_name(const char *name)
47 for (i = 0; i < nb_hw_devices; i++) {
48 if (!strcmp(hw_devices[i]->name, name))
54 static HWDevice *hw_device_add(void)
57 err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
63 hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
64 if (!hw_devices[nb_hw_devices])
66 return hw_devices[nb_hw_devices++];
69 static char *hw_device_default_name(enum AVHWDeviceType type)
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);
77 int index, index_limit = 1000;
78 index_pos = strlen(type_name);
79 name = av_malloc(index_pos + 4);
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))
87 if (index >= index_limit) {
94 int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
96 // "type=name:device,key=value,key2=value2"
97 // "type:device,key=value,key2=value2"
98 // -> av_hwdevice_ctx_create()
101 // -> av_hwdevice_ctx_create_derived()
103 AVDictionary *options = NULL;
104 const char *type_name = NULL, *name = NULL, *device = NULL;
105 enum AVHWDeviceType type;
107 AVBufferRef *device_ref = NULL;
109 const char *errmsg, *p, *q;
112 k = strcspn(arg, ":=@");
115 type_name = av_strndup(arg, k);
117 err = AVERROR(ENOMEM);
120 type = av_hwdevice_find_type_by_name(type_name);
121 if (type == AV_HWDEVICE_TYPE_NONE) {
122 errmsg = "unknown device type";
127 k = strcspn(p + 1, ":@");
129 name = av_strndup(p + 1, k);
131 err = AVERROR(ENOMEM);
134 if (hw_device_get_by_name(name)) {
135 errmsg = "named device already exists";
141 name = hw_device_default_name(type);
143 err = AVERROR(ENOMEM);
149 // New device with no parameters.
150 err = av_hwdevice_ctx_create(&device_ref, type,
155 } else if (*p == ':') {
156 // New device with some parameters.
161 device = av_strndup(p, q - p);
163 err = AVERROR(ENOMEM);
167 err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
169 errmsg = "failed to parse options";
174 err = av_hwdevice_ctx_create(&device_ref, type,
175 q ? device : p[0] ? p : NULL,
180 } else if (*p == '@') {
181 // Derive from existing device.
183 src = hw_device_get_by_name(p + 1);
185 errmsg = "invalid source device name";
189 err = av_hwdevice_ctx_create_derived(&device_ref, type,
194 errmsg = "parse error";
198 dev = hw_device_add();
200 err = AVERROR(ENOMEM);
206 dev->device_ref = device_ref;
214 av_freep(&type_name);
217 av_dict_free(&options);
220 av_log(NULL, AV_LOG_ERROR,
221 "Invalid device specification \"%s\": %s\n", arg, errmsg);
222 err = AVERROR(EINVAL);
225 av_log(NULL, AV_LOG_ERROR,
226 "Device creation failed: %d.\n", err);
227 av_buffer_unref(&device_ref);
231 static int hw_device_init_from_type(enum AVHWDeviceType type,
235 AVBufferRef *device_ref = NULL;
240 name = hw_device_default_name(type);
242 err = AVERROR(ENOMEM);
246 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
248 av_log(NULL, AV_LOG_ERROR,
249 "Device creation failed: %d.\n", err);
253 dev = hw_device_add();
255 err = AVERROR(ENOMEM);
261 dev->device_ref = device_ref;
270 av_buffer_unref(&device_ref);
274 void hw_device_free_all(void)
277 for (i = 0; i < nb_hw_devices; i++) {
278 av_freep(&hw_devices[i]->name);
279 av_buffer_unref(&hw_devices[i]->device_ref);
280 av_freep(&hw_devices[i]);
282 av_freep(&hw_devices);
286 static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
288 const AVCodecHWConfig *config;
292 config = avcodec_get_hw_config(codec, i);
295 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
297 dev = hw_device_get_by_type(config->device_type);
303 int hw_device_setup_for_decode(InputStream *ist)
305 const AVCodecHWConfig *config;
306 enum AVHWDeviceType type;
307 HWDevice *dev = NULL;
308 int err, auto_device = 0;
310 if (ist->hwaccel_device) {
311 dev = hw_device_get_by_name(ist->hwaccel_device);
313 if (ist->hwaccel_id == HWACCEL_AUTO) {
315 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
316 type = ist->hwaccel_device_type;
317 err = hw_device_init_from_type(type, ist->hwaccel_device,
320 // This will be dealt with by API-specific initialisation
321 // (using hwaccel_device), so nothing further needed here.
325 if (ist->hwaccel_id == HWACCEL_AUTO) {
326 ist->hwaccel_device_type = dev->type;
327 } else if (ist->hwaccel_device_type != dev->type) {
328 av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device "
329 "specified for decoder: device %s of type %s is not "
330 "usable with hwaccel %s.\n", dev->name,
331 av_hwdevice_get_type_name(dev->type),
332 av_hwdevice_get_type_name(ist->hwaccel_device_type));
333 return AVERROR(EINVAL);
337 if (ist->hwaccel_id == HWACCEL_AUTO) {
339 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
340 type = ist->hwaccel_device_type;
341 dev = hw_device_get_by_type(type);
343 err = hw_device_init_from_type(type, NULL, &dev);
345 dev = hw_device_match_by_codec(ist->dec);
347 // No device for this codec, but not using generic hwaccel
348 // and therefore may well not need one - ignore.
356 if (!avcodec_get_hw_config(ist->dec, 0)) {
357 // Decoder does not support any hardware devices.
360 for (i = 0; !dev; i++) {
361 config = avcodec_get_hw_config(ist->dec, i);
364 type = config->device_type;
365 dev = hw_device_get_by_type(type);
367 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
368 "hwaccel type %s with existing device %s.\n",
369 av_hwdevice_get_type_name(type), dev->name);
372 for (i = 0; !dev; i++) {
373 config = avcodec_get_hw_config(ist->dec, i);
376 type = config->device_type;
377 // Try to make a new device of this type.
378 err = hw_device_init_from_type(type, ist->hwaccel_device,
381 // Can't make a device of this type.
384 if (ist->hwaccel_device) {
385 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
386 "hwaccel type %s with new device created "
387 "from %s.\n", av_hwdevice_get_type_name(type),
388 ist->hwaccel_device);
390 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
391 "hwaccel type %s with new default device.\n",
392 av_hwdevice_get_type_name(type));
396 ist->hwaccel_device_type = type;
398 av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel "
399 "disabled: no device found.\n");
400 ist->hwaccel_id = HWACCEL_NONE;
406 av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available "
407 "for decoder: device type %s needed for codec %s.\n",
408 av_hwdevice_get_type_name(type), ist->dec->name);
412 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
413 if (!ist->dec_ctx->hw_device_ctx)
414 return AVERROR(ENOMEM);
419 int hw_device_setup_for_encode(OutputStream *ost)
421 const AVCodecHWConfig *config;
422 HWDevice *dev = NULL;
423 AVBufferRef *frames_ref = NULL;
427 frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter);
429 ((AVHWFramesContext*)frames_ref->data)->format ==
430 ost->enc_ctx->pix_fmt) {
431 // Matching format, will try to use hw_frames_ctx.
438 config = avcodec_get_hw_config(ost->enc, i);
443 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
444 (config->pix_fmt == AV_PIX_FMT_NONE ||
445 config->pix_fmt == ost->enc_ctx->pix_fmt)) {
446 av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
447 "frames context (format %s) with %s encoder.\n",
448 av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
450 ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
451 if (!ost->enc_ctx->hw_frames_ctx)
452 return AVERROR(ENOMEM);
457 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
458 dev = hw_device_get_by_type(config->device_type);
462 av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
463 "(type %s) with %s encoder.\n", dev->name,
464 av_hwdevice_get_type_name(dev->type), ost->enc->name);
465 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
466 if (!ost->enc_ctx->hw_device_ctx)
467 return AVERROR(ENOMEM);
469 // No device required, or no device available.
474 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
476 InputStream *ist = avctx->opaque;
477 AVFrame *output = NULL;
478 enum AVPixelFormat output_format = ist->hwaccel_output_format;
481 if (input->format == output_format) {
486 output = av_frame_alloc();
488 return AVERROR(ENOMEM);
490 output->format = output_format;
492 err = av_hwframe_transfer_data(output, input, 0);
494 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
495 "output frame: %d.\n", err);
499 err = av_frame_copy_props(output, input);
501 av_frame_unref(output);
505 av_frame_unref(input);
506 av_frame_move_ref(input, output);
507 av_frame_free(&output);
512 av_frame_free(&output);
516 int hwaccel_decode_init(AVCodecContext *avctx)
518 InputStream *ist = avctx->opaque;
520 ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
525 int hw_device_setup_for_filter(FilterGraph *fg)
530 // If the user has supplied exactly one hardware device then just
531 // give it straight to every filter for convenience. If more than
532 // one device is available then the user needs to pick one explcitly
533 // with the filter_hw_device option.
534 if (filter_hw_device)
535 dev = filter_hw_device;
536 else if (nb_hw_devices == 1)
542 for (i = 0; i < fg->graph->nb_filters; i++) {
543 fg->graph->filters[i]->hw_device_ctx =
544 av_buffer_ref(dev->device_ref);
545 if (!fg->graph->filters[i]->hw_device_ctx)
546 return AVERROR(ENOMEM);