]> git.sesse.net Git - ffmpeg/blob - avtools/avconv_hw.c
hevc: Make sure to update the current frame transfer characteristic
[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 = NULL;
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     av_buffer_unref(&device_ref);
212     goto done;
213 }
214
215 void hw_device_free_all(void)
216 {
217     int i;
218     for (i = 0; i < nb_hw_devices; i++) {
219         av_freep(&hw_devices[i]->name);
220         av_buffer_unref(&hw_devices[i]->device_ref);
221         av_freep(&hw_devices[i]);
222     }
223     av_freep(&hw_devices);
224     nb_hw_devices = 0;
225 }
226
227 static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
228 {
229     int i;
230     if (hwaccel_id == HWACCEL_NONE)
231         return AV_HWDEVICE_TYPE_NONE;
232     for (i = 0; hwaccels[i].name; i++) {
233         if (hwaccels[i].id == hwaccel_id)
234             return hwaccels[i].device_type;
235     }
236     return AV_HWDEVICE_TYPE_NONE;
237 }
238
239 static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
240 {
241     const char *type_name;
242     enum AVHWDeviceType type;
243     for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
244          type != AV_HWDEVICE_TYPE_NONE;
245          type = av_hwdevice_iterate_types(type)) {
246         type_name = av_hwdevice_get_type_name(type);
247         if (strstr(codec_name, type_name))
248             return type;
249     }
250     return AV_HWDEVICE_TYPE_NONE;
251 }
252
253 int hw_device_setup_for_decode(InputStream *ist)
254 {
255     enum AVHWDeviceType type;
256     HWDevice *dev;
257     const char *type_name;
258     int err;
259
260     if (ist->hwaccel_device) {
261         dev = hw_device_get_by_name(ist->hwaccel_device);
262         if (!dev) {
263             char *tmp;
264             size_t len;
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.
270                 return 0;
271             }
272             type_name = av_hwdevice_get_type_name(type);
273             len = strlen(type_name) + 1 +
274                   strlen(ist->hwaccel_device) + 1;
275             tmp = av_malloc(len);
276             if (!tmp)
277                 return AVERROR(ENOMEM);
278             snprintf(tmp, len, "%s:%s", type_name, ist->hwaccel_device);
279             err = hw_device_init_from_string(tmp, &dev);
280             av_free(tmp);
281             if (err < 0)
282                 return err;
283         }
284     } else {
285         if (ist->hwaccel_id != HWACCEL_NONE)
286             type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
287         else
288             type = hw_device_match_type_in_name(ist->dec->name);
289         if (type != AV_HWDEVICE_TYPE_NONE) {
290             dev = hw_device_get_by_type(type);
291             if (!dev) {
292                 hw_device_init_from_string(av_hwdevice_get_type_name(type),
293                                            &dev);
294             }
295         } else {
296             // No device required.
297             return 0;
298         }
299     }
300
301     if (!dev) {
302         av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
303                "for decoder (device type %s for codec %s).\n",
304                av_hwdevice_get_type_name(type), ist->dec->name);
305         return 0;
306     }
307
308     ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
309     if (!ist->dec_ctx->hw_device_ctx)
310         return AVERROR(ENOMEM);
311
312     return 0;
313 }
314
315 int hw_device_setup_for_encode(OutputStream *ost)
316 {
317     enum AVHWDeviceType type;
318     HWDevice *dev;
319
320     type = hw_device_match_type_in_name(ost->enc->name);
321     if (type != AV_HWDEVICE_TYPE_NONE) {
322         dev = hw_device_get_by_type(type);
323         if (!dev) {
324             av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
325                    "for encoder (device type %s for codec %s).\n",
326                    av_hwdevice_get_type_name(type), ost->enc->name);
327             return 0;
328         }
329         ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
330         if (!ost->enc_ctx->hw_device_ctx)
331             return AVERROR(ENOMEM);
332         return 0;
333     } else {
334         // No device required.
335         return 0;
336     }
337 }
338
339 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
340 {
341     InputStream *ist = avctx->opaque;
342     AVFrame *output = NULL;
343     enum AVPixelFormat output_format = ist->hwaccel_output_format;
344     int err;
345
346     if (input->format == output_format) {
347         // Nothing to do.
348         return 0;
349     }
350
351     output = av_frame_alloc();
352     if (!output)
353         return AVERROR(ENOMEM);
354
355     output->format = output_format;
356
357     err = av_hwframe_transfer_data(output, input, 0);
358     if (err < 0) {
359         av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
360                "output frame: %d.\n", err);
361         goto fail;
362     }
363
364     err = av_frame_copy_props(output, input);
365     if (err < 0) {
366         av_frame_unref(output);
367         goto fail;
368     }
369
370     av_frame_unref(input);
371     av_frame_move_ref(input, output);
372     av_frame_free(&output);
373
374     return 0;
375
376 fail:
377     av_frame_free(&output);
378     return err;
379 }
380
381 int hwaccel_decode_init(AVCodecContext *avctx)
382 {
383     InputStream *ist = avctx->opaque;
384
385     ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
386
387     return 0;
388 }