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