]> git.sesse.net Git - nageru/blob - futatabi/flags.cpp
Only enable the queue button if we can actually queue something.
[nageru] / futatabi / flags.cpp
1 #include "flags.h"
2
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <utility>
8
9 using namespace std;
10
11 Flags global_flags;
12 int flow_initialized_interpolation_quality;
13
14 // Long options that have no corresponding short option.
15 enum LongOption {
16         OPTION_HELP = 1000,
17         OPTION_SLOW_DOWN_INPUT = 1001,
18         OPTION_HTTP_PORT = 1002,
19         OPTION_TALLY_URL = 1003,
20         OPTION_CUE_POINT_PADDING = 1004
21 };
22
23 void usage()
24 {
25         fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n");
26         fprintf(stderr, "\n");
27         fprintf(stderr, "      --help                      print usage information\n");
28         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
29         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
30         fprintf(stderr, "  -r, --frame-rate NUM[/NUM]      output frame rate, as a float or fraction\n");
31         fprintf(stderr, "                                    (default 60000/1001 ~= 59.94)\n");
32         fprintf(stderr, "      --slow-down-input           slow down input to realtime (default on if no\n");
33         fprintf(stderr, "                                    source URL given)\n");
34         fprintf(stderr, "  -q, --interpolation-quality N   0 = off\n");
35         fprintf(stderr, "                                  1 = fastest\n");
36         fprintf(stderr, "                                  2 = default (realtime 720p on fast embedded GPUs)\n");
37         fprintf(stderr, "                                  3 = good (realtime 720p on GTX 970 or so)\n");
38         fprintf(stderr, "                                  4 = best (not realtime on any current GPU)\n");
39         fprintf(stderr, "      --cue-point-padding SECS    move cue-in/cue-out N seconds earlier/later on set\n");
40         fprintf(stderr, "  -d, --working-directory DIR     where to store frames and database\n");
41         fprintf(stderr, "      --http-port PORT            which port to listen on for output\n");
42         fprintf(stderr, "      --tally-url URL             URL to get tally color from (polled every 100 ms)\n");
43 }
44
45 void parse_flags(int argc, char *const argv[])
46 {
47         static const option long_options[] = {
48                 { "help", no_argument, 0, OPTION_HELP },
49                 { "width", required_argument, 0, 'w' },
50                 { "height", required_argument, 0, 'h' },
51                 { "frame-rate", required_argument, 0, 'r' },
52                 { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT },
53                 { "interpolation-quality", required_argument, 0, 'q' },
54                 { "working-directory", required_argument, 0, 'd' },
55                 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
56                 { "tally-url", required_argument, 0, OPTION_TALLY_URL },
57                 { "cue-point-padding", required_argument, 0, OPTION_CUE_POINT_PADDING },
58                 { 0, 0, 0, 0 }
59         };
60         for (;;) {
61                 int option_index = 0;
62                 int c = getopt_long(argc, argv, "w:h:r:q:d:", long_options, &option_index);
63
64                 if (c == -1) {
65                         break;
66                 }
67                 switch (c) {
68                 case 'w':
69                         global_flags.width = atoi(optarg);
70                         break;
71                 case 'h':
72                         global_flags.height = atoi(optarg);
73                         break;
74                 case 'r': {
75                         double num, den;
76                         if (sscanf(optarg, "%lf/%lf", &num, &den) == 2) {
77                                 global_flags.output_framerate = num / den;
78                         } else if (sscanf(optarg, "%lf", &num) == 1) {
79                                 global_flags.output_framerate = num;
80                         } else {
81                                 fprintf(stderr, "Invalid frame rate given (must be on the form N or N/M)\n");
82                                 exit(1);
83                         }
84                         break;
85                 }
86                 case OPTION_SLOW_DOWN_INPUT:
87                         global_flags.slow_down_input = true;
88                         break;
89                 case 'q':
90                         global_flags.interpolation_quality = atoi(optarg);
91                         global_flags.interpolation_quality_set = true;
92                         break;
93                 case 'd':
94                         global_flags.working_directory = optarg;
95                         break;
96                 case OPTION_HTTP_PORT:
97                         global_flags.http_port = atoi(optarg);
98                         break;
99                 case OPTION_TALLY_URL:
100                         global_flags.tally_url = optarg;
101                         break;
102                 case OPTION_CUE_POINT_PADDING:
103                         global_flags.cue_point_padding_seconds = atof(optarg);
104                         global_flags.cue_point_padding_set = true;
105                         break;
106                 case OPTION_HELP:
107                         usage();
108                         exit(0);
109                 default:
110                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
111                         fprintf(stderr, "\n");
112                         usage();
113                         exit(1);
114                 }
115         }
116
117         if (global_flags.interpolation_quality < 0 || global_flags.interpolation_quality > 4) {
118                 fprintf(stderr, "Interpolation quality must be 0, 1, 2, 3 or 4.\n");
119                 usage();
120                 exit(1);
121         }
122         if (global_flags.cue_point_padding_seconds < 0.0) {
123                 fprintf(stderr, "Cue point padding cannot be negative.\n");
124                 usage();
125                 exit(1);
126         }
127 }