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