FFmpegKit Android API 5.1
fftools_ffmpeg_hw.c
Go to the documentation of this file.
1/*
2 * copyright (c) 2018 Taner Sener ( tanersener gmail com )
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * This file is the modified version of ffmpeg_hw.c file living in ffmpeg source code under the fftools folder. We
23 * manually update it each time we depend on a new ffmpeg version. Below you can see the list of changes applied
24 * by us to develop mobile-ffmpeg and later ffmpeg-kit libraries.
25 *
26 * mobile-ffmpeg / ffmpeg-kit changes by Taner Sener
27 *
28 * 12.2019
29 * --------------------------------------------------------
30 * - concurrent execution support ("__thread" specifier added to variables used by multiple threads)
31 *
32 * 08.2018
33 * --------------------------------------------------------
34 * - fftools_ prefix added to file name and parent header
35 */
36
37#include <string.h>
38
39#include "libavutil/avstring.h"
40#include "libavutil/pixdesc.h"
41#include "libavfilter/buffersink.h"
42
43#include "fftools_ffmpeg.h"
44
45__thread int nb_hw_devices;
47
48static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
49{
50 HWDevice *found = NULL;
51 int i;
52 for (i = 0; i < nb_hw_devices; i++) {
53 if (hw_devices[i]->type == type) {
54 if (found)
55 return NULL;
56 found = hw_devices[i];
57 }
58 }
59 return found;
60}
61
63{
64 int i;
65 for (i = 0; i < nb_hw_devices; i++) {
66 if (!strcmp(hw_devices[i]->name, name))
67 return hw_devices[i];
68 }
69 return NULL;
70}
71
73{
74 int err;
75 err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
76 sizeof(*hw_devices));
77 if (err) {
78 nb_hw_devices = 0;
79 return NULL;
80 }
81 hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
83 return NULL;
84 return hw_devices[nb_hw_devices++];
85}
86
87static char *hw_device_default_name(enum AVHWDeviceType type)
88{
89 // Make an automatic name of the form "type%d". We arbitrarily
90 // limit at 1000 anonymous devices of the same type - there is
91 // probably something else very wrong if you get to this limit.
92 const char *type_name = av_hwdevice_get_type_name(type);
93 char *name;
94 size_t index_pos;
95 int index, index_limit = 1000;
96 index_pos = strlen(type_name);
97 name = av_malloc(index_pos + 4);
98 if (!name)
99 return NULL;
100 for (index = 0; index < index_limit; index++) {
101 snprintf(name, index_pos + 4, "%s%d", type_name, index);
102 if (!hw_device_get_by_name(name))
103 break;
104 }
105 if (index >= index_limit) {
106 av_freep(&name);
107 return NULL;
108 }
109 return name;
110}
111
112int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
113{
114 // "type=name"
115 // "type=name,key=value,key2=value2"
116 // "type=name:device,key=value,key2=value2"
117 // "type:device,key=value,key2=value2"
118 // -> av_hwdevice_ctx_create()
119 // "type=name@name"
120 // "type@name"
121 // -> av_hwdevice_ctx_create_derived()
122
123 AVDictionary *options = NULL;
124 const char *type_name = NULL, *name = NULL, *device = NULL;
125 enum AVHWDeviceType type;
126 HWDevice *dev, *src;
127 AVBufferRef *device_ref = NULL;
128 int err;
129 const char *errmsg, *p, *q;
130 size_t k;
131
132 k = strcspn(arg, ":=@");
133 p = arg + k;
134
135 type_name = av_strndup(arg, k);
136 if (!type_name) {
137 err = AVERROR(ENOMEM);
138 goto fail;
139 }
140 type = av_hwdevice_find_type_by_name(type_name);
141 if (type == AV_HWDEVICE_TYPE_NONE) {
142 errmsg = "unknown device type";
143 goto invalid;
144 }
145
146 if (*p == '=') {
147 k = strcspn(p + 1, ":@,");
148
149 name = av_strndup(p + 1, k);
150 if (!name) {
151 err = AVERROR(ENOMEM);
152 goto fail;
153 }
154 if (hw_device_get_by_name(name)) {
155 errmsg = "named device already exists";
156 goto invalid;
157 }
158
159 p += 1 + k;
160 } else {
161 name = hw_device_default_name(type);
162 if (!name) {
163 err = AVERROR(ENOMEM);
164 goto fail;
165 }
166 }
167
168 if (!*p) {
169 // New device with no parameters.
170 err = av_hwdevice_ctx_create(&device_ref, type,
171 NULL, NULL, 0);
172 if (err < 0)
173 goto fail;
174
175 } else if (*p == ':') {
176 // New device with some parameters.
177 ++p;
178 q = strchr(p, ',');
179 if (q) {
180 if (q - p > 0) {
181 device = av_strndup(p, q - p);
182 if (!device) {
183 err = AVERROR(ENOMEM);
184 goto fail;
185 }
186 }
187 err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
188 if (err < 0) {
189 errmsg = "failed to parse options";
190 goto invalid;
191 }
192 }
193
194 err = av_hwdevice_ctx_create(&device_ref, type,
195 q ? device : p[0] ? p : NULL,
196 options, 0);
197 if (err < 0)
198 goto fail;
199
200 } else if (*p == '@') {
201 // Derive from existing device.
202
203 src = hw_device_get_by_name(p + 1);
204 if (!src) {
205 errmsg = "invalid source device name";
206 goto invalid;
207 }
208
209 err = av_hwdevice_ctx_create_derived(&device_ref, type,
210 src->device_ref, 0);
211 if (err < 0)
212 goto fail;
213 } else if (*p == ',') {
214 err = av_dict_parse_string(&options, p + 1, "=", ",", 0);
215
216 if (err < 0) {
217 errmsg = "failed to parse options";
218 goto invalid;
219 }
220
221 err = av_hwdevice_ctx_create(&device_ref, type,
222 NULL, options, 0);
223 if (err < 0)
224 goto fail;
225 } else {
226 errmsg = "parse error";
227 goto invalid;
228 }
229
230 dev = hw_device_add();
231 if (!dev) {
232 err = AVERROR(ENOMEM);
233 goto fail;
234 }
235
236 dev->name = name;
237 dev->type = type;
238 dev->device_ref = device_ref;
239
240 if (dev_out)
241 *dev_out = dev;
242
243 name = NULL;
244 err = 0;
245done:
246 av_freep(&type_name);
247 av_freep(&name);
248 av_freep(&device);
249 av_dict_free(&options);
250 return err;
251invalid:
252 av_log(NULL, AV_LOG_ERROR,
253 "Invalid device specification \"%s\": %s\n", arg, errmsg);
254 err = AVERROR(EINVAL);
255 goto done;
256fail:
257 av_log(NULL, AV_LOG_ERROR,
258 "Device creation failed: %d.\n", err);
259 av_buffer_unref(&device_ref);
260 goto done;
261}
262
263static int hw_device_init_from_type(enum AVHWDeviceType type,
264 const char *device,
265 HWDevice **dev_out)
266{
267 AVBufferRef *device_ref = NULL;
268 HWDevice *dev;
269 char *name;
270 int err;
271
272 name = hw_device_default_name(type);
273 if (!name) {
274 err = AVERROR(ENOMEM);
275 goto fail;
276 }
277
278 err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
279 if (err < 0) {
280 av_log(NULL, AV_LOG_ERROR,
281 "Device creation failed: %d.\n", err);
282 goto fail;
283 }
284
285 dev = hw_device_add();
286 if (!dev) {
287 err = AVERROR(ENOMEM);
288 goto fail;
289 }
290
291 dev->name = name;
292 dev->type = type;
293 dev->device_ref = device_ref;
294
295 if (dev_out)
296 *dev_out = dev;
297
298 return 0;
299
300fail:
301 av_freep(&name);
302 av_buffer_unref(&device_ref);
303 return err;
304}
305
307{
308 int i;
309 for (i = 0; i < nb_hw_devices; i++) {
310 av_freep(&hw_devices[i]->name);
311 av_buffer_unref(&hw_devices[i]->device_ref);
312 av_freep(&hw_devices[i]);
313 }
314 av_freep(&hw_devices);
315 nb_hw_devices = 0;
316}
317
318static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
319{
320 const AVCodecHWConfig *config;
321 HWDevice *dev;
322 int i;
323 for (i = 0;; i++) {
324 config = avcodec_get_hw_config(codec, i);
325 if (!config)
326 return NULL;
327 if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
328 continue;
329 dev = hw_device_get_by_type(config->device_type);
330 if (dev)
331 return dev;
332 }
333}
334
336{
337 const AVCodecHWConfig *config;
338 enum AVHWDeviceType type;
339 HWDevice *dev = NULL;
340 int err, auto_device = 0;
341
342 if (ist->hwaccel_device) {
344 if (!dev) {
345 if (ist->hwaccel_id == HWACCEL_AUTO) {
346 auto_device = 1;
347 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
348 type = ist->hwaccel_device_type;
350 &dev);
351 } else {
352 // This will be dealt with by API-specific initialisation
353 // (using hwaccel_device), so nothing further needed here.
354 return 0;
355 }
356 } else {
357 if (ist->hwaccel_id == HWACCEL_AUTO) {
358 ist->hwaccel_device_type = dev->type;
359 } else if (ist->hwaccel_device_type != dev->type) {
360 av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device "
361 "specified for decoder: device %s of type %s is not "
362 "usable with hwaccel %s.\n", dev->name,
363 av_hwdevice_get_type_name(dev->type),
364 av_hwdevice_get_type_name(ist->hwaccel_device_type));
365 return AVERROR(EINVAL);
366 }
367 }
368 } else {
369 if (ist->hwaccel_id == HWACCEL_AUTO) {
370 auto_device = 1;
371 } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
372 type = ist->hwaccel_device_type;
373 dev = hw_device_get_by_type(type);
374
375 // When "-qsv_device device" is used, an internal QSV device named
376 // as "__qsv_device" is created. Another QSV device is created too
377 // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
378 // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
379 // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
380 // To keep back-compatibility with the removed ad-hoc libmfx setup code,
381 // call hw_device_get_by_name("__qsv_device") to select the internal QSV
382 // device.
383 if (!dev && type == AV_HWDEVICE_TYPE_QSV)
384 dev = hw_device_get_by_name("__qsv_device");
385
386 if (!dev)
387 err = hw_device_init_from_type(type, NULL, &dev);
388 } else {
389 dev = hw_device_match_by_codec(ist->dec);
390 if (!dev) {
391 // No device for this codec, but not using generic hwaccel
392 // and therefore may well not need one - ignore.
393 return 0;
394 }
395 }
396 }
397
398 if (auto_device) {
399 int i;
400 if (!avcodec_get_hw_config(ist->dec, 0)) {
401 // Decoder does not support any hardware devices.
402 return 0;
403 }
404 for (i = 0; !dev; i++) {
405 config = avcodec_get_hw_config(ist->dec, i);
406 if (!config)
407 break;
408 type = config->device_type;
409 dev = hw_device_get_by_type(type);
410 if (dev) {
411 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
412 "hwaccel type %s with existing device %s.\n",
413 av_hwdevice_get_type_name(type), dev->name);
414 }
415 }
416 for (i = 0; !dev; i++) {
417 config = avcodec_get_hw_config(ist->dec, i);
418 if (!config)
419 break;
420 type = config->device_type;
421 // Try to make a new device of this type.
423 &dev);
424 if (err < 0) {
425 // Can't make a device of this type.
426 continue;
427 }
428 if (ist->hwaccel_device) {
429 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
430 "hwaccel type %s with new device created "
431 "from %s.\n", av_hwdevice_get_type_name(type),
432 ist->hwaccel_device);
433 } else {
434 av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
435 "hwaccel type %s with new default device.\n",
436 av_hwdevice_get_type_name(type));
437 }
438 }
439 if (dev) {
440 ist->hwaccel_device_type = type;
441 } else {
442 av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel "
443 "disabled: no device found.\n");
445 return 0;
446 }
447 }
448
449 if (!dev) {
450 av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available "
451 "for decoder: device type %s needed for codec %s.\n",
452 av_hwdevice_get_type_name(type), ist->dec->name);
453 return err;
454 }
455
456 ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
457 if (!ist->dec_ctx->hw_device_ctx)
458 return AVERROR(ENOMEM);
459
460 return 0;
461}
462
464{
465 const AVCodecHWConfig *config;
466 HWDevice *dev = NULL;
467 AVBufferRef *frames_ref = NULL;
468 int i;
469
470 if (ost->filter) {
471 frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter);
472 if (frames_ref &&
473 ((AVHWFramesContext*)frames_ref->data)->format ==
474 ost->enc_ctx->pix_fmt) {
475 // Matching format, will try to use hw_frames_ctx.
476 } else {
477 frames_ref = NULL;
478 }
479 }
480
481 for (i = 0;; i++) {
482 config = avcodec_get_hw_config(ost->enc, i);
483 if (!config)
484 break;
485
486 if (frames_ref &&
487 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
488 (config->pix_fmt == AV_PIX_FMT_NONE ||
489 config->pix_fmt == ost->enc_ctx->pix_fmt)) {
490 av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
491 "frames context (format %s) with %s encoder.\n",
492 av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
493 ost->enc->name);
494 ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
495 if (!ost->enc_ctx->hw_frames_ctx)
496 return AVERROR(ENOMEM);
497 return 0;
498 }
499
500 if (!dev &&
501 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
502 dev = hw_device_get_by_type(config->device_type);
503 }
504
505 if (dev) {
506 av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
507 "(type %s) with %s encoder.\n", dev->name,
508 av_hwdevice_get_type_name(dev->type), ost->enc->name);
509 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
510 if (!ost->enc_ctx->hw_device_ctx)
511 return AVERROR(ENOMEM);
512 } else {
513 // No device required, or no device available.
514 }
515 return 0;
516}
517
518static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
519{
520 InputStream *ist = avctx->opaque;
521 AVFrame *output = NULL;
522 enum AVPixelFormat output_format = ist->hwaccel_output_format;
523 int err;
524
525 if (input->format == output_format) {
526 // Nothing to do.
527 return 0;
528 }
529
530 output = av_frame_alloc();
531 if (!output)
532 return AVERROR(ENOMEM);
533
534 output->format = output_format;
535
536 err = av_hwframe_transfer_data(output, input, 0);
537 if (err < 0) {
538 av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
539 "output frame: %d.\n", err);
540 goto fail;
541 }
542
543 err = av_frame_copy_props(output, input);
544 if (err < 0) {
545 av_frame_unref(output);
546 goto fail;
547 }
548
549 av_frame_unref(input);
550 av_frame_move_ref(input, output);
551 av_frame_free(&output);
552
553 return 0;
554
555fail:
556 av_frame_free(&output);
557 return err;
558}
559
560int hwaccel_decode_init(AVCodecContext *avctx)
561{
562 InputStream *ist = avctx->opaque;
563
565
566 return 0;
567}
568
570{
571 HWDevice *dev;
572 int i;
573
574 // Pick the last hardware device if the user doesn't pick the device for
575 // filters explicitly with the filter_hw_device option.
577 dev = filter_hw_device;
578 else if (nb_hw_devices > 0) {
579 dev = hw_devices[nb_hw_devices - 1];
580
581 if (nb_hw_devices > 1)
582 av_log(NULL, AV_LOG_WARNING, "There are %d hardware devices. device "
583 "%s of type %s is picked for filters by default. Set hardware "
584 "device explicitly with the filter_hw_device option if device "
585 "%s is not usable for filters.\n",
586 nb_hw_devices, dev->name,
587 av_hwdevice_get_type_name(dev->type), dev->name);
588 } else
589 dev = NULL;
590
591 if (dev) {
592 for (i = 0; i < fg->graph->nb_filters; i++) {
593 fg->graph->filters[i]->hw_device_ctx =
594 av_buffer_ref(dev->device_ref);
595 if (!fg->graph->filters[i]->hw_device_ctx)
596 return AVERROR(ENOMEM);
597 }
598 }
599
600 return 0;
601}
@ HWACCEL_NONE
@ HWACCEL_GENERIC
@ HWACCEL_AUTO
__thread HWDevice * filter_hw_device
int hw_device_setup_for_encode(OutputStream *ost)
int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
int hwaccel_decode_init(AVCodecContext *avctx)
HWDevice * hw_device_get_by_name(const char *name)
static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
static HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
int hw_device_setup_for_decode(InputStream *ist)
void hw_device_free_all(void)
int hw_device_setup_for_filter(FilterGraph *fg)
static HWDevice * hw_device_match_by_codec(const AVCodec *codec)
static int hw_device_init_from_type(enum AVHWDeviceType type, const char *device, HWDevice **dev_out)
static HWDevice * hw_device_add(void)
__thread int nb_hw_devices
__thread HWDevice ** hw_devices
static char * hw_device_default_name(enum AVHWDeviceType type)
AVFilterGraph * graph
AVBufferRef * device_ref
enum AVHWDeviceType type
const char * name
enum AVPixelFormat hwaccel_output_format
AVCodecContext * dec_ctx
enum HWAccelID hwaccel_id
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
char * hwaccel_device
const AVCodec * dec
enum AVHWDeviceType hwaccel_device_type
AVFilterContext * filter
const AVCodec * enc
AVCodecContext * enc_ctx
OutputFilter * filter