video-io.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 <assert.h>
  15. #include <inttypes.h>
  16. #include "../util/bmem.h"
  17. #include "../util/platform.h"
  18. #include "../util/profiler.h"
  19. #include "../util/threading.h"
  20. #include "../util/darray.h"
  21. #include "../util/util_uint64.h"
  22. #include "format-conversion.h"
  23. #include "video-io.h"
  24. #include "video-frame.h"
  25. #include "video-scaler.h"
  26. extern profiler_name_store_t *obs_get_profiler_name_store(void);
  27. #define MAX_CONVERT_BUFFERS 3
  28. #define MAX_CACHE_SIZE 16
  29. struct cached_frame_info {
  30. struct video_data frame;
  31. int skipped;
  32. int count;
  33. };
  34. struct video_input {
  35. struct video_scale_info conversion;
  36. video_scaler_t *scaler;
  37. struct video_frame frame[MAX_CONVERT_BUFFERS];
  38. int cur_frame;
  39. // allow outputting at fractions of main composition FPS,
  40. // e.g. 60 FPS with frame_rate_divisor = 1 turns into 30 FPS
  41. //
  42. // a separate counter is used in favor of using remainder calculations
  43. // to allow "inputs" started at the same time to start on the same frame
  44. // whereas with remainder calculation the frame alignment would depend on
  45. // the total frame count at the time the encoder was started
  46. uint32_t frame_rate_divisor;
  47. uint32_t frame_rate_divisor_counter;
  48. void (*callback)(void *param, struct video_data *frame);
  49. void *param;
  50. };
  51. static inline void video_input_free(struct video_input *input)
  52. {
  53. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  54. video_frame_free(&input->frame[i]);
  55. video_scaler_destroy(input->scaler);
  56. }
  57. struct video_output {
  58. struct video_output_info info;
  59. pthread_t thread;
  60. pthread_mutex_t data_mutex;
  61. bool stop;
  62. os_sem_t *update_semaphore;
  63. uint64_t frame_time;
  64. volatile long skipped_frames;
  65. volatile long total_frames;
  66. pthread_mutex_t input_mutex;
  67. DARRAY(struct video_input) inputs;
  68. size_t available_frames;
  69. size_t first_added;
  70. size_t last_added;
  71. struct cached_frame_info cache[MAX_CACHE_SIZE];
  72. struct video_output *parent;
  73. volatile bool raw_active;
  74. volatile long gpu_refs;
  75. };
  76. /* ------------------------------------------------------------------------- */
  77. static inline bool scale_video_output(struct video_input *input, struct video_data *data)
  78. {
  79. bool success = true;
  80. if (input->scaler) {
  81. struct video_frame *frame;
  82. if (++input->cur_frame == MAX_CONVERT_BUFFERS)
  83. input->cur_frame = 0;
  84. frame = &input->frame[input->cur_frame];
  85. success = video_scaler_scale(input->scaler, frame->data, frame->linesize,
  86. (const uint8_t *const *)data->data, data->linesize);
  87. if (success) {
  88. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  89. data->data[i] = frame->data[i];
  90. data->linesize[i] = frame->linesize[i];
  91. }
  92. } else {
  93. blog(LOG_WARNING, "video-io: Could not scale frame!");
  94. }
  95. }
  96. return success;
  97. }
  98. static inline bool video_output_cur_frame(struct video_output *video)
  99. {
  100. struct cached_frame_info *frame_info;
  101. bool complete;
  102. bool skipped;
  103. /* -------------------------------- */
  104. pthread_mutex_lock(&video->data_mutex);
  105. frame_info = &video->cache[video->first_added];
  106. pthread_mutex_unlock(&video->data_mutex);
  107. /* -------------------------------- */
  108. pthread_mutex_lock(&video->input_mutex);
  109. for (size_t i = 0; i < video->inputs.num; i++) {
  110. struct video_input *input = video->inputs.array + i;
  111. struct video_data frame = frame_info->frame;
  112. // an explicit counter is used instead of remainder calculation
  113. // to allow multiple encoders started at the same time to start on
  114. // the same frame
  115. uint32_t skip = input->frame_rate_divisor_counter++;
  116. if (input->frame_rate_divisor_counter == input->frame_rate_divisor)
  117. input->frame_rate_divisor_counter = 0;
  118. if (skip)
  119. continue;
  120. if (scale_video_output(input, &frame))
  121. input->callback(input->param, &frame);
  122. }
  123. pthread_mutex_unlock(&video->input_mutex);
  124. /* -------------------------------- */
  125. pthread_mutex_lock(&video->data_mutex);
  126. frame_info->frame.timestamp += video->frame_time;
  127. complete = --frame_info->count == 0;
  128. skipped = frame_info->skipped > 0;
  129. if (complete) {
  130. if (++video->first_added == video->info.cache_size)
  131. video->first_added = 0;
  132. if (++video->available_frames == video->info.cache_size)
  133. video->last_added = video->first_added;
  134. } else if (skipped) {
  135. --frame_info->skipped;
  136. os_atomic_inc_long(&video->skipped_frames);
  137. }
  138. pthread_mutex_unlock(&video->data_mutex);
  139. /* -------------------------------- */
  140. return complete;
  141. }
  142. static void *video_thread(void *param)
  143. {
  144. struct video_output *video = param;
  145. os_set_thread_name("video-io: video thread");
  146. const char *video_thread_name =
  147. profile_store_name(obs_get_profiler_name_store(), "video_thread(%s)", video->info.name);
  148. while (os_sem_wait(video->update_semaphore) == 0) {
  149. if (video->stop)
  150. break;
  151. profile_start(video_thread_name);
  152. while (!video->stop && !video_output_cur_frame(video)) {
  153. os_atomic_inc_long(&video->total_frames);
  154. }
  155. os_atomic_inc_long(&video->total_frames);
  156. profile_end(video_thread_name);
  157. profile_reenable_thread();
  158. }
  159. return NULL;
  160. }
  161. /* ------------------------------------------------------------------------- */
  162. static inline bool valid_video_params(const struct video_output_info *info)
  163. {
  164. return info->height != 0 && info->width != 0 && info->fps_den != 0 && info->fps_num != 0;
  165. }
  166. static inline void init_cache(struct video_output *video)
  167. {
  168. if (video->info.cache_size > MAX_CACHE_SIZE)
  169. video->info.cache_size = MAX_CACHE_SIZE;
  170. for (size_t i = 0; i < video->info.cache_size; i++) {
  171. struct video_frame *frame;
  172. frame = (struct video_frame *)&video->cache[i];
  173. video_frame_init(frame, video->info.format, video->info.width, video->info.height);
  174. }
  175. video->available_frames = video->info.cache_size;
  176. }
  177. int video_output_open(video_t **video, struct video_output_info *info)
  178. {
  179. struct video_output *out;
  180. if (!valid_video_params(info))
  181. return VIDEO_OUTPUT_INVALIDPARAM;
  182. out = bzalloc(sizeof(struct video_output));
  183. if (!out)
  184. goto fail0;
  185. memcpy(&out->info, info, sizeof(struct video_output_info));
  186. out->frame_time = util_mul_div64(1000000000ULL, info->fps_den, info->fps_num);
  187. if (pthread_mutex_init_recursive(&out->data_mutex) != 0)
  188. goto fail0;
  189. if (pthread_mutex_init_recursive(&out->input_mutex) != 0)
  190. goto fail1;
  191. if (os_sem_init(&out->update_semaphore, 0) != 0)
  192. goto fail2;
  193. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  194. goto fail3;
  195. init_cache(out);
  196. *video = out;
  197. return VIDEO_OUTPUT_SUCCESS;
  198. fail3:
  199. os_sem_destroy(out->update_semaphore);
  200. fail2:
  201. pthread_mutex_destroy(&out->input_mutex);
  202. fail1:
  203. pthread_mutex_destroy(&out->data_mutex);
  204. fail0:
  205. bfree(out);
  206. return VIDEO_OUTPUT_FAIL;
  207. }
  208. void video_output_close(video_t *video)
  209. {
  210. if (!video)
  211. return;
  212. video_output_stop(video);
  213. pthread_mutex_lock(&video->input_mutex);
  214. for (size_t i = 0; i < video->inputs.num; i++)
  215. video_input_free(&video->inputs.array[i]);
  216. da_free(video->inputs);
  217. for (size_t i = 0; i < video->info.cache_size; i++)
  218. video_frame_free((struct video_frame *)&video->cache[i]);
  219. pthread_mutex_unlock(&video->input_mutex);
  220. os_sem_destroy(video->update_semaphore);
  221. pthread_mutex_destroy(&video->data_mutex);
  222. pthread_mutex_destroy(&video->input_mutex);
  223. bfree(video);
  224. }
  225. static size_t video_get_input_idx(const video_t *video, void (*callback)(void *param, struct video_data *frame),
  226. void *param)
  227. {
  228. for (size_t i = 0; i < video->inputs.num; i++) {
  229. struct video_input *input = video->inputs.array + i;
  230. if (input->callback == callback && input->param == param)
  231. return i;
  232. }
  233. return DARRAY_INVALID;
  234. }
  235. static bool match_range(enum video_range_type a, enum video_range_type b)
  236. {
  237. return (a == VIDEO_RANGE_FULL) == (b == VIDEO_RANGE_FULL);
  238. }
  239. static enum video_colorspace collapse_space(enum video_colorspace cs)
  240. {
  241. switch (cs) {
  242. case VIDEO_CS_SRGB:
  243. cs = VIDEO_CS_709;
  244. break;
  245. case VIDEO_CS_2100_HLG:
  246. cs = VIDEO_CS_2100_PQ;
  247. break;
  248. default:
  249. break;
  250. }
  251. return cs;
  252. }
  253. static bool match_space(enum video_colorspace a, enum video_colorspace b)
  254. {
  255. return (a == VIDEO_CS_DEFAULT) || (b == VIDEO_CS_DEFAULT) || (collapse_space(a) == collapse_space(b));
  256. }
  257. static inline bool video_input_init(struct video_input *input, struct video_output *video)
  258. {
  259. if (input->conversion.width != video->info.width || input->conversion.height != video->info.height ||
  260. input->conversion.format != video->info.format ||
  261. !match_range(input->conversion.range, video->info.range) ||
  262. !match_space(input->conversion.colorspace, video->info.colorspace)) {
  263. struct video_scale_info from = {.format = video->info.format,
  264. .width = video->info.width,
  265. .height = video->info.height,
  266. .range = video->info.range,
  267. .colorspace = video->info.colorspace};
  268. int ret = video_scaler_create(&input->scaler, &input->conversion, &from, VIDEO_SCALE_FAST_BILINEAR);
  269. if (ret != VIDEO_SCALER_SUCCESS) {
  270. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  271. blog(LOG_ERROR, "video_input_init: Bad "
  272. "scale conversion type");
  273. else
  274. blog(LOG_ERROR, "video_input_init: Failed to "
  275. "create scaler");
  276. return false;
  277. }
  278. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  279. video_frame_init(&input->frame[i], input->conversion.format, input->conversion.width,
  280. input->conversion.height);
  281. }
  282. return true;
  283. }
  284. static inline void reset_frames(video_t *video)
  285. {
  286. os_atomic_set_long(&video->skipped_frames, 0);
  287. os_atomic_set_long(&video->total_frames, 0);
  288. }
  289. static const video_t *get_const_root(const video_t *video)
  290. {
  291. while (video->parent)
  292. video = video->parent;
  293. return video;
  294. }
  295. static video_t *get_root(video_t *video)
  296. {
  297. while (video->parent)
  298. video = video->parent;
  299. return video;
  300. }
  301. bool video_output_connect(video_t *video, const struct video_scale_info *conversion,
  302. void (*callback)(void *param, struct video_data *frame), void *param)
  303. {
  304. return video_output_connect2(video, conversion, 1, callback, param);
  305. }
  306. bool video_output_connect2(video_t *video, const struct video_scale_info *conversion, uint32_t frame_rate_divisor,
  307. void (*callback)(void *param, struct video_data *frame), void *param)
  308. {
  309. bool success = false;
  310. video = get_root(video);
  311. if (!video || !callback || frame_rate_divisor == 0)
  312. return false;
  313. pthread_mutex_lock(&video->input_mutex);
  314. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  315. struct video_input input;
  316. memset(&input, 0, sizeof(input));
  317. input.callback = callback;
  318. input.param = param;
  319. input.frame_rate_divisor = frame_rate_divisor;
  320. if (conversion) {
  321. input.conversion = *conversion;
  322. } else {
  323. input.conversion.format = video->info.format;
  324. input.conversion.width = video->info.width;
  325. input.conversion.height = video->info.height;
  326. input.conversion.range = video->info.range;
  327. input.conversion.colorspace = video->info.colorspace;
  328. }
  329. if (input.conversion.width == 0)
  330. input.conversion.width = video->info.width;
  331. if (input.conversion.height == 0)
  332. input.conversion.height = video->info.height;
  333. success = video_input_init(&input, video);
  334. if (success) {
  335. if (video->inputs.num == 0) {
  336. if (!os_atomic_load_long(&video->gpu_refs)) {
  337. reset_frames(video);
  338. }
  339. os_atomic_set_bool(&video->raw_active, true);
  340. }
  341. da_push_back(video->inputs, &input);
  342. }
  343. }
  344. pthread_mutex_unlock(&video->input_mutex);
  345. return success;
  346. }
  347. static void log_skipped(video_t *video)
  348. {
  349. long skipped = os_atomic_load_long(&video->skipped_frames);
  350. double percentage_skipped = (double)skipped / (double)os_atomic_load_long(&video->total_frames) * 100.0;
  351. if (skipped)
  352. blog(LOG_INFO,
  353. "Video stopped, number of "
  354. "skipped frames due "
  355. "to encoding lag: "
  356. "%ld/%ld (%0.1f%%)",
  357. video->skipped_frames, video->total_frames, percentage_skipped);
  358. }
  359. void video_output_disconnect(video_t *video, void (*callback)(void *param, struct video_data *frame), void *param)
  360. {
  361. video_output_disconnect2(video, callback, param);
  362. }
  363. bool video_output_disconnect2(video_t *video, void (*callback)(void *param, struct video_data *frame), void *param)
  364. {
  365. if (!video || !callback)
  366. return false;
  367. video = get_root(video);
  368. pthread_mutex_lock(&video->input_mutex);
  369. size_t idx = video_get_input_idx(video, callback, param);
  370. if (idx != DARRAY_INVALID) {
  371. video_input_free(video->inputs.array + idx);
  372. da_erase(video->inputs, idx);
  373. if (video->inputs.num == 0) {
  374. os_atomic_set_bool(&video->raw_active, false);
  375. if (!os_atomic_load_long(&video->gpu_refs)) {
  376. log_skipped(video);
  377. }
  378. }
  379. }
  380. pthread_mutex_unlock(&video->input_mutex);
  381. return idx != DARRAY_INVALID;
  382. }
  383. bool video_output_active(const video_t *video)
  384. {
  385. if (!video)
  386. return false;
  387. return os_atomic_load_bool(&get_const_root(video)->raw_active);
  388. }
  389. const struct video_output_info *video_output_get_info(const video_t *video)
  390. {
  391. return video ? &video->info : NULL;
  392. }
  393. bool video_output_lock_frame(video_t *video, struct video_frame *frame, int count, uint64_t timestamp)
  394. {
  395. struct cached_frame_info *cfi;
  396. bool locked;
  397. if (!video)
  398. return false;
  399. video = get_root(video);
  400. pthread_mutex_lock(&video->data_mutex);
  401. if (video->available_frames == 0) {
  402. video->cache[video->last_added].count += count;
  403. video->cache[video->last_added].skipped += count;
  404. locked = false;
  405. } else {
  406. if (video->available_frames != video->info.cache_size) {
  407. if (++video->last_added == video->info.cache_size)
  408. video->last_added = 0;
  409. }
  410. cfi = &video->cache[video->last_added];
  411. cfi->frame.timestamp = timestamp;
  412. cfi->count = count;
  413. cfi->skipped = 0;
  414. memcpy(frame, &cfi->frame, sizeof(*frame));
  415. locked = true;
  416. }
  417. pthread_mutex_unlock(&video->data_mutex);
  418. return locked;
  419. }
  420. void video_output_unlock_frame(video_t *video)
  421. {
  422. if (!video)
  423. return;
  424. video = get_root(video);
  425. pthread_mutex_lock(&video->data_mutex);
  426. video->available_frames--;
  427. os_sem_post(video->update_semaphore);
  428. pthread_mutex_unlock(&video->data_mutex);
  429. }
  430. uint64_t video_output_get_frame_time(const video_t *video)
  431. {
  432. return video ? video->frame_time : 0;
  433. }
  434. void video_output_stop(video_t *video)
  435. {
  436. void *thread_ret;
  437. if (!video)
  438. return;
  439. video = get_root(video);
  440. if (!video->stop) {
  441. video->stop = true;
  442. os_sem_post(video->update_semaphore);
  443. pthread_join(video->thread, &thread_ret);
  444. }
  445. }
  446. bool video_output_stopped(video_t *video)
  447. {
  448. if (!video)
  449. return true;
  450. return get_root(video)->stop;
  451. }
  452. enum video_format video_output_get_format(const video_t *video)
  453. {
  454. return video ? get_const_root(video)->info.format : VIDEO_FORMAT_NONE;
  455. }
  456. uint32_t video_output_get_width(const video_t *video)
  457. {
  458. return video ? get_const_root(video)->info.width : 0;
  459. }
  460. uint32_t video_output_get_height(const video_t *video)
  461. {
  462. return video ? get_const_root(video)->info.height : 0;
  463. }
  464. double video_output_get_frame_rate(const video_t *video)
  465. {
  466. if (!video)
  467. return 0.0;
  468. video = get_const_root(video);
  469. return (double)video->info.fps_num / (double)video->info.fps_den;
  470. }
  471. uint32_t video_output_get_skipped_frames(const video_t *video)
  472. {
  473. return (uint32_t)os_atomic_load_long(&get_const_root(video)->skipped_frames);
  474. }
  475. uint32_t video_output_get_total_frames(const video_t *video)
  476. {
  477. return (uint32_t)os_atomic_load_long(&get_const_root(video)->total_frames);
  478. }
  479. /* Note: These four functions below are a very slight bit of a hack. If the
  480. * texture encoder thread is active while the raw encoder thread is active, the
  481. * total frame count will just be doubled while they're both active. Which is
  482. * fine. What's more important is having a relatively accurate skipped frame
  483. * count. */
  484. void video_output_inc_texture_encoders(video_t *video)
  485. {
  486. video = get_root(video);
  487. if (os_atomic_inc_long(&video->gpu_refs) == 1 && !os_atomic_load_bool(&video->raw_active)) {
  488. reset_frames(video);
  489. }
  490. }
  491. void video_output_dec_texture_encoders(video_t *video)
  492. {
  493. video = get_root(video);
  494. if (os_atomic_dec_long(&video->gpu_refs) == 0 && !os_atomic_load_bool(&video->raw_active)) {
  495. log_skipped(video);
  496. }
  497. }
  498. void video_output_inc_texture_frames(video_t *video)
  499. {
  500. os_atomic_inc_long(&get_root(video)->total_frames);
  501. }
  502. void video_output_inc_texture_skipped_frames(video_t *video)
  503. {
  504. os_atomic_inc_long(&get_root(video)->skipped_frames);
  505. }
  506. video_t *video_output_create_with_frame_rate_divisor(video_t *video, uint32_t divisor)
  507. {
  508. // `divisor == 1` would result in the same frame rate,
  509. // resulting in an unnecessary additional video output
  510. if (!video || divisor == 0 || divisor == 1)
  511. return NULL;
  512. video_t *new_video = bzalloc(sizeof(video_t));
  513. memcpy(new_video, video, sizeof(*new_video));
  514. new_video->parent = video;
  515. new_video->info.fps_den *= divisor;
  516. return new_video;
  517. }
  518. void video_output_free_frame_rate_divisor(video_t *video)
  519. {
  520. if (video && video->parent)
  521. bfree(video);
  522. }