Skip to content

Commit

Permalink
libobs: Add ability to rotate async sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jp9000 committed Mar 1, 2020
1 parent 88e8ffc commit b9d6675
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/sphinx/reference-sources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,14 @@ Functions used by sources
---------------------

.. function:: void obs_source_set_async_rotation(obs_source_t *source, long rotation)

Allows the ability to set rotation (0, 90, 180, -90, 270) for an
async video source. The rotation will be automatically applied to
the source.

---------------------

.. function:: void obs_source_preload_video(obs_source_t *source, const struct obs_source_frame *frame)

Preloads a video frame to ensure a frame is ready for playback as
Expand Down
1 change: 1 addition & 0 deletions libobs/obs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ struct obs_source {
bool async_cache_full_range;
enum gs_color_format async_texture_formats[MAX_AV_PLANES];
int async_channel_count;
long async_rotation;
bool async_flip;
bool async_active;
bool async_update_texture;
Expand Down
55 changes: 52 additions & 3 deletions libobs/obs-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -2038,10 +2038,41 @@ static void obs_source_update_async_video(obs_source_t *source)
}
}

static void rotate_async_video(obs_source_t *source, long rotation)
{
float x = 0;
float y = 0;

switch (rotation) {
case 90:
y = (float)source->async_width;
break;
case 270:
case -90:
x = (float)source->async_height;
break;
case 180:
x = (float)source->async_width;
y = (float)source->async_height;
}

gs_matrix_translate3f(x, y, 0);
gs_matrix_rotaa4f(0.0f, 0.0f, -1.0f, RAD((float)rotation));
}

static inline void obs_source_render_async_video(obs_source_t *source)
{
if (source->async_textures[0] && source->async_active)
if (source->async_textures[0] && source->async_active) {
long rotation = source->async_rotation;
if (rotation) {
gs_matrix_push();
rotate_async_video(source, rotation);
}
obs_source_draw_async_texture(source);
if (rotation) {
gs_matrix_pop();
}
}
}

static inline void obs_source_render_filters(obs_source_t *source)
Expand Down Expand Up @@ -2165,6 +2196,18 @@ void obs_source_video_render(obs_source_t *source)
obs_source_release(source);
}

static inline uint32_t get_async_width(const obs_source_t *source)
{
return ((source->async_rotation % 180) == 0) ? source->async_width
: source->async_height;
}

static inline uint32_t get_async_height(const obs_source_t *source)
{
return ((source->async_rotation % 180) == 0) ? source->async_height
: source->async_width;
}

static uint32_t get_base_width(const obs_source_t *source)
{
bool is_filter = !!source->filter_parent;
Expand All @@ -2180,7 +2223,7 @@ static uint32_t get_base_width(const obs_source_t *source)
return get_base_width(source->filter_target);
}

return source->async_active ? source->async_width : 0;
return source->async_active ? get_async_width(source) : 0;
}

static uint32_t get_base_height(const obs_source_t *source)
Expand All @@ -2198,7 +2241,7 @@ static uint32_t get_base_height(const obs_source_t *source)
return get_base_height(source->filter_target);
}

return source->async_active ? source->async_height : 0;
return source->async_active ? get_async_height(source) : 0;
}

static uint32_t get_recurse_width(obs_source_t *source)
Expand Down Expand Up @@ -2800,6 +2843,12 @@ void obs_source_output_video2(obs_source_t *source,
obs_source_output_video_internal(source, &new_frame);
}

void obs_source_set_async_rotation(obs_source_t *source, long rotation)
{
if (source)
source->async_rotation = rotation;
}

static inline bool preload_frame_changed(obs_source_t *source,
const struct obs_source_frame *in)
{
Expand Down
2 changes: 2 additions & 0 deletions libobs/obs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,8 @@ EXPORT void obs_source_output_video(obs_source_t *source,
EXPORT void obs_source_output_video2(obs_source_t *source,
const struct obs_source_frame2 *frame);

EXPORT void obs_source_set_async_rotation(obs_source_t *source, long rotation);

/**
* Preloads asynchronous video data to allow instantaneous playback
*
Expand Down

0 comments on commit b9d6675

Please sign in to comment.