FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/hwcontext.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 0 481 0.0%
Functions: 0 26 0.0%
Branches: 0 258 0.0%

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