obs-x264: Move options parser to its own lib

master
jp9000 2021-10-23 05:30:46 -07:00
parent 0c1524b53c
commit 6534bf5834
8 changed files with 71 additions and 68 deletions

1
deps/CMakeLists.txt vendored
View File

@ -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)

17
deps/opts-parser/CMakeLists.txt vendored Normal file
View File

@ -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)

View File

@ -4,7 +4,7 @@
#include <string.h>
#include <util/bmem.h>
#include <util/dstr.h>
#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);

19
deps/opts-parser/opts-parser.h vendored Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <stddef.h>
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);

View File

@ -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)

View File

@ -1,19 +0,0 @@
#pragma once
#include <stddef.h>
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);

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "obs-x264-options.h"
#include <opts-parser.h>
#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;
}

View File

@ -22,7 +22,7 @@
#include <util/darray.h>
#include <util/platform.h>
#include <obs-module.h>
#include "obs-x264-options.h"
#include <opts-parser.h>
#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);