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. }
  132. video->total_frames++;
  133. profile_end(video_thread_name);
  134. profile_reenable_thread();
  135. }
  136. return NULL;
  137. }
  138. /* ------------------------------------------------------------------------- */
  139. static inline bool valid_video_params(const struct video_output_info *info)
  140. {
  141. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  142. info->fps_num != 0;
  143. }
  144. static inline void init_cache(struct video_output *video)
  145. {
  146. if (video->info.cache_size > MAX_CACHE_SIZE)
  147. video->info.cache_size = MAX_CACHE_SIZE;
  148. for (size_t i = 0; i < video->info.cache_size; i++) {
  149. struct video_frame *frame;
  150. frame = (struct video_frame*)&video->cache[i];
  151. video_frame_init(frame, video->info.format,
  152. video->info.width, video->info.height);
  153. }
  154. video->available_frames = video->info.cache_size;
  155. }
  156. int video_output_open(video_t **video, struct video_output_info *info)
  157. {
  158. struct video_output *out;
  159. pthread_mutexattr_t attr;
  160. if (!valid_video_params(info))
  161. return VIDEO_OUTPUT_INVALIDPARAM;
  162. out = bzalloc(sizeof(struct video_output));
  163. if (!out)
  164. goto fail;
  165. memcpy(&out->info, info, sizeof(struct video_output_info));
  166. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  167. (double)info->fps_num);
  168. out->initialized = false;
  169. if (pthread_mutexattr_init(&attr) != 0)
  170. goto fail;
  171. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  172. goto fail;
  173. if (pthread_mutex_init(&out->data_mutex, &attr) != 0)
  174. goto fail;
  175. if (pthread_mutex_init(&out->input_mutex, &attr) != 0)
  176. goto fail;
  177. if (os_sem_init(&out->update_semaphore, 0) != 0)
  178. goto fail;
  179. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  180. goto fail;
  181. init_cache(out);
  182. out->initialized = true;
  183. *video = out;
  184. return VIDEO_OUTPUT_SUCCESS;
  185. fail:
  186. video_output_close(out);
  187. return VIDEO_OUTPUT_FAIL;
  188. }
  189. void video_output_close(video_t *video)
  190. {
  191. if (!video)
  192. return;
  193. video_output_stop(video);
  194. for (size_t i = 0; i < video->inputs.num; i++)
  195. video_input_free(&video->inputs.array[i]);
  196. da_free(video->inputs);
  197. for (size_t i = 0; i < video->info.cache_size; i++)
  198. video_frame_free((struct video_frame*)&video->cache[i]);
  199. os_sem_destroy(video->update_semaphore);
  200. pthread_mutex_destroy(&video->data_mutex);
  201. pthread_mutex_destroy(&video->input_mutex);
  202. bfree(video);
  203. }
  204. static size_t video_get_input_idx(const video_t *video,
  205. void (*callback)(void *param, struct video_data *frame),
  206. void *param)
  207. {
  208. for (size_t i = 0; i < video->inputs.num; i++) {
  209. struct video_input *input = video->inputs.array+i;
  210. if (input->callback == callback && input->param == param)
  211. return i;
  212. }
  213. return DARRAY_INVALID;
  214. }
  215. static inline bool video_input_init(struct video_input *input,
  216. struct video_output *video)
  217. {
  218. if (input->conversion.width != video->info.width ||
  219. input->conversion.height != video->info.height ||
  220. input->conversion.format != video->info.format) {
  221. struct video_scale_info from = {
  222. .format = video->info.format,
  223. .width = video->info.width,
  224. .height = video->info.height,
  225. };
  226. int ret = video_scaler_create(&input->scaler,
  227. &input->conversion, &from,
  228. VIDEO_SCALE_FAST_BILINEAR);
  229. if (ret != VIDEO_SCALER_SUCCESS) {
  230. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  231. blog(LOG_ERROR, "video_input_init: Bad "
  232. "scale conversion type");
  233. else
  234. blog(LOG_ERROR, "video_input_init: Failed to "
  235. "create scaler");
  236. return false;
  237. }
  238. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  239. video_frame_init(&input->frame[i],
  240. input->conversion.format,
  241. input->conversion.width,
  242. input->conversion.height);
  243. }
  244. return true;
  245. }
  246. bool video_output_connect(video_t *video,
  247. const struct video_scale_info *conversion,
  248. void (*callback)(void *param, struct video_data *frame),
  249. void *param)
  250. {
  251. bool success = false;
  252. if (!video || !callback)
  253. return false;
  254. pthread_mutex_lock(&video->input_mutex);
  255. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  256. struct video_input input;
  257. memset(&input, 0, sizeof(input));
  258. input.callback = callback;
  259. input.param = param;
  260. if (conversion) {
  261. input.conversion = *conversion;
  262. } else {
  263. input.conversion.format = video->info.format;
  264. input.conversion.width = video->info.width;
  265. input.conversion.height = video->info.height;
  266. }
  267. if (input.conversion.width == 0)
  268. input.conversion.width = video->info.width;
  269. if (input.conversion.height == 0)
  270. input.conversion.height = video->info.height;
  271. success = video_input_init(&input, video);
  272. if (success)
  273. da_push_back(video->inputs, &input);
  274. }
  275. pthread_mutex_unlock(&video->input_mutex);
  276. return success;
  277. }
  278. void video_output_disconnect(video_t *video,
  279. void (*callback)(void *param, struct video_data *frame),
  280. void *param)
  281. {
  282. if (!video || !callback)
  283. return;
  284. pthread_mutex_lock(&video->input_mutex);
  285. size_t idx = video_get_input_idx(video, callback, param);
  286. if (idx != DARRAY_INVALID) {
  287. video_input_free(video->inputs.array+idx);
  288. da_erase(video->inputs, idx);
  289. }
  290. pthread_mutex_unlock(&video->input_mutex);
  291. }
  292. bool video_output_active(const video_t *video)
  293. {
  294. if (!video) return false;
  295. return video->inputs.num != 0;
  296. }
  297. const struct video_output_info *video_output_get_info(const video_t *video)
  298. {
  299. return video ? &video->info : NULL;
  300. }
  301. bool video_output_lock_frame(video_t *video, struct video_frame *frame,
  302. int count, uint64_t timestamp)
  303. {
  304. struct cached_frame_info *cfi;
  305. bool locked;
  306. if (!video) return false;
  307. pthread_mutex_lock(&video->data_mutex);
  308. if (video->available_frames == 0) {
  309. video->skipped_frames += count;
  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. }