From 6534bf58340536c3861c04a10759c4827a34dab3 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Sat, 23 Oct 2021 05:30:46 -0700 Subject: [PATCH] obs-x264: Move options parser to its own lib --- deps/CMakeLists.txt | 1 + deps/opts-parser/CMakeLists.txt | 17 ++++++++++ .../opts-parser/opts-parser.c | 14 ++++---- deps/opts-parser/opts-parser.h | 19 +++++++++++ plugins/obs-x264/CMakeLists.txt | 18 ++--------- plugins/obs-x264/obs-x264-options.h | 19 ----------- plugins/obs-x264/obs-x264-test.c | 32 +++++++++---------- plugins/obs-x264/obs-x264.c | 19 ++++++----- 8 files changed, 71 insertions(+), 68 deletions(-) create mode 100644 deps/opts-parser/CMakeLists.txt rename plugins/obs-x264/obs-x264-options.c => deps/opts-parser/opts-parser.c (82%) create mode 100644 deps/opts-parser/opts-parser.h delete mode 100644 plugins/obs-x264/obs-x264-options.h diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index 886241e31..bd93a6e80 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -13,6 +13,7 @@ endif() add_subdirectory(media-playback) add_subdirectory(file-updater) add_subdirectory(obs-scripting) +add_subdirectory(opts-parser) if(WIN32) add_subdirectory(blake2) diff --git a/deps/opts-parser/CMakeLists.txt b/deps/opts-parser/CMakeLists.txt new file mode 100644 index 000000000..27e9a4c30 --- /dev/null +++ b/deps/opts-parser/CMakeLists.txt @@ -0,0 +1,17 @@ +project(opts-parser) + +set(opts-parser_SOURCES + opts-parser.c) +set(opts-parser_HEADERS + opts-parser.h) + +add_library(opts-parser STATIC + ${opts-parser_SOURCES} + ${opts-parser_HEADERS}) +target_include_directories(opts-parser + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(opts-parser + libobs) +set_target_properties(opts-parser PROPERTIES + FOLDER "deps" + POSITION_INDEPENDENT_CODE ON) diff --git a/plugins/obs-x264/obs-x264-options.c b/deps/opts-parser/opts-parser.c similarity index 82% rename from plugins/obs-x264/obs-x264-options.c rename to deps/opts-parser/opts-parser.c index 39373aa83..1e3f3d225 100644 --- a/plugins/obs-x264/obs-x264-options.c +++ b/deps/opts-parser/opts-parser.c @@ -4,7 +4,7 @@ #include #include #include -#include "obs-x264-options.h" +#include "opts-parser.h" static bool getparam(const char *param, char **name, const char **value) { @@ -22,11 +22,11 @@ static bool getparam(const char *param, char **name, const char **value) return true; } -struct obs_x264_options obs_x264_parse_options(const char *options_string) +struct obs_options obs_parse_options(const char *options_string) { char **input_words = strlist_split(options_string, ' ', false); if (!input_words) { - return (struct obs_x264_options){ + return (struct obs_options){ .count = 0, .options = NULL, .ignored_word_count = 0, @@ -40,9 +40,9 @@ struct obs_x264_options obs_x264_parse_options(const char *options_string) char **ignored_words = bmalloc(input_option_count * sizeof(*ignored_words)); char **ignored_word = ignored_words; - struct obs_x264_option *out_options = + struct obs_option *out_options = bmalloc(input_option_count * sizeof(*out_options)); - struct obs_x264_option *out_option = out_options; + struct obs_option *out_option = out_options; for (char **input_word = input_words; *input_word; ++input_word) { if (getparam(*input_word, &out_option->name, (const char **)&out_option->value)) { @@ -52,7 +52,7 @@ struct obs_x264_options obs_x264_parse_options(const char *options_string) ++ignored_word; } } - return (struct obs_x264_options){ + return (struct obs_options){ .count = out_option - out_options, .options = out_options, .ignored_word_count = ignored_word - ignored_words, @@ -61,7 +61,7 @@ struct obs_x264_options obs_x264_parse_options(const char *options_string) }; } -void obs_x264_free_options(struct obs_x264_options options) +void obs_free_options(struct obs_options options) { for (size_t i = 0; i < options.count; ++i) { bfree(options.options[i].name); diff --git a/deps/opts-parser/opts-parser.h b/deps/opts-parser/opts-parser.h new file mode 100644 index 000000000..5a228f8d2 --- /dev/null +++ b/deps/opts-parser/opts-parser.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +struct obs_option { + char *name; + char *value; +}; + +struct obs_options { + size_t count; + struct obs_option *options; + size_t ignored_word_count; + char **ignored_words; + char **input_words; +}; + +struct obs_options obs_parse_options(const char *options_string); +void obs_free_options(struct obs_options options); diff --git a/plugins/obs-x264/CMakeLists.txt b/plugins/obs-x264/CMakeLists.txt index c9f75d979..8c8100fa2 100644 --- a/plugins/obs-x264/CMakeLists.txt +++ b/plugins/obs-x264/CMakeLists.txt @@ -4,20 +4,6 @@ find_package(Libx264 REQUIRED) include_directories(${LIBX264_INCLUDE_DIRS}) add_definitions(${LIBX264_DEFINITIONS}) -set(obs-x264-util_HEADERS - obs-x264-options.h) - -set(obs-x264-util_SOURCES - obs-x264-options.c) - -add_library(obs-x264-util STATIC - ${obs-x264-util_HEADERS} - ${obs-x264-util_SOURCES}) -target_link_libraries(obs-x264-util PRIVATE libobs) -set_target_properties(obs-x264-util PROPERTIES - FOLDER "plugins" - POSITION_INDEPENDENT_CODE ON) - set(obs-x264_SOURCES obs-x264.c obs-x264-plugin-main.c) @@ -34,7 +20,7 @@ add_library(obs-x264 MODULE ${obs-x264_SOURCES}) target_link_libraries(obs-x264 libobs - obs-x264-util + opts-parser ${LIBX264_LIBRARIES}) set_target_properties(obs-x264 PROPERTIES FOLDER "plugins") @@ -42,5 +28,5 @@ install_obs_plugin_with_data(obs-x264 data) add_executable(obs-x264-test obs-x264-test.c) set_target_properties(obs-x264-test PROPERTIES FOLDER "plugins") -target_link_libraries(obs-x264-test PRIVATE libobs obs-x264-util) +target_link_libraries(obs-x264-test PRIVATE libobs opts-parser) add_test(NAME obs-x264-test COMMAND obs-x264-test) diff --git a/plugins/obs-x264/obs-x264-options.h b/plugins/obs-x264/obs-x264-options.h deleted file mode 100644 index 12c0674e9..000000000 --- a/plugins/obs-x264/obs-x264-options.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -struct obs_x264_option { - char *name; - char *value; -}; - -struct obs_x264_options { - size_t count; - struct obs_x264_option *options; - size_t ignored_word_count; - char **ignored_words; - char **input_words; -}; - -struct obs_x264_options obs_x264_parse_options(const char *options_string); -void obs_x264_free_options(struct obs_x264_options options); diff --git a/plugins/obs-x264/obs-x264-test.c b/plugins/obs-x264/obs-x264-test.c index e64e824be..10a9d4b56 100644 --- a/plugins/obs-x264/obs-x264-test.c +++ b/plugins/obs-x264/obs-x264-test.c @@ -1,7 +1,7 @@ #include #include #include -#include "obs-x264-options.h" +#include #define CHECK(condition) \ do { \ @@ -12,38 +12,38 @@ } \ } while (0) -static void test_obs_x264_parse_options() +static void test_obs_parse_options() { - struct obs_x264_options options; + struct obs_options options; - options = obs_x264_parse_options(NULL); + options = obs_parse_options(NULL); CHECK(options.count == 0); CHECK(options.ignored_word_count == 0); - obs_x264_free_options(options); + obs_free_options(options); - options = obs_x264_parse_options(""); + options = obs_parse_options(""); CHECK(options.count == 0); CHECK(options.ignored_word_count == 0); - obs_x264_free_options(options); + obs_free_options(options); - options = obs_x264_parse_options("ref=3"); + options = obs_parse_options("ref=3"); CHECK(options.count == 1); CHECK(strcmp(options.options[0].name, "ref") == 0); CHECK(strcmp(options.options[0].value, "3") == 0); CHECK(options.ignored_word_count == 0); - obs_x264_free_options(options); + obs_free_options(options); - options = obs_x264_parse_options("ref=3 bframes=8"); + options = obs_parse_options("ref=3 bframes=8"); CHECK(options.count == 2); CHECK(strcmp(options.options[0].name, "ref") == 0); CHECK(strcmp(options.options[0].value, "3") == 0); CHECK(strcmp(options.options[1].name, "bframes") == 0); CHECK(strcmp(options.options[1].value, "8") == 0); CHECK(options.ignored_word_count == 0); - obs_x264_free_options(options); + obs_free_options(options); // Invalid options are ignored. - options = obs_x264_parse_options( + options = obs_parse_options( "ref=3 option_with_no_equal_sign bframes=8 1234"); CHECK(options.count == 2); CHECK(strcmp(options.options[0].name, "ref") == 0); @@ -54,21 +54,21 @@ static void test_obs_x264_parse_options() CHECK(strcmp(options.ignored_words[0], "option_with_no_equal_sign") == 0); CHECK(strcmp(options.ignored_words[1], "1234") == 0); - obs_x264_free_options(options); + obs_free_options(options); // Extra whitespace is ignored between and around options. - options = obs_x264_parse_options(" ref=3 bframes=8 "); + options = obs_parse_options(" ref=3 bframes=8 "); CHECK(options.count == 2); CHECK(strcmp(options.options[0].name, "ref") == 0); CHECK(strcmp(options.options[0].value, "3") == 0); CHECK(strcmp(options.options[1].name, "bframes") == 0); CHECK(strcmp(options.options[1].value, "8") == 0); CHECK(options.ignored_word_count == 0); - obs_x264_free_options(options); + obs_free_options(options); } int main() { - test_obs_x264_parse_options(); + test_obs_parse_options(); return 0; } diff --git a/plugins/obs-x264/obs-x264.c b/plugins/obs-x264/obs-x264.c index f31220cef..22b8303d1 100644 --- a/plugins/obs-x264/obs-x264.c +++ b/plugins/obs-x264/obs-x264.c @@ -22,7 +22,7 @@ #include #include #include -#include "obs-x264-options.h" +#include #ifndef _STDINT_H_INCLUDED #define _STDINT_H_INCLUDED @@ -265,7 +265,7 @@ static const char *validate(struct obs_x264 *obsx264, const char *val, } static void override_base_param(struct obs_x264 *obsx264, - struct obs_x264_option option, char **preset, + struct obs_option option, char **preset, char **profile, char **tune) { const char *name = option.name; @@ -297,7 +297,7 @@ static void override_base_param(struct obs_x264 *obsx264, } static inline void override_base_params(struct obs_x264 *obsx264, - const struct obs_x264_options *options, + const struct obs_options *options, char **preset, char **profile, char **tune) { @@ -308,8 +308,7 @@ static inline void override_base_params(struct obs_x264 *obsx264, #define OPENCL_ALIAS "opencl_is_experimental_and_potentially_unstable" -static inline void set_param(struct obs_x264 *obsx264, - struct obs_x264_option option) +static inline void set_param(struct obs_x264 *obsx264, struct obs_option option) { const char *name = option.name; const char *val = option.value; @@ -384,7 +383,7 @@ enum rate_control { }; static void update_params(struct obs_x264 *obsx264, obs_data_t *settings, - const struct obs_x264_options *options, bool update) + const struct obs_options *options, bool update) { video_t *video = obs_encoder_video(obsx264->encoder); const struct video_output_info *voi = video_output_get_info(video); @@ -555,7 +554,7 @@ static void update_params(struct obs_x264 *obsx264, obs_data_t *settings, } static void log_custom_options(struct obs_x264 *obsx264, - const struct obs_x264_options *options) + const struct obs_options *options) { if (options->count == 0) { return; @@ -590,8 +589,8 @@ static bool update_settings(struct obs_x264 *obsx264, obs_data_t *settings, char *preset = bstrdup(obs_data_get_string(settings, "preset")); char *profile = bstrdup(obs_data_get_string(settings, "profile")); char *tune = bstrdup(obs_data_get_string(settings, "tune")); - struct obs_x264_options options = obs_x264_parse_options( - obs_data_get_string(settings, "x264opts")); + struct obs_options options = + obs_parse_options(obs_data_get_string(settings, "x264opts")); bool repeat_headers = obs_data_get_bool(settings, "repeat_headers"); bool success = true; @@ -629,7 +628,7 @@ static bool update_settings(struct obs_x264 *obsx264, obs_data_t *settings, apply_x264_profile(obsx264, profile); } - obs_x264_free_options(options); + obs_free_options(options); bfree(preset); bfree(profile); bfree(tune);