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