39#include "libavutil/avstring.h"
40#include "libavutil/pixdesc.h"
41#include "libavfilter/buffersink.h"
92 const char *type_name = av_hwdevice_get_type_name(type);
95 int index, index_limit = 1000;
96 index_pos = strlen(type_name);
97 name = av_malloc(index_pos + 4);
100 for (index = 0; index < index_limit; index++) {
101 snprintf(name, index_pos + 4,
"%s%d", type_name, index);
105 if (index >= index_limit) {
123 AVDictionary *options = NULL;
124 const char *type_name = NULL, *name = NULL, *device = NULL;
125 enum AVHWDeviceType type;
127 AVBufferRef *device_ref = NULL;
129 const char *errmsg, *p, *q;
132 k = strcspn(arg,
":=@");
135 type_name = av_strndup(arg, k);
137 err = AVERROR(ENOMEM);
140 type = av_hwdevice_find_type_by_name(type_name);
141 if (type == AV_HWDEVICE_TYPE_NONE) {
142 errmsg =
"unknown device type";
147 k = strcspn(p + 1,
":@,");
149 name = av_strndup(p + 1, k);
151 err = AVERROR(ENOMEM);
155 errmsg =
"named device already exists";
163 err = AVERROR(ENOMEM);
170 err = av_hwdevice_ctx_create(&device_ref, type,
175 }
else if (*p ==
':') {
181 device = av_strndup(p, q - p);
183 err = AVERROR(ENOMEM);
187 err = av_dict_parse_string(&options, q + 1,
"=",
",", 0);
189 errmsg =
"failed to parse options";
194 err = av_hwdevice_ctx_create(&device_ref, type,
195 q ? device : p[0] ? p : NULL,
200 }
else if (*p ==
'@') {
205 errmsg =
"invalid source device name";
209 err = av_hwdevice_ctx_create_derived(&device_ref, type,
213 }
else if (*p ==
',') {
214 err = av_dict_parse_string(&options, p + 1,
"=",
",", 0);
217 errmsg =
"failed to parse options";
221 err = av_hwdevice_ctx_create(&device_ref, type,
226 errmsg =
"parse error";
232 err = AVERROR(ENOMEM);
246 av_freep(&type_name);
249 av_dict_free(&options);
252 av_log(NULL, AV_LOG_ERROR,
253 "Invalid device specification \"%s\": %s\n", arg, errmsg);
254 err = AVERROR(EINVAL);
257 av_log(NULL, AV_LOG_ERROR,
258 "Device creation failed: %d.\n", err);
259 av_buffer_unref(&device_ref);
267 AVBufferRef *device_ref = NULL;
274 err = AVERROR(ENOMEM);
278 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
280 av_log(NULL, AV_LOG_ERROR,
281 "Device creation failed: %d.\n", err);
287 err = AVERROR(ENOMEM);
302 av_buffer_unref(&device_ref);
320 const AVCodecHWConfig *config;
324 config = avcodec_get_hw_config(codec, i);
327 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
337 const AVCodecHWConfig *config;
338 enum AVHWDeviceType type;
340 int err, auto_device = 0;
360 av_log(ist->
dec_ctx, AV_LOG_ERROR,
"Invalid hwaccel device "
361 "specified for decoder: device %s of type %s is not "
362 "usable with hwaccel %s.\n", dev->
name,
363 av_hwdevice_get_type_name(dev->
type),
365 return AVERROR(EINVAL);
383 if (!dev && type == AV_HWDEVICE_TYPE_QSV)
400 if (!avcodec_get_hw_config(ist->
dec, 0)) {
404 for (i = 0; !dev; i++) {
405 config = avcodec_get_hw_config(ist->
dec, i);
408 type = config->device_type;
411 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Using auto "
412 "hwaccel type %s with existing device %s.\n",
413 av_hwdevice_get_type_name(type), dev->
name);
416 for (i = 0; !dev; i++) {
417 config = avcodec_get_hw_config(ist->
dec, i);
420 type = config->device_type;
429 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Using auto "
430 "hwaccel type %s with new device created "
431 "from %s.\n", av_hwdevice_get_type_name(type),
434 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Using auto "
435 "hwaccel type %s with new default device.\n",
436 av_hwdevice_get_type_name(type));
442 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Auto hwaccel "
443 "disabled: no device found.\n");
450 av_log(ist->
dec_ctx, AV_LOG_ERROR,
"No device available "
451 "for decoder: device type %s needed for codec %s.\n",
452 av_hwdevice_get_type_name(type), ist->
dec->name);
457 if (!ist->
dec_ctx->hw_device_ctx)
458 return AVERROR(ENOMEM);
465 const AVCodecHWConfig *config;
467 AVBufferRef *frames_ref = NULL;
471 frames_ref = av_buffersink_get_hw_frames_ctx(ost->
filter->
filter);
473 ((AVHWFramesContext*)frames_ref->data)->format ==
482 config = avcodec_get_hw_config(ost->
enc, i);
487 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
488 (config->pix_fmt == AV_PIX_FMT_NONE ||
489 config->pix_fmt == ost->
enc_ctx->pix_fmt)) {
490 av_log(ost->
enc_ctx, AV_LOG_VERBOSE,
"Using input "
491 "frames context (format %s) with %s encoder.\n",
492 av_get_pix_fmt_name(ost->
enc_ctx->pix_fmt),
494 ost->
enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
495 if (!ost->
enc_ctx->hw_frames_ctx)
496 return AVERROR(ENOMEM);
501 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
506 av_log(ost->
enc_ctx, AV_LOG_VERBOSE,
"Using device %s "
507 "(type %s) with %s encoder.\n", dev->
name,
508 av_hwdevice_get_type_name(dev->
type), ost->
enc->name);
510 if (!ost->
enc_ctx->hw_device_ctx)
511 return AVERROR(ENOMEM);
521 AVFrame *output = NULL;
525 if (input->format == output_format) {
530 output = av_frame_alloc();
532 return AVERROR(ENOMEM);
534 output->format = output_format;
536 err = av_hwframe_transfer_data(output, input, 0);
538 av_log(avctx, AV_LOG_ERROR,
"Failed to transfer data to "
539 "output frame: %d.\n", err);
543 err = av_frame_copy_props(output, input);
545 av_frame_unref(output);
549 av_frame_unref(input);
550 av_frame_move_ref(input, output);
551 av_frame_free(&output);
556 av_frame_free(&output);
582 av_log(NULL, AV_LOG_WARNING,
"There are %d hardware devices. device "
583 "%s of type %s is picked for filters by default. Set hardware "
584 "device explicitly with the filter_hw_device option if device "
585 "%s is not usable for filters.\n",
587 av_hwdevice_get_type_name(dev->
type), dev->
name);
592 for (i = 0; i < fg->
graph->nb_filters; i++) {
593 fg->
graph->filters[i]->hw_device_ctx =
595 if (!fg->
graph->filters[i]->hw_device_ctx)
596 return AVERROR(ENOMEM);