video-io.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. struct video_input {
  22. struct video_convert_info conversion;
  23. void (*callback)(void *param, const struct video_frame *frame);
  24. void *param;
  25. };
  26. struct video_output {
  27. struct video_output_info info;
  28. pthread_t thread;
  29. pthread_mutex_t data_mutex;
  30. event_t stop_event;
  31. struct video_frame cur_frame;
  32. struct video_frame next_frame;
  33. bool new_frame;
  34. event_t update_event;
  35. uint64_t frame_time;
  36. volatile uint64_t cur_video_time;
  37. bool initialized;
  38. pthread_mutex_t input_mutex;
  39. DARRAY(struct video_input) inputs;
  40. };
  41. /* ------------------------------------------------------------------------- */
  42. static inline void video_swapframes(struct video_output *video)
  43. {
  44. pthread_mutex_lock(&video->data_mutex);
  45. if (video->new_frame) {
  46. video->cur_frame = video->next_frame;
  47. video->new_frame = false;
  48. }
  49. pthread_mutex_unlock(&video->data_mutex);
  50. }
  51. static inline void video_output_cur_frame(struct video_output *video)
  52. {
  53. size_t width = video->info.width;
  54. size_t height = video->info.height;
  55. if (!video->cur_frame.data[0])
  56. return;
  57. pthread_mutex_lock(&video->input_mutex);
  58. /* TEST CODE */
  59. /*static struct video_frame frame = {0};
  60. if (!frame.data[0]) {
  61. frame.data[0] = bmalloc(width * height);
  62. frame.data[1] = bmalloc((width/2) * (height/2));
  63. frame.data[2] = bmalloc((width/2) * (height/2));
  64. frame.row_size[0] = width;
  65. frame.row_size[1] = width/2;
  66. frame.row_size[2] = width/2;
  67. }
  68. compress_uyvx_to_i420(
  69. video->cur_frame.data[0], video->cur_frame.row_size[0],
  70. width, height, 0, height,
  71. (uint8_t**)frame.data, (uint32_t*)frame.row_size);*/
  72. /* TODO: conversion */
  73. for (size_t i = 0; i < video->inputs.num; i++) {
  74. struct video_input *input = video->inputs.array+i;
  75. input->callback(input->param, &video->cur_frame);//&frame);
  76. }
  77. pthread_mutex_unlock(&video->input_mutex);
  78. }
  79. static void *video_thread(void *param)
  80. {
  81. struct video_output *video = param;
  82. uint64_t cur_time = os_gettime_ns();
  83. while (event_try(&video->stop_event) == EAGAIN) {
  84. /* wait half a frame, update frame */
  85. os_sleepto_ns(cur_time += (video->frame_time/2));
  86. video->cur_video_time = cur_time;
  87. event_signal(&video->update_event);
  88. /* wait another half a frame, swap and output frames */
  89. os_sleepto_ns(cur_time += (video->frame_time/2));
  90. video_swapframes(video);
  91. video_output_cur_frame(video);
  92. }
  93. return NULL;
  94. }
  95. /* ------------------------------------------------------------------------- */
  96. static inline bool valid_video_params(struct video_output_info *info)
  97. {
  98. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  99. info->fps_num != 0;
  100. }
  101. int video_output_open(video_t *video, struct video_output_info *info)
  102. {
  103. struct video_output *out;
  104. if (!valid_video_params(info))
  105. return VIDEO_OUTPUT_INVALIDPARAM;
  106. out = bmalloc(sizeof(struct video_output));
  107. memset(out, 0, sizeof(struct video_output));
  108. memcpy(&out->info, info, sizeof(struct video_output_info));
  109. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  110. (double)info->fps_num);
  111. out->initialized = false;
  112. if (pthread_mutex_init(&out->data_mutex, NULL) != 0)
  113. goto fail;
  114. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  115. goto fail;
  116. if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
  117. goto fail;
  118. if (event_init(&out->update_event, EVENT_TYPE_AUTO) != 0)
  119. goto fail;
  120. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  121. goto fail;
  122. out->initialized = true;
  123. *video = out;
  124. return VIDEO_OUTPUT_SUCCESS;
  125. fail:
  126. video_output_close(out);
  127. return VIDEO_OUTPUT_FAIL;
  128. }
  129. void video_output_close(video_t video)
  130. {
  131. if (!video)
  132. return;
  133. video_output_stop(video);
  134. da_free(video->inputs);
  135. event_destroy(&video->update_event);
  136. event_destroy(&video->stop_event);
  137. pthread_mutex_destroy(&video->data_mutex);
  138. pthread_mutex_destroy(&video->input_mutex);
  139. bfree(video);
  140. }
  141. static size_t video_get_input_idx(video_t video,
  142. void (*callback)(void *param, const struct video_frame *frame),
  143. void *param)
  144. {
  145. for (size_t i = 0; i < video->inputs.num; i++) {
  146. struct video_input *input = video->inputs.array+i;
  147. if (input->callback == callback && input->param == param)
  148. return i;
  149. }
  150. return DARRAY_INVALID;
  151. }
  152. void video_output_connect(video_t video,
  153. struct video_convert_info *conversion,
  154. void (*callback)(void *param, const struct video_frame *frame),
  155. void *param)
  156. {
  157. pthread_mutex_lock(&video->input_mutex);
  158. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  159. struct video_input input;
  160. input.callback = callback;
  161. input.param = param;
  162. /* TODO: conversion */
  163. if (conversion) {
  164. input.conversion = *conversion;
  165. if (input.conversion.width == 0)
  166. input.conversion.width = video->info.width;
  167. if (input.conversion.height == 0)
  168. input.conversion.height = video->info.height;
  169. } else {
  170. input.conversion.format = video->info.format;
  171. input.conversion.width = video->info.width;
  172. input.conversion.height = video->info.height;
  173. input.conversion.row_align = 1;
  174. }
  175. da_push_back(video->inputs, &input);
  176. }
  177. pthread_mutex_unlock(&video->input_mutex);
  178. }
  179. void video_output_disconnect(video_t video,
  180. void (*callback)(void *param, const struct video_frame *frame),
  181. void *param)
  182. {
  183. pthread_mutex_lock(&video->input_mutex);
  184. size_t idx = video_get_input_idx(video, callback, param);
  185. if (idx != DARRAY_INVALID)
  186. da_erase(video->inputs, idx);
  187. pthread_mutex_unlock(&video->input_mutex);
  188. }
  189. const struct video_output_info *video_output_getinfo(video_t video)
  190. {
  191. return &video->info;
  192. }
  193. void video_output_frame(video_t video, struct video_frame *frame)
  194. {
  195. pthread_mutex_lock(&video->data_mutex);
  196. video->next_frame = *frame;
  197. video->new_frame = true;
  198. pthread_mutex_unlock(&video->data_mutex);
  199. }
  200. bool video_output_wait(video_t video)
  201. {
  202. event_wait(&video->update_event);
  203. return event_try(&video->stop_event) == EAGAIN;
  204. }
  205. uint64_t video_getframetime(video_t video)
  206. {
  207. return video->frame_time;
  208. }
  209. uint64_t video_gettime(video_t video)
  210. {
  211. return video->cur_video_time;
  212. }
  213. void video_output_stop(video_t video)
  214. {
  215. void *thread_ret;
  216. if (!video)
  217. return;
  218. if (video->initialized) {
  219. event_signal(&video->stop_event);
  220. pthread_join(video->thread, &thread_ret);
  221. event_signal(&video->update_event);
  222. }
  223. }