obs-qsv11.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) Oct. 2015 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. Seung-Woo Kim, [email protected]
  15. 705 5th Ave S #500, Seattle, WA 98104
  16. BSD LICENSE
  17. Copyright(c) <date> Intel Corporation.
  18. Redistribution and use in source and binary forms, with or without
  19. modification, are permitted provided that the following conditions
  20. are met:
  21. * Redistributions of source code must retain the above copyright
  22. notice, this list of conditions and the following disclaimer.
  23. * Redistributions in binary form must reproduce the above copyright
  24. notice, this list of conditions and the following disclaimer in
  25. the documentation and/or other materials provided with the
  26. distribution.
  27. * Neither the name of Intel Corporation nor the names of its
  28. contributors may be used to endorse or promote products derived
  29. from this software without specific prior written permission.
  30. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. #include <stdio.h>
  43. #include <util/dstr.h>
  44. #include <util/darray.h>
  45. #include <util/platform.h>
  46. #include <obs-module.h>
  47. #include <obs-avc.h>
  48. #ifndef _STDINT_H_INCLUDED
  49. #define _STDINT_H_INCLUDED
  50. #endif
  51. #include "QSV_Encoder.h"
  52. #include <Windows.h>
  53. #define do_log(level, format, ...) \
  54. blog(level, "[qsv encoder: '%s'] " format, \
  55. obs_encoder_get_name(obsqsv->encoder), ##__VA_ARGS__)
  56. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  57. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  58. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  59. /* ------------------------------------------------------------------------- */
  60. struct obs_qsv {
  61. obs_encoder_t *encoder;
  62. qsv_param_t params;
  63. qsv_t *context;
  64. DARRAY(uint8_t) packet_data;
  65. uint8_t *extra_data;
  66. uint8_t *sei;
  67. size_t extra_data_size;
  68. size_t sei_size;
  69. os_performance_token_t *performance_token;
  70. };
  71. /* ------------------------------------------------------------------------- */
  72. static CRITICAL_SECTION g_QsvCs;
  73. static unsigned short g_verMajor;
  74. static unsigned short g_verMinor;
  75. static int64_t g_pts2dtsShift;
  76. static int64_t g_prevDts;
  77. static bool g_bFirst;
  78. static const char *obs_qsv_getname(void *type_data)
  79. {
  80. UNUSED_PARAMETER(type_data);
  81. return "QuickSync H.264";
  82. }
  83. static void obs_qsv_stop(void *data);
  84. static void clear_data(struct obs_qsv *obsqsv)
  85. {
  86. if (obsqsv->context) {
  87. EnterCriticalSection(&g_QsvCs);
  88. qsv_encoder_close(obsqsv->context);
  89. obsqsv->context = NULL;
  90. LeaveCriticalSection(&g_QsvCs);
  91. // bfree(obsqsv->sei);
  92. bfree(obsqsv->extra_data);
  93. // obsqsv->sei = NULL;
  94. obsqsv->extra_data = NULL;
  95. }
  96. }
  97. static void obs_qsv_destroy(void *data)
  98. {
  99. struct obs_qsv *obsqsv = (struct obs_qsv *)data;
  100. if (obsqsv) {
  101. os_end_high_performance(obsqsv->performance_token);
  102. clear_data(obsqsv);
  103. da_free(obsqsv->packet_data);
  104. bfree(obsqsv);
  105. }
  106. }
  107. static void obs_qsv_defaults(obs_data_t *settings)
  108. {
  109. obs_data_set_default_string(settings, "target_usage", "balanced");
  110. obs_data_set_default_int(settings, "bitrate", 2500);
  111. obs_data_set_default_int(settings, "max_bitrate", 3000);
  112. obs_data_set_default_string(settings, "profile", "high");
  113. obs_data_set_default_int(settings, "async_depth", 4);
  114. obs_data_set_default_string(settings, "rate_control", "CBR");
  115. obs_data_set_default_int(settings, "accuracy", 1000);
  116. obs_data_set_default_int(settings, "convergence", 1);
  117. obs_data_set_default_int(settings, "qpi", 23);
  118. obs_data_set_default_int(settings, "qpp", 23);
  119. obs_data_set_default_int(settings, "qpb", 23);
  120. obs_data_set_default_int(settings, "icq_quality", 23);
  121. obs_data_set_default_int(settings, "la_depth", 15);
  122. obs_data_set_default_int(settings, "keyint_sec", 3);
  123. obs_data_set_default_int(settings, "bframes", 3);
  124. obs_data_set_default_bool(settings, "mbbrc", true);
  125. }
  126. static inline void add_strings(obs_property_t *list, const char *const *strings)
  127. {
  128. while (*strings) {
  129. obs_property_list_add_string(list, *strings, *strings);
  130. strings++;
  131. }
  132. }
  133. #define TEXT_SPEED obs_module_text("TargetUsage")
  134. #define TEXT_TARGET_BITRATE obs_module_text("Bitrate")
  135. #define TEXT_MAX_BITRATE obs_module_text("MaxBitrate")
  136. #define TEXT_PROFILE obs_module_text("Profile")
  137. #define TEXT_ASYNC_DEPTH obs_module_text("AsyncDepth")
  138. #define TEXT_RATE_CONTROL obs_module_text("RateControl")
  139. #define TEXT_ACCURACY obs_module_text("Accuracy")
  140. #define TEXT_CONVERGENCE obs_module_text("Convergence")
  141. #define TEXT_ICQ_QUALITY obs_module_text("ICQQuality")
  142. #define TEXT_LA_DEPTH obs_module_text("LookAheadDepth")
  143. #define TEXT_KEYINT_SEC obs_module_text("KeyframeIntervalSec")
  144. #define TEXT_BFRAMES obs_module_text("B Frames")
  145. #define TEXT_MBBRC obs_module_text("Content Adaptive Quantization")
  146. static inline bool is_skl_or_greater_platform()
  147. {
  148. enum qsv_cpu_platform plat = qsv_get_cpu_platform();
  149. return (plat >= QSV_CPU_PLATFORM_SKL);
  150. }
  151. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  152. obs_data_t *settings)
  153. {
  154. const char *rate_control =
  155. obs_data_get_string(settings, "rate_control");
  156. bool bVisible = astrcmpi(rate_control, "VCM") == 0 ||
  157. astrcmpi(rate_control, "VBR") == 0;
  158. p = obs_properties_get(ppts, "max_bitrate");
  159. obs_property_set_visible(p, bVisible);
  160. bVisible = astrcmpi(rate_control, "CQP") == 0 ||
  161. astrcmpi(rate_control, "LA_ICQ") == 0 ||
  162. astrcmpi(rate_control, "ICQ") == 0;
  163. p = obs_properties_get(ppts, "bitrate");
  164. obs_property_set_visible(p, !bVisible);
  165. bVisible = astrcmpi(rate_control, "AVBR") == 0;
  166. p = obs_properties_get(ppts, "accuracy");
  167. obs_property_set_visible(p, bVisible);
  168. p = obs_properties_get(ppts, "convergence");
  169. obs_property_set_visible(p, bVisible);
  170. bVisible = astrcmpi(rate_control, "CQP") == 0;
  171. p = obs_properties_get(ppts, "qpi");
  172. obs_property_set_visible(p, bVisible);
  173. p = obs_properties_get(ppts, "qpb");
  174. obs_property_set_visible(p, bVisible);
  175. p = obs_properties_get(ppts, "qpp");
  176. obs_property_set_visible(p, bVisible);
  177. bVisible = astrcmpi(rate_control, "ICQ") == 0 ||
  178. astrcmpi(rate_control, "LA_ICQ") == 0;
  179. p = obs_properties_get(ppts, "icq_quality");
  180. obs_property_set_visible(p, bVisible);
  181. bVisible = astrcmpi(rate_control, "LA_ICQ") == 0 ||
  182. astrcmpi(rate_control, "LA_CBR") == 0 ||
  183. astrcmpi(rate_control, "LA_VBR") == 0;
  184. p = obs_properties_get(ppts, "la_depth");
  185. obs_property_set_visible(p, bVisible);
  186. bVisible = astrcmpi(rate_control, "CBR") == 0 ||
  187. astrcmpi(rate_control, "VBR") == 0 ||
  188. astrcmpi(rate_control, "AVBR") == 0;
  189. p = obs_properties_get(ppts, "mbbrc");
  190. obs_property_set_visible(p, bVisible);
  191. return true;
  192. }
  193. static bool profile_modified(obs_properties_t *ppts, obs_property_t *p,
  194. obs_data_t *settings)
  195. {
  196. const char *profile = obs_data_get_string(settings, "profile");
  197. enum qsv_cpu_platform plat = qsv_get_cpu_platform();
  198. bool bVisible = ((astrcmpi(profile, "high") == 0) &&
  199. (plat >= QSV_CPU_PLATFORM_ICL));
  200. p = obs_properties_get(ppts, "CQM");
  201. obs_property_set_visible(p, bVisible);
  202. return true;
  203. }
  204. static inline void add_rate_controls(obs_property_t *list,
  205. const struct qsv_rate_control_info *rc)
  206. {
  207. enum qsv_cpu_platform plat = qsv_get_cpu_platform();
  208. while (rc->name) {
  209. if (!rc->haswell_or_greater || plat >= QSV_CPU_PLATFORM_HSW)
  210. obs_property_list_add_string(list, rc->name, rc->name);
  211. rc++;
  212. }
  213. }
  214. static obs_properties_t *obs_qsv_props(void *unused)
  215. {
  216. UNUSED_PARAMETER(unused);
  217. obs_properties_t *props = obs_properties_create();
  218. obs_property_t *list;
  219. list = obs_properties_add_list(props, "target_usage", TEXT_SPEED,
  220. OBS_COMBO_TYPE_LIST,
  221. OBS_COMBO_FORMAT_STRING);
  222. add_strings(list, qsv_usage_names);
  223. list = obs_properties_add_list(props, "profile", TEXT_PROFILE,
  224. OBS_COMBO_TYPE_LIST,
  225. OBS_COMBO_FORMAT_STRING);
  226. add_strings(list, qsv_profile_names);
  227. obs_property_set_modified_callback(list, profile_modified);
  228. obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 1, 20, 1);
  229. obs_properties_add_int(props, "async_depth", TEXT_ASYNC_DEPTH, 1, 7, 1);
  230. list = obs_properties_add_list(props, "rate_control", TEXT_RATE_CONTROL,
  231. OBS_COMBO_TYPE_LIST,
  232. OBS_COMBO_FORMAT_STRING);
  233. add_rate_controls(list, qsv_ratecontrols);
  234. obs_property_set_modified_callback(list, rate_control_modified);
  235. obs_property_t *p;
  236. p = obs_properties_add_int(props, "bitrate", TEXT_TARGET_BITRATE, 50,
  237. 10000000, 50);
  238. obs_property_int_set_suffix(p, " Kbps");
  239. p = obs_properties_add_int(props, "max_bitrate", TEXT_MAX_BITRATE, 50,
  240. 10000000, 50);
  241. obs_property_int_set_suffix(p, " Kbps");
  242. obs_properties_add_int(props, "accuracy", TEXT_ACCURACY, 0, 10000, 1);
  243. obs_properties_add_int(props, "convergence", TEXT_CONVERGENCE, 0, 10,
  244. 1);
  245. obs_properties_add_int(props, "qpi", "QPI", 1, 51, 1);
  246. obs_properties_add_int(props, "qpp", "QPP", 1, 51, 1);
  247. obs_properties_add_int(props, "qpb", "QPB", 1, 51, 1);
  248. obs_properties_add_int(props, "icq_quality", TEXT_ICQ_QUALITY, 1, 51,
  249. 1);
  250. obs_properties_add_int(props, "la_depth", TEXT_LA_DEPTH, 10, 100, 1);
  251. obs_properties_add_int(props, "bframes", TEXT_BFRAMES, 0, 3, 1);
  252. if (is_skl_or_greater_platform())
  253. obs_properties_add_bool(props, "mbbrc", TEXT_MBBRC);
  254. obs_properties_add_bool(props, "CQM", "Customized quantization matrix");
  255. return props;
  256. }
  257. static void update_params(struct obs_qsv *obsqsv, obs_data_t *settings)
  258. {
  259. video_t *video = obs_encoder_video(obsqsv->encoder);
  260. const struct video_output_info *voi = video_output_get_info(video);
  261. const char *target_usage =
  262. obs_data_get_string(settings, "target_usage");
  263. const char *profile = obs_data_get_string(settings, "profile");
  264. const char *rate_control =
  265. obs_data_get_string(settings, "rate_control");
  266. int async_depth = (int)obs_data_get_int(settings, "async_depth");
  267. int target_bitrate = (int)obs_data_get_int(settings, "bitrate");
  268. int max_bitrate = (int)obs_data_get_int(settings, "max_bitrate");
  269. int accuracy = (int)obs_data_get_int(settings, "accuracy");
  270. int convergence = (int)obs_data_get_int(settings, "convergence");
  271. int qpi = (int)obs_data_get_int(settings, "qpi");
  272. int qpp = (int)obs_data_get_int(settings, "qpp");
  273. int qpb = (int)obs_data_get_int(settings, "qpb");
  274. int icq_quality = (int)obs_data_get_int(settings, "icq_quality");
  275. int la_depth = (int)obs_data_get_int(settings, "la_depth");
  276. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  277. bool cbr_override = obs_data_get_bool(settings, "cbr");
  278. int bFrames = (int)obs_data_get_int(settings, "bframes");
  279. bool mbbrc = obs_data_get_bool(settings, "mbbrc");
  280. if (obs_data_has_user_value(settings, "bf"))
  281. bFrames = (int)obs_data_get_int(settings, "bf");
  282. enum qsv_cpu_platform plat = qsv_get_cpu_platform();
  283. if (plat == QSV_CPU_PLATFORM_IVB || plat == QSV_CPU_PLATFORM_SNB)
  284. bFrames = 0;
  285. int width = (int)obs_encoder_get_width(obsqsv->encoder);
  286. int height = (int)obs_encoder_get_height(obsqsv->encoder);
  287. if (astrcmpi(target_usage, "quality") == 0)
  288. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_BEST_QUALITY;
  289. else if (astrcmpi(target_usage, "balanced") == 0)
  290. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_BALANCED;
  291. else if (astrcmpi(target_usage, "speed") == 0)
  292. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_BEST_SPEED;
  293. else if (astrcmpi(target_usage, "veryslow") == 0)
  294. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_1;
  295. else if (astrcmpi(target_usage, "slower") == 0)
  296. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_2;
  297. else if (astrcmpi(target_usage, "slow") == 0)
  298. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_3;
  299. else if (astrcmpi(target_usage, "medium") == 0)
  300. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_4;
  301. else if (astrcmpi(target_usage, "fast") == 0)
  302. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_5;
  303. else if (astrcmpi(target_usage, "faster") == 0)
  304. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_6;
  305. else if (astrcmpi(target_usage, "veryfast") == 0)
  306. obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_7;
  307. if (astrcmpi(profile, "baseline") == 0)
  308. obsqsv->params.nCodecProfile = MFX_PROFILE_AVC_BASELINE;
  309. else if (astrcmpi(profile, "main") == 0)
  310. obsqsv->params.nCodecProfile = MFX_PROFILE_AVC_MAIN;
  311. else if (astrcmpi(profile, "high") == 0)
  312. obsqsv->params.nCodecProfile = MFX_PROFILE_AVC_HIGH;
  313. /* internal convenience parameter, overrides rate control param
  314. * XXX: Deprecated */
  315. if (cbr_override) {
  316. warn("\"cbr\" setting has been deprecated for all encoders! "
  317. "Please set \"rate_control\" to \"CBR\" instead. "
  318. "Forcing CBR mode. "
  319. "(Note to all: this is why you shouldn't use strings for "
  320. "common settings)");
  321. rate_control = "CBR";
  322. }
  323. if (astrcmpi(rate_control, "CBR") == 0)
  324. obsqsv->params.nRateControl = MFX_RATECONTROL_CBR;
  325. else if (astrcmpi(rate_control, "VBR") == 0)
  326. obsqsv->params.nRateControl = MFX_RATECONTROL_VBR;
  327. else if (astrcmpi(rate_control, "VCM") == 0)
  328. obsqsv->params.nRateControl = MFX_RATECONTROL_VCM;
  329. else if (astrcmpi(rate_control, "CQP") == 0)
  330. obsqsv->params.nRateControl = MFX_RATECONTROL_CQP;
  331. else if (astrcmpi(rate_control, "AVBR") == 0)
  332. obsqsv->params.nRateControl = MFX_RATECONTROL_AVBR;
  333. else if (astrcmpi(rate_control, "ICQ") == 0)
  334. obsqsv->params.nRateControl = MFX_RATECONTROL_ICQ;
  335. else if (astrcmpi(rate_control, "LA_ICQ") == 0)
  336. obsqsv->params.nRateControl = MFX_RATECONTROL_LA_ICQ;
  337. else if (astrcmpi(rate_control, "LA_VBR") == 0)
  338. obsqsv->params.nRateControl = MFX_RATECONTROL_LA;
  339. else if (astrcmpi(rate_control, "LA_CBR") == 0)
  340. obsqsv->params.nRateControl = MFX_RATECONTROL_LA_HRD;
  341. obsqsv->params.nAsyncDepth = (mfxU16)async_depth;
  342. obsqsv->params.nAccuracy = (mfxU16)accuracy;
  343. obsqsv->params.nConvergence = (mfxU16)convergence;
  344. obsqsv->params.nQPI = (mfxU16)qpi;
  345. obsqsv->params.nQPP = (mfxU16)qpp;
  346. obsqsv->params.nQPB = (mfxU16)qpb;
  347. obsqsv->params.nLADEPTH = (mfxU16)la_depth;
  348. obsqsv->params.nTargetBitRate = (mfxU16)target_bitrate;
  349. obsqsv->params.nMaxBitRate = (mfxU16)max_bitrate;
  350. obsqsv->params.nWidth = (mfxU16)width;
  351. obsqsv->params.nHeight = (mfxU16)height;
  352. obsqsv->params.nFpsNum = (mfxU16)voi->fps_num;
  353. obsqsv->params.nFpsDen = (mfxU16)voi->fps_den;
  354. obsqsv->params.nbFrames = (mfxU16)bFrames;
  355. obsqsv->params.nKeyIntSec = (mfxU16)keyint_sec;
  356. obsqsv->params.nICQQuality = (mfxU16)icq_quality;
  357. obsqsv->params.bMBBRC = mbbrc;
  358. info("settings:\n\trate_control: %s", rate_control);
  359. if (obsqsv->params.nRateControl != MFX_RATECONTROL_LA_ICQ &&
  360. obsqsv->params.nRateControl != MFX_RATECONTROL_ICQ &&
  361. obsqsv->params.nRateControl != MFX_RATECONTROL_CQP)
  362. blog(LOG_INFO, "\ttarget_bitrate: %d",
  363. (int)obsqsv->params.nTargetBitRate);
  364. if (obsqsv->params.nRateControl == MFX_RATECONTROL_VBR ||
  365. obsqsv->params.nRateControl == MFX_RATECONTROL_VCM)
  366. blog(LOG_INFO, "\tmax_bitrate: %d",
  367. (int)obsqsv->params.nMaxBitRate);
  368. if (obsqsv->params.nRateControl == MFX_RATECONTROL_LA_ICQ ||
  369. obsqsv->params.nRateControl == MFX_RATECONTROL_ICQ)
  370. blog(LOG_INFO, "\tICQ Quality: %d",
  371. (int)obsqsv->params.nICQQuality);
  372. if (obsqsv->params.nRateControl == MFX_RATECONTROL_LA_ICQ ||
  373. obsqsv->params.nRateControl == MFX_RATECONTROL_LA ||
  374. obsqsv->params.nRateControl == MFX_RATECONTROL_LA_HRD)
  375. blog(LOG_INFO, "\tLookahead Depth:%d",
  376. (int)obsqsv->params.nLADEPTH);
  377. if (obsqsv->params.nRateControl == MFX_RATECONTROL_CQP)
  378. blog(LOG_INFO,
  379. "\tqpi: %d\n"
  380. "\tqpb: %d\n"
  381. "\tqpp: %d",
  382. qpi, qpb, qpp);
  383. blog(LOG_INFO,
  384. "\tfps_num: %d\n"
  385. "\tfps_den: %d\n"
  386. "\twidth: %d\n"
  387. "\theight: %d",
  388. voi->fps_num, voi->fps_den, width, height);
  389. obsqsv->params.bCQM = (bool)obs_data_get_bool(settings, "CQM");
  390. info("debug info:");
  391. }
  392. static bool update_settings(struct obs_qsv *obsqsv, obs_data_t *settings)
  393. {
  394. update_params(obsqsv, settings);
  395. return true;
  396. }
  397. static void load_headers(struct obs_qsv *obsqsv)
  398. {
  399. DARRAY(uint8_t) header;
  400. static uint8_t sei = 0;
  401. // Not sure if SEI is needed.
  402. // Just filling in empty meaningless SEI message.
  403. // Seems to work fine.
  404. // DARRAY(uint8_t) sei;
  405. da_init(header);
  406. // da_init(sei);
  407. uint8_t *pSPS, *pPPS;
  408. uint16_t nSPS, nPPS;
  409. qsv_encoder_headers(obsqsv->context, &pSPS, &pPPS, &nSPS, &nPPS);
  410. da_push_back_array(header, pSPS, nSPS);
  411. da_push_back_array(header, pPPS, nPPS);
  412. obsqsv->extra_data = header.array;
  413. obsqsv->extra_data_size = header.num;
  414. obsqsv->sei = &sei;
  415. obsqsv->sei_size = 1;
  416. }
  417. static bool obs_qsv_update(void *data, obs_data_t *settings)
  418. {
  419. struct obs_qsv *obsqsv = data;
  420. bool success = update_settings(obsqsv, settings);
  421. int ret;
  422. if (success) {
  423. EnterCriticalSection(&g_QsvCs);
  424. ret = qsv_encoder_reconfig(obsqsv->context, &obsqsv->params);
  425. if (ret != 0)
  426. warn("Failed to reconfigure: %d", ret);
  427. LeaveCriticalSection(&g_QsvCs);
  428. return ret == 0;
  429. }
  430. return false;
  431. }
  432. static void *obs_qsv_create(obs_data_t *settings, obs_encoder_t *encoder)
  433. {
  434. InitializeCriticalSection(&g_QsvCs);
  435. struct obs_qsv *obsqsv = bzalloc(sizeof(struct obs_qsv));
  436. obsqsv->encoder = encoder;
  437. if (update_settings(obsqsv, settings)) {
  438. EnterCriticalSection(&g_QsvCs);
  439. obsqsv->context = qsv_encoder_open(&obsqsv->params);
  440. LeaveCriticalSection(&g_QsvCs);
  441. if (obsqsv->context == NULL)
  442. warn("qsv failed to load");
  443. else
  444. load_headers(obsqsv);
  445. } else {
  446. warn("bad settings specified");
  447. }
  448. qsv_encoder_version(&g_verMajor, &g_verMinor);
  449. blog(LOG_INFO,
  450. "\tmajor: %d\n"
  451. "\tminor: %d",
  452. g_verMajor, g_verMinor);
  453. // MSDK 1.6 or less doesn't have automatic DTS calculation
  454. // including early SandyBridge.
  455. // Need to add manual DTS from PTS.
  456. if (g_verMajor == 1 && g_verMinor < 7) {
  457. int64_t interval = obsqsv->params.nbFrames + 1;
  458. int64_t GopPicSize = (int64_t)(obsqsv->params.nKeyIntSec *
  459. obsqsv->params.nFpsNum /
  460. (float)obsqsv->params.nFpsDen);
  461. g_pts2dtsShift =
  462. GopPicSize - (GopPicSize / interval) * interval;
  463. blog(LOG_INFO,
  464. "\tinterval: %d\n"
  465. "\tGopPictSize: %d\n"
  466. "\tg_pts2dtsShift: %d",
  467. interval, GopPicSize, g_pts2dtsShift);
  468. } else
  469. g_pts2dtsShift = -1;
  470. if (!obsqsv->context) {
  471. bfree(obsqsv);
  472. return NULL;
  473. }
  474. obsqsv->performance_token = os_request_high_performance("qsv encoding");
  475. g_bFirst = true;
  476. return obsqsv;
  477. }
  478. static bool obs_qsv_extra_data(void *data, uint8_t **extra_data, size_t *size)
  479. {
  480. struct obs_qsv *obsqsv = data;
  481. if (!obsqsv->context)
  482. return false;
  483. *extra_data = obsqsv->extra_data;
  484. *size = obsqsv->extra_data_size;
  485. return true;
  486. }
  487. static bool obs_qsv_sei(void *data, uint8_t **sei, size_t *size)
  488. {
  489. struct obs_qsv *obsqsv = data;
  490. if (!obsqsv->context)
  491. return false;
  492. /* (Jim) Unused */
  493. UNUSED_PARAMETER(sei);
  494. UNUSED_PARAMETER(size);
  495. *sei = obsqsv->sei;
  496. *size = obsqsv->sei_size;
  497. return true;
  498. }
  499. static inline bool valid_format(enum video_format format)
  500. {
  501. return format == VIDEO_FORMAT_NV12;
  502. }
  503. static inline void cap_resolution(obs_encoder_t *encoder,
  504. struct video_scale_info *info)
  505. {
  506. enum qsv_cpu_platform qsv_platform = qsv_get_cpu_platform();
  507. uint32_t width = obs_encoder_get_width(encoder);
  508. uint32_t height = obs_encoder_get_height(encoder);
  509. info->height = height;
  510. info->width = width;
  511. if (qsv_platform <= QSV_CPU_PLATFORM_IVB) {
  512. if (width > 1920) {
  513. info->width = 1920;
  514. }
  515. if (height > 1200) {
  516. info->height = 1200;
  517. }
  518. }
  519. }
  520. static void obs_qsv_video_info(void *data, struct video_scale_info *info)
  521. {
  522. struct obs_qsv *obsqsv = data;
  523. enum video_format pref_format;
  524. pref_format = obs_encoder_get_preferred_video_format(obsqsv->encoder);
  525. if (!valid_format(pref_format)) {
  526. pref_format = valid_format(info->format) ? info->format
  527. : VIDEO_FORMAT_NV12;
  528. }
  529. info->format = pref_format;
  530. cap_resolution(obsqsv->encoder, info);
  531. }
  532. static void parse_packet(struct obs_qsv *obsqsv, struct encoder_packet *packet,
  533. mfxBitstream *pBS, uint32_t fps_num,
  534. bool *received_packet)
  535. {
  536. uint8_t *start, *end;
  537. int type;
  538. if (pBS == NULL || pBS->DataLength == 0) {
  539. *received_packet = false;
  540. return;
  541. }
  542. da_resize(obsqsv->packet_data, 0);
  543. da_push_back_array(obsqsv->packet_data, &pBS->Data[pBS->DataOffset],
  544. pBS->DataLength);
  545. packet->data = obsqsv->packet_data.array;
  546. packet->size = obsqsv->packet_data.num;
  547. packet->type = OBS_ENCODER_VIDEO;
  548. packet->pts = pBS->TimeStamp * fps_num / 90000;
  549. packet->keyframe = (pBS->FrameType & MFX_FRAMETYPE_IDR);
  550. uint16_t frameType = pBS->FrameType;
  551. uint8_t priority;
  552. if (frameType & MFX_FRAMETYPE_I)
  553. priority = OBS_NAL_PRIORITY_HIGHEST;
  554. else if ((frameType & MFX_FRAMETYPE_P) ||
  555. (frameType & MFX_FRAMETYPE_REF))
  556. priority = OBS_NAL_PRIORITY_HIGH;
  557. else
  558. priority = 0;
  559. packet->priority = priority;
  560. /* ------------------------------------ */
  561. start = obsqsv->packet_data.array;
  562. end = start + obsqsv->packet_data.num;
  563. start = (uint8_t *)obs_avc_find_startcode(start, end);
  564. while (true) {
  565. while (start < end && !*(start++))
  566. ;
  567. if (start == end)
  568. break;
  569. type = start[0] & 0x1F;
  570. if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE) {
  571. start[0] &= ~(3 << 5);
  572. start[0] |=
  573. priority
  574. << 5; //0 for non-ref frames and not equal to 0 for ref frames
  575. }
  576. start = (uint8_t *)obs_avc_find_startcode(start, end);
  577. }
  578. /* ------------------------------------ */
  579. //bool iFrame = pBS->FrameType & MFX_FRAMETYPE_I;
  580. //bool bFrame = pBS->FrameType & MFX_FRAMETYPE_B;
  581. bool pFrame = pBS->FrameType & MFX_FRAMETYPE_P;
  582. //int iType = iFrame ? 0 : (bFrame ? 1 : (pFrame ? 2 : -1));
  583. //int64_t interval = obsqsv->params.nbFrames + 1;
  584. // In case MSDK doesn't support automatic DecodeTimeStamp, do manual
  585. // calculation
  586. if (g_pts2dtsShift >= 0) {
  587. if (g_bFirst) {
  588. packet->dts = packet->pts - 3 * obsqsv->params.nFpsDen;
  589. } else if (pFrame) {
  590. packet->dts = packet->pts - 10 * obsqsv->params.nFpsDen;
  591. g_prevDts = packet->dts;
  592. } else {
  593. packet->dts = g_prevDts + obsqsv->params.nFpsDen;
  594. g_prevDts = packet->dts;
  595. }
  596. } else {
  597. packet->dts = pBS->DecodeTimeStamp * fps_num / 90000;
  598. }
  599. #if 0
  600. info("parse packet:\n"
  601. "\tFrameType: %d\n"
  602. "\tpts: %d\n"
  603. "\tdts: %d",
  604. iType, packet->pts, packet->dts);
  605. #endif
  606. *received_packet = true;
  607. pBS->DataLength = 0;
  608. g_bFirst = false;
  609. }
  610. static bool obs_qsv_encode(void *data, struct encoder_frame *frame,
  611. struct encoder_packet *packet, bool *received_packet)
  612. {
  613. struct obs_qsv *obsqsv = data;
  614. if (!frame || !packet || !received_packet)
  615. return false;
  616. EnterCriticalSection(&g_QsvCs);
  617. video_t *video = obs_encoder_video(obsqsv->encoder);
  618. const struct video_output_info *voi = video_output_get_info(video);
  619. mfxBitstream *pBS = NULL;
  620. int ret;
  621. mfxU64 qsvPTS = frame->pts * 90000 / voi->fps_num;
  622. // FIXME: remove null check from the top of this function
  623. // if we actually do expect null frames to complete output.
  624. if (frame)
  625. ret = qsv_encoder_encode(obsqsv->context, qsvPTS,
  626. frame->data[0], frame->data[1],
  627. frame->linesize[0], frame->linesize[1],
  628. &pBS);
  629. else
  630. ret = qsv_encoder_encode(obsqsv->context, qsvPTS, NULL, NULL, 0,
  631. 0, &pBS);
  632. if (ret < 0) {
  633. warn("encode failed");
  634. LeaveCriticalSection(&g_QsvCs);
  635. return false;
  636. }
  637. parse_packet(obsqsv, packet, pBS, voi->fps_num, received_packet);
  638. LeaveCriticalSection(&g_QsvCs);
  639. return true;
  640. }
  641. struct obs_encoder_info obs_qsv_encoder = {
  642. .id = "obs_qsv11",
  643. .type = OBS_ENCODER_VIDEO,
  644. .codec = "h264",
  645. .get_name = obs_qsv_getname,
  646. .create = obs_qsv_create,
  647. .destroy = obs_qsv_destroy,
  648. .encode = obs_qsv_encode,
  649. .update = obs_qsv_update,
  650. .get_properties = obs_qsv_props,
  651. .get_defaults = obs_qsv_defaults,
  652. .get_extra_data = obs_qsv_extra_data,
  653. .get_sei_data = obs_qsv_sei,
  654. .get_video_info = obs_qsv_video_info,
  655. .caps = OBS_ENCODER_CAP_DYN_BITRATE,
  656. };