]> git.sesse.net Git - ffmpeg/blob - avtools/avconv_hw.c
configure: Do not treat JACK as a system library
[ffmpeg] / avtools / avconv_hw.c
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <string.h>
20
21 #include "avconv.h"
22
23 static int nb_hw_devices;
24 static HWDevice **hw_devices;
25
26 static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
27 {
28     HWDevice *found = NULL;
29     int i;
30     for (i = 0; i < nb_hw_devices; i++) {
31         if (hw_devices[i]->type == type) {
32             if (found)
33                 return NULL;
34             found = hw_devices[i];
35         }
36     }
37     return found;
38 }
39
40 HWDevice *hw_device_get_by_name(const char *name)
41 {
42     int i;
43     for (i = 0; i < nb_hw_devices; i++) {
44         if (!strcmp(hw_devices[i]->name, name))
45             return hw_devices[i];
46     }
47     return NULL;
48 }
49
50 static HWDevice *hw_device_add(void)
51 {
52     int err;
53     err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
54                             sizeof(*hw_devices));
55     if (err) {
56         nb_hw_devices = 0;
57         return NULL;
58     }
59     hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
60     if (!hw_devices[nb_hw_devices])
61         return NULL;
62     return hw_devices[nb_hw_devices++];
63 }
64
65 int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
66 {
67     // "type=name:device,key=value,key2=value2"
68     // "type:device,key=value,key2=value2"
69     // -> av_hwdevice_ctx_create()
70     // "type=name@name"
71     // "type@name"
72     // -> av_hwdevice_ctx_create_derived()
73
74     AVDictionary *options = NULL;
75     char *type_name = NULL, *name = NULL, *device = NULL;
76     enum AVHWDeviceType type;
77     HWDevice *dev, *src;
78     AVBufferRef *device_ref;
79     int err;
80     const char *errmsg, *p, *q;
81     size_t k;
82
83     k = strcspn(arg, ":=@");
84     p = arg + k;
85
86     type_name = av_strndup(arg, k);
87     if (!type_name) {
88         err = AVERROR(ENOMEM);
89         goto fail;
90     }
91     type = av_hwdevice_find_type_by_name(type_name);
92     if (type == AV_HWDEVICE_TYPE_NONE) {
93         errmsg = "unknown device type";
94         goto invalid;
95     }
96
97     if (*p == '=') {
98         k = strcspn(p + 1, ":@");
99
100         name = av_strndup(p + 1, k);
101         if (!name) {
102             err = AVERROR(ENOMEM);
103             goto fail;
104         }
105         if (hw_device_get_by_name(name)) {
106             errmsg = "named device already exists";
107             goto invalid;
108         }
109
110         p += 1 + k;
111     } else {
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.
116         size_t index_pos;
117         int index, index_limit = 1000;
118         index_pos = strlen(type_name);
119         name = av_malloc(index_pos + 4);
120         if (!name) {
121             err = AVERROR(ENOMEM);
122             goto fail;
123         }
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))
127                 break;
128         }
129         if (index >= index_limit) {
130             errmsg = "too many devices";
131             goto invalid;
132         }
133     }
134
135     if (!*p) {
136         // New device with no parameters.
137         err = av_hwdevice_ctx_create(&device_ref, type,
138                                      NULL, NULL, 0);
139         if (err < 0)
140             goto fail;
141
142     } else if (*p == ':') {
143         // New device with some parameters.
144         ++p;
145         q = strchr(p, ',');
146         if (q) {
147             device = av_strndup(p, q - p);
148             if (!device) {
149                 err = AVERROR(ENOMEM);
150                 goto fail;
151             }
152             err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
153             if (err < 0) {
154                 errmsg = "failed to parse options";
155                 goto invalid;
156             }
157         }
158
159         err = av_hwdevice_ctx_create(&device_ref, type,
160                                      device ? device : p, options, 0);
161         if (err < 0)
162             goto fail;
163
164     } else if (*p == '@') {
165         // Derive from existing device.
166
167         src = hw_device_get_by_name(p + 1);
168         if (!src) {
169             errmsg = "invalid source device name";
170             goto invalid;
171         }
172
173         err = av_hwdevice_ctx_create_derived(&device_ref, type,
174                                              src->device_ref, 0);
175         if (err < 0)
176             goto fail;
177     } else {
178         errmsg = "parse error";
179         goto invalid;
180     }
181
182     dev = hw_device_add();
183     if (!dev) {
184         err = AVERROR(ENOMEM);
185         goto fail;
186     }
187
188     dev->name = name;
189     dev->type = type;
190     dev->device_ref = device_ref;
191
192     if (dev_out)
193         *dev_out = dev;
194
195     name = NULL;
196     err = 0;
197 done:
198     av_freep(&type_name);
199     av_freep(&name);
200     av_freep(&device);
201     av_dict_free(&options);
202     return err;
203 invalid:
204     av_log(NULL, AV_LOG_ERROR,
205            "Invalid device specification \"%s\": %s\n", arg, errmsg);
206     err = AVERROR(EINVAL);
207     goto done;
208 fail:
209     av_log(NULL, AV_LOG_ERROR,
210            "Device creation failed: %d.\n", err);
211     goto done;
212 }
213
214 void hw_device_free_all(void)
215 {
216     int i;
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]);
221     }
222     av_freep(&hw_devices);
223     nb_hw_devices = 0;
224 }
225
226 static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
227 {
228     int i;
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;
234     }
235     return AV_HWDEVICE_TYPE_NONE;
236 }
237
238 static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
239 {
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))
247             return type;
248     }
249     return AV_HWDEVICE_TYPE_NONE;
250 }
251
252 int hw_device_setup_for_decode(InputStream *ist)
253 {
254     enum AVHWDeviceType type;
255     HWDevice *dev;
256     const char *type_name;
257     int err;
258
259     if (ist->hwaccel_device) {
260         dev = hw_device_get_by_name(ist->hwaccel_device);
261         if (!dev) {
262             char *tmp;
263             size_t len;
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.
269                 return 0;
270             }
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);
275             if (!tmp)
276                 return AVERROR(ENOMEM);
277             snprintf(tmp, len, "%s:%s", type_name, ist->hwaccel_device);
278             err = hw_device_init_from_string(tmp, &dev);
279             av_free(tmp);
280             if (err < 0)
281                 return err;
282         }
283     } else {
284         if (ist->hwaccel_id != HWACCEL_NONE)
285             type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
286         else
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);
290         } else {
291             // No device required.
292             return 0;
293         }
294     }
295
296     if (!dev) {
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);
300         return 0;
301     }
302
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);
306
307     return 0;
308 }
309
310 int hw_device_setup_for_encode(OutputStream *ost)
311 {
312     enum AVHWDeviceType type;
313     HWDevice *dev;
314
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);
318         if (!dev) {
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);
322             return 0;
323         }
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);
327         return 0;
328     } else {
329         // No device required.
330         return 0;
331     }
332 }
333
334 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
335 {
336     InputStream *ist = avctx->opaque;
337     AVFrame *output = NULL;
338     enum AVPixelFormat output_format = ist->hwaccel_output_format;
339     int err;
340
341     if (input->format == output_format) {
342         // Nothing to do.
343         return 0;
344     }
345
346     output = av_frame_alloc();
347     if (!output)
348         return AVERROR(ENOMEM);
349
350     output->format = output_format;
351
352     err = av_hwframe_transfer_data(output, input, 0);
353     if (err < 0) {
354         av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
355                "output frame: %d.\n", err);
356         goto fail;
357     }
358
359     err = av_frame_copy_props(output, input);
360     if (err < 0) {
361         av_frame_unref(output);
362         goto fail;
363     }
364
365     av_frame_unref(input);
366     av_frame_move_ref(input, output);
367     av_frame_free(&output);
368
369     return 0;
370
371 fail:
372     av_frame_free(&output);
373     return err;
374 }
375
376 int hwaccel_decode_init(AVCodecContext *avctx)
377 {
378     InputStream *ist = avctx->opaque;
379
380     ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
381
382     return 0;
383 }