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