obs-video.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "obs.h"
  15. #include "obs-internal.h"
  16. #include "graphics/vec4.h"
  17. #include "media-io/format-conversion.h"
  18. static void tick_sources(uint64_t cur_time, uint64_t *last_time)
  19. {
  20. size_t i;
  21. uint64_t delta_time;
  22. float seconds;
  23. if (!last_time)
  24. *last_time = cur_time - video_getframetime(obs->video.video);
  25. delta_time = cur_time - *last_time;
  26. seconds = (float)((double)delta_time / 1000000000.0);
  27. for (i = 0; i < obs->data.sources.num; i++)
  28. obs_source_video_tick(obs->data.sources.array[i], seconds);
  29. *last_time = cur_time;
  30. }
  31. static inline void render_display_begin(struct obs_display *display)
  32. {
  33. struct vec4 clear_color;
  34. uint32_t width, height;
  35. gs_load_swapchain(display ? display->swap : NULL);
  36. gs_getsize(&width, &height);
  37. gs_beginscene();
  38. vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
  39. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  40. &clear_color, 1.0f, 0);
  41. gs_enable_depthtest(false);
  42. /* gs_enable_blending(false); */
  43. gs_setcullmode(GS_NEITHER);
  44. gs_ortho(0.0f, (float)obs->video.base_width,
  45. 0.0f, (float)obs->video.base_height, -100.0f, 100.0f);
  46. gs_setviewport(0, 0, width, height);
  47. }
  48. static inline void render_display_end(struct obs_display *display)
  49. {
  50. gs_endscene();
  51. gs_present();
  52. }
  53. static void render_display(struct obs_display *display)
  54. {
  55. render_display_begin(display);
  56. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  57. struct obs_source **p_source;
  58. p_source = (display) ? display->channels+i :
  59. obs->data.channels+i;
  60. if (*p_source) {
  61. if ((*p_source)->removed) {
  62. obs_source_release(*p_source);
  63. *p_source = NULL;
  64. } else {
  65. obs_source_video_render(*p_source);
  66. }
  67. }
  68. }
  69. render_display_end(display);
  70. }
  71. static inline void render_displays(void)
  72. {
  73. if (!obs->data.valid)
  74. return;
  75. gs_entercontext(obs_graphics());
  76. /* render extra displays/swaps */
  77. pthread_mutex_lock(&obs->data.displays_mutex);
  78. for (size_t i = 0; i < obs->data.displays.num; i++)
  79. render_display(obs->data.displays.array[i]);
  80. pthread_mutex_unlock(&obs->data.displays_mutex);
  81. /* render main display */
  82. render_display(NULL);
  83. gs_leavecontext();
  84. }
  85. static inline void set_render_size(uint32_t width, uint32_t height)
  86. {
  87. gs_enable_depthtest(false);
  88. gs_setcullmode(GS_NEITHER);
  89. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
  90. gs_setviewport(0, 0, width, height);
  91. }
  92. static inline void render_channels(void)
  93. {
  94. struct obs_core_data *data = &obs->data;
  95. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  96. struct obs_source *source = data->channels[i];
  97. if (source)
  98. obs_source_video_render(source);
  99. }
  100. }
  101. static inline void unmap_last_surface(struct obs_core_video *video)
  102. {
  103. if (video->mapped_surface) {
  104. stagesurface_unmap(video->mapped_surface);
  105. video->mapped_surface = NULL;
  106. }
  107. }
  108. static inline void render_main_texture(struct obs_core_video *video,
  109. int cur_texture, int prev_texture)
  110. {
  111. struct vec4 clear_color;
  112. vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
  113. gs_setrendertarget(video->render_textures[cur_texture], NULL);
  114. gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
  115. set_render_size(video->base_width, video->base_height);
  116. render_channels();
  117. video->textures_rendered[cur_texture] = true;
  118. }
  119. static inline void render_output_texture(struct obs_core_video *video,
  120. int cur_texture, int prev_texture)
  121. {
  122. texture_t texture = video->render_textures[prev_texture];
  123. texture_t target = video->output_textures[cur_texture];
  124. uint32_t width = texture_getwidth(target);
  125. uint32_t height = texture_getheight(target);
  126. /* TODO: replace with actual downscalers or unpackers */
  127. effect_t effect = video->default_effect;
  128. technique_t tech = effect_gettechnique(effect, "DrawMatrix");
  129. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  130. eparam_t matrix = effect_getparambyname(effect, "color_matrix");
  131. size_t passes, i;
  132. if (!video->textures_rendered[prev_texture])
  133. return;
  134. gs_setrendertarget(target, NULL);
  135. set_render_size(width, height);
  136. /* TODO: replace with programmable code */
  137. const float mat_val[16] =
  138. {
  139. -0.100644f, -0.338572f, 0.439216f, 0.501961f,
  140. 0.182586f, 0.614231f, 0.062007f, 0.062745f,
  141. 0.439216f, -0.398942f, -0.040274f, 0.501961f,
  142. 0.000000f, 0.000000f, 0.000000f, 1.000000f
  143. };
  144. effect_setval(effect, matrix, mat_val, sizeof(mat_val));
  145. effect_settexture(effect, diffuse, texture);
  146. passes = technique_begin(tech);
  147. for (i = 0; i < passes; i++) {
  148. technique_beginpass(tech, i);
  149. gs_draw_sprite(texture, 0, width, height);
  150. technique_endpass(tech);
  151. }
  152. technique_end(tech);
  153. video->textures_output[cur_texture] = true;
  154. }
  155. static inline void stage_output_texture(struct obs_core_video *video,
  156. int cur_texture, int prev_texture)
  157. {
  158. texture_t texture = video->output_textures[prev_texture];
  159. stagesurf_t copy = video->copy_surfaces[cur_texture];
  160. unmap_last_surface(video);
  161. if (!video->textures_output[prev_texture])
  162. return;
  163. gs_stage_texture(copy, texture);
  164. video->textures_copied[cur_texture] = true;
  165. }
  166. static inline void render_video(struct obs_core_video *video, int cur_texture,
  167. int prev_texture)
  168. {
  169. gs_beginscene();
  170. gs_enable_depthtest(false);
  171. gs_setcullmode(GS_NEITHER);
  172. render_main_texture(video, cur_texture, prev_texture);
  173. render_output_texture(video, cur_texture, prev_texture);
  174. stage_output_texture(video, cur_texture, prev_texture);
  175. gs_setrendertarget(NULL, NULL);
  176. gs_endscene();
  177. }
  178. /* TODO: replace with more optimal conversion */
  179. static inline bool download_frame(struct obs_core_video *video, int cur_texture,
  180. int prev_texture, struct video_frame *frame)
  181. {
  182. stagesurf_t surface = video->copy_surfaces[prev_texture];
  183. if (!video->textures_copied[prev_texture])
  184. return false;
  185. if (!stagesurface_map(surface, &frame->data[0], &frame->linesize[0]))
  186. return false;
  187. video->mapped_surface = surface;
  188. return true;
  189. }
  190. static bool convert_frame(struct obs_core_video *video,
  191. struct video_frame *frame,
  192. const struct video_output_info *info, int cur_texture)
  193. {
  194. struct source_frame *new_frame = &video->convert_frames[cur_texture];
  195. if (info->format == VIDEO_FORMAT_I420) {
  196. compress_uyvx_to_i420(
  197. frame->data[0], frame->linesize[0],
  198. info->width, info->height,
  199. 0, info->height,
  200. new_frame->data, new_frame->linesize);
  201. } else if (info->format == VIDEO_FORMAT_NV12) {
  202. compress_uyvx_to_nv12(
  203. frame->data[0], frame->linesize[0],
  204. info->width, info->height,
  205. 0, info->height,
  206. new_frame->data, new_frame->linesize);
  207. } else {
  208. blog(LOG_WARNING, "convert_frame: unsupported texture format");
  209. return false;
  210. }
  211. for (size_t i = 0; i < MAX_VIDEO_PLANES; i++) {
  212. frame->data[i] = new_frame->data[i];
  213. frame->linesize[i] = new_frame->linesize[i];
  214. }
  215. return true;
  216. }
  217. static inline void output_video_data(struct obs_core_video *video,
  218. struct video_frame *frame, int cur_texture)
  219. {
  220. const struct video_output_info *info;
  221. info = video_output_getinfo(video->video);
  222. if (format_is_yuv(info->format))
  223. if (!convert_frame(video, frame, info, cur_texture))
  224. return;
  225. video_output_frame(video->video, frame);
  226. }
  227. static inline void output_frame(uint64_t timestamp)
  228. {
  229. struct obs_core_video *video = &obs->video;
  230. int cur_texture = video->cur_texture;
  231. int prev_texture = cur_texture == 0 ? NUM_TEXTURES-1 : cur_texture-1;
  232. struct video_frame frame;
  233. bool frame_ready;
  234. memset(&frame, 0, sizeof(struct video_frame));
  235. frame.timestamp = timestamp;
  236. gs_entercontext(obs_graphics());
  237. render_video(video, cur_texture, prev_texture);
  238. frame_ready = download_frame(video, cur_texture, prev_texture, &frame);
  239. gs_leavecontext();
  240. if (frame_ready)
  241. output_video_data(video, &frame, cur_texture);
  242. if (++video->cur_texture == NUM_TEXTURES)
  243. video->cur_texture = 0;
  244. }
  245. void *obs_video_thread(void *param)
  246. {
  247. uint64_t last_time = 0;
  248. while (video_output_wait(obs->video.video)) {
  249. uint64_t cur_time = video_gettime(obs->video.video);
  250. tick_sources(cur_time, &last_time);
  251. render_displays();
  252. output_frame(cur_time);
  253. }
  254. return NULL;
  255. }