Importing and Exporting Shared Surfaces#
Intel® VPL API 2.10 introduces new interfaces for sharing surfaces between Intel® VPL runtime and other applications, frameworks, and graphics APIs. This functionality is only supported when using the internal memory management model.
Importing a surface enables Intel® VPL to access raw video data as input to encode or VPP
operations without first mapping the data to system memory and then copying it to
a surface allocated by Intel® VPL runtime using mfxFrameSurfaceInterface::Map
.
Exporting a surface similarly enables the application to access raw video
data which is the output of decode or VPP operations and which was allocated by
Intel® VPL runtime, without first mapping to system memory using
mfxFrameSurfaceInterface::Map
.
Import Example#
The following code snippet shows an example of importing a shared surface.
1// get interface with import function
2mfxMemoryInterface *memoryInterface = nullptr;
3MFXGetMemoryInterface(session, &memoryInterface);
4
5// capture desktop as a D3D11 texture using an OS-specific capture API
6ID3D11Texture2D *pTexture2D;
7CaptureFrame(&pTexture2D);
8
9// import D3D11 texture into Intel® VPL, zero-copy (shared) is preferred, copy is permitted if zero-copy is not supported
10mfxSurfaceD3D11Tex2D d3d11_surface = {};
11d3d11_surface.SurfaceInterface.Header.SurfaceType = MFX_SURFACE_TYPE_D3D11_TEX2D;
12d3d11_surface.SurfaceInterface.Header.SurfaceFlags = (MFX_SURFACE_FLAG_IMPORT_SHARED | MFX_SURFACE_FLAG_IMPORT_COPY);
13d3d11_surface.SurfaceInterface.Header.StructSize = sizeof(mfxSurfaceD3D11Tex2D);
14
15// pass the pointer to the shared D3D11 texture
16d3d11_surface.texture2D = pTexture2D;
17
18// external_surface is a pointer to mfxSurfaceHeader but points to a complete structure of type mfxSurfaceD3D11Tex2D
19mfxSurfaceHeader *external_surface = reinterpret_cast<mfxSurfaceHeader *>(&d3d11_surface);
20
21// ImportFrameSurface() will return an Intel® VPL surface which may then be used as input for encode or VPP
22mfxFrameSurface1 *imported_surface = nullptr;
23memoryInterface->ImportFrameSurface(memoryInterface, MFX_SURFACE_COMPONENT_ENCODE, external_surface, &imported_surface);
24
25// encode the surface
26MFXVideoENCODE_EncodeFrameAsync(session, nullptr, imported_surface, &bitstream, &syncp);
27
28// release imported surface
29imported_surface->FrameInterface->Release(imported_surface);
Export Example#
The following code snippet shows an example of exporting a shared surface.
1// decode frame
2mfxFrameSurface1 *decoded_surface = nullptr;
3MFXVideoDECODE_DecodeFrameAsync(session, &bitstream, nullptr, &decoded_surface, &syncp);
4
5// run VPP on frame
6mfxFrameSurface1 *vpp_out_surface = nullptr;
7MFXVideoVPP_ProcessFrameAsync(session, decoded_surface, &vpp_out_surface);
8
9// release decoded frame (decrease reference count) after passing to VPP
10decoded_surface->FrameInterface->Release(decoded_surface);
11
12// export mfxFrameSurface1 from Intel® VPL to a shared D3D11 texture, zero-copy (shared) is enabled
13mfxSurfaceHeader export_header = {};
14export_header.SurfaceType = MFX_SURFACE_TYPE_D3D11_TEX2D;
15export_header.SurfaceFlags = MFX_SURFACE_FLAG_EXPORT_SHARED;
16
17// exported_surface is a pointer to mfxSurfaceHeader but will point to a complete structure of type mfxSurfaceD3D11Tex2D
18mfxSurfaceHeader *exported_surface = nullptr;
19vpp_out_surface->FrameInterface->Export(vpp_out_surface, export_header, &exported_surface);
20
21// get pointer to the shared D3D11 texture
22mfxSurfaceD3D11Tex2D *d3d11_surface = reinterpret_cast<mfxSurfaceD3D11Tex2D *>(exported_surface);
23ID3D11Texture2D *pTexture2D = reinterpret_cast<ID3D11Texture2D *>(d3d11_surface->texture2D);
24
25// render the D3D11 texture to screen
26RenderFrame(pTexture2D);
27
28// release exported surface
29mfxSurfaceInterface *exported_surface_interface = reinterpret_cast<mfxSurfaceInterface *>(exported_surface);
30exported_surface_interface->Release(exported_surface_interface);
31
32// release VPP output frame
33vpp_out_surface->FrameInterface->Release(vpp_out_surface);