texture-amf.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. #include <util/threading.h>
  2. #include <opts-parser.h>
  3. #include <obs-module.h>
  4. #include <obs-avc.h>
  5. #include "obs-ffmpeg-config.h"
  6. #include <unordered_map>
  7. #include <cstdlib>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include <mutex>
  12. #include <deque>
  13. #include <map>
  14. #include "external/AMF/include/components/VideoEncoderHEVC.h"
  15. #include "external/AMF/include/components/VideoEncoderVCE.h"
  16. #include "external/AMF/include/components/VideoEncoderAV1.h"
  17. #include "external/AMF/include/core/Factory.h"
  18. #include "external/AMF/include/core/Trace.h"
  19. #include <dxgi.h>
  20. #include <d3d11.h>
  21. #include <d3d11_1.h>
  22. #include <util/windows/HRError.hpp>
  23. #include <util/windows/ComPtr.hpp>
  24. #include <util/platform.h>
  25. #include <util/util.hpp>
  26. #include <util/pipe.h>
  27. #include <util/dstr.h>
  28. using namespace amf;
  29. /* ========================================================================= */
  30. /* Junk */
  31. #define do_log(level, format, ...) \
  32. blog(level, "[%s: '%s'] " format, enc->encoder_str, \
  33. obs_encoder_get_name(enc->encoder), ##__VA_ARGS__)
  34. #define error(format, ...) do_log(LOG_ERROR, format, ##__VA_ARGS__)
  35. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  36. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  37. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  38. struct amf_error {
  39. const char *str;
  40. AMF_RESULT res;
  41. inline amf_error(const char *str, AMF_RESULT res) : str(str), res(res)
  42. {
  43. }
  44. };
  45. struct handle_tex {
  46. uint32_t handle;
  47. ComPtr<ID3D11Texture2D> tex;
  48. ComPtr<IDXGIKeyedMutex> km;
  49. };
  50. struct adapter_caps {
  51. bool is_amd = false;
  52. bool supports_avc = false;
  53. bool supports_hevc = false;
  54. bool supports_av1 = false;
  55. };
  56. /* ------------------------------------------------------------------------- */
  57. static std::map<uint32_t, adapter_caps> caps;
  58. static bool h264_supported = false;
  59. static AMFFactory *amf_factory = nullptr;
  60. static AMFTrace *amf_trace = nullptr;
  61. static HMODULE amf_module = nullptr;
  62. static uint64_t amf_version = 0;
  63. /* ========================================================================= */
  64. /* Main Implementation */
  65. enum class amf_codec_type {
  66. AVC,
  67. HEVC,
  68. AV1,
  69. };
  70. struct amf_base {
  71. obs_encoder_t *encoder;
  72. const char *encoder_str;
  73. amf_codec_type codec;
  74. bool fallback;
  75. AMFContextPtr amf_context;
  76. AMFComponentPtr amf_encoder;
  77. AMFBufferPtr packet_data;
  78. AMFRate amf_frame_rate;
  79. AMFBufferPtr header;
  80. std::deque<AMFDataPtr> queued_packets;
  81. AMF_VIDEO_CONVERTER_COLOR_PROFILE_ENUM amf_color_profile;
  82. AMF_COLOR_TRANSFER_CHARACTERISTIC_ENUM amf_characteristic;
  83. AMF_COLOR_PRIMARIES_ENUM amf_primaries;
  84. AMF_SURFACE_FORMAT amf_format;
  85. amf_int64 max_throughput = 0;
  86. amf_int64 throughput = 0;
  87. int64_t dts_offset = 0;
  88. uint32_t cx;
  89. uint32_t cy;
  90. uint32_t linesize = 0;
  91. int fps_num;
  92. int fps_den;
  93. bool full_range;
  94. bool bframes_supported = false;
  95. bool first_update = true;
  96. inline amf_base(bool fallback) : fallback(fallback) {}
  97. virtual ~amf_base() = default;
  98. virtual void init() = 0;
  99. };
  100. using d3dtex_t = ComPtr<ID3D11Texture2D>;
  101. using buf_t = std::vector<uint8_t>;
  102. struct amf_texencode : amf_base, public AMFSurfaceObserver {
  103. volatile bool destroying = false;
  104. std::vector<handle_tex> input_textures;
  105. std::mutex textures_mutex;
  106. std::vector<d3dtex_t> available_textures;
  107. std::unordered_map<AMFSurface *, d3dtex_t> active_textures;
  108. ComPtr<ID3D11Device> device;
  109. ComPtr<ID3D11DeviceContext> context;
  110. inline amf_texencode() : amf_base(false) {}
  111. ~amf_texencode() { os_atomic_set_bool(&destroying, true); }
  112. void AMF_STD_CALL OnSurfaceDataRelease(amf::AMFSurface *surf) override
  113. {
  114. if (os_atomic_load_bool(&destroying))
  115. return;
  116. std::scoped_lock lock(textures_mutex);
  117. auto it = active_textures.find(surf);
  118. if (it != active_textures.end()) {
  119. available_textures.push_back(it->second);
  120. active_textures.erase(it);
  121. }
  122. }
  123. void init() override
  124. {
  125. AMF_RESULT res = amf_context->InitDX11(device, AMF_DX11_1);
  126. if (res != AMF_OK)
  127. throw amf_error("InitDX11 failed", res);
  128. }
  129. };
  130. struct amf_fallback : amf_base, public AMFSurfaceObserver {
  131. volatile bool destroying = false;
  132. std::mutex buffers_mutex;
  133. std::vector<buf_t> available_buffers;
  134. std::unordered_map<AMFSurface *, buf_t> active_buffers;
  135. inline amf_fallback() : amf_base(true) {}
  136. ~amf_fallback() { os_atomic_set_bool(&destroying, true); }
  137. void AMF_STD_CALL OnSurfaceDataRelease(amf::AMFSurface *surf) override
  138. {
  139. if (os_atomic_load_bool(&destroying))
  140. return;
  141. std::scoped_lock lock(buffers_mutex);
  142. auto it = active_buffers.find(surf);
  143. if (it != active_buffers.end()) {
  144. available_buffers.push_back(std::move(it->second));
  145. active_buffers.erase(it);
  146. }
  147. }
  148. void init() override
  149. {
  150. AMF_RESULT res = amf_context->InitDX11(nullptr, AMF_DX11_1);
  151. if (res != AMF_OK)
  152. throw amf_error("InitDX11 failed", res);
  153. }
  154. };
  155. /* ------------------------------------------------------------------------- */
  156. /* More garbage */
  157. template<typename T>
  158. static bool get_amf_property(amf_base *enc, const wchar_t *name, T *value)
  159. {
  160. AMF_RESULT res = enc->amf_encoder->GetProperty(name, value);
  161. return res == AMF_OK;
  162. }
  163. template<typename T>
  164. static void set_amf_property(amf_base *enc, const wchar_t *name, const T &value)
  165. {
  166. AMF_RESULT res = enc->amf_encoder->SetProperty(name, value);
  167. if (res != AMF_OK)
  168. error("Failed to set property '%ls': %ls", name,
  169. amf_trace->GetResultText(res));
  170. }
  171. #define set_avc_property(enc, name, value) \
  172. set_amf_property(enc, AMF_VIDEO_ENCODER_##name, value)
  173. #define set_hevc_property(enc, name, value) \
  174. set_amf_property(enc, AMF_VIDEO_ENCODER_HEVC_##name, value)
  175. #define set_av1_property(enc, name, value) \
  176. set_amf_property(enc, AMF_VIDEO_ENCODER_AV1_##name, value)
  177. #define get_avc_property(enc, name, value) \
  178. get_amf_property(enc, AMF_VIDEO_ENCODER_##name, value)
  179. #define get_hevc_property(enc, name, value) \
  180. get_amf_property(enc, AMF_VIDEO_ENCODER_HEVC_##name, value)
  181. #define get_av1_property(enc, name, value) \
  182. get_amf_property(enc, AMF_VIDEO_ENCODER_AV1_##name, value)
  183. #define get_opt_name(name) \
  184. ((enc->codec == amf_codec_type::AVC) ? AMF_VIDEO_ENCODER_##name \
  185. : (enc->codec == amf_codec_type::HEVC) \
  186. ? AMF_VIDEO_ENCODER_HEVC_##name \
  187. : AMF_VIDEO_ENCODER_AV1_##name)
  188. #define set_opt(name, value) set_amf_property(enc, get_opt_name(name), value)
  189. #define get_opt(name, value) get_amf_property(enc, get_opt_name(name), value)
  190. #define set_avc_opt(name, value) set_avc_property(enc, name, value)
  191. #define set_hevc_opt(name, value) set_hevc_property(enc, name, value)
  192. #define set_av1_opt(name, value) set_av1_property(enc, name, value)
  193. #define set_enum_opt(name, value) \
  194. set_amf_property(enc, get_opt_name(name), get_opt_name(name##_##value))
  195. #define set_avc_enum(name, value) \
  196. set_avc_property(enc, name, AMF_VIDEO_ENCODER_##name##_##value)
  197. #define set_hevc_enum(name, value) \
  198. set_hevc_property(enc, name, AMF_VIDEO_ENCODER_HEVC_##name##_##value)
  199. #define set_av1_enum(name, value) \
  200. set_av1_property(enc, name, AMF_VIDEO_ENCODER_AV1_##name##_##value)
  201. /* ------------------------------------------------------------------------- */
  202. /* Implementation */
  203. static HMODULE get_lib(const char *lib)
  204. {
  205. HMODULE mod = GetModuleHandleA(lib);
  206. if (mod)
  207. return mod;
  208. return LoadLibraryA(lib);
  209. }
  210. #define AMD_VENDOR_ID 0x1002
  211. typedef HRESULT(WINAPI *CREATEDXGIFACTORY1PROC)(REFIID, void **);
  212. static bool amf_init_d3d11(amf_texencode *enc)
  213. try {
  214. HMODULE dxgi = get_lib("DXGI.dll");
  215. HMODULE d3d11 = get_lib("D3D11.dll");
  216. CREATEDXGIFACTORY1PROC create_dxgi;
  217. PFN_D3D11_CREATE_DEVICE create_device;
  218. ComPtr<IDXGIFactory> factory;
  219. ComPtr<ID3D11Device> device;
  220. ComPtr<ID3D11DeviceContext> context;
  221. ComPtr<IDXGIAdapter> adapter;
  222. DXGI_ADAPTER_DESC desc;
  223. HRESULT hr;
  224. if (!dxgi || !d3d11)
  225. throw "Couldn't get D3D11/DXGI libraries? "
  226. "That definitely shouldn't be possible.";
  227. create_dxgi = (CREATEDXGIFACTORY1PROC)GetProcAddress(
  228. dxgi, "CreateDXGIFactory1");
  229. create_device = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(
  230. d3d11, "D3D11CreateDevice");
  231. if (!create_dxgi || !create_device)
  232. throw "Failed to load D3D11/DXGI procedures";
  233. hr = create_dxgi(__uuidof(IDXGIFactory2), (void **)&factory);
  234. if (FAILED(hr))
  235. throw HRError("CreateDXGIFactory1 failed", hr);
  236. obs_video_info ovi;
  237. obs_get_video_info(&ovi);
  238. hr = factory->EnumAdapters(ovi.adapter, &adapter);
  239. if (FAILED(hr))
  240. throw HRError("EnumAdapters failed", hr);
  241. adapter->GetDesc(&desc);
  242. if (desc.VendorId != AMD_VENDOR_ID)
  243. throw "Seems somehow AMF is trying to initialize "
  244. "on a non-AMD adapter";
  245. hr = create_device(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0,
  246. nullptr, 0, D3D11_SDK_VERSION, &device, nullptr,
  247. &context);
  248. if (FAILED(hr))
  249. throw HRError("D3D11CreateDevice failed", hr);
  250. enc->device = device;
  251. enc->context = context;
  252. return true;
  253. } catch (const HRError &err) {
  254. error("%s: %s: 0x%lX", __FUNCTION__, err.str, err.hr);
  255. return false;
  256. } catch (const char *err) {
  257. error("%s: %s", __FUNCTION__, err);
  258. return false;
  259. }
  260. static void add_output_tex(amf_texencode *enc,
  261. ComPtr<ID3D11Texture2D> &output_tex,
  262. ID3D11Texture2D *from)
  263. {
  264. ID3D11Device *device = enc->device;
  265. HRESULT hr;
  266. D3D11_TEXTURE2D_DESC desc;
  267. from->GetDesc(&desc);
  268. desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  269. desc.MiscFlags = 0;
  270. hr = device->CreateTexture2D(&desc, nullptr, &output_tex);
  271. if (FAILED(hr))
  272. throw HRError("Failed to create texture", hr);
  273. }
  274. static inline bool get_available_tex(amf_texencode *enc,
  275. ComPtr<ID3D11Texture2D> &output_tex)
  276. {
  277. std::scoped_lock lock(enc->textures_mutex);
  278. if (enc->available_textures.size()) {
  279. output_tex = enc->available_textures.back();
  280. enc->available_textures.pop_back();
  281. return true;
  282. }
  283. return false;
  284. }
  285. static inline void get_output_tex(amf_texencode *enc,
  286. ComPtr<ID3D11Texture2D> &output_tex,
  287. ID3D11Texture2D *from)
  288. {
  289. if (!get_available_tex(enc, output_tex))
  290. add_output_tex(enc, output_tex, from);
  291. }
  292. static void get_tex_from_handle(amf_texencode *enc, uint32_t handle,
  293. IDXGIKeyedMutex **km_out,
  294. ID3D11Texture2D **tex_out)
  295. {
  296. ID3D11Device *device = enc->device;
  297. ComPtr<ID3D11Texture2D> tex;
  298. HRESULT hr;
  299. for (size_t i = 0; i < enc->input_textures.size(); i++) {
  300. struct handle_tex &ht = enc->input_textures[i];
  301. if (ht.handle == handle) {
  302. ht.km.CopyTo(km_out);
  303. ht.tex.CopyTo(tex_out);
  304. return;
  305. }
  306. }
  307. hr = device->OpenSharedResource((HANDLE)(uintptr_t)handle,
  308. __uuidof(ID3D11Resource),
  309. (void **)&tex);
  310. if (FAILED(hr))
  311. throw HRError("OpenSharedResource failed", hr);
  312. ComQIPtr<IDXGIKeyedMutex> km(tex);
  313. if (!km)
  314. throw "QueryInterface(IDXGIKeyedMutex) failed";
  315. tex->SetEvictionPriority(DXGI_RESOURCE_PRIORITY_MAXIMUM);
  316. struct handle_tex new_ht = {handle, tex, km};
  317. enc->input_textures.push_back(std::move(new_ht));
  318. *km_out = km.Detach();
  319. *tex_out = tex.Detach();
  320. }
  321. static constexpr amf_int64 macroblock_size = 16;
  322. static inline void calc_throughput(amf_base *enc)
  323. {
  324. amf_int64 mb_cx =
  325. ((amf_int64)enc->cx + (macroblock_size - 1)) / macroblock_size;
  326. amf_int64 mb_cy =
  327. ((amf_int64)enc->cy + (macroblock_size - 1)) / macroblock_size;
  328. amf_int64 mb_frame = mb_cx * mb_cy;
  329. enc->throughput =
  330. mb_frame * (amf_int64)enc->fps_num / (amf_int64)enc->fps_den;
  331. }
  332. static inline int get_avc_preset(amf_base *enc, const char *preset);
  333. #if ENABLE_HEVC
  334. static inline int get_hevc_preset(amf_base *enc, const char *preset);
  335. #endif
  336. static inline int get_av1_preset(amf_base *enc, const char *preset);
  337. static inline int get_preset(amf_base *enc, const char *preset)
  338. {
  339. if (enc->codec == amf_codec_type::AVC)
  340. return get_avc_preset(enc, preset);
  341. #if ENABLE_HEVC
  342. else if (enc->codec == amf_codec_type::HEVC)
  343. return get_hevc_preset(enc, preset);
  344. #endif
  345. else if (enc->codec == amf_codec_type::AV1)
  346. return get_av1_preset(enc, preset);
  347. return 0;
  348. }
  349. static inline void refresh_throughput_caps(amf_base *enc, const char *&preset)
  350. {
  351. AMF_RESULT res = AMF_OK;
  352. AMFCapsPtr caps;
  353. set_opt(QUALITY_PRESET, get_preset(enc, preset));
  354. res = enc->amf_encoder->GetCaps(&caps);
  355. if (res == AMF_OK) {
  356. caps->GetProperty(get_opt_name(CAP_MAX_THROUGHPUT),
  357. &enc->max_throughput);
  358. }
  359. }
  360. static inline void check_preset_compatibility(amf_base *enc,
  361. const char *&preset)
  362. {
  363. /* The throughput depends on the current preset and the other static
  364. * encoder properties. If the throughput is lower than the max
  365. * throughput, switch to a lower preset. */
  366. if (astrcmpi(preset, "highQuality") == 0) {
  367. if (!enc->max_throughput) {
  368. preset = "quality";
  369. set_opt(QUALITY_PRESET, get_preset(enc, preset));
  370. } else {
  371. if (enc->max_throughput < enc->throughput) {
  372. preset = "quality";
  373. refresh_throughput_caps(enc, preset);
  374. }
  375. }
  376. }
  377. if (astrcmpi(preset, "quality") == 0) {
  378. if (!enc->max_throughput) {
  379. preset = "balanced";
  380. set_opt(QUALITY_PRESET, get_preset(enc, preset));
  381. } else {
  382. if (enc->max_throughput < enc->throughput) {
  383. preset = "balanced";
  384. refresh_throughput_caps(enc, preset);
  385. }
  386. }
  387. }
  388. if (astrcmpi(preset, "balanced") == 0) {
  389. if (enc->max_throughput &&
  390. enc->max_throughput < enc->throughput) {
  391. preset = "speed";
  392. refresh_throughput_caps(enc, preset);
  393. }
  394. }
  395. }
  396. static inline int64_t convert_to_amf_ts(amf_base *enc, int64_t ts)
  397. {
  398. constexpr int64_t amf_timebase = AMF_SECOND;
  399. return ts * amf_timebase / (int64_t)enc->fps_den;
  400. }
  401. static inline int64_t convert_to_obs_ts(amf_base *enc, int64_t ts)
  402. {
  403. constexpr int64_t amf_timebase = AMF_SECOND;
  404. return ts * (int64_t)enc->fps_den / amf_timebase;
  405. }
  406. static void convert_to_encoder_packet(amf_base *enc, AMFDataPtr &data,
  407. encoder_packet *packet)
  408. {
  409. if (!data)
  410. return;
  411. enc->packet_data = AMFBufferPtr(data);
  412. data->GetProperty(L"PTS", &packet->pts);
  413. const wchar_t *get_output_type;
  414. switch (enc->codec) {
  415. case amf_codec_type::AVC:
  416. get_output_type = AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE;
  417. break;
  418. case amf_codec_type::HEVC:
  419. get_output_type = AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE;
  420. break;
  421. case amf_codec_type::AV1:
  422. get_output_type = AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE;
  423. break;
  424. }
  425. uint64_t type = 0;
  426. AMF_RESULT res = data->GetProperty(get_output_type, &type);
  427. if (res != AMF_OK)
  428. throw amf_error("Failed to GetProperty(): encoder output "
  429. "data type",
  430. res);
  431. if (enc->codec == amf_codec_type::AVC ||
  432. enc->codec == amf_codec_type::HEVC) {
  433. switch (type) {
  434. case AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR:
  435. packet->priority = OBS_NAL_PRIORITY_HIGHEST;
  436. break;
  437. case AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_I:
  438. packet->priority = OBS_NAL_PRIORITY_HIGH;
  439. break;
  440. case AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_P:
  441. packet->priority = OBS_NAL_PRIORITY_LOW;
  442. break;
  443. case AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_B:
  444. packet->priority = OBS_NAL_PRIORITY_DISPOSABLE;
  445. break;
  446. }
  447. } else if (enc->codec == amf_codec_type::AV1) {
  448. switch (type) {
  449. case AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_KEY:
  450. packet->priority = OBS_NAL_PRIORITY_HIGHEST;
  451. break;
  452. case AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_INTRA_ONLY:
  453. packet->priority = OBS_NAL_PRIORITY_HIGH;
  454. break;
  455. case AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_INTER:
  456. packet->priority = OBS_NAL_PRIORITY_LOW;
  457. break;
  458. case AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_SWITCH:
  459. packet->priority = OBS_NAL_PRIORITY_DISPOSABLE;
  460. break;
  461. case AMF_VIDEO_ENCODER_AV1_OUTPUT_FRAME_TYPE_SHOW_EXISTING:
  462. packet->priority = OBS_NAL_PRIORITY_DISPOSABLE;
  463. break;
  464. }
  465. }
  466. packet->data = (uint8_t *)enc->packet_data->GetNative();
  467. packet->size = enc->packet_data->GetSize();
  468. packet->type = OBS_ENCODER_VIDEO;
  469. packet->dts = convert_to_obs_ts(enc, data->GetPts());
  470. packet->keyframe = type == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR;
  471. if (enc->dts_offset)
  472. packet->dts -= enc->dts_offset;
  473. }
  474. #ifndef SEC_TO_NSEC
  475. #define SEC_TO_NSEC 1000000000ULL
  476. #endif
  477. static void amf_encode_base(amf_base *enc, AMFSurface *amf_surf,
  478. encoder_packet *packet, bool *received_packet)
  479. {
  480. auto &queued_packets = enc->queued_packets;
  481. uint64_t ts_start = os_gettime_ns();
  482. AMF_RESULT res;
  483. *received_packet = false;
  484. bool waiting = true;
  485. while (waiting) {
  486. /* ----------------------------------- */
  487. /* submit frame */
  488. res = enc->amf_encoder->SubmitInput(amf_surf);
  489. if (res == AMF_OK || res == AMF_NEED_MORE_INPUT) {
  490. waiting = false;
  491. } else if (res == AMF_INPUT_FULL) {
  492. os_sleep_ms(1);
  493. uint64_t duration = os_gettime_ns() - ts_start;
  494. constexpr uint64_t timeout = 5 * SEC_TO_NSEC;
  495. if (duration >= timeout) {
  496. throw amf_error("SubmitInput timed out", res);
  497. }
  498. } else {
  499. throw amf_error("SubmitInput failed", res);
  500. }
  501. /* ----------------------------------- */
  502. /* query as many packets as possible */
  503. AMFDataPtr new_packet;
  504. do {
  505. res = enc->amf_encoder->QueryOutput(&new_packet);
  506. if (new_packet)
  507. queued_packets.push_back(new_packet);
  508. if (res != AMF_REPEAT && res != AMF_OK) {
  509. throw amf_error("QueryOutput failed", res);
  510. }
  511. } while (!!new_packet);
  512. }
  513. /* ----------------------------------- */
  514. /* return a packet if available */
  515. if (queued_packets.size()) {
  516. AMFDataPtr amf_out;
  517. amf_out = queued_packets.front();
  518. queued_packets.pop_front();
  519. *received_packet = true;
  520. convert_to_encoder_packet(enc, amf_out, packet);
  521. }
  522. }
  523. static bool amf_encode_tex(void *data, uint32_t handle, int64_t pts,
  524. uint64_t lock_key, uint64_t *next_key,
  525. encoder_packet *packet, bool *received_packet)
  526. try {
  527. amf_texencode *enc = (amf_texencode *)data;
  528. ID3D11DeviceContext *context = enc->context;
  529. ComPtr<ID3D11Texture2D> output_tex;
  530. ComPtr<ID3D11Texture2D> input_tex;
  531. ComPtr<IDXGIKeyedMutex> km;
  532. AMFSurfacePtr amf_surf;
  533. AMF_RESULT res;
  534. if (handle == GS_INVALID_HANDLE) {
  535. *next_key = lock_key;
  536. throw "Encode failed: bad texture handle";
  537. }
  538. /* ------------------------------------ */
  539. /* get the input tex */
  540. get_tex_from_handle(enc, handle, &km, &input_tex);
  541. /* ------------------------------------ */
  542. /* get an output tex */
  543. get_output_tex(enc, output_tex, input_tex);
  544. /* ------------------------------------ */
  545. /* copy to output tex */
  546. km->AcquireSync(lock_key, INFINITE);
  547. context->CopyResource((ID3D11Resource *)output_tex.Get(),
  548. (ID3D11Resource *)input_tex.Get());
  549. context->Flush();
  550. km->ReleaseSync(*next_key);
  551. /* ------------------------------------ */
  552. /* map output tex to amf surface */
  553. res = enc->amf_context->CreateSurfaceFromDX11Native(output_tex,
  554. &amf_surf, enc);
  555. if (res != AMF_OK)
  556. throw amf_error("CreateSurfaceFromDX11Native failed", res);
  557. int64_t last_ts = convert_to_amf_ts(enc, pts - 1);
  558. int64_t cur_ts = convert_to_amf_ts(enc, pts);
  559. amf_surf->SetPts(cur_ts);
  560. amf_surf->SetProperty(L"PTS", pts);
  561. {
  562. std::scoped_lock lock(enc->textures_mutex);
  563. enc->active_textures[amf_surf.GetPtr()] = output_tex;
  564. }
  565. /* ------------------------------------ */
  566. /* do actual encode */
  567. amf_encode_base(enc, amf_surf, packet, received_packet);
  568. return true;
  569. } catch (const char *err) {
  570. amf_texencode *enc = (amf_texencode *)data;
  571. error("%s: %s", __FUNCTION__, err);
  572. return false;
  573. } catch (const amf_error &err) {
  574. amf_texencode *enc = (amf_texencode *)data;
  575. error("%s: %s: %ls", __FUNCTION__, err.str,
  576. amf_trace->GetResultText(err.res));
  577. *received_packet = false;
  578. return false;
  579. } catch (const HRError &err) {
  580. amf_texencode *enc = (amf_texencode *)data;
  581. error("%s: %s: 0x%lX", __FUNCTION__, err.str, err.hr);
  582. *received_packet = false;
  583. return false;
  584. }
  585. static buf_t alloc_buf(amf_fallback *enc)
  586. {
  587. buf_t buf;
  588. size_t size;
  589. if (enc->amf_format == AMF_SURFACE_NV12) {
  590. size = enc->linesize * enc->cy * 2;
  591. } else if (enc->amf_format == AMF_SURFACE_RGBA) {
  592. size = enc->linesize * enc->cy * 4;
  593. } else if (enc->amf_format == AMF_SURFACE_P010) {
  594. size = enc->linesize * enc->cy * 2 * 2;
  595. } else {
  596. throw "Invalid amf_format";
  597. }
  598. buf.resize(size);
  599. return buf;
  600. }
  601. static buf_t get_buf(amf_fallback *enc)
  602. {
  603. std::scoped_lock lock(enc->buffers_mutex);
  604. buf_t buf;
  605. if (enc->available_buffers.size()) {
  606. buf = std::move(enc->available_buffers.back());
  607. enc->available_buffers.pop_back();
  608. } else {
  609. buf = alloc_buf(enc);
  610. }
  611. return buf;
  612. }
  613. static inline void copy_frame_data(amf_fallback *enc, buf_t &buf,
  614. struct encoder_frame *frame)
  615. {
  616. uint8_t *dst = &buf[0];
  617. if (enc->amf_format == AMF_SURFACE_NV12 ||
  618. enc->amf_format == AMF_SURFACE_P010) {
  619. size_t size = enc->linesize * enc->cy;
  620. memcpy(&buf[0], frame->data[0], size);
  621. memcpy(&buf[size], frame->data[1], size / 2);
  622. } else if (enc->amf_format == AMF_SURFACE_RGBA) {
  623. memcpy(dst, frame->data[0], enc->linesize * enc->cy);
  624. }
  625. }
  626. static bool amf_encode_fallback(void *data, struct encoder_frame *frame,
  627. struct encoder_packet *packet,
  628. bool *received_packet)
  629. try {
  630. amf_fallback *enc = (amf_fallback *)data;
  631. AMFSurfacePtr amf_surf;
  632. AMF_RESULT res;
  633. buf_t buf;
  634. if (!enc->linesize)
  635. enc->linesize = frame->linesize[0];
  636. buf = get_buf(enc);
  637. copy_frame_data(enc, buf, frame);
  638. res = enc->amf_context->CreateSurfaceFromHostNative(
  639. enc->amf_format, enc->cx, enc->cy, enc->linesize, 0, &buf[0],
  640. &amf_surf, enc);
  641. if (res != AMF_OK)
  642. throw amf_error("CreateSurfaceFromHostNative failed", res);
  643. int64_t last_ts = convert_to_amf_ts(enc, frame->pts - 1);
  644. int64_t cur_ts = convert_to_amf_ts(enc, frame->pts);
  645. amf_surf->SetPts(cur_ts);
  646. amf_surf->SetProperty(L"PTS", frame->pts);
  647. {
  648. std::scoped_lock lock(enc->buffers_mutex);
  649. enc->active_buffers[amf_surf.GetPtr()] = std::move(buf);
  650. }
  651. /* ------------------------------------ */
  652. /* do actual encode */
  653. amf_encode_base(enc, amf_surf, packet, received_packet);
  654. return true;
  655. } catch (const amf_error &err) {
  656. amf_fallback *enc = (amf_fallback *)data;
  657. error("%s: %s: %ls", __FUNCTION__, err.str,
  658. amf_trace->GetResultText(err.res));
  659. *received_packet = false;
  660. return false;
  661. } catch (const char *err) {
  662. amf_fallback *enc = (amf_fallback *)data;
  663. error("%s: %s", __FUNCTION__, err);
  664. *received_packet = false;
  665. return false;
  666. }
  667. static bool amf_extra_data(void *data, uint8_t **header, size_t *size)
  668. {
  669. amf_base *enc = (amf_base *)data;
  670. if (!enc->header)
  671. return false;
  672. *header = (uint8_t *)enc->header->GetNative();
  673. *size = enc->header->GetSize();
  674. return true;
  675. }
  676. static void h264_video_info_fallback(void *, struct video_scale_info *info)
  677. {
  678. switch (info->format) {
  679. case VIDEO_FORMAT_RGBA:
  680. case VIDEO_FORMAT_BGRA:
  681. case VIDEO_FORMAT_BGRX:
  682. info->format = VIDEO_FORMAT_RGBA;
  683. break;
  684. default:
  685. info->format = VIDEO_FORMAT_NV12;
  686. break;
  687. }
  688. }
  689. static void h265_video_info_fallback(void *, struct video_scale_info *info)
  690. {
  691. switch (info->format) {
  692. case VIDEO_FORMAT_RGBA:
  693. case VIDEO_FORMAT_BGRA:
  694. case VIDEO_FORMAT_BGRX:
  695. info->format = VIDEO_FORMAT_RGBA;
  696. break;
  697. case VIDEO_FORMAT_I010:
  698. case VIDEO_FORMAT_P010:
  699. info->format = VIDEO_FORMAT_P010;
  700. break;
  701. default:
  702. info->format = VIDEO_FORMAT_NV12;
  703. }
  704. }
  705. static void av1_video_info_fallback(void *, struct video_scale_info *info)
  706. {
  707. switch (info->format) {
  708. case VIDEO_FORMAT_RGBA:
  709. case VIDEO_FORMAT_BGRA:
  710. case VIDEO_FORMAT_BGRX:
  711. info->format = VIDEO_FORMAT_RGBA;
  712. break;
  713. case VIDEO_FORMAT_I010:
  714. case VIDEO_FORMAT_P010:
  715. info->format = VIDEO_FORMAT_P010;
  716. break;
  717. default:
  718. info->format = VIDEO_FORMAT_NV12;
  719. }
  720. }
  721. static bool amf_create_encoder(amf_base *enc)
  722. try {
  723. AMF_RESULT res;
  724. /* ------------------------------------ */
  725. /* get video info */
  726. struct obs_video_info ovi;
  727. obs_get_video_info(&ovi);
  728. struct video_scale_info info;
  729. info.format = ovi.output_format;
  730. info.colorspace = ovi.colorspace;
  731. info.range = ovi.range;
  732. if (enc->fallback) {
  733. if (enc->codec == amf_codec_type::AVC)
  734. h264_video_info_fallback(NULL, &info);
  735. else if (enc->codec == amf_codec_type::HEVC)
  736. h265_video_info_fallback(NULL, &info);
  737. else
  738. av1_video_info_fallback(NULL, &info);
  739. }
  740. enc->cx = obs_encoder_get_width(enc->encoder);
  741. enc->cy = obs_encoder_get_height(enc->encoder);
  742. enc->amf_frame_rate = AMFConstructRate(ovi.fps_num, ovi.fps_den);
  743. enc->fps_num = (int)ovi.fps_num;
  744. enc->fps_den = (int)ovi.fps_den;
  745. enc->full_range = info.range == VIDEO_RANGE_FULL;
  746. switch (info.colorspace) {
  747. case VIDEO_CS_601:
  748. enc->amf_color_profile =
  749. enc->full_range
  750. ? AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_601
  751. : AMF_VIDEO_CONVERTER_COLOR_PROFILE_601;
  752. enc->amf_primaries = AMF_COLOR_PRIMARIES_SMPTE170M;
  753. enc->amf_characteristic =
  754. AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE170M;
  755. break;
  756. case VIDEO_CS_DEFAULT:
  757. case VIDEO_CS_709:
  758. enc->amf_color_profile =
  759. enc->full_range
  760. ? AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709
  761. : AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
  762. enc->amf_primaries = AMF_COLOR_PRIMARIES_BT709;
  763. enc->amf_characteristic =
  764. AMF_COLOR_TRANSFER_CHARACTERISTIC_BT709;
  765. break;
  766. case VIDEO_CS_SRGB:
  767. enc->amf_color_profile =
  768. enc->full_range
  769. ? AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_709
  770. : AMF_VIDEO_CONVERTER_COLOR_PROFILE_709;
  771. enc->amf_primaries = AMF_COLOR_PRIMARIES_BT709;
  772. enc->amf_characteristic =
  773. AMF_COLOR_TRANSFER_CHARACTERISTIC_IEC61966_2_1;
  774. break;
  775. case VIDEO_CS_2100_HLG:
  776. enc->amf_color_profile =
  777. enc->full_range
  778. ? AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020
  779. : AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
  780. enc->amf_primaries = AMF_COLOR_PRIMARIES_BT2020;
  781. enc->amf_characteristic =
  782. AMF_COLOR_TRANSFER_CHARACTERISTIC_ARIB_STD_B67;
  783. break;
  784. case VIDEO_CS_2100_PQ:
  785. enc->amf_color_profile =
  786. enc->full_range
  787. ? AMF_VIDEO_CONVERTER_COLOR_PROFILE_FULL_2020
  788. : AMF_VIDEO_CONVERTER_COLOR_PROFILE_2020;
  789. enc->amf_primaries = AMF_COLOR_PRIMARIES_BT2020;
  790. enc->amf_characteristic =
  791. AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE2084;
  792. break;
  793. }
  794. switch (info.format) {
  795. case VIDEO_FORMAT_NV12:
  796. enc->amf_format = AMF_SURFACE_NV12;
  797. break;
  798. case VIDEO_FORMAT_P010:
  799. enc->amf_format = AMF_SURFACE_P010;
  800. break;
  801. case VIDEO_FORMAT_RGBA:
  802. enc->amf_format = AMF_SURFACE_RGBA;
  803. break;
  804. }
  805. /* ------------------------------------ */
  806. /* create encoder */
  807. res = amf_factory->CreateContext(&enc->amf_context);
  808. if (res != AMF_OK)
  809. throw amf_error("CreateContext failed", res);
  810. enc->init();
  811. const wchar_t *codec = nullptr;
  812. switch (enc->codec) {
  813. case (amf_codec_type::AVC):
  814. codec = AMFVideoEncoderVCE_AVC;
  815. break;
  816. case (amf_codec_type::HEVC):
  817. codec = AMFVideoEncoder_HEVC;
  818. break;
  819. case (amf_codec_type::AV1):
  820. codec = AMFVideoEncoder_AV1;
  821. break;
  822. default:
  823. codec = AMFVideoEncoder_HEVC;
  824. }
  825. res = amf_factory->CreateComponent(enc->amf_context, codec,
  826. &enc->amf_encoder);
  827. if (res != AMF_OK)
  828. throw amf_error("CreateComponent failed", res);
  829. calc_throughput(enc);
  830. return true;
  831. } catch (const amf_error &err) {
  832. error("%s: %s: %ls", __FUNCTION__, err.str,
  833. amf_trace->GetResultText(err.res));
  834. return false;
  835. }
  836. static void amf_destroy(void *data)
  837. {
  838. amf_base *enc = (amf_base *)data;
  839. delete enc;
  840. }
  841. static void check_texture_encode_capability(obs_encoder_t *encoder,
  842. amf_codec_type codec)
  843. {
  844. obs_video_info ovi;
  845. obs_get_video_info(&ovi);
  846. bool avc = amf_codec_type::AVC == codec;
  847. bool hevc = amf_codec_type::HEVC == codec;
  848. bool av1 = amf_codec_type::AV1 == codec;
  849. if (obs_encoder_scaling_enabled(encoder))
  850. throw "Encoder scaling is active";
  851. if (hevc || av1) {
  852. if (!obs_nv12_tex_active() && !obs_p010_tex_active())
  853. throw "NV12/P010 textures aren't active";
  854. } else if (!obs_nv12_tex_active()) {
  855. throw "NV12 textures aren't active";
  856. }
  857. video_t *video = obs_encoder_video(encoder);
  858. const struct video_output_info *voi = video_output_get_info(video);
  859. switch (voi->format) {
  860. case VIDEO_FORMAT_I010:
  861. case VIDEO_FORMAT_P010:
  862. break;
  863. default:
  864. switch (voi->colorspace) {
  865. case VIDEO_CS_2100_PQ:
  866. case VIDEO_CS_2100_HLG:
  867. throw "OBS does not support 8-bit output of Rec. 2100";
  868. }
  869. }
  870. if ((avc && !caps[ovi.adapter].supports_avc) ||
  871. (hevc && !caps[ovi.adapter].supports_hevc) ||
  872. (av1 && !caps[ovi.adapter].supports_av1))
  873. throw "Wrong adapter";
  874. }
  875. #include "texture-amf-opts.hpp"
  876. static void amf_defaults(obs_data_t *settings)
  877. {
  878. obs_data_set_default_int(settings, "bitrate", 2500);
  879. obs_data_set_default_int(settings, "cqp", 20);
  880. obs_data_set_default_string(settings, "rate_control", "CBR");
  881. obs_data_set_default_string(settings, "preset", "quality");
  882. obs_data_set_default_string(settings, "profile", "high");
  883. }
  884. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  885. obs_data_t *settings)
  886. {
  887. const char *rc = obs_data_get_string(settings, "rate_control");
  888. bool cqp = astrcmpi(rc, "CQP") == 0;
  889. p = obs_properties_get(ppts, "bitrate");
  890. obs_property_set_visible(p, !cqp);
  891. p = obs_properties_get(ppts, "cqp");
  892. obs_property_set_visible(p, cqp);
  893. return true;
  894. }
  895. static obs_properties_t *amf_properties_internal(amf_codec_type codec)
  896. {
  897. obs_properties_t *props = obs_properties_create();
  898. obs_property_t *p;
  899. p = obs_properties_add_list(props, "rate_control",
  900. obs_module_text("RateControl"),
  901. OBS_COMBO_TYPE_LIST,
  902. OBS_COMBO_FORMAT_STRING);
  903. obs_property_list_add_string(p, "CBR", "CBR");
  904. obs_property_list_add_string(p, "CQP", "CQP");
  905. obs_property_list_add_string(p, "VBR", "VBR");
  906. if (amf_codec_type::AV1 == codec) {
  907. obs_property_list_add_string(p, "VBR_LAT", "VBR_LAT");
  908. obs_property_list_add_string(p, "QVBR", "QVBR");
  909. obs_property_list_add_string(p, "HQVBR", "HQVBR");
  910. obs_property_list_add_string(p, "HQCBR", "HQCBR");
  911. }
  912. obs_property_set_modified_callback(p, rate_control_modified);
  913. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  914. 50, 100000, 50);
  915. obs_property_int_set_suffix(p, " Kbps");
  916. obs_properties_add_int(props, "cqp", obs_module_text("NVENC.CQLevel"),
  917. 0, codec == amf_codec_type::AV1 ? 63 : 51, 1);
  918. p = obs_properties_add_int(props, "keyint_sec",
  919. obs_module_text("KeyframeIntervalSec"), 0,
  920. 10, 1);
  921. obs_property_int_set_suffix(p, " s");
  922. p = obs_properties_add_list(props, "preset", obs_module_text("Preset"),
  923. OBS_COMBO_TYPE_LIST,
  924. OBS_COMBO_FORMAT_STRING);
  925. #define add_preset(val) \
  926. obs_property_list_add_string(p, obs_module_text("AMF.Preset." val), val)
  927. if (amf_codec_type::AV1 == codec) {
  928. add_preset("highQuality");
  929. }
  930. add_preset("quality");
  931. add_preset("balanced");
  932. add_preset("speed");
  933. #undef add_preset
  934. if (amf_codec_type::AVC == codec || amf_codec_type::AV1 == codec) {
  935. p = obs_properties_add_list(props, "profile",
  936. obs_module_text("Profile"),
  937. OBS_COMBO_TYPE_LIST,
  938. OBS_COMBO_FORMAT_STRING);
  939. #define add_profile(val) obs_property_list_add_string(p, val, val)
  940. if (amf_codec_type::AVC == codec)
  941. add_profile("high");
  942. add_profile("main");
  943. if (amf_codec_type::AVC == codec)
  944. add_profile("baseline");
  945. #undef add_profile
  946. }
  947. if (amf_codec_type::AVC == codec) {
  948. obs_properties_add_int(props, "bf", obs_module_text("BFrames"),
  949. 0, 5, 1);
  950. }
  951. p = obs_properties_add_text(props, "ffmpeg_opts",
  952. obs_module_text("AMFOpts"),
  953. OBS_TEXT_DEFAULT);
  954. obs_property_set_long_description(p,
  955. obs_module_text("AMFOpts.ToolTip"));
  956. return props;
  957. }
  958. static obs_properties_t *amf_avc_properties(void *unused)
  959. {
  960. UNUSED_PARAMETER(unused);
  961. return amf_properties_internal(amf_codec_type::AVC);
  962. }
  963. static obs_properties_t *amf_hevc_properties(void *unused)
  964. {
  965. UNUSED_PARAMETER(unused);
  966. return amf_properties_internal(amf_codec_type::HEVC);
  967. }
  968. static obs_properties_t *amf_av1_properties(void *unused)
  969. {
  970. UNUSED_PARAMETER(unused);
  971. return amf_properties_internal(amf_codec_type::AV1);
  972. }
  973. /* ========================================================================= */
  974. /* AVC Implementation */
  975. static const char *amf_avc_get_name(void *)
  976. {
  977. return "AMD HW H.264 (AVC)";
  978. }
  979. static inline int get_avc_preset(amf_base *enc, const char *preset)
  980. {
  981. if (astrcmpi(preset, "quality") == 0)
  982. return AMF_VIDEO_ENCODER_QUALITY_PRESET_QUALITY;
  983. else if (astrcmpi(preset, "speed") == 0)
  984. return AMF_VIDEO_ENCODER_QUALITY_PRESET_SPEED;
  985. return AMF_VIDEO_ENCODER_QUALITY_PRESET_BALANCED;
  986. }
  987. static inline int get_avc_rate_control(const char *rc_str)
  988. {
  989. if (astrcmpi(rc_str, "cqp") == 0)
  990. return AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP;
  991. else if (astrcmpi(rc_str, "vbr") == 0)
  992. return AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;
  993. return AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CBR;
  994. }
  995. static inline int get_avc_profile(obs_data_t *settings)
  996. {
  997. const char *profile = obs_data_get_string(settings, "profile");
  998. if (astrcmpi(profile, "baseline") == 0)
  999. return AMF_VIDEO_ENCODER_PROFILE_BASELINE;
  1000. else if (astrcmpi(profile, "main") == 0)
  1001. return AMF_VIDEO_ENCODER_PROFILE_MAIN;
  1002. else if (astrcmpi(profile, "constrained_baseline") == 0)
  1003. return AMF_VIDEO_ENCODER_PROFILE_CONSTRAINED_BASELINE;
  1004. else if (astrcmpi(profile, "constrained_high") == 0)
  1005. return AMF_VIDEO_ENCODER_PROFILE_CONSTRAINED_HIGH;
  1006. return AMF_VIDEO_ENCODER_PROFILE_HIGH;
  1007. }
  1008. static void amf_avc_update_data(amf_base *enc, int rc, int64_t bitrate,
  1009. int64_t qp)
  1010. {
  1011. if (rc != AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP) {
  1012. set_avc_property(enc, TARGET_BITRATE, bitrate);
  1013. set_avc_property(enc, PEAK_BITRATE, bitrate);
  1014. set_avc_property(enc, VBV_BUFFER_SIZE, bitrate);
  1015. if (rc == AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CBR) {
  1016. set_avc_property(enc, FILLER_DATA_ENABLE, true);
  1017. }
  1018. } else {
  1019. set_avc_property(enc, QP_I, qp);
  1020. set_avc_property(enc, QP_P, qp);
  1021. set_avc_property(enc, QP_B, qp);
  1022. }
  1023. }
  1024. static bool amf_avc_update(void *data, obs_data_t *settings)
  1025. try {
  1026. amf_base *enc = (amf_base *)data;
  1027. if (enc->first_update) {
  1028. enc->first_update = false;
  1029. return true;
  1030. }
  1031. int64_t bitrate = obs_data_get_int(settings, "bitrate");
  1032. int64_t qp = obs_data_get_int(settings, "cqp");
  1033. const char *rc_str = obs_data_get_string(settings, "rate_control");
  1034. int rc = get_avc_rate_control(rc_str);
  1035. AMF_RESULT res;
  1036. amf_avc_update_data(enc, rc, bitrate * 1000, qp);
  1037. res = enc->amf_encoder->ReInit(enc->cx, enc->cy);
  1038. if (res != AMF_OK)
  1039. throw amf_error("AMFComponent::Init failed", res);
  1040. return true;
  1041. } catch (const amf_error &err) {
  1042. amf_base *enc = (amf_base *)data;
  1043. error("%s: %s: %ls", __FUNCTION__, err.str,
  1044. amf_trace->GetResultText(err.res));
  1045. return false;
  1046. }
  1047. static bool amf_avc_init(void *data, obs_data_t *settings)
  1048. {
  1049. amf_base *enc = (amf_base *)data;
  1050. int64_t bitrate = obs_data_get_int(settings, "bitrate");
  1051. int64_t qp = obs_data_get_int(settings, "cqp");
  1052. const char *preset = obs_data_get_string(settings, "preset");
  1053. const char *profile = obs_data_get_string(settings, "profile");
  1054. const char *rc_str = obs_data_get_string(settings, "rate_control");
  1055. int64_t bf = obs_data_get_int(settings, "bf");
  1056. if (enc->bframes_supported) {
  1057. set_avc_property(enc, MAX_CONSECUTIVE_BPICTURES, 3);
  1058. set_avc_property(enc, B_PIC_PATTERN, bf);
  1059. } else if (bf != 0) {
  1060. warn("B-Frames set to %lld but b-frames are not "
  1061. "supported by this device",
  1062. bf);
  1063. bf = 0;
  1064. }
  1065. int rc = get_avc_rate_control(rc_str);
  1066. set_avc_property(enc, RATE_CONTROL_METHOD, rc);
  1067. if (rc != AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP)
  1068. set_avc_property(enc, ENABLE_VBAQ, true);
  1069. amf_avc_update_data(enc, rc, bitrate * 1000, qp);
  1070. set_avc_property(enc, ENFORCE_HRD, true);
  1071. set_avc_property(enc, HIGH_MOTION_QUALITY_BOOST_ENABLE, false);
  1072. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  1073. int gop_size = (keyint_sec) ? keyint_sec * enc->fps_num / enc->fps_den
  1074. : 250;
  1075. set_avc_property(enc, IDR_PERIOD, gop_size);
  1076. bool repeat_headers = obs_data_get_bool(settings, "repeat_headers");
  1077. if (repeat_headers)
  1078. set_avc_property(enc, HEADER_INSERTION_SPACING, gop_size);
  1079. set_avc_property(enc, DE_BLOCKING_FILTER, true);
  1080. check_preset_compatibility(enc, preset);
  1081. const char *ffmpeg_opts = obs_data_get_string(settings, "ffmpeg_opts");
  1082. if (ffmpeg_opts && *ffmpeg_opts) {
  1083. struct obs_options opts = obs_parse_options(ffmpeg_opts);
  1084. for (size_t i = 0; i < opts.count; i++) {
  1085. amf_apply_opt(enc, &opts.options[i]);
  1086. }
  1087. obs_free_options(opts);
  1088. }
  1089. if (!ffmpeg_opts || !*ffmpeg_opts)
  1090. ffmpeg_opts = "(none)";
  1091. info("settings:\n"
  1092. "\trate_control: %s\n"
  1093. "\tbitrate: %d\n"
  1094. "\tcqp: %d\n"
  1095. "\tkeyint: %d\n"
  1096. "\tpreset: %s\n"
  1097. "\tprofile: %s\n"
  1098. "\tb-frames: %d\n"
  1099. "\twidth: %d\n"
  1100. "\theight: %d\n"
  1101. "\tparams: %s",
  1102. rc_str, bitrate, qp, gop_size, preset, profile, bf, enc->cx,
  1103. enc->cy, ffmpeg_opts);
  1104. return true;
  1105. }
  1106. static void amf_avc_create_internal(amf_base *enc, obs_data_t *settings)
  1107. {
  1108. AMF_RESULT res;
  1109. AMFVariant p;
  1110. enc->codec = amf_codec_type::AVC;
  1111. if (!amf_create_encoder(enc))
  1112. throw "Failed to create encoder";
  1113. AMFCapsPtr caps;
  1114. res = enc->amf_encoder->GetCaps(&caps);
  1115. if (res == AMF_OK) {
  1116. caps->GetProperty(AMF_VIDEO_ENCODER_CAP_BFRAMES,
  1117. &enc->bframes_supported);
  1118. caps->GetProperty(AMF_VIDEO_ENCODER_CAP_MAX_THROUGHPUT,
  1119. &enc->max_throughput);
  1120. }
  1121. const char *preset = obs_data_get_string(settings, "preset");
  1122. set_avc_property(enc, FRAMESIZE, AMFConstructSize(enc->cx, enc->cy));
  1123. set_avc_property(enc, USAGE, AMF_VIDEO_ENCODER_USAGE_TRANSCODING);
  1124. set_avc_property(enc, QUALITY_PRESET, get_avc_preset(enc, preset));
  1125. set_avc_property(enc, PROFILE, get_avc_profile(settings));
  1126. set_avc_property(enc, LOWLATENCY_MODE, false);
  1127. set_avc_property(enc, CABAC_ENABLE, AMF_VIDEO_ENCODER_UNDEFINED);
  1128. set_avc_property(enc, PREENCODE_ENABLE, true);
  1129. set_avc_property(enc, OUTPUT_COLOR_PROFILE, enc->amf_color_profile);
  1130. set_avc_property(enc, OUTPUT_TRANSFER_CHARACTERISTIC,
  1131. enc->amf_characteristic);
  1132. set_avc_property(enc, OUTPUT_COLOR_PRIMARIES, enc->amf_primaries);
  1133. set_avc_property(enc, FULL_RANGE_COLOR, enc->full_range);
  1134. amf_avc_init(enc, settings);
  1135. res = enc->amf_encoder->Init(enc->amf_format, enc->cx, enc->cy);
  1136. if (res != AMF_OK)
  1137. throw amf_error("AMFComponent::Init failed", res);
  1138. set_avc_property(enc, FRAMERATE, enc->amf_frame_rate);
  1139. res = enc->amf_encoder->GetProperty(AMF_VIDEO_ENCODER_EXTRADATA, &p);
  1140. if (res == AMF_OK && p.type == AMF_VARIANT_INTERFACE)
  1141. enc->header = AMFBufferPtr(p.pInterface);
  1142. if (enc->bframes_supported) {
  1143. amf_int64 b_frames = 0;
  1144. amf_int64 b_max = 0;
  1145. if (get_avc_property(enc, B_PIC_PATTERN, &b_frames) &&
  1146. get_avc_property(enc, MAX_CONSECUTIVE_BPICTURES, &b_max))
  1147. enc->dts_offset = b_frames + 1;
  1148. else
  1149. enc->dts_offset = 0;
  1150. }
  1151. }
  1152. static void *amf_avc_create_texencode(obs_data_t *settings,
  1153. obs_encoder_t *encoder)
  1154. try {
  1155. check_texture_encode_capability(encoder, amf_codec_type::AVC);
  1156. std::unique_ptr<amf_texencode> enc = std::make_unique<amf_texencode>();
  1157. enc->encoder = encoder;
  1158. enc->encoder_str = "texture-amf-h264";
  1159. if (!amf_init_d3d11(enc.get()))
  1160. throw "Failed to create D3D11";
  1161. amf_avc_create_internal(enc.get(), settings);
  1162. return enc.release();
  1163. } catch (const amf_error &err) {
  1164. blog(LOG_ERROR, "[texture-amf-h264] %s: %s: %ls", __FUNCTION__, err.str,
  1165. amf_trace->GetResultText(err.res));
  1166. return obs_encoder_create_rerouted(encoder, "h264_fallback_amf");
  1167. } catch (const char *err) {
  1168. blog(LOG_ERROR, "[texture-amf-h264] %s: %s", __FUNCTION__, err);
  1169. return obs_encoder_create_rerouted(encoder, "h264_fallback_amf");
  1170. }
  1171. static void *amf_avc_create_fallback(obs_data_t *settings,
  1172. obs_encoder_t *encoder)
  1173. try {
  1174. std::unique_ptr<amf_fallback> enc = std::make_unique<amf_fallback>();
  1175. enc->encoder = encoder;
  1176. enc->encoder_str = "fallback-amf-h264";
  1177. video_t *video = obs_encoder_video(encoder);
  1178. const struct video_output_info *voi = video_output_get_info(video);
  1179. switch (voi->format) {
  1180. case VIDEO_FORMAT_I010:
  1181. case VIDEO_FORMAT_P010: {
  1182. const char *const text =
  1183. obs_module_text("AMF.10bitUnsupportedAvc");
  1184. obs_encoder_set_last_error(encoder, text);
  1185. throw text;
  1186. }
  1187. default:
  1188. switch (voi->colorspace) {
  1189. case VIDEO_CS_2100_PQ:
  1190. case VIDEO_CS_2100_HLG: {
  1191. const char *const text =
  1192. obs_module_text("AMF.8bitUnsupportedHdr");
  1193. obs_encoder_set_last_error(encoder, text);
  1194. throw text;
  1195. }
  1196. }
  1197. }
  1198. amf_avc_create_internal(enc.get(), settings);
  1199. return enc.release();
  1200. } catch (const amf_error &err) {
  1201. blog(LOG_ERROR, "[fallback-amf-h264] %s: %s: %ls", __FUNCTION__,
  1202. err.str, amf_trace->GetResultText(err.res));
  1203. return nullptr;
  1204. } catch (const char *err) {
  1205. blog(LOG_ERROR, "[fallback-amf-h264] %s: %s", __FUNCTION__, err);
  1206. return nullptr;
  1207. }
  1208. static void register_avc()
  1209. {
  1210. struct obs_encoder_info amf_encoder_info = {};
  1211. amf_encoder_info.id = "h264_texture_amf";
  1212. amf_encoder_info.type = OBS_ENCODER_VIDEO;
  1213. amf_encoder_info.codec = "h264";
  1214. amf_encoder_info.get_name = amf_avc_get_name;
  1215. amf_encoder_info.create = amf_avc_create_texencode;
  1216. amf_encoder_info.destroy = amf_destroy;
  1217. amf_encoder_info.update = amf_avc_update;
  1218. amf_encoder_info.encode_texture = amf_encode_tex;
  1219. amf_encoder_info.get_defaults = amf_defaults;
  1220. amf_encoder_info.get_properties = amf_avc_properties;
  1221. amf_encoder_info.get_extra_data = amf_extra_data;
  1222. amf_encoder_info.caps = OBS_ENCODER_CAP_PASS_TEXTURE |
  1223. OBS_ENCODER_CAP_DYN_BITRATE;
  1224. obs_register_encoder(&amf_encoder_info);
  1225. amf_encoder_info.id = "h264_fallback_amf";
  1226. amf_encoder_info.caps = OBS_ENCODER_CAP_INTERNAL |
  1227. OBS_ENCODER_CAP_DYN_BITRATE;
  1228. amf_encoder_info.encode_texture = nullptr;
  1229. amf_encoder_info.create = amf_avc_create_fallback;
  1230. amf_encoder_info.encode = amf_encode_fallback;
  1231. amf_encoder_info.get_video_info = h264_video_info_fallback;
  1232. obs_register_encoder(&amf_encoder_info);
  1233. }
  1234. /* ========================================================================= */
  1235. /* HEVC Implementation */
  1236. #if ENABLE_HEVC
  1237. static const char *amf_hevc_get_name(void *)
  1238. {
  1239. return "AMD HW H.265 (HEVC)";
  1240. }
  1241. static inline int get_hevc_preset(amf_base *enc, const char *preset)
  1242. {
  1243. if (astrcmpi(preset, "balanced") == 0)
  1244. return AMF_VIDEO_ENCODER_HEVC_QUALITY_PRESET_BALANCED;
  1245. else if (astrcmpi(preset, "speed") == 0)
  1246. return AMF_VIDEO_ENCODER_HEVC_QUALITY_PRESET_SPEED;
  1247. return AMF_VIDEO_ENCODER_HEVC_QUALITY_PRESET_QUALITY;
  1248. }
  1249. static inline int get_hevc_rate_control(const char *rc_str)
  1250. {
  1251. if (astrcmpi(rc_str, "cqp") == 0)
  1252. return AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_CONSTANT_QP;
  1253. else if (astrcmpi(rc_str, "vbr") == 0)
  1254. return AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;
  1255. return AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_CBR;
  1256. }
  1257. static void amf_hevc_update_data(amf_base *enc, int rc, int64_t bitrate,
  1258. int64_t qp)
  1259. {
  1260. if (rc != AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP) {
  1261. set_hevc_property(enc, TARGET_BITRATE, bitrate);
  1262. set_hevc_property(enc, PEAK_BITRATE, bitrate);
  1263. set_hevc_property(enc, VBV_BUFFER_SIZE, bitrate);
  1264. if (rc == AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_CBR) {
  1265. set_hevc_property(enc, FILLER_DATA_ENABLE, true);
  1266. }
  1267. } else {
  1268. set_hevc_property(enc, QP_I, qp);
  1269. set_hevc_property(enc, QP_P, qp);
  1270. }
  1271. }
  1272. static bool amf_hevc_update(void *data, obs_data_t *settings)
  1273. try {
  1274. amf_base *enc = (amf_base *)data;
  1275. if (enc->first_update) {
  1276. enc->first_update = false;
  1277. return true;
  1278. }
  1279. int64_t bitrate = obs_data_get_int(settings, "bitrate");
  1280. int64_t qp = obs_data_get_int(settings, "cqp");
  1281. const char *rc_str = obs_data_get_string(settings, "rate_control");
  1282. int rc = get_hevc_rate_control(rc_str);
  1283. AMF_RESULT res;
  1284. amf_hevc_update_data(enc, rc, bitrate * 1000, qp);
  1285. res = enc->amf_encoder->ReInit(enc->cx, enc->cy);
  1286. if (res != AMF_OK)
  1287. throw amf_error("AMFComponent::Init failed", res);
  1288. return true;
  1289. } catch (const amf_error &err) {
  1290. amf_base *enc = (amf_base *)data;
  1291. error("%s: %s: %ls", __FUNCTION__, err.str,
  1292. amf_trace->GetResultText(err.res));
  1293. return false;
  1294. }
  1295. static bool amf_hevc_init(void *data, obs_data_t *settings)
  1296. {
  1297. amf_base *enc = (amf_base *)data;
  1298. int64_t bitrate = obs_data_get_int(settings, "bitrate");
  1299. int64_t qp = obs_data_get_int(settings, "cqp");
  1300. const char *preset = obs_data_get_string(settings, "preset");
  1301. const char *profile = obs_data_get_string(settings, "profile");
  1302. const char *rc_str = obs_data_get_string(settings, "rate_control");
  1303. int rc = get_hevc_rate_control(rc_str);
  1304. set_hevc_property(enc, RATE_CONTROL_METHOD, rc);
  1305. if (rc != AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_CONSTANT_QP)
  1306. set_hevc_property(enc, ENABLE_VBAQ, true);
  1307. amf_hevc_update_data(enc, rc, bitrate * 1000, qp);
  1308. set_hevc_property(enc, ENFORCE_HRD, true);
  1309. set_hevc_property(enc, HIGH_MOTION_QUALITY_BOOST_ENABLE, false);
  1310. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  1311. int gop_size = (keyint_sec) ? keyint_sec * enc->fps_num / enc->fps_den
  1312. : 250;
  1313. set_hevc_property(enc, GOP_SIZE, gop_size);
  1314. check_preset_compatibility(enc, preset);
  1315. const char *ffmpeg_opts = obs_data_get_string(settings, "ffmpeg_opts");
  1316. if (ffmpeg_opts && *ffmpeg_opts) {
  1317. struct obs_options opts = obs_parse_options(ffmpeg_opts);
  1318. for (size_t i = 0; i < opts.count; i++) {
  1319. amf_apply_opt(enc, &opts.options[i]);
  1320. }
  1321. obs_free_options(opts);
  1322. }
  1323. if (!ffmpeg_opts || !*ffmpeg_opts)
  1324. ffmpeg_opts = "(none)";
  1325. info("settings:\n"
  1326. "\trate_control: %s\n"
  1327. "\tbitrate: %d\n"
  1328. "\tcqp: %d\n"
  1329. "\tkeyint: %d\n"
  1330. "\tpreset: %s\n"
  1331. "\tprofile: %s\n"
  1332. "\twidth: %d\n"
  1333. "\theight: %d\n"
  1334. "\tparams: %s",
  1335. rc_str, bitrate, qp, gop_size, preset, profile, enc->cx, enc->cy,
  1336. ffmpeg_opts);
  1337. return true;
  1338. }
  1339. static inline bool is_hlg(amf_base *enc)
  1340. {
  1341. return enc->amf_characteristic ==
  1342. AMF_COLOR_TRANSFER_CHARACTERISTIC_ARIB_STD_B67;
  1343. }
  1344. static inline bool is_pq(amf_base *enc)
  1345. {
  1346. return enc->amf_characteristic ==
  1347. AMF_COLOR_TRANSFER_CHARACTERISTIC_SMPTE2084;
  1348. }
  1349. constexpr amf_uint16 amf_hdr_primary(uint32_t num, uint32_t den)
  1350. {
  1351. return (amf_uint16)(num * 50000 / den);
  1352. }
  1353. constexpr amf_uint32 lum_mul = 10000;
  1354. constexpr amf_uint32 amf_make_lum(amf_uint32 val)
  1355. {
  1356. return val * lum_mul;
  1357. }
  1358. static void amf_hevc_create_internal(amf_base *enc, obs_data_t *settings)
  1359. {
  1360. AMF_RESULT res;
  1361. AMFVariant p;
  1362. enc->codec = amf_codec_type::HEVC;
  1363. if (!amf_create_encoder(enc))
  1364. throw "Failed to create encoder";
  1365. AMFCapsPtr caps;
  1366. res = enc->amf_encoder->GetCaps(&caps);
  1367. if (res == AMF_OK) {
  1368. caps->GetProperty(AMF_VIDEO_ENCODER_HEVC_CAP_MAX_THROUGHPUT,
  1369. &enc->max_throughput);
  1370. }
  1371. const bool is10bit = enc->amf_format == AMF_SURFACE_P010;
  1372. const bool pq = is_pq(enc);
  1373. const bool hlg = is_hlg(enc);
  1374. const bool is_hdr = pq || hlg;
  1375. const char *preset = obs_data_get_string(settings, "preset");
  1376. set_hevc_property(enc, FRAMESIZE, AMFConstructSize(enc->cx, enc->cy));
  1377. set_hevc_property(enc, USAGE, AMF_VIDEO_ENCODER_USAGE_TRANSCODING);
  1378. set_hevc_property(enc, QUALITY_PRESET, get_hevc_preset(enc, preset));
  1379. set_hevc_property(enc, COLOR_BIT_DEPTH,
  1380. is10bit ? AMF_COLOR_BIT_DEPTH_10
  1381. : AMF_COLOR_BIT_DEPTH_8);
  1382. set_hevc_property(enc, PROFILE,
  1383. is10bit ? AMF_VIDEO_ENCODER_HEVC_PROFILE_MAIN_10
  1384. : AMF_VIDEO_ENCODER_HEVC_PROFILE_MAIN);
  1385. set_hevc_property(enc, LOWLATENCY_MODE, false);
  1386. set_hevc_property(enc, OUTPUT_COLOR_PROFILE, enc->amf_color_profile);
  1387. set_hevc_property(enc, OUTPUT_TRANSFER_CHARACTERISTIC,
  1388. enc->amf_characteristic);
  1389. set_hevc_property(enc, OUTPUT_COLOR_PRIMARIES, enc->amf_primaries);
  1390. set_hevc_property(enc, NOMINAL_RANGE, enc->full_range);
  1391. if (is_hdr) {
  1392. const int hdr_nominal_peak_level =
  1393. pq ? (int)obs_get_video_hdr_nominal_peak_level()
  1394. : (hlg ? 1000 : 0);
  1395. AMFBufferPtr buf;
  1396. enc->amf_context->AllocBuffer(AMF_MEMORY_HOST,
  1397. sizeof(AMFHDRMetadata), &buf);
  1398. AMFHDRMetadata *md = (AMFHDRMetadata *)buf->GetNative();
  1399. md->redPrimary[0] = amf_hdr_primary(17, 25);
  1400. md->redPrimary[1] = amf_hdr_primary(8, 25);
  1401. md->greenPrimary[0] = amf_hdr_primary(53, 200);
  1402. md->greenPrimary[1] = amf_hdr_primary(69, 100);
  1403. md->bluePrimary[0] = amf_hdr_primary(3, 20);
  1404. md->bluePrimary[1] = amf_hdr_primary(3, 50);
  1405. md->whitePoint[0] = amf_hdr_primary(3127, 10000);
  1406. md->whitePoint[1] = amf_hdr_primary(329, 1000);
  1407. md->minMasteringLuminance = 0;
  1408. md->maxMasteringLuminance =
  1409. amf_make_lum(hdr_nominal_peak_level);
  1410. md->maxContentLightLevel = hdr_nominal_peak_level;
  1411. md->maxFrameAverageLightLevel = hdr_nominal_peak_level;
  1412. set_hevc_property(enc, INPUT_HDR_METADATA, buf);
  1413. }
  1414. amf_hevc_init(enc, settings);
  1415. res = enc->amf_encoder->Init(enc->amf_format, enc->cx, enc->cy);
  1416. if (res != AMF_OK)
  1417. throw amf_error("AMFComponent::Init failed", res);
  1418. set_hevc_property(enc, FRAMERATE, enc->amf_frame_rate);
  1419. res = enc->amf_encoder->GetProperty(AMF_VIDEO_ENCODER_HEVC_EXTRADATA,
  1420. &p);
  1421. if (res == AMF_OK && p.type == AMF_VARIANT_INTERFACE)
  1422. enc->header = AMFBufferPtr(p.pInterface);
  1423. }
  1424. static void *amf_hevc_create_texencode(obs_data_t *settings,
  1425. obs_encoder_t *encoder)
  1426. try {
  1427. check_texture_encode_capability(encoder, amf_codec_type::HEVC);
  1428. std::unique_ptr<amf_texencode> enc = std::make_unique<amf_texencode>();
  1429. enc->encoder = encoder;
  1430. enc->encoder_str = "texture-amf-h265";
  1431. if (!amf_init_d3d11(enc.get()))
  1432. throw "Failed to create D3D11";
  1433. amf_hevc_create_internal(enc.get(), settings);
  1434. return enc.release();
  1435. } catch (const amf_error &err) {
  1436. blog(LOG_ERROR, "[texture-amf-h265] %s: %s: %ls", __FUNCTION__, err.str,
  1437. amf_trace->GetResultText(err.res));
  1438. return obs_encoder_create_rerouted(encoder, "h265_fallback_amf");
  1439. } catch (const char *err) {
  1440. blog(LOG_ERROR, "[texture-amf-h265] %s: %s", __FUNCTION__, err);
  1441. return obs_encoder_create_rerouted(encoder, "h265_fallback_amf");
  1442. }
  1443. static void *amf_hevc_create_fallback(obs_data_t *settings,
  1444. obs_encoder_t *encoder)
  1445. try {
  1446. std::unique_ptr<amf_fallback> enc = std::make_unique<amf_fallback>();
  1447. enc->encoder = encoder;
  1448. enc->encoder_str = "fallback-amf-h265";
  1449. video_t *video = obs_encoder_video(encoder);
  1450. const struct video_output_info *voi = video_output_get_info(video);
  1451. switch (voi->format) {
  1452. case VIDEO_FORMAT_I010:
  1453. case VIDEO_FORMAT_P010:
  1454. break;
  1455. default:
  1456. switch (voi->colorspace) {
  1457. case VIDEO_CS_2100_PQ:
  1458. case VIDEO_CS_2100_HLG: {
  1459. const char *const text =
  1460. obs_module_text("AMF.8bitUnsupportedHdr");
  1461. obs_encoder_set_last_error(encoder, text);
  1462. throw text;
  1463. }
  1464. }
  1465. }
  1466. amf_hevc_create_internal(enc.get(), settings);
  1467. return enc.release();
  1468. } catch (const amf_error &err) {
  1469. blog(LOG_ERROR, "[fallback-amf-h265] %s: %s: %ls", __FUNCTION__,
  1470. err.str, amf_trace->GetResultText(err.res));
  1471. return nullptr;
  1472. } catch (const char *err) {
  1473. blog(LOG_ERROR, "[fallback-amf-h265] %s: %s", __FUNCTION__, err);
  1474. return nullptr;
  1475. }
  1476. static void register_hevc()
  1477. {
  1478. struct obs_encoder_info amf_encoder_info = {};
  1479. amf_encoder_info.id = "h265_texture_amf";
  1480. amf_encoder_info.type = OBS_ENCODER_VIDEO;
  1481. amf_encoder_info.codec = "hevc";
  1482. amf_encoder_info.get_name = amf_hevc_get_name;
  1483. amf_encoder_info.create = amf_hevc_create_texencode;
  1484. amf_encoder_info.destroy = amf_destroy;
  1485. amf_encoder_info.update = amf_hevc_update;
  1486. amf_encoder_info.encode_texture = amf_encode_tex;
  1487. amf_encoder_info.get_defaults = amf_defaults;
  1488. amf_encoder_info.get_properties = amf_hevc_properties;
  1489. amf_encoder_info.get_extra_data = amf_extra_data;
  1490. amf_encoder_info.caps = OBS_ENCODER_CAP_PASS_TEXTURE |
  1491. OBS_ENCODER_CAP_DYN_BITRATE;
  1492. obs_register_encoder(&amf_encoder_info);
  1493. amf_encoder_info.id = "h265_fallback_amf";
  1494. amf_encoder_info.caps = OBS_ENCODER_CAP_INTERNAL |
  1495. OBS_ENCODER_CAP_DYN_BITRATE;
  1496. amf_encoder_info.encode_texture = nullptr;
  1497. amf_encoder_info.create = amf_hevc_create_fallback;
  1498. amf_encoder_info.encode = amf_encode_fallback;
  1499. amf_encoder_info.get_video_info = h265_video_info_fallback;
  1500. obs_register_encoder(&amf_encoder_info);
  1501. }
  1502. #endif //ENABLE_HEVC
  1503. /* ========================================================================= */
  1504. /* AV1 Implementation */
  1505. static const char *amf_av1_get_name(void *)
  1506. {
  1507. return "AMD HW AV1";
  1508. }
  1509. static inline int get_av1_preset(amf_base *enc, const char *preset)
  1510. {
  1511. if (astrcmpi(preset, "highquality") == 0)
  1512. return AMF_VIDEO_ENCODER_AV1_QUALITY_PRESET_HIGH_QUALITY;
  1513. else if (astrcmpi(preset, "quality") == 0)
  1514. return AMF_VIDEO_ENCODER_AV1_QUALITY_PRESET_QUALITY;
  1515. else if (astrcmpi(preset, "balanced") == 0)
  1516. return AMF_VIDEO_ENCODER_AV1_QUALITY_PRESET_BALANCED;
  1517. else if (astrcmpi(preset, "speed") == 0)
  1518. return AMF_VIDEO_ENCODER_AV1_QUALITY_PRESET_SPEED;
  1519. return AMF_VIDEO_ENCODER_AV1_QUALITY_PRESET_BALANCED;
  1520. }
  1521. static inline int get_av1_rate_control(const char *rc_str)
  1522. {
  1523. if (astrcmpi(rc_str, "cqp") == 0)
  1524. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_CONSTANT_QP;
  1525. else if (astrcmpi(rc_str, "vbr_lat") == 0)
  1526. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR;
  1527. else if (astrcmpi(rc_str, "vbr") == 0)
  1528. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR;
  1529. else if (astrcmpi(rc_str, "cbr") == 0)
  1530. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_CBR;
  1531. else if (astrcmpi(rc_str, "qvbr") == 0)
  1532. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_QUALITY_VBR;
  1533. else if (astrcmpi(rc_str, "hqvbr") == 0)
  1534. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_HIGH_QUALITY_VBR;
  1535. else if (astrcmpi(rc_str, "hqcbr") == 0)
  1536. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_HIGH_QUALITY_CBR;
  1537. return AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_CBR;
  1538. }
  1539. static inline int get_av1_profile(obs_data_t *settings)
  1540. {
  1541. const char *profile = obs_data_get_string(settings, "profile");
  1542. if (astrcmpi(profile, "main") == 0)
  1543. return AMF_VIDEO_ENCODER_AV1_PROFILE_MAIN;
  1544. return AMF_VIDEO_ENCODER_AV1_PROFILE_MAIN;
  1545. }
  1546. static void amf_av1_update_data(amf_base *enc, int rc, int64_t bitrate,
  1547. int64_t cq_value)
  1548. {
  1549. if (rc != AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_CONSTANT_QP) {
  1550. set_av1_property(enc, TARGET_BITRATE, bitrate);
  1551. set_av1_property(enc, PEAK_BITRATE, bitrate);
  1552. set_av1_property(enc, VBV_BUFFER_SIZE, bitrate);
  1553. if (rc == AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CBR) {
  1554. set_av1_property(enc, FILLER_DATA, true);
  1555. } else if (
  1556. rc == AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR ||
  1557. rc == AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_HIGH_QUALITY_VBR) {
  1558. set_av1_property(enc, PEAK_BITRATE, bitrate * 1.5);
  1559. }
  1560. } else {
  1561. int64_t qp = cq_value * 4;
  1562. set_av1_property(enc, Q_INDEX_INTRA, qp);
  1563. set_av1_property(enc, Q_INDEX_INTER, qp);
  1564. }
  1565. }
  1566. static bool amf_av1_update(void *data, obs_data_t *settings)
  1567. try {
  1568. amf_base *enc = (amf_base *)data;
  1569. if (enc->first_update) {
  1570. enc->first_update = false;
  1571. return true;
  1572. }
  1573. int64_t bitrate = obs_data_get_int(settings, "bitrate");
  1574. int64_t cq_level = obs_data_get_int(settings, "cqp");
  1575. const char *rc_str = obs_data_get_string(settings, "rate_control");
  1576. int rc = get_av1_rate_control(rc_str);
  1577. amf_av1_update_data(enc, rc, bitrate * 1000, cq_level);
  1578. AMF_RESULT res = enc->amf_encoder->ReInit(enc->cx, enc->cy);
  1579. if (res != AMF_OK)
  1580. throw amf_error("AMFComponent::Init failed", res);
  1581. return true;
  1582. } catch (const amf_error &err) {
  1583. amf_base *enc = (amf_base *)data;
  1584. error("%s: %s: %ls", __FUNCTION__, err.str,
  1585. amf_trace->GetResultText(err.res));
  1586. return false;
  1587. }
  1588. static bool amf_av1_init(void *data, obs_data_t *settings)
  1589. {
  1590. amf_base *enc = (amf_base *)data;
  1591. int64_t bitrate = obs_data_get_int(settings, "bitrate");
  1592. int64_t qp = obs_data_get_int(settings, "cqp");
  1593. const char *preset = obs_data_get_string(settings, "preset");
  1594. const char *profile = obs_data_get_string(settings, "profile");
  1595. const char *rc_str = obs_data_get_string(settings, "rate_control");
  1596. int rc = get_av1_rate_control(rc_str);
  1597. set_av1_property(enc, RATE_CONTROL_METHOD, rc);
  1598. amf_av1_update_data(enc, rc, bitrate * 1000, qp);
  1599. set_av1_property(enc, ENFORCE_HRD, true);
  1600. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  1601. int gop_size = (keyint_sec) ? keyint_sec * enc->fps_num / enc->fps_den
  1602. : 250;
  1603. set_av1_property(enc, GOP_SIZE, gop_size);
  1604. const char *ffmpeg_opts = obs_data_get_string(settings, "ffmpeg_opts");
  1605. if (ffmpeg_opts && *ffmpeg_opts) {
  1606. struct obs_options opts = obs_parse_options(ffmpeg_opts);
  1607. for (size_t i = 0; i < opts.count; i++) {
  1608. amf_apply_opt(enc, &opts.options[i]);
  1609. }
  1610. obs_free_options(opts);
  1611. }
  1612. check_preset_compatibility(enc, preset);
  1613. if (!ffmpeg_opts || !*ffmpeg_opts)
  1614. ffmpeg_opts = "(none)";
  1615. info("settings:\n"
  1616. "\trate_control: %s\n"
  1617. "\tbitrate: %d\n"
  1618. "\tcqp: %d\n"
  1619. "\tkeyint: %d\n"
  1620. "\tpreset: %s\n"
  1621. "\tprofile: %s\n"
  1622. "\twidth: %d\n"
  1623. "\theight: %d\n"
  1624. "\tparams: %s",
  1625. rc_str, bitrate, qp, gop_size, preset, profile, enc->cx, enc->cy,
  1626. ffmpeg_opts);
  1627. return true;
  1628. }
  1629. static void amf_av1_create_internal(amf_base *enc, obs_data_t *settings)
  1630. {
  1631. enc->codec = amf_codec_type::AV1;
  1632. if (!amf_create_encoder(enc))
  1633. throw "Failed to create encoder";
  1634. AMFCapsPtr caps;
  1635. AMF_RESULT res = enc->amf_encoder->GetCaps(&caps);
  1636. if (res == AMF_OK) {
  1637. caps->GetProperty(AMF_VIDEO_ENCODER_AV1_CAP_MAX_THROUGHPUT,
  1638. &enc->max_throughput);
  1639. }
  1640. const bool is10bit = enc->amf_format == AMF_SURFACE_P010;
  1641. const char *preset = obs_data_get_string(settings, "preset");
  1642. set_av1_property(enc, FRAMESIZE, AMFConstructSize(enc->cx, enc->cy));
  1643. set_av1_property(enc, USAGE, AMF_VIDEO_ENCODER_USAGE_TRANSCODING);
  1644. set_av1_property(enc, ALIGNMENT_MODE,
  1645. AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_NO_RESTRICTIONS);
  1646. set_av1_property(enc, QUALITY_PRESET, get_av1_preset(enc, preset));
  1647. set_av1_property(enc, COLOR_BIT_DEPTH,
  1648. is10bit ? AMF_COLOR_BIT_DEPTH_10
  1649. : AMF_COLOR_BIT_DEPTH_8);
  1650. set_av1_property(enc, PROFILE, get_av1_profile(settings));
  1651. set_av1_property(enc, ENCODING_LATENCY_MODE,
  1652. AMF_VIDEO_ENCODER_AV1_ENCODING_LATENCY_MODE_NONE);
  1653. // set_av1_property(enc, RATE_CONTROL_PREENCODE, true);
  1654. set_av1_property(enc, OUTPUT_COLOR_PROFILE, enc->amf_color_profile);
  1655. set_av1_property(enc, OUTPUT_TRANSFER_CHARACTERISTIC,
  1656. enc->amf_characteristic);
  1657. set_av1_property(enc, OUTPUT_COLOR_PRIMARIES, enc->amf_primaries);
  1658. amf_av1_init(enc, settings);
  1659. res = enc->amf_encoder->Init(enc->amf_format, enc->cx, enc->cy);
  1660. if (res != AMF_OK)
  1661. throw amf_error("AMFComponent::Init failed", res);
  1662. set_av1_property(enc, FRAMERATE, enc->amf_frame_rate);
  1663. AMFVariant p;
  1664. res = enc->amf_encoder->GetProperty(AMF_VIDEO_ENCODER_AV1_EXTRA_DATA,
  1665. &p);
  1666. if (res == AMF_OK && p.type == AMF_VARIANT_INTERFACE)
  1667. enc->header = AMFBufferPtr(p.pInterface);
  1668. }
  1669. static void *amf_av1_create_texencode(obs_data_t *settings,
  1670. obs_encoder_t *encoder)
  1671. try {
  1672. check_texture_encode_capability(encoder, amf_codec_type::AV1);
  1673. std::unique_ptr<amf_texencode> enc = std::make_unique<amf_texencode>();
  1674. enc->encoder = encoder;
  1675. enc->encoder_str = "texture-amf-av1";
  1676. if (!amf_init_d3d11(enc.get()))
  1677. throw "Failed to create D3D11";
  1678. amf_av1_create_internal(enc.get(), settings);
  1679. return enc.release();
  1680. } catch (const amf_error &err) {
  1681. blog(LOG_ERROR, "[texture-amf-av1] %s: %s: %ls", __FUNCTION__, err.str,
  1682. amf_trace->GetResultText(err.res));
  1683. return obs_encoder_create_rerouted(encoder, "av1_fallback_amf");
  1684. } catch (const char *err) {
  1685. blog(LOG_ERROR, "[texture-amf-av1] %s: %s", __FUNCTION__, err);
  1686. return obs_encoder_create_rerouted(encoder, "av1_fallback_amf");
  1687. }
  1688. static void *amf_av1_create_fallback(obs_data_t *settings,
  1689. obs_encoder_t *encoder)
  1690. try {
  1691. std::unique_ptr<amf_fallback> enc = std::make_unique<amf_fallback>();
  1692. enc->encoder = encoder;
  1693. enc->encoder_str = "fallback-amf-av1";
  1694. video_t *video = obs_encoder_video(encoder);
  1695. const struct video_output_info *voi = video_output_get_info(video);
  1696. switch (voi->format) {
  1697. case VIDEO_FORMAT_I010:
  1698. case VIDEO_FORMAT_P010: {
  1699. break;
  1700. }
  1701. default:
  1702. switch (voi->colorspace) {
  1703. case VIDEO_CS_2100_PQ:
  1704. case VIDEO_CS_2100_HLG: {
  1705. const char *const text =
  1706. obs_module_text("AMF.8bitUnsupportedHdr");
  1707. obs_encoder_set_last_error(encoder, text);
  1708. throw text;
  1709. }
  1710. }
  1711. }
  1712. amf_av1_create_internal(enc.get(), settings);
  1713. return enc.release();
  1714. } catch (const amf_error &err) {
  1715. blog(LOG_ERROR, "[fallback-amf-av1] %s: %s: %ls", __FUNCTION__, err.str,
  1716. amf_trace->GetResultText(err.res));
  1717. return nullptr;
  1718. } catch (const char *err) {
  1719. blog(LOG_ERROR, "[fallback-amf-av1] %s: %s", __FUNCTION__, err);
  1720. return nullptr;
  1721. }
  1722. static void amf_av1_defaults(obs_data_t *settings)
  1723. {
  1724. obs_data_set_default_int(settings, "bitrate", 2500);
  1725. obs_data_set_default_int(settings, "cqp", 7);
  1726. obs_data_set_default_string(settings, "rate_control", "CBR");
  1727. obs_data_set_default_string(settings, "preset", "quality");
  1728. obs_data_set_default_string(settings, "profile", "high");
  1729. }
  1730. static void register_av1()
  1731. {
  1732. struct obs_encoder_info amf_encoder_info = {};
  1733. amf_encoder_info.id = "av1_texture_amf";
  1734. amf_encoder_info.type = OBS_ENCODER_VIDEO;
  1735. amf_encoder_info.codec = "av1";
  1736. amf_encoder_info.get_name = amf_av1_get_name;
  1737. amf_encoder_info.create = amf_av1_create_texencode;
  1738. amf_encoder_info.destroy = amf_destroy;
  1739. amf_encoder_info.update = amf_av1_update;
  1740. amf_encoder_info.encode_texture = amf_encode_tex;
  1741. amf_encoder_info.get_defaults = amf_av1_defaults;
  1742. amf_encoder_info.get_properties = amf_av1_properties;
  1743. amf_encoder_info.get_extra_data = amf_extra_data;
  1744. amf_encoder_info.caps = OBS_ENCODER_CAP_PASS_TEXTURE |
  1745. OBS_ENCODER_CAP_DYN_BITRATE;
  1746. obs_register_encoder(&amf_encoder_info);
  1747. amf_encoder_info.id = "av1_fallback_amf";
  1748. amf_encoder_info.caps = OBS_ENCODER_CAP_INTERNAL |
  1749. OBS_ENCODER_CAP_DYN_BITRATE;
  1750. amf_encoder_info.encode_texture = nullptr;
  1751. amf_encoder_info.create = amf_av1_create_fallback;
  1752. amf_encoder_info.encode = amf_encode_fallback;
  1753. amf_encoder_info.get_video_info = av1_video_info_fallback;
  1754. obs_register_encoder(&amf_encoder_info);
  1755. }
  1756. /* ========================================================================= */
  1757. /* Global Stuff */
  1758. extern "C" void amf_load(void)
  1759. try {
  1760. AMF_RESULT res;
  1761. HMODULE amf_module_test;
  1762. /* Check if the DLL is present before running the more expensive */
  1763. /* obs-amf-test.exe, but load it as data so it can't crash us */
  1764. amf_module_test =
  1765. LoadLibraryExW(AMF_DLL_NAME, nullptr, LOAD_LIBRARY_AS_DATAFILE);
  1766. if (!amf_module_test)
  1767. throw "No AMF library";
  1768. FreeLibrary(amf_module_test);
  1769. /* ----------------------------------- */
  1770. /* Check for supported codecs */
  1771. BPtr<char> test_exe = os_get_executable_path_ptr("obs-amf-test.exe");
  1772. std::string caps_str;
  1773. os_process_pipe_t *pp = os_process_pipe_create(test_exe, "r");
  1774. if (!pp)
  1775. throw "Failed to launch the AMF test process I guess";
  1776. for (;;) {
  1777. char data[2048];
  1778. size_t len =
  1779. os_process_pipe_read(pp, (uint8_t *)data, sizeof(data));
  1780. if (!len)
  1781. break;
  1782. caps_str.append(data, len);
  1783. }
  1784. os_process_pipe_destroy(pp);
  1785. if (caps_str.empty())
  1786. throw "Seems the AMF test subprocess crashed. "
  1787. "Better there than here I guess. "
  1788. "Let's just skip loading AMF then I suppose.";
  1789. ConfigFile config;
  1790. if (config.OpenString(caps_str.c_str()) != 0)
  1791. throw "Failed to open config string";
  1792. const char *error = config_get_string(config, "error", "string");
  1793. if (error)
  1794. throw std::string(error);
  1795. uint32_t adapter_count = (uint32_t)config_num_sections(config);
  1796. bool avc_supported = false;
  1797. bool hevc_supported = false;
  1798. bool av1_supported = false;
  1799. for (uint32_t i = 0; i < adapter_count; i++) {
  1800. std::string section = std::to_string(i);
  1801. adapter_caps &info = caps[i];
  1802. info.is_amd =
  1803. config_get_bool(config, section.c_str(), "is_amd");
  1804. info.supports_avc = config_get_bool(config, section.c_str(),
  1805. "supports_avc");
  1806. info.supports_hevc = config_get_bool(config, section.c_str(),
  1807. "supports_hevc");
  1808. info.supports_av1 = config_get_bool(config, section.c_str(),
  1809. "supports_av1");
  1810. avc_supported |= info.supports_avc;
  1811. hevc_supported |= info.supports_hevc;
  1812. av1_supported |= info.supports_av1;
  1813. }
  1814. if (!avc_supported && !hevc_supported && !av1_supported)
  1815. throw "Neither AVC, HEVC, nor AV1 are supported by any devices";
  1816. /* ----------------------------------- */
  1817. /* Init AMF */
  1818. amf_module = LoadLibraryW(AMF_DLL_NAME);
  1819. if (!amf_module)
  1820. throw "AMF library failed to load";
  1821. AMFInit_Fn init =
  1822. (AMFInit_Fn)GetProcAddress(amf_module, AMF_INIT_FUNCTION_NAME);
  1823. if (!init)
  1824. throw "Failed to get AMFInit address";
  1825. res = init(AMF_FULL_VERSION, &amf_factory);
  1826. if (res != AMF_OK)
  1827. throw amf_error("AMFInit failed", res);
  1828. res = amf_factory->GetTrace(&amf_trace);
  1829. if (res != AMF_OK)
  1830. throw amf_error("GetTrace failed", res);
  1831. AMFQueryVersion_Fn get_ver = (AMFQueryVersion_Fn)GetProcAddress(
  1832. amf_module, AMF_QUERY_VERSION_FUNCTION_NAME);
  1833. if (!get_ver)
  1834. throw "Failed to get AMFQueryVersion address";
  1835. res = get_ver(&amf_version);
  1836. if (res != AMF_OK)
  1837. throw amf_error("AMFQueryVersion failed", res);
  1838. #ifndef DEBUG_AMF_STUFF
  1839. amf_trace->EnableWriter(AMF_TRACE_WRITER_DEBUG_OUTPUT, false);
  1840. amf_trace->EnableWriter(AMF_TRACE_WRITER_CONSOLE, false);
  1841. #endif
  1842. /* ----------------------------------- */
  1843. /* Register encoders */
  1844. if (avc_supported)
  1845. register_avc();
  1846. #if ENABLE_HEVC
  1847. if (hevc_supported)
  1848. register_hevc();
  1849. #endif
  1850. if (av1_supported)
  1851. register_av1();
  1852. } catch (const std::string &str) {
  1853. /* doing debug here because string exceptions indicate the user is
  1854. * probably not using AMD */
  1855. blog(LOG_DEBUG, "%s: %s", __FUNCTION__, str.c_str());
  1856. } catch (const char *str) {
  1857. /* doing debug here because string exceptions indicate the user is
  1858. * probably not using AMD */
  1859. blog(LOG_DEBUG, "%s: %s", __FUNCTION__, str);
  1860. } catch (const amf_error &err) {
  1861. /* doing an error here because it means at least the library has loaded
  1862. * successfully, so they probably have AMD at this point */
  1863. blog(LOG_ERROR, "%s: %s: 0x%lX", __FUNCTION__, err.str,
  1864. (uint32_t)err.res);
  1865. }
  1866. extern "C" void amf_unload(void)
  1867. {
  1868. if (amf_module && amf_trace) {
  1869. amf_trace->TraceFlush();
  1870. amf_trace->UnregisterWriter(L"obs_amf_trace_writer");
  1871. }
  1872. }