obs-ffmpeg-av1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /******************************************************************************
  2. Copyright (C) 2022 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "obs-ffmpeg-video-encoders.h"
  15. #define do_log(level, format, ...) \
  16. blog(level, "[AV1 encoder: '%s'] " format, \
  17. obs_encoder_get_name(enc->ffve.encoder), ##__VA_ARGS__)
  18. #define error(format, ...) do_log(LOG_ERROR, format, ##__VA_ARGS__)
  19. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  20. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  21. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  22. enum av1_encoder_type {
  23. AV1_ENCODER_TYPE_AOM,
  24. AV1_ENCODER_TYPE_SVT,
  25. };
  26. struct av1_encoder {
  27. struct ffmpeg_video_encoder ffve;
  28. enum av1_encoder_type type;
  29. DARRAY(uint8_t) header;
  30. };
  31. static const char *aom_av1_getname(void *unused)
  32. {
  33. UNUSED_PARAMETER(unused);
  34. return "AOM AV1";
  35. }
  36. static const char *svt_av1_getname(void *unused)
  37. {
  38. UNUSED_PARAMETER(unused);
  39. return "SVT-AV1";
  40. }
  41. static void av1_video_info(void *data, struct video_scale_info *info)
  42. {
  43. UNUSED_PARAMETER(data);
  44. switch (info->format) {
  45. case VIDEO_FORMAT_I010:
  46. case VIDEO_FORMAT_P010:
  47. info->format = VIDEO_FORMAT_I010;
  48. break;
  49. default:
  50. info->format = VIDEO_FORMAT_I420;
  51. }
  52. }
  53. static bool av1_update(struct av1_encoder *enc, obs_data_t *settings)
  54. {
  55. const char *rc = obs_data_get_string(settings, "rate_control");
  56. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  57. int cqp = (int)obs_data_get_int(settings, "cqp");
  58. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  59. int preset = (int)obs_data_get_int(settings, "preset");
  60. AVDictionary *svtav1_opts = NULL;
  61. video_t *video = obs_encoder_video(enc->ffve.encoder);
  62. const struct video_output_info *voi = video_output_get_info(video);
  63. struct video_scale_info info;
  64. info.format = voi->format;
  65. info.colorspace = voi->colorspace;
  66. info.range = voi->range;
  67. enc->ffve.context->thread_count = 0;
  68. av1_video_info(enc, &info);
  69. if (enc->type == AV1_ENCODER_TYPE_SVT) {
  70. av_opt_set_int(enc->ffve.context->priv_data, "preset", preset,
  71. 0);
  72. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59, 37, 100)
  73. av_dict_set_int(&svtav1_opts, "rc", 1, 0);
  74. #else
  75. av_opt_set(enc->ffve.context->priv_data, "rc", "vbr", 0);
  76. #endif
  77. } else if (enc->type == AV1_ENCODER_TYPE_AOM) {
  78. av_opt_set_int(enc->ffve.context->priv_data, "cpu-used", preset,
  79. 0);
  80. av_opt_set(enc->ffve.context->priv_data, "usage", "realtime",
  81. 0);
  82. #if 0
  83. av_opt_set_int(enc->ffve.context->priv_data, "tile-columns", 4, 0);
  84. //av_opt_set_int(enc->ffve.context->priv_data, "tile-rows", 4, 0);
  85. #else
  86. av_opt_set_int(enc->ffve.context->priv_data, "tile-columns", 2,
  87. 0);
  88. av_opt_set_int(enc->ffve.context->priv_data, "tile-rows", 2, 0);
  89. #endif
  90. av_opt_set_int(enc->ffve.context->priv_data, "row-mt", 1, 0);
  91. }
  92. if (astrcmpi(rc, "cqp") == 0) {
  93. bitrate = 0;
  94. av_opt_set_int(enc->ffve.context->priv_data, "crf", cqp, 0);
  95. if (enc->type == AV1_ENCODER_TYPE_SVT) {
  96. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59, 37, 100)
  97. av_dict_set_int(&svtav1_opts, "rc", 0, 0);
  98. av_opt_set_int(enc->ffve.context->priv_data, "qp", cqp,
  99. 0);
  100. #else
  101. av_opt_set(enc->ffve.context->priv_data, "rc", "cqp",
  102. 0);
  103. av_opt_set_int(enc->ffve.context->priv_data, "qp", cqp,
  104. 0);
  105. #endif
  106. }
  107. } else if (astrcmpi(rc, "vbr") != 0) { /* CBR by default */
  108. const int64_t rate = bitrate * INT64_C(1000);
  109. enc->ffve.context->rc_min_rate = rate;
  110. cqp = 0;
  111. if (enc->type == AV1_ENCODER_TYPE_SVT) {
  112. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59, 37, 100)
  113. av_dict_set_int(&svtav1_opts, "rc", 2, 0);
  114. av_dict_set_int(&svtav1_opts, "pred-struct", 1, 0);
  115. av_dict_set_int(&svtav1_opts, "bias-pct", 0, 0);
  116. av_dict_set_int(&svtav1_opts, "tbr", rate, 0);
  117. #else
  118. enc->ffve.context->rc_max_rate = rate;
  119. av_opt_set(enc->ffve.context->priv_data, "rc", "cvbr",
  120. 0);
  121. #endif
  122. } else {
  123. enc->ffve.context->rc_max_rate = rate;
  124. }
  125. }
  126. if (enc->type == AV1_ENCODER_TYPE_SVT) {
  127. av_opt_set_dict_val(enc->ffve.context->priv_data, "svtav1_opts",
  128. svtav1_opts, 0);
  129. }
  130. const char *ffmpeg_opts = obs_data_get_string(settings, "ffmpeg_opts");
  131. ffmpeg_video_encoder_update(&enc->ffve, bitrate, keyint_sec, voi, &info,
  132. ffmpeg_opts);
  133. av_dict_free(&svtav1_opts);
  134. info("settings:\n"
  135. "\tencoder: %s\n"
  136. "\trate_control: %s\n"
  137. "\tbitrate: %d\n"
  138. "\tcqp: %d\n"
  139. "\tkeyint: %d\n"
  140. "\tpreset: %d\n"
  141. "\twidth: %d\n"
  142. "\theight: %d\n"
  143. "\tffmpeg opts: %s\n",
  144. enc->ffve.enc_name, rc, bitrate, cqp, enc->ffve.context->gop_size,
  145. preset, enc->ffve.context->width, enc->ffve.height, ffmpeg_opts);
  146. enc->ffve.context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  147. return ffmpeg_video_encoder_init_codec(&enc->ffve);
  148. }
  149. static void av1_destroy(void *data)
  150. {
  151. struct av1_encoder *enc = data;
  152. ffmpeg_video_encoder_free(&enc->ffve);
  153. da_free(enc->header);
  154. bfree(enc);
  155. }
  156. static void on_first_packet(void *data, AVPacket *pkt, struct darray *da)
  157. {
  158. struct av1_encoder *enc = data;
  159. if (enc->type == AV1_ENCODER_TYPE_SVT) {
  160. da_copy_array(enc->header, enc->ffve.context->extradata,
  161. enc->ffve.context->extradata_size);
  162. } else {
  163. for (int i = 0; i < pkt->side_data_elems; i++) {
  164. AVPacketSideData *side_data = pkt->side_data + i;
  165. if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
  166. da_copy_array(enc->header, side_data->data,
  167. side_data->size);
  168. break;
  169. }
  170. }
  171. }
  172. darray_copy_array(1, da, pkt->data, pkt->size);
  173. }
  174. static void *av1_create_internal(obs_data_t *settings, obs_encoder_t *encoder,
  175. const char *enc_lib, const char *enc_name)
  176. {
  177. video_t *video = obs_encoder_video(encoder);
  178. const struct video_output_info *voi = video_output_get_info(video);
  179. if (voi->format != VIDEO_FORMAT_P010 &&
  180. voi->format != VIDEO_FORMAT_I010) {
  181. if (voi->colorspace == VIDEO_CS_2100_PQ ||
  182. voi->colorspace == VIDEO_CS_2100_HLG) {
  183. const char *const text =
  184. obs_module_text("AV1.8bitUnsupportedHdr");
  185. obs_encoder_set_last_error(encoder, text);
  186. blog(LOG_ERROR, "[AV1 encoder] %s", text);
  187. return NULL;
  188. }
  189. }
  190. struct av1_encoder *enc = bzalloc(sizeof(*enc));
  191. if (strcmp(enc_lib, "libsvtav1") == 0)
  192. enc->type = AV1_ENCODER_TYPE_SVT;
  193. else if (strcmp(enc_lib, "libaom-av1") == 0)
  194. enc->type = AV1_ENCODER_TYPE_AOM;
  195. if (!ffmpeg_video_encoder_init(&enc->ffve, enc, encoder, enc_lib, NULL,
  196. enc_name, NULL, on_first_packet))
  197. goto fail;
  198. if (!av1_update(enc, settings))
  199. goto fail;
  200. return enc;
  201. fail:
  202. av1_destroy(enc);
  203. return NULL;
  204. }
  205. static void *svt_av1_create(obs_data_t *settings, obs_encoder_t *encoder)
  206. {
  207. return av1_create_internal(settings, encoder, "libsvtav1", "SVT-AV1");
  208. }
  209. static void *aom_av1_create(obs_data_t *settings, obs_encoder_t *encoder)
  210. {
  211. return av1_create_internal(settings, encoder, "libaom-av1", "AOM AV1");
  212. }
  213. static bool av1_encode(void *data, struct encoder_frame *frame,
  214. struct encoder_packet *packet, bool *received_packet)
  215. {
  216. struct av1_encoder *enc = data;
  217. return ffmpeg_video_encode(&enc->ffve, frame, packet, received_packet);
  218. }
  219. void av1_defaults(obs_data_t *settings)
  220. {
  221. obs_data_set_default_int(settings, "bitrate", 2500);
  222. obs_data_set_default_int(settings, "keyint_sec", 0);
  223. obs_data_set_default_int(settings, "cqp", 50);
  224. obs_data_set_default_string(settings, "rate_control", "CBR");
  225. obs_data_set_default_int(settings, "preset", 8);
  226. }
  227. static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
  228. obs_data_t *settings)
  229. {
  230. const char *rc = obs_data_get_string(settings, "rate_control");
  231. bool cqp = astrcmpi(rc, "CQP") == 0;
  232. bool vbr = astrcmpi(rc, "VBR") == 0;
  233. p = obs_properties_get(ppts, "bitrate");
  234. obs_property_set_visible(p, !cqp);
  235. p = obs_properties_get(ppts, "max_bitrate");
  236. obs_property_set_visible(p, vbr);
  237. p = obs_properties_get(ppts, "cqp");
  238. obs_property_set_visible(p, cqp);
  239. return true;
  240. }
  241. obs_properties_t *av1_properties(enum av1_encoder_type type)
  242. {
  243. obs_properties_t *props = obs_properties_create();
  244. obs_property_t *p;
  245. p = obs_properties_add_list(props, "rate_control",
  246. obs_module_text("RateControl"),
  247. OBS_COMBO_TYPE_LIST,
  248. OBS_COMBO_FORMAT_STRING);
  249. obs_property_list_add_string(p, "CBR", "CBR");
  250. obs_property_list_add_string(p, "CQP", "CQP");
  251. obs_property_list_add_string(p, "VBR", "VBR");
  252. obs_property_set_modified_callback(p, rate_control_modified);
  253. p = obs_properties_add_int(props, "bitrate", obs_module_text("Bitrate"),
  254. 50, 300000, 50);
  255. obs_property_int_set_suffix(p, " Kbps");
  256. obs_properties_add_int(props, "cqp", obs_module_text("NVENC.CQLevel"),
  257. 1, 63, 1);
  258. p = obs_properties_add_int(props, "keyint_sec",
  259. obs_module_text("KeyframeIntervalSec"), 0,
  260. 10, 1);
  261. obs_property_int_set_suffix(p, " s");
  262. p = obs_properties_add_list(props, "preset", obs_module_text("Preset"),
  263. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  264. if (type == AV1_ENCODER_TYPE_SVT) {
  265. obs_property_list_add_int(p, "Very likely too slow (6)", 6);
  266. obs_property_list_add_int(p, "Probably too slow (7)", 7);
  267. obs_property_list_add_int(p, "Seems okay (8)", 8);
  268. obs_property_list_add_int(p, "Might be better (9)", 9);
  269. obs_property_list_add_int(p, "A little bit faster? (10)", 10);
  270. obs_property_list_add_int(p, "Hmm, not bad speed (11)", 11);
  271. obs_property_list_add_int(
  272. p, "Whoa, although quality might be not so great (12)",
  273. 12);
  274. } else if (type == AV1_ENCODER_TYPE_AOM) {
  275. obs_property_list_add_int(p, "Probably too slow (7)", 7);
  276. obs_property_list_add_int(p, "Okay (8)", 8);
  277. obs_property_list_add_int(p, "Fast (9)", 9);
  278. obs_property_list_add_int(p, "Fastest (10)", 10);
  279. }
  280. obs_properties_add_text(props, "ffmpeg_opts",
  281. obs_module_text("FFmpegOpts"),
  282. OBS_TEXT_DEFAULT);
  283. return props;
  284. }
  285. obs_properties_t *aom_av1_properties(void *unused)
  286. {
  287. UNUSED_PARAMETER(unused);
  288. return av1_properties(AV1_ENCODER_TYPE_AOM);
  289. }
  290. obs_properties_t *svt_av1_properties(void *unused)
  291. {
  292. UNUSED_PARAMETER(unused);
  293. return av1_properties(AV1_ENCODER_TYPE_SVT);
  294. }
  295. static bool av1_extra_data(void *data, uint8_t **extra_data, size_t *size)
  296. {
  297. struct av1_encoder *enc = data;
  298. *extra_data = enc->header.array;
  299. *size = enc->header.num;
  300. return true;
  301. }
  302. struct obs_encoder_info svt_av1_encoder_info = {
  303. .id = "ffmpeg_svt_av1",
  304. .type = OBS_ENCODER_VIDEO,
  305. .codec = "av1",
  306. .get_name = svt_av1_getname,
  307. .create = svt_av1_create,
  308. .destroy = av1_destroy,
  309. .encode = av1_encode,
  310. .get_defaults = av1_defaults,
  311. .get_properties = svt_av1_properties,
  312. .get_extra_data = av1_extra_data,
  313. .get_video_info = av1_video_info,
  314. };
  315. struct obs_encoder_info aom_av1_encoder_info = {
  316. .id = "ffmpeg_aom_av1",
  317. .type = OBS_ENCODER_VIDEO,
  318. .codec = "av1",
  319. .get_name = aom_av1_getname,
  320. .create = aom_av1_create,
  321. .destroy = av1_destroy,
  322. .encode = av1_encode,
  323. .get_defaults = av1_defaults,
  324. .get_properties = aom_av1_properties,
  325. .get_extra_data = av1_extra_data,
  326. .get_video_info = av1_video_info,
  327. };