]> git.sesse.net Git - ffmpeg/blob - libavutil/hwcontext_qsv.c
lavu/hwcontext_qsv: Mark a pointer as const.
[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;
867     const AVFrame *src_frame;
868     int realigned = 0;
869
870
871     while (!s->session_upload_init && !s->session_upload && !ret) {
872 #if HAVE_PTHREADS
873         if (pthread_mutex_trylock(&s->session_lock) == 0) {
874 #endif
875             if (!s->session_upload_init) {
876                 ret = qsv_init_internal_session(ctx, &s->session_upload, 1);
877                 if (s->session_upload)
878                     s->session_upload_init = 1;
879             }
880 #if HAVE_PTHREADS
881             pthread_mutex_unlock(&s->session_lock);
882             pthread_cond_signal(&s->session_cond);
883         } else {
884             pthread_mutex_lock(&s->session_lock);
885             while (!s->session_upload_init && !s->session_upload) {
886                 pthread_cond_wait(&s->session_cond, &s->session_lock);
887             }
888             pthread_mutex_unlock(&s->session_lock);
889         }
890 #endif
891     }
892     if (ret < 0)
893         return ret;
894
895
896     if (src->height & 16 || src->linesize[0] & 16) {
897         realigned = 1;
898         memset(&tmp_frame, 0, sizeof(tmp_frame));
899         tmp_frame.format         = src->format;
900         tmp_frame.width          = FFALIGN(src->width, 16);
901         tmp_frame.height         = FFALIGN(src->height, 16);
902         ret = av_frame_get_buffer(&tmp_frame, 32);
903         if (ret < 0)
904             return ret;
905
906         ret = av_frame_copy(&tmp_frame, src);
907         if (ret < 0) {
908             av_frame_unref(&tmp_frame);
909             return ret;
910         }
911     }
912
913     src_frame = realigned ? &tmp_frame : src;
914
915     if (!s->session_upload) {
916         if (s->child_frames_ref)
917             return qsv_transfer_data_child(ctx, dst, src_frame);
918
919         av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
920         return AVERROR(ENOSYS);
921     }
922
923     in.Info = out->Info;
924     map_frame_to_surface(src_frame, &in);
925
926     do {
927         err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
928         if (err == MFX_WRN_DEVICE_BUSY)
929             av_usleep(1);
930     } while (err == MFX_WRN_DEVICE_BUSY);
931
932     if (err < 0 || !sync) {
933         av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
934         return AVERROR_UNKNOWN;
935     }
936
937     do {
938         err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
939     } while (err == MFX_WRN_IN_EXECUTION);
940     if (err < 0) {
941         av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
942         return AVERROR_UNKNOWN;
943     }
944
945     if (realigned)
946         av_frame_unref(&tmp_frame);
947
948     return 0;
949 }
950
951 static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx,
952                                 AVHWFramesContext *src_ctx, int flags)
953 {
954     QSVFramesContext *s = dst_ctx->internal->priv;
955     AVQSVFramesContext *dst_hwctx = dst_ctx->hwctx;
956     int i;
957
958     switch (src_ctx->device_ctx->type) {
959 #if CONFIG_VAAPI
960     case AV_HWDEVICE_TYPE_VAAPI:
961         {
962             AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
963             s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
964                                                     sizeof(*s->surfaces_internal));
965             if (!s->surfaces_internal)
966                 return AVERROR(ENOMEM);
967             for (i = 0; i < src_hwctx->nb_surfaces; i++) {
968                 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
969                 s->surfaces_internal[i].Data.MemId = src_hwctx->surface_ids + i;
970             }
971             dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
972             dst_hwctx->frame_type  = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
973         }
974         break;
975 #endif
976 #if CONFIG_DXVA2
977     case AV_HWDEVICE_TYPE_DXVA2:
978         {
979             AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
980             s->surfaces_internal = av_mallocz_array(src_hwctx->nb_surfaces,
981                                                     sizeof(*s->surfaces_internal));
982             if (!s->surfaces_internal)
983                 return AVERROR(ENOMEM);
984             for (i = 0; i < src_hwctx->nb_surfaces; i++) {
985                 qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
986                 s->surfaces_internal[i].Data.MemId = (mfxMemId)src_hwctx->surfaces[i];
987             }
988             dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
989             if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
990                 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
991             else
992                 dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
993         }
994         break;
995 #endif
996     default:
997         return AVERROR(ENOSYS);
998     }
999
1000     dst_hwctx->surfaces = s->surfaces_internal;
1001
1002     return 0;
1003 }
1004
1005 static int qsv_map_to(AVHWFramesContext *dst_ctx,
1006                       AVFrame *dst, const AVFrame *src, int flags)
1007 {
1008     AVQSVFramesContext *hwctx = dst_ctx->hwctx;
1009     int i, err;
1010
1011     for (i = 0; i < hwctx->nb_surfaces; i++) {
1012 #if CONFIG_VAAPI
1013         if (*(VASurfaceID*)hwctx->surfaces[i].Data.MemId ==
1014             (VASurfaceID)(uintptr_t)src->data[3])
1015             break;
1016 #endif
1017 #if CONFIG_DXVA2
1018         if ((IDirect3DSurface9*)hwctx->surfaces[i].Data.MemId ==
1019             (IDirect3DSurface9*)(uintptr_t)src->data[3])
1020             break;
1021 #endif
1022     }
1023     if (i >= hwctx->nb_surfaces) {
1024         av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
1025                "is not in the mapped frames context.\n");
1026         return AVERROR(EINVAL);
1027     }
1028
1029     err = ff_hwframe_map_create(dst->hw_frames_ctx,
1030                                 dst, src, NULL, NULL);
1031     if (err)
1032         return err;
1033
1034     dst->width   = src->width;
1035     dst->height  = src->height;
1036     dst->data[3] = (uint8_t*)&hwctx->surfaces[i];
1037
1038     return 0;
1039 }
1040
1041 static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
1042                                       const void *hwconfig,
1043                                       AVHWFramesConstraints *constraints)
1044 {
1045     int i;
1046
1047     constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
1048                                                     sizeof(*constraints->valid_sw_formats));
1049     if (!constraints->valid_sw_formats)
1050         return AVERROR(ENOMEM);
1051
1052     for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
1053         constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
1054     constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
1055
1056     constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
1057     if (!constraints->valid_hw_formats)
1058         return AVERROR(ENOMEM);
1059
1060     constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
1061     constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
1062
1063     return 0;
1064 }
1065
1066 static void qsv_device_free(AVHWDeviceContext *ctx)
1067 {
1068     AVQSVDeviceContext *hwctx = ctx->hwctx;
1069     QSVDevicePriv       *priv = ctx->user_opaque;
1070
1071     if (hwctx->session)
1072         MFXClose(hwctx->session);
1073
1074     av_buffer_unref(&priv->child_device_ctx);
1075     av_freep(&priv);
1076 }
1077
1078 static mfxIMPL choose_implementation(const char *device)
1079 {
1080     static const struct {
1081         const char *name;
1082         mfxIMPL     impl;
1083     } impl_map[] = {
1084         { "auto",     MFX_IMPL_AUTO         },
1085         { "sw",       MFX_IMPL_SOFTWARE     },
1086         { "hw",       MFX_IMPL_HARDWARE     },
1087         { "auto_any", MFX_IMPL_AUTO_ANY     },
1088         { "hw_any",   MFX_IMPL_HARDWARE_ANY },
1089         { "hw2",      MFX_IMPL_HARDWARE2    },
1090         { "hw3",      MFX_IMPL_HARDWARE3    },
1091         { "hw4",      MFX_IMPL_HARDWARE4    },
1092     };
1093
1094     mfxIMPL impl = MFX_IMPL_AUTO_ANY;
1095     int i;
1096
1097     if (device) {
1098         for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
1099             if (!strcmp(device, impl_map[i].name)) {
1100                 impl = impl_map[i].impl;
1101                 break;
1102             }
1103         if (i == FF_ARRAY_ELEMS(impl_map))
1104             impl = strtol(device, NULL, 0);
1105     }
1106
1107     return impl;
1108 }
1109
1110 static int qsv_device_derive_from_child(AVHWDeviceContext *ctx,
1111                                         mfxIMPL implementation,
1112                                         AVHWDeviceContext *child_device_ctx,
1113                                         int flags)
1114 {
1115     AVQSVDeviceContext *hwctx = ctx->hwctx;
1116
1117     mfxVersion    ver = { { 3, 1 } };
1118     mfxHDL        handle;
1119     mfxHandleType handle_type;
1120     mfxStatus     err;
1121     int ret;
1122
1123     switch (child_device_ctx->type) {
1124 #if CONFIG_VAAPI
1125     case AV_HWDEVICE_TYPE_VAAPI:
1126         {
1127             AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1128             handle_type = MFX_HANDLE_VA_DISPLAY;
1129             handle = (mfxHDL)child_device_hwctx->display;
1130         }
1131         break;
1132 #endif
1133 #if CONFIG_DXVA2
1134     case AV_HWDEVICE_TYPE_DXVA2:
1135         {
1136             AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
1137             handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
1138             handle = (mfxHDL)child_device_hwctx->devmgr;
1139         }
1140         break;
1141 #endif
1142     default:
1143         ret = AVERROR(ENOSYS);
1144         goto fail;
1145     }
1146
1147     err = MFXInit(implementation, &ver, &hwctx->session);
1148     if (err != MFX_ERR_NONE) {
1149         av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
1150                "%d.\n", err);
1151         ret = AVERROR_UNKNOWN;
1152         goto fail;
1153     }
1154
1155     err = MFXQueryVersion(hwctx->session, &ver);
1156     if (err != MFX_ERR_NONE) {
1157         av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: %d.\n", err);
1158         ret = AVERROR_UNKNOWN;
1159         goto fail;
1160     }
1161
1162     av_log(ctx, AV_LOG_VERBOSE,
1163            "Initialize MFX session: API version is %d.%d, implementation version is %d.%d\n",
1164            MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
1165
1166     MFXClose(hwctx->session);
1167
1168     err = MFXInit(implementation, &ver, &hwctx->session);
1169     if (err != MFX_ERR_NONE) {
1170         av_log(ctx, AV_LOG_ERROR,
1171                "Error initializing an MFX session: %d.\n", err);
1172         ret = AVERROR_UNKNOWN;
1173         goto fail;
1174     }
1175
1176     err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
1177     if (err != MFX_ERR_NONE) {
1178         av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
1179                "%d\n", err);
1180         ret = AVERROR_UNKNOWN;
1181         goto fail;
1182     }
1183
1184     ret = MFXQueryVersion(hwctx->session,&ver);
1185     if (ret == MFX_ERR_NONE) {
1186         av_log(ctx, AV_LOG_VERBOSE, "MFX compile/runtime API: %d.%d/%d.%d\n",
1187                MFX_VERSION_MAJOR, MFX_VERSION_MINOR, ver.Major, ver.Minor);
1188     }
1189     return 0;
1190
1191 fail:
1192     if (hwctx->session)
1193         MFXClose(hwctx->session);
1194     return ret;
1195 }
1196
1197 static int qsv_device_derive(AVHWDeviceContext *ctx,
1198                              AVHWDeviceContext *child_device_ctx, int flags)
1199 {
1200     return qsv_device_derive_from_child(ctx, MFX_IMPL_HARDWARE_ANY,
1201                                         child_device_ctx, flags);
1202 }
1203
1204 static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
1205                              AVDictionary *opts, int flags)
1206 {
1207     QSVDevicePriv *priv;
1208     enum AVHWDeviceType child_device_type;
1209     AVHWDeviceContext *child_device;
1210     AVDictionaryEntry *e;
1211
1212     mfxIMPL impl;
1213     int ret;
1214
1215     priv = av_mallocz(sizeof(*priv));
1216     if (!priv)
1217         return AVERROR(ENOMEM);
1218
1219     ctx->user_opaque = priv;
1220     ctx->free        = qsv_device_free;
1221
1222     e = av_dict_get(opts, "child_device", NULL, 0);
1223
1224     if (CONFIG_VAAPI)
1225         child_device_type = AV_HWDEVICE_TYPE_VAAPI;
1226     else if (CONFIG_DXVA2)
1227         child_device_type = AV_HWDEVICE_TYPE_DXVA2;
1228     else {
1229         av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
1230         return AVERROR(ENOSYS);
1231     }
1232
1233     ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
1234                                  e ? e->value : NULL, NULL, 0);
1235     if (ret < 0)
1236         return ret;
1237
1238     child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
1239
1240     impl = choose_implementation(device);
1241
1242     return qsv_device_derive_from_child(ctx, impl, child_device, 0);
1243 }
1244
1245 const HWContextType ff_hwcontext_type_qsv = {
1246     .type                   = AV_HWDEVICE_TYPE_QSV,
1247     .name                   = "QSV",
1248
1249     .device_hwctx_size      = sizeof(AVQSVDeviceContext),
1250     .device_priv_size       = sizeof(QSVDeviceContext),
1251     .frames_hwctx_size      = sizeof(AVQSVFramesContext),
1252     .frames_priv_size       = sizeof(QSVFramesContext),
1253
1254     .device_create          = qsv_device_create,
1255     .device_derive          = qsv_device_derive,
1256     .device_init            = qsv_device_init,
1257     .frames_get_constraints = qsv_frames_get_constraints,
1258     .frames_init            = qsv_frames_init,
1259     .frames_uninit          = qsv_frames_uninit,
1260     .frames_get_buffer      = qsv_get_buffer,
1261     .transfer_get_formats   = qsv_transfer_get_formats,
1262     .transfer_data_to       = qsv_transfer_data_to,
1263     .transfer_data_from     = qsv_transfer_data_from,
1264     .map_to                 = qsv_map_to,
1265     .map_from               = qsv_map_from,
1266     .frames_derive_to       = qsv_frames_derive_to,
1267     .frames_derive_from     = qsv_frames_derive_from,
1268
1269     .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },
1270 };