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