| FFmpegKit Android API 6.0

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