video-io.c 12 KB

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