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