video-io.c 14 KB

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