obs-studio/libobs/obs-video.c

337 lines
8.7 KiB
C
Raw Normal View History

2013-09-30 19:37:13 -07:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
2013-09-30 19:37:13 -07:00
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "obs.h"
#include "obs-internal.h"
2013-09-30 19:37:13 -07:00
#include "graphics/vec4.h"
#include "media-io/format-conversion.h"
2013-09-30 19:37:13 -07:00
static void tick_sources(uint64_t cur_time, uint64_t *last_time)
2013-09-30 19:37:13 -07:00
{
size_t i;
uint64_t delta_time;
float seconds;
if (!last_time)
*last_time = cur_time - video_getframetime(obs->video.video);
2013-09-30 19:37:13 -07:00
delta_time = cur_time - *last_time;
seconds = (float)((double)delta_time / 1000000000.0);
for (i = 0; i < obs->data.sources.num; i++)
obs_source_video_tick(obs->data.sources.array[i], seconds);
2013-09-30 19:37:13 -07:00
*last_time = cur_time;
}
static inline void render_display_begin(struct obs_display *display)
2013-09-30 19:37:13 -07:00
{
struct vec4 clear_color;
uint32_t width, height;
2013-09-30 19:37:13 -07:00
gs_load_swapchain(display ? display->swap : NULL);
2013-10-17 17:21:42 -07:00
gs_getsize(&width, &height);
2013-09-30 19:37:13 -07:00
gs_beginscene();
vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
2013-09-30 19:37:13 -07:00
&clear_color, 1.0f, 0);
gs_enable_depthtest(false);
/* gs_enable_blending(false); */
gs_setcullmode(GS_NEITHER);
gs_ortho(0.0f, (float)obs->video.base_width,
0.0f, (float)obs->video.base_height, -100.0f, 100.0f);
gs_setviewport(0, 0, width, height);
}
static inline void render_display_end(struct obs_display *display)
{
gs_endscene();
gs_present();
}
static void render_display(struct obs_display *display)
{
render_display_begin(display);
for (size_t i = 0; i < MAX_CHANNELS; i++) {
struct obs_source **p_source;
p_source = (display) ? display->channels+i :
obs->data.channels+i;
if (*p_source) {
if ((*p_source)->removed) {
obs_source_release(*p_source);
*p_source = NULL;
} else {
obs_source_video_render(*p_source);
}
}
}
2013-09-30 19:37:13 -07:00
render_display_end(display);
2013-09-30 19:37:13 -07:00
}
static inline void render_displays(void)
{
if (!obs->data.valid)
return;
gs_entercontext(obs_graphics());
/* render extra displays/swaps */
pthread_mutex_lock(&obs->data.displays_mutex);
for (size_t i = 0; i < obs->data.displays.num; i++)
render_display(obs->data.displays.array[i]);
pthread_mutex_unlock(&obs->data.displays_mutex);
/* render main display */
render_display(NULL);
gs_leavecontext();
}
static inline void set_render_size(uint32_t width, uint32_t height)
2013-09-30 19:37:13 -07:00
{
gs_enable_depthtest(false);
gs_setcullmode(GS_NEITHER);
gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
gs_setviewport(0, 0, width, height);
}
static inline void render_channels(void)
{
struct obs_core_data *data = &obs->data;
2013-09-30 19:37:13 -07:00
for (size_t i = 0; i < MAX_CHANNELS; i++) {
struct obs_source *source = data->channels[i];
if (source)
obs_source_video_render(source);
}
}
static inline void unmap_last_surface(struct obs_core_video *video)
{
if (video->mapped_surface) {
stagesurface_unmap(video->mapped_surface);
video->mapped_surface = NULL;
2013-09-30 19:37:13 -07:00
}
}
2013-09-30 19:37:13 -07:00
static inline void render_main_texture(struct obs_core_video *video,
int cur_texture, int prev_texture)
{
struct vec4 clear_color;
vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
2013-09-30 19:37:13 -07:00
gs_setrendertarget(video->render_textures[cur_texture], NULL);
gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
2013-09-30 19:37:13 -07:00
set_render_size(video->base_width, video->base_height);
render_channels();
2013-09-30 19:37:13 -07:00
video->textures_rendered[cur_texture] = true;
}
static inline void render_output_texture(struct obs_core_video *video,
int cur_texture, int prev_texture)
{
texture_t texture = video->render_textures[prev_texture];
texture_t target = video->output_textures[cur_texture];
uint32_t width = texture_getwidth(target);
uint32_t height = texture_getheight(target);
/* TODO: replace with actual downscalers or unpackers */
effect_t effect = video->default_effect;
technique_t tech = effect_gettechnique(effect, "DrawMatrix");
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 07:04:50 -08:00
eparam_t image = effect_getparambyname(effect, "image");
eparam_t matrix = effect_getparambyname(effect, "color_matrix");
size_t passes, i;
if (!video->textures_rendered[prev_texture])
return;
gs_setrendertarget(target, NULL);
set_render_size(width, height);
/* TODO: replace with programmable code */
const float mat_val[16] =
{
-0.100644f, -0.338572f, 0.439216f, 0.501961f,
0.182586f, 0.614231f, 0.062007f, 0.062745f,
0.439216f, -0.398942f, -0.040274f, 0.501961f,
0.000000f, 0.000000f, 0.000000f, 1.000000f
};
effect_setval(effect, matrix, mat_val, sizeof(mat_val));
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 07:04:50 -08:00
effect_settexture(effect, image, texture);
passes = technique_begin(tech);
for (i = 0; i < passes; i++) {
technique_beginpass(tech, i);
gs_draw_sprite(texture, 0, width, height);
technique_endpass(tech);
}
technique_end(tech);
video->textures_output[cur_texture] = true;
}
static inline void stage_output_texture(struct obs_core_video *video,
int cur_texture, int prev_texture)
{
texture_t texture = video->output_textures[prev_texture];
stagesurf_t copy = video->copy_surfaces[cur_texture];
unmap_last_surface(video);
if (!video->textures_output[prev_texture])
return;
gs_stage_texture(copy, texture);
video->textures_copied[cur_texture] = true;
}
static inline void render_video(struct obs_core_video *video, int cur_texture,
int prev_texture)
{
gs_beginscene();
gs_enable_depthtest(false);
gs_setcullmode(GS_NEITHER);
render_main_texture(video, cur_texture, prev_texture);
render_output_texture(video, cur_texture, prev_texture);
stage_output_texture(video, cur_texture, prev_texture);
gs_setrendertarget(NULL, NULL);
gs_endscene();
}
/* TODO: replace with more optimal conversion */
static inline bool download_frame(struct obs_core_video *video, int cur_texture,
int prev_texture, struct video_frame *frame)
{
stagesurf_t surface = video->copy_surfaces[prev_texture];
if (!video->textures_copied[prev_texture])
return false;
if (!stagesurface_map(surface, &frame->data[0], &frame->linesize[0]))
return false;
video->mapped_surface = surface;
return true;
}
static bool convert_frame(struct obs_core_video *video,
struct video_frame *frame,
const struct video_output_info *info, int cur_texture)
{
struct source_frame *new_frame = &video->convert_frames[cur_texture];
if (info->format == VIDEO_FORMAT_I420) {
compress_uyvx_to_i420(
frame->data[0], frame->linesize[0],
info->width, info->height,
0, info->height,
new_frame->data, new_frame->linesize);
} else if (info->format == VIDEO_FORMAT_NV12) {
compress_uyvx_to_nv12(
frame->data[0], frame->linesize[0],
info->width, info->height,
0, info->height,
new_frame->data, new_frame->linesize);
} else {
blog(LOG_WARNING, "convert_frame: unsupported texture format");
return false;
2013-09-30 19:37:13 -07:00
}
for (size_t i = 0; i < MAX_VIDEO_PLANES; i++) {
frame->data[i] = new_frame->data[i];
frame->linesize[i] = new_frame->linesize[i];
}
return true;
}
static inline void output_video_data(struct obs_core_video *video,
struct video_frame *frame, int cur_texture)
{
const struct video_output_info *info;
info = video_output_getinfo(video->video);
if (format_is_yuv(info->format))
if (!convert_frame(video, frame, info, cur_texture))
return;
video_output_frame(video->video, frame);
}
static inline void output_frame(uint64_t timestamp)
{
struct obs_core_video *video = &obs->video;
int cur_texture = video->cur_texture;
int prev_texture = cur_texture == 0 ? NUM_TEXTURES-1 : cur_texture-1;
struct video_frame frame;
bool frame_ready;
memset(&frame, 0, sizeof(struct video_frame));
frame.timestamp = timestamp;
gs_entercontext(obs_graphics());
render_video(video, cur_texture, prev_texture);
frame_ready = download_frame(video, cur_texture, prev_texture, &frame);
gs_leavecontext();
if (frame_ready)
output_video_data(video, &frame, cur_texture);
2013-09-30 19:37:13 -07:00
if (++video->cur_texture == NUM_TEXTURES)
video->cur_texture = 0;
2013-09-30 19:37:13 -07:00
}
void *obs_video_thread(void *param)
{
uint64_t last_time = 0;
while (video_output_wait(obs->video.video)) {
uint64_t cur_time = video_gettime(obs->video.video);
2013-09-30 19:37:13 -07:00
tick_sources(cur_time, &last_time);
render_displays();
output_frame(cur_time);
2013-09-30 19:37:13 -07:00
}
return NULL;
}