0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 04:42:18 +02:00

linux-pipewire: Factor out format data in a struct

And use it instead of exhaustively open coding each field of
the anonymous struct into the arguments of the lookup function.
This commit is contained in:
Georges Basile Stavracas Neto 2022-07-01 20:45:05 -03:00 committed by Georges Basile Stavracas Neto
parent d40f5808f4
commit 37fd856e91

View File

@ -253,13 +253,15 @@ static bool push_rotation(obs_pipewire_stream *obs_pw_stream)
return rotation != 0;
}
static const struct {
struct format_data {
uint32_t spa_format;
uint32_t drm_format;
enum gs_color_format gs_format;
bool swap_red_blue;
const char *pretty_name;
} supported_formats[] = {
};
static const struct format_data supported_formats[] = {
{
SPA_VIDEO_FORMAT_BGRA,
DRM_FORMAT_ARGB8888,
@ -311,20 +313,17 @@ static const struct {
#define N_SUPPORTED_FORMATS \
(sizeof(supported_formats) / sizeof(supported_formats[0]))
static bool lookup_format_info_from_spa_format(
uint32_t spa_format, uint32_t *out_drm_format,
enum gs_color_format *out_gs_format, bool *out_swap_red_blue)
static bool
lookup_format_info_from_spa_format(uint32_t spa_format,
struct format_data *out_format_data)
{
for (size_t i = 0; i < N_SUPPORTED_FORMATS; i++) {
if (supported_formats[i].spa_format != spa_format)
continue;
if (out_drm_format)
*out_drm_format = supported_formats[i].drm_format;
if (out_gs_format)
*out_gs_format = supported_formats[i].gs_format;
if (out_swap_red_blue)
*out_swap_red_blue = supported_formats[i].swap_red_blue;
if (out_format_data)
*out_format_data = supported_formats[i];
return true;
}
return false;
@ -579,6 +578,7 @@ static void on_process_cb(void *user_data)
struct spa_meta_header *header;
struct spa_meta_region *region;
struct spa_meta_videotransform *video_transform;
struct format_data format_data;
struct spa_buffer *buffer;
struct pw_buffer *b;
bool swap_red_blue = false;
@ -638,8 +638,8 @@ static void on_process_cb(void *user_data)
#endif
if (!lookup_format_info_from_spa_format(
obs_pw_stream->format.info.raw.format, &drm_format,
NULL, NULL)) {
obs_pw_stream->format.info.raw.format,
&format_data)) {
blog(LOG_ERROR,
"[pipewire] unsupported DMA buffer format: %d",
obs_pw_stream->format.info.raw.format);
@ -668,9 +668,9 @@ static void on_process_cb(void *user_data)
DRM_FORMAT_MOD_INVALID;
obs_pw_stream->texture = gs_texture_create_from_dmabuf(
obs_pw_stream->format.info.raw.size.width,
obs_pw_stream->format.info.raw.size.height, drm_format,
GS_BGRX, planes, fds, strides, offsets,
use_modifiers ? modifiers : NULL);
obs_pw_stream->format.info.raw.size.height,
format_data.drm_format, GS_BGRX, planes, fds, strides,
offsets, use_modifiers ? modifiers : NULL);
if (obs_pw_stream->texture == NULL) {
remove_modifier_from_format(
@ -684,11 +684,10 @@ static void on_process_cb(void *user_data)
}
} else {
blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
enum gs_color_format gs_format;
if (!lookup_format_info_from_spa_format(
obs_pw_stream->format.info.raw.format, NULL,
&gs_format, &swap_red_blue)) {
obs_pw_stream->format.info.raw.format,
&format_data)) {
blog(LOG_ERROR,
"[pipewire] unsupported buffer format: %d",
obs_pw_stream->format.info.raw.format);
@ -711,9 +710,9 @@ static void on_process_cb(void *user_data)
g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
obs_pw_stream->texture = gs_texture_create(
obs_pw_stream->format.info.raw.size.width,
obs_pw_stream->format.info.raw.size.height, gs_format,
1, (const uint8_t **)&buffer->datas[0].data,
GS_DYNAMIC);
obs_pw_stream->format.info.raw.size.height,
format_data.gs_format, 1,
(const uint8_t **)&buffer->datas[0].data, GS_DYNAMIC);
}
if (swap_red_blue)
@ -756,7 +755,6 @@ read_metadata:
spa_meta_cursor_is_valid(cursor);
if (obs_pw_stream->cursor.visible && obs_pw_stream->cursor.valid) {
struct spa_meta_bitmap *bitmap = NULL;
enum gs_color_format gs_format;
if (cursor->bitmap_offset)
bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset,
@ -768,8 +766,8 @@ read_metadata:
if (bitmap && bitmap->size.width > 0 &&
bitmap->size.height > 0 &&
lookup_format_info_from_spa_format(
bitmap->format, NULL, &gs_format, &swap_red_blue)) {
lookup_format_info_from_spa_format(bitmap->format,
&format_data)) {
const uint8_t *bitmap_data;
bitmap_data =
@ -780,10 +778,11 @@ read_metadata:
obs_pw_stream->cursor.height = bitmap->size.height;
assert(obs_pw_stream->cursor.texture == NULL);
obs_pw_stream->cursor.texture = gs_texture_create(
obs_pw_stream->cursor.width,
obs_pw_stream->cursor.height, gs_format, 1,
&bitmap_data, GS_DYNAMIC);
obs_pw_stream->cursor.texture =
gs_texture_create(obs_pw_stream->cursor.width,
obs_pw_stream->cursor.height,
format_data.gs_format, 1,
&bitmap_data, GS_DYNAMIC);
if (swap_red_blue)
swap_texture_red_blue(