video-io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/threading.h"
  18. #include "../util/darray.h"
  19. #include "format-conversion.h"
  20. #include "video-io.h"
  21. #include "video-frame.h"
  22. #include "video-scaler.h"
  23. #define MAX_CONVERT_BUFFERS 3
  24. struct video_input {
  25. struct video_scale_info conversion;
  26. video_scaler_t scaler;
  27. struct video_frame frame[MAX_CONVERT_BUFFERS];
  28. int cur_frame;
  29. void (*callback)(void *param, struct video_data *frame);
  30. void *param;
  31. };
  32. static inline void video_input_free(struct video_input *input)
  33. {
  34. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  35. video_frame_free(&input->frame[i]);
  36. video_scaler_destroy(input->scaler);
  37. }
  38. struct video_output {
  39. struct video_output_info info;
  40. pthread_t thread;
  41. pthread_mutex_t data_mutex;
  42. os_event_t stop_event;
  43. struct video_data cur_frame;
  44. struct video_data next_frame;
  45. bool new_frame;
  46. os_event_t update_event;
  47. uint64_t frame_time;
  48. volatile uint64_t cur_video_time;
  49. uint32_t skipped_frames;
  50. uint32_t total_frames;
  51. bool initialized;
  52. pthread_mutex_t input_mutex;
  53. DARRAY(struct video_input) inputs;
  54. };
  55. /* ------------------------------------------------------------------------- */
  56. static inline void video_swapframes(struct video_output *video)
  57. {
  58. if (video->new_frame) {
  59. video->cur_frame = video->next_frame;
  60. video->new_frame = false;
  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. }
  82. }
  83. return success;
  84. }
  85. static inline void video_output_cur_frame(struct video_output *video)
  86. {
  87. if (!video->cur_frame.data[0])
  88. return;
  89. pthread_mutex_lock(&video->input_mutex);
  90. for (size_t i = 0; i < video->inputs.num; i++) {
  91. struct video_input *input = video->inputs.array+i;
  92. if (scale_video_output(input, &video->cur_frame))
  93. input->callback(input->param, &video->cur_frame);
  94. }
  95. pthread_mutex_unlock(&video->input_mutex);
  96. }
  97. #define MAX_MISSED_TIMINGS 8
  98. static inline bool safe_sleepto(uint64_t t, uint32_t *missed_timings)
  99. {
  100. if (!os_sleepto_ns(t))
  101. (*missed_timings)++;
  102. else
  103. *missed_timings = 0;
  104. return *missed_timings <= MAX_MISSED_TIMINGS;
  105. }
  106. static void *video_thread(void *param)
  107. {
  108. struct video_output *video = param;
  109. uint64_t cur_time = os_gettime_ns();
  110. uint32_t missed_timings = 0;
  111. while (os_event_try(video->stop_event) == EAGAIN) {
  112. /* wait half a frame, update frame */
  113. cur_time += (video->frame_time/2);
  114. if (safe_sleepto(cur_time, &missed_timings)) {
  115. video->cur_video_time = cur_time;
  116. os_event_signal(video->update_event);
  117. } else {
  118. video->skipped_frames++;
  119. }
  120. /* wait another half a frame, swap and output frames */
  121. cur_time += (video->frame_time/2);
  122. safe_sleepto(cur_time, &missed_timings);
  123. pthread_mutex_lock(&video->data_mutex);
  124. video_swapframes(video);
  125. video_output_cur_frame(video);
  126. pthread_mutex_unlock(&video->data_mutex);
  127. video->total_frames++;
  128. }
  129. return NULL;
  130. }
  131. /* ------------------------------------------------------------------------- */
  132. static inline bool valid_video_params(struct video_output_info *info)
  133. {
  134. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  135. info->fps_num != 0;
  136. }
  137. int video_output_open(video_t *video, struct video_output_info *info)
  138. {
  139. struct video_output *out;
  140. if (!valid_video_params(info))
  141. return VIDEO_OUTPUT_INVALIDPARAM;
  142. out = bzalloc(sizeof(struct video_output));
  143. if (!out)
  144. goto fail;
  145. memcpy(&out->info, info, sizeof(struct video_output_info));
  146. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  147. (double)info->fps_num);
  148. out->initialized = false;
  149. if (pthread_mutex_init(&out->data_mutex, NULL) != 0)
  150. goto fail;
  151. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  152. goto fail;
  153. if (os_event_init(&out->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  154. goto fail;
  155. if (os_event_init(&out->update_event, OS_EVENT_TYPE_AUTO) != 0)
  156. goto fail;
  157. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  158. goto fail;
  159. out->initialized = true;
  160. *video = out;
  161. return VIDEO_OUTPUT_SUCCESS;
  162. fail:
  163. video_output_close(out);
  164. return VIDEO_OUTPUT_FAIL;
  165. }
  166. void video_output_close(video_t video)
  167. {
  168. if (!video)
  169. return;
  170. video_output_stop(video);
  171. for (size_t i = 0; i < video->inputs.num; i++)
  172. video_input_free(&video->inputs.array[i]);
  173. da_free(video->inputs);
  174. os_event_destroy(video->update_event);
  175. os_event_destroy(video->stop_event);
  176. pthread_mutex_destroy(&video->data_mutex);
  177. pthread_mutex_destroy(&video->input_mutex);
  178. bfree(video);
  179. }
  180. static size_t video_get_input_idx(video_t video,
  181. void (*callback)(void *param, struct video_data *frame),
  182. void *param)
  183. {
  184. for (size_t i = 0; i < video->inputs.num; i++) {
  185. struct video_input *input = video->inputs.array+i;
  186. if (input->callback == callback && input->param == param)
  187. return i;
  188. }
  189. return DARRAY_INVALID;
  190. }
  191. static inline bool video_input_init(struct video_input *input,
  192. struct video_output *video)
  193. {
  194. if (input->conversion.width != video->info.width ||
  195. input->conversion.height != video->info.height ||
  196. input->conversion.format != video->info.format) {
  197. struct video_scale_info from = {
  198. .format = video->info.format,
  199. .width = video->info.width,
  200. .height = video->info.height,
  201. };
  202. int ret = video_scaler_create(&input->scaler,
  203. &input->conversion, &from,
  204. VIDEO_SCALE_FAST_BILINEAR);
  205. if (ret != VIDEO_SCALER_SUCCESS) {
  206. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  207. blog(LOG_ERROR, "video_input_init: Bad "
  208. "scale conversion type");
  209. else
  210. blog(LOG_ERROR, "video_input_init: Failed to "
  211. "create scaler");
  212. return false;
  213. }
  214. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  215. video_frame_init(&input->frame[i],
  216. video->info.format,
  217. video->info.width,
  218. video->info.height);
  219. }
  220. return true;
  221. }
  222. bool video_output_connect(video_t video,
  223. const struct video_scale_info *conversion,
  224. void (*callback)(void *param, struct video_data *frame),
  225. void *param)
  226. {
  227. bool success = false;
  228. if (!video || !callback)
  229. return false;
  230. pthread_mutex_lock(&video->input_mutex);
  231. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  232. struct video_input input;
  233. memset(&input, 0, sizeof(input));
  234. input.callback = callback;
  235. input.param = param;
  236. if (conversion) {
  237. input.conversion = *conversion;
  238. } else {
  239. input.conversion.format = video->info.format;
  240. input.conversion.width = video->info.width;
  241. input.conversion.height = video->info.height;
  242. }
  243. if (input.conversion.width == 0)
  244. input.conversion.width = video->info.width;
  245. if (input.conversion.height == 0)
  246. input.conversion.height = video->info.height;
  247. success = video_input_init(&input, video);
  248. if (success)
  249. da_push_back(video->inputs, &input);
  250. }
  251. pthread_mutex_unlock(&video->input_mutex);
  252. return success;
  253. }
  254. void video_output_disconnect(video_t video,
  255. void (*callback)(void *param, struct video_data *frame),
  256. void *param)
  257. {
  258. if (!video || !callback)
  259. return;
  260. pthread_mutex_lock(&video->input_mutex);
  261. size_t idx = video_get_input_idx(video, callback, param);
  262. if (idx != DARRAY_INVALID) {
  263. video_input_free(video->inputs.array+idx);
  264. da_erase(video->inputs, idx);
  265. }
  266. pthread_mutex_unlock(&video->input_mutex);
  267. }
  268. bool video_output_active(video_t video)
  269. {
  270. if (!video) return false;
  271. return video->inputs.num != 0;
  272. }
  273. const struct video_output_info *video_output_getinfo(video_t video)
  274. {
  275. return video ? &video->info : NULL;
  276. }
  277. void video_output_swap_frame(video_t video, struct video_data *frame)
  278. {
  279. if (!video) return;
  280. pthread_mutex_lock(&video->data_mutex);
  281. video->next_frame = *frame;
  282. video->new_frame = true;
  283. pthread_mutex_unlock(&video->data_mutex);
  284. }
  285. bool video_output_wait(video_t video)
  286. {
  287. if (!video) return false;
  288. os_event_wait(video->update_event);
  289. return os_event_try(video->stop_event) == EAGAIN;
  290. }
  291. uint64_t video_getframetime(video_t video)
  292. {
  293. return video ? video->frame_time : 0;
  294. }
  295. uint64_t video_gettime(video_t video)
  296. {
  297. return video ? video->cur_video_time : 0;
  298. }
  299. void video_output_stop(video_t video)
  300. {
  301. void *thread_ret;
  302. if (!video)
  303. return;
  304. if (video->initialized) {
  305. video->initialized = false;
  306. os_event_signal(video->stop_event);
  307. pthread_join(video->thread, &thread_ret);
  308. os_event_signal(video->update_event);
  309. }
  310. }
  311. uint32_t video_output_width(video_t video)
  312. {
  313. return video ? video->info.width : 0;
  314. }
  315. uint32_t video_output_height(video_t video)
  316. {
  317. return video ? video->info.height : 0;
  318. }
  319. double video_output_framerate(video_t video)
  320. {
  321. if (!video)
  322. return 0.0;
  323. return (double)video->info.fps_num / (double)video->info.fps_den;
  324. }
  325. uint32_t video_output_num_skipped_frames(video_t video)
  326. {
  327. return video->skipped_frames;
  328. }
  329. uint32_t video_output_total_frames(video_t video)
  330. {
  331. return video->total_frames;
  332. }