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);
91 for (index = 0; index < index_limit; index++) {
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;
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),
342 return AVERROR(EINVAL);
365 if (!avcodec_get_hw_config(ist->
dec, 0)) {
369 for (i = 0; !dev; i++) {
370 config = avcodec_get_hw_config(ist->
dec, i);
373 type = config->device_type;
376 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Using auto "
377 "hwaccel type %s with existing device %s.\n",
378 av_hwdevice_get_type_name(type), dev->
name);
381 for (i = 0; !dev; i++) {
382 config = avcodec_get_hw_config(ist->
dec, i);
385 type = config->device_type;
394 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Using auto "
395 "hwaccel type %s with new device created "
396 "from %s.\n", av_hwdevice_get_type_name(type),
399 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Using auto "
400 "hwaccel type %s with new default device.\n",
401 av_hwdevice_get_type_name(type));
407 av_log(ist->
dec_ctx, AV_LOG_INFO,
"Auto hwaccel "
408 "disabled: no device found.\n");
415 av_log(ist->
dec_ctx, AV_LOG_ERROR,
"No device available "
416 "for decoder: device type %s needed for codec %s.\n",
417 av_hwdevice_get_type_name(type), ist->
dec->name);
422 if (!ist->
dec_ctx->hw_device_ctx)
423 return AVERROR(ENOMEM);
430 const AVCodecHWConfig *config;
432 AVBufferRef *frames_ref = NULL;
436 frames_ref = av_buffersink_get_hw_frames_ctx(ost->
filter->
filter);
438 ((AVHWFramesContext*)frames_ref->data)->format ==
447 config = avcodec_get_hw_config(ost->
enc, i);
452 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
453 (config->pix_fmt == AV_PIX_FMT_NONE ||
454 config->pix_fmt == ost->
enc_ctx->pix_fmt)) {
455 av_log(ost->
enc_ctx, AV_LOG_VERBOSE,
"Using input "
456 "frames context (format %s) with %s encoder.\n",
457 av_get_pix_fmt_name(ost->
enc_ctx->pix_fmt),
459 ost->
enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
460 if (!ost->
enc_ctx->hw_frames_ctx)
461 return AVERROR(ENOMEM);
466 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
471 av_log(ost->
enc_ctx, AV_LOG_VERBOSE,
"Using device %s "
472 "(type %s) with %s encoder.\n", dev->
name,
473 av_hwdevice_get_type_name(dev->
type), ost->
enc->name);
475 if (!ost->
enc_ctx->hw_device_ctx)
476 return AVERROR(ENOMEM);
486 AVFrame *output = NULL;
490 if (input->format == output_format) {
495 output = av_frame_alloc();
497 return AVERROR(ENOMEM);
499 output->format = output_format;
501 err = av_hwframe_transfer_data(output, input, 0);
503 av_log(avctx, AV_LOG_ERROR,
"Failed to transfer data to "
504 "output frame: %d.\n", err);
508 err = av_frame_copy_props(output, input);
510 av_frame_unref(output);
514 av_frame_unref(input);
515 av_frame_move_ref(input, output);
516 av_frame_free(&output);
521 av_frame_free(&output);
551 for (i = 0; i < fg->
graph->nb_filters; i++) {
552 fg->
graph->filters[i]->hw_device_ctx =
554 if (!fg->
graph->filters[i]->hw_device_ctx)
555 return AVERROR(ENOMEM);