video-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. .range = video->info.range,
  233. .colorspace = video->info.colorspace
  234. };
  235. int ret = video_scaler_create(&input->scaler,
  236. &input->conversion, &from,
  237. VIDEO_SCALE_FAST_BILINEAR);
  238. if (ret != VIDEO_SCALER_SUCCESS) {
  239. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  240. blog(LOG_ERROR, "video_input_init: Bad "
  241. "scale conversion type");
  242. else
  243. blog(LOG_ERROR, "video_input_init: Failed to "
  244. "create scaler");
  245. return false;
  246. }
  247. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  248. video_frame_init(&input->frame[i],
  249. input->conversion.format,
  250. input->conversion.width,
  251. input->conversion.height);
  252. }
  253. return true;
  254. }
  255. bool video_output_connect(video_t *video,
  256. const struct video_scale_info *conversion,
  257. void (*callback)(void *param, struct video_data *frame),
  258. void *param)
  259. {
  260. bool success = false;
  261. if (!video || !callback)
  262. return false;
  263. pthread_mutex_lock(&video->input_mutex);
  264. if (video->inputs.num == 0) {
  265. video->skipped_frames = 0;
  266. video->total_frames = 0;
  267. }
  268. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  269. struct video_input input;
  270. memset(&input, 0, sizeof(input));
  271. input.callback = callback;
  272. input.param = param;
  273. if (conversion) {
  274. input.conversion = *conversion;
  275. } else {
  276. input.conversion.format = video->info.format;
  277. input.conversion.width = video->info.width;
  278. input.conversion.height = video->info.height;
  279. }
  280. if (input.conversion.width == 0)
  281. input.conversion.width = video->info.width;
  282. if (input.conversion.height == 0)
  283. input.conversion.height = video->info.height;
  284. success = video_input_init(&input, video);
  285. if (success)
  286. da_push_back(video->inputs, &input);
  287. }
  288. pthread_mutex_unlock(&video->input_mutex);
  289. return success;
  290. }
  291. void video_output_disconnect(video_t *video,
  292. void (*callback)(void *param, struct video_data *frame),
  293. void *param)
  294. {
  295. if (!video || !callback)
  296. return;
  297. pthread_mutex_lock(&video->input_mutex);
  298. size_t idx = video_get_input_idx(video, callback, param);
  299. if (idx != DARRAY_INVALID) {
  300. video_input_free(video->inputs.array+idx);
  301. da_erase(video->inputs, idx);
  302. }
  303. if (video->inputs.num == 0) {
  304. double percentage_skipped = (double)video->skipped_frames /
  305. (double)video->total_frames * 100.0;
  306. if (video->skipped_frames)
  307. blog(LOG_INFO, "Video stopped, number of "
  308. "skipped frames due "
  309. "to encoding lag: "
  310. "%"PRIu32"/%"PRIu32" (%0.1f%%)",
  311. video->skipped_frames,
  312. video->total_frames,
  313. percentage_skipped);
  314. }
  315. pthread_mutex_unlock(&video->input_mutex);
  316. }
  317. bool video_output_active(const video_t *video)
  318. {
  319. if (!video) return false;
  320. return video->inputs.num != 0;
  321. }
  322. const struct video_output_info *video_output_get_info(const video_t *video)
  323. {
  324. return video ? &video->info : NULL;
  325. }
  326. bool video_output_lock_frame(video_t *video, struct video_frame *frame,
  327. int count, uint64_t timestamp)
  328. {
  329. struct cached_frame_info *cfi;
  330. bool locked;
  331. if (!video) return false;
  332. pthread_mutex_lock(&video->data_mutex);
  333. if (video->available_frames == 0) {
  334. video->cache[video->last_added].count += count;
  335. video->cache[video->last_added].skipped += count;
  336. locked = false;
  337. } else {
  338. if (video->available_frames != video->info.cache_size) {
  339. if (++video->last_added == video->info.cache_size)
  340. video->last_added = 0;
  341. }
  342. cfi = &video->cache[video->last_added];
  343. cfi->frame.timestamp = timestamp;
  344. cfi->count = count;
  345. cfi->skipped = 0;
  346. memcpy(frame, &cfi->frame, sizeof(*frame));
  347. locked = true;
  348. }
  349. pthread_mutex_unlock(&video->data_mutex);
  350. return locked;
  351. }
  352. void video_output_unlock_frame(video_t *video)
  353. {
  354. if (!video) return;
  355. pthread_mutex_lock(&video->data_mutex);
  356. video->available_frames--;
  357. os_sem_post(video->update_semaphore);
  358. pthread_mutex_unlock(&video->data_mutex);
  359. }
  360. uint64_t video_output_get_frame_time(const video_t *video)
  361. {
  362. return video ? video->frame_time : 0;
  363. }
  364. void video_output_stop(video_t *video)
  365. {
  366. void *thread_ret;
  367. if (!video)
  368. return;
  369. if (video->initialized) {
  370. video->initialized = false;
  371. video->stop = true;
  372. os_sem_post(video->update_semaphore);
  373. pthread_join(video->thread, &thread_ret);
  374. }
  375. }
  376. bool video_output_stopped(video_t *video)
  377. {
  378. if (!video)
  379. return true;
  380. return video->stop;
  381. }
  382. enum video_format video_output_get_format(const video_t *video)
  383. {
  384. return video ? video->info.format : VIDEO_FORMAT_NONE;
  385. }
  386. uint32_t video_output_get_width(const video_t *video)
  387. {
  388. return video ? video->info.width : 0;
  389. }
  390. uint32_t video_output_get_height(const video_t *video)
  391. {
  392. return video ? video->info.height : 0;
  393. }
  394. double video_output_get_frame_rate(const video_t *video)
  395. {
  396. if (!video)
  397. return 0.0;
  398. return (double)video->info.fps_num / (double)video->info.fps_den;
  399. }
  400. uint32_t video_output_get_skipped_frames(const video_t *video)
  401. {
  402. return video->skipped_frames;
  403. }
  404. uint32_t video_output_get_total_frames(const video_t *video)
  405. {
  406. return video->total_frames;
  407. }