FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/hwcontext.c
Date: 2026-10-15 22:25:26
Exec Total Coverage
Lines: 4 487 0.8%
Functions: 1 26 3.8%
Branches: 1 266 0.4%

Line Branch Exec Source
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 "config.h"
20
21 #include "avassert.h"
22 #include "buffer.h"
23 #include "common.h"
24 #include "hwcontext.h"
25 #include "hwcontext_internal.h"
26 #include "imgutils.h"
27 #include "log.h"
28 #include "mem.h"
29 #include "pixdesc.h"
30 #include "pixfmt.h"
31
32 static const HWContextType * const hw_table[] = {
33 #if CONFIG_CUDA
34 &ff_hwcontext_type_cuda,
35 #endif
36 #if CONFIG_D3D11VA
37 &ff_hwcontext_type_d3d11va,
38 #endif
39 #if CONFIG_D3D12VA
40 &ff_hwcontext_type_d3d12va,
41 #endif
42 #if CONFIG_LIBDRM
43 &ff_hwcontext_type_drm,
44 #endif
45 #if CONFIG_DXVA2
46 &ff_hwcontext_type_dxva2,
47 #endif
48 #if CONFIG_OPENCL
49 &ff_hwcontext_type_opencl,
50 #endif
51 #if CONFIG_QSV
52 &ff_hwcontext_type_qsv,
53 #endif
54 #if CONFIG_VAAPI
55 &ff_hwcontext_type_vaapi,
56 #endif
57 #if CONFIG_VDPAU
58 &ff_hwcontext_type_vdpau,
59 #endif
60 #if CONFIG_VIDEOTOOLBOX
61 &ff_hwcontext_type_videotoolbox,
62 #endif
63 #if CONFIG_MEDIACODEC
64 &ff_hwcontext_type_mediacodec,
65 #endif
66 #if CONFIG_VULKAN
67 &ff_hwcontext_type_vulkan,
68 #endif
69 #if CONFIG_AMF
70 &ff_hwcontext_type_amf,
71 #endif
72 #if CONFIG_OHCODEC
73 &ff_hwcontext_type_oh,
74 #endif
75 NULL,
76 };
77
78 static const char *const hw_type_names[] = {
79 [AV_HWDEVICE_TYPE_CUDA] = "cuda",
80 [AV_HWDEVICE_TYPE_DRM] = "drm",
81 [AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
82 [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
83 [AV_HWDEVICE_TYPE_D3D12VA] = "d3d12va",
84 [AV_HWDEVICE_TYPE_OPENCL] = "opencl",
85 [AV_HWDEVICE_TYPE_QSV] = "qsv",
86 [AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
87 [AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
88 [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
89 [AV_HWDEVICE_TYPE_MEDIACODEC] = "mediacodec",
90 [AV_HWDEVICE_TYPE_VULKAN] = "vulkan",
91 [AV_HWDEVICE_TYPE_AMF] = "amf",
92 [AV_HWDEVICE_TYPE_OHCODEC] = "ohcodec",
93 };
94
95 typedef struct FFHWDeviceContext {
96 /**
97 * The public AVHWDeviceContext. See hwcontext.h for it.
98 */
99 AVHWDeviceContext p;
100
101 const HWContextType *hw_type;
102
103 /**
104 * For a derived device, a reference to the original device
105 * context it was derived from.
106 */
107 AVBufferRef *source_device;
108 } FFHWDeviceContext;
109
110 ✗ enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
111 {
112 int type;
113 ✗ for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
114 ✗ if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
115 ✗ return type;
116 }
117 ✗ return AV_HWDEVICE_TYPE_NONE;
118 }
119
120 ✗ const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
121 {
122 ✗ if (type > AV_HWDEVICE_TYPE_NONE &&
123 type < FF_ARRAY_ELEMS(hw_type_names))
124 ✗ return hw_type_names[type];
125 else
126 ✗ return NULL;
127 }
128
129 ✗ enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
130 {
131 enum AVHWDeviceType next;
132 ✗ int i, set = 0;
133 ✗ for (i = 0; hw_table[i]; i++) {
134 ✗ if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
135 ✗ continue;
136 ✗ if (!set || hw_table[i]->type < next) {
137 ✗ next = hw_table[i]->type;
138 ✗ set = 1;
139 }
140 }
141 ✗ return set ? next : AV_HWDEVICE_TYPE_NONE;
142 }
143
144 ✗ static const char *hwdevice_ctx_get_name(void *ptr)
145 {
146 ✗ FFHWDeviceContext *ctx = ptr;
147 ✗ return ctx->hw_type->name;
148 }
149
150 static const AVClass hwdevice_ctx_class = {
151 .class_name = "AVHWDeviceContext",
152 .item_name = hwdevice_ctx_get_name,
153 .category = AV_CLASS_CATEGORY_HWDEVICE,
154 .version = LIBAVUTIL_VERSION_INT,
155 };
156
157 ✗ static void hwdevice_ctx_free(void *opaque, uint8_t *data)
158 {
159 ✗ FFHWDeviceContext *ctxi = (FFHWDeviceContext*)data;
160 ✗ AVHWDeviceContext *ctx = &ctxi->p;
161
162 /* uninit might still want access the hw context and the user
163 * free() callback might destroy it, so uninit has to be called first */
164 ✗ if (ctxi->hw_type->device_uninit)
165 ✗ ctxi->hw_type->device_uninit(ctx);
166
167 ✗ if (ctx->free)
168 ✗ ctx->free(ctx);
169
170 ✗ av_buffer_unref(&ctxi->source_device);
171
172 ✗ av_freep(&ctx->hwctx);
173 ✗ av_freep(&ctx);
174 ✗ }
175
176 ✗ AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
177 {
178 FFHWDeviceContext *ctxi;
179 AVHWDeviceContext *ctx;
180 AVBufferRef *buf;
181 ✗ const HWContextType *hw_type = NULL;
182 int i;
183
184 ✗ for (i = 0; hw_table[i]; i++) {
185 ✗ if (hw_table[i]->type == type) {
186 ✗ hw_type = hw_table[i];
187 ✗ break;
188 }
189 }
190 ✗ if (!hw_type)
191 ✗ return NULL;
192
193 ✗ ctxi = av_mallocz(sizeof(*ctxi));
194 ✗ if (!ctxi)
195 ✗ return NULL;
196 ✗ ctx = &ctxi->p;
197
198 ✗ if (hw_type->device_hwctx_size) {
199 ✗ ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
200 ✗ if (!ctx->hwctx)
201 ✗ goto fail;
202 }
203
204 ✗ buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
205 hwdevice_ctx_free, NULL,
206 AV_BUFFER_FLAG_READONLY);
207 ✗ if (!buf)
208 ✗ goto fail;
209
210 ✗ ctx->type = type;
211 ✗ ctx->av_class = &hwdevice_ctx_class;
212
213 ✗ ctxi->hw_type = hw_type;
214
215 ✗ return buf;
216
217 ✗ fail:
218 ✗ av_freep(&ctx->hwctx);
219 ✗ av_freep(&ctx);
220 ✗ return NULL;
221 }
222
223 ✗ int av_hwdevice_ctx_init(AVBufferRef *ref)
224 {
225 ✗ FFHWDeviceContext *ctxi = (FFHWDeviceContext*)ref->data;
226 ✗ AVHWDeviceContext *ctx = &ctxi->p;
227 ✗ int ret = 0;
228
229 ✗ if (ctxi->hw_type->device_init)
230 ✗ ret = ctxi->hw_type->device_init(ctx);
231
232 ✗ return ret;
233 }
234
235 static const AVClass hwframe_ctx_class = {
236 .class_name = "AVHWFramesContext",
237 .item_name = av_default_item_name,
238 .version = LIBAVUTIL_VERSION_INT,
239 };
240
241 ✗ static void hwframe_ctx_free(void *opaque, uint8_t *data)
242 {
243 ✗ FFHWFramesContext *ctxi = (FFHWFramesContext*)data;
244 ✗ AVHWFramesContext *ctx = &ctxi->p;
245
246 ✗ if (ctxi->pool_internal)
247 ✗ av_buffer_pool_uninit(&ctxi->pool_internal);
248
249 ✗ if (ctxi->hw_type->frames_uninit)
250 ✗ ctxi->hw_type->frames_uninit(ctx);
251
252 ✗ if (ctx->free)
253 ✗ ctx->free(ctx);
254
255 ✗ av_buffer_unref(&ctxi->source_frames);
256
257 ✗ av_buffer_unref(&ctx->device_ref);
258
259 ✗ av_freep(&ctx->hwctx);
260 ✗ av_freep(&ctx);
261 ✗ }
262
263 ✗ AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
264 {
265 ✗ FFHWDeviceContext *device_ctx = (FFHWDeviceContext*)device_ref_in->data;
266 ✗ const HWContextType *hw_type = device_ctx->hw_type;
267 FFHWFramesContext *ctxi;
268 AVHWFramesContext *ctx;
269 ✗ AVBufferRef *buf, *device_ref = NULL;
270
271 ✗ ctxi = av_mallocz(sizeof(*ctxi));
272 ✗ if (!ctxi)
273 ✗ return NULL;
274 ✗ ctx = &ctxi->p;
275
276 ✗ if (hw_type->frames_hwctx_size) {
277 ✗ ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
278 ✗ if (!ctx->hwctx)
279 ✗ goto fail;
280 }
281
282 ✗ device_ref = av_buffer_ref(device_ref_in);
283 ✗ if (!device_ref)
284 ✗ goto fail;
285
286 ✗ buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
287 hwframe_ctx_free, NULL,
288 AV_BUFFER_FLAG_READONLY);
289 ✗ if (!buf)
290 ✗ goto fail;
291
292 ✗ ctx->av_class = &hwframe_ctx_class;
293 ✗ ctx->device_ref = device_ref;
294 ✗ ctx->device_ctx = &device_ctx->p;
295 ✗ ctx->format = AV_PIX_FMT_NONE;
296 ✗ ctx->sw_format = AV_PIX_FMT_NONE;
297
298 ✗ ctxi->hw_type = hw_type;
299
300 ✗ return buf;
301
302 ✗ fail:
303 ✗ av_buffer_unref(&device_ref);
304 ✗ av_freep(&ctx->hwctx);
305 ✗ av_freep(&ctx);
306 ✗ return NULL;
307 }
308
309 ✗ static int hwframe_pool_prealloc(AVBufferRef *ref)
310 {
311 ✗ AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
312 AVFrame **frames;
313 ✗ int i, ret = 0;
314
315 ✗ frames = av_calloc(ctx->initial_pool_size, sizeof(*frames));
316 ✗ if (!frames)
317 ✗ return AVERROR(ENOMEM);
318
319 ✗ for (i = 0; i < ctx->initial_pool_size; i++) {
320 ✗ frames[i] = av_frame_alloc();
321 ✗ if (!frames[i])
322 ✗ goto fail;
323
324 ✗ ret = av_hwframe_get_buffer(ref, frames[i], 0);
325 ✗ if (ret < 0)
326 ✗ goto fail;
327 }
328
329 ✗ fail:
330 ✗ for (i = 0; i < ctx->initial_pool_size; i++)
331 ✗ av_frame_free(&frames[i]);
332 ✗ av_freep(&frames);
333
334 ✗ return ret;
335 }
336
337 ✗ int av_hwframe_ctx_init(AVBufferRef *ref)
338 {
339 ✗ FFHWFramesContext *ctxi = (FFHWFramesContext*)ref->data;
340 ✗ AVHWFramesContext *ctx = &ctxi->p;
341 const enum AVPixelFormat *pix_fmt;
342 int ret;
343
344 ✗ if (ctxi->source_frames) {
345 /* A derived frame context is already initialised. */
346 ✗ return 0;
347 }
348
349 /* validate the pixel format */
350 ✗ for (pix_fmt = ctxi->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
351 ✗ if (*pix_fmt == ctx->format)
352 ✗ break;
353 }
354 ✗ if (*pix_fmt == AV_PIX_FMT_NONE) {
355 ✗ av_log(ctx, AV_LOG_ERROR,
356 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
357 ✗ av_get_pix_fmt_name(ctx->format), ctxi->hw_type->name);
358 ✗ return AVERROR(ENOSYS);
359 }
360
361 /* validate the dimensions */
362 ✗ ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
363 ✗ if (ret < 0)
364 ✗ return ret;
365
366 /* format-specific init */
367 ✗ if (ctxi->hw_type->frames_init) {
368 ✗ ret = ctxi->hw_type->frames_init(ctx);
369 ✗ if (ret < 0)
370 ✗ return ret;
371 }
372
373 ✗ if (ctxi->pool_internal && !ctx->pool)
374 ✗ ctx->pool = ctxi->pool_internal;
375
376 /* preallocate the frames in the pool, if requested */
377 ✗ if (ctx->initial_pool_size > 0) {
378 ✗ ret = hwframe_pool_prealloc(ref);
379 ✗ if (ret < 0)
380 ✗ return ret;
381 }
382
383 ✗ return 0;
384 }
385
386 ✗ int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
387 enum AVHWFrameTransferDirection dir,
388 enum AVPixelFormat **formats, int flags)
389 {
390 ✗ FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
391
392 ✗ if (!ctxi->hw_type->transfer_get_formats)
393 ✗ return AVERROR(ENOSYS);
394
395 ✗ return ctxi->hw_type->transfer_get_formats(&ctxi->p, dir, formats);
396 }
397
398 ✗ static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
399 {
400 AVHWFramesContext *ctx;
401 AVFrame *frame_tmp;
402 ✗ int ret = 0;
403
404 ✗ if (!src->hw_frames_ctx)
405 ✗ return AVERROR(EINVAL);
406 ✗ ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
407
408 ✗ frame_tmp = av_frame_alloc();
409 ✗ if (!frame_tmp)
410 ✗ return AVERROR(ENOMEM);
411
412 /* if the format is set, use that
413 * otherwise pick the first supported one */
414 ✗ if (dst->format >= 0) {
415 ✗ frame_tmp->format = dst->format;
416 } else {
417 enum AVPixelFormat *formats;
418
419 ✗ ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
420 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
421 &formats, 0);
422 ✗ if (ret < 0)
423 ✗ goto fail;
424 ✗ frame_tmp->format = formats[0];
425 ✗ av_freep(&formats);
426 }
427 ✗ frame_tmp->width = ctx->width;
428 ✗ frame_tmp->height = ctx->height;
429
430 ✗ ret = av_frame_get_buffer(frame_tmp, 0);
431 ✗ if (ret < 0)
432 ✗ goto fail;
433
434 ✗ ret = av_hwframe_transfer_data(frame_tmp, src, flags);
435 ✗ if (ret < 0)
436 ✗ goto fail;
437
438 ✗ frame_tmp->width = src->width;
439 ✗ frame_tmp->height = src->height;
440
441 ✗ av_frame_move_ref(dst, frame_tmp);
442
443 ✗ fail:
444 ✗ av_frame_free(&frame_tmp);
445 ✗ return ret;
446 }
447
448 ✗ int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
449 {
450 int ret;
451
452 ✗ if (!dst->buf[0])
453 ✗ return transfer_data_alloc(dst, src, flags);
454
455 /*
456 * Hardware -> Hardware Transfer.
457 * Unlike Software -> Hardware or Hardware -> Software, the transfer
458 * function could be provided by either the src or dst, depending on
459 * the specific combination of hardware.
460 */
461 ✗ if (src->hw_frames_ctx && dst->hw_frames_ctx) {
462 ✗ FFHWFramesContext *src_ctx =
463 ✗ (FFHWFramesContext*)src->hw_frames_ctx->data;
464 ✗ FFHWFramesContext *dst_ctx =
465 ✗ (FFHWFramesContext*)dst->hw_frames_ctx->data;
466
467 ✗ if (src_ctx->source_frames) {
468 ✗ av_log(src_ctx, AV_LOG_ERROR,
469 "A device with a derived frame context cannot be used as "
470 "the source of a HW -> HW transfer.");
471 ✗ return AVERROR(ENOSYS);
472 }
473
474 ✗ if (dst_ctx->source_frames) {
475 ✗ av_log(src_ctx, AV_LOG_ERROR,
476 "A device with a derived frame context cannot be used as "
477 "the destination of a HW -> HW transfer.");
478 ✗ return AVERROR(ENOSYS);
479 }
480
481 ✗ ret = AVERROR(ENOSYS);
482 ✗ if (src_ctx->hw_type->transfer_data_from)
483 ✗ ret = src_ctx->hw_type->transfer_data_from(&src_ctx->p, dst, src);
484 ✗ if (ret == AVERROR(ENOSYS) && dst_ctx->hw_type->transfer_data_to)
485 ✗ ret = dst_ctx->hw_type->transfer_data_to(&dst_ctx->p, dst, src);
486 ✗ if (ret < 0)
487 ✗ return ret;
488 } else {
489 ✗ if (src->hw_frames_ctx) {
490 ✗ FFHWFramesContext *ctx = (FFHWFramesContext*)src->hw_frames_ctx->data;
491
492 ✗ if (!ctx->hw_type->transfer_data_from)
493 ✗ return AVERROR(ENOSYS);
494
495 ✗ ret = ctx->hw_type->transfer_data_from(&ctx->p, dst, src);
496 ✗ if (ret < 0)
497 ✗ return ret;
498 ✗ } else if (dst->hw_frames_ctx) {
499 ✗ FFHWFramesContext *ctx = (FFHWFramesContext*)dst->hw_frames_ctx->data;
500
501 ✗ if (!ctx->hw_type->transfer_data_to)
502 ✗ return AVERROR(ENOSYS);
503
504 ✗ ret = ctx->hw_type->transfer_data_to(&ctx->p, dst, src);
505 ✗ if (ret < 0)
506 ✗ return ret;
507 } else {
508 ✗ return AVERROR(ENOSYS);
509 }
510 }
511 ✗ return 0;
512 }
513
514 ✗ int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
515 {
516 ✗ FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
517 ✗ AVHWFramesContext *ctx = &ctxi->p;
518 int ret;
519
520 ✗ if (ctxi->source_frames) {
521 // This is a derived frame context, so we allocate in the source
522 // and map the frame immediately.
523 AVFrame *src_frame;
524
525 ✗ frame->format = ctx->format;
526 ✗ frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
527 ✗ if (!frame->hw_frames_ctx)
528 ✗ return AVERROR(ENOMEM);
529
530 ✗ src_frame = av_frame_alloc();
531 ✗ if (!src_frame)
532 ✗ return AVERROR(ENOMEM);
533
534 ✗ ret = av_hwframe_get_buffer(ctxi->source_frames,
535 src_frame, 0);
536 ✗ if (ret < 0) {
537 ✗ av_frame_free(&src_frame);
538 ✗ return ret;
539 }
540
541 ✗ ret = av_hwframe_map(frame, src_frame,
542 ctxi->source_allocation_map_flags);
543 ✗ if (ret) {
544 ✗ av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
545 "frame context: %d.\n", ret);
546 ✗ av_frame_free(&src_frame);
547 ✗ return ret;
548 }
549
550 // Free the source frame immediately - the mapped frame still
551 // contains a reference to it.
552 ✗ av_frame_free(&src_frame);
553
554 ✗ return 0;
555 }
556
557 ✗ if (!ctxi->hw_type->frames_get_buffer)
558 ✗ return AVERROR(ENOSYS);
559
560 ✗ if (!ctx->pool)
561 ✗ return AVERROR(EINVAL);
562
563 ✗ frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
564 ✗ if (!frame->hw_frames_ctx)
565 ✗ return AVERROR(ENOMEM);
566
567 ✗ ret = ctxi->hw_type->frames_get_buffer(ctx, frame);
568 ✗ if (ret < 0) {
569 ✗ av_buffer_unref(&frame->hw_frames_ctx);
570 ✗ return ret;
571 }
572
573 ✗ frame->extended_data = frame->data;
574
575 ✗ return 0;
576 }
577
578 ✗ void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
579 {
580 ✗ FFHWDeviceContext *ctx = (FFHWDeviceContext*)ref->data;
581 ✗ const HWContextType *hw_type = ctx->hw_type;
582
583 ✗ if (hw_type->device_hwconfig_size == 0)
584 ✗ return NULL;
585
586 ✗ return av_mallocz(hw_type->device_hwconfig_size);
587 }
588
589 ✗ AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
590 const void *hwconfig)
591 {
592 ✗ FFHWDeviceContext *ctx = (FFHWDeviceContext*)ref->data;
593 ✗ const HWContextType *hw_type = ctx->hw_type;
594 AVHWFramesConstraints *constraints;
595
596 ✗ if (!hw_type->frames_get_constraints)
597 ✗ return NULL;
598
599 ✗ constraints = av_mallocz(sizeof(*constraints));
600 ✗ if (!constraints)
601 ✗ return NULL;
602
603 ✗ constraints->min_width = constraints->min_height = 0;
604 ✗ constraints->max_width = constraints->max_height = INT_MAX;
605
606 ✗ if (hw_type->frames_get_constraints(&ctx->p, hwconfig, constraints) >= 0) {
607 ✗ return constraints;
608 } else {
609 ✗ av_hwframe_constraints_free(&constraints);
610 ✗ return NULL;
611 }
612 }
613
614 6 void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
615 {
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (*constraints) {
617 ✗ av_freep(&(*constraints)->valid_hw_formats);
618 ✗ av_freep(&(*constraints)->valid_sw_formats);
619 }
620 6 av_freep(constraints);
621 6 }
622
623 ✗ int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
624 const char *device, AVDictionary *opts, int flags)
625 {
626 ✗ AVBufferRef *device_ref = NULL;
627 FFHWDeviceContext *device_ctx;
628 ✗ int ret = 0;
629
630 ✗ device_ref = av_hwdevice_ctx_alloc(type);
631 ✗ if (!device_ref) {
632 ✗ ret = AVERROR(ENOMEM);
633 ✗ goto fail;
634 }
635 ✗ device_ctx = (FFHWDeviceContext*)device_ref->data;
636
637 ✗ if (!device_ctx->hw_type->device_create) {
638 ✗ ret = AVERROR(ENOSYS);
639 ✗ goto fail;
640 }
641
642 ✗ ret = device_ctx->hw_type->device_create(&device_ctx->p, device,
643 opts, flags);
644 ✗ if (ret < 0)
645 ✗ goto fail;
646
647 ✗ ret = av_hwdevice_ctx_init(device_ref);
648 ✗ if (ret < 0)
649 ✗ goto fail;
650
651 ✗ *pdevice_ref = device_ref;
652 ✗ return 0;
653 ✗ fail:
654 ✗ av_buffer_unref(&device_ref);
655 ✗ *pdevice_ref = NULL;
656 ✗ return ret;
657 }
658
659 ✗ int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ref_ptr,
660 enum AVHWDeviceType type,
661 AVBufferRef *src_ref,
662 AVDictionary *options, int flags)
663 {
664 ✗ AVBufferRef *dst_ref = NULL, *tmp_ref;
665 FFHWDeviceContext *dst_ctx;
666 ✗ int ret = 0;
667
668 ✗ tmp_ref = src_ref;
669 ✗ while (tmp_ref) {
670 ✗ FFHWDeviceContext *tmp_ctx = (FFHWDeviceContext*)tmp_ref->data;
671 ✗ if (tmp_ctx->p.type == type) {
672 ✗ dst_ref = av_buffer_ref(tmp_ref);
673 ✗ if (!dst_ref) {
674 ✗ ret = AVERROR(ENOMEM);
675 ✗ goto fail;
676 }
677 ✗ goto done;
678 }
679 ✗ tmp_ref = tmp_ctx->source_device;
680 }
681
682 ✗ dst_ref = av_hwdevice_ctx_alloc(type);
683 ✗ if (!dst_ref) {
684 ✗ ret = AVERROR(ENOMEM);
685 ✗ goto fail;
686 }
687 ✗ dst_ctx = (FFHWDeviceContext*)dst_ref->data;
688
689 ✗ tmp_ref = src_ref;
690 ✗ while (tmp_ref) {
691 ✗ FFHWDeviceContext *tmp_ctx = (FFHWDeviceContext*)tmp_ref->data;
692 ✗ if (dst_ctx->hw_type->device_derive) {
693 ✗ ret = dst_ctx->hw_type->device_derive(&dst_ctx->p,
694 &tmp_ctx->p,
695 options, flags);
696 ✗ if (ret == 0) {
697 ✗ dst_ctx->source_device = av_buffer_ref(src_ref);
698 ✗ if (!dst_ctx->source_device) {
699 ✗ ret = AVERROR(ENOMEM);
700 ✗ goto fail;
701 }
702 ✗ ret = av_hwdevice_ctx_init(dst_ref);
703 ✗ if (ret < 0)
704 ✗ goto fail;
705 ✗ goto done;
706 }
707 ✗ if (ret != AVERROR(ENOSYS))
708 ✗ goto fail;
709 }
710 ✗ tmp_ref = tmp_ctx->source_device;
711 }
712
713 ✗ ret = AVERROR(ENOSYS);
714 ✗ goto fail;
715
716 ✗ done:
717 ✗ *dst_ref_ptr = dst_ref;
718 ✗ return 0;
719
720 ✗ fail:
721 ✗ av_buffer_unref(&dst_ref);
722 ✗ *dst_ref_ptr = NULL;
723 ✗ return ret;
724 }
725
726 ✗ int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
727 enum AVHWDeviceType type,
728 AVBufferRef *src_ref, int flags)
729 {
730 ✗ return av_hwdevice_ctx_create_derived_opts(dst_ref_ptr, type, src_ref,
731 NULL, flags);
732 }
733
734 ✗ static void ff_hwframe_unmap(void *opaque, uint8_t *data)
735 {
736 ✗ HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
737 ✗ AVHWFramesContext *ctx = opaque;
738
739 ✗ if (hwmap->unmap)
740 ✗ hwmap->unmap(ctx, hwmap);
741
742 ✗ av_frame_free(&hwmap->source);
743
744 ✗ av_buffer_unref(&hwmap->hw_frames_ctx);
745
746 ✗ av_free(hwmap);
747 ✗ }
748
749 ✗ int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
750 AVFrame *dst, const AVFrame *src,
751 void (*unmap)(AVHWFramesContext *ctx,
752 HWMapDescriptor *hwmap),
753 void *priv)
754 {
755 ✗ AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
756 HWMapDescriptor *hwmap;
757 int ret;
758
759 ✗ hwmap = av_mallocz(sizeof(*hwmap));
760 ✗ if (!hwmap) {
761 ✗ ret = AVERROR(ENOMEM);
762 ✗ goto fail;
763 }
764
765 ✗ hwmap->source = av_frame_alloc();
766 ✗ if (!hwmap->source) {
767 ✗ ret = AVERROR(ENOMEM);
768 ✗ goto fail;
769 }
770 ✗ ret = av_frame_ref(hwmap->source, src);
771 ✗ if (ret < 0)
772 ✗ goto fail;
773
774 ✗ hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
775 ✗ if (!hwmap->hw_frames_ctx) {
776 ✗ ret = AVERROR(ENOMEM);
777 ✗ goto fail;
778 }
779
780 ✗ hwmap->unmap = unmap;
781 ✗ hwmap->priv = priv;
782
783 ✗ dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
784 &ff_hwframe_unmap, ctx, 0);
785 ✗ if (!dst->buf[0]) {
786 ✗ ret = AVERROR(ENOMEM);
787 ✗ goto fail;
788 }
789
790 ✗ return 0;
791
792 ✗ fail:
793 ✗ if (hwmap) {
794 ✗ av_buffer_unref(&hwmap->hw_frames_ctx);
795 ✗ av_frame_free(&hwmap->source);
796 }
797 ✗ av_free(hwmap);
798 ✗ return ret;
799 }
800
801 ✗ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
802 {
803 ✗ AVBufferRef *orig_dst_frames = dst->hw_frames_ctx;
804 ✗ enum AVPixelFormat orig_dst_fmt = dst->format;
805 HWMapDescriptor *hwmap;
806 int ret;
807
808 ✗ if (src->hw_frames_ctx && dst->hw_frames_ctx) {
809 ✗ FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
810 ✗ FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
811
812 ✗ if ((src_frames == dst_frames &&
813 ✗ src->format == dst_frames->p.sw_format &&
814 ✗ dst->format == dst_frames->p.format) ||
815 ✗ (src_frames->source_frames &&
816 ✗ src_frames->source_frames->data ==
817 (uint8_t*)dst_frames)) {
818 // This is an unmap operation. We don't need to directly
819 // do anything here other than fill in the original frame,
820 // because the real unmap will be invoked when the last
821 // reference to the mapped frame disappears.
822 ✗ if (!src->buf[0]) {
823 ✗ av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
824 "found when attempting unmap.\n");
825 ✗ return AVERROR(EINVAL);
826 }
827 ✗ hwmap = (HWMapDescriptor*)src->buf[0]->data;
828 ✗ return av_frame_replace(dst, hwmap->source);
829 }
830 }
831
832 ✗ if (src->hw_frames_ctx) {
833 ✗ FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
834
835 ✗ if (src_frames->p.format == src->format &&
836 ✗ src_frames->hw_type->map_from) {
837 ✗ ret = src_frames->hw_type->map_from(&src_frames->p,
838 dst, src, flags);
839 ✗ if (ret >= 0)
840 ✗ return ret;
841 ✗ else if (ret != AVERROR(ENOSYS))
842 ✗ goto fail;
843 }
844 }
845
846 ✗ if (dst->hw_frames_ctx) {
847 ✗ FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
848
849 ✗ if (dst_frames->p.format == dst->format &&
850 ✗ dst_frames->hw_type->map_to) {
851 ✗ ret = dst_frames->hw_type->map_to(&dst_frames->p,
852 dst, src, flags);
853 ✗ if (ret >= 0)
854 ✗ return ret;
855 ✗ else if (ret != AVERROR(ENOSYS))
856 ✗ goto fail;
857 }
858 }
859
860 ✗ return AVERROR(ENOSYS);
861
862 ✗ fail:
863 // if the caller provided dst frames context, it should be preserved
864 // by this function
865 ✗ av_assert0(orig_dst_frames == NULL ||
866 orig_dst_frames == dst->hw_frames_ctx);
867
868 // preserve user-provided dst frame fields, but clean
869 // anything we might have set
870 ✗ dst->hw_frames_ctx = NULL;
871 ✗ av_frame_unref(dst);
872
873 ✗ dst->hw_frames_ctx = orig_dst_frames;
874 ✗ dst->format = orig_dst_fmt;
875
876 ✗ return ret;
877 }
878
879 ✗ int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
880 enum AVPixelFormat format,
881 AVBufferRef *derived_device_ctx,
882 AVBufferRef *source_frame_ctx,
883 int flags)
884 {
885 ✗ AVBufferRef *dst_ref = NULL;
886 ✗ FFHWFramesContext *dsti = NULL;
887 ✗ FFHWFramesContext *srci = (FFHWFramesContext*)source_frame_ctx->data;
888 ✗ AVHWFramesContext *dst, *src = &srci->p;
889 int ret;
890
891 ✗ if (srci->source_frames) {
892 ✗ AVHWFramesContext *src_src =
893 ✗ (AVHWFramesContext*)srci->source_frames->data;
894 ✗ AVHWDeviceContext *dst_dev =
895 (AVHWDeviceContext*)derived_device_ctx->data;
896
897 ✗ if (src_src->device_ctx == dst_dev) {
898 // This is actually an unmapping, so we just return a
899 // reference to the source frame context.
900 ✗ *derived_frame_ctx = av_buffer_ref(srci->source_frames);
901 ✗ if (!*derived_frame_ctx) {
902 ✗ ret = AVERROR(ENOMEM);
903 ✗ goto fail;
904 }
905 ✗ return 0;
906 }
907 }
908
909 ✗ dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
910 ✗ if (!dst_ref) {
911 ✗ ret = AVERROR(ENOMEM);
912 ✗ goto fail;
913 }
914
915 ✗ dsti = (FFHWFramesContext*)dst_ref->data;
916 ✗ dst = &dsti->p;
917
918 ✗ dst->format = format;
919 ✗ dst->sw_format = src->sw_format;
920 ✗ dst->width = src->width;
921 ✗ dst->height = src->height;
922
923 ✗ dsti->source_frames = av_buffer_ref(source_frame_ctx);
924 ✗ if (!dsti->source_frames) {
925 ✗ ret = AVERROR(ENOMEM);
926 ✗ goto fail;
927 }
928
929 ✗ dsti->source_allocation_map_flags =
930 ✗ flags & (AV_HWFRAME_MAP_READ |
931 AV_HWFRAME_MAP_WRITE |
932 AV_HWFRAME_MAP_OVERWRITE |
933 AV_HWFRAME_MAP_DIRECT);
934
935 ✗ ret = AVERROR(ENOSYS);
936 ✗ if (srci->hw_type->frames_derive_from)
937 ✗ ret = srci->hw_type->frames_derive_from(dst, src, flags);
938 ✗ if (ret == AVERROR(ENOSYS) &&
939 ✗ dsti->hw_type->frames_derive_to)
940 ✗ ret = dsti->hw_type->frames_derive_to(dst, src, flags);
941 ✗ if (ret == AVERROR(ENOSYS))
942 ✗ ret = 0;
943 ✗ if (ret)
944 ✗ goto fail;
945
946 ✗ *derived_frame_ctx = dst_ref;
947 ✗ return 0;
948
949 ✗ fail:
950 ✗ if (dsti)
951 ✗ av_buffer_unref(&dsti->source_frames);
952 ✗ av_buffer_unref(&dst_ref);
953 ✗ return ret;
954 }
955
956 ✗ int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
957 {
958 ✗ HWMapDescriptor *hwmap = (HWMapDescriptor*)dst->buf[0]->data;
959 ✗ return av_frame_replace(hwmap->source, src);
960 }
961