30 #include "libavutil/avstring.h"
81 const char *type_name = av_hwdevice_get_type_name(type);
84 int index, index_limit = 1000;
85 index_pos = strlen(type_name);
86 name = av_malloc(index_pos + 4);
89 for (index = 0; index < index_limit; index++) {
90 snprintf(name, index_pos + 4,
"%s%d", type_name, index);
94 if (index >= index_limit) {
110 AVDictionary *options = NULL;
111 const char *type_name = NULL, *name = NULL, *device = NULL;
112 enum AVHWDeviceType type;
114 AVBufferRef *device_ref = NULL;
116 const char *errmsg, *p, *q;
119 k = strcspn(arg,
":=@");
122 type_name = av_strndup(arg, k);
124 err = AVERROR(ENOMEM);
127 type = av_hwdevice_find_type_by_name(type_name);
128 if (type == AV_HWDEVICE_TYPE_NONE) {
129 errmsg =
"unknown device type";
134 k = strcspn(p + 1,
":@");
136 name = av_strndup(p + 1, k);
138 err = AVERROR(ENOMEM);
142 errmsg =
"named device already exists";
150 err = AVERROR(ENOMEM);
157 err = av_hwdevice_ctx_create(&device_ref, type,
162 }
else if (*p ==
':') {
168 device = av_strndup(p, q - p);
170 err = AVERROR(ENOMEM);
174 err = av_dict_parse_string(&options, q + 1,
"=",
",", 0);
176 errmsg =
"failed to parse options";
181 err = av_hwdevice_ctx_create(&device_ref, type,
182 q ? device : p[0] ? p : NULL,
187 }
else if (*p ==
'@') {
192 errmsg =
"invalid source device name";
196 err = av_hwdevice_ctx_create_derived(&device_ref, type,
201 errmsg =
"parse error";
207 err = AVERROR(ENOMEM);
221 av_freep(&type_name);
224 av_dict_free(&options);
228 "Invalid device specification \"%s\": %s\n", arg, errmsg);
229 err = AVERROR(EINVAL);
233 "Device creation failed: %d.\n", err);
234 av_buffer_unref(&device_ref);
242 AVBufferRef *device_ref = NULL;
249 err = AVERROR(ENOMEM);
253 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
256 "Device creation failed: %d.\n", err);
262 err = AVERROR(ENOMEM);
277 av_buffer_unref(&device_ref);
295 const AVCodecHWConfig *config;
299 config = avcodec_get_hw_config(codec, i);
302 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
312 const AVCodecHWConfig *config;
313 enum AVHWDeviceType type;
315 int err, auto_device = 0;
336 "specified for decoder: device %s of type %s is not "
337 "usable with hwaccel %s.\n", dev->
name,
338 av_hwdevice_get_type_name(dev->
type),
340 return AVERROR(EINVAL);
363 if (!avcodec_get_hw_config(ist->
dec, 0)) {
367 for (i = 0; !dev; i++) {
368 config = avcodec_get_hw_config(ist->
dec, i);
371 type = config->device_type;
375 "hwaccel type %s with existing device %s.\n",
376 av_hwdevice_get_type_name(type), dev->
name);
379 for (i = 0; !dev; i++) {
380 config = avcodec_get_hw_config(ist->
dec, i);
383 type = config->device_type;
393 "hwaccel type %s with new device created "
394 "from %s.\n", av_hwdevice_get_type_name(type),
398 "hwaccel type %s with new default device.\n",
399 av_hwdevice_get_type_name(type));
406 "disabled: no device found.\n");
414 "for decoder: device type %s needed for codec %s.\n",
415 av_hwdevice_get_type_name(type), ist->
dec->name);
420 if (!ist->
dec_ctx->hw_device_ctx)
421 return AVERROR(ENOMEM);
433 if (!ost->
enc_ctx->hw_device_ctx)
434 return AVERROR(ENOMEM);
445 AVFrame *output = NULL;
449 if (input->format == output_format) {
454 output = av_frame_alloc();
456 return AVERROR(ENOMEM);
458 output->format = output_format;
460 err = av_hwframe_transfer_data(output, input, 0);
462 av_log(avctx,
AV_LOG_ERROR,
"Failed to transfer data to "
463 "output frame: %d.\n", err);
467 err = av_frame_copy_props(output, input);
469 av_frame_unref(output);
473 av_frame_unref(input);
474 av_frame_move_ref(input, output);
475 av_frame_free(&output);
480 av_frame_free(&output);