2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 static int nb_hw_devices;
24 static HWDevice **hw_devices;
26 static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
28 HWDevice *found = NULL;
30 for (i = 0; i < nb_hw_devices; i++) {
31 if (hw_devices[i]->type == type) {
34 found = hw_devices[i];
40 HWDevice *hw_device_get_by_name(const char *name)
43 for (i = 0; i < nb_hw_devices; i++) {
44 if (!strcmp(hw_devices[i]->name, name))
50 static HWDevice *hw_device_add(void)
53 err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
59 hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
60 if (!hw_devices[nb_hw_devices])
62 return hw_devices[nb_hw_devices++];
65 int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
67 // "type=name:device,key=value,key2=value2"
68 // "type:device,key=value,key2=value2"
69 // -> av_hwdevice_ctx_create()
72 // -> av_hwdevice_ctx_create_derived()
74 AVDictionary *options = NULL;
75 char *type_name = NULL, *name = NULL, *device = NULL;
76 enum AVHWDeviceType type;
78 AVBufferRef *device_ref;
80 const char *errmsg, *p, *q;
83 k = strcspn(arg, ":=@");
86 type_name = av_strndup(arg, k);
88 err = AVERROR(ENOMEM);
91 type = av_hwdevice_find_type_by_name(type_name);
92 if (type == AV_HWDEVICE_TYPE_NONE) {
93 errmsg = "unknown device type";
98 k = strcspn(p + 1, ":@");
100 name = av_strndup(p + 1, k);
102 err = AVERROR(ENOMEM);
105 if (hw_device_get_by_name(name)) {
106 errmsg = "named device already exists";
112 // Give the device an automatic name of the form "type%d".
113 // We arbitrarily limit at 1000 anonymous devices of the same
114 // type - there is probably something else very wrong if you
115 // get to this limit.
117 int index, index_limit = 1000;
118 index_pos = strlen(type_name);
119 name = av_malloc(index_pos + 4);
121 err = AVERROR(ENOMEM);
124 for (index = 0; index < index_limit; index++) {
125 snprintf(name, index_pos + 4, "%s%d", type_name, index);
126 if (!hw_device_get_by_name(name))
129 if (index >= index_limit) {
130 errmsg = "too many devices";
136 // New device with no parameters.
137 err = av_hwdevice_ctx_create(&device_ref, type,
142 } else if (*p == ':') {
143 // New device with some parameters.
147 device = av_strndup(p, q - p);
149 err = AVERROR(ENOMEM);
152 err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
154 errmsg = "failed to parse options";
159 err = av_hwdevice_ctx_create(&device_ref, type,
160 device ? device : p, options, 0);
164 } else if (*p == '@') {
165 // Derive from existing device.
167 src = hw_device_get_by_name(p + 1);
169 errmsg = "invalid source device name";
173 err = av_hwdevice_ctx_create_derived(&device_ref, type,
178 errmsg = "parse error";
182 dev = hw_device_add();
184 err = AVERROR(ENOMEM);
190 dev->device_ref = device_ref;
198 av_freep(&type_name);
201 av_dict_free(&options);
204 av_log(NULL, AV_LOG_ERROR,
205 "Invalid device specification \"%s\": %s\n", arg, errmsg);
206 err = AVERROR(EINVAL);
209 av_log(NULL, AV_LOG_ERROR,
210 "Device creation failed: %d.\n", err);
214 void hw_device_free_all(void)
217 for (i = 0; i < nb_hw_devices; i++) {
218 av_freep(&hw_devices[i]->name);
219 av_buffer_unref(&hw_devices[i]->device_ref);
220 av_freep(&hw_devices[i]);
222 av_freep(&hw_devices);
226 static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
229 if (hwaccel_id == HWACCEL_NONE)
230 return AV_HWDEVICE_TYPE_NONE;
231 for (i = 0; hwaccels[i].name; i++) {
232 if (hwaccels[i].id == hwaccel_id)
233 return hwaccels[i].device_type;
235 return AV_HWDEVICE_TYPE_NONE;
238 static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
240 const char *type_name;
241 enum AVHWDeviceType type;
242 for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
243 type != AV_HWDEVICE_TYPE_NONE;
244 type = av_hwdevice_iterate_types(type)) {
245 type_name = av_hwdevice_get_type_name(type);
246 if (strstr(codec_name, type_name))
249 return AV_HWDEVICE_TYPE_NONE;
252 int hw_device_setup_for_decode(InputStream *ist)
254 enum AVHWDeviceType type;
256 const char *type_name;
259 if (ist->hwaccel_device) {
260 dev = hw_device_get_by_name(ist->hwaccel_device);
264 type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
265 if (type == AV_HWDEVICE_TYPE_NONE) {
266 // No match - this isn't necessarily invalid, though,
267 // because an explicit device might not be needed or
268 // the hwaccel setup could be handled elsewhere.
271 type_name = av_hwdevice_get_type_name(type);
272 len = strlen(type_name) + 1 +
273 strlen(ist->hwaccel_device) + 1;
274 tmp = av_malloc(len);
276 return AVERROR(ENOMEM);
277 snprintf(tmp, len, "%s:%s", type_name, ist->hwaccel_device);
278 err = hw_device_init_from_string(tmp, &dev);
284 if (ist->hwaccel_id != HWACCEL_NONE)
285 type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
287 type = hw_device_match_type_in_name(ist->dec->name);
288 if (type != AV_HWDEVICE_TYPE_NONE) {
289 dev = hw_device_get_by_type(type);
291 // No device required.
297 av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
298 "for decoder (device type %s for codec %s).\n",
299 av_hwdevice_get_type_name(type), ist->dec->name);
303 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
304 if (!ist->dec_ctx->hw_device_ctx)
305 return AVERROR(ENOMEM);
310 int hw_device_setup_for_encode(OutputStream *ost)
312 enum AVHWDeviceType type;
315 type = hw_device_match_type_in_name(ost->enc->name);
316 if (type != AV_HWDEVICE_TYPE_NONE) {
317 dev = hw_device_get_by_type(type);
319 av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
320 "for encoder (device type %s for codec %s).\n",
321 av_hwdevice_get_type_name(type), ost->enc->name);
324 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
325 if (!ost->enc_ctx->hw_device_ctx)
326 return AVERROR(ENOMEM);
329 // No device required.
334 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
336 InputStream *ist = avctx->opaque;
337 AVFrame *output = NULL;
338 enum AVPixelFormat output_format = ist->hwaccel_output_format;
341 if (input->format == output_format) {
346 output = av_frame_alloc();
348 return AVERROR(ENOMEM);
350 output->format = output_format;
352 err = av_hwframe_transfer_data(output, input, 0);
354 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
355 "output frame: %d.\n", err);
359 err = av_frame_copy_props(output, input);
361 av_frame_unref(output);
365 av_frame_unref(input);
366 av_frame_move_ref(input, output);
367 av_frame_free(&output);
372 av_frame_free(&output);
376 int hwaccel_decode_init(AVCodecContext *avctx)
378 InputStream *ist = avctx->opaque;
380 ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;