video-io.c 17 KB

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