2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avstring.h"
25 static int nb_hw_devices;
26 static HWDevice **hw_devices;
28 static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
30 HWDevice *found = NULL;
32 for (i = 0; i < nb_hw_devices; i++) {
33 if (hw_devices[i]->type == type) {
36 found = hw_devices[i];
42 HWDevice *hw_device_get_by_name(const char *name)
45 for (i = 0; i < nb_hw_devices; i++) {
46 if (!strcmp(hw_devices[i]->name, name))
52 static HWDevice *hw_device_add(void)
55 err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
61 hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
62 if (!hw_devices[nb_hw_devices])
64 return hw_devices[nb_hw_devices++];
67 int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
69 // "type=name:device,key=value,key2=value2"
70 // "type:device,key=value,key2=value2"
71 // -> av_hwdevice_ctx_create()
74 // -> av_hwdevice_ctx_create_derived()
76 AVDictionary *options = NULL;
77 char *type_name = NULL, *name = NULL, *device = NULL;
78 enum AVHWDeviceType type;
80 AVBufferRef *device_ref = NULL;
82 const char *errmsg, *p, *q;
85 k = strcspn(arg, ":=@");
88 type_name = av_strndup(arg, k);
90 err = AVERROR(ENOMEM);
93 type = av_hwdevice_find_type_by_name(type_name);
94 if (type == AV_HWDEVICE_TYPE_NONE) {
95 errmsg = "unknown device type";
100 k = strcspn(p + 1, ":@");
102 name = av_strndup(p + 1, k);
104 err = AVERROR(ENOMEM);
107 if (hw_device_get_by_name(name)) {
108 errmsg = "named device already exists";
114 // Give the device an automatic name of the form "type%d".
115 // We arbitrarily limit at 1000 anonymous devices of the same
116 // type - there is probably something else very wrong if you
117 // get to this limit.
119 int index, index_limit = 1000;
120 index_pos = strlen(type_name);
121 name = av_malloc(index_pos + 4);
123 err = AVERROR(ENOMEM);
126 for (index = 0; index < index_limit; index++) {
127 snprintf(name, index_pos + 4, "%s%d", type_name, index);
128 if (!hw_device_get_by_name(name))
131 if (index >= index_limit) {
132 errmsg = "too many devices";
138 // New device with no parameters.
139 err = av_hwdevice_ctx_create(&device_ref, type,
144 } else if (*p == ':') {
145 // New device with some parameters.
149 device = av_strndup(p, q - p);
151 err = AVERROR(ENOMEM);
154 err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
156 errmsg = "failed to parse options";
161 err = av_hwdevice_ctx_create(&device_ref, type,
162 device ? device : p, options, 0);
166 } else if (*p == '@') {
167 // Derive from existing device.
169 src = hw_device_get_by_name(p + 1);
171 errmsg = "invalid source device name";
175 err = av_hwdevice_ctx_create_derived(&device_ref, type,
180 errmsg = "parse error";
184 dev = hw_device_add();
186 err = AVERROR(ENOMEM);
192 dev->device_ref = device_ref;
200 av_freep(&type_name);
203 av_dict_free(&options);
206 av_log(NULL, AV_LOG_ERROR,
207 "Invalid device specification \"%s\": %s\n", arg, errmsg);
208 err = AVERROR(EINVAL);
211 av_log(NULL, AV_LOG_ERROR,
212 "Device creation failed: %d.\n", err);
213 av_buffer_unref(&device_ref);
217 void hw_device_free_all(void)
220 for (i = 0; i < nb_hw_devices; i++) {
221 av_freep(&hw_devices[i]->name);
222 av_buffer_unref(&hw_devices[i]->device_ref);
223 av_freep(&hw_devices[i]);
225 av_freep(&hw_devices);
229 static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
232 if (hwaccel_id == HWACCEL_NONE)
233 return AV_HWDEVICE_TYPE_NONE;
234 for (i = 0; hwaccels[i].name; i++) {
235 if (hwaccels[i].id == hwaccel_id)
236 return hwaccels[i].device_type;
238 return AV_HWDEVICE_TYPE_NONE;
241 static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
243 const char *type_name;
244 enum AVHWDeviceType type;
245 for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
246 type != AV_HWDEVICE_TYPE_NONE;
247 type = av_hwdevice_iterate_types(type)) {
248 type_name = av_hwdevice_get_type_name(type);
249 if (strstr(codec_name, type_name))
252 return AV_HWDEVICE_TYPE_NONE;
255 int hw_device_setup_for_decode(InputStream *ist)
257 enum AVHWDeviceType type;
261 if (ist->hwaccel_device) {
262 dev = hw_device_get_by_name(ist->hwaccel_device);
265 type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
266 if (type == AV_HWDEVICE_TYPE_NONE) {
267 // No match - this isn't necessarily invalid, though,
268 // because an explicit device might not be needed or
269 // the hwaccel setup could be handled elsewhere.
272 tmp = av_asprintf("%s:%s", av_hwdevice_get_type_name(type),
273 ist->hwaccel_device);
275 return AVERROR(ENOMEM);
276 err = hw_device_init_from_string(tmp, &dev);
282 if (ist->hwaccel_id != HWACCEL_NONE)
283 type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
285 type = hw_device_match_type_in_name(ist->dec->name);
286 if (type != AV_HWDEVICE_TYPE_NONE) {
287 dev = hw_device_get_by_type(type);
289 hw_device_init_from_string(av_hwdevice_get_type_name(type),
293 // No device required.
299 av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
300 "for decoder (device type %s for codec %s).\n",
301 av_hwdevice_get_type_name(type), ist->dec->name);
305 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
306 if (!ist->dec_ctx->hw_device_ctx)
307 return AVERROR(ENOMEM);
312 int hw_device_setup_for_encode(OutputStream *ost)
314 enum AVHWDeviceType type;
317 type = hw_device_match_type_in_name(ost->enc->name);
318 if (type != AV_HWDEVICE_TYPE_NONE) {
319 dev = hw_device_get_by_type(type);
321 av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
322 "for encoder (device type %s for codec %s).\n",
323 av_hwdevice_get_type_name(type), ost->enc->name);
326 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
327 if (!ost->enc_ctx->hw_device_ctx)
328 return AVERROR(ENOMEM);
331 // No device required.
336 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
338 InputStream *ist = avctx->opaque;
339 AVFrame *output = NULL;
340 enum AVPixelFormat output_format = ist->hwaccel_output_format;
343 if (input->format == output_format) {
348 output = av_frame_alloc();
350 return AVERROR(ENOMEM);
352 output->format = output_format;
354 err = av_hwframe_transfer_data(output, input, 0);
356 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
357 "output frame: %d.\n", err);
361 err = av_frame_copy_props(output, input);
363 av_frame_unref(output);
367 av_frame_unref(input);
368 av_frame_move_ref(input, output);
369 av_frame_free(&output);
374 av_frame_free(&output);
378 int hwaccel_decode_init(AVCodecContext *avctx)
380 InputStream *ist = avctx->opaque;
382 ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;