Bladeren bron

(#88) Implement video_params_from_file

rexim 5 jaren geleden
bovenliggende
commit
38f645271c
4 gewijzigde bestanden met toevoegingen van 100 en 31 verwijderingen
  1. 12 1
      src/vodus_image32.cpp
  2. 5 30
      src/vodus_main.cpp
  3. 80 0
      src/vodus_video_params.cpp
  4. 3 0
      src/vodus_video_params.hpp

+ 12 - 1
src/vodus_image32.cpp

@@ -201,7 +201,18 @@ void slap_image32_onto_image32(Image32 dst, Image32 src,
     }
 }
 
-// TODO(#97): gif rendering does not handle disposal flags correctly
+Maybe<Pixel32> hexstr_as_pixel32(String_View hexstr)
+{
+    if (hexstr.count != 8) return {};
+
+    Pixel32 result = {};
+    unwrap_into(result.r, hexstr.subview(0, 2).from_hex<uint8_t>());
+    unwrap_into(result.g, hexstr.subview(2, 2).from_hex<uint8_t>());
+    unwrap_into(result.b, hexstr.subview(4, 2).from_hex<uint8_t>());
+    unwrap_into(result.a, hexstr.subview(6, 2).from_hex<uint8_t>());
+    return {true, result};
+}
+
 Image32 load_image32_from_savedimage(GifFileType *gif_file,
                                      size_t index,
                                      GraphicsControlBlock gcb,

+ 5 - 30
src/vodus_main.cpp

@@ -440,47 +440,22 @@ void usage(FILE *stream)
                     "                                    Examples: ffffffff, 000000FF, A0eA0000");
 }
 
-Maybe<Pixel32> hexstr_as_pixel32(String_View hexstr)
+int main()
 {
-    if (hexstr.count != 8) return {};
-
-    Pixel32 result = {};
-    unwrap_into(result.r, hexstr.subview(0, 2).from_hex<uint8_t>());
-    unwrap_into(result.g, hexstr.subview(2, 2).from_hex<uint8_t>());
-    unwrap_into(result.b, hexstr.subview(4, 2).from_hex<uint8_t>());
-    unwrap_into(result.a, hexstr.subview(6, 2).from_hex<uint8_t>());
-    return {true, result};
-}
+    auto params = video_params_from_file("video.params");
+    println(stdout, params);
 
-int main_()
-{
-    void *iterator = NULL;
-    const AVCodec *codec = NULL;
-    println(stdout, "Probably available codecs:");
-    while ((codec = av_codec_iterate(&iterator))) {
-        if (avcodec_find_encoder_by_name(codec->name)) {
-            println(stdout, "  ", codec->name);
-        }
-    }
     return 0;
 }
 
-int main(int argc, char *argv[])
+int main_(int argc, char *argv[])
 {
     const char *log_filepath = nullptr;
     const char *face_filepath = nullptr;
     const char *output_filepath = nullptr;
     size_t messages_limit = VODUS_MESSAGES_CAPACITY;
 
-    Video_Params params = {};
-    params.fps               = 60;
-    params.width             = 1920;
-    params.height            = 1080;
-    params.font_size         = 128;
-    params.background_color = {32, 32, 32, 255};
-    params.nickname_color   = {255, 100, 100, 255};
-    params.text_color       = {200, 200, 200, 255};
-    params.bitrate           = 400'000;
+    Video_Params params = default_video_params();
 
     for (int i = 1; i < argc;) {
         const auto arg = cstr_as_string_view(argv[i]);

+ 80 - 0
src/vodus_video_params.cpp

@@ -13,3 +13,83 @@ void print1(FILE *stream, Video_Params params)
     println(stream, "    .bitrate = ", params.bitrate, ",");
     print(stream, "}");
 }
+
+Video_Params default_video_params() {
+    Video_Params params = {};
+    params.fps               = 60;
+    params.width             = 1920;
+    params.height            = 1080;
+    params.font_size         = 128;
+    params.background_color = {32, 32, 32, 255};
+    params.nickname_color   = {255, 100, 100, 255};
+    params.text_color       = {200, 200, 200, 255};
+    params.bitrate           = 400'000;
+    return params;
+}
+
+#define expect_into(lvalue, maybe, message, ...)    \
+    do {                                            \
+        auto maybe_var = (maybe);                   \
+        if (!maybe_var.has_value) {                 \
+            println(stderr, message, __VA_ARGS__);  \
+            abort();                                \
+        }                                           \
+        (lvalue) = maybe_var.unwrap;                \
+    } while (0)
+
+
+Video_Params video_params_from_file(const char *filepath)
+{
+    Video_Params result = default_video_params();
+    String_View content = {};
+    expect_into(
+        content, read_file_as_string_view(filepath),
+        "Could not read file `", filepath, "`");
+
+    for (int line_number = 1; content.count > 0; ++line_number) {
+        auto line = content.chop_by_delim('\n').trim();
+        if (line.count != 0) {
+            auto key = line.chop_by_delim('=').trim();
+            auto value = line.trim();
+
+            if ("fps"_sv == key) {
+                expect_into(
+                    result.fps, value.as_integer<size_t>(),
+                    filepath, ":", line_number, ": `", value, "` is not a number");
+            } else if ("width"_sv == key) {
+                expect_into(
+                    result.width, value.as_integer<size_t>(),
+                    filepath, ":", line_number, ": `", value, "` is not a number");
+            } else if ("height"_sv == key) {
+                expect_into(
+                    result.height, value.as_integer<size_t>(),
+                    filepath, ":", line_number, ": `", value, "` is not a number");
+            } else if ("font_size"_sv == key) {
+                expect_into(
+                    result.font_size, value.as_integer<size_t>(),
+                    filepath, ":", line_number, ": `", value, "` is not a number");
+            } else if ("background_color"_sv == key) {
+                expect_into(
+                    result.background_color, hexstr_as_pixel32(value),
+                    filepath, ":", line_number, ": `", value, "` is not a color");
+            } else if ("nickname_color"_sv == key) {
+                expect_into(
+                    result.nickname_color, hexstr_as_pixel32(value),
+                    filepath, ":", line_number, ": `", value, "` is not a color");
+            } else if ("text_color"_sv == key) {
+                expect_into(
+                    result.text_color, hexstr_as_pixel32(value),
+                    filepath, ":", line_number, ": `", value, "` is not a color");
+            } else if ("bitrate"_sv == key) {
+                expect_into(
+                    result.bitrate, value.as_integer<int>(),
+                    filepath, ":", line_number, ": `", value, "` is not a number");
+            } else {
+                println(stderr, "Unknown Video Parameter `", key, "`");
+                abort();
+            }
+        }
+    }
+
+    return result;
+}

+ 3 - 0
src/vodus_video_params.hpp

@@ -15,4 +15,7 @@ struct Video_Params
 
 void print1(FILE *stream, Video_Params params);
 
+Video_Params default_video_params();
+Video_Params video_params_from_file(const char *filepath);
+
 #endif  // VODUS_VIDEO_PARAMS_HPP_