46#include "libavutil/avstring.h"
47#include "libavutil/pixdesc.h"
48#include "libavfilter/buffersink.h"
99 const char *type_name = av_hwdevice_get_type_name(type);
102 int index, index_limit = 1000;
103 index_pos = strlen(type_name);
104 name = av_malloc(index_pos + 4);
107 for (index = 0; index < index_limit; index++) {
108 snprintf(name, index_pos + 4,
"%s%d", type_name, index);
112 if (index >= index_limit) {
130 AVDictionary *options = NULL;
131 const char *type_name = NULL, *name = NULL, *device = NULL;
132 enum AVHWDeviceType type;
134 AVBufferRef *device_ref = NULL;
136 const char *errmsg, *p, *q;
139 k = strcspn(arg,
":=@");
142 type_name = av_strndup(arg, k);
144 err = AVERROR(ENOMEM);
147 type = av_hwdevice_find_type_by_name(type_name);
148 if (type == AV_HWDEVICE_TYPE_NONE) {
149 errmsg =
"unknown device type";
154 k = strcspn(p + 1,
":@,");
156 name = av_strndup(p + 1, k);
158 err = AVERROR(ENOMEM);
162 errmsg =
"named device already exists";
170 err = AVERROR(ENOMEM);
177 err = av_hwdevice_ctx_create(&device_ref, type,
182 }
else if (*p ==
':') {
188 device = av_strndup(p, q - p);
190 err = AVERROR(ENOMEM);
194 err = av_dict_parse_string(&options, q + 1,
"=",
",", 0);
196 errmsg =
"failed to parse options";
201 err = av_hwdevice_ctx_create(&device_ref, type,
202 q ? device : p[0] ? p : NULL,
207 }
else if (*p ==
'@') {
212 errmsg =
"invalid source device name";
216 err = av_hwdevice_ctx_create_derived(&device_ref, type,
220 }
else if (*p ==
',') {
221 err = av_dict_parse_string(&options, p + 1,
"=",
",", 0);
224 errmsg =
"failed to parse options";
228 err = av_hwdevice_ctx_create(&device_ref, type,
233 errmsg =
"parse error";
239 err = AVERROR(ENOMEM);
253 av_freep(&type_name);
256 av_dict_free(&options);
259 av_log(NULL, AV_LOG_ERROR,
260 "Invalid device specification \"%s\": %s\n", arg, errmsg);
261 err = AVERROR(EINVAL);
264 av_log(NULL, AV_LOG_ERROR,
265 "Device creation failed: %d.\n", err);
266 av_buffer_unref(&device_ref);
274 AVBufferRef *device_ref = NULL;
281 err = AVERROR(ENOMEM);
285 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
287 av_log(NULL, AV_LOG_ERROR,
288 "Device creation failed: %d.\n", err);
294 err = AVERROR(ENOMEM);
309 av_buffer_unref(&device_ref);
327 const AVCodecHWConfig *config;
331 config = avcodec_get_hw_config(codec, i);
334 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
344 const AVCodecHWConfig *config;
345 enum AVHWDeviceType type;
347 int err, auto_device = 0;
367 av_log(NULL, AV_LOG_ERROR,
"Invalid hwaccel device "
368 "specified for decoder: device %s of type %s is not "
369 "usable with hwaccel %s.\n", dev->
name,
370 av_hwdevice_get_type_name(dev->
type),
372 return AVERROR(EINVAL);
390 if (!dev && type == AV_HWDEVICE_TYPE_QSV)
407 if (!avcodec_get_hw_config(ist->
dec, 0)) {
411 for (i = 0; !dev; i++) {
412 config = avcodec_get_hw_config(ist->
dec, i);
415 type = config->device_type;
418 av_log(NULL, AV_LOG_INFO,
"Using auto "
419 "hwaccel type %s with existing device %s.\n",
420 av_hwdevice_get_type_name(type), dev->
name);
423 for (i = 0; !dev; i++) {
424 config = avcodec_get_hw_config(ist->
dec, i);
427 type = config->device_type;
436 av_log(NULL, AV_LOG_INFO,
"Using auto "
437 "hwaccel type %s with new device created "
438 "from %s.\n", av_hwdevice_get_type_name(type),
441 av_log(NULL, AV_LOG_INFO,
"Using auto "
442 "hwaccel type %s with new default device.\n",
443 av_hwdevice_get_type_name(type));
449 av_log(NULL, AV_LOG_INFO,
"Auto hwaccel "
450 "disabled: no device found.\n");
457 av_log(NULL, AV_LOG_ERROR,
"No device available "
458 "for decoder: device type %s needed for codec %s.\n",
459 av_hwdevice_get_type_name(type), ist->
dec->name);
464 if (!ist->
dec_ctx->hw_device_ctx)
465 return AVERROR(ENOMEM);
472 const AVCodecHWConfig *config;
474 AVBufferRef *frames_ref = NULL;
478 frames_ref = av_buffersink_get_hw_frames_ctx(ost->
filter->
filter);
480 ((AVHWFramesContext*)frames_ref->data)->format ==
489 config = avcodec_get_hw_config(ost->
enc_ctx->codec, i);
494 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
495 (config->pix_fmt == AV_PIX_FMT_NONE ||
496 config->pix_fmt == ost->
enc_ctx->pix_fmt)) {
497 av_log(ost->
enc_ctx, AV_LOG_VERBOSE,
"Using input "
498 "frames context (format %s) with %s encoder.\n",
499 av_get_pix_fmt_name(ost->
enc_ctx->pix_fmt),
501 ost->
enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
502 if (!ost->
enc_ctx->hw_frames_ctx)
503 return AVERROR(ENOMEM);
508 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
513 av_log(ost->
enc_ctx, AV_LOG_VERBOSE,
"Using device %s "
514 "(type %s) with %s encoder.\n", dev->
name,
515 av_hwdevice_get_type_name(dev->
type), ost->
enc_ctx->codec->name);
517 if (!ost->
enc_ctx->hw_device_ctx)
518 return AVERROR(ENOMEM);
528 AVFrame *output = NULL;
532 if (input->format == output_format) {
537 output = av_frame_alloc();
539 return AVERROR(ENOMEM);
541 output->format = output_format;
543 err = av_hwframe_transfer_data(output, input, 0);
545 av_log(avctx, AV_LOG_ERROR,
"Failed to transfer data to "
546 "output frame: %d.\n", err);
550 err = av_frame_copy_props(output, input);
552 av_frame_unref(output);
556 av_frame_unref(input);
557 av_frame_move_ref(input, output);
558 av_frame_free(&output);
563 av_frame_free(&output);
589 av_log(NULL, AV_LOG_WARNING,
"There are %d hardware devices. device "
590 "%s of type %s is picked for filters by default. Set hardware "
591 "device explicitly with the filter_hw_device option if device "
592 "%s is not usable for filters.\n",
594 av_hwdevice_get_type_name(dev->
type), dev->
name);
599 for (i = 0; i < fg->
graph->nb_filters; i++) {
600 fg->
graph->filters[i]->hw_device_ctx =
602 if (!fg->
graph->filters[i]->hw_device_ctx)
603 return AVERROR(ENOMEM);