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