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