video-io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #define MAX_CACHE_SIZE 16
  25. struct cached_frame_info {
  26. struct video_data frame;
  27. int count;
  28. };
  29. struct video_input {
  30. struct video_scale_info conversion;
  31. video_scaler_t *scaler;
  32. struct video_frame frame[MAX_CONVERT_BUFFERS];
  33. int cur_frame;
  34. void (*callback)(void *param, struct video_data *frame);
  35. void *param;
  36. };
  37. static inline void video_input_free(struct video_input *input)
  38. {
  39. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  40. video_frame_free(&input->frame[i]);
  41. video_scaler_destroy(input->scaler);
  42. }
  43. struct video_output {
  44. struct video_output_info info;
  45. pthread_t thread;
  46. pthread_mutex_t data_mutex;
  47. bool stop;
  48. os_sem_t *update_semaphore;
  49. uint64_t frame_time;
  50. uint32_t skipped_frames;
  51. uint32_t total_frames;
  52. bool initialized;
  53. pthread_mutex_t input_mutex;
  54. DARRAY(struct video_input) inputs;
  55. size_t available_frames;
  56. size_t first_added;
  57. size_t last_added;
  58. struct cached_frame_info cache[MAX_CACHE_SIZE];
  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. (const uint8_t * const*)data->data,
  73. data->linesize);
  74. if (success) {
  75. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  76. data->data[i] = frame->data[i];
  77. data->linesize[i] = frame->linesize[i];
  78. }
  79. } else {
  80. blog(LOG_WARNING, "video-io: Could not scale frame!");
  81. }
  82. }
  83. return success;
  84. }
  85. static inline bool video_output_cur_frame(struct video_output *video)
  86. {
  87. struct cached_frame_info *frame_info;
  88. bool complete;
  89. /* -------------------------------- */
  90. pthread_mutex_lock(&video->data_mutex);
  91. frame_info = &video->cache[video->first_added];
  92. pthread_mutex_unlock(&video->data_mutex);
  93. /* -------------------------------- */
  94. pthread_mutex_lock(&video->input_mutex);
  95. for (size_t i = 0; i < video->inputs.num; i++) {
  96. struct video_input *input = video->inputs.array+i;
  97. struct video_data frame = frame_info->frame;
  98. if (scale_video_output(input, &frame))
  99. input->callback(input->param, &frame);
  100. }
  101. pthread_mutex_unlock(&video->input_mutex);
  102. /* -------------------------------- */
  103. pthread_mutex_lock(&video->data_mutex);
  104. frame_info->frame.timestamp += video->frame_time;
  105. complete = --frame_info->count == 0;
  106. if (complete) {
  107. if (++video->first_added == video->info.cache_size)
  108. video->first_added = 0;
  109. if (++video->available_frames == video->info.cache_size)
  110. video->last_added = video->first_added;
  111. }
  112. pthread_mutex_unlock(&video->data_mutex);
  113. /* -------------------------------- */
  114. return complete;
  115. }
  116. static void *video_thread(void *param)
  117. {
  118. struct video_output *video = param;
  119. os_set_thread_name("video-io: video thread");
  120. while (os_sem_wait(video->update_semaphore) == 0) {
  121. if (video->stop)
  122. break;
  123. while (!video->stop && !video_output_cur_frame(video)) {
  124. video->total_frames++;
  125. video->skipped_frames++;
  126. }
  127. video->total_frames++;
  128. }
  129. return NULL;
  130. }
  131. /* ------------------------------------------------------------------------- */
  132. static inline bool valid_video_params(const 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. static inline void init_cache(struct video_output *video)
  138. {
  139. if (video->info.cache_size > MAX_CACHE_SIZE)
  140. video->info.cache_size = MAX_CACHE_SIZE;
  141. for (size_t i = 0; i < video->info.cache_size; i++) {
  142. struct video_frame *frame;
  143. frame = (struct video_frame*)&video->cache[i];
  144. video_frame_init(frame, video->info.format,
  145. video->info.width, video->info.height);
  146. }
  147. video->available_frames = video->info.cache_size;
  148. }
  149. int video_output_open(video_t **video, struct video_output_info *info)
  150. {
  151. struct video_output *out;
  152. pthread_mutexattr_t attr;
  153. if (!valid_video_params(info))
  154. return VIDEO_OUTPUT_INVALIDPARAM;
  155. out = bzalloc(sizeof(struct video_output));
  156. if (!out)
  157. goto fail;
  158. memcpy(&out->info, info, sizeof(struct video_output_info));
  159. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  160. (double)info->fps_num);
  161. out->initialized = false;
  162. if (pthread_mutexattr_init(&attr) != 0)
  163. goto fail;
  164. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  165. goto fail;
  166. if (pthread_mutex_init(&out->data_mutex, &attr) != 0)
  167. goto fail;
  168. if (pthread_mutex_init(&out->input_mutex, &attr) != 0)
  169. goto fail;
  170. if (os_sem_init(&out->update_semaphore, 0) != 0)
  171. goto fail;
  172. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  173. goto fail;
  174. init_cache(out);
  175. out->initialized = true;
  176. *video = out;
  177. return VIDEO_OUTPUT_SUCCESS;
  178. fail:
  179. video_output_close(out);
  180. return VIDEO_OUTPUT_FAIL;
  181. }
  182. void video_output_close(video_t *video)
  183. {
  184. if (!video)
  185. return;
  186. video_output_stop(video);
  187. for (size_t i = 0; i < video->inputs.num; i++)
  188. video_input_free(&video->inputs.array[i]);
  189. da_free(video->inputs);
  190. for (size_t i = 0; i < video->info.cache_size; i++)
  191. video_frame_free((struct video_frame*)&video->cache[i]);
  192. os_sem_destroy(video->update_semaphore);
  193. pthread_mutex_destroy(&video->data_mutex);
  194. pthread_mutex_destroy(&video->input_mutex);
  195. bfree(video);
  196. }
  197. static size_t video_get_input_idx(const video_t *video,
  198. void (*callback)(void *param, struct video_data *frame),
  199. void *param)
  200. {
  201. for (size_t i = 0; i < video->inputs.num; i++) {
  202. struct video_input *input = video->inputs.array+i;
  203. if (input->callback == callback && input->param == param)
  204. return i;
  205. }
  206. return DARRAY_INVALID;
  207. }
  208. static inline bool video_input_init(struct video_input *input,
  209. struct video_output *video)
  210. {
  211. if (input->conversion.width != video->info.width ||
  212. input->conversion.height != video->info.height ||
  213. input->conversion.format != video->info.format) {
  214. struct video_scale_info from = {
  215. .format = video->info.format,
  216. .width = video->info.width,
  217. .height = video->info.height,
  218. };
  219. int ret = video_scaler_create(&input->scaler,
  220. &input->conversion, &from,
  221. VIDEO_SCALE_FAST_BILINEAR);
  222. if (ret != VIDEO_SCALER_SUCCESS) {
  223. if (ret == VIDEO_SCALER_BAD_CONVERSION)
  224. blog(LOG_ERROR, "video_input_init: Bad "
  225. "scale conversion type");
  226. else
  227. blog(LOG_ERROR, "video_input_init: Failed to "
  228. "create scaler");
  229. return false;
  230. }
  231. for (size_t i = 0; i < MAX_CONVERT_BUFFERS; i++)
  232. video_frame_init(&input->frame[i],
  233. input->conversion.format,
  234. input->conversion.width,
  235. input->conversion.height);
  236. }
  237. return true;
  238. }
  239. bool video_output_connect(video_t *video,
  240. const struct video_scale_info *conversion,
  241. void (*callback)(void *param, struct video_data *frame),
  242. void *param)
  243. {
  244. bool success = false;
  245. if (!video || !callback)
  246. return false;
  247. pthread_mutex_lock(&video->input_mutex);
  248. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  249. struct video_input input;
  250. memset(&input, 0, sizeof(input));
  251. input.callback = callback;
  252. input.param = param;
  253. if (conversion) {
  254. input.conversion = *conversion;
  255. } else {
  256. input.conversion.format = video->info.format;
  257. input.conversion.width = video->info.width;
  258. input.conversion.height = video->info.height;
  259. }
  260. if (input.conversion.width == 0)
  261. input.conversion.width = video->info.width;
  262. if (input.conversion.height == 0)
  263. input.conversion.height = video->info.height;
  264. success = video_input_init(&input, video);
  265. if (success)
  266. da_push_back(video->inputs, &input);
  267. }
  268. pthread_mutex_unlock(&video->input_mutex);
  269. return success;
  270. }
  271. void video_output_disconnect(video_t *video,
  272. void (*callback)(void *param, struct video_data *frame),
  273. void *param)
  274. {
  275. if (!video || !callback)
  276. return;
  277. pthread_mutex_lock(&video->input_mutex);
  278. size_t idx = video_get_input_idx(video, callback, param);
  279. if (idx != DARRAY_INVALID) {
  280. video_input_free(video->inputs.array+idx);
  281. da_erase(video->inputs, idx);
  282. }
  283. pthread_mutex_unlock(&video->input_mutex);
  284. }
  285. bool video_output_active(const video_t *video)
  286. {
  287. if (!video) return false;
  288. return video->inputs.num != 0;
  289. }
  290. const struct video_output_info *video_output_get_info(const video_t *video)
  291. {
  292. return video ? &video->info : NULL;
  293. }
  294. bool video_output_lock_frame(video_t *video, struct video_frame *frame,
  295. int count, uint64_t timestamp)
  296. {
  297. struct cached_frame_info *cfi;
  298. bool locked;
  299. if (!video) return false;
  300. pthread_mutex_lock(&video->data_mutex);
  301. if (video->available_frames == 0) {
  302. video->cache[video->last_added].count += count;
  303. locked = false;
  304. } else {
  305. if (video->available_frames != video->info.cache_size) {
  306. if (++video->last_added == video->info.cache_size)
  307. video->last_added = 0;
  308. }
  309. cfi = &video->cache[video->last_added];
  310. cfi->frame.timestamp = timestamp;
  311. cfi->count = count;
  312. memcpy(frame, &cfi->frame, sizeof(*frame));
  313. locked = true;
  314. }
  315. pthread_mutex_unlock(&video->data_mutex);
  316. return locked;
  317. }
  318. void video_output_unlock_frame(video_t *video)
  319. {
  320. if (!video) return;
  321. pthread_mutex_lock(&video->data_mutex);
  322. video->available_frames--;
  323. os_sem_post(video->update_semaphore);
  324. pthread_mutex_unlock(&video->data_mutex);
  325. }
  326. uint64_t video_output_get_frame_time(const video_t *video)
  327. {
  328. return video ? video->frame_time : 0;
  329. }
  330. void video_output_stop(video_t *video)
  331. {
  332. void *thread_ret;
  333. if (!video)
  334. return;
  335. if (video->initialized) {
  336. video->initialized = false;
  337. video->stop = true;
  338. os_sem_post(video->update_semaphore);
  339. pthread_join(video->thread, &thread_ret);
  340. }
  341. }
  342. bool video_output_stopped(video_t *video)
  343. {
  344. if (!video)
  345. return true;
  346. return video->stop;
  347. }
  348. enum video_format video_output_get_format(const video_t *video)
  349. {
  350. return video ? video->info.format : VIDEO_FORMAT_NONE;
  351. }
  352. uint32_t video_output_get_width(const video_t *video)
  353. {
  354. return video ? video->info.width : 0;
  355. }
  356. uint32_t video_output_get_height(const video_t *video)
  357. {
  358. return video ? video->info.height : 0;
  359. }
  360. double video_output_get_frame_rate(const video_t *video)
  361. {
  362. if (!video)
  363. return 0.0;
  364. return (double)video->info.fps_num / (double)video->info.fps_den;
  365. }
  366. uint32_t video_output_get_skipped_frames(const video_t *video)
  367. {
  368. return video->skipped_frames;
  369. }
  370. uint32_t video_output_get_total_frames(const video_t *video)
  371. {
  372. return video->total_frames;
  373. }