]> git.sesse.net Git - ffmpeg/blob - libavutil/hwcontext_qsv.c
Merge commit '90adbf4abf336f8042aecdf1e18fdf76a96304b1'
[ffmpeg] / libavutil / hwcontext_qsv.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 <stdint.h>
20 #include <string.h>
21
22 #include <mfx/mfxvideo.h>
23
24 #include "config.h"
25
26 #if HAVE_PTHREADS
27 #include <pthread.h>
28 #endif
29
30 #if CONFIG_VAAPI
31 #include "hwcontext_vaapi.h"
32 #endif
33 #if CONFIG_DXVA2
34 #include "hwcontext_dxva2.h"
35 #endif
36
37 #include "buffer.h"
38 #include "common.h"
39 #include "hwcontext.h"
40 #include "hwcontext_internal.h"
41 #include "hwcontext_qsv.h"
42 #include "mem.h"
43 #include "pixfmt.h"
44 #include "pixdesc.h"
45 #include "time.h"
46
47 typedef struct QSVDevicePriv {
48     AVBufferRef *child_device_ctx;
49 } QSVDevicePriv;
50
51 typedef struct QSVDeviceContext {
52     mfxHDL              handle;
53     mfxHandleType       handle_type;
54     mfxVersion          ver;
55     mfxIMPL             impl;
56
57     enum AVHWDeviceType child_device_type;
58     enum AVPixelFormat  child_pix_fmt;
59 } QSVDeviceContext;
60
61 typedef struct QSVFramesContext {
62     mfxSession session_download;
63     int session_download_init;
64     mfxSession session_upload;
65     int session_upload_init;
66 #if HAVE_PTHREADS
67     pthread_mutex_t session_lock;
68     pthread_cond_t session_cond;
69 #endif
70
71     AVBufferRef *child_frames_ref;
72     mfxFrameSurface1 *surfaces_internal;
73     int             nb_surfaces_used;
74
75     // used in the frame allocator for non-opaque surfaces
76     mfxMemId *mem_ids;
77     // used in the opaque alloc request for opaque surfaces
78     mfxFrameSurface1 **surface_ptrs;
79
80     mfxExtOpaqueSurfaceAlloc opaque_alloc;
81     mfxExtBuffer *ext_buffers[1];
82 } QSVFramesContext;
83
84 static const struct {
85     mfxHandleType handle_type;
86     enum AVHWDeviceType device_type;
87     enum AVPixelFormat  pix_fmt;
88 } supported_handle_types[] = {
89 #if CONFIG_VAAPI
90     { MFX_HANDLE_VA_DISPLAY,          AV_HWDEVICE_TYPE_VAAPI, AV_PIX_FMT_VAAPI },
91 #endif
92 #if CONFIG_DXVA2
93     { MFX_HANDLE_D3D9_DEVICE_MANAGER, AV_HWDEVICE_TYPE_DXVA2, AV_PIX_FMT_DXVA2_VLD },
94 #endif
95     { 0 },
96 };
97
98 static const struct {
99     enum AVPixelFormat pix_fmt;
100     uint32_t           fourcc;
101 } supported_pixel_formats[] = {
102     { AV_PIX_FMT_NV12, MFX_FOURCC_NV12 },
103     { AV_PIX_FMT_BGRA, MFX_FOURCC_RGB4 },
104     { AV_PIX_FMT_P010, MFX_FOURCC_P010 },
105     { AV_PIX_FMT_PAL8, MFX_FOURCC_P8   },
106 };
107
108 static uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
109 {
110     int i;
111     for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
112         if (supported_pixel_formats[i].pix_fmt == pix_fmt)
113             return supported_pixel_formats[i].fourcc;
114     }
115     return 0;
116 }
117
118 static int qsv_device_init(AVHWDeviceContext *ctx)
119 {
120     AVQSVDeviceContext *hwctx = ctx->hwctx;
121     QSVDeviceContext       *s = ctx->internal->priv;
122
123     mfxStatus err;
124     int i;
125
126     for (i = 0; supported_handle_types[i].handle_type; i++) {
127         err = MFXVideoCORE_GetHandle(hwctx->session, supported_handle_types[i].handle_type,
128                                      &s->handle);
129         if (err == MFX_ERR_NONE) {
130             s->handle_type       = supported_handle_types[i].handle_type;
131             s->child_device_type = supported_handle_types[i].device_type;
132             s->child_pix_fmt     = supported_handle_types[i].pix_fmt;
133             break;
134         }
135     }
136     if (!s->handle) {
137         av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
138                "from the session\n");
139     }
140
141     err = MFXQueryIMPL(hwctx->session, &s->impl);
142     if (err == MFX_ERR_NONE)
143         err = MFXQueryVersion(hwctx->session, &s->ver);
144     if (err != MFX_ERR_NONE) {
145         av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
146         return AVERROR_UNKNOWN;
147     }
148
149     return 0;
150 }
151
152 static void qsv_frames_uninit(AVHWFramesContext *ctx)
153 {
154     QSVFramesContext *s = ctx->internal->priv;
155
156     if (s->session_download) {
157         MFXVideoVPP_Close(s->session_download);
158         MFXClose(s->session_download);
159     }
160     s->session_download = NULL;
161     s->session_download_init = 0;
162
163     if (s->session_upload) {
164         MFXVideoVPP_Close(s->session_upload);
165         MFXClose(s->session_upload);
166     }
167     s->session_upload = NULL;
168     s->session_upload_init = 0;
169
170 #if HAVE_PTHREADS
171     pthread_mutex_destroy(&s->session_lock);
172     pthread_cond_destroy(&s->session_cond);
173 #endif
174
175     av_freep(&s->mem_ids);
176     av_freep(&s->surface_ptrs);
177     av_freep(&s->surfaces_internal);
178     av_buffer_unref(&s->child_frames_ref);
179 }
180
181 static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
182 {
183 }
184
185 static AVBufferRef *qsv_pool_alloc(void *opaque, int size)
186 {
187     AVHWFramesContext    *ctx = (AVHWFramesContext*)opaque;
188     QSVFramesContext       *s = ctx->internal->priv;
189     AVQSVFramesContext *hwctx = ctx->hwctx;
190
191     if (s->nb_surfaces_used < hwctx->nb_surfaces) {
192         s->nb_surfaces_used++;
193         return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
194                                 sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
195     }
196
197     return NULL;
198 }
199
200 static int qsv_init_child_ctx(AVHWFramesContext *ctx)
201 {
202     AVQSVFramesContext     *hwctx = ctx->hwctx;
203     QSVFramesContext           *s = ctx->internal->priv;
204     QSVDeviceContext *device_priv = ctx->device_ctx->internal->priv;
205
206     AVBufferRef *child_device_ref = NULL;
207     AVBufferRef *child_frames_ref = NULL;
208
209     AVHWDeviceContext *child_device_ctx;
210     AVHWFramesContext *child_frames_ctx;
211
212     int i, ret = 0;
213
214     if (!device_priv->handle) {
215         av_log(ctx, AV_LOG_ERROR,
216                "Cannot create a non-opaque internal surface pool without "
217                "a hardware handle\n");
218         return AVERROR(EINVAL);
219     }
220
221     child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
222     if (!child_device_ref)
223         return AVERROR(ENOMEM);
224     child_device_ctx   = (AVHWDeviceContext*)child_device_ref->data;
225
226 #if CONFIG_VAAPI
227     if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
228         AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
229         child_device_hwctx->display = (VADisplay)device_priv->handle;
230     }
231 #endif
232 #if CONFIG_DXVA2
233     if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
234         AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
235         child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
236     }
237 #endif
238
239     ret = av_hwdevice_ctx_init(child_device_ref);
240     if (ret < 0) {
241         av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
242         goto fail;
243     }
244
245     child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
246     if (!child_frames_ref) {
247         ret = AVERROR(ENOMEM);
248         goto fail;
249     }
250     child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
251
252     child_frames_ctx->format            = device_priv->child_pix_fmt;
253     child_frames_ctx->sw_format         = ctx->sw_format;
254     child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
255     child_frames_ctx->width             = FFALIGN(ctx->width, 16);
256     child_frames_ctx->height            = FFALIGN(ctx->height, 16);
257
258 #if CONFIG_DXVA2
259     if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
260         AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
261         if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
262             child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
263         else
264             child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
265     }
266 #endif
267
268     ret = av_hwframe_ctx_init(child_frames_ref);
269     if (ret < 0) {
270         av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
271         goto fail;
272     }
273
274 #if CONFIG_VAAPI
275     if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
276         AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
277         for (i = 0; i < ctx->initial_pool_size; i++)
278             s->surfaces_internal[i].Data.MemId = child_frames_hwctx->surface_ids + i;
279         hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
280     }
281 #endif
282 #if CONFIG_DXVA2
283     if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
284         AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
285         for (i = 0; i < ctx->initial_pool_size; i++)
286             s->surfaces_internal[i].Data.MemId = (mfxMemId)child_frames_hwctx->surfaces[i];
287         if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
288             hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
289         else
290             hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
291     }
292 #endif
293
294     s->child_frames_ref       = child_frames_ref;
295     child_frames_ref          = NULL;
296
297 fail:
298     av_buffer_unref(&child_device_ref);
299     av_buffer_unref(&child_frames_ref);
300     return ret;
301 }
302
303 static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
304 {
305     const AVPixFmtDescriptor *desc;
306     uint32_t fourcc;
307
308     desc = av_pix_fmt_desc_get(ctx->sw_format);
309     if (!desc)
310         return AVERROR(EINVAL);
311
312     fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
313     if (!fourcc)
314         return AVERROR(EINVAL);
315
316     surf->Info.BitDepthLuma   = desc->comp[0].depth;
317     surf->Info.BitDepthChroma = desc->comp[0].depth;
318     surf->Info.Shift          = desc->comp[0].depth > 8;
319
320     if (desc->log2_chroma_w && desc->log2_chroma_h)
321         surf->Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
322     else if (desc->log2_chroma_w)
323         surf->Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV422;
324     else
325         surf->Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV444;
326
327     surf->Info.FourCC         = fourcc;
328     surf->Info.Width          = FFALIGN(ctx->width, 16);
329     surf->Info.CropW          = ctx->width;
330     surf->Info.Height         = FFALIGN(ctx->height, 16);
331     surf->Info.CropH          = ctx->height;
332     surf->Info.FrameRateExtN  = 25;
333     surf->Info.FrameRateExtD  = 1;
334     surf->Info.PicStruct      = MFX_PICSTRUCT_PROGRESSIVE;
335
336     return 0;
337 }
338
339 static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
340 {
341     QSVFramesContext              *s = ctx->internal->priv;
342     AVQSVFramesContext *frames_hwctx = ctx->hwctx;
343
344     int i, ret = 0;
345
346     if (ctx->initial_pool_size <= 0) {
347         av_log(ctx, AV_LOG_ERROR, "QSV requires a fixed frame pool size\n");
348         return AVERROR(EINVAL);
349     }
350
351     s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
352                                             sizeof(*s->surfaces_internal));
353     if (!s->surfaces_internal)
354         return AVERROR(ENOMEM);
355
356     for (i = 0; i < ctx->initial_pool_size; i++) {
357         ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
358         if (ret < 0)
359             return ret;
360     }
361
362     if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
363         ret = qsv_init_child_ctx(ctx);
364         if (ret < 0)
365             return ret;
366     }
367
368     ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
369                                                         ctx, qsv_pool_alloc, NULL);
370     if (!ctx->internal->pool_internal)
371         return AVERROR(ENOMEM);
372
373     frames_hwctx->surfaces    = s->surfaces_internal;
374     frames_hwctx->nb_surfaces = ctx->initial_pool_size;
375
376     return 0;
377 }
378
379 static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
380                              mfxFrameAllocResponse *resp)
381 {
382     AVHWFramesContext    *ctx = pthis;
383     QSVFramesContext       *s = ctx->internal->priv;
384     AVQSVFramesContext *hwctx = ctx->hwctx;
385     mfxFrameInfo *i  = &req->Info;
386     mfxFrameInfo *i1 = &hwctx->surfaces[0].Info;
387
388     if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
389         !(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
390         !(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
391         return MFX_ERR_UNSUPPORTED;
392     if (i->Width  != i1->Width || i->Height != i1->Height ||
393         i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
394         av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
395                "allocation request: %dx%d %d %d vs %dx%d %d %d\n",
396                i->Width,  i->Height,  i->FourCC,  i->ChromaFormat,
397                i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
398         return MFX_ERR_UNSUPPORTED;
399     }
400
401     resp->mids           = s->mem_ids;
402     resp->NumFrameActual = hwctx->nb_surfaces;
403
404     return MFX_ERR_NONE;
405 }
406
407 static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
408 {
409     return MFX_ERR_NONE;
410 }
411
412 static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
413 {
414     return MFX_ERR_UNSUPPORTED;
415 }
416
417 static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
418 {
419     return MFX_ERR_UNSUPPORTED;
420 }
421
422 static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
423 {
424     *hdl = mid;
425     return MFX_ERR_NONE;
426 }
427
428 static int qsv_init_internal_session(AVHWFramesContext *ctx,
429                                      mfxSession *session, int upload)
430 {
431     QSVFramesContext              *s = ctx->internal->priv;
432     AVQSVFramesContext *frames_hwctx = ctx->hwctx;
433     QSVDeviceContext   *device_priv  = ctx->device_ctx->internal->priv;
434     int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
435
436     mfxFrameAllocator frame_allocator = {
437         .pthis  = ctx,
438         .Alloc  = frame_alloc,
439         .Lock   = frame_lock,
440         .Unlock = frame_unlock,
441         .GetHDL = frame_get_hdl,
442         .Free   = frame_free,
443     };
444
445     mfxVideoParam par;
446     mfxStatus err;
447
448     err = MFXInit(device_priv->impl, &device_priv->ver, session);
449     if (err != MFX_ERR_NONE) {
450         av_log(ctx, AV_LOG_ERROR, "Error initializing an internal session\n");
451         return AVERROR_UNKNOWN;
452     }
453
454     if (device_priv->handle) {
455         err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
456                                      device_priv->handle);
457         if (err != MFX_ERR_NONE)
458             return AVERROR_UNKNOWN;
459     }
460
461     if (!opaque) {
462         err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
463         if (err != MFX_ERR_NONE)
464             return AVERROR_UNKNOWN;
465     }
466
467     memset(&par, 0, sizeof(par));
468
469     if (opaque) {
470         par.ExtParam    = s->ext_buffers;
471         par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
472         par.IOPattern   = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
473                                    MFX_IOPATTERN_IN_OPAQUE_MEMORY;
474     } else {
475         par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
476                                  MFX_IOPATTERN_IN_VIDEO_MEMORY;
477     }
478
479     par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
480                               MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
481     par.AsyncDepth = 1;
482
483     par.vpp.In = frames_hwctx->surfaces[0].Info;
484
485     /* Apparently VPP requires the frame rate to be set to some value, otherwise
486      * init will fail (probably for the framerate conversion filter). Since we
487      * are only doing data upload/download here, we just invent an arbitrary
488      * value */
489     par.vpp.In.FrameRateExtN = 25;
490     par.vpp.In.FrameRateExtD = 1;
491     par.vpp.Out = par.vpp.In;
492
493     err = MFXVideoVPP_Init(*session, &par);
494     if (err != MFX_ERR_NONE) {
495         av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
496                "Surface upload/download will not be possible\n");
497         MFXClose(*session);
498         *session = NULL;
499     }
500
501     return 0;
502 }
503
504 static int qsv_frames_init(AVHWFramesContext *ctx)
505 {
506     QSVFramesContext              *s = ctx->internal->priv;
507     AVQSVFramesContext *frames_hwctx = ctx->hwctx;
508
509     int opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
510
511     uint32_t fourcc;
512     int i, ret;
513
514     fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
515     if (!fourcc) {
516         av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
517         return AVERROR(ENOSYS);
518     }
519
520     if (!ctx->pool) {
521         ret = qsv_init_pool(ctx, fourcc);
522         if (ret < 0) {
523             av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
524             return ret;
525         }
526     }
527
528     if (opaque) {
529         s->surface_ptrs = av_mallocz_array(frames_hwctx->nb_surfaces,
530                                            sizeof(*s->surface_ptrs));
531         if (!s->surface_ptrs)
532             return AVERROR(ENOMEM);
533
534         for (i = 0; i < frames_hwctx->nb_surfaces; i++)
535             s->surface_ptrs[i] = frames_hwctx->surfaces + i;
536
537         s->opaque_alloc.In.Surfaces   = s->surface_ptrs;
538         s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
539         s->opaque_alloc.In.Type       = frames_hwctx->frame_type;
540
541         s->opaque_alloc.Out = s->opaque_alloc.In;
542
543         s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
544         s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
545
546         s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
547     } else {
548         s->mem_ids = av_mallocz_array(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
549         if (!s->mem_ids)
550             return AVERROR(ENOMEM);
551
552         for (i = 0; i < frames_hwctx->nb_surfaces; i++)
553             s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
554     }
555
556     s->session_download = NULL;
557     s->session_upload   = NULL;
558
559     s->session_download_init = 0;
560     s->session_upload_init   = 0;
561
562 #if HAVE_PTHREADS
563     pthread_mutex_init(&s->session_lock, NULL);
564     pthread_cond_init(&s->session_cond, NULL);
565 #endif
566
567     return 0;
568 }
569
570 static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
571 {
572     frame->buf[0] = av_buffer_pool_get(ctx->pool);
573     if (!frame->buf[0])
574         return AVERROR(ENOMEM);
575
576     frame->data[3] = frame->buf[0]->data;
577     frame->format  = AV_PIX_FMT_QSV;
578     frame->width   = ctx->width;
579     frame->height  = ctx->height;
580
581     return 0;
582 }
583
584 static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
585                                     enum AVHWFrameTransferDirection dir,
586                                     enum AVPixelFormat **formats)
587 {
588     enum AVPixelFormat *fmts;
589
590     fmts = av_malloc_array(2, sizeof(*fmts));
591     if (!fmts)
592         return AVERROR(ENOMEM);
593
594     fmts[0] = ctx->sw_format;
595     fmts[1] = AV_PIX_FMT_NONE;
596
597     *formats = fmts;
598
599     return 0;
600 }
601
602 static int qsv_frames_derive_from(AVHWFramesContext *dst_ctx,
603                                   AVHWFramesContext *src_ctx, int flags)
604 {
605     AVQSVFramesContext *src_hwctx = src_ctx->hwctx;
606     int i;
607
608     switch (dst_ctx->device_ctx->type) {
609 #if CONFIG_VAAPI
610     case AV_HWDEVICE_TYPE_VAAPI:
611         {
612             AVVAAPIFramesContext *dst_hwctx = dst_ctx->hwctx;
613             dst_hwctx->surface_ids = av_mallocz_array(src_hwctx->nb_surfaces,
614                                                       sizeof(*dst_hwctx->surface_ids));
615             if (!dst_hwctx->surface_ids)
616                 return AVERROR(ENOMEM);
617             for (i = 0; i < src_hwctx->nb_surfaces; i++)
618                 dst_hwctx->surface_ids[i] =
619                     *(VASurfaceID*)src_hwctx->surfaces[i].Data.MemId;
620             dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
621         }
622         break;
623 #endif
624 #if CONFIG_DXVA2
625     case AV_HWDEVICE_TYPE_DXVA2:
626         {
627             AVDXVA2FramesContext *dst_hwctx = dst_ctx->hwctx;
628             dst_hwctx->surfaces = av_mallocz_array(src_hwctx->nb_surfaces,
629                                                    sizeof(*dst_hwctx->surfaces));
630             if (!dst_hwctx->surfaces)
631                 return AVERROR(ENOMEM);
632             for (i = 0; i < src_hwctx->nb_surfaces; i++)
633                 dst_hwctx->surfaces[i] =
634                     (IDirect3DSurface9*)src_hwctx->surfaces[i].Data.MemId;
635             dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
636             if (src_hwctx->frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
637                 dst_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
638             else
639                 dst_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
640         }
641         break;
642 #endif
643     default:
644         return AVERROR(ENOSYS);
645     }
646
647     return 0;
648 }
649
650 static int qsv_map_from(AVHWFramesContext *ctx,
651                         AVFrame *dst, const AVFrame *src, int flags)
652 {
653     QSVFramesContext *s = ctx->internal->priv;
654     mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
655     AVHWFramesContext *child_frames_ctx;
656     const AVPixFmtDescriptor *desc;
657     uint8_t *child_data;
658     AVFrame *dummy;
659     int ret = 0;
660
661     if (!s->child_frames_ref)
662         return AVERROR(ENOSYS);
663     child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
664
665     switch (child_frames_ctx->device_ctx->type) {
666 #if CONFIG_VAAPI
667     case AV_HWDEVICE_TYPE_VAAPI:
668         child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)surf->Data.MemId;
669         break;
670 #endif
671 #if CONFIG_DXVA2
672     case AV_HWDEVICE_TYPE_DXVA2:
673         child_data = surf->Data.MemId;
674         break;
675 #endif
676     default:
677         return AVERROR(ENOSYS);
678     }
679
680     if (dst->format == child_frames_ctx->format) {
681         ret = ff_hwframe_map_create(s->child_frames_ref,
682                                     dst, src, NULL, NULL);
683         if (ret < 0)
684             return ret;
685
686         dst->width   = src->width;
687         dst->height  = src->height;
688         dst->data[3] = child_data;
689
690         return 0;
691     }
692
693     desc = av_pix_fmt_desc_get(dst->format);
694     if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
695         // This only supports mapping to software.
696         return AVERROR(ENOSYS);
697     }
698
699     dummy = av_frame_alloc();
700     if (!dummy)
701         return AVERROR(ENOMEM);
702
703     dummy->buf[0]        = av_buffer_ref(src->buf[0]);
704     dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
705     if (!dummy->buf[0] || !dummy->hw_frames_ctx)
706         goto fail;
707
708     dummy->format        = child_frames_ctx->format;
709     dummy->width         = src->width;
710     dummy->height        = src->height;
711     dummy->data[3]       = child_data;
712
713     ret = av_hwframe_map(dst, dummy, flags);
714
715 fail:
716     av_frame_free(&dummy);
717
718     return ret;
719 }
720
721 static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst,
722                                    const AVFrame *src)
723 {
724     QSVFramesContext *s = ctx->internal->priv;
725     AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
726     int download = !!src->hw_frames_ctx;
727     mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
728
729     AVFrame *dummy;
730     int ret;
731
732     dummy = av_frame_alloc();
733     if (!dummy)
734         return AVERROR(ENOMEM);
735
736     dummy->format        = child_frames_ctx->format;
737     dummy->width         = src->width;
738     dummy->height        = src->height;
739     dummy->buf[0]        = download ? src->buf[0] : dst->buf[0];
740     dummy->data[3]       = surf->Data.MemId;
741     dummy->hw_frames_ctx = s->child_frames_ref;
742
743     ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
744                      av_hwframe_transfer_data(dummy, src, 0);
745
746     dummy->buf[0]        = NULL;
747     dummy->data[3]       = NULL;
748     dummy->hw_frames_ctx = NULL;
749
750     av_frame_free(&dummy);
751
752     return ret;
753 }
754
755 static int map_frame_to_surface(const AVFrame *frame, mfxFrameSurface1 *surface)
756 {
757     switch (frame->format) {
758     case AV_PIX_FMT_NV12:
759     case AV_PIX_FMT_P010:
760         surface->Data.Y  = frame->data[0];
761         surface->Data.UV = frame->data[1];
762         break;
763
764     case AV_PIX_FMT_YUV420P:
765         surface->Data.Y = frame->data[0];
766         surface->Data.U = frame->data[1];
767         surface->Data.V = frame->data[2];
768         break;
769
770     case AV_PIX_FMT_BGRA:
771         surface->Data.B = frame->data[0];
772         surface->Data.G = frame->data[0] + 1;
773         surface->Data.R = frame->data[0] + 2;
774         surface->Data.A = frame->data[0] + 3;
775         break;
776
777     default:
778         return MFX_ERR_UNSUPPORTED;
779     }
780     surface->Data.Pitch     = frame->linesize[0];
781     surface->Data.TimeStamp = frame->pts;
782
783     return 0;
784 }
785
786 static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
787                                   const AVFrame *src)
788 {
789     QSVFramesContext  *s = ctx->internal->priv;
790     mfxFrameSurface1 out = {{ 0 }};
791     mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
792
793     mfxSyncPoint sync = NULL;
794     mfxStatus err;
795     int ret = 0;
796
797     while (!s->session_download_init && !s->session_download && !ret) {
798 #if HAVE_PTHREADS
799         if (pthread_mutex_trylock(&s->session_lock) == 0) {
800 #endif
801             if (!s->session_download_init) {
802                 ret = qsv_init_internal_session(ctx, &s->session_download, 0);
803                 if (s->session_download)
804                     s->session_download_init = 1;
805             }
806 #if HAVE_PTHREADS
807             pthread_mutex_unlock(&s->session_lock);
808             pthread_cond_signal(&s->session_cond);
809         } else {
810             pthread_mutex_lock(&s->session_lock);
811             while (!s->session_download_init && !s->session_download) {
812                 pthread_cond_wait(&s->session_cond, &s->session_lock);
813             }
814             pthread_mutex_unlock(&s->session_lock);
815         }
816 #endif
817     }
818
819     if (ret < 0)
820         return ret;
821
822     if (!s->session_download) {
823         if (s->child_frames_ref)
824             return qsv_transfer_data_child(ctx, dst, src);
825
826         av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
827         return AVERROR(ENOSYS);
828     }
829
830     out.Info = in->Info;
831     map_frame_to_surface(dst, &out);
832
833     do {
834         err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
835         if (err == MFX_WRN_DEVICE_BUSY)
836             av_usleep(1);
837     } while (err == MFX_WRN_DEVICE_BUSY);
838
839     if (err < 0 || !sync) {
840         av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
841         return AVERROR_UNKNOWN;
842     }
843
844     do {
845         err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
846     } while (err == MFX_WRN_IN_EXECUTION);
847     if (err < 0) {
848         av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
849         return AVERROR_UNKNOWN;
850     }
851
852     return 0;
853 }
854
855 static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
856                                 const AVFrame *src)
857 {
858     QSVFramesContext   *s = ctx->internal->priv;
859     mfxFrameSurface1   in = {{ 0 }};
860     mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
861
862     mfxSyncPoint sync = NULL;
863     mfxStatus err;
864     int ret = 0;
865     /* make a copy if the input is not padded as libmfx requires */
866     AVFrame tmp_frame, *src_frame;
867     int realigned = 0;
868
869
870     while (!s->session_upload_init && !s->session_upload && !ret) {
871 #if HAVE_PTHREADS
872         if (pthread_mutex_trylock(&s->session_lock) == 0) {
873 #endif
874             if (!s->session_upload_init) {
875                 ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
876                 if (s->session_upload)
877                     s->session_upload_init = 1;
878             }
879 #if HAVE_PTHREADS
880             pthread_mutex_unlock(&s->session_lock);
881             pthread_cond_signal(&s->session_cond);
882         } else {
883             pthread_mutex_lock(&s->session_lock);
884             while (!s->session_upload_init && !s->session_upload) {
885                 pthread_cond_wait(&s->session_cond, &s->session_lock);
886             }
887             pthread_mutex_unlock(&s->session_lock);
888         }
889 #endif
890     }
891     if (ret < 0)
892         return ret;
893
894
895     if (src->height & 16 || src->linesize[0] & 16) {
896         realigned = 1;
897         memset(&tmp_frame, 0, sizeof(tmp_frame));
898         tmp_frame.format         = src->format;
899         tmp_frame.width          = FFALIGN(src->width, 16);
900         tmp_frame.height         = FFALIGN(src->height, 16);
901         ret = av_frame_get_buffer(&tmp_frame, 32);
902         if (ret < 0)
903             return ret;
904
905         ret = av_frame_copy(&tmp_frame, src);
906         if (ret < 0) {
907             av_frame_unref(&tmp_frame);
908             return ret;
909         }
910     }
911
912     src_frame = realigned ? &tmp_frame : src;
913
914     if (!s->session_upload) {
915         if (s->child_frames_ref)
916             return qsv_transfer_data_child(ctx, dst, src_frame);
917
918         av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
919         return AVERROR(ENOSYS);
920     }
921
922     in.Info = out->Info;
923     map_frame_to_surface(src_frame, &in);
924
925     do {
926         err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
927         if (err == MFX_WRN_DEVICE_BUSY)
928             av_usleep(1);
929     } while (err == MFX_WRN_DEVICE_BUSY);
930
931     if (err < 0 || !sync) {
932         av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
933         return AVERROR_UNKNOWN;
934     }
935
936     do {
937         err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
938     } while (err == MFX_WRN_IN_EXECUTION);
939     if (err < 0) {
940         av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
941         return AVERROR_UNKNOWN;
942     }
943
944     if (realigned)
945         av_frame_unref(&tmp_frame);
946
947     return 0;
948 }
949
950 static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx,
951                                 AVHWFramesContext *src_ctx, int flags)
952 {
953     QSVFramesContext *s = dst_ctx->internal->priv;
954     AVQSVFramesContext *dst_hwctx = dst_ctx->hwctx;
955     int i;
956
957     switch (src_ctx->device_ctx->type) {
958 #if CONFIG_VAAPI
959     case AV_HWDEVICE_TYPE_VAAPI:
960         {
961             AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
962             s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
963                                                     sizeof(*s->surfaces_internal));
964             if (!s->surfaces_internal)
965                 return AVERROR(ENOMEM);
966             for (i = 0; i < src_hwctx->nb_surfaces; i++) {
967                 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
968                 s->surfaces_internal[i].Data.MemId = src_hwctx->surface_ids + i;
969             }
970             dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
971             dst_hwctx->frame_type  = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
972         }
973         break;
974 #endif
975 #if CONFIG_DXVA2
976     case AV_HWDEVICE_TYPE_DXVA2:
977         {
978             AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
979             s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
980                                                     sizeof(*s->surfaces_internal));
981             if (!s->surfaces_internal)
982                 return AVERROR(ENOMEM);
983             for (i = 0; i < src_hwctx->nb_surfaces; i++) {
984                 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
985                 s->surfaces_internal[i].Data.MemId = (mfxMemId)src_hwctx->surfaces[i];
986             }
987             dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
988             if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
989                 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
990             else
991                 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
992         }
993         break;
994 #endif
995     default:
996         return AVERROR(ENOSYS);
997     }
998
999     dst_hwctx->surfaces = s->surfaces_internal;
1000
1001     return 0;
1002 }
1003
1004 static int qsv_map_to(AVHWFramesContext *dst_ctx,
1005                       AVFrame *dst, const AVFrame *src, int flags)
1006 {
1007     AVQSVFramesContext *hwctx = dst_ctx->hwctx;
1008     int i, err;
1009
1010     for (i = 0; i < hwctx->nb_surfaces; i++) {
1011 #if CONFIG_VAAPI
1012         if (*(VASurfaceID*)hwctx->surfaces[i].Data.MemId ==
1013             (VASurfaceID)(uintptr_t)src->data[3])
1014             break;
1015 #endif
1016 #if CONFIG_DXVA2
1017         if ((IDirect3DSurface9*)hwctx->surfaces[i].Data.MemId ==
1018             (IDirect3DSurface9*)(uintptr_t)src->data[3])
1019             break;
1020 #endif
1021     }
1022     if (i >= hwctx->nb_surfaces) {
1023         av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
1024                "is not in the mapped frames context.\n");
1025         return AVERROR(EINVAL);
1026     }
1027
1028     err = ff_hwframe_map_create(dst->hw_frames_ctx,
1029                                 dst, src, NULL, NULL);
1030     if (err)
1031         return err;
1032
1033     dst->width   = src->width;
1034     dst->height  = src->height;
1035     dst->data[3] = (uint8_t*)&hwctx->surfaces[i];
1036
1037     return 0;
1038 }
1039
1040 static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
1041                                       const void *hwconfig,
1042                                       AVHWFramesConstraints *constraints)
1043 {
1044     int i;
1045
1046     constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
1047                                                     sizeof(*constraints->valid_sw_formats));
1048     if (!constraints->valid_sw_formats)
1049         return AVERROR(ENOMEM);
1050
1051     for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
1052         constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
1053     constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
1054
1055     constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
1056     if (!constraints->valid_hw_formats)
1057         return AVERROR(ENOMEM);
1058
1059     constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
1060     constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
1061
1062     return 0;
1063 }
1064
1065 static void qsv_device_free(AVHWDeviceContext *ctx)
1066 {
1067     AVQSVDeviceContext *hwctx = ctx->hwctx;
1068     QSVDevicePriv       *priv = ctx->user_opaque;
1069
1070     if (hwctx->session)
1071         MFXClose(hwctx->session);
1072
1073     av_buffer_unref(&priv->child_device_ctx);
1074     av_freep(&priv);
1075 }
1076
1077 static mfxIMPL choose_implementation(const char *device)
1078 {
1079     static const struct {
1080         const char *name;
1081         mfxIMPL     impl;
1082     } impl_map[] = {
1083         { "auto",     MFX_IMPL_AUTO         },
1084         { "sw",       MFX_IMPL_SOFTWARE     },
1085         { "hw",       MFX_IMPL_HARDWARE     },
1086         { "auto_any", MFX_IMPL_AUTO_ANY     },
1087         { "hw_any",   MFX_IMPL_HARDWARE_ANY },
1088         { "hw2",      MFX_IMPL_HARDWARE2    },
1089         { "hw3",      MFX_IMPL_HARDWARE3    },
1090         { "hw4",      MFX_IMPL_HARDWARE4    },
1091     };
1092
1093     mfxIMPL impl = MFX_IMPL_AUTO_ANY;
1094     int i;
1095
1096     if (device) {
1097         for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
1098             if (!strcmp(device, impl_map[i].name)) {
1099                 impl = impl_map[i].impl;
1100                 break;
1101             }
1102         if (i == FF_ARRAY_ELEMS(impl_map))
1103             impl = strtol(device, NULL, 0);
1104     }
1105
1106     return impl;
1107 }
1108
1109 static int qsv_device_derive_from_child(AVHWDeviceContext *ctx,
1110                                         mfxIMPL implementation,
1111                                         AVHWDeviceContext *child_device_ctx,
1112                                         int flags)
1113 {
1114     AVQSVDeviceContext *hwctx = ctx->hwctx;
1115
1116     mfxVersion    ver = { { 3, 1 } };
1117     mfxHDL        handle;
1118     mfxHandleType handle_type;
1119     mfxStatus     err;
1120     int ret;
1121
1122     switch (child_device_ctx->type) {
1123 #if CONFIG_VAAPI
1124     case AV_HWDEVICE_TYPE_VAAPI:
1125         {
1126             AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1127             handle_type = MFX_HANDLE_VA_DISPLAY;
1128             handle = (mfxHDL)child_device_hwctx->display;
1129         }
1130         break;
1131 #endif
1132 #if CONFIG_DXVA2
1133     case AV_HWDEVICE_TYPE_DXVA2:
1134         {
1135             AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1136             handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
1137             handle = (mfxHDL)child_device_hwctx->devmgr;
1138         }
1139         break;
1140 #endif
1141     default:
1142         ret = AVERROR(ENOSYS);
1143         goto fail;
1144     }
1145
1146     err = MFXInit(implementation, &ver, &hwctx->session);
1147     if (err != MFX_ERR_NONE) {
1148         av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
1149                "%d.\n", err);
1150         ret = AVERROR_UNKNOWN;
1151         goto fail;
1152     }
1153
1154     err = MFXQueryVersion(hwctx->session, &ver);
1155     if (err != MFX_ERR_NONE) {
1156         av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: %d.\n", err);
1157         ret = AVERROR_UNKNOWN;
1158         goto fail;
1159     }
1160
1161     av_log(ctx, AV_LOG_VERBOSE,
1162            "Initialize MFX session: API version is %d.%d, implementation version is %d.%d\n",
1163            MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
1164
1165     MFXClose(hwctx->session);
1166
1167     err = MFXInit(implementation, &ver, &hwctx->session);
1168     if (err != MFX_ERR_NONE) {
1169         av_log(ctx, AV_LOG_ERROR,
1170                "Error initializing an MFX session: %d.\n", err);
1171         ret = AVERROR_UNKNOWN;
1172         goto fail;
1173     }
1174
1175     err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
1176     if (err != MFX_ERR_NONE) {
1177         av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
1178                "%d\n", err);
1179         ret = AVERROR_UNKNOWN;
1180         goto fail;
1181     }
1182
1183     ret = MFXQueryVersion(hwctx->session,&ver);
1184     if (ret == MFX_ERR_NONE) {
1185         av_log(ctx, AV_LOG_VERBOSE, "MFX compile/runtime API: %d.%d/%d.%d\n",
1186                MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
1187     }
1188     return 0;
1189
1190 fail:
1191     if (hwctx->session)
1192         MFXClose(hwctx->session);
1193     return ret;
1194 }
1195
1196 static int qsv_device_derive(AVHWDeviceContext *ctx,
1197                              AVHWDeviceContext *child_device_ctx, int flags)
1198 {
1199     return qsv_device_derive_from_child(ctx, MFX_IMPL_HARDWARE_ANY,
1200                                         child_device_ctx, flags);
1201 }
1202
1203 static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
1204                              AVDictionary *opts, int flags)
1205 {
1206     QSVDevicePriv *priv;
1207     enum AVHWDeviceType child_device_type;
1208     AVHWDeviceContext *child_device;
1209     AVDictionaryEntry *e;
1210
1211     mfxIMPL impl;
1212     int ret;
1213
1214     priv = av_mallocz(sizeof(*priv));
1215     if (!priv)
1216         return AVERROR(ENOMEM);
1217
1218     ctx->user_opaque = priv;
1219     ctx->free        = qsv_device_free;
1220
1221     e = av_dict_get(opts, "child_device", NULL, 0);
1222
1223     if (CONFIG_VAAPI)
1224         child_device_type = AV_HWDEVICE_TYPE_VAAPI;
1225     else if (CONFIG_DXVA2)
1226         child_device_type = AV_HWDEVICE_TYPE_DXVA2;
1227     else {
1228         av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
1229         return AVERROR(ENOSYS);
1230     }
1231
1232     ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
1233                                  e ? e->value : NULL, NULL, 0);
1234     if (ret < 0)
1235         return ret;
1236
1237     child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
1238
1239     impl = choose_implementation(device);
1240
1241     return qsv_device_derive_from_child(ctx, impl, child_device, 0);
1242 }
1243
1244 const HWContextType ff_hwcontext_type_qsv = {
1245     .type                   = AV_HWDEVICE_TYPE_QSV,
1246     .name                   = "QSV",
1247
1248     .device_hwctx_size      = sizeof(AVQSVDeviceContext),
1249     .device_priv_size       = sizeof(QSVDeviceContext),
1250     .frames_hwctx_size      = sizeof(AVQSVFramesContext),
1251     .frames_priv_size       = sizeof(QSVFramesContext),
1252
1253     .device_create          = qsv_device_create,
1254     .device_derive          = qsv_device_derive,
1255     .device_init            = qsv_device_init,
1256     .frames_get_constraints = qsv_frames_get_constraints,
1257     .frames_init            = qsv_frames_init,
1258     .frames_uninit          = qsv_frames_uninit,
1259     .frames_get_buffer      = qsv_get_buffer,
1260     .transfer_get_formats   = qsv_transfer_get_formats,
1261     .transfer_data_to       = qsv_transfer_data_to,
1262     .transfer_data_from     = qsv_transfer_data_from,
1263     .map_to                 = qsv_map_to,
1264     .map_from               = qsv_map_from,
1265     .frames_derive_to       = qsv_frames_derive_to,
1266     .frames_derive_from     = qsv_frames_derive_from,
1267
1268     .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },
1269 };