video-io.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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, const 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. event_t stop_event;
  43. struct video_data cur_frame;
  44. struct video_data next_frame;
  45. bool new_frame;
  46. event_t update_event;
  47. uint64_t frame_time;
  48. volatile uint64_t cur_video_time;
  49. bool initialized;
  50. pthread_mutex_t input_mutex;
  51. DARRAY(struct video_input) inputs;
  52. };
  53. /* ------------------------------------------------------------------------- */
  54. static inline void video_swapframes(struct video_output *video)
  55. {
  56. if (video->new_frame) {
  57. video->cur_frame = video->next_frame;
  58. video->new_frame = false;
  59. }
  60. }
  61. static inline bool scale_video_output(struct video_input *input,
  62. struct video_data *data)
  63. {
  64. bool success = true;
  65. if (input->scaler) {
  66. struct video_frame *frame;
  67. if (++input->cur_frame == MAX_CONVERT_BUFFERS)
  68. input->cur_frame = 0;
  69. frame = &input->frame[input->cur_frame];
  70. success = video_scaler_scale(input->scaler,
  71. frame->data, frame->linesize,
  72. data->data, data->linesize);
  73. if (success) {
  74. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  75. data->data[i] = frame->data[i];
  76. data->linesize[i] = frame->linesize[i];
  77. }
  78. }
  79. }
  80. return success;
  81. }
  82. static inline void video_output_cur_frame(struct video_output *video)
  83. {
  84. if (!video->cur_frame.data[0])
  85. return;
  86. pthread_mutex_lock(&video->input_mutex);
  87. for (size_t i = 0; i < video->inputs.num; i++) {
  88. struct video_input *input = video->inputs.array+i;
  89. if (scale_video_output(input, &video->cur_frame))
  90. input->callback(input->param, &video->cur_frame);
  91. }
  92. pthread_mutex_unlock(&video->input_mutex);
  93. }
  94. static void *video_thread(void *param)
  95. {
  96. struct video_output *video = param;
  97. uint64_t cur_time = os_gettime_ns();
  98. while (event_try(video->stop_event) == EAGAIN) {
  99. /* wait half a frame, update frame */
  100. cur_time += (video->frame_time/2);
  101. os_sleepto_ns(cur_time);
  102. video->cur_video_time = cur_time;
  103. event_signal(video->update_event);
  104. /* wait another half a frame, swap and output frames */
  105. cur_time += (video->frame_time/2);
  106. os_sleepto_ns(cur_time);
  107. pthread_mutex_lock(&video->data_mutex);
  108. video_swapframes(video);
  109. video_output_cur_frame(video);
  110. pthread_mutex_unlock(&video->data_mutex);
  111. }
  112. return NULL;
  113. }
  114. /* ------------------------------------------------------------------------- */
  115. static inline bool valid_video_params(struct video_output_info *info)
  116. {
  117. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  118. info->fps_num != 0;
  119. }
  120. int video_output_open(video_t *video, struct video_output_info *info)
  121. {
  122. struct video_output *out;
  123. if (!valid_video_params(info))
  124. return VIDEO_OUTPUT_INVALIDPARAM;
  125. out = bzalloc(sizeof(struct video_output));
  126. memcpy(&out->info, info, sizeof(struct video_output_info));
  127. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  128. (double)info->fps_num);
  129. out->initialized = false;
  130. if (pthread_mutex_init(&out->data_mutex, NULL) != 0)
  131. goto fail;
  132. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  133. goto fail;
  134. if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
  135. goto fail;
  136. if (event_init(&out->update_event, EVENT_TYPE_AUTO) != 0)
  137. goto fail;
  138. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  139. goto fail;
  140. out->initialized = true;
  141. *video = out;
  142. return VIDEO_OUTPUT_SUCCESS;
  143. fail:
  144. video_output_close(out);
  145. return VIDEO_OUTPUT_FAIL;
  146. }
  147. void video_output_close(video_t video)
  148. {
  149. if (!video)
  150. return;
  151. video_output_stop(video);
  152. for (size_t i = 0; i < video->inputs.num; i++)
  153. video_input_free(&video->inputs.array[i]);
  154. da_free(video->inputs);
  155. event_destroy(video->update_event);
  156. event_destroy(video->stop_event);
  157. pthread_mutex_destroy(&video->data_mutex);
  158. pthread_mutex_destroy(&video->input_mutex);
  159. bfree(video);
  160. }
  161. static size_t video_get_input_idx(video_t video,
  162. void (*callback)(void *param, const struct video_data *frame),
  163. void *param)
  164. {
  165. for (size_t i = 0; i < video->inputs.num; i++) {
  166. struct video_input *input = video->inputs.array+i;
  167. if (input->callback == callback && input->param == param)
  168. return i;
  169. }
  170. return DARRAY_INVALID;
  171. }
  172. static inline bool video_input_init(struct video_input *input,
  173. struct video_output *video)
  174. {
  175. if (input->conversion.width != video->info.width ||
  176. input->conversion.height != video->info.height ||
  177. input->conversion.format != video->info.format) {
  178. struct video_scale_info from = {
  179. .format = video->info.format,
  180. .width = video->info.width,
  181. .height = video->info.height,
  182. };
  183. int ret = video_scaler_create(&input->scaler,
  184. &input->conversion, &from,
  185. VIDEO_SCALE_FAST_BILINEAR);
  186. if (ret != VIDEO_SCALER_SUCCESS) {
  187. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  188. blog(LOG_ERROR, "video_input_init: Bad "
  189. "scale conversion type");
  190. else
  191. blog(LOG_ERROR, "video_input_init: Failed to "
  192. "create scaler");
  193. return false;
  194. }
  195. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  196. video_frame_init(&input->frame[i],
  197. input->conversion.format,
  198. input->conversion.width,
  199. input->conversion.height);
  200. }
  201. return true;
  202. }
  203. bool video_output_connect(video_t video,
  204. const struct video_scale_info *conversion,
  205. void (*callback)(void *param, const struct video_data *frame),
  206. void *param)
  207. {
  208. bool success = false;
  209. if (!video || !callback)
  210. return false;
  211. pthread_mutex_lock(&video->input_mutex);
  212. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  213. struct video_input input;
  214. memset(&input, 0, sizeof(input));
  215. input.callback = callback;
  216. input.param = param;
  217. if (conversion) {
  218. input.conversion = *conversion;
  219. } else {
  220. input.conversion.format = video->info.format;
  221. input.conversion.width = video->info.width;
  222. input.conversion.height = video->info.height;
  223. }
  224. if (input.conversion.width == 0)
  225. input.conversion.width = video->info.width;
  226. if (input.conversion.height == 0)
  227. input.conversion.height = video->info.height;
  228. success = video_input_init(&input, video);
  229. if (success)
  230. da_push_back(video->inputs, &input);
  231. }
  232. pthread_mutex_unlock(&video->input_mutex);
  233. return success;
  234. }
  235. void video_output_disconnect(video_t video,
  236. void (*callback)(void *param, const struct video_data *frame),
  237. void *param)
  238. {
  239. if (!video || !callback)
  240. return;
  241. pthread_mutex_lock(&video->input_mutex);
  242. size_t idx = video_get_input_idx(video, callback, param);
  243. if (idx != DARRAY_INVALID) {
  244. video_input_free(video->inputs.array+idx);
  245. da_erase(video->inputs, idx);
  246. }
  247. pthread_mutex_unlock(&video->input_mutex);
  248. }
  249. bool video_output_active(video_t video)
  250. {
  251. if (!video) return false;
  252. return video->inputs.num != 0;
  253. }
  254. const struct video_output_info *video_output_getinfo(video_t video)
  255. {
  256. return video ? &video->info : NULL;
  257. }
  258. void video_output_swap_frame(video_t video, struct video_data *frame)
  259. {
  260. if (!video) return;
  261. pthread_mutex_lock(&video->data_mutex);
  262. video->next_frame = *frame;
  263. video->new_frame = true;
  264. pthread_mutex_unlock(&video->data_mutex);
  265. }
  266. bool video_output_wait(video_t video)
  267. {
  268. if (!video) return false;
  269. event_wait(video->update_event);
  270. return event_try(video->stop_event) == EAGAIN;
  271. }
  272. uint64_t video_getframetime(video_t video)
  273. {
  274. return video ? video->frame_time : 0;
  275. }
  276. uint64_t video_gettime(video_t video)
  277. {
  278. return video ? video->cur_video_time : 0;
  279. }
  280. void video_output_stop(video_t video)
  281. {
  282. void *thread_ret;
  283. if (!video)
  284. return;
  285. if (video->initialized) {
  286. event_signal(video->stop_event);
  287. pthread_join(video->thread, &thread_ret);
  288. event_signal(video->update_event);
  289. }
  290. }