forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwcontext_qsv.c
2644 lines (2255 loc) · 83.3 KB
/
hwcontext_qsv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdatomic.h>
#include <stdint.h>
#include <string.h>
#include <mfxvideo.h>
#include "config.h"
#if HAVE_PTHREADS
#include <pthread.h>
#endif
#define COBJMACROS
#if CONFIG_VAAPI
#include "hwcontext_vaapi.h"
#endif
#if CONFIG_D3D11VA
#include "hwcontext_d3d11va.h"
#endif
#if CONFIG_DXVA2
#include <initguid.h>
#include "hwcontext_dxva2.h"
#endif
#include "buffer.h"
#include "common.h"
#include "hwcontext.h"
#include "hwcontext_internal.h"
#include "hwcontext_qsv.h"
#include "mem.h"
#include "pixfmt.h"
#include "pixdesc.h"
#include "time.h"
#include "imgutils.h"
#include "avassert.h"
#define QSV_VERSION_ATLEAST(MAJOR, MINOR) \
(MFX_VERSION_MAJOR > (MAJOR) || \
MFX_VERSION_MAJOR == (MAJOR) && MFX_VERSION_MINOR >= (MINOR))
#define QSV_RUNTIME_VERSION_ATLEAST(MFX_VERSION, MAJOR, MINOR) \
((MFX_VERSION.Major > (MAJOR)) || \
(MFX_VERSION.Major == (MAJOR) && MFX_VERSION.Minor >= (MINOR)))
#define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
#define QSV_ONEVPL QSV_VERSION_ATLEAST(2, 0)
#define QSV_HAVE_OPAQUE !QSV_ONEVPL
#if QSV_ONEVPL
#include <mfxdispatcher.h>
#else
#define MFXUnload(a) do { } while(0)
#endif
typedef struct QSVDevicePriv {
AVBufferRef *child_device_ctx;
} QSVDevicePriv;
typedef struct QSVDeviceContext {
/**
* The public AVQSVDeviceContext. See hwcontext_qsv.h for it.
*/
AVQSVDeviceContext p;
mfxHDL handle;
mfxHandleType handle_type;
mfxVersion ver;
mfxIMPL impl;
enum AVHWDeviceType child_device_type;
enum AVPixelFormat child_pix_fmt;
} QSVDeviceContext;
typedef struct QSVFramesContext {
/**
* The public AVQSVFramesContext. See hwcontext_qsv.h for it.
*/
AVQSVFramesContext p;
mfxSession session_download;
atomic_int session_download_init;
mfxSession session_upload;
atomic_int session_upload_init;
#if HAVE_PTHREADS
pthread_mutex_t session_lock;
#endif
AVBufferRef *child_frames_ref;
mfxFrameSurface1 *surfaces_internal;
mfxHDLPair *handle_pairs_internal;
int nb_surfaces_used;
// used in the frame allocator for non-opaque surfaces
mfxMemId *mem_ids;
#if QSV_HAVE_OPAQUE
// used in the opaque alloc request for opaque surfaces
mfxFrameSurface1 **surface_ptrs;
mfxExtOpaqueSurfaceAlloc opaque_alloc;
mfxExtBuffer *ext_buffers[1];
#endif
AVFrame realigned_upload_frame;
AVFrame realigned_download_frame;
mfxFrameInfo frame_info;
} QSVFramesContext;
typedef struct QSVSurface {
mfxFrameSurface1 mfx_surface;
AVFrame *child_frame;
} QSVSurface;
static const struct {
enum AVPixelFormat pix_fmt;
uint32_t fourcc;
uint16_t mfx_shift;
} supported_pixel_formats[] = {
{ AV_PIX_FMT_NV12, MFX_FOURCC_NV12, 0 },
{ AV_PIX_FMT_BGRA, MFX_FOURCC_RGB4, 0 },
{ AV_PIX_FMT_P010, MFX_FOURCC_P010, 1 },
{ AV_PIX_FMT_PAL8, MFX_FOURCC_P8, 0 },
{ AV_PIX_FMT_YUYV422,
MFX_FOURCC_YUY2, 0 },
#if CONFIG_VAAPI
{ AV_PIX_FMT_UYVY422,
MFX_FOURCC_UYVY, 0 },
#endif
{ AV_PIX_FMT_Y210,
MFX_FOURCC_Y210, 1 },
// VUYX is used for VAAPI child device,
// the SDK only delares support for AYUV
{ AV_PIX_FMT_VUYX,
MFX_FOURCC_AYUV, 0 },
// XV30 is used for VAAPI child device,
// the SDK only delares support for Y410
{ AV_PIX_FMT_XV30,
MFX_FOURCC_Y410, 0 },
#if QSV_VERSION_ATLEAST(1, 31)
// P012 is used for VAAPI child device,
// the SDK only delares support for P016
{ AV_PIX_FMT_P012,
MFX_FOURCC_P016, 1 },
// Y212 is used for VAAPI child device,
// the SDK only delares support for Y216
{ AV_PIX_FMT_Y212,
MFX_FOURCC_Y216, 1 },
// XV36 is used for VAAPI child device,
// the SDK only delares support for Y416
{ AV_PIX_FMT_XV36,
MFX_FOURCC_Y416, 1 },
#endif
};
extern int ff_qsv_get_surface_base_handle(mfxFrameSurface1 *surf,
enum AVHWDeviceType base_dev_type,
void **base_handle);
static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf);
/**
* Caller needs to allocate enough space for base_handle pointer.
**/
int ff_qsv_get_surface_base_handle(mfxFrameSurface1 *surf,
enum AVHWDeviceType base_dev_type,
void **base_handle)
{
mfxHDLPair *handle_pair;
handle_pair = surf->Data.MemId;
switch (base_dev_type) {
#if CONFIG_VAAPI
case AV_HWDEVICE_TYPE_VAAPI:
base_handle[0] = handle_pair->first;
return 0;
#endif
#if CONFIG_D3D11VA
case AV_HWDEVICE_TYPE_D3D11VA:
base_handle[0] = handle_pair->first;
base_handle[1] = handle_pair->second;
return 0;
#endif
#if CONFIG_DXVA2
case AV_HWDEVICE_TYPE_DXVA2:
base_handle[0] = handle_pair->first;
return 0;
#endif
}
return AVERROR(EINVAL);
}
static uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
{
int i;
for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
if (supported_pixel_formats[i].pix_fmt == pix_fmt)
return supported_pixel_formats[i].fourcc;
}
return 0;
}
static uint16_t qsv_shift_from_pix_fmt(enum AVPixelFormat pix_fmt)
{
for (int i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
if (supported_pixel_formats[i].pix_fmt == pix_fmt)
return supported_pixel_formats[i].mfx_shift;
}
return 0;
}
#if CONFIG_D3D11VA
static uint32_t qsv_get_d3d11va_bind_flags(int mem_type)
{
uint32_t bind_flags = 0;
if ((mem_type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) && (mem_type & MFX_MEMTYPE_INTERNAL_FRAME))
bind_flags = D3D11_BIND_DECODER | D3D11_BIND_VIDEO_ENCODER;
else
bind_flags = D3D11_BIND_DECODER;
if ((MFX_MEMTYPE_FROM_VPPOUT & mem_type) || (MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET & mem_type))
bind_flags = D3D11_BIND_RENDER_TARGET;
return bind_flags;
}
#endif
static int qsv_fill_border(AVFrame *dst, const AVFrame *src)
{
const AVPixFmtDescriptor *desc;
int i, planes_nb = 0;
if (dst->format != src->format)
return AVERROR(EINVAL);
desc = av_pix_fmt_desc_get(dst->format);
for (i = 0; i < desc->nb_components; i++)
planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
for (i = 0; i < planes_nb; i++) {
int sheight, dheight, y;
ptrdiff_t swidth = av_image_get_linesize(src->format,
src->width,
i);
ptrdiff_t dwidth = av_image_get_linesize(dst->format,
dst->width,
i);
const AVComponentDescriptor comp = desc->comp[i];
if (swidth < 0 || dwidth < 0) {
av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
return AVERROR(EINVAL);
}
sheight = src->height;
dheight = dst->height;
if (i) {
sheight = AV_CEIL_RSHIFT(src->height, desc->log2_chroma_h);
dheight = AV_CEIL_RSHIFT(dst->height, desc->log2_chroma_h);
}
//fill right padding
for (y = 0; y < sheight; y++) {
void *line_ptr = dst->data[i] + y*dst->linesize[i] + swidth;
av_memcpy_backptr(line_ptr,
comp.depth > 8 ? 2 : 1,
dwidth - swidth);
}
//fill bottom padding
for (y = sheight; y < dheight; y++) {
memcpy(dst->data[i]+y*dst->linesize[i],
dst->data[i]+(sheight-1)*dst->linesize[i],
dwidth);
}
}
return 0;
}
static int qsv_device_init(AVHWDeviceContext *ctx)
{
QSVDeviceContext *s = ctx->hwctx;
AVQSVDeviceContext *hwctx = &s->p;
int hw_handle_supported = 0;
mfxHandleType handle_type;
enum AVHWDeviceType device_type;
enum AVPixelFormat pix_fmt;
mfxStatus err;
err = MFXQueryIMPL(hwctx->session, &s->impl);
if (err == MFX_ERR_NONE)
err = MFXQueryVersion(hwctx->session, &s->ver);
if (err != MFX_ERR_NONE) {
av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
return AVERROR_UNKNOWN;
}
if (MFX_IMPL_VIA_VAAPI == MFX_IMPL_VIA_MASK(s->impl)) {
#if CONFIG_VAAPI
handle_type = MFX_HANDLE_VA_DISPLAY;
device_type = AV_HWDEVICE_TYPE_VAAPI;
pix_fmt = AV_PIX_FMT_VAAPI;
hw_handle_supported = 1;
#endif
} else if (MFX_IMPL_VIA_D3D11 == MFX_IMPL_VIA_MASK(s->impl)) {
#if CONFIG_D3D11VA
handle_type = MFX_HANDLE_D3D11_DEVICE;
device_type = AV_HWDEVICE_TYPE_D3D11VA;
pix_fmt = AV_PIX_FMT_D3D11;
hw_handle_supported = 1;
#endif
} else if (MFX_IMPL_VIA_D3D9 == MFX_IMPL_VIA_MASK(s->impl)) {
#if CONFIG_DXVA2
handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
device_type = AV_HWDEVICE_TYPE_DXVA2;
pix_fmt = AV_PIX_FMT_DXVA2_VLD;
hw_handle_supported = 1;
#endif
}
if (hw_handle_supported) {
err = MFXVideoCORE_GetHandle(hwctx->session, handle_type, &s->handle);
if (err == MFX_ERR_NONE) {
s->handle_type = handle_type;
s->child_device_type = device_type;
s->child_pix_fmt = pix_fmt;
}
}
if (!s->handle) {
av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
"from the session\n");
}
return 0;
}
static void qsv_frames_uninit(AVHWFramesContext *ctx)
{
QSVFramesContext *s = ctx->hwctx;
if (s->session_download) {
MFXVideoVPP_Close(s->session_download);
MFXClose(s->session_download);
}
s->session_download = NULL;
s->session_download_init = 0;
if (s->session_upload) {
MFXVideoVPP_Close(s->session_upload);
MFXClose(s->session_upload);
}
s->session_upload = NULL;
s->session_upload_init = 0;
#if HAVE_PTHREADS
pthread_mutex_destroy(&s->session_lock);
#endif
av_freep(&s->mem_ids);
#if QSV_HAVE_OPAQUE
av_freep(&s->surface_ptrs);
#endif
av_freep(&s->surfaces_internal);
av_freep(&s->handle_pairs_internal);
av_frame_unref(&s->realigned_upload_frame);
av_frame_unref(&s->realigned_download_frame);
av_buffer_unref(&s->child_frames_ref);
}
static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
{
}
static void qsv_pool_release(void *opaque, uint8_t *data)
{
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
QSVFramesContext *s = ctx->hwctx;
QSVSurface *qsv_surface = (QSVSurface *)data;
mfxHDLPair *hdl_pair = (mfxHDLPair *)qsv_surface->mfx_surface.Data.MemId;
AVHWFramesContext *child_frames_ctx;
if (!s->child_frames_ref)
return;
child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
if (!child_frames_ctx->device_ctx)
return;
#if CONFIG_VAAPI
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_VAAPI)
av_freep(&hdl_pair->first);
#endif
av_freep(&hdl_pair);
av_frame_free(&qsv_surface->child_frame);
av_freep(&qsv_surface);
}
static AVBufferRef *qsv_fixed_pool_alloc(void *opaque, size_t size)
{
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
QSVFramesContext *s = ctx->hwctx;
AVQSVFramesContext *hwctx = &s->p;
if (s->nb_surfaces_used < hwctx->nb_surfaces) {
s->nb_surfaces_used++;
return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
}
return NULL;
}
static AVBufferRef *qsv_dynamic_pool_alloc(void *opaque, size_t size)
{
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
QSVFramesContext *s = ctx->hwctx;
AVHWFramesContext *child_frames_ctx;
QSVSurface *qsv_surface = NULL;
mfxHDLPair *handle_pairs_internal = NULL;
int ret;
if (!s->child_frames_ref)
goto fail;
child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
if (!child_frames_ctx->device_ctx)
goto fail;
#if CONFIG_DXVA2
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
av_log(ctx, AV_LOG_ERROR,
"QSV on dxva2 requires a fixed frame pool size\n");
goto fail;
}
#endif
qsv_surface = av_calloc(1, sizeof(*qsv_surface));
if (!qsv_surface)
goto fail;
qsv_surface->child_frame = av_frame_alloc();
if (!qsv_surface->child_frame)
goto fail;
ret = av_hwframe_get_buffer(s->child_frames_ref, qsv_surface->child_frame, 0);
if (ret < 0)
goto fail;
handle_pairs_internal = av_calloc(1, sizeof(*handle_pairs_internal));
if (!handle_pairs_internal)
goto fail;
ret = qsv_init_surface(ctx, &qsv_surface->mfx_surface);
if (ret < 0)
goto fail;
#if CONFIG_VAAPI
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
VASurfaceID *surface_id_internal;
surface_id_internal = av_calloc(1, sizeof(*surface_id_internal));
if (!surface_id_internal)
goto fail;
*surface_id_internal = (VASurfaceID)(uintptr_t)qsv_surface->child_frame->data[3];
handle_pairs_internal->first = (mfxHDL)surface_id_internal;
handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
}
#endif
#if CONFIG_D3D11VA
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
AVD3D11VAFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
handle_pairs_internal->first = (mfxMemId)qsv_surface->child_frame->data[0];
if (child_frames_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET)
handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
else
handle_pairs_internal->second = (mfxMemId)qsv_surface->child_frame->data[1];
}
#endif
qsv_surface->mfx_surface.Data.MemId = (mfxMemId)handle_pairs_internal;
return av_buffer_create((uint8_t *)qsv_surface, sizeof(*qsv_surface),
qsv_pool_release, ctx, 0);
fail:
if (qsv_surface) {
av_frame_free(&qsv_surface->child_frame);
}
av_freep(&qsv_surface);
av_freep(&handle_pairs_internal);
return NULL;
}
static AVBufferRef *qsv_pool_alloc(void *opaque, size_t size)
{
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
AVQSVFramesContext *hwctx = ctx->hwctx;
if (hwctx->nb_surfaces == 0) {
return qsv_dynamic_pool_alloc(opaque, size);
} else {
return qsv_fixed_pool_alloc(opaque, size);
}
}
static int qsv_init_child_ctx(AVHWFramesContext *ctx)
{
QSVDeviceContext *device_priv = ctx->device_ctx->hwctx;
QSVFramesContext *s = ctx->hwctx;
AVQSVFramesContext *hwctx = &s->p;
AVBufferRef *child_device_ref = NULL;
AVBufferRef *child_frames_ref = NULL;
AVHWDeviceContext *child_device_ctx;
AVHWFramesContext *child_frames_ctx;
int i, ret = 0;
if (!device_priv->handle) {
av_log(ctx, AV_LOG_ERROR,
"Cannot create a non-opaque internal surface pool without "
"a hardware handle\n");
return AVERROR(EINVAL);
}
child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
if (!child_device_ref)
return AVERROR(ENOMEM);
child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
#if CONFIG_VAAPI
if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
child_device_hwctx->display = (VADisplay)device_priv->handle;
}
#endif
#if CONFIG_D3D11VA
if (child_device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
AVD3D11VADeviceContext *child_device_hwctx = child_device_ctx->hwctx;
ID3D11Device_AddRef((ID3D11Device*)device_priv->handle);
child_device_hwctx->device = (ID3D11Device*)device_priv->handle;
}
#endif
#if CONFIG_DXVA2
if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
}
#endif
ret = av_hwdevice_ctx_init(child_device_ref);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
goto fail;
}
child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
if (!child_frames_ref) {
ret = AVERROR(ENOMEM);
goto fail;
}
child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
child_frames_ctx->format = device_priv->child_pix_fmt;
child_frames_ctx->sw_format = ctx->sw_format;
child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
child_frames_ctx->width = FFALIGN(ctx->width, 16);
child_frames_ctx->height = FFALIGN(ctx->height, 16);
#if CONFIG_D3D11VA
if (child_device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
AVD3D11VAFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
if (hwctx->frame_type == 0)
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
if (hwctx->frame_type & MFX_MEMTYPE_SHARED_RESOURCE)
child_frames_hwctx->MiscFlags = D3D11_RESOURCE_MISC_SHARED;
child_frames_hwctx->BindFlags = qsv_get_d3d11va_bind_flags(hwctx->frame_type);
}
#endif
#if CONFIG_DXVA2
if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
else
child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
}
#endif
ret = av_hwframe_ctx_init(child_frames_ref);
if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
goto fail;
}
#if CONFIG_VAAPI
if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
for (i = 0; i < ctx->initial_pool_size; i++) {
s->handle_pairs_internal[i].first = child_frames_hwctx->surface_ids + i;
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
}
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
}
#endif
#if CONFIG_D3D11VA
if (child_device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
AVD3D11VAFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
for (i = 0; i < ctx->initial_pool_size; i++) {
s->handle_pairs_internal[i].first = (mfxMemId)child_frames_hwctx->texture_infos[i].texture;
if(child_frames_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
} else {
s->handle_pairs_internal[i].second = (mfxMemId)child_frames_hwctx->texture_infos[i].index;
}
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
}
if (child_frames_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
} else {
hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
}
}
#endif
#if CONFIG_DXVA2
if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
for (i = 0; i < ctx->initial_pool_size; i++) {
s->handle_pairs_internal[i].first = (mfxMemId)child_frames_hwctx->surfaces[i];
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
}
if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
else
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
}
#endif
s->child_frames_ref = child_frames_ref;
child_frames_ref = NULL;
fail:
av_buffer_unref(&child_device_ref);
av_buffer_unref(&child_frames_ref);
return ret;
}
static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
{
const AVPixFmtDescriptor *desc;
uint32_t fourcc;
desc = av_pix_fmt_desc_get(ctx->sw_format);
if (!desc)
return AVERROR(EINVAL);
fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
if (!fourcc)
return AVERROR(EINVAL);
surf->Info.BitDepthLuma = desc->comp[0].depth;
surf->Info.BitDepthChroma = desc->comp[0].depth;
surf->Info.Shift = qsv_shift_from_pix_fmt(ctx->sw_format);
if (desc->log2_chroma_w && desc->log2_chroma_h)
surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
else if (desc->log2_chroma_w)
surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
else
surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
surf->Info.FourCC = fourcc;
surf->Info.Width = FFALIGN(ctx->width, 16);
surf->Info.CropW = ctx->width;
surf->Info.Height = FFALIGN(ctx->height, 16);
surf->Info.CropH = ctx->height;
surf->Info.FrameRateExtN = 25;
surf->Info.FrameRateExtD = 1;
surf->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
return 0;
}
static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
{
QSVFramesContext *s = ctx->hwctx;
AVQSVFramesContext *frames_hwctx = &s->p;
int i, ret = 0;
if (ctx->initial_pool_size < 0) {
av_log(ctx, AV_LOG_ERROR, "Invalid frame pool size\n");
return AVERROR(EINVAL);
} else if (ctx->initial_pool_size == 0) {
mfxFrameSurface1 mfx_surf1;
ret = qsv_init_child_ctx(ctx);
if (ret < 0)
return ret;
ffhwframesctx(ctx)->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
ctx, qsv_pool_alloc, NULL);
if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM);
memset(&mfx_surf1, 0, sizeof(mfx_surf1));
qsv_init_surface(ctx, &mfx_surf1);
s->frame_info = mfx_surf1.Info;
frames_hwctx->info = &s->frame_info;
frames_hwctx->nb_surfaces = 0;
return 0;
}
s->handle_pairs_internal = av_calloc(ctx->initial_pool_size,
sizeof(*s->handle_pairs_internal));
if (!s->handle_pairs_internal)
return AVERROR(ENOMEM);
s->surfaces_internal = av_calloc(ctx->initial_pool_size,
sizeof(*s->surfaces_internal));
if (!s->surfaces_internal)
return AVERROR(ENOMEM);
for (i = 0; i < ctx->initial_pool_size; i++) {
ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
if (ret < 0)
return ret;
}
#if QSV_HAVE_OPAQUE
if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
ret = qsv_init_child_ctx(ctx);
if (ret < 0)
return ret;
}
#else
ret = qsv_init_child_ctx(ctx);
if (ret < 0)
return ret;
#endif
ffhwframesctx(ctx)->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
ctx, qsv_pool_alloc, NULL);
if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM);
frames_hwctx->surfaces = s->surfaces_internal;
frames_hwctx->nb_surfaces = ctx->initial_pool_size;
return 0;
}
static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
mfxFrameAllocResponse *resp)
{
AVHWFramesContext *ctx = pthis;
QSVFramesContext *s = ctx->hwctx;
AVQSVFramesContext *hwctx = &s->p;
mfxFrameInfo *i = &req->Info;
mfxFrameInfo *i1 = hwctx->nb_surfaces ? &hwctx->surfaces[0].Info : hwctx->info;
if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
!(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
!(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
return MFX_ERR_UNSUPPORTED;
if (i->Width > i1->Width || i->Height > i1->Height ||
i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
"allocation request: %dx%d %d %d vs %dx%d %d %d\n",
i->Width, i->Height, i->FourCC, i->ChromaFormat,
i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
return MFX_ERR_UNSUPPORTED;
}
resp->mids = s->mem_ids;
resp->NumFrameActual = hwctx->nb_surfaces;
return MFX_ERR_NONE;
}
static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
{
return MFX_ERR_NONE;
}
static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
{
return MFX_ERR_UNSUPPORTED;
}
static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
{
return MFX_ERR_UNSUPPORTED;
}
static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
{
mfxHDLPair *pair_dst = (mfxHDLPair*)hdl;
mfxHDLPair *pair_src = (mfxHDLPair*)mid;
pair_dst->first = pair_src->first;
if (pair_src->second != (mfxMemId)MFX_INFINITE)
pair_dst->second = pair_src->second;
return MFX_ERR_NONE;
}
#if QSV_ONEVPL
static int qsv_d3d11_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
{
int ret = AVERROR_UNKNOWN;
#if CONFIG_D3D11VA
mfxStatus sts;
IDXGIAdapter *pDXGIAdapter;
DXGI_ADAPTER_DESC adapterDesc;
IDXGIDevice *pDXGIDevice = NULL;
HRESULT hr;
ID3D11Device *device = handle;
mfxVariant impl_value = {0};
hr = ID3D11Device_QueryInterface(device, &IID_IDXGIDevice, (void**)&pDXGIDevice);
if (SUCCEEDED(hr)) {
hr = IDXGIDevice_GetAdapter(pDXGIDevice, &pDXGIAdapter);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error IDXGIDevice_GetAdapter %d\n", hr);
IDXGIDevice_Release(pDXGIDevice);
return ret;
}
hr = IDXGIAdapter_GetDesc(pDXGIAdapter, &adapterDesc);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error IDXGIAdapter_GetDesc %d\n", hr);
goto fail;
}
} else {
av_log(ctx, AV_LOG_ERROR, "Error ID3D11Device_QueryInterface %d\n", hr);
return ret;
}
impl_value.Type = MFX_VARIANT_TYPE_U16;
impl_value.Data.U16 = adapterDesc.DeviceId;
sts = MFXSetConfigFilterProperty(cfg,
(const mfxU8 *)"mfxExtendedDeviceId.DeviceID", impl_value);
if (sts != MFX_ERR_NONE) {
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
"DeviceID property: %d.\n", sts);
goto fail;
}
impl_value.Type = MFX_VARIANT_TYPE_PTR;
impl_value.Data.Ptr = &adapterDesc.AdapterLuid;
sts = MFXSetConfigFilterProperty(cfg,
(const mfxU8 *)"mfxExtendedDeviceId.DeviceLUID", impl_value);
if (sts != MFX_ERR_NONE) {
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
"DeviceLUID property: %d.\n", sts);
goto fail;
}
impl_value.Type = MFX_VARIANT_TYPE_U32;
impl_value.Data.U32 = 0x0001;
sts = MFXSetConfigFilterProperty(cfg,
(const mfxU8 *)"mfxExtendedDeviceId.LUIDDeviceNodeMask", impl_value);
if (sts != MFX_ERR_NONE) {
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
"LUIDDeviceNodeMask property: %d.\n", sts);
goto fail;
}
ret = 0;
fail:
IDXGIAdapter_Release(pDXGIAdapter);
IDXGIDevice_Release(pDXGIDevice);
#endif
return ret;
}
static int qsv_d3d9_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
{
int ret = AVERROR_UNKNOWN;
#if CONFIG_DXVA2
mfxStatus sts;
IDirect3DDeviceManager9* devmgr = handle;
IDirect3DDevice9 *device = NULL;
IDirect3DDevice9Ex *device_ex = NULL;
HANDLE device_handle = 0;
IDirect3D9Ex *d3d9ex = NULL;
IDirect3D9 *d3d9 = NULL;
LUID luid;
D3DDEVICE_CREATION_PARAMETERS params;
HRESULT hr;
mfxVariant impl_value = {0};
hr = IDirect3DDeviceManager9_OpenDeviceHandle(devmgr, &device_handle);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error OpenDeviceHandle %d\n", hr);
goto fail;
}
hr = IDirect3DDeviceManager9_LockDevice(devmgr, device_handle, &device, TRUE);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error LockDevice %d\n", hr);
IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
goto fail;
}
hr = IDirect3DDevice9_QueryInterface(device, &IID_IDirect3DDevice9Ex, (void **)&device_ex);
IDirect3DDevice9_Release(device);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9_QueryInterface %d\n", hr);
goto unlock;
}
hr = IDirect3DDevice9Ex_GetCreationParameters(device_ex, ¶ms);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9_GetCreationParameters %d\n", hr);
IDirect3DDevice9Ex_Release(device_ex);
goto unlock;
}
hr = IDirect3DDevice9Ex_GetDirect3D(device_ex, &d3d9);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9Ex_GetDirect3D %d\n", hr);
IDirect3DDevice9Ex_Release(device_ex);
goto unlock;
}
hr = IDirect3D9_QueryInterface(d3d9, &IID_IDirect3D9Ex, (void **)&d3d9ex);
IDirect3D9_Release(d3d9);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error IDirect3D9_QueryInterface3D %d\n", hr);
IDirect3DDevice9Ex_Release(device_ex);
goto unlock;
}
hr = IDirect3D9Ex_GetAdapterLUID(d3d9ex, params.AdapterOrdinal, &luid);
if (FAILED(hr)) {
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9Ex_GetAdapterLUID %d\n", hr);
goto release;
}
impl_value.Type = MFX_VARIANT_TYPE_PTR;
impl_value.Data.Ptr = &luid;
sts = MFXSetConfigFilterProperty(cfg,
(const mfxU8 *)"mfxExtendedDeviceId.DeviceLUID", impl_value);
if (sts != MFX_ERR_NONE) {
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
"DeviceLUID property: %d.\n", sts);
goto release;
}
ret = 0;
release:
IDirect3D9Ex_Release(d3d9ex);
IDirect3DDevice9Ex_Release(device_ex);
unlock:
IDirect3DDeviceManager9_UnlockDevice(devmgr, device_handle, FALSE);
IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
fail:
#endif
return ret;
}
static int qsv_va_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
{
#if CONFIG_VAAPI
#if VA_CHECK_VERSION(1, 15, 0)
mfxStatus sts;
VADisplay dpy = handle;
VAStatus vas;
VADisplayAttribute attr = {
.type = VADisplayPCIID,
};
mfxVariant impl_value = {0};
vas = vaGetDisplayAttributes(dpy, &attr, 1);