video-io.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. os_sleepto_ns(cur_time += (video->frame_time/2));
  101. video->cur_video_time = cur_time;
  102. event_signal(&video->update_event);
  103. /* wait another half a frame, swap and output frames */
  104. os_sleepto_ns(cur_time += (video->frame_time/2));
  105. pthread_mutex_lock(&video->data_mutex);
  106. video_swapframes(video);
  107. video_output_cur_frame(video);
  108. pthread_mutex_unlock(&video->data_mutex);
  109. }
  110. return NULL;
  111. }
  112. /* ------------------------------------------------------------------------- */
  113. static inline bool valid_video_params(struct video_output_info *info)
  114. {
  115. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  116. info->fps_num != 0;
  117. }
  118. int video_output_open(video_t *video, struct video_output_info *info)
  119. {
  120. struct video_output *out;
  121. if (!valid_video_params(info))
  122. return VIDEO_OUTPUT_INVALIDPARAM;
  123. out = bzalloc(sizeof(struct video_output));
  124. memcpy(&out->info, info, sizeof(struct video_output_info));
  125. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  126. (double)info->fps_num);
  127. out->initialized = false;
  128. if (pthread_mutex_init(&out->data_mutex, NULL) != 0)
  129. goto fail;
  130. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  131. goto fail;
  132. if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
  133. goto fail;
  134. if (event_init(&out->update_event, EVENT_TYPE_AUTO) != 0)
  135. goto fail;
  136. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  137. goto fail;
  138. out->initialized = true;
  139. *video = out;
  140. return VIDEO_OUTPUT_SUCCESS;
  141. fail:
  142. video_output_close(out);
  143. return VIDEO_OUTPUT_FAIL;
  144. }
  145. void video_output_close(video_t video)
  146. {
  147. if (!video)
  148. return;
  149. video_output_stop(video);
  150. for (size_t i = 0; i < video->inputs.num; i++)
  151. video_input_free(&video->inputs.array[i]);
  152. da_free(video->inputs);
  153. event_destroy(&video->update_event);
  154. event_destroy(&video->stop_event);
  155. pthread_mutex_destroy(&video->data_mutex);
  156. pthread_mutex_destroy(&video->input_mutex);
  157. bfree(video);
  158. }
  159. static size_t video_get_input_idx(video_t video,
  160. void (*callback)(void *param, const struct video_data *frame),
  161. void *param)
  162. {
  163. for (size_t i = 0; i < video->inputs.num; i++) {
  164. struct video_input *input = video->inputs.array+i;
  165. if (input->callback == callback && input->param == param)
  166. return i;
  167. }
  168. return DARRAY_INVALID;
  169. }
  170. static inline bool video_input_init(struct video_input *input,
  171. struct video_output *video)
  172. {
  173. if (input->conversion.width != video->info.width ||
  174. input->conversion.height != video->info.height ||
  175. input->conversion.format != video->info.format) {
  176. struct video_scale_info from = {
  177. .format = video->info.format,
  178. .width = video->info.width,
  179. .height = video->info.height,
  180. };
  181. int ret = video_scaler_create(&input->scaler,
  182. &input->conversion, &from,
  183. VIDEO_SCALE_FAST_BILINEAR);
  184. if (ret != VIDEO_SCALER_SUCCESS) {
  185. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  186. blog(LOG_WARNING, "video_input_init: Bad "
  187. "scale conversion type");
  188. else
  189. blog(LOG_WARNING, "video_input_init: Failed to "
  190. "create scaler");
  191. return false;
  192. }
  193. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  194. video_frame_init(&input->frame[i],
  195. input->conversion.format,
  196. input->conversion.width,
  197. input->conversion.height);
  198. }
  199. return true;
  200. }
  201. bool video_output_connect(video_t video,
  202. struct video_scale_info *conversion,
  203. void (*callback)(void *param, const struct video_data *frame),
  204. void *param)
  205. {
  206. bool success = false;
  207. pthread_mutex_lock(&video->input_mutex);
  208. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  209. struct video_input input;
  210. memset(&input, 0, sizeof(input));
  211. input.callback = callback;
  212. input.param = param;
  213. if (conversion) {
  214. input.conversion = *conversion;
  215. } else {
  216. input.conversion.format = video->info.format;
  217. input.conversion.width = video->info.width;
  218. input.conversion.height = video->info.height;
  219. }
  220. if (input.conversion.width == 0)
  221. input.conversion.width = video->info.width;
  222. if (input.conversion.height == 0)
  223. input.conversion.height = video->info.height;
  224. success = video_input_init(&input, video);
  225. if (success)
  226. da_push_back(video->inputs, &input);
  227. }
  228. pthread_mutex_unlock(&video->input_mutex);
  229. return success;
  230. }
  231. void video_output_disconnect(video_t video,
  232. void (*callback)(void *param, const struct video_data *frame),
  233. void *param)
  234. {
  235. pthread_mutex_lock(&video->input_mutex);
  236. size_t idx = video_get_input_idx(video, callback, param);
  237. if (idx != DARRAY_INVALID) {
  238. video_input_free(video->inputs.array+idx);
  239. da_erase(video->inputs, idx);
  240. }
  241. pthread_mutex_unlock(&video->input_mutex);
  242. }
  243. bool video_output_active(video_t video)
  244. {
  245. if (!video) return false;
  246. return video->inputs.num != 0;
  247. }
  248. const struct video_output_info *video_output_getinfo(video_t video)
  249. {
  250. return &video->info;
  251. }
  252. void video_output_swap_frame(video_t video, struct video_data *frame)
  253. {
  254. pthread_mutex_lock(&video->data_mutex);
  255. video->next_frame = *frame;
  256. video->new_frame = true;
  257. pthread_mutex_unlock(&video->data_mutex);
  258. }
  259. bool video_output_wait(video_t video)
  260. {
  261. event_wait(&video->update_event);
  262. return event_try(&video->stop_event) == EAGAIN;
  263. }
  264. uint64_t video_getframetime(video_t video)
  265. {
  266. return video->frame_time;
  267. }
  268. uint64_t video_gettime(video_t video)
  269. {
  270. return video->cur_video_time;
  271. }
  272. void video_output_stop(video_t video)
  273. {
  274. void *thread_ret;
  275. if (!video)
  276. return;
  277. if (video->initialized) {
  278. event_signal(&video->stop_event);
  279. pthread_join(video->thread, &thread_ret);
  280. event_signal(&video->update_event);
  281. }
  282. }