]> git.sesse.net Git - nageru/blob - flags.cpp
Make it possible to set width/height on the command line, instead of compiling it...
[nageru] / flags.cpp
1 #include "flags.h"
2
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <utility>
9
10 using namespace std;
11
12 Flags global_flags;
13
14 // Long options that have no corresponding short option.
15 enum LongOption {
16         OPTION_HELP = 1000,
17         OPTION_MULTICHANNEL,
18         OPTION_MIDI_MAPPING,
19         OPTION_FAKE_CARDS_AUDIO,
20         OPTION_HTTP_UNCOMPRESSED_VIDEO,
21         OPTION_HTTP_X264_VIDEO,
22         OPTION_X264_PRESET,
23         OPTION_X264_TUNE,
24         OPTION_X264_SPEEDCONTROL,
25         OPTION_X264_SPEEDCONTROL_VERBOSE,
26         OPTION_X264_BITRATE,
27         OPTION_X264_VBV_BUFSIZE,
28         OPTION_X264_VBV_MAX_BITRATE,
29         OPTION_X264_PARAM,
30         OPTION_HTTP_MUX,
31         OPTION_HTTP_COARSE_TIMEBASE,
32         OPTION_HTTP_AUDIO_CODEC,
33         OPTION_HTTP_AUDIO_BITRATE,
34         OPTION_FLAT_AUDIO,
35         OPTION_GAIN_STAGING,
36         OPTION_DISABLE_LOCUT,
37         OPTION_ENABLE_LOCUT,
38         OPTION_DISABLE_GAIN_STAGING_AUTO,
39         OPTION_ENABLE_GAIN_STAGING_AUTO,
40         OPTION_DISABLE_COMPRESSOR,
41         OPTION_ENABLE_COMPRESSOR,
42         OPTION_DISABLE_LIMITER,
43         OPTION_ENABLE_LIMITER,
44         OPTION_DISABLE_MAKEUP_GAIN_AUTO,
45         OPTION_ENABLE_MAKEUP_GAIN_AUTO,
46         OPTION_DISABLE_ALSA_OUTPUT,
47         OPTION_NO_FLUSH_PBOS
48 };
49
50 void usage()
51 {
52         fprintf(stderr, "Usage: nageru [OPTION]...\n");
53         fprintf(stderr, "\n");
54         fprintf(stderr, "      --help                      print usage information\n");
55         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
56         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
57         fprintf(stderr, "  -c, --num-cards                 set number of input cards (default 2)\n");
58         fprintf(stderr, "  -t, --theme=FILE                choose theme (default theme.lua)\n");
59         fprintf(stderr, "  -I, --theme-dir=DIR             search for theme in this directory (can be given multiple times)\n");
60         fprintf(stderr, "  -v, --va-display=SPEC           VA-API device for H.264 encoding\n");
61         fprintf(stderr, "                                    ($DISPLAY spec or /dev/dri/render* path)\n");
62         fprintf(stderr, "  -m, --map-signal=SIGNAL,CARD    set a default card mapping (can be given multiple times)\n");
63         fprintf(stderr, "  -M, --input-mapping=FILE        start with the given audio input mapping (implies --multichannel)\n");
64         fprintf(stderr, "      --multichannel              start in multichannel audio mapping mode\n");
65         fprintf(stderr, "      --midi-mapping=FILE         start with the given MIDI controller mapping (implies --multichannel)\n");
66         fprintf(stderr, "      --fake-cards-audio          make fake (disconnected) cards output a simple tone\n");
67         fprintf(stderr, "      --http-uncompressed-video   send uncompressed NV12 video to HTTP clients\n");
68         fprintf(stderr, "      --http-x264-video           send x264-compressed video to HTTP clients\n");
69         fprintf(stderr, "      --x264-preset               x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
70         fprintf(stderr, "      --x264-tune                 x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
71         fprintf(stderr, "      --x264-speedcontrol         try to match x264 preset to available CPU speed\n");
72         fprintf(stderr, "      --x264-speedcontrol-verbose  output speedcontrol debugging statistics\n");
73         fprintf(stderr, "      --x264-bitrate              x264 bitrate (in kilobit/sec, default %d)\n",
74                 DEFAULT_X264_OUTPUT_BIT_RATE);
75         fprintf(stderr, "      --x264-vbv-bufsize          x264 VBV size (in kilobits, 0 = one-frame VBV,\n");
76         fprintf(stderr, "                                  default: same as --x264-bitrate, that is, one-second VBV)\n");
77         fprintf(stderr, "      --x264-vbv-max-bitrate      x264 local max bitrate (in kilobit/sec per --vbv-bufsize,\n");
78         fprintf(stderr, "                                  0 = no limit, default: same as --x264-bitrate, i.e., CBR)\n");
79         fprintf(stderr, "      --x264-param=NAME[,VALUE]   set any x264 parameter, for fine tuning\n");
80         fprintf(stderr, "      --http-mux=NAME             mux to use for HTTP streams (default " DEFAULT_STREAM_MUX_NAME ")\n");
81         fprintf(stderr, "      --http-audio-codec=NAME     audio codec to use for HTTP streams\n");
82         fprintf(stderr, "                                  (default is to use the same as for the recording)\n");
83         fprintf(stderr, "      --http-audio-bitrate=KBITS  audio codec bit rate to use for HTTP streams\n");
84         fprintf(stderr, "                                  (default is %d, ignored unless --http-audio-codec is set)\n",
85                 DEFAULT_AUDIO_OUTPUT_BIT_RATE / 1000);
86         fprintf(stderr, "      --http-coarse-timebase      use less timebase for HTTP (recommended for muxers\n");
87         fprintf(stderr, "                                  that handle large pts poorly, like e.g. MP4)\n");
88         fprintf(stderr, "      --flat-audio                start with most audio processing turned off\n");
89         fprintf(stderr, "                                    (can be overridden by e.g. --enable-limiter)\n");
90         fprintf(stderr, "      --gain-staging=DB           set initial gain staging to the given value\n");
91         fprintf(stderr, "                                    (--disable-gain-staging-auto)\n");
92         fprintf(stderr, "      --disable-locut             turn off locut filter (also --enable)\n");
93         fprintf(stderr, "      --disable-gain-staging-auto  turn off automatic gain staging (also --enable)\n");
94         fprintf(stderr, "      --disable-compressor        turn off regular compressor (also --enable)\n");
95         fprintf(stderr, "      --disable-limiter           turn off limiter (also --enable)\n");
96         fprintf(stderr, "      --disable-makeup-gain-auto  turn off auto-adjustment of final makeup gain (also --enable)\n");
97         fprintf(stderr, "      --disable-alsa-output       disable audio monitoring via ALSA\n");
98         fprintf(stderr, "      --no-flush-pbos             do not explicitly signal texture data uploads\n");
99         fprintf(stderr, "                                    (will give display corruption, but makes it\n");
100         fprintf(stderr, "                                    possible to run with apitrace in real time)\n");
101 }
102
103 void parse_flags(int argc, char * const argv[])
104 {
105         static const option long_options[] = {
106                 { "help", no_argument, 0, OPTION_HELP },
107                 { "width", required_argument, 0, 'w' },
108                 { "height", required_argument, 0, 'h' },
109                 { "num-cards", required_argument, 0, 'c' },
110                 { "theme", required_argument, 0, 't' },
111                 { "theme-dir", required_argument, 0, 'I' },
112                 { "map-signal", required_argument, 0, 'm' },
113                 { "input-mapping", required_argument, 0, 'M' },
114                 { "va-display", required_argument, 0, 'v' },
115                 { "multichannel", no_argument, 0, OPTION_MULTICHANNEL },
116                 { "midi-mapping", required_argument, 0, OPTION_MIDI_MAPPING },
117                 { "fake-cards-audio", no_argument, 0, OPTION_FAKE_CARDS_AUDIO },
118                 { "http-uncompressed-video", no_argument, 0, OPTION_HTTP_UNCOMPRESSED_VIDEO },
119                 { "http-x264-video", no_argument, 0, OPTION_HTTP_X264_VIDEO },
120                 { "x264-preset", required_argument, 0, OPTION_X264_PRESET },
121                 { "x264-tune", required_argument, 0, OPTION_X264_TUNE },
122                 { "x264-speedcontrol", no_argument, 0, OPTION_X264_SPEEDCONTROL },
123                 { "x264-speedcontrol-verbose", no_argument, 0, OPTION_X264_SPEEDCONTROL_VERBOSE },
124                 { "x264-bitrate", required_argument, 0, OPTION_X264_BITRATE },
125                 { "x264-vbv-bufsize", required_argument, 0, OPTION_X264_VBV_BUFSIZE },
126                 { "x264-vbv-max-bitrate", required_argument, 0, OPTION_X264_VBV_MAX_BITRATE },
127                 { "x264-param", required_argument, 0, OPTION_X264_PARAM },
128                 { "http-mux", required_argument, 0, OPTION_HTTP_MUX },
129                 { "http-coarse-timebase", no_argument, 0, OPTION_HTTP_COARSE_TIMEBASE },
130                 { "http-audio-codec", required_argument, 0, OPTION_HTTP_AUDIO_CODEC },
131                 { "http-audio-bitrate", required_argument, 0, OPTION_HTTP_AUDIO_BITRATE },
132                 { "flat-audio", no_argument, 0, OPTION_FLAT_AUDIO },
133                 { "gain-staging", required_argument, 0, OPTION_GAIN_STAGING },
134                 { "disable-locut", no_argument, 0, OPTION_DISABLE_LOCUT },
135                 { "enable-locut", no_argument, 0, OPTION_ENABLE_LOCUT },
136                 { "disable-gain-staging-auto", no_argument, 0, OPTION_DISABLE_GAIN_STAGING_AUTO },
137                 { "enable-gain-staging-auto", no_argument, 0, OPTION_ENABLE_GAIN_STAGING_AUTO },
138                 { "disable-compressor", no_argument, 0, OPTION_DISABLE_COMPRESSOR },
139                 { "enable-compressor", no_argument, 0, OPTION_ENABLE_COMPRESSOR },
140                 { "disable-limiter", no_argument, 0, OPTION_DISABLE_LIMITER },
141                 { "enable-limiter", no_argument, 0, OPTION_ENABLE_LIMITER },
142                 { "disable-makeup-gain-auto", no_argument, 0, OPTION_DISABLE_MAKEUP_GAIN_AUTO },
143                 { "enable-makeup-gain-auto", no_argument, 0, OPTION_ENABLE_MAKEUP_GAIN_AUTO },
144                 { "disable-alsa-output", no_argument, 0, OPTION_DISABLE_ALSA_OUTPUT },
145                 { "no-flush-pbos", no_argument, 0, OPTION_NO_FLUSH_PBOS },
146                 { 0, 0, 0, 0 }
147         };
148         vector<string> theme_dirs;
149         for ( ;; ) {
150                 int option_index = 0;
151                 int c = getopt_long(argc, argv, "c:t:I:v:m:M:w:h:", long_options, &option_index);
152
153                 if (c == -1) {
154                         break;
155                 }
156                 switch (c) {
157                 case 'w':
158                         global_flags.width = atoi(optarg);
159                         break;
160                 case 'h':
161                         global_flags.height = atoi(optarg);
162                         break;
163                 case 'c':
164                         global_flags.num_cards = atoi(optarg);
165                         break;
166                 case 't':
167                         global_flags.theme_filename = optarg;
168                         break;
169                 case 'I':
170                         theme_dirs.push_back(optarg);
171                         break;
172                 case 'm': {
173                         char *ptr = strchr(optarg, ',');
174                         if (ptr == nullptr) {
175                                 fprintf(stderr, "ERROR: Invalid argument '%s' to --map-signal (needs a signal and a card number, separated by comma)\n", optarg);
176                                 exit(1);
177                         }
178                         *ptr = '\0';
179                         const int signal_num = atoi(optarg);
180                         const int card_num = atoi(ptr + 1);
181                         if (global_flags.default_stream_mapping.count(signal_num)) {
182                                 fprintf(stderr, "ERROR: Signal %d already mapped to card %d\n",
183                                         signal_num, global_flags.default_stream_mapping[signal_num]);
184                                 exit(1);
185                         }
186                         global_flags.default_stream_mapping[signal_num] = card_num;
187                         break;
188                 }
189                 case 'M':
190                         global_flags.input_mapping_filename = optarg;
191                         break;
192                 case OPTION_MULTICHANNEL:
193                         global_flags.multichannel_mapping_mode = true;
194                         break;
195                 case 'v':
196                         global_flags.va_display = optarg;
197                         break;
198                 case OPTION_MIDI_MAPPING:
199                         global_flags.midi_mapping_filename = optarg;
200                         global_flags.multichannel_mapping_mode = true;
201                         break;
202                 case OPTION_FAKE_CARDS_AUDIO:
203                         global_flags.fake_cards_audio = true;
204                         break;
205                 case OPTION_HTTP_UNCOMPRESSED_VIDEO:
206                         global_flags.uncompressed_video_to_http = true;
207                         break;
208                 case OPTION_HTTP_MUX:
209                         global_flags.stream_mux_name = optarg;
210                         break;
211                 case OPTION_HTTP_COARSE_TIMEBASE:
212                         global_flags.stream_coarse_timebase = true;
213                         break;
214                 case OPTION_HTTP_AUDIO_CODEC:
215                         global_flags.stream_audio_codec_name = optarg;
216                         break;
217                 case OPTION_HTTP_AUDIO_BITRATE:
218                         global_flags.stream_audio_codec_bitrate = atoi(optarg) * 1000;
219                         break;
220                 case OPTION_HTTP_X264_VIDEO:
221                         global_flags.x264_video_to_http = true;
222                         break;
223                 case OPTION_X264_PRESET:
224                         global_flags.x264_preset = optarg;
225                         break;
226                 case OPTION_X264_TUNE:
227                         global_flags.x264_tune = optarg;
228                         break;
229                 case OPTION_X264_SPEEDCONTROL:
230                         global_flags.x264_speedcontrol = true;
231                         break;
232                 case OPTION_X264_SPEEDCONTROL_VERBOSE:
233                         global_flags.x264_speedcontrol_verbose = true;
234                         break;
235                 case OPTION_X264_BITRATE:
236                         global_flags.x264_bitrate = atoi(optarg);
237                         break;
238                 case OPTION_X264_VBV_BUFSIZE:
239                         global_flags.x264_vbv_buffer_size = atoi(optarg);
240                         break;
241                 case OPTION_X264_VBV_MAX_BITRATE:
242                         global_flags.x264_vbv_max_bitrate = atoi(optarg);
243                         break;
244                 case OPTION_X264_PARAM:
245                         global_flags.x264_extra_param.push_back(optarg);
246                         break;
247                 case OPTION_FLAT_AUDIO:
248                         // If --flat-audio is given, turn off everything that messes with the sound,
249                         // except the final makeup gain.
250                         global_flags.locut_enabled = false;
251                         global_flags.gain_staging_auto = false;
252                         global_flags.compressor_enabled = false;
253                         global_flags.limiter_enabled = false;
254                         break;
255                 case OPTION_GAIN_STAGING:
256                         global_flags.initial_gain_staging_db = atof(optarg);
257                         global_flags.gain_staging_auto = false;
258                         break;
259                 case OPTION_DISABLE_LOCUT:
260                         global_flags.locut_enabled = false;
261                         break;
262                 case OPTION_ENABLE_LOCUT:
263                         global_flags.locut_enabled = true;
264                         break;
265                 case OPTION_DISABLE_GAIN_STAGING_AUTO:
266                         global_flags.gain_staging_auto = false;
267                         break;
268                 case OPTION_ENABLE_GAIN_STAGING_AUTO:
269                         global_flags.gain_staging_auto = true;
270                         break;
271                 case OPTION_DISABLE_COMPRESSOR:
272                         global_flags.compressor_enabled = false;
273                         break;
274                 case OPTION_ENABLE_COMPRESSOR:
275                         global_flags.compressor_enabled = true;
276                         break;
277                 case OPTION_DISABLE_LIMITER:
278                         global_flags.limiter_enabled = false;
279                         break;
280                 case OPTION_ENABLE_LIMITER:
281                         global_flags.limiter_enabled = true;
282                         break;
283                 case OPTION_DISABLE_MAKEUP_GAIN_AUTO:
284                         global_flags.final_makeup_gain_auto = false;
285                         break;
286                 case OPTION_ENABLE_MAKEUP_GAIN_AUTO:
287                         global_flags.final_makeup_gain_auto = true;
288                         break;
289                 case OPTION_DISABLE_ALSA_OUTPUT:
290                         global_flags.enable_alsa_output = false;
291                         break;
292                 case OPTION_NO_FLUSH_PBOS:
293                         global_flags.flush_pbos = false;
294                         break;
295                 case OPTION_HELP:
296                         usage();
297                         exit(0);
298                 default:
299                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
300                         fprintf(stderr, "\n");
301                         usage();
302                         exit(1);
303                 }
304         }
305
306         if (global_flags.uncompressed_video_to_http &&
307             global_flags.x264_video_to_http) {
308                 fprintf(stderr, "ERROR: --http-uncompressed-video and --http-x264-video are mutually incompatible\n");
309                 exit(1);
310         }
311         if (global_flags.num_cards <= 0) {
312                 fprintf(stderr, "ERROR: --num-cards must be at least 1\n");
313                 exit(1);
314         }
315         if (global_flags.x264_speedcontrol) {
316                 if (!global_flags.x264_preset.empty() && global_flags.x264_preset != "faster") {
317                         fprintf(stderr, "WARNING: --x264-preset is overridden by --x264-speedcontrol (implicitly uses \"faster\" as base preset)\n");
318                 }
319                 global_flags.x264_preset = "faster";
320         } else if (global_flags.x264_preset.empty()) {
321                 global_flags.x264_preset = X264_DEFAULT_PRESET;
322         }
323         if (!theme_dirs.empty()) {
324                 global_flags.theme_dirs = theme_dirs;
325         }
326
327         // In reality, we could probably do with any even value (we subsample
328         // by two in some places), but it's better to be on the safe side
329         // wrt. video codecs and such. (I'd set 16 if I could, but 1080 isn't
330         // divisible by 16.)
331         if (global_flags.width <= 0 || (global_flags.width % 8) != 0 ||
332             global_flags.height <= 0 || (global_flags.height % 8) != 0) {
333                 fprintf(stderr, "ERROR: --width and --height must be positive integers divisible by 8\n");
334                 exit(1);
335         }
336
337         for (pair<int, int> mapping : global_flags.default_stream_mapping) {
338                 if (mapping.second >= global_flags.num_cards) {
339                         fprintf(stderr, "ERROR: Signal %d mapped to card %d, which doesn't exist (try adjusting --num-cards)\n",
340                                 mapping.first, mapping.second);
341                         exit(1);
342                 }
343         }
344 }