video-io.c 15 KB

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