obs-ffmpeg-av1.c 11 KB

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