video-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. uint32_t skipped_frames;
  55. uint32_t 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. };
  64. /* ------------------------------------------------------------------------- */
  65. static inline bool scale_video_output(struct video_input *input,
  66. struct video_data *data)
  67. {
  68. bool success = true;
  69. if (input->scaler) {
  70. struct video_frame *frame;
  71. if (++input->cur_frame == MAX_CONVERT_BUFFERS)
  72. input->cur_frame = 0;
  73. frame = &input->frame[input->cur_frame];
  74. success = video_scaler_scale(input->scaler,
  75. frame->data, frame->linesize,
  76. (const uint8_t * const*)data->data,
  77. data->linesize);
  78. if (success) {
  79. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  80. data->data[i] = frame->data[i];
  81. data->linesize[i] = frame->linesize[i];
  82. }
  83. } else {
  84. blog(LOG_WARNING, "video-io: Could not scale frame!");
  85. }
  86. }
  87. return success;
  88. }
  89. static inline bool video_output_cur_frame(struct video_output *video)
  90. {
  91. struct cached_frame_info *frame_info;
  92. bool complete;
  93. bool skipped;
  94. /* -------------------------------- */
  95. pthread_mutex_lock(&video->data_mutex);
  96. frame_info = &video->cache[video->first_added];
  97. pthread_mutex_unlock(&video->data_mutex);
  98. /* -------------------------------- */
  99. pthread_mutex_lock(&video->input_mutex);
  100. for (size_t i = 0; i < video->inputs.num; i++) {
  101. struct video_input *input = video->inputs.array+i;
  102. struct video_data frame = frame_info->frame;
  103. if (scale_video_output(input, &frame))
  104. input->callback(input->param, &frame);
  105. }
  106. pthread_mutex_unlock(&video->input_mutex);
  107. /* -------------------------------- */
  108. pthread_mutex_lock(&video->data_mutex);
  109. frame_info->frame.timestamp += video->frame_time;
  110. complete = --frame_info->count == 0;
  111. skipped = frame_info->skipped > 0;
  112. if (complete) {
  113. if (++video->first_added == video->info.cache_size)
  114. video->first_added = 0;
  115. if (++video->available_frames == video->info.cache_size)
  116. video->last_added = video->first_added;
  117. } else if (skipped) {
  118. --frame_info->skipped;
  119. ++video->skipped_frames;
  120. }
  121. pthread_mutex_unlock(&video->data_mutex);
  122. /* -------------------------------- */
  123. return complete;
  124. }
  125. static void *video_thread(void *param)
  126. {
  127. struct video_output *video = param;
  128. os_set_thread_name("video-io: video thread");
  129. const char *video_thread_name =
  130. profile_store_name(obs_get_profiler_name_store(),
  131. "video_thread(%s)", video->info.name);
  132. while (os_sem_wait(video->update_semaphore) == 0) {
  133. if (video->stop)
  134. break;
  135. profile_start(video_thread_name);
  136. while (!video->stop && !video_output_cur_frame(video)) {
  137. video->total_frames++;
  138. }
  139. video->total_frames++;
  140. profile_end(video_thread_name);
  141. profile_reenable_thread();
  142. }
  143. return NULL;
  144. }
  145. /* ------------------------------------------------------------------------- */
  146. static inline bool valid_video_params(const struct video_output_info *info)
  147. {
  148. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  149. info->fps_num != 0;
  150. }
  151. static inline void init_cache(struct video_output *video)
  152. {
  153. if (video->info.cache_size > MAX_CACHE_SIZE)
  154. video->info.cache_size = MAX_CACHE_SIZE;
  155. for (size_t i = 0; i < video->info.cache_size; i++) {
  156. struct video_frame *frame;
  157. frame = (struct video_frame*)&video->cache[i];
  158. video_frame_init(frame, video->info.format,
  159. video->info.width, video->info.height);
  160. }
  161. video->available_frames = video->info.cache_size;
  162. }
  163. int video_output_open(video_t **video, struct video_output_info *info)
  164. {
  165. struct video_output *out;
  166. pthread_mutexattr_t attr;
  167. if (!valid_video_params(info))
  168. return VIDEO_OUTPUT_INVALIDPARAM;
  169. out = bzalloc(sizeof(struct video_output));
  170. if (!out)
  171. goto fail;
  172. memcpy(&out->info, info, sizeof(struct video_output_info));
  173. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  174. (double)info->fps_num);
  175. out->initialized = false;
  176. if (pthread_mutexattr_init(&attr) != 0)
  177. goto fail;
  178. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  179. goto fail;
  180. if (pthread_mutex_init(&out->data_mutex, &attr) != 0)
  181. goto fail;
  182. if (pthread_mutex_init(&out->input_mutex, &attr) != 0)
  183. goto fail;
  184. if (os_sem_init(&out->update_semaphore, 0) != 0)
  185. goto fail;
  186. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  187. goto fail;
  188. init_cache(out);
  189. out->initialized = true;
  190. *video = out;
  191. return VIDEO_OUTPUT_SUCCESS;
  192. fail:
  193. video_output_close(out);
  194. return VIDEO_OUTPUT_FAIL;
  195. }
  196. void video_output_close(video_t *video)
  197. {
  198. if (!video)
  199. return;
  200. video_output_stop(video);
  201. for (size_t i = 0; i < video->inputs.num; i++)
  202. video_input_free(&video->inputs.array[i]);
  203. da_free(video->inputs);
  204. for (size_t i = 0; i < video->info.cache_size; i++)
  205. video_frame_free((struct video_frame*)&video->cache[i]);
  206. os_sem_destroy(video->update_semaphore);
  207. pthread_mutex_destroy(&video->data_mutex);
  208. pthread_mutex_destroy(&video->input_mutex);
  209. bfree(video);
  210. }
  211. static size_t video_get_input_idx(const video_t *video,
  212. void (*callback)(void *param, struct video_data *frame),
  213. void *param)
  214. {
  215. for (size_t i = 0; i < video->inputs.num; i++) {
  216. struct video_input *input = video->inputs.array+i;
  217. if (input->callback == callback && input->param == param)
  218. return i;
  219. }
  220. return DARRAY_INVALID;
  221. }
  222. static inline bool video_input_init(struct video_input *input,
  223. struct video_output *video)
  224. {
  225. if (input->conversion.width != video->info.width ||
  226. input->conversion.height != video->info.height ||
  227. input->conversion.format != video->info.format) {
  228. struct video_scale_info from = {
  229. .format = video->info.format,
  230. .width = video->info.width,
  231. .height = video->info.height,
  232. };
  233. int ret = video_scaler_create(&input->scaler,
  234. &input->conversion, &from,
  235. VIDEO_SCALE_FAST_BILINEAR);
  236. if (ret != VIDEO_SCALER_SUCCESS) {
  237. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  238. blog(LOG_ERROR, "video_input_init: Bad "
  239. "scale conversion type");
  240. else
  241. blog(LOG_ERROR, "video_input_init: Failed to "
  242. "create scaler");
  243. return false;
  244. }
  245. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  246. video_frame_init(&input->frame[i],
  247. input->conversion.format,
  248. input->conversion.width,
  249. input->conversion.height);
  250. }
  251. return true;
  252. }
  253. bool video_output_connect(video_t *video,
  254. const struct video_scale_info *conversion,
  255. void (*callback)(void *param, struct video_data *frame),
  256. void *param)
  257. {
  258. bool success = false;
  259. if (!video || !callback)
  260. return false;
  261. pthread_mutex_lock(&video->input_mutex);
  262. if (video->inputs.num == 0) {
  263. video->skipped_frames = 0;
  264. video->total_frames = 0;
  265. }
  266. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  267. struct video_input input;
  268. memset(&input, 0, sizeof(input));
  269. input.callback = callback;
  270. input.param = param;
  271. if (conversion) {
  272. input.conversion = *conversion;
  273. } else {
  274. input.conversion.format = video->info.format;
  275. input.conversion.width = video->info.width;
  276. input.conversion.height = video->info.height;
  277. }
  278. if (input.conversion.width == 0)
  279. input.conversion.width = video->info.width;
  280. if (input.conversion.height == 0)
  281. input.conversion.height = video->info.height;
  282. success = video_input_init(&input, video);
  283. if (success)
  284. da_push_back(video->inputs, &input);
  285. }
  286. pthread_mutex_unlock(&video->input_mutex);
  287. return success;
  288. }
  289. void video_output_disconnect(video_t *video,
  290. void (*callback)(void *param, struct video_data *frame),
  291. void *param)
  292. {
  293. if (!video || !callback)
  294. return;
  295. pthread_mutex_lock(&video->input_mutex);
  296. size_t idx = video_get_input_idx(video, callback, param);
  297. if (idx != DARRAY_INVALID) {
  298. video_input_free(video->inputs.array+idx);
  299. da_erase(video->inputs, idx);
  300. }
  301. if (video->inputs.num == 0) {
  302. double percentage_skipped = (double)video->skipped_frames /
  303. (double)video->total_frames * 100.0;
  304. if (video->skipped_frames)
  305. blog(LOG_INFO, "Video stopped, number of "
  306. "skipped frames due "
  307. "to encoding lag: "
  308. "%"PRIu32"/%"PRIu32" (%0.1f%%)",
  309. video->skipped_frames,
  310. video->total_frames,
  311. percentage_skipped);
  312. }
  313. pthread_mutex_unlock(&video->input_mutex);
  314. }
  315. bool video_output_active(const video_t *video)
  316. {
  317. if (!video) return false;
  318. return video->inputs.num != 0;
  319. }
  320. const struct video_output_info *video_output_get_info(const video_t *video)
  321. {
  322. return video ? &video->info : NULL;
  323. }
  324. bool video_output_lock_frame(video_t *video, struct video_frame *frame,
  325. int count, uint64_t timestamp)
  326. {
  327. struct cached_frame_info *cfi;
  328. bool locked;
  329. if (!video) return false;
  330. pthread_mutex_lock(&video->data_mutex);
  331. if (video->available_frames == 0) {
  332. video->cache[video->last_added].count += count;
  333. video->cache[video->last_added].skipped += count;
  334. locked = false;
  335. } else {
  336. if (video->available_frames != video->info.cache_size) {
  337. if (++video->last_added == video->info.cache_size)
  338. video->last_added = 0;
  339. }
  340. cfi = &video->cache[video->last_added];
  341. cfi->frame.timestamp = timestamp;
  342. cfi->count = count;
  343. cfi->skipped = 0;
  344. memcpy(frame, &cfi->frame, sizeof(*frame));
  345. locked = true;
  346. }
  347. pthread_mutex_unlock(&video->data_mutex);
  348. return locked;
  349. }
  350. void video_output_unlock_frame(video_t *video)
  351. {
  352. if (!video) return;
  353. pthread_mutex_lock(&video->data_mutex);
  354. video->available_frames--;
  355. os_sem_post(video->update_semaphore);
  356. pthread_mutex_unlock(&video->data_mutex);
  357. }
  358. uint64_t video_output_get_frame_time(const video_t *video)
  359. {
  360. return video ? video->frame_time : 0;
  361. }
  362. void video_output_stop(video_t *video)
  363. {
  364. void *thread_ret;
  365. if (!video)
  366. return;
  367. if (video->initialized) {
  368. video->initialized = false;
  369. video->stop = true;
  370. os_sem_post(video->update_semaphore);
  371. pthread_join(video->thread, &thread_ret);
  372. }
  373. }
  374. bool video_output_stopped(video_t *video)
  375. {
  376. if (!video)
  377. return true;
  378. return video->stop;
  379. }
  380. enum video_format video_output_get_format(const video_t *video)
  381. {
  382. return video ? video->info.format : VIDEO_FORMAT_NONE;
  383. }
  384. uint32_t video_output_get_width(const video_t *video)
  385. {
  386. return video ? video->info.width : 0;
  387. }
  388. uint32_t video_output_get_height(const video_t *video)
  389. {
  390. return video ? video->info.height : 0;
  391. }
  392. double video_output_get_frame_rate(const video_t *video)
  393. {
  394. if (!video)
  395. return 0.0;
  396. return (double)video->info.fps_num / (double)video->info.fps_den;
  397. }
  398. uint32_t video_output_get_skipped_frames(const video_t *video)
  399. {
  400. return video->skipped_frames;
  401. }
  402. uint32_t video_output_get_total_frames(const video_t *video)
  403. {
  404. return video->total_frames;
  405. }