1 // FFMPEG Video Encoder Integration for OBS Studio
2 // Copyright (c) 2019 Michael Fabian Dirks <info@xaymar.com>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in all
12 // copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 
22 #pragma once
23 #include "base.hpp"
24 
25 #ifdef _MSC_VER
26 #pragma warning(push)
27 #pragma warning(disable : 4191 4242 4244 4365 4777 4986 5039 5204)
28 #endif
29 #include <atlutil.h>
30 #include <d3d11.h>
31 #include <d3d11_1.h>
32 #include <dxgi.h>
33 #ifdef _MSC_VER
34 #pragma warning(pop)
35 #endif
36 
37 namespace ffmpeg::hwapi {
38 	class d3d11 : public ffmpeg::hwapi::base {
39 		typedef HRESULT(__stdcall* CreateDXGIFactory_t)(REFIID, void**);
40 		typedef HRESULT(__stdcall* CreateDXGIFactory1_t)(REFIID, void**);
41 		typedef HRESULT(__stdcall* D3D11CreateDevice_t)(IDXGIAdapter*, D3D_DRIVER_TYPE, HMODULE, UINT,
42 														CONST D3D_FEATURE_LEVEL*, UINT, UINT, ID3D11Device**,
43 														D3D_FEATURE_LEVEL*, ID3D11DeviceContext**);
44 
45 		HMODULE              _dxgi_module;
46 		CreateDXGIFactory_t  _CreateDXGIFactory;
47 		CreateDXGIFactory1_t _CreateDXGIFactory1;
48 
49 		HMODULE             _d3d11_module;
50 		D3D11CreateDevice_t _D3D11CreateDevice;
51 
52 		ATL::CComPtr<IDXGIFactory1> _dxgifactory;
53 
54 		public:
55 		d3d11();
56 		virtual ~d3d11();
57 
58 		virtual std::list<hwapi::device> enumerate_adapters() override;
59 
60 		virtual std::shared_ptr<hwapi::instance> create(hwapi::device target) override;
61 
62 		virtual std::shared_ptr<hwapi::instance> create_from_obs() override;
63 	};
64 
65 	class d3d11_instance : public ffmpeg::hwapi::instance {
66 		ATL::CComPtr<ID3D11Device>        _device;
67 		ATL::CComPtr<ID3D11DeviceContext> _context;
68 
69 		public:
70 		d3d11_instance(ATL::CComPtr<ID3D11Device> device, ATL::CComPtr<ID3D11DeviceContext> context);
71 		virtual ~d3d11_instance();
72 
73 		virtual AVBufferRef* create_device_context() override;
74 
75 		virtual std::shared_ptr<AVFrame> allocate_frame(AVBufferRef* frames) override;
76 
77 		virtual void copy_from_obs(AVBufferRef* frames, uint32_t handle, uint64_t lock_key, uint64_t* next_lock_key,
78 								   std::shared_ptr<AVFrame> frame) override;
79 
80 		virtual std::shared_ptr<AVFrame> avframe_from_obs(AVBufferRef* frames, uint32_t handle, uint64_t lock_key,
81 														  uint64_t* next_lock_key) override;
82 	};
83 } // namespace ffmpeg::hwapi
84