]> git.sesse.net Git - nageru/blob - futatabi/flags.cpp
Fix an issue where scrubbing in Futatabi could lock up if there were no inputs going...
[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_IN_POINT_PADDING = 1004,
21         OPTION_CUE_OUT_POINT_PADDING = 1005,
22         OPTION_MIDI_MAPPING = 1006
23 };
24
25 void usage()
26 {
27         fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n");
28         fprintf(stderr, "\n");
29         fprintf(stderr, "      --help                      print usage information\n");
30         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
31         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
32         fprintf(stderr, "  -r, --frame-rate NUM[/NUM]      output frame rate, as a float or fraction\n");
33         fprintf(stderr, "                                    (default 60000/1001 ~= 59.94)\n");
34         fprintf(stderr, "      --slow-down-input           slow down input to realtime (default on if no\n");
35         fprintf(stderr, "                                    source URL given)\n");
36         fprintf(stderr, "  -q, --interpolation-quality N   0 = off\n");
37         fprintf(stderr, "                                  1 = fastest\n");
38         fprintf(stderr, "                                  2 = default (realtime 720p on fast embedded GPUs)\n");
39         fprintf(stderr, "                                  3 = good (realtime 720p on GTX 970 or so)\n");
40         fprintf(stderr, "                                  4 = best (not realtime on any current GPU)\n");
41         fprintf(stderr, "      --cue-in-point-padding SECS   move cue-in N seconds earlier on set\n");
42         fprintf(stderr, "      --cue-out-point-padding SECS  move cue-out N seconds later on set\n");
43         fprintf(stderr, "  -d, --working-directory DIR     where to store frames and database\n");
44         fprintf(stderr, "      --http-port PORT            which port to listen on for output\n");
45         fprintf(stderr, "      --tally-url URL             URL to get tally color from (polled every 100 ms)\n");
46         fprintf(stderr, "      --midi-mapping=FILE         start with the given MIDI controller mapping\n");
47         fprintf(stderr, "  -l  --source-label NUM:LABEL    label source NUM as LABEL, if visible\n");
48 }
49
50 void parse_flags(int argc, char *const argv[])
51 {
52         static const option long_options[] = {
53                 { "help", no_argument, 0, OPTION_HELP },
54                 { "width", required_argument, 0, 'w' },
55                 { "height", required_argument, 0, 'h' },
56                 { "frame-rate", required_argument, 0, 'r' },
57                 { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT },
58                 { "interpolation-quality", required_argument, 0, 'q' },
59                 { "working-directory", required_argument, 0, 'd' },
60                 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
61                 { "tally-url", required_argument, 0, OPTION_TALLY_URL },
62                 { "cue-in-point-padding", required_argument, 0, OPTION_CUE_IN_POINT_PADDING },
63                 { "cue-out-point-padding", required_argument, 0, OPTION_CUE_OUT_POINT_PADDING },
64                 { "midi-mapping", required_argument, 0, OPTION_MIDI_MAPPING },
65                 { "source-label", required_argument, 0, 'l' },
66                 { 0, 0, 0, 0 }
67         };
68         for (;;) {
69                 int option_index = 0;
70                 int c = getopt_long(argc, argv, "w:h:r:q:d:l:", long_options, &option_index);
71
72                 if (c == -1) {
73                         break;
74                 }
75                 switch (c) {
76                 case 'w':
77                         global_flags.width = atoi(optarg);
78                         break;
79                 case 'h':
80                         global_flags.height = atoi(optarg);
81                         break;
82                 case 'r': {
83                         double num, den;
84                         if (sscanf(optarg, "%lf/%lf", &num, &den) == 2) {
85                                 global_flags.output_framerate = num / den;
86                         } else if (sscanf(optarg, "%lf", &num) == 1) {
87                                 global_flags.output_framerate = num;
88                         } else {
89                                 fprintf(stderr, "Invalid frame rate given (must be on the form N or N/M)\n");
90                                 exit(1);
91                         }
92                         break;
93                 }
94                 case OPTION_SLOW_DOWN_INPUT:
95                         global_flags.slow_down_input = true;
96                         break;
97                 case 'q':
98                         global_flags.interpolation_quality = atoi(optarg);
99                         global_flags.interpolation_quality_set = true;
100                         break;
101                 case 'd':
102                         global_flags.working_directory = optarg;
103                         break;
104                 case 'l': {
105                         int prefix_len;
106                         unsigned channel_idx;
107                         if (sscanf(optarg, "%u:%n", &channel_idx, &prefix_len) == 1) {
108                                 const char *label = optarg + prefix_len;
109                                 global_flags.source_labels[channel_idx] = label;
110                         } else {
111                                 fprintf(stderr, "Invalid source label format (must be on the form NUM:LABEL)\n");
112                                 exit(1);
113                         }
114                         break;
115                 }
116                 case OPTION_HTTP_PORT:
117                         global_flags.http_port = atoi(optarg);
118                         break;
119                 case OPTION_TALLY_URL:
120                         global_flags.tally_url = optarg;
121                         break;
122                 case OPTION_CUE_IN_POINT_PADDING:
123                         global_flags.cue_in_point_padding_seconds = atof(optarg);
124                         global_flags.cue_in_point_padding_set = true;
125                         break;
126                 case OPTION_CUE_OUT_POINT_PADDING:
127                         global_flags.cue_out_point_padding_seconds = atof(optarg);
128                         global_flags.cue_out_point_padding_set = true;
129                         break;
130                 case OPTION_MIDI_MAPPING:
131                         global_flags.midi_mapping_filename = optarg;
132                         break;
133                 case OPTION_HELP:
134                         usage();
135                         exit(0);
136                 default:
137                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
138                         fprintf(stderr, "\n");
139                         usage();
140                         exit(1);
141                 }
142         }
143
144         if (global_flags.interpolation_quality < 0 || global_flags.interpolation_quality > 4) {
145                 fprintf(stderr, "Interpolation quality must be 0, 1, 2, 3 or 4.\n");
146                 usage();
147                 exit(1);
148         }
149         if (global_flags.cue_in_point_padding_seconds < 0.0 ||
150             global_flags.cue_out_point_padding_seconds < 0.0) {
151                 fprintf(stderr, "Cue point padding cannot be negative.\n");
152                 usage();
153                 exit(1);
154         }
155 }