Debian Codegraph @

doc/spec/source/snippets/prg_surface_sharing.cpp

raw
1 /*############################################################################
2 # Copyright (C) 2020 Intel Corporation
3 #
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
6
7 // Example using Intel® Video Processing Library (Intel® VPL)
8 #include <stdlib.h>
9
10 #include "mfxdefs.h"
11 #include "mfxstructures.h"
12 #include "mfxsession.h"
13 #include "mfxvideo.h"
14
15 /* These macro required for code compilation. */
16 #define UNUSED_PARAM(x) (void)(x)
17
18 static mfxSession session;
19 static mfxSyncPoint syncp;
20 static mfxBitstream bitstream;
21 typedef void *ID3D11Texture2D;
22
23 static int CaptureFrame(ID3D11Texture2D **p)
24 {
25 UNUSED_PARAM(p);
26 return 1;
27 }
28
29 static int RenderFrame(ID3D11Texture2D *p)
30 {
31 UNUSED_PARAM(p);
32 return 1;
33 }
34
35 static void prg_surface_sharing1 () {
36 /*beg1*/
37
38 // get interface with import function
39 mfxMemoryInterface *memoryInterface = nullptr;
40 MFXGetMemoryInterface(session, &memoryInterface);
41
42 // capture desktop as a D3D11 texture using an OS-specific capture API
43 ID3D11Texture2D *pTexture2D;
44 CaptureFrame(&pTexture2D);
45
46 // import D3D11 texture into Intel® VPL, zero-copy (shared) is preferred, copy is permitted if zero-copy is not supported
47 mfxSurfaceD3D11Tex2D d3d11_surface = {};
48 d3d11_surface.SurfaceInterface.Header.SurfaceType = MFX_SURFACE_TYPE_D3D11_TEX2D;
49 d3d11_surface.SurfaceInterface.Header.SurfaceFlags = (MFX_SURFACE_FLAG_IMPORT_SHARED | MFX_SURFACE_FLAG_IMPORT_COPY);
50 d3d11_surface.SurfaceInterface.Header.StructSize = sizeof(mfxSurfaceD3D11Tex2D);
51
52 // pass the pointer to the shared D3D11 texture
53 d3d11_surface.texture2D = pTexture2D;
54
55 // external_surface is a pointer to mfxSurfaceHeader but points to a complete structure of type mfxSurfaceD3D11Tex2D
56 mfxSurfaceHeader *external_surface = reinterpret_cast<mfxSurfaceHeader *>(&d3d11_surface);
57
58 // ImportFrameSurface() will return an Intel® VPL surface which may then be used as input for encode or VPP
59 mfxFrameSurface1 *imported_surface = nullptr;
60 memoryInterface->ImportFrameSurface(memoryInterface, MFX_SURFACE_COMPONENT_ENCODE, external_surface, &imported_surface);
61
62 // encode the surface
63 MFXVideoENCODE_EncodeFrameAsync(session, nullptr, imported_surface, &bitstream, &syncp);
64
65 // release imported surface
66 imported_surface->FrameInterface->Release(imported_surface);
67 /*end1*/
68 }
69
70 static void prg_surface_sharing2 () {
71 /*beg2*/
72
73 // decode frame
74 mfxFrameSurface1 *decoded_surface = nullptr;
75 MFXVideoDECODE_DecodeFrameAsync(session, &bitstream, nullptr, &decoded_surface, &syncp);
76
77 // run VPP on frame
78 mfxFrameSurface1 *vpp_out_surface = nullptr;
79 MFXVideoVPP_ProcessFrameAsync(session, decoded_surface, &vpp_out_surface);
80
81 // release decoded frame (decrease reference count) after passing to VPP
82 decoded_surface->FrameInterface->Release(decoded_surface);
83
84 // export mfxFrameSurface1 from Intel® VPL to a shared D3D11 texture, zero-copy (shared) is enabled
85 mfxSurfaceHeader export_header = {};
86 export_header.SurfaceType = MFX_SURFACE_TYPE_D3D11_TEX2D;
87 export_header.SurfaceFlags = MFX_SURFACE_FLAG_EXPORT_SHARED;
88
89 // exported_surface is a pointer to mfxSurfaceHeader but will point to a complete structure of type mfxSurfaceD3D11Tex2D
90 mfxSurfaceHeader *exported_surface = nullptr;
91 vpp_out_surface->FrameInterface->Export(vpp_out_surface, export_header, &exported_surface);
92
93 // get pointer to the shared D3D11 texture
94 mfxSurfaceD3D11Tex2D *d3d11_surface = reinterpret_cast<mfxSurfaceD3D11Tex2D *>(exported_surface);
95 ID3D11Texture2D *pTexture2D = reinterpret_cast<ID3D11Texture2D *>(d3d11_surface->texture2D);
96
97 // render the D3D11 texture to screen
98 RenderFrame(pTexture2D);
99
100 // release exported surface
101 mfxSurfaceInterface *exported_surface_interface = reinterpret_cast<mfxSurfaceInterface *>(exported_surface);
102 exported_surface_interface->Release(exported_surface_interface);
103
104 // release VPP output frame
105 vpp_out_surface->FrameInterface->Release(vpp_out_surface);
106
107 /*end2*/
108 }