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