30#include "libavutil/avstring.h"
31#include "libavutil/pixdesc.h"
32#include "libavfilter/buffersink.h"
83 const char *type_name = av_hwdevice_get_type_name(type);
86 int index, index_limit = 1000;
87 index_pos = strlen(type_name);
88 name = av_malloc(index_pos + 4);
92 snprintf(name, index_pos + 4,
"%s%d", type_name,
index);
96 if (
index >= index_limit) {
112 AVDictionary *options = NULL;
113 const char *type_name = NULL, *name = NULL, *device = NULL;
114 enum AVHWDeviceType type;
116 AVBufferRef *device_ref = NULL;
118 const char *errmsg, *p, *q;
121 k = strcspn(arg,
":=@");
124 type_name = av_strndup(arg, k);
126 err = AVERROR(ENOMEM);
129 type = av_hwdevice_find_type_by_name(type_name);
130 if (type == AV_HWDEVICE_TYPE_NONE) {
131 errmsg =
"unknown device type";
136 k = strcspn(p + 1,
":@");
138 name = av_strndup(p + 1, k);
140 err = AVERROR(ENOMEM);
144 errmsg =
"named device already exists";
152 err = AVERROR(ENOMEM);
159 err = av_hwdevice_ctx_create(&device_ref, type,
164 }
else if (*p ==
':') {
170 device = av_strndup(p, q - p);
172 err = AVERROR(ENOMEM);
176 err = av_dict_parse_string(&options, q + 1,
"=",
",", 0);
178 errmsg =
"failed to parse options";
183 err = av_hwdevice_ctx_create(&device_ref, type,
184 q ? device : p[0] ? p : NULL,
189 }
else if (*p ==
'@') {
194 errmsg =
"invalid source device name";
198 err = av_hwdevice_ctx_create_derived(&device_ref, type,
203 errmsg =
"parse error";
209 err = AVERROR(ENOMEM);
223 av_freep(&type_name);
226 av_dict_free(&options);
229 av_log(NULL, AV_LOG_ERROR,
230 "Invalid device specification \"%s\": %s\n", arg, errmsg);
231 err = AVERROR(EINVAL);
234 av_log(NULL, AV_LOG_ERROR,
235 "Device creation failed: %d.\n", err);
236 av_buffer_unref(&device_ref);
244 AVBufferRef *device_ref = NULL;
251 err = AVERROR(ENOMEM);
255 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
257 av_log(NULL, AV_LOG_ERROR,
258 "Device creation failed: %d.\n", err);
264 err = AVERROR(ENOMEM);
279 av_buffer_unref(&device_ref);
297 const AVCodecHWConfig *config;
301 config = avcodec_get_hw_config(codec, i);
304 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
314 const AVCodecHWConfig *config;
315 enum AVHWDeviceType type;
317 int err, auto_device = 0;
319 if (
ist->hwaccel_device) {
325 type =
ist->hwaccel_device_type;
335 ist->hwaccel_device_type = dev->
type;
336 }
else if (
ist->hwaccel_device_type != dev->
type) {
337 av_log(
ist->dec_ctx, AV_LOG_ERROR,
"Invalid hwaccel device "
338 "specified for decoder: device %s of type %s is not "
339 "usable with hwaccel %s.\n", dev->
name,
340 av_hwdevice_get_type_name(dev->
type),
341 av_hwdevice_get_type_name(
ist->hwaccel_device_type));
342 return AVERROR(EINVAL);
349 type =
ist->hwaccel_device_type;
360 if (!dev && type == AV_HWDEVICE_TYPE_QSV)
377 if (!avcodec_get_hw_config(
ist->dec, 0)) {
381 for (i = 0; !dev; i++) {
382 config = avcodec_get_hw_config(
ist->dec, i);
385 type = config->device_type;
388 av_log(
ist->dec_ctx, AV_LOG_INFO,
"Using auto "
389 "hwaccel type %s with existing device %s.\n",
390 av_hwdevice_get_type_name(type), dev->
name);
393 for (i = 0; !dev; i++) {
394 config = avcodec_get_hw_config(
ist->dec, i);
397 type = config->device_type;
405 if (
ist->hwaccel_device) {
406 av_log(
ist->dec_ctx, AV_LOG_INFO,
"Using auto "
407 "hwaccel type %s with new device created "
408 "from %s.\n", av_hwdevice_get_type_name(type),
409 ist->hwaccel_device);
411 av_log(
ist->dec_ctx, AV_LOG_INFO,
"Using auto "
412 "hwaccel type %s with new default device.\n",
413 av_hwdevice_get_type_name(type));
417 ist->hwaccel_device_type = type;
419 av_log(
ist->dec_ctx, AV_LOG_INFO,
"Auto hwaccel "
420 "disabled: no device found.\n");
427 av_log(
ist->dec_ctx, AV_LOG_ERROR,
"No device available "
428 "for decoder: device type %s needed for codec %s.\n",
429 av_hwdevice_get_type_name(type),
ist->dec->name);
433 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->
device_ref);
434 if (!
ist->dec_ctx->hw_device_ctx)
435 return AVERROR(ENOMEM);
442 const AVCodecHWConfig *config;
444 AVBufferRef *frames_ref = NULL;
450 ((AVHWFramesContext*)frames_ref->data)->format ==
459 config = avcodec_get_hw_config(
ost->
enc, i);
464 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
465 (config->pix_fmt == AV_PIX_FMT_NONE ||
467 av_log(
ost->
enc_ctx, AV_LOG_VERBOSE,
"Using input "
468 "frames context (format %s) with %s encoder.\n",
471 ost->
enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
473 return AVERROR(ENOMEM);
478 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
483 av_log(
ost->
enc_ctx, AV_LOG_VERBOSE,
"Using device %s "
484 "(type %s) with %s encoder.\n", dev->
name,
485 av_hwdevice_get_type_name(dev->
type),
ost->
enc->name);
488 return AVERROR(ENOMEM);
498 AVFrame *output = NULL;
499 enum AVPixelFormat output_format =
ist->hwaccel_output_format;
502 if (input->format == output_format) {
507 output = av_frame_alloc();
509 return AVERROR(ENOMEM);
511 output->format = output_format;
513 err = av_hwframe_transfer_data(output, input, 0);
515 av_log(avctx, AV_LOG_ERROR,
"Failed to transfer data to "
516 "output frame: %d.\n", err);
520 err = av_frame_copy_props(output, input);
522 av_frame_unref(output);
526 av_frame_unref(input);
527 av_frame_move_ref(input, output);
528 av_frame_free(&output);
533 av_frame_free(&output);
563 for (i = 0; i < fg->
graph->nb_filters; i++) {
564 fg->
graph->filters[i]->hw_device_ctx =
566 if (!fg->
graph->filters[i]->hw_device_ctx)
567 return AVERROR(ENOMEM);