diff --git a/libobs/build/libobs.graphics-image-max-size+.h.c b/libobs/build/libobs.graphics-image-max-size+.h.c new file mode 100644 index 0000000000..40bd9508b3 --- /dev/null +++ b/libobs/build/libobs.graphics-image-max-size+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): private image dependency, separate archive member. */ +#include "libobs/build/libobs-graphics.pch.h" + +#include "libobs/graphics/graphics-image-max-size.c.h" diff --git a/libobs/build/libobs.media+.h.c b/libobs/build/libobs.media+.h.c index 687dbd3adb..922bafb829 100644 --- a/libobs/build/libobs.media+.h.c +++ b/libobs/build/libobs.media+.h.c @@ -3,8 +3,6 @@ #include "libobs/build/libobs-media.pch.h" #include "libobs/graphics/graphics-ffmpeg-init-adapter.c.h" -#include "libobs/graphics/graphics-ffmpeg-orientation-adapter.c.h" -#include "libobs/graphics/graphics-ffmpeg-orientation-fields.c.h" #include "libobs/graphics/graphics-ffmpeg.c.h" #include "libobs/graphics/graphics-texture-file-data.c.h" #include "libobs/media-io/media-remux-ffmpeg.c.h" diff --git a/libobs/build/libobs.media-image-orientation-fields+.h.c b/libobs/build/libobs.media-image-orientation-fields+.h.c new file mode 100644 index 0000000000..916c4dc9ff --- /dev/null +++ b/libobs/build/libobs.media-image-orientation-fields+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): private image dependency, separate archive member. */ +#include "libobs/build/libobs-media.pch.h" + +#include "libobs/graphics/graphics-ffmpeg-orientation-fields.c.h" diff --git a/libobs/build/libobs.media-texture-file-data-convert+.h.c b/libobs/build/libobs.media-texture-file-data-convert+.h.c new file mode 100644 index 0000000000..29094fb3e8 --- /dev/null +++ b/libobs/build/libobs.media-texture-file-data-convert+.h.c @@ -0,0 +1,5 @@ +/* Unity group (BUILD.md): private image dependency, separate archive member. */ +#include "libobs/build/libobs-media.pch.h" + +#include "libobs/graphics/graphics-ffmpeg-orientation-adapter.c.h" +#include "libobs/graphics/graphics-texture-file-data-convert.c.h" diff --git a/libobs/build/libobs.media-texture-file-data-session+.h.c b/libobs/build/libobs.media-texture-file-data-session+.h.c new file mode 100644 index 0000000000..88f20f8367 --- /dev/null +++ b/libobs/build/libobs.media-texture-file-data-session+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): private image dependency, separate archive member. */ +#include "libobs/build/libobs-media.pch.h" + +#include "libobs/graphics/graphics-texture-file-data-adapter.c.h" diff --git a/libobs/build/modules.d/graphics.json b/libobs/build/modules.d/graphics.json index 97ee7299ad..84d14de7d2 100644 --- a/libobs/build/modules.d/graphics.json +++ b/libobs/build/modules.d/graphics.json @@ -3,6 +3,7 @@ "groups": [ "effect-parser-funcs", "graphics", + "graphics-image-max-size", "graphics-linear-srgb", "graphics-stencil-adapter", "graphics-texture-creators-fields" diff --git a/libobs/build/modules.d/media.json b/libobs/build/modules.d/media.json index a426d580db..8bf2fe1dc4 100644 --- a/libobs/build/modules.d/media.json +++ b/libobs/build/modules.d/media.json @@ -2,7 +2,10 @@ "description": "libobs/media-io.", "groups": [ "media", - "media-resampler-kernel" + "media-image-orientation-fields", + "media-resampler-kernel", + "media-texture-file-data-convert", + "media-texture-file-data-session" ], "depends": [] } diff --git a/libobs/build/source-sets.d/LIBOBS_SOURCES.json b/libobs/build/source-sets.d/LIBOBS_SOURCES.json index 90aff04738..788f0bd47e 100644 --- a/libobs/build/source-sets.d/LIBOBS_SOURCES.json +++ b/libobs/build/source-sets.d/LIBOBS_SOURCES.json @@ -228,11 +228,15 @@ "build/libobs.effect-parser-funcs+.h.c", "build/libobs.util-cf+.h.c", "build/libobs.graphics+.h.c", + "build/libobs.graphics-image-max-size+.h.c", "build/libobs.graphics-linear-srgb+.h.c", "build/libobs.graphics-stencil-adapter+.h.c", "build/libobs.graphics-texture-creators-fields+.h.c", "build/libobs.image-decode-adapter+.h.c", "build/libobs.media+.h.c", + "build/libobs.media-image-orientation-fields+.h.c", + "build/libobs.media-texture-file-data-convert+.h.c", + "build/libobs.media-texture-file-data-session+.h.c", "build/libobs.media-resampler-kernel+.h.c", "build/libobs.source-private-settings+.h.c" ] diff --git a/libobs/graphics/graphics-ffmpeg.c.h b/libobs/graphics/graphics-ffmpeg.c.h index 6be2622c77..13b59bd273 100644 --- a/libobs/graphics/graphics-ffmpeg.c.h +++ b/libobs/graphics/graphics-ffmpeg.c.h @@ -1,183 +1,4 @@ -#include "build/libobs-media.pch.h" - -static void *ffmpeg_image_copy_data_straight(struct ffmpeg_image *info, AVFrame *frame) -{ - const size_t linesize = (size_t)info->cx * 4; - const size_t totalsize = (size_t)info->cy * linesize; - void *data = bmalloc(totalsize); - - gs_ffmpeg_image_copy_rows(data, frame->data[0], (size_t)info->cx, (size_t)info->cy, (size_t)frame->linesize[0]); - - return data; -} - -static void *ffmpeg_image_orient(struct ffmpeg_image *info, void *in_data, int orient) -{ - const size_t sx = (size_t)info->cx; - const size_t sy = (size_t)info->cy; - uint8_t *data = NULL; - - if (!gs_ffmpeg_image_orient_needed(orient)) - return in_data; - - data = bmalloc(sx * 4 * sy); - - if (gs_ffmpeg_image_orient_swaps(orient)) { - info->cx = (int)sy; - info->cy = (int)sx; - } - - gs_ffmpeg_image_orient_copy(data, in_data, sx, sy, orient); - - bfree(in_data); - return data; -} - -/* graphics-ffmpeg-route.c gives the pixel formats, the texture formats and - * the alpha modes as their values. */ -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV420P == AV_PIX_FMT_YUV420P, "AV_PIX_FMT_YUV420P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV422P == AV_PIX_FMT_YUV422P, "AV_PIX_FMT_YUV422P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV444P == AV_PIX_FMT_YUV444P, "AV_PIX_FMT_YUV444P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV411P == AV_PIX_FMT_YUV411P, "AV_PIX_FMT_YUV411P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ420P == AV_PIX_FMT_YUVJ420P, "AV_PIX_FMT_YUVJ420P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ422P == AV_PIX_FMT_YUVJ422P, "AV_PIX_FMT_YUVJ422P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ444P == AV_PIX_FMT_YUVJ444P, "AV_PIX_FMT_YUVJ444P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_RGBA == AV_PIX_FMT_RGBA, "AV_PIX_FMT_RGBA"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_BGRA == AV_PIX_FMT_BGRA, "AV_PIX_FMT_BGRA"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV440P == AV_PIX_FMT_YUV440P, "AV_PIX_FMT_YUV440P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ440P == AV_PIX_FMT_YUVJ440P, "AV_PIX_FMT_YUVJ440P"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_RGBA64BE == AV_PIX_FMT_RGBA64BE, "AV_PIX_FMT_RGBA64BE"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_BGR0 == AV_PIX_FMT_BGR0, "AV_PIX_FMT_BGR0"); -_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ411P == AV_PIX_FMT_YUVJ411P, "AV_PIX_FMT_YUVJ411P"); -_Static_assert(GS_FFMPEG_IMAGE_GS_RGBA == GS_RGBA, "GS_RGBA"); -_Static_assert(GS_FFMPEG_IMAGE_GS_BGRX == GS_BGRX, "GS_BGRX"); -_Static_assert(GS_FFMPEG_IMAGE_GS_BGRA == GS_BGRA, "GS_BGRA"); -_Static_assert(GS_FFMPEG_IMAGE_GS_RGBA16 == GS_RGBA16, "GS_RGBA16"); -_Static_assert(GS_FFMPEG_IMAGE_ALPHA_STRAIGHT == GS_IMAGE_ALPHA_STRAIGHT, "GS_IMAGE_ALPHA_STRAIGHT"); -_Static_assert(GS_FFMPEG_IMAGE_ALPHA_PREMULTIPLY_SRGB == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB, - "GS_IMAGE_ALPHA_PREMULTIPLY_SRGB"); -_Static_assert(GS_FFMPEG_IMAGE_ALPHA_PREMULTIPLY == GS_IMAGE_ALPHA_PREMULTIPLY, "GS_IMAGE_ALPHA_PREMULTIPLY"); - -/* graphics-ffmpeg-rgba64.c gives the alpha modes as their values. */ -_Static_assert(GS_FFMPEG_RGBA64_ALPHA_STRAIGHT == GS_IMAGE_ALPHA_STRAIGHT, "GS_IMAGE_ALPHA_STRAIGHT"); -_Static_assert(GS_FFMPEG_RGBA64_ALPHA_PREMULTIPLY_SRGB == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB, - "GS_IMAGE_ALPHA_PREMULTIPLY_SRGB"); -_Static_assert(GS_FFMPEG_RGBA64_ALPHA_PREMULTIPLY == GS_IMAGE_ALPHA_PREMULTIPLY, "GS_IMAGE_ALPHA_PREMULTIPLY"); - -void *ffmpeg_image_reformat_frame(struct ffmpeg_image *info, AVFrame *frame, enum gs_image_alpha_mode alpha_mode) -{ - struct SwsContext *sws_ctx = NULL; - void *data = NULL; - int ret = 0; - - const int orient = ffmpeg_image_get_orientation(frame); - const struct gs_ffmpeg_image_plan plan = gs_ffmpeg_image_plan(info->format, (int)alpha_mode); - - switch (plan.route) { - case GS_FFMPEG_IMAGE_ROUTE_COPY: - data = ffmpeg_image_copy_data_straight(info, frame); - break; - case GS_FFMPEG_IMAGE_ROUTE_PREMULTIPLY_ROWS: { - const size_t linesize = (size_t)info->cx * 4; - const size_t totalsize = (size_t)info->cy * linesize; - data = bmalloc(totalsize); - const size_t src_linesize = (size_t)frame->linesize[0]; - const size_t min_line = linesize < src_linesize ? linesize : src_linesize; - uint8_t *dst = data; - const uint8_t *src = frame->data[0]; - const size_t row_elements = min_line >> 2; - if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_SRGB) { - for (int y = 0; y < info->cy; y++) { - gs_premultiply_xyza_srgb_loop_restrict(dst, src, row_elements); - dst += linesize; - src += src_linesize; - } - } else if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_LINEAR) { - for (int y = 0; y < info->cy; y++) { - gs_premultiply_xyza_loop_restrict(dst, src, row_elements); - dst += linesize; - src += src_linesize; - } - } - break; - } - case GS_FFMPEG_IMAGE_ROUTE_RGBA64: { - const size_t dst_linesize = (size_t)info->cx * 4; - data = bmalloc((size_t)info->cy * dst_linesize); - gs_ffmpeg_image_rgba64be_rows(data, frame->data[0], info->cx, info->cy, (size_t)frame->linesize[0], - (int)alpha_mode); - - info->format = (enum AVPixelFormat)plan.out_format; - break; - } - default: { - /* GS_FFMPEG_IMAGE_ROUTE_SWS */ - static const enum AVPixelFormat format = AV_PIX_FMT_BGRA; - - sws_ctx = sws_getContext(info->cx, info->cy, (enum AVPixelFormat)plan.sws_format, info->cx, info->cy, - format, SWS_POINT, NULL, NULL, NULL); - if (!sws_ctx) { - blog(LOG_WARNING, - "Failed to create scale context " - "for '%s'", - info->file); - goto fail; - } - - if (plan.full_range) { - /* What swscale sets up for a YUVJ format itself: - * full range in, its default (BT.601) matrix. */ - const int *coeff = sws_getCoefficients(SWS_CS_DEFAULT); - sws_setColorspaceDetails(sws_ctx, coeff, 1, coeff, 0, 0, 1 << 16, 1 << 16); - } - - uint8_t *pointers[4]; - int linesizes[4]; - ret = av_image_alloc(pointers, linesizes, info->cx, info->cy, format, 32); - if (ret < 0) { - blog(LOG_WARNING, "av_image_alloc failed for '%s': %s", info->file, av_err2str(ret)); - sws_freeContext(sws_ctx); - goto fail; - } - - ret = sws_scale(sws_ctx, (const uint8_t *const *)frame->data, frame->linesize, 0, info->cy, pointers, - linesizes); - sws_freeContext(sws_ctx); - - if (ret < 0) { - blog(LOG_WARNING, "sws_scale failed for '%s': %s", info->file, av_err2str(ret)); - av_freep(pointers); - goto fail; - } - - const size_t linesize = (size_t)info->cx * 4; - data = bmalloc((size_t)info->cy * linesize); - const uint8_t *src = pointers[0]; - uint8_t *dst = data; - for (size_t y = 0; y < (size_t)info->cy; y++) { - memcpy(dst, src, linesize); - dst += linesize; - src += linesizes[0]; - } - - av_freep(pointers); - - if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_SRGB) { - gs_premultiply_xyza_srgb_loop(data, (size_t)info->cx * (size_t)info->cy); - } else if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_LINEAR) { - gs_premultiply_xyza_loop(data, (size_t)info->cx * (size_t)info->cy); - } - - info->format = (enum AVPixelFormat)plan.out_format; - break; - } - } - - data = ffmpeg_image_orient(info, data, orient); - -fail: - return data; -} +#include "libobs/build/libobs-media.pch.h" void gs_init_image_deps(void); void gs_free_image_deps(void); diff --git a/libobs/graphics/graphics-image-max-size.c.h b/libobs/graphics/graphics-image-max-size.c.h new file mode 100644 index 0000000000..b4f670a9ad --- /dev/null +++ b/libobs/graphics/graphics-image-max-size.c.h @@ -0,0 +1,17 @@ +#include "libobs/build/libobs-graphics.pch.h" + +/* Shared by the real graphics lifecycle and CPU image loading. Keep the + * atomic state with its callback so standalone consumers need no runtime. */ +static volatile long image_max_texture_size; + +void gs_image_set_max_texture_size(uint32_t size); + +uint32_t gs_image_max_texture_size(void) +{ + return (uint32_t)os_atomic_load_long(&image_max_texture_size); +} + +void gs_image_set_max_texture_size(uint32_t size) +{ + os_atomic_set_long(&image_max_texture_size, (long)size); +} diff --git a/libobs/graphics/graphics-texture-file-data-adapter.c.h b/libobs/graphics/graphics-texture-file-data-adapter.c.h new file mode 100644 index 0000000000..7d95da2272 --- /dev/null +++ b/libobs/graphics/graphics-texture-file-data-adapter.c.h @@ -0,0 +1,49 @@ +#include "libobs/build/libobs-media.pch.h" + +/* Private C/Rust session ABI, deliberately opaque outside this file. */ +void *obs_texture_file_data_open(const char *file); +void *obs_texture_file_data_decode(void *session); +int obs_texture_file_data_format(void *session); +uint32_t obs_texture_file_data_width(void *session); +uint32_t obs_texture_file_data_height(void *session); +void obs_texture_file_data_close(void *session); + +/* Private opaque session: FFmpeg's layout never crosses the Rust boundary. + * Initialization already releases FFmpeg resources on failure. */ +void *obs_texture_file_data_open(const char *file) +{ + struct ffmpeg_image *image = bzalloc(sizeof(*image)); + if (!ffmpeg_image_init(image, file, gs_image_max_texture_size)) { + bfree(image); + return NULL; + } + return image; +} + +void *obs_texture_file_data_decode(void *session) +{ + struct gs_image_decode_context decode = {session, ffmpeg_image_too_large, ffmpeg_image_reformat_frame}; + return gs_ffmpeg_image_decode(&decode, (int)GS_IMAGE_ALPHA_STRAIGHT); +} + +int obs_texture_file_data_format(void *session) +{ + const struct ffmpeg_image *image = session; + return gs_ffmpeg_image_color_format(image->format); +} + +uint32_t obs_texture_file_data_width(void *session) +{ + return (uint32_t)((struct ffmpeg_image *)session)->cx; +} + +uint32_t obs_texture_file_data_height(void *session) +{ + return (uint32_t)((struct ffmpeg_image *)session)->cy; +} + +void obs_texture_file_data_close(void *session) +{ + ffmpeg_image_free(session); + bfree(session); +} diff --git a/libobs/graphics/graphics-texture-file-data-convert.c.h b/libobs/graphics/graphics-texture-file-data-convert.c.h new file mode 100644 index 0000000000..30c4f76036 --- /dev/null +++ b/libobs/graphics/graphics-texture-file-data-convert.c.h @@ -0,0 +1,180 @@ +#include "libobs/build/libobs-media.pch.h" + +static void *ffmpeg_image_copy_data_straight(struct ffmpeg_image *info, AVFrame *frame) +{ + const size_t linesize = (size_t)info->cx * 4; + const size_t totalsize = (size_t)info->cy * linesize; + void *data = bmalloc(totalsize); + + gs_ffmpeg_image_copy_rows(data, frame->data[0], (size_t)info->cx, (size_t)info->cy, (size_t)frame->linesize[0]); + + return data; +} + +static void *ffmpeg_image_orient(struct ffmpeg_image *info, void *in_data, int orient) +{ + const size_t sx = (size_t)info->cx; + const size_t sy = (size_t)info->cy; + uint8_t *data = NULL; + + if (!gs_ffmpeg_image_orient_needed(orient)) + return in_data; + + data = bmalloc(sx * 4 * sy); + + if (gs_ffmpeg_image_orient_swaps(orient)) { + info->cx = (int)sy; + info->cy = (int)sx; + } + + gs_ffmpeg_image_orient_copy(data, in_data, sx, sy, orient); + + bfree(in_data); + return data; +} + +/* graphics-ffmpeg-route.c gives the pixel formats, the texture formats and + * the alpha modes as their values. */ +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV420P == AV_PIX_FMT_YUV420P, "AV_PIX_FMT_YUV420P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV422P == AV_PIX_FMT_YUV422P, "AV_PIX_FMT_YUV422P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV444P == AV_PIX_FMT_YUV444P, "AV_PIX_FMT_YUV444P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV411P == AV_PIX_FMT_YUV411P, "AV_PIX_FMT_YUV411P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ420P == AV_PIX_FMT_YUVJ420P, "AV_PIX_FMT_YUVJ420P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ422P == AV_PIX_FMT_YUVJ422P, "AV_PIX_FMT_YUVJ422P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ444P == AV_PIX_FMT_YUVJ444P, "AV_PIX_FMT_YUVJ444P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_RGBA == AV_PIX_FMT_RGBA, "AV_PIX_FMT_RGBA"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_BGRA == AV_PIX_FMT_BGRA, "AV_PIX_FMT_BGRA"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUV440P == AV_PIX_FMT_YUV440P, "AV_PIX_FMT_YUV440P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ440P == AV_PIX_FMT_YUVJ440P, "AV_PIX_FMT_YUVJ440P"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_RGBA64BE == AV_PIX_FMT_RGBA64BE, "AV_PIX_FMT_RGBA64BE"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_BGR0 == AV_PIX_FMT_BGR0, "AV_PIX_FMT_BGR0"); +_Static_assert(GS_FFMPEG_IMAGE_PIX_FMT_YUVJ411P == AV_PIX_FMT_YUVJ411P, "AV_PIX_FMT_YUVJ411P"); +_Static_assert(GS_FFMPEG_IMAGE_GS_RGBA == GS_RGBA, "GS_RGBA"); +_Static_assert(GS_FFMPEG_IMAGE_GS_BGRX == GS_BGRX, "GS_BGRX"); +_Static_assert(GS_FFMPEG_IMAGE_GS_BGRA == GS_BGRA, "GS_BGRA"); +_Static_assert(GS_FFMPEG_IMAGE_GS_RGBA16 == GS_RGBA16, "GS_RGBA16"); +_Static_assert(GS_FFMPEG_IMAGE_ALPHA_STRAIGHT == GS_IMAGE_ALPHA_STRAIGHT, "GS_IMAGE_ALPHA_STRAIGHT"); +_Static_assert(GS_FFMPEG_IMAGE_ALPHA_PREMULTIPLY_SRGB == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB, + "GS_IMAGE_ALPHA_PREMULTIPLY_SRGB"); +_Static_assert(GS_FFMPEG_IMAGE_ALPHA_PREMULTIPLY == GS_IMAGE_ALPHA_PREMULTIPLY, "GS_IMAGE_ALPHA_PREMULTIPLY"); + +/* graphics-ffmpeg-rgba64.c gives the alpha modes as their values. */ +_Static_assert(GS_FFMPEG_RGBA64_ALPHA_STRAIGHT == GS_IMAGE_ALPHA_STRAIGHT, "GS_IMAGE_ALPHA_STRAIGHT"); +_Static_assert(GS_FFMPEG_RGBA64_ALPHA_PREMULTIPLY_SRGB == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB, + "GS_IMAGE_ALPHA_PREMULTIPLY_SRGB"); +_Static_assert(GS_FFMPEG_RGBA64_ALPHA_PREMULTIPLY == GS_IMAGE_ALPHA_PREMULTIPLY, "GS_IMAGE_ALPHA_PREMULTIPLY"); + +void *ffmpeg_image_reformat_frame(struct ffmpeg_image *info, AVFrame *frame, enum gs_image_alpha_mode alpha_mode) +{ + struct SwsContext *sws_ctx = NULL; + void *data = NULL; + int ret = 0; + + const int orient = ffmpeg_image_get_orientation(frame); + const struct gs_ffmpeg_image_plan plan = gs_ffmpeg_image_plan(info->format, (int)alpha_mode); + + switch (plan.route) { + case GS_FFMPEG_IMAGE_ROUTE_COPY: + data = ffmpeg_image_copy_data_straight(info, frame); + break; + case GS_FFMPEG_IMAGE_ROUTE_PREMULTIPLY_ROWS: { + const size_t linesize = (size_t)info->cx * 4; + const size_t totalsize = (size_t)info->cy * linesize; + data = bmalloc(totalsize); + const size_t src_linesize = (size_t)frame->linesize[0]; + const size_t min_line = linesize < src_linesize ? linesize : src_linesize; + uint8_t *dst = data; + const uint8_t *src = frame->data[0]; + const size_t row_elements = min_line >> 2; + if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_SRGB) { + for (int y = 0; y < info->cy; y++) { + gs_premultiply_xyza_srgb_loop_restrict(dst, src, row_elements); + dst += linesize; + src += src_linesize; + } + } else if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_LINEAR) { + for (int y = 0; y < info->cy; y++) { + gs_premultiply_xyza_loop_restrict(dst, src, row_elements); + dst += linesize; + src += src_linesize; + } + } + break; + } + case GS_FFMPEG_IMAGE_ROUTE_RGBA64: { + const size_t dst_linesize = (size_t)info->cx * 4; + data = bmalloc((size_t)info->cy * dst_linesize); + gs_ffmpeg_image_rgba64be_rows(data, frame->data[0], info->cx, info->cy, (size_t)frame->linesize[0], + (int)alpha_mode); + + info->format = (enum AVPixelFormat)plan.out_format; + break; + } + default: { + /* GS_FFMPEG_IMAGE_ROUTE_SWS */ + static const enum AVPixelFormat format = AV_PIX_FMT_BGRA; + + sws_ctx = sws_getContext(info->cx, info->cy, (enum AVPixelFormat)plan.sws_format, info->cx, info->cy, + format, SWS_POINT, NULL, NULL, NULL); + if (!sws_ctx) { + blog(LOG_WARNING, + "Failed to create scale context " + "for '%s'", + info->file); + goto fail; + } + + if (plan.full_range) { + /* What swscale sets up for a YUVJ format itself: + * full range in, its default (BT.601) matrix. */ + const int *coeff = sws_getCoefficients(SWS_CS_DEFAULT); + sws_setColorspaceDetails(sws_ctx, coeff, 1, coeff, 0, 0, 1 << 16, 1 << 16); + } + + uint8_t *pointers[4]; + int linesizes[4]; + ret = av_image_alloc(pointers, linesizes, info->cx, info->cy, format, 32); + if (ret < 0) { + blog(LOG_WARNING, "av_image_alloc failed for '%s': %s", info->file, av_err2str(ret)); + sws_freeContext(sws_ctx); + goto fail; + } + + ret = sws_scale(sws_ctx, (const uint8_t *const *)frame->data, frame->linesize, 0, info->cy, pointers, + linesizes); + sws_freeContext(sws_ctx); + + if (ret < 0) { + blog(LOG_WARNING, "sws_scale failed for '%s': %s", info->file, av_err2str(ret)); + av_freep(pointers); + goto fail; + } + + const size_t linesize = (size_t)info->cx * 4; + data = bmalloc((size_t)info->cy * linesize); + const uint8_t *src = pointers[0]; + uint8_t *dst = data; + for (size_t y = 0; y < (size_t)info->cy; y++) { + memcpy(dst, src, linesize); + dst += linesize; + src += linesizes[0]; + } + + av_freep(pointers); + + if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_SRGB) { + gs_premultiply_xyza_srgb_loop(data, (size_t)info->cx * (size_t)info->cy); + } else if (plan.premultiply == GS_FFMPEG_IMAGE_PREMULTIPLY_LINEAR) { + gs_premultiply_xyza_loop(data, (size_t)info->cx * (size_t)info->cy); + } + + info->format = (enum AVPixelFormat)plan.out_format; + break; + } + } + + data = ffmpeg_image_orient(info, data, orient); + +fail: + return data; +} diff --git a/libobs/graphics/graphics.c.h b/libobs/graphics/graphics.c.h index bc9e72434b..7c20491b66 100644 --- a/libobs/graphics/graphics.c.h +++ b/libobs/graphics/graphics.c.h @@ -200,12 +200,8 @@ static bool graphics_init(struct graphics_subsystem *graphics) return true; } -static volatile long max_texture_size; - -uint32_t gs_image_max_texture_size(void) -{ - return (uint32_t)os_atomic_load_long(&max_texture_size); -} +/* Private atomic image limit; independently linkable without this runtime. */ +extern void gs_image_set_max_texture_size(uint32_t size); int gs_create(graphics_t **pgraphics, const char *module, uint32_t adapter) { @@ -233,7 +229,7 @@ int gs_create(graphics_t **pgraphics, const char *module, uint32_t adapter) goto error; } - os_atomic_set_long(&max_texture_size, (long)obs_graphics_device_max_texture_size(graphics)); + gs_image_set_max_texture_size(obs_graphics_device_max_texture_size(graphics)); *pgraphics = graphics; return errcode; @@ -270,7 +266,7 @@ void gs_destroy(graphics_t *graphics) graphics->exports.gs_vertexbuffer_destroy(graphics->sprite_buffers[i - 1]); graphics->exports.gs_vertexbuffer_destroy(graphics->immediate_vertbuffer); graphics->exports.device_destroy(graphics->device); - os_atomic_set_long(&max_texture_size, 0); + gs_image_set_max_texture_size(0); thread_graphics = NULL; } diff --git a/test/cmocka/test_image_max_texture_size.c b/test/cmocka/test_image_max_texture_size.c new file mode 100644 index 0000000000..5651302ffc --- /dev/null +++ b/test/cmocka/test_image_max_texture_size.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +/* Private production seam. Deliberately link only its real C unit: neither + * libobs nor a fake graphics runtime may supply this callback. The unchanged + * test_graphics_viewport additionally observes publication by gs_create and + * reset by gs_destroy through the public image loader in both OFF/ON modes. */ +uint32_t gs_image_max_texture_size(void); +void gs_image_set_max_texture_size(uint32_t size); + +static void cached_limit(void **state) +{ + (void)state; + assert_int_equal(gs_image_max_texture_size(), 0); + const uint32_t limits[] = {1, 7, 8192, 16384, UINT32_MAX, 0}; + for (size_t i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) { + gs_image_set_max_texture_size(limits[i]); + assert_int_equal(gs_image_max_texture_size(), limits[i]); + /* Loading the callback repeatedly must not consume or change it. */ + assert_int_equal(gs_image_max_texture_size(), limits[i]); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = {cmocka_unit_test(cached_limit)}; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_image_max_texture_size.cmake b/test/cmocka/tests.d/test_image_max_texture_size.cmake new file mode 100644 index 0000000000..a6f01cb08b --- /dev/null +++ b/test/cmocka/tests.d/test_image_max_texture_size.cmake @@ -0,0 +1,17 @@ +# This executable must link without libobs or a graphics backend. Keep the +# atomic state and callback together in the actual production unity member. +add_executable( + test_image_max_texture_size + test_image_max_texture_size.c + "${CMAKE_SOURCE_DIR}/libobs/build/libobs.graphics-image-max-size+.h.c" +) +target_include_directories( + test_image_max_texture_size + PRIVATE ${CMOCKA_INCLUDE_DIR} "${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/libobs" +) +target_link_libraries(test_image_max_texture_size PRIVATE ${CMOCKA_LIBRARIES} SIMDe::SIMDe Threads::Threads) +if(OS_WINDOWS) + target_include_directories(test_image_max_texture_size SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/deps/w32-pthreads") +endif() +add_dependencies(cmocka-tests test_image_max_texture_size) +add_test(NAME test_image_max_texture_size COMMAND test_image_max_texture_size)